diff --git a/ecmascript/builtins/builtins_string.cpp b/ecmascript/builtins/builtins_string.cpp index 325a2dc492bf1c7c0542376086dd5a9211aeb4da..3edb6e60545e2d8f8e0e7407c34abbadf817033e 100644 --- a/ecmascript/builtins/builtins_string.cpp +++ b/ecmascript/builtins/builtins_string.cpp @@ -267,7 +267,7 @@ JSTaggedValue BuiltinsString::CharAt(EcmaRuntimeCallInfo *argv) return factory->GetEmptyString().GetTaggedValue(); } pos = posVal.ToInt32(); - } + } if (pos < 0 || pos >= thisLen) { return factory->GetEmptyString().GetTaggedValue(); } @@ -514,16 +514,19 @@ JSTaggedValue BuiltinsString::LastIndexOf(EcmaRuntimeCallInfo *argv) JSHandle posTag = BuiltinsString::GetCallArg(argv, 1); JSTaggedNumber posVal = JSTaggedValue::ToInteger(thread, posTag); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); - if (std::isnan(JSTaggedValue::ToNumber(thread, posTag).GetNumber())) { + if (std::isnan(JSTaggedValue::ToNumber(thread, posTag).GetNumber()) + || posVal.GetNumber() == BuiltinsNumber::POSITIVE_INFINITY) { pos = thisLen; } else { - pos = posVal.ToInt32(); + // pos = posVal.ToInt32(); + pos = base::NumberHelper::DoubleInRangeInt32(posVal.GetNumber()); } RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); } pos = std::min(std::max(pos, 0), thisLen); int32_t res = EcmaStringAccessor::LastIndexOf(thread->GetEcmaVM(), thisHandle, searchHandle, pos); - if (res >= 0 && res < thisLen) { + if ((res >= 0 && res < thisLen) + || (EcmaStringAccessor(searchHandle).GetLength() == 0 && res == thisLen)) { return GetTaggedInt(res); } res = -1;