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