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 "v8_native_engine.h"
17
18 #include "v8_native_deferred.h"
19
V8NativeDeferred(V8NativeEngine * engine,v8::Local<v8::Promise::Resolver> deferred)20 V8NativeDeferred::V8NativeDeferred(V8NativeEngine* engine, v8::Local<v8::Promise::Resolver> deferred)
21 : engine_(engine), deferred_(engine->GetIsolate(), deferred)
22 {
23 }
24
~V8NativeDeferred()25 V8NativeDeferred::~V8NativeDeferred() {}
26
Resolve(NativeValue * data)27 void V8NativeDeferred::Resolve(NativeValue* data)
28 {
29 v8::Local<v8::Context> context = engine_->GetContext();
30 v8::Isolate* isolate = engine_->GetIsolate();
31
32 v8::Local<v8::Value> value = *data;
33 auto v8Resolver = deferred_.Get(isolate);
34
35 v8Resolver->Resolve(context, value).ToChecked();
36 }
37
Reject(NativeValue * reason)38 void V8NativeDeferred::Reject(NativeValue* reason)
39 {
40 v8::Local<v8::Context> context = engine_->GetContext();
41 v8::Isolate* isolate = engine_->GetIsolate();
42
43 v8::Local<v8::Value> value = *reason;
44 auto v8Resolver = deferred_.Get(isolate);
45
46 v8Resolver->Reject(context, value).ToChecked();
47 }