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 "quickjs_native_deferred.h"
17
QuickJSNativeDeferred(QuickJSNativeEngine * engine,JSValue values[2])18 QuickJSNativeDeferred::QuickJSNativeDeferred(QuickJSNativeEngine* engine, JSValue values[2])
19 : engine_(engine), resolve_(values[0]), reject_(values[1])
20 {
21 }
22
~QuickJSNativeDeferred()23 QuickJSNativeDeferred::~QuickJSNativeDeferred()
24 {
25 JS_FreeValue(engine_->GetContext(), resolve_);
26 JS_FreeValue(engine_->GetContext(), reject_);
27 }
28
Resolve(NativeValue * data)29 void QuickJSNativeDeferred::Resolve(NativeValue* data)
30 {
31 JSValue value = JS_UNDEFINED;
32 if (data != nullptr) {
33 value = *data;
34 }
35
36 JSValue jsResult = JS_Call(engine_->GetContext(), resolve_, JS_UNDEFINED, 1, &value);
37 JS_FreeValue(engine_->GetContext(), jsResult);
38 js_std_loop(engine_->GetContext());
39 }
40
Reject(NativeValue * reason)41 void QuickJSNativeDeferred::Reject(NativeValue* reason)
42 {
43 JSValue value = JS_UNDEFINED;
44 if (reason != nullptr) {
45 value = *reason;
46 }
47
48 JSValue jsResult = JS_Call(engine_->GetContext(), reject_, JS_UNDEFINED, 1, &value);
49 JS_FreeValue(engine_->GetContext(), jsResult);
50 js_std_loop(engine_->GetContext());
51 }
52