1 /*
2 * Copyright (c) 2024-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 "ark_hz/ark_js_runtime.h"
17 #include <uv.h>
18 #include "ark_js_runtime.h"
19 #include "utils/utils.h"
20
21 #include "ecmascript/base/string_helper.h"
22 #include "ecmascript/napi/include/jsnapi.h"
23 #include "static_core/plugins/ets/runtime/interop_js/timer_helper/interop_timer_helper.h"
24
25 #include <iostream>
26
27 namespace panda {
28
GetHelper()29 std::string GetHelper()
30 {
31 return ecmascript::COMMON_HELP_HEAD_MSG + ecmascript::HELP_OPTION_MSG;
32 }
33
ProcessOptions(int argc,const char ** argv,arg_list_t * filenames)34 bool ArkJsRuntime::ProcessOptions(int argc, const char **argv, arg_list_t *filenames)
35 {
36 if (argc < 2) { // 2: at least have two arguments
37 std::cerr << GetHelper();
38 return false;
39 }
40
41 size_t jsvmArgsEndIdx = 0;
42 for (int idx = 1; idx < argc; ++idx) {
43 if (ecmascript::base::StringHelper::EndsWith(argv[idx], ".abc")) {
44 jsvmArgsEndIdx = idx;
45 break;
46 }
47 }
48
49 std::string files = argv[jsvmArgsEndIdx];
50 if (files.empty()) {
51 std::cerr << "Abc file must pass after arguments to jsvm" << std::endl;
52 std::cerr << GetHelper();
53 return 1;
54 }
55
56 bool retOpt = options_.ParseCommand(jsvmArgsEndIdx, argv);
57 if (!retOpt) {
58 std::cerr << GetHelper();
59 return false;
60 }
61
62 *filenames = ecmascript::base::StringHelper::SplitString(files, ":");
63 return true;
64 }
65
JsValueFromLocalValue(panda::Local<panda::JSValueRef> local)66 static napi_value JsValueFromLocalValue(panda::Local<panda::JSValueRef> local)
67 {
68 return reinterpret_cast<napi_value>(*local);
69 }
70
Init()71 bool ArkJsRuntime::Init()
72 {
73 if (vm_ != nullptr) {
74 return true;
75 }
76
77 vm_ = JSNApi::CreateEcmaVM(options_);
78 if (vm_ == nullptr) {
79 std::cerr << "Cannot Create vm" << std::endl;
80 return false;
81 }
82
83 engine_ = std::make_unique<ArkNativeEngine>(vm_, nullptr);
84
85 engine_->SetGetAssetFunc(utils::GetAsset);
86 engine_->SetCleanEnv([this] { JSNApi::DestroyJSVM(vm_); });
87
88 auto *engine = GetNativeEngine();
89 Local<ObjectRef> global = panda::JSNApi::GetGlobalObject(vm_);
90 ark::ets::interop::js::helper::Init(reinterpret_cast<napi_env>(engine), JsValueFromLocalValue(global));
91 return true;
92 }
93
Execute(const std::string & filename)94 bool ArkJsRuntime::Execute(const std::string &filename)
95 {
96 LocalScope scope(vm_);
97 return JSNApi::Execute(vm_, filename, options_.GetEntryPoint());
98 }
99
GetUVLoop()100 uv_loop_t *ArkJsRuntime::GetUVLoop()
101 {
102 return engine_->GetUVLoop();
103 }
104
Loop()105 void ArkJsRuntime::Loop()
106 {
107 // 2 here for NativeEngine async_t and 1 for main CallbackPoster
108 static constexpr uint32_t MANUALLY_HANDLED_ASYNC_COUNT = 2U + 1U; // CC-OFF(G.NAM.03-CPP) project code style
109 auto *loop = GetUVLoop();
110 auto cntHandles = []([[maybe_unused]] uv_handle_t *handle, void *arg) {
111 auto *cnt = reinterpret_cast<uint32_t *>(arg);
112 (*cnt)++;
113 };
114 // CC-OFFNXT(G.CTL.03) false positive
115 while (true) {
116 uint32_t handleCount = 0;
117 uv_walk(loop, cntHandles, &handleCount);
118 if (handleCount <= MANUALLY_HANDLED_ASYNC_COUNT) {
119 break;
120 }
121 engine_->Loop(LOOP_ONCE);
122 }
123 }
124
125 } // namespace panda
126