14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_proxy.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/ecma_runtime_call_info.h" 194514f5e3Sopenharmony_ci#include "ecmascript/ecma_string.h" 204514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h" 214514f5e3Sopenharmony_ci#include "ecmascript/global_env.h" 224514f5e3Sopenharmony_ci#include "ecmascript/js_array.h" 234514f5e3Sopenharmony_ci#include "ecmascript/js_handle.h" 244514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value-inl.h" 254514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h" 264514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h" 274514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 284514f5e3Sopenharmony_ci 294514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 304514f5e3Sopenharmony_ciusing namespace panda::ecmascript::builtins; 314514f5e3Sopenharmony_ci 324514f5e3Sopenharmony_cinamespace panda::test { 334514f5e3Sopenharmony_ciclass BuiltinsProxyTest : public BaseTestWithScope<false> { 344514f5e3Sopenharmony_ci}; 354514f5e3Sopenharmony_ci 364514f5e3Sopenharmony_ciJSHandle<JSObject> BuiltinsTestProxyCreate(JSThread *thread) 374514f5e3Sopenharmony_ci{ 384514f5e3Sopenharmony_ci EcmaVM *ecmaVM = thread->GetEcmaVM(); 394514f5e3Sopenharmony_ci JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv(); 404514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> objFun(globalEnv->GetObjectFunction()); 414514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 424514f5e3Sopenharmony_ci 434514f5e3Sopenharmony_ci JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun); 444514f5e3Sopenharmony_ci return obj; 454514f5e3Sopenharmony_ci} 464514f5e3Sopenharmony_ci 474514f5e3Sopenharmony_ci// 26.2.1.1 Proxy( [ value ] ) 484514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsProxyTest, ProxyConstructor) 494514f5e3Sopenharmony_ci{ 504514f5e3Sopenharmony_ci JSHandle<JSObject> target = BuiltinsTestProxyCreate(thread); 514514f5e3Sopenharmony_ci JSHandle<JSObject> handler = BuiltinsTestProxyCreate(thread); 524514f5e3Sopenharmony_ci 534514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Null(), 8); 544514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 554514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 564514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue()); 574514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(1, handler.GetTaggedValue()); 584514f5e3Sopenharmony_ci 594514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 604514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsProxy::ProxyConstructor(ecmaRuntimeCallInfo); 614514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsECMAObject()); 624514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> resultHandle(thread, result); 634514f5e3Sopenharmony_ci EXPECT_TRUE(resultHandle->IsJSProxy()); 644514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 654514f5e3Sopenharmony_ci} 664514f5e3Sopenharmony_ci 674514f5e3Sopenharmony_ci// 26.2.2.1 Proxy.revocable ( target, handler ) 684514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsProxyTest, Revocable) 694514f5e3Sopenharmony_ci{ 704514f5e3Sopenharmony_ci JSHandle<JSObject> target = BuiltinsTestProxyCreate(thread); 714514f5e3Sopenharmony_ci JSHandle<JSObject> handler = BuiltinsTestProxyCreate(thread); 724514f5e3Sopenharmony_ci 734514f5e3Sopenharmony_ci JSHandle<GlobalEnv> globalEnv = thread->GetEcmaVM()->GetGlobalEnv(); 744514f5e3Sopenharmony_ci JSHandle<JSFunction> proxyFun(globalEnv->GetProxyFunction()); 754514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(thread->GetEcmaVM()->GetFactory()->NewFromASCII("prop")); 764514f5e3Sopenharmony_ci PropertyDescriptor desc(thread); 774514f5e3Sopenharmony_ci desc.SetWritable(false); 784514f5e3Sopenharmony_ci JSObject::DefineOwnProperty(thread, handler, key, desc); 794514f5e3Sopenharmony_ci 804514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); 814514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 824514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 834514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue()); 844514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(1, handler.GetTaggedValue()); 854514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetNewTarget(JSTaggedValue(*proxyFun)); 864514f5e3Sopenharmony_ci 874514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 884514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsProxy::Revocable(ecmaRuntimeCallInfo); 894514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsECMAObject()); 904514f5e3Sopenharmony_ci JSHandle<JSObject> resultHandle(thread, result); 914514f5e3Sopenharmony_ci 924514f5e3Sopenharmony_ci auto globalConst = thread->GlobalConstants(); 934514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> proxyKey = globalConst->GetHandledProxyString(); 944514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> revokeKey = globalConst->GetHandledRevokeString(); 954514f5e3Sopenharmony_ci 964514f5e3Sopenharmony_ci JSHandle<TaggedArray> keys = JSObject::GetOwnPropertyKeys(thread, resultHandle); 974514f5e3Sopenharmony_ci bool pflag = false; 984514f5e3Sopenharmony_ci bool rflag = false; 994514f5e3Sopenharmony_ci for (uint32_t i = 0; i < keys->GetLength(); i++) { 1004514f5e3Sopenharmony_ci if (JSTaggedValue::SameValue(keys->Get(i), proxyKey.GetTaggedValue())) { 1014514f5e3Sopenharmony_ci pflag = true; 1024514f5e3Sopenharmony_ci } 1034514f5e3Sopenharmony_ci if (JSTaggedValue::SameValue(keys->Get(i), revokeKey.GetTaggedValue())) { 1044514f5e3Sopenharmony_ci rflag = true; 1054514f5e3Sopenharmony_ci } 1064514f5e3Sopenharmony_ci } 1074514f5e3Sopenharmony_ci EXPECT_TRUE(pflag); 1084514f5e3Sopenharmony_ci EXPECT_TRUE(rflag); 1094514f5e3Sopenharmony_ci 1104514f5e3Sopenharmony_ci PropertyDescriptor descRes(thread); 1114514f5e3Sopenharmony_ci JSObject::GetOwnProperty(thread, resultHandle, revokeKey, descRes); 1124514f5e3Sopenharmony_ci EXPECT_TRUE(descRes.GetValue()->IsProxyRevocFunction()); 1134514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1144514f5e3Sopenharmony_ci} 1154514f5e3Sopenharmony_ci} // namespace panda::test 116