• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "frameworks/bridge/declarative_frontend/engine/v8/functions/v8_function.h"
17 
18 #include "base/json/json_util.h"
19 #include "base/log/ace_trace.h"
20 #include "base/log/log.h"
21 #include "frameworks/bridge/declarative_frontend/engine/v8/v8_utils.h"
22 
23 namespace OHOS::Ace::Framework {
24 
V8Function(v8::Local<v8::Value> jsObject,v8::Local<v8::Function> jsFunction)25 V8Function::V8Function(v8::Local<v8::Value> jsObject, v8::Local<v8::Function> jsFunction)
26 {
27     isolate_ = v8::Isolate::GetCurrent();
28     ctx_.Reset(isolate_, isolate_->GetCurrentContext());
29     jsThis_.Reset(isolate_, jsObject);
30     jsFunction_.Reset(isolate_, jsFunction);
31 }
32 
~V8Function()33 V8Function::~V8Function()
34 {
35     LOGD("Destroy: V8Function");
36     ctx_.Reset();
37     jsThis_.Reset();
38     jsFunction_.Reset();
39 }
40 
execute()41 void V8Function::execute()
42 {
43     ACE_DCHECK(isolate_);
44     v8::Isolate::Scope isolateScope(isolate_);
45     v8::HandleScope handleScope(isolate_);
46     auto context = ctx_.Get(isolate_);
47     v8::Context::Scope contextScope(context);
48     V8Function::executeJS();
49 }
50 
execute(std::vector<std::string> keys,const std::string & param)51 void V8Function::execute(std::vector<std::string> keys, const std::string& param)
52 {
53     ACE_DCHECK(isolate_);
54     v8::Isolate::Scope isolateScope(isolate_);
55     v8::HandleScope handleScope(isolate_);
56     auto context = ctx_.Get(isolate_);
57     v8::Context::Scope contextScope(context);
58     LOGI("param : %{public}s", param.c_str());
59     std::unique_ptr<JsonValue> argsPtr = JsonUtil::ParseJsonString(param);
60     if (!argsPtr) {
61         LOGW("Parse param failed!");
62         return;
63     }
64     v8::Local<v8::Object> eventInfo = v8::Object::New(isolate_);
65     for (auto iter = keys.begin(); iter != keys.end(); iter++) {
66         const std::string key = *iter;
67         const auto value = argsPtr->GetValue(key);
68         if (!value) {
69             LOGI("key[%{public}s] is not exist.", key.c_str());
70             continue;
71         }
72 
73         if (value->IsString()) {
74             eventInfo
75                 ->Set(context, v8::String::NewFromUtf8(isolate_, key.c_str()).ToLocalChecked(),
76                     v8::String::NewFromUtf8(isolate_, value->GetString().c_str()).ToLocalChecked())
77                 .ToChecked();
78         } else if (value->IsNumber()) {
79             eventInfo
80                 ->Set(context, v8::String::NewFromUtf8(isolate_, key.c_str()).ToLocalChecked(),
81                     v8::Number::New(isolate_, value->GetDouble()))
82                 .ToChecked();
83         } else if (value->IsObject()) {
84             v8::Local<v8::String> objStr =
85                 v8::String::NewFromUtf8(isolate_, value->ToString().c_str()).ToLocalChecked();
86             v8::Local<v8::Value> obj = v8::JSON::Parse(context, objStr).ToLocalChecked();
87             eventInfo->Set(context, v8::String::NewFromUtf8(isolate_, key.c_str()).ToLocalChecked(), obj).ToChecked();
88         }
89     }
90 
91     v8::Local<v8::Value> v8Param = eventInfo;
92     V8Function::executeJS(1, &v8Param);
93 }
94 
executeJS(int argc,v8::Local<v8::Value> * argv)95 v8::Local<v8::Value> V8Function::executeJS(int argc, v8::Local<v8::Value>* argv)
96 {
97     ACE_DCHECK(isolate_);
98     ACE_FUNCTION_TRACE();
99 
100     auto context = isolate_->GetCurrentContext();
101 
102     LOGD("JS_CALL: %s", V8Utils::ScopedString(jsFunction_.Get(isolate_)).get());
103 
104     v8::TryCatch tryCatch(isolate_);
105     v8::Local<v8::Value> result;
106     bool success = jsFunction_.Get(isolate_)->Call(context, jsThis_.Get(isolate_), argc, argv).ToLocal(&result);
107 
108     if (!success) {
109         V8Utils::JsStdDumpErrorAce(isolate_, &tryCatch);
110         return v8::Undefined(isolate_);
111     }
112 
113     return result;
114 }
115 
116 } // namespace OHOS::Ace::Framework