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 #include "ecmascript/ecma_vm.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/js_function.h"
20 #include "ecmascript/object_factory.h"
21 #include "ecmascript/tagged_array-inl.h"
22
23 namespace panda::ecmascript::builtins {
24 // 26.2.1.1 Proxy( [ value ] )
ProxyConstructor(EcmaRuntimeCallInfo * argv)25 JSTaggedValue BuiltinsProxy::ProxyConstructor(EcmaRuntimeCallInfo *argv)
26 {
27 ASSERT(argv);
28 BUILTINS_API_TRACE(argv->GetThread(), Proxy, Constructor);
29 [[maybe_unused]] EcmaHandleScope handleScope(argv->GetThread());
30
31 // 1.If NewTarget is undefined, throw a TypeError exception.
32 JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
33 if (newTarget->IsUndefined()) {
34 THROW_TYPE_ERROR_AND_RETURN(argv->GetThread(), "ProxyConstructor: NewTarget is undefined",
35 JSTaggedValue::Exception());
36 }
37
38 // 2.Return ProxyCreate(target, handler).
39 JSHandle<JSProxy> proxy = JSProxy::ProxyCreate(argv->GetThread(), GetCallArg(argv, 0), GetCallArg(argv, 1));
40 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(argv->GetThread());
41 return proxy.GetTaggedValue();
42 }
43
44 // 26.2.2.1 Proxy.revocable ( target, handler )
Revocable(EcmaRuntimeCallInfo * argv)45 JSTaggedValue BuiltinsProxy::Revocable([[maybe_unused]] EcmaRuntimeCallInfo *argv)
46 {
47 ASSERT(argv);
48 BUILTINS_API_TRACE(argv->GetThread(), Proxy, Revocable);
49 JSThread *thread = argv->GetThread();
50 [[maybe_unused]] EcmaHandleScope handleScope(thread);
51
52 // 1.Let p be ProxyCreate(target, handler).
53 JSHandle<JSProxy> proxy = JSProxy::ProxyCreate(thread, GetCallArg(argv, 0), GetCallArg(argv, 1));
54
55 // 2.ReturnIfAbrupt(p).
56 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
57
58 // 3 ~ 4 new revoker function and set the [[RevocableProxy]] internal slot
59 JSHandle<JSProxyRevocFunction> revoker = thread->GetEcmaVM()->GetFactory()->NewJSProxyRevocFunction(
60 proxy, reinterpret_cast<void *>(InvalidateProxyFunction));
61
62 // 5.Let result be ObjectCreate(%ObjectPrototype%).
63 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
64 JSHandle<JSTaggedValue> proto = env->GetObjectFunctionPrototype();
65 JSHandle<JSObject> result = thread->GetEcmaVM()->GetFactory()->OrdinaryNewJSObjectCreate(proto);
66
67 // 6.Perform CreateDataProperty(result, "proxy", p).
68 auto globalConst = thread->GlobalConstants();
69 JSHandle<JSTaggedValue> proxyKey = globalConst->GetHandledProxyString();
70 JSObject::CreateDataProperty(thread, result, proxyKey, JSHandle<JSTaggedValue>(proxy));
71
72 // 7.Perform CreateDataProperty(result, "revoke", revoker).
73 JSHandle<JSTaggedValue> revokeKey = globalConst->GetHandledRevokeString();
74 JSObject::CreateDataProperty(thread, result, revokeKey, JSHandle<JSTaggedValue>(revoker));
75
76 // 8.Return result.
77 return result.GetTaggedValue();
78 }
79
80 // A Proxy revocation function to invalidate a specific Proxy object
InvalidateProxyFunction(EcmaRuntimeCallInfo * argv)81 JSTaggedValue BuiltinsProxy::InvalidateProxyFunction(EcmaRuntimeCallInfo *argv)
82 {
83 ASSERT(argv);
84 BUILTINS_API_TRACE(argv->GetThread(), Proxy, InvalidateProxyFunction);
85 JSThread *thread = argv->GetThread();
86 [[maybe_unused]] EcmaHandleScope handleScope(thread);
87
88 JSHandle<JSObject> revoke_obj(GetThis(argv));
89 JSHandle<JSTaggedValue> revokeKey = thread->GlobalConstants()->GetHandledRevokeString();
90
91 PropertyDescriptor desc(thread);
92 JSObject::GetOwnProperty(thread, revoke_obj, revokeKey, desc);
93 JSProxyRevocFunction::ProxyRevocFunctions(thread, JSHandle<JSProxyRevocFunction>(desc.GetValue()));
94 return JSTaggedValue::Undefined();
95 }
96 } // namespace panda::ecmascript::builtins
97