1 /*
2 * Copyright (c) 2023 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 "js_environment.h"
17
18 #include "js_env_logger.h"
19 #include "js_environment_impl.h"
20 #include "native_engine/impl/ark/ark_native_engine.h"
21 #include "uncaught_exception_callback.h"
22 #include "commonlibrary/ets_utils/js_sys_module/console/console.h"
23
24 namespace OHOS {
25 namespace JsEnv {
26 namespace {
27 static const std::string DEBUGGER = "@Debugger";
28 }
29
ConvertProfilerType(JsEnvironment::PROFILERTYPE type)30 static panda::DFXJSNApi::ProfilerType ConvertProfilerType(JsEnvironment::PROFILERTYPE type)
31 {
32 if (type == JsEnvironment::PROFILERTYPE::PROFILERTYPE_CPU) {
33 return panda::DFXJSNApi::ProfilerType::CPU_PROFILER;
34 } else {
35 return panda::DFXJSNApi::ProfilerType::HEAP_PROFILER;
36 }
37 }
38
JsEnvironment(std::unique_ptr<JsEnvironmentImpl> impl)39 JsEnvironment::JsEnvironment(std::unique_ptr<JsEnvironmentImpl> impl) : impl_(std::move(impl))
40 {
41 JSENV_LOG_D("called");
42 }
43
~JsEnvironment()44 JsEnvironment::~JsEnvironment()
45 {
46 JSENV_LOG_D("called");
47
48 if (engine_ != nullptr) {
49 delete engine_;
50 engine_ = nullptr;
51 }
52
53 if (vm_ != nullptr) {
54 panda::JSNApi::DestroyJSVM(vm_);
55 vm_ = nullptr;
56 }
57 }
58
Initialize(const panda::RuntimeOption & pandaOption,void * jsEngine)59 bool JsEnvironment::Initialize(const panda::RuntimeOption& pandaOption, void* jsEngine)
60 {
61 JSENV_LOG_D("Js environment initialize.");
62 vm_ = panda::JSNApi::CreateJSVM(pandaOption);
63 if (vm_ == nullptr) {
64 JSENV_LOG_E("Create vm failed.");
65 return false;
66 }
67
68 engine_ = new ArkNativeEngine(vm_, jsEngine);
69 return true;
70 }
71
InitTimerModule()72 void JsEnvironment::InitTimerModule()
73 {
74 if (engine_ == nullptr) {
75 JSENV_LOG_E("Invalid native engine.");
76 return;
77 }
78
79 if (impl_ != nullptr) {
80 impl_->InitTimerModule(engine_);
81 }
82 }
83
InitWorkerModule(std::shared_ptr<WorkerInfo> workerInfo)84 void JsEnvironment::InitWorkerModule(std::shared_ptr<WorkerInfo> workerInfo)
85 {
86 if (engine_ == nullptr) {
87 JSENV_LOG_E("Invalid native engine.");
88 return;
89 }
90
91 if (impl_ != nullptr) {
92 impl_->InitWorkerModule(engine_, workerInfo);
93 }
94 }
95
InitSyscapModule()96 void JsEnvironment::InitSyscapModule()
97 {
98 if (impl_ != nullptr) {
99 impl_->InitSyscapModule();
100 }
101 }
102
PostTask(const std::function<void ()> & task,const std::string & name,int64_t delayTime)103 void JsEnvironment::PostTask(const std::function<void()>& task, const std::string& name, int64_t delayTime)
104 {
105 if (impl_ != nullptr) {
106 impl_->PostTask(task, name, delayTime);
107 }
108 }
109
PostSyncTask(const std::function<void ()> & task,const std::string & name)110 void JsEnvironment::PostSyncTask(const std::function<void()>& task, const std::string& name)
111 {
112 if (impl_ != nullptr) {
113 impl_->PostSyncTask(task, name);
114 }
115 }
116
RemoveTask(const std::string & name)117 void JsEnvironment::RemoveTask(const std::string& name)
118 {
119 if (impl_ != nullptr) {
120 impl_->RemoveTask(name);
121 }
122 }
123
InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperator> operatorObj)124 void JsEnvironment::InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperator> operatorObj)
125 {
126 sourceMapOperator_ = operatorObj;
127 if (engine_ == nullptr) {
128 JSENV_LOG_E("Invalid Native Engine.");
129 return;
130 }
131
132 auto translateBySourceMapFunc = [&](const std::string& rawStack) {
133 return sourceMapOperator_->TranslateBySourceMap(rawStack);
134 };
135 engine_->RegisterTranslateBySourceMap(translateBySourceMapFunc);
136
137 auto translateUrlBySourceMapFunc = [&](std::string& url, int& line, int& column) {
138 return sourceMapOperator_->TranslateUrlPositionBySourceMap(url, line, column);
139 };
140 engine_->RegisterSourceMapTranslateCallback(translateUrlBySourceMapFunc);
141 }
142
RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo & uncaughtExceptionInfo)143 void JsEnvironment::RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo)
144 {
145 if (engine_ == nullptr) {
146 JSENV_LOG_E("Invalid Native Engine.");
147 return;
148 }
149
150 engine_->RegisterNapiUncaughtExceptionHandler(NapiUncaughtExceptionCallback(uncaughtExceptionInfo.uncaughtTask,
151 sourceMapOperator_, reinterpret_cast<napi_env>(engine_)));
152 }
153
LoadScript(const std::string & path,std::vector<uint8_t> * buffer,bool isBundle)154 bool JsEnvironment::LoadScript(const std::string& path, std::vector<uint8_t>* buffer, bool isBundle)
155 {
156 if (engine_ == nullptr) {
157 JSENV_LOG_E("Invalid Native Engine.");
158 return false;
159 }
160
161 if (buffer == nullptr) {
162 return engine_->RunScriptPath(path.c_str());
163 }
164
165 return engine_->RunScriptBuffer(path.c_str(), *buffer, isBundle) != nullptr;
166 }
167
StartDebugger(std::string & option,const char * libraryPath,uint32_t socketFd,bool needBreakPoint,uint32_t instanceId)168 bool JsEnvironment::StartDebugger(
169 std::string& option, const char* libraryPath, uint32_t socketFd, bool needBreakPoint, uint32_t instanceId)
170 {
171 JSENV_LOG_D("call.");
172 if (vm_ == nullptr) {
173 JSENV_LOG_E("Invalid vm.");
174 return false;
175 }
176 int32_t identifierId = ParseHdcRegisterOption(option);
177 if (identifierId == -1) {
178 JSENV_LOG_E("Abnormal parsing of tid results.");
179 return false;
180 }
181 debugMode_ = panda::JSNApi::StartDebuggerForSocketPair(identifierId, socketFd);
182 return debugMode_;
183 }
184
StopDebugger()185 void JsEnvironment::StopDebugger()
186 {
187 if (vm_ == nullptr) {
188 JSENV_LOG_E("Invalid vm.");
189 return;
190 }
191
192 (void)panda::JSNApi::StopDebugger(vm_);
193 }
194
StopDebugger(std::string & option)195 void JsEnvironment::StopDebugger(std::string& option)
196 {
197 int32_t identifierId = ParseHdcRegisterOption(option);
198 if (identifierId == -1) {
199 JSENV_LOG_E("Abnormal parsing of tid results.");
200 return;
201 }
202 panda::JSNApi::StopDebugger(identifierId);
203 }
204
InitConsoleModule()205 void JsEnvironment::InitConsoleModule()
206 {
207 if (engine_ == nullptr) {
208 JSENV_LOG_E("Invalid Native Engine.");
209 return;
210 }
211
212 if (impl_ != nullptr) {
213 impl_->InitConsoleModule(engine_);
214 }
215 }
216
InitLoop()217 bool JsEnvironment::InitLoop()
218 {
219 if (engine_ == nullptr) {
220 JSENV_LOG_E("Invalid Native Engine.");
221 return false;
222 }
223
224 if (impl_ != nullptr) {
225 impl_->InitLoop(engine_);
226 }
227 return true;
228 }
229
DeInitLoop()230 void JsEnvironment::DeInitLoop()
231 {
232 if (engine_ == nullptr) {
233 JSENV_LOG_E("Invalid Native Engine.");
234 return;
235 }
236
237 if (impl_ != nullptr) {
238 impl_->DeInitLoop(engine_);
239 }
240 }
241
LoadScript(const std::string & path,uint8_t * buffer,size_t len,bool isBundle)242 bool JsEnvironment::LoadScript(const std::string& path, uint8_t* buffer, size_t len, bool isBundle)
243 {
244 if (engine_ == nullptr) {
245 JSENV_LOG_E("Invalid Native Engine.");
246 return false;
247 }
248
249 return engine_->RunScriptBuffer(path, buffer, len, isBundle);
250 }
251
StartProfiler(const char * libraryPath,uint32_t instanceId,PROFILERTYPE profiler,int32_t interval,int tid,bool isDebugApp)252 void JsEnvironment::StartProfiler(const char* libraryPath, uint32_t instanceId, PROFILERTYPE profiler,
253 int32_t interval, int tid, bool isDebugApp)
254 {
255 if (vm_ == nullptr) {
256 JSENV_LOG_E("Invalid vm.");
257 return;
258 }
259
260 auto debuggerPostTask = [weak = weak_from_this()](std::function<void()>&& task) {
261 auto jsEnv = weak.lock();
262 if (jsEnv == nullptr) {
263 JSENV_LOG_E("JsEnv is invalid.");
264 return;
265 }
266 jsEnv->PostTask(task, "JsEnvironment::StartProfiler");
267 };
268
269 panda::DFXJSNApi::ProfilerOption option;
270 option.libraryPath = libraryPath;
271 option.profilerType = ConvertProfilerType(profiler);
272 option.interval = interval;
273
274 panda::DFXJSNApi::StartProfiler(vm_, option, tid, instanceId, debuggerPostTask, isDebugApp);
275 }
276
DestroyHeapProfiler()277 void JsEnvironment::DestroyHeapProfiler()
278 {
279 if (vm_ == nullptr) {
280 JSENV_LOG_E("Invalid vm.");
281 return;
282 }
283 panda::DFXJSNApi::DestroyHeapProfiler(vm_);
284 }
285
GetHeapPrepare()286 void JsEnvironment::GetHeapPrepare()
287 {
288 if (vm_ == nullptr) {
289 JSENV_LOG_E("Invalid vm.");
290 return;
291 }
292 panda::DFXJSNApi::GetHeapPrepare(vm_);
293 }
294
ReInitJsEnvImpl(std::unique_ptr<JsEnvironmentImpl> impl)295 void JsEnvironment::ReInitJsEnvImpl(std::unique_ptr<JsEnvironmentImpl> impl)
296 {
297 JSENV_LOG_D("ReInit jsenv impl.");
298 impl_ = std::move(impl);
299 }
300
SetModuleLoadChecker(const std::shared_ptr<ModuleCheckerDelegate> & moduleCheckerDelegate)301 void JsEnvironment::SetModuleLoadChecker(const std::shared_ptr<ModuleCheckerDelegate>& moduleCheckerDelegate)
302 {
303 if (engine_ == nullptr) {
304 JSENV_LOG_E("Invalid native engine.");
305 return;
306 }
307
308 engine_->SetModuleLoadChecker(moduleCheckerDelegate);
309 }
310
SetRequestAotCallback(const RequestAotCallback & cb)311 void JsEnvironment::SetRequestAotCallback(const RequestAotCallback& cb)
312 {
313 if (vm_ == nullptr) {
314 JSENV_LOG_E("Invalid vm.");
315 return;
316 }
317
318 panda::JSNApi::SetRequestAotCallback(vm_, cb);
319 }
320
SetDeviceDisconnectCallback(const std::function<bool ()> & cb)321 void JsEnvironment::SetDeviceDisconnectCallback(const std::function<bool()> &cb)
322 {
323 panda::JSNApi::SetDeviceDisconnectCallback(vm_, std::move(cb));
324 }
325
StartMonitorJSHeapUsage()326 void JsEnvironment::StartMonitorJSHeapUsage()
327 {
328 if (engine_ == nullptr) {
329 JSENV_LOG_E("Invalid native engine.");
330 return;
331 }
332 engine_->StartMonitorJSHeapUsage();
333 }
334
StopMonitorJSHeapUsage()335 void JsEnvironment::StopMonitorJSHeapUsage()
336 {
337 if (engine_ == nullptr) {
338 JSENV_LOG_E("Invalid native engine.");
339 return;
340 }
341 engine_->StopMonitorJSHeapUsage();
342 }
343
GetDebuggerPostTask()344 DebuggerPostTask JsEnvironment::GetDebuggerPostTask()
345 {
346 auto debuggerPostTask = [weak = weak_from_this()](std::function<void()>&& task) {
347 auto jsEnv = weak.lock();
348 if (jsEnv == nullptr) {
349 JSENV_LOG_E("JsEnv is invalid.");
350 return;
351 }
352 jsEnv->PostTask(task, "JsEnvironment:GetDebuggerPostTask");
353 };
354 return debuggerPostTask;
355 }
356
NotifyDebugMode(int tid,const char * libraryPath,uint32_t instanceId,bool debug,bool debugMode)357 void JsEnvironment::NotifyDebugMode(
358 int tid, const char* libraryPath, uint32_t instanceId, bool debug, bool debugMode)
359 {
360 if (vm_ == nullptr) {
361 JSENV_LOG_E("Invalid vm.");
362 return;
363 }
364 panda::JSNApi::DebugOption debugOption = {libraryPath, debug ? debugMode : false};
365 auto debuggerPostTask = [weak = weak_from_this()](std::function<void()>&& task) {
366 auto jsEnv = weak.lock();
367 if (jsEnv == nullptr) {
368 JSENV_LOG_E("JsEnv is invalid.");
369 return;
370 }
371 jsEnv->PostTask(task, "JsEnvironment:NotifyDebugMode");
372 };
373 panda::JSNApi::NotifyDebugMode(tid, vm_, debugOption, instanceId, debuggerPostTask, debug);
374 }
375
ParseHdcRegisterOption(std::string & option)376 int32_t JsEnvironment::ParseHdcRegisterOption(std::string& option)
377 {
378 JSENV_LOG_D("Start.");
379 std::size_t pos = option.find_first_of(":");
380 if (pos == std::string::npos) {
381 return -1;
382 }
383 std::string idStr = option.substr(pos + 1);
384 pos = idStr.find(DEBUGGER);
385 if (pos == std::string::npos) {
386 return -1;
387 }
388 idStr = idStr.substr(0, pos);
389 pos = idStr.find("@");
390 if (pos != std::string::npos) {
391 idStr = idStr.substr(pos + 1);
392 }
393 return std::atoi(idStr.c_str());
394 }
395
GetDebugMode() const396 bool JsEnvironment::GetDebugMode() const
397 {
398 return debugMode_;
399 }
400 } // namespace JsEnv
401 } // namespace OHOS
402