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 #include "native_engine/native_utils.h"
22
ArkNativeDeferred(ArkNativeEngine * engine,Local<PromiseCapabilityRef> deferred)23 ArkNativeDeferred::ArkNativeDeferred(ArkNativeEngine* engine, Local<PromiseCapabilityRef> deferred)
24 : engine_(engine), deferred_(engine->GetEcmaVm(), deferred)
25 {}
26
~ArkNativeDeferred()27 ArkNativeDeferred::~ArkNativeDeferred()
28 {
29 // Addr of Global stored in ArkNativeDeferred should be released.
30 deferred_.FreeGlobalHandleAddr();
31 }
32
Resolve(napi_value data)33 void ArkNativeDeferred::Resolve(napi_value data)
34 {
35 auto vm = engine_->GetEcmaVm();
36 LocalScope scope(vm);
37 Local<JSValueRef> value = LocalValueFromJsValue(data);
38 deferred_->Resolve(vm, value);
39 }
40
Reject(napi_value reason)41 void ArkNativeDeferred::Reject(napi_value reason)
42 {
43 auto vm = engine_->GetEcmaVm();
44 LocalScope scope(vm);
45 Local<JSValueRef> value = LocalValueFromJsValue(reason);
46 deferred_->Reject(vm, value);
47 }
48