1/*
2 * Copyright (c) 2022 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_weak_ref.h"
17#include "ecmascript/js_weak_ref.h"
18
19namespace panda::ecmascript::builtins {
20JSTaggedValue BuiltinsWeakRef::WeakRefConstructor(EcmaRuntimeCallInfo *argv)
21{
22    ASSERT(argv);
23    JSThread *thread = argv->GetThread();
24    BUILTINS_API_TRACE(thread, WeakRef, Constructor);
25    [[maybe_unused]] EcmaHandleScope handleScope(thread);
26    JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
27    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
28    // 1. If NewTarget is undefined, throw a TypeError exception.
29    if (newTarget->IsUndefined()) {
30        THROW_TYPE_ERROR_AND_RETURN(thread, "new target can't be undefined", JSTaggedValue::Exception());
31    }
32    // 2. If CanBeHeldWeakly(target) is false, throw a TypeError exception.
33    JSHandle<JSTaggedValue> target = GetCallArg(argv, 0);
34    if (!JSTaggedValue::CanBeHeldWeakly(thread, target)) {
35        THROW_TYPE_ERROR_AND_RETURN(thread, "target invalid", JSTaggedValue::Exception());
36    }
37    // 3. Let weakRef be ? OrdinaryCreateFromConstructor(NewTarget, "%WeakRef.prototype%", « [[WeakRefTarget]] »).
38    JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
39    JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
40    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
41    // 4. Perform ! AddToKeptObjects(target).
42    thread->GetCurrentEcmaContext()->AddToKeptObjects(target);
43    // 5. Set weakRef.[[WeakRefTarget]] to target.
44    // 6. Return weakRef.
45    JSHandle<JSWeakRef> weakRef = JSHandle<JSWeakRef>::Cast(obj);
46    weakRef->SetToWeak(thread, target.GetTaggedValue());
47    return weakRef.GetTaggedValue();
48}
49
50JSTaggedValue BuiltinsWeakRef::Deref(EcmaRuntimeCallInfo *argv)
51{
52    ASSERT(argv);
53    JSThread *thread = argv->GetThread();
54    BUILTINS_API_TRACE(thread, WeakRef, Deref);
55    [[maybe_unused]] EcmaHandleScope handleScope(thread);
56    // 1. Let weakRef be the this value.
57    JSHandle<JSTaggedValue> thisValue = GetThis(argv);
58    // 2. Perform ? RequireInternalSlot(weakRef, [[WeakRefTarget]]).
59    if (!thisValue->IsJSWeakRef()) {
60        THROW_TYPE_ERROR_AND_RETURN(thread, "thisValue is not object or does not have an internalSlot internal slot",
61                                    JSTaggedValue::Exception());
62    }
63    // 3. Return ! WeakRefDeref(weakRef).
64    JSHandle<JSWeakRef> weakRef = JSHandle<JSWeakRef>::Cast(thisValue);
65    return JSWeakRef::WeakRefDeref(thread, weakRef);
66}
67} // namespace panda::ecmascript::builtins
68