14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021-2024 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#include "ecmascript/global_env.h"
184514f5e3Sopenharmony_ci#include "ecmascript/js_function.h"
194514f5e3Sopenharmony_ci
204514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins {
214514f5e3Sopenharmony_ci// 26.2.1.1 Proxy( [ value ] )
224514f5e3Sopenharmony_ciJSTaggedValue BuiltinsProxy::ProxyConstructor(EcmaRuntimeCallInfo *argv)
234514f5e3Sopenharmony_ci{
244514f5e3Sopenharmony_ci    ASSERT(argv);
254514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Proxy, Constructor);
264514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(argv->GetThread());
274514f5e3Sopenharmony_ci
284514f5e3Sopenharmony_ci    // 1.If NewTarget is undefined, throw a TypeError exception.
294514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
304514f5e3Sopenharmony_ci    if (newTarget->IsUndefined()) {
314514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(argv->GetThread(), "ProxyConstructor: NewTarget is undefined",
324514f5e3Sopenharmony_ci                                    JSTaggedValue::Exception());
334514f5e3Sopenharmony_ci    }
344514f5e3Sopenharmony_ci
354514f5e3Sopenharmony_ci    // 2.Return ProxyCreate(target, handler).
364514f5e3Sopenharmony_ci    JSHandle<JSProxy> proxy = JSProxy::ProxyCreate(argv->GetThread(), GetCallArg(argv, 0), GetCallArg(argv, 1));
374514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(argv->GetThread());
384514f5e3Sopenharmony_ci    return proxy.GetTaggedValue();
394514f5e3Sopenharmony_ci}
404514f5e3Sopenharmony_ci
414514f5e3Sopenharmony_ci// 26.2.2.1 Proxy.revocable ( target, handler )
424514f5e3Sopenharmony_ciJSTaggedValue BuiltinsProxy::Revocable(EcmaRuntimeCallInfo *argv)
434514f5e3Sopenharmony_ci{
444514f5e3Sopenharmony_ci    ASSERT(argv);
454514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Proxy, Revocable);
464514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
474514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
484514f5e3Sopenharmony_ci
494514f5e3Sopenharmony_ci    // 1.Let p be ProxyCreate(target, handler).
504514f5e3Sopenharmony_ci    JSHandle<JSProxy> proxy = JSProxy::ProxyCreate(thread, GetCallArg(argv, 0), GetCallArg(argv, 1));
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci    // 2.ReturnIfAbrupt(p).
534514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
544514f5e3Sopenharmony_ci
554514f5e3Sopenharmony_ci    // 3 ~ 4 new revoker function and set the [[RevocableProxy]] internal slot
564514f5e3Sopenharmony_ci    JSHandle<JSProxyRevocFunction> revoker = thread->GetEcmaVM()->GetFactory()->NewJSProxyRevocFunction(proxy);
574514f5e3Sopenharmony_ci
584514f5e3Sopenharmony_ci    // 5.Let result be ObjectCreate(%ObjectPrototype%).
594514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
604514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> proto = env->GetObjectFunctionPrototype();
614514f5e3Sopenharmony_ci    JSHandle<JSObject> result = thread->GetEcmaVM()->GetFactory()->OrdinaryNewJSObjectCreate(proto);
624514f5e3Sopenharmony_ci
634514f5e3Sopenharmony_ci    // 6.Perform CreateDataProperty(result, "proxy", p).
644514f5e3Sopenharmony_ci    auto globalConst = thread->GlobalConstants();
654514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> proxyKey = globalConst->GetHandledProxyString();
664514f5e3Sopenharmony_ci    JSObject::CreateDataProperty(thread, result, proxyKey, JSHandle<JSTaggedValue>(proxy));
674514f5e3Sopenharmony_ci
684514f5e3Sopenharmony_ci    // 7.Perform CreateDataProperty(result, "revoke", revoker).
694514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> revokeKey = globalConst->GetHandledRevokeString();
704514f5e3Sopenharmony_ci    JSObject::CreateDataProperty(thread, result, revokeKey, JSHandle<JSTaggedValue>(revoker));
714514f5e3Sopenharmony_ci
724514f5e3Sopenharmony_ci    // 8.Return result.
734514f5e3Sopenharmony_ci    return result.GetTaggedValue();
744514f5e3Sopenharmony_ci}
754514f5e3Sopenharmony_ci
764514f5e3Sopenharmony_ci// A Proxy revocation function to invalidate a specific Proxy object
774514f5e3Sopenharmony_ciJSTaggedValue BuiltinsProxy::InvalidateProxyFunction(EcmaRuntimeCallInfo *argv)
784514f5e3Sopenharmony_ci{
794514f5e3Sopenharmony_ci    ASSERT(argv);
804514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Proxy, InvalidateProxyFunction);
814514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
824514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
834514f5e3Sopenharmony_ci
844514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> proxy = GetConstructor(argv);
854514f5e3Sopenharmony_ci    JSProxyRevocFunction::ProxyRevocFunctions(thread, JSHandle<JSProxyRevocFunction>(proxy));
864514f5e3Sopenharmony_ci    return JSTaggedValue::Undefined();
874514f5e3Sopenharmony_ci}
884514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::builtins
89