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 "ark_native_engine.h"
17
18 #ifdef ENABLE_CONTAINER_SCOPE
19 #include "core/common/container_scope.h"
20 #endif
21
22 #include "ark_native_deferred.h"
23
24 using panda::Global;
25 using panda::Local;
26 using panda::JSValueRef;
ArkNativeDeferred(ArkNativeEngine * engine,Local<PromiseCapabilityRef> deferred)27 ArkNativeDeferred::ArkNativeDeferred(ArkNativeEngine* engine, Local<PromiseCapabilityRef> deferred)
28 : engine_(engine), deferred_(engine->GetEcmaVm(), deferred)
29 {
30 #ifdef ENABLE_CONTAINER_SCOPE
31 scopeId_ = OHOS::Ace::ContainerScope::CurrentId();
32 #endif
33 }
34
~ArkNativeDeferred()35 ArkNativeDeferred::~ArkNativeDeferred()
36 {
37 // Addr of Global stored in ArkNativeDeferred should be released.
38 deferred_.FreeGlobalHandleAddr();
39 }
40
Resolve(NativeValue * data)41 void ArkNativeDeferred::Resolve(NativeValue* data)
42 {
43 #ifdef ENABLE_CONTAINER_SCOPE
44 OHOS::Ace::ContainerScope containerScope(scopeId_);
45 #endif
46 auto vm = engine_->GetEcmaVm();
47 LocalScope scope(vm);
48 Global<JSValueRef> value = *data;
49 deferred_->Resolve(vm, value.ToLocal(vm));
50 }
51
Reject(NativeValue * reason)52 void ArkNativeDeferred::Reject(NativeValue* reason)
53 {
54 #ifdef ENABLE_CONTAINER_SCOPE
55 OHOS::Ace::ContainerScope containerScope(scopeId_);
56 #endif
57 auto vm = engine_->GetEcmaVm();
58 LocalScope scope(vm);
59 Global<JSValueRef> value = *reason;
60 deferred_->Reject(vm, value.ToLocal(vm));
61 }