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_vm.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/js_function.h"
21 #include "ecmascript/object_factory.h"
22 #include "ecmascript/tagged_array-inl.h"
23
24 namespace panda::ecmascript::builtins {
25 // 26.2.1.1 Proxy( [ value ] )
ProxyConstructor(EcmaRuntimeCallInfo * argv)26 JSTaggedValue BuiltinsProxy::ProxyConstructor(EcmaRuntimeCallInfo *argv)
27 {
28 ASSERT(argv);
29 BUILTINS_API_TRACE(argv->GetThread(), Proxy, Constructor);
30 [[maybe_unused]] EcmaHandleScope handleScope(argv->GetThread());
31
32 // 1.If NewTarget is undefined, throw a TypeError exception.
33 JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
34 if (newTarget->IsUndefined()) {
35 THROW_TYPE_ERROR_AND_RETURN(argv->GetThread(), "ProxyConstructor: NewTarget is undefined",
36 JSTaggedValue::Exception());
37 }
38
39 // 2.Return ProxyCreate(target, handler).
40 JSHandle<JSProxy> proxy = JSProxy::ProxyCreate(argv->GetThread(), GetCallArg(argv, 0), GetCallArg(argv, 1));
41 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(argv->GetThread());
42 return proxy.GetTaggedValue();
43 }
44
45 // 26.2.2.1 Proxy.revocable ( target, handler )
Revocable(EcmaRuntimeCallInfo * argv)46 JSTaggedValue BuiltinsProxy::Revocable(EcmaRuntimeCallInfo *argv)
47 {
48 ASSERT(argv);
49 BUILTINS_API_TRACE(argv->GetThread(), Proxy, Revocable);
50 JSThread *thread = argv->GetThread();
51 [[maybe_unused]] EcmaHandleScope handleScope(thread);
52
53 // 1.Let p be ProxyCreate(target, handler).
54 JSHandle<JSProxy> proxy = JSProxy::ProxyCreate(thread, GetCallArg(argv, 0), GetCallArg(argv, 1));
55
56 // 2.ReturnIfAbrupt(p).
57 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
58
59 // 3 ~ 4 new revoker function and set the [[RevocableProxy]] internal slot
60 JSHandle<JSProxyRevocFunction> revoker = thread->GetEcmaVM()->GetFactory()->NewJSProxyRevocFunction(proxy);
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<JSTaggedValue> proxy = GetConstructor(argv);
89 JSProxyRevocFunction::ProxyRevocFunctions(thread, JSHandle<JSProxyRevocFunction>(proxy));
90 return JSTaggedValue::Undefined();
91 }
92 } // namespace panda::ecmascript::builtins
93