From 53156998ecd3a058539b8a96133f64865ba829e5 Mon Sep 17 00:00:00 2001 From: yanpeng Date: Mon, 18 Dec 2023 17:18:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9BuiltinsString::Match?= =?UTF-8?q?=EF=BC=8CReplace=EF=BC=8CSearch=20=E7=AD=89=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yanpeng Change-Id: I0607ff10ad301385ac2933b847e5b1b14eb90ab2 --- ecmascript/builtins/builtins_string.cpp | 63 +++++++++---------- test/moduletest/BUILD.gn | 1 + test/moduletest/regresssstring/BUILD.gn | 18 ++++++ .../regresssstring/expect_output.txt | 16 +++++ .../regresssstring/regresssstring.js | 39 ++++++++++++ 5 files changed, 102 insertions(+), 35 deletions(-) create mode 100755 test/moduletest/regresssstring/BUILD.gn create mode 100755 test/moduletest/regresssstring/expect_output.txt create mode 100755 test/moduletest/regresssstring/regresssstring.js diff --git a/ecmascript/builtins/builtins_string.cpp b/ecmascript/builtins/builtins_string.cpp index 7268a4774f..4a63620238 100644 --- a/ecmascript/builtins/builtins_string.cpp +++ b/ecmascript/builtins/builtins_string.cpp @@ -639,17 +639,15 @@ JSTaggedValue BuiltinsString::Match(EcmaRuntimeCallInfo *argv) } } if (!regexp->IsUndefined() && !regexp->IsNull()) { - if (regexp->IsECMAObject()) { - JSHandle matcher = JSObject::GetMethod(thread, regexp, matchTag); + JSHandle matcher = JSObject::GetMethod(thread, regexp, matchTag); + RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); + if (!matcher->IsUndefined()) { + ASSERT(matcher->IsJSFunction()); + EcmaRuntimeCallInfo *info = + EcmaInterpreter::NewRuntimeCallInfo(thread, matcher, regexp, undefined, 1); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); - if (!matcher->IsUndefined()) { - ASSERT(matcher->IsJSFunction()); - EcmaRuntimeCallInfo *info = - EcmaInterpreter::NewRuntimeCallInfo(thread, matcher, regexp, undefined, 1); - RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); - info->SetCallArg(thisTag.GetTaggedValue()); - return JSFunction::Call(info); - } + info->SetCallArg(thisTag.GetTaggedValue()); + return JSFunction::Call(info); } } JSHandle thisVal = JSTaggedValue::ToString(thread, thisTag); @@ -706,20 +704,17 @@ JSTaggedValue BuiltinsString::MatchAll(EcmaRuntimeCallInfo *argv) } } - if (regexp->IsECMAObject()) { - // c. c. Let matcher be ? GetMethod(regexp, @@matchAll). - // d. d. If matcher is not undefined, then - JSHandle matcher = JSObject::GetMethod(thread, regexp, matchAllTag); + // c. Let matcher be ? GetMethod(regexp, @@matchAll). + // d. If matcher is not undefined, then + JSHandle matcher = JSObject::GetMethod(thread, regexp, matchAllTag); + RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); + if (!matcher->IsUndefined()) { + // i. Return ? Call(matcher, regexp, « O »). + EcmaRuntimeCallInfo *info = + EcmaInterpreter::NewRuntimeCallInfo(thread, matcher, regexp, undefined, 1); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); - if (!matcher->IsUndefined()) { - ASSERT(matcher->IsJSFunction()); - // i. i. Return ? Call(matcher, regexp, « O »). - EcmaRuntimeCallInfo *info = - EcmaInterpreter::NewRuntimeCallInfo(thread, matcher, regexp, undefined, 1); - RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); - info->SetCallArg(thisTag.GetTaggedValue()); - return JSFunction::Call(info); - } + info->SetCallArg(thisTag.GetTaggedValue()); + return JSFunction::Call(info); } } // 3. Let S be ? ToString(O). @@ -887,7 +882,7 @@ JSTaggedValue BuiltinsString::Replace(EcmaRuntimeCallInfo *argv) } // If searchValue is neither undefined nor null, then - if (searchTag->IsECMAObject()) { + if (!searchTag->IsUndefined() && !searchTag->IsNull()) { JSHandle replaceKey = env->GetReplaceSymbol(); // Let replacer be GetMethod(searchValue, @@replace). JSHandle replaceMethod = JSObject::GetMethod(thread, searchTag, replaceKey); @@ -1320,17 +1315,15 @@ JSTaggedValue BuiltinsString::Search(EcmaRuntimeCallInfo *argv) JSHandle searchTag = thread->GetEcmaVM()->GetGlobalEnv()->GetSearchSymbol(); JSHandle undefined = globalConst->GetHandledUndefined(); if (!regexp->IsUndefined() && !regexp->IsNull()) { - if (regexp->IsECMAObject()) { - JSHandle searcher = JSObject::GetMethod(thread, regexp, searchTag); + JSHandle searcher = JSObject::GetMethod(thread, regexp, searchTag); + RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); + if (!searcher->IsUndefined()) { + ASSERT(searcher->IsJSFunction()); + EcmaRuntimeCallInfo *info = + EcmaInterpreter::NewRuntimeCallInfo(thread, searcher, regexp, undefined, 1); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); - if (!searcher->IsUndefined()) { - ASSERT(searcher->IsJSFunction()); - EcmaRuntimeCallInfo *info = - EcmaInterpreter::NewRuntimeCallInfo(thread, searcher, regexp, undefined, 1); - RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); - info->SetCallArg(thisTag.GetTaggedValue()); - return JSFunction::Call(info); - } + info->SetCallArg(thisTag.GetTaggedValue()); + return JSFunction::Call(info); } } JSHandle thisVal = JSTaggedValue::ToString(thread, thisTag); @@ -1425,7 +1418,7 @@ JSTaggedValue BuiltinsString::Split(EcmaRuntimeCallInfo *argv) } // If separator is neither undefined nor null, then - if (seperatorTag->IsECMAObject()) { + if (!seperatorTag->IsUndefined() && !seperatorTag->IsNull()) { JSHandle splitKey = env->GetSplitSymbol(); // Let splitter be GetMethod(separator, @@split). JSHandle splitter = JSObject::GetMethod(thread, seperatorTag, splitKey); diff --git a/test/moduletest/BUILD.gn b/test/moduletest/BUILD.gn index dcadd7f51f..a53b84e4f6 100644 --- a/test/moduletest/BUILD.gn +++ b/test/moduletest/BUILD.gn @@ -120,6 +120,7 @@ group("ark_js_moduletest") { "regexpflagd", "regress", "regressdefineproperty", + "regresssstring", "require", "setobjectwithproto", "spreadoperator", diff --git a/test/moduletest/regresssstring/BUILD.gn b/test/moduletest/regresssstring/BUILD.gn new file mode 100755 index 0000000000..9a370909d7 --- /dev/null +++ b/test/moduletest/regresssstring/BUILD.gn @@ -0,0 +1,18 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//arkcompiler/ets_runtime/test/test_helper.gni") + +host_moduletest_action("regresssstring") { + deps = [] +} diff --git a/test/moduletest/regresssstring/expect_output.txt b/test/moduletest/regresssstring/expect_output.txt new file mode 100755 index 0000000000..b0049c774d --- /dev/null +++ b/test/moduletest/regresssstring/expect_output.txt @@ -0,0 +1,16 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +true +true +true diff --git a/test/moduletest/regresssstring/regresssstring.js b/test/moduletest/regresssstring/regresssstring.js new file mode 100755 index 0000000000..452b2bc166 --- /dev/null +++ b/test/moduletest/regresssstring/regresssstring.js @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +const str = "Umbridge has been reading your mail, Harry." + +{ + let monkey_called = false; + str.__proto__.__proto__[Symbol.replace] = + () => { monkey_called = true; }; + str.replace(str); + print(monkey_called); +} + +{ + let monkey_called = false; + str.__proto__.__proto__[Symbol.search] = + () => { monkey_called = true; }; + str.search(str); + print(monkey_called); +} + +{ + let monkey_called = false; + str.__proto__.__proto__[Symbol.match] = + () => { monkey_called = true; }; + str.match(str); + print(monkey_called); +}