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