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