• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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/functions/js_function.h"
17 
18 #include "base/json/json_util.h"
19 #include "base/log/ace_performance_monitor.h"
20 #include "base/log/ace_trace.h"
21 #include "base/log/log.h"
22 
23 namespace OHOS::Ace::Framework {
24 namespace {
ExecuteInternal(const std::unique_ptr<JsonValue> & value,const std::string & key,const JSRef<JSObject> & eventInfo)25 void ExecuteInternal(const std::unique_ptr<JsonValue>& value, const std::string& key,
26     const JSRef<JSObject>& eventInfo)
27 {
28     if (value->IsString()) {
29         eventInfo->SetProperty<std::string>(key.c_str(), value->GetString().c_str());
30     } else if (value->IsNumber()) {
31         eventInfo->SetProperty<double>(key.c_str(), value->GetDouble());
32     } else if (value->IsBool()) {
33         eventInfo->SetProperty<bool>(key.c_str(), value->GetBool());
34     } else if (value->IsObject()) {
35         eventInfo->SetPropertyJsonObject(key.c_str(), value->ToString().c_str());
36     } else if (value->IsArray()) {
37         JSRef<JSArray> valueArray = JSRef<JSArray>::New();
38         for (auto index = 0; index < value->GetArraySize(); index++) {
39             auto item = value->GetArrayItem(index);
40             if (item && item->IsString()) {
41                 valueArray->SetValueAt(index, JSRef<JSVal>::Make(ToJSValue(item->GetString())));
42             }
43             if (item && item->IsNumber()) {
44                 valueArray->SetValueAt(index, JSRef<JSVal>::Make(ToJSValue(item->GetInt())));
45             }
46         }
47         eventInfo->SetPropertyObject(key.c_str(), valueArray);
48     }
49 }
50 }
51 
Execute(const std::vector<std::string> & keys,const std::string & param)52 void JsFunctionBase::Execute(const std::vector<std::string>& keys, const std::string& param)
53 {
54     std::unique_ptr<JsonValue> argsPtr = JsonUtil::ParseJsonString(param);
55     if (!argsPtr) {
56         return;
57     }
58     JSRef<JSObject> eventInfo = JSRef<JSObject>::New();
59     for (auto iter = keys.begin(); iter != keys.end(); iter++) {
60         const std::string key = *iter;
61         const auto value = argsPtr->GetValue(key);
62         if (!value) {
63             LOGD("key[%{public}s] not exist.", key.c_str());
64             continue;
65         }
66         ExecuteInternal(value, key, eventInfo);
67     }
68 
69     JSRef<JSVal> paramObj = JSRef<JSVal>::Cast(eventInfo);
70     ExecuteJS(1, &paramObj);
71 }
72 
ExecuteJS(int argc,JSRef<JSVal> argv[])73 JSRef<JSVal> JsFunctionBase::ExecuteJS(int argc, JSRef<JSVal> argv[])
74 {
75     int32_t id = -1;
76     if (SystemProperties::GetAcePerformanceMonitorEnabled()) {
77         id = Container::CurrentId();
78     }
79     JS_CALLBACK_DURATION(id);
80     JAVASCRIPT_EXECUTION_SCOPE_STATIC;
81     ACE_FUNCTION_TRACE();
82     auto jsFunction = GetFunction();
83     if (jsFunction.IsEmpty()) {
84         LOGW("js function is null.");
85         return {};
86     }
87     return jsFunction->Call(This(), argc, argv);
88 }
89 
90 } // namespace OHOS::Ace::Framework
91