From 4c6b65c7a7dc581c7ce67de709a61ad2f94de184 Mon Sep 17 00:00:00 2001 From: wengchangcheng Date: Thu, 2 Jun 2022 11:28:13 +0800 Subject: [PATCH] Descriptor: fix proxy bug details:SetMethod always is nullptr, set right method: CallJsProxy issue: https://gitee.com/openharmony/ark_js_runtime/issues/I5AIBR Signed-off-by: wengchangcheng Change-Id: Ia0fa992d302b9183a20faafeffbfce31f01cd072 --- ecmascript/file_loader.cpp | 4 ++-- ecmascript/js_function.h | 5 ----- ecmascript/js_proxy.h | 5 ----- ecmascript/object_factory.cpp | 28 ++++++++++-------------- ecmascript/tests/native_pointer_test.cpp | 2 +- test/moduletest/BUILD.gn | 2 ++ test/moduletest/proxy/BUILD.gn | 18 +++++++++++++++ test/moduletest/proxy/expect_output.txt | 16 ++++++++++++++ test/moduletest/proxy/proxy.js | 28 ++++++++++++++++++++++++ 9 files changed, 79 insertions(+), 29 deletions(-) create mode 100644 test/moduletest/proxy/BUILD.gn create mode 100644 test/moduletest/proxy/expect_output.txt create mode 100644 test/moduletest/proxy/proxy.js diff --git a/ecmascript/file_loader.cpp b/ecmascript/file_loader.cpp index 814b76b9db..ef23df778f 100644 --- a/ecmascript/file_loader.cpp +++ b/ecmascript/file_loader.cpp @@ -262,7 +262,7 @@ void FileLoader::UpdateJSMethods(JSHandle mainFunc, const JSPandaFil auto mainEntry = GetAOTFuncEntry(fileHash, mainFuncMethodId); // 1 : default para number JSMethod *mainMethod = factory_->NewMethodForAOTFunction(reinterpret_cast(mainEntry), 1); - mainFunc->SetCallTarget(thread, mainMethod); + mainFunc->SetMethod(mainMethod); mainFunc->SetCodeEntry(reinterpret_cast(mainEntry)); const CUnorderedMap &constpoolMap = jsPandaFile->GetConstpoolMap(); @@ -277,7 +277,7 @@ void FileLoader::UpdateJSMethods(JSHandle mainFunc, const JSPandaFil auto codeEntry = GetAOTFuncEntry(fileHash, it.first); JSMethod *curMethod = factory_->NewMethodForAOTFunction(reinterpret_cast(codeEntry), 1); auto curFunction = JSFunction::Cast(curPool->GetObjectFromCache(id).GetTaggedObject()); - curFunction->SetCallTarget(thread, curMethod); + curFunction->SetMethod(curMethod); curFunction->SetCodeEntry(reinterpret_cast(codeEntry)); } } diff --git a/ecmascript/js_function.h b/ecmascript/js_function.h index 663c6e0cf4..d9c25f827b 100644 --- a/ecmascript/js_function.h +++ b/ecmascript/js_function.h @@ -39,11 +39,6 @@ public: const JSHandle &name, const JSHandle &prefix); static JSHandle GetFunctionName(JSThread *thread, const JSHandle &func); - void SetCallTarget([[maybe_unused]] const JSThread *thread, JSMethod *p) - { - SetMethod(p); - } - static constexpr size_t METHOD_OFFSET = JSObject::SIZE; ACCESSORS_NATIVE_FIELD(Method, JSMethod, METHOD_OFFSET, CODE_ENTRY_OFFSET) ACCESSORS_PRIMITIVE_FIELD(CodeEntry, uintptr_t, CODE_ENTRY_OFFSET, LAST_OFFSET) diff --git a/ecmascript/js_proxy.h b/ecmascript/js_proxy.h index d2d33d465a..14f474e8fd 100644 --- a/ecmascript/js_proxy.h +++ b/ecmascript/js_proxy.h @@ -77,11 +77,6 @@ public: GetClass()->SetConstructor(constructor); } - void SetCallTarget([[maybe_unused]] const JSThread *thread, JSMethod *p) - { - SetMethod(p); - } - JSHandle GetSourceTarget(JSThread *thread) const; // ES6 9.5.13 [[Call]] (thisArgument, argumentsList) diff --git a/ecmascript/object_factory.cpp b/ecmascript/object_factory.cpp index cf58710370..b7a4370d0c 100644 --- a/ecmascript/object_factory.cpp +++ b/ecmascript/object_factory.cpp @@ -1344,7 +1344,7 @@ JSHandle ObjectFactory::NewJSFunctionByDynClass(JSMethod *method, co clazz->SetCallable(true); clazz->SetExtensible(true); JSFunction::InitializeJSFunction(thread_, function, kind); - function->SetCallTarget(thread_, method); + function->SetMethod(method); return function; } @@ -1355,7 +1355,7 @@ JSHandle ObjectFactory::NewJSFunctionByDynClass(const void *func, co clazz->SetCallable(true); clazz->SetExtensible(true); JSFunction::InitializeJSFunction(thread_, function, kind); - function->SetCallTarget(thread_, NewMethodForNativeFunction(func)); + function->SetMethod(NewMethodForNativeFunction(func)); return function; } @@ -1403,7 +1403,7 @@ JSHandle ObjectFactory::NewJSBoundFunction(const JSHandleSetConstructor(true); } JSMethod *method = GetMethodByIndex(MethodIndex::BUILTINS_GLOBAL_CALL_JS_BOUND_FUNCTION); - bundleFunction->SetCallTarget(thread_, method); + bundleFunction->SetMethod(method); return bundleFunction; } @@ -1416,7 +1416,7 @@ JSHandle ObjectFactory::NewJSIntlBoundFunction(MethodIndex intlBoundFunc->SetNumberFormat(JSTaggedValue::Undefined()); intlBoundFunc->SetDateTimeFormat(JSTaggedValue::Undefined()); intlBoundFunc->SetCollator(JSTaggedValue::Undefined()); - intlBoundFunc->SetCallTarget(thread_, GetMethodByIndex(idx)); + intlBoundFunc->SetMethod(GetMethodByIndex(idx)); JSHandle function = JSHandle::Cast(intlBoundFunc); JSFunction::InitializeJSFunction(thread_, function, FunctionKind::NORMAL_FUNCTION); JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(functionLength)); @@ -1437,7 +1437,7 @@ JSHandle ObjectFactory::NewJSProxyRevocFunction(const JSHa JSHandle revocFunction = JSHandle::Cast(NewJSObject(dynclass)); revocFunction->SetRevocableProxy(JSTaggedValue::Undefined()); revocFunction->SetRevocableProxy(thread_, proxy); - revocFunction->SetCallTarget(thread_, GetMethodByIndex(MethodIndex::BUILTINS_PROXY_INVALIDATE_PROXY_FUNCTION)); + revocFunction->SetMethod(GetMethodByIndex(MethodIndex::BUILTINS_PROXY_INVALIDATE_PROXY_FUNCTION)); JSHandle function = JSHandle::Cast(revocFunction); JSFunction::InitializeJSFunction(thread_, function, FunctionKind::NORMAL_FUNCTION); JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(0)); @@ -1457,7 +1457,7 @@ JSHandle ObjectFactory::NewJSAsyncAwaitStatusFunctio JSHandle::Cast(NewJSObject(dynclass)); awaitFunction->SetAsyncContext(JSTaggedValue::Undefined()); JSFunction::InitializeJSFunction(thread_, JSHandle::Cast(awaitFunction)); - awaitFunction->SetCallTarget(thread_, GetMethodByIndex(idx)); + awaitFunction->SetMethod(GetMethodByIndex(idx)); return awaitFunction; } @@ -1468,7 +1468,7 @@ JSHandle ObjectFactory::NewJSGeneratorFunction(JSMethod *method) JSHandle dynclass = JSHandle::Cast(env->GetGeneratorFunctionClass()); JSHandle generatorFunc = JSHandle::Cast(NewJSObject(dynclass)); JSFunction::InitializeJSFunction(thread_, generatorFunc, FunctionKind::GENERATOR_FUNCTION); - generatorFunc->SetCallTarget(thread_, method); + generatorFunc->SetMethod(method); return generatorFunc; } @@ -1492,7 +1492,7 @@ JSHandle ObjectFactory::NewAsyncFunction(JSMethod *method) JSHandle dynclass = JSHandle::Cast(env->GetAsyncFunctionClass()); JSHandle asyncFunction = JSHandle::Cast(NewJSObject(dynclass)); JSFunction::InitializeJSFunction(thread_, JSHandle::Cast(asyncFunction)); - asyncFunction->SetCallTarget(thread_, method); + asyncFunction->SetMethod(method); return asyncFunction; } @@ -1817,11 +1817,7 @@ JSHandle ObjectFactory::NewJSProxy(const JSHandle &targe } JSHandle proxy(thread_, header); - JSMethod *method = nullptr; - if (target->IsCallable()) { - JSMethod *nativeMethod = GetMethodByIndex(MethodIndex::BUILTINS_GLOBAL_CALL_JS_PROXY); - proxy->SetCallTarget(thread_, nativeMethod); - } + JSMethod *method = GetMethodByIndex(MethodIndex::BUILTINS_GLOBAL_CALL_JS_PROXY); proxy->SetMethod(method); proxy->SetTarget(thread_, target.GetTaggedValue()); @@ -2318,7 +2314,7 @@ JSHandle ObjectFactory::CreateJSPromiseReactionsFunc JSHandle::Cast(NewJSObject(dynclass)); reactionsFunction->SetPromise(thread_, JSTaggedValue::Hole()); reactionsFunction->SetAlreadyResolved(thread_, JSTaggedValue::Hole()); - reactionsFunction->SetCallTarget(thread_, GetMethodByIndex(idx)); + reactionsFunction->SetMethod(GetMethodByIndex(idx)); JSHandle function = JSHandle::Cast(reactionsFunction); JSFunction::InitializeJSFunction(thread_, function); JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(1)); @@ -2332,7 +2328,7 @@ JSHandle ObjectFactory::CreateJSPromiseExecutorFuncti JSHandle executorFunction = JSHandle::Cast(NewJSObject(dynclass)); executorFunction->SetCapability(thread_, JSTaggedValue::Hole()); - executorFunction->SetCallTarget(thread_, GetMethodByIndex(MethodIndex::BUILTINS_PROMISE_HANDLER_EXECUTOR)); + executorFunction->SetMethod(GetMethodByIndex(MethodIndex::BUILTINS_PROMISE_HANDLER_EXECUTOR)); executorFunction->SetCapability(thread_, JSTaggedValue::Undefined()); JSHandle function = JSHandle::Cast(executorFunction); JSFunction::InitializeJSFunction(thread_, function, FunctionKind::NORMAL_FUNCTION); @@ -2347,7 +2343,7 @@ JSHandle ObjectFactory::NewJSPromiseAllResol JSHandle function = JSHandle::Cast(NewJSObject(dynclass)); JSFunction::InitializeJSFunction(thread_, JSHandle::Cast(function)); - function->SetCallTarget(thread_, GetMethodByIndex(MethodIndex::BUILTINS_PROMISE_HANDLER_RESOLVE_ELEMENT_FUNCTION)); + function->SetMethod(GetMethodByIndex(MethodIndex::BUILTINS_PROMISE_HANDLER_RESOLVE_ELEMENT_FUNCTION)); function->SetIndex(JSTaggedValue::Undefined()); function->SetValues(JSTaggedValue::Undefined()); function->SetCapabilities(JSTaggedValue::Undefined()); diff --git a/ecmascript/tests/native_pointer_test.cpp b/ecmascript/tests/native_pointer_test.cpp index f7660649ff..3700f3ae07 100644 --- a/ecmascript/tests/native_pointer_test.cpp +++ b/ecmascript/tests/native_pointer_test.cpp @@ -67,7 +67,7 @@ HWTEST_F_L0(NativePointerTest, Print) EXPECT_TRUE(*jsFunction != nullptr); JSMethod *target = factory->NewMethodForNativeFunction(nullptr); - jsFunction->SetCallTarget(thread, target); + jsFunction->SetMethod(target); // run cpp methed 'Print' ASSERT_EQ(target, jsFunction->GetCallTarget()); diff --git a/test/moduletest/BUILD.gn b/test/moduletest/BUILD.gn index 57a2b14f59..f0cfc60371 100644 --- a/test/moduletest/BUILD.gn +++ b/test/moduletest/BUILD.gn @@ -37,6 +37,7 @@ group("ark_js_moduletest") { "newobjdynrange:newobjdynrangeAction", "objectcloneproperties:objectclonepropertiesAction", "promise:promiseAction", + "proxy:proxyAction", "regexpcallthrow:regexpcallthrowAction", "spreadoperator:spreadoperatorAction", "stackoverflow:stackoverflowAction", @@ -77,6 +78,7 @@ group("ark_asm_test") { "newobjdynrange:newobjdynrangeAsmAction", "objectcloneproperties:objectclonepropertiesAsmAction", "promise:promiseAsmAction", + "proxy:proxyAsmAction", "regexpcallthrow:regexpcallthrowAsmAction", "spreadoperator:spreadoperatorAsmAction", "stackoverflow:stackoverflowAsmAction", diff --git a/test/moduletest/proxy/BUILD.gn b/test/moduletest/proxy/BUILD.gn new file mode 100644 index 0000000000..cd3688f254 --- /dev/null +++ b/test/moduletest/proxy/BUILD.gn @@ -0,0 +1,18 @@ +# Copyright (c) 2021 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("//ark/js_runtime/test/test_helper.gni") + +host_moduletest_action("proxy") { + deps = [] +} diff --git a/test/moduletest/proxy/expect_output.txt b/test/moduletest/proxy/expect_output.txt new file mode 100644 index 0000000000..d9b6bf26f9 --- /dev/null +++ b/test/moduletest/proxy/expect_output.txt @@ -0,0 +1,16 @@ +# Copyright (c) 2021 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/proxy/proxy.js b/test/moduletest/proxy/proxy.js new file mode 100644 index 0000000000..d0ef4d4ec0 --- /dev/null +++ b/test/moduletest/proxy/proxy.js @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2021 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. + */ + +function add(a, b) { + return a + b +} + +let addProxy = new Proxy(add, { + apply: function(target, thisObj, args) { + return target.apply(thisObj, args) + } +}) + +for (var a = 1; a < 4; a++) { + print(addProxy(a, 12345 * a) == add(12345 * a, a)) +} -- Gitee