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