133eb0b6dSopenharmony_ci/*
233eb0b6dSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
333eb0b6dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
433eb0b6dSopenharmony_ci * you may not use this file except in compliance with the License.
533eb0b6dSopenharmony_ci * You may obtain a copy of the License at
633eb0b6dSopenharmony_ci *
733eb0b6dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
833eb0b6dSopenharmony_ci *
933eb0b6dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1033eb0b6dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1133eb0b6dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1233eb0b6dSopenharmony_ci * See the License for the specific language governing permissions and
1333eb0b6dSopenharmony_ci * limitations under the License.
1433eb0b6dSopenharmony_ci */
1533eb0b6dSopenharmony_ci
1633eb0b6dSopenharmony_ci#include "ark_native_deferred.h"
1733eb0b6dSopenharmony_ci
1833eb0b6dSopenharmony_ci#include <cstring>
1933eb0b6dSopenharmony_ci
2033eb0b6dSopenharmony_ci#include "ark_native_engine.h"
2133eb0b6dSopenharmony_ci#ifdef ENABLE_CONTAINER_SCOPE
2233eb0b6dSopenharmony_ci#include "core/common/container_scope.h"
2333eb0b6dSopenharmony_ci#endif
2433eb0b6dSopenharmony_ci#include "native_engine/native_utils.h"
2533eb0b6dSopenharmony_ci
2633eb0b6dSopenharmony_ciArkNativeDeferred::ArkNativeDeferred(ArkNativeEngine* engine, Local<PromiseCapabilityRef> deferred)
2733eb0b6dSopenharmony_ci    : engine_(engine), deferred_(engine->GetEcmaVm(), deferred)
2833eb0b6dSopenharmony_ci{
2933eb0b6dSopenharmony_ci#ifdef ENABLE_CONTAINER_SCOPE
3033eb0b6dSopenharmony_ci    scopeId_ = OHOS::Ace::ContainerScope::CurrentId();
3133eb0b6dSopenharmony_ci#endif
3233eb0b6dSopenharmony_ci}
3333eb0b6dSopenharmony_ci
3433eb0b6dSopenharmony_ciArkNativeDeferred::~ArkNativeDeferred()
3533eb0b6dSopenharmony_ci{
3633eb0b6dSopenharmony_ci    // Addr of Global stored in ArkNativeDeferred should be released.
3733eb0b6dSopenharmony_ci    deferred_.FreeGlobalHandleAddr();
3833eb0b6dSopenharmony_ci}
3933eb0b6dSopenharmony_ci
4033eb0b6dSopenharmony_civoid ArkNativeDeferred::Resolve(napi_value data)
4133eb0b6dSopenharmony_ci{
4233eb0b6dSopenharmony_ci#ifdef ENABLE_CONTAINER_SCOPE
4333eb0b6dSopenharmony_ci    OHOS::Ace::ContainerScope containerScope(scopeId_);
4433eb0b6dSopenharmony_ci#endif
4533eb0b6dSopenharmony_ci    auto vm = engine_->GetEcmaVm();
4633eb0b6dSopenharmony_ci    deferred_->Resolve(vm, reinterpret_cast<uintptr_t>(data));
4733eb0b6dSopenharmony_ci}
4833eb0b6dSopenharmony_ci
4933eb0b6dSopenharmony_civoid ArkNativeDeferred::Reject(napi_value reason)
5033eb0b6dSopenharmony_ci{
5133eb0b6dSopenharmony_ci#ifdef ENABLE_CONTAINER_SCOPE
5233eb0b6dSopenharmony_ci    OHOS::Ace::ContainerScope containerScope(scopeId_);
5333eb0b6dSopenharmony_ci#endif
5433eb0b6dSopenharmony_ci    auto vm = engine_->GetEcmaVm();
5533eb0b6dSopenharmony_ci    deferred_->Reject(vm, reinterpret_cast<uintptr_t>(reason));
5633eb0b6dSopenharmony_ci}
57