1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/builtins/builtins_proxy.h"
17
18 #include "ecmascript/ecma_runtime_call_info.h"
19 #include "ecmascript/ecma_string.h"
20 #include "ecmascript/ecma_vm.h"
21 #include "ecmascript/global_env.h"
22 #include "ecmascript/js_array.h"
23 #include "ecmascript/js_handle.h"
24 #include "ecmascript/js_tagged_value-inl.h"
25 #include "ecmascript/js_thread.h"
26 #include "ecmascript/object_factory.h"
27 #include "ecmascript/tests/test_helper.h"
28
29 using namespace panda::ecmascript;
30 using namespace panda::ecmascript::builtins;
31
32 namespace panda::test {
33 class BuiltinsProxyTest : public testing::Test {
34 public:
SetUpTestCase()35 static void SetUpTestCase()
36 {
37 GTEST_LOG_(INFO) << "SetUpTestCase";
38 }
39
TearDownTestCase()40 static void TearDownTestCase()
41 {
42 GTEST_LOG_(INFO) << "TearDownCase";
43 }
44
SetUp()45 void SetUp() override
46 {
47 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
48 }
49
TearDown()50 void TearDown() override
51 {
52 TestHelper::DestroyEcmaVMWithScope(instance, scope);
53 }
54
55 EcmaVM *instance {nullptr};
56 EcmaHandleScope *scope {nullptr};
57 JSThread *thread {nullptr};
58 };
59
BuiltinsTestProxyCreate(JSThread * thread)60 JSHandle<JSObject> BuiltinsTestProxyCreate(JSThread *thread)
61 {
62 EcmaVM *ecmaVM = thread->GetEcmaVM();
63 JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
64 JSHandle<JSTaggedValue> objFun(globalEnv->GetObjectFunction());
65 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
66
67 JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
68 return obj;
69 }
70
71 // 26.2.1.1 Proxy( [ value ] )
HWTEST_F_L0(BuiltinsProxyTest,ProxyConstructor)72 HWTEST_F_L0(BuiltinsProxyTest, ProxyConstructor)
73 {
74 JSHandle<JSObject> target = BuiltinsTestProxyCreate(thread);
75 JSHandle<JSObject> handler = BuiltinsTestProxyCreate(thread);
76
77 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Null(), 8);
78 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
79 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
80 ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
81 ecmaRuntimeCallInfo->SetCallArg(1, handler.GetTaggedValue());
82
83 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
84 JSTaggedValue result = BuiltinsProxy::ProxyConstructor(ecmaRuntimeCallInfo);
85 ASSERT_TRUE(result.IsECMAObject());
86 JSHandle<JSTaggedValue> resultHandle(thread, result);
87 EXPECT_TRUE(resultHandle->IsJSProxy());
88 TestHelper::TearDownFrame(thread, prev);
89 }
90
91 // 26.2.2.1 Proxy.revocable ( target, handler )
HWTEST_F_L0(BuiltinsProxyTest,Revocable)92 HWTEST_F_L0(BuiltinsProxyTest, Revocable)
93 {
94 JSHandle<JSObject> target = BuiltinsTestProxyCreate(thread);
95 JSHandle<JSObject> handler = BuiltinsTestProxyCreate(thread);
96
97 JSHandle<GlobalEnv> globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
98 JSHandle<JSFunction> proxyFun(globalEnv->GetProxyFunction());
99 JSHandle<JSTaggedValue> key(thread->GetEcmaVM()->GetFactory()->NewFromASCII("prop"));
100 PropertyDescriptor desc(thread);
101 desc.SetWritable(false);
102 JSObject::DefineOwnProperty(thread, handler, key, desc);
103
104 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
105 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
106 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
107 ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
108 ecmaRuntimeCallInfo->SetCallArg(1, handler.GetTaggedValue());
109 ecmaRuntimeCallInfo->SetNewTarget(JSTaggedValue(*proxyFun));
110
111 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
112 JSTaggedValue result = BuiltinsProxy::Revocable(ecmaRuntimeCallInfo);
113 ASSERT_TRUE(result.IsECMAObject());
114 JSHandle<JSObject> resultHandle(thread, result);
115
116 auto globalConst = thread->GlobalConstants();
117 JSHandle<JSTaggedValue> proxyKey = globalConst->GetHandledProxyString();
118 JSHandle<JSTaggedValue> revokeKey = globalConst->GetHandledRevokeString();
119
120 JSHandle<TaggedArray> keys = JSObject::GetOwnPropertyKeys(thread, resultHandle);
121 bool pflag = false;
122 bool rflag = false;
123 for (uint32_t i = 0; i < keys->GetLength(); i++) {
124 if (JSTaggedValue::SameValue(keys->Get(i), proxyKey.GetTaggedValue())) {
125 pflag = true;
126 }
127 if (JSTaggedValue::SameValue(keys->Get(i), revokeKey.GetTaggedValue())) {
128 rflag = true;
129 }
130 }
131 EXPECT_TRUE(pflag);
132 EXPECT_TRUE(rflag);
133
134 PropertyDescriptor descRes(thread);
135 JSObject::GetOwnProperty(thread, resultHandle, revokeKey, descRes);
136 EXPECT_TRUE(descRes.GetValue()->IsProxyRevocFunction());
137 TestHelper::TearDownFrame(thread, prev);
138 }
139 } // namespace panda::test
140