1 /*
2 * Copyright (c) 2021-2022 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_native_engine_impl.h"
17
18 #include "ark_native_deferred.h"
19 #include "ark_native_reference.h"
20 #include "scope_manager/native_scope_manager.h"
21
22 #ifdef ENABLE_CONTAINER_SCOPE
23 #include "core/common/container_scope.h"
24 #endif
25
26 #include "native_engine/native_property.h"
27
28 #include "ark_native_engine.h"
29 #include "native_value/ark_native_array.h"
30 #include "native_value/ark_native_array_buffer.h"
31 #include "native_value/ark_native_big_int.h"
32 #include "native_value/ark_native_boolean.h"
33 #include "native_value/ark_native_data_view.h"
34 #include "native_value/ark_native_external.h"
35 #include "native_value/ark_native_function.h"
36 #include "native_value/ark_native_number.h"
37 #include "native_value/ark_native_object.h"
38 #include "native_value/ark_native_string.h"
39 #include "native_value/ark_native_typed_array.h"
40 #include "native_value/ark_native_date.h"
41
42 #ifdef ENABLE_HITRACE
43 #include "hitrace_meter.h"
44 #endif
45 #ifndef PREVIEW
46 #if defined(ECMASCRIPT_SUPPORT_SNAPSHOT)
47 #include "parameters.h"
48 #endif
49 #endif
50 #include "securec.h"
51 #include "utils/log.h"
52 #ifdef ENABLE_HITRACE
53 #include "parameter.h"
54 #endif
55
56 using panda::JsiRuntimeCallInfo;
57 using panda::BooleanRef;
58 using panda::ObjectRef;
59 using panda::StringRef;
60 using panda::FunctionRef;
61 using panda::PrimitiveRef;
62 using panda::JSValueRef;
63 using panda::ArrayBufferRef;
64 using panda::TypedArrayRef;
65 using panda::PromiseCapabilityRef;
66 using panda::NativePointerRef;
67 using panda::SymbolRef;
68 using panda::IntegerRef;
69 using panda::DateRef;
70 using panda::BigIntRef;
71 static constexpr auto PANDA_MAIN_FUNCTION = "_GLOBAL::func_main_0";
72 #ifdef ENABLE_HITRACE
73 constexpr auto NAPI_PROFILER_PARAM_SIZE = 10;
74 #endif
75
76 bool ArkNativeEngineImpl::napiProfilerEnabled {false};
77 bool ArkNativeEngineImpl::napiProfilerParamReaded {false};
78 std::string ArkNativeEngineImpl::tempModuleName_ {""};
79
80 struct MoudleNameLocker {
MoudleNameLockerMoudleNameLocker81 explicit MoudleNameLocker(std::string moduleName)
82 {
83 ArkNativeEngineImpl::tempModuleName_ = moduleName;
84 }
~MoudleNameLockerMoudleNameLocker85 ~MoudleNameLocker()
86 {
87 ArkNativeEngineImpl::tempModuleName_ = "";
88 }
89 };
90
ArkNativeEngineImpl(EcmaVM * vm,NativeEngine * engine,void * jsEngine)91 ArkNativeEngineImpl::ArkNativeEngineImpl(
92 EcmaVM* vm, NativeEngine* engine, void* jsEngine) : NativeEngineInterface(engine, jsEngine),
93 vm_(vm), topScope_(vm)
94 {
95 #ifdef ENABLE_HITRACE
96 if (!ArkNativeEngineImpl::napiProfilerParamReaded) {
97 char napiProfilerParam[NAPI_PROFILER_PARAM_SIZE] = {0};
98 int ret = GetParameter("persist.hiviewdfx.napiprofiler.enabled", "false",
99 napiProfilerParam, sizeof(napiProfilerParam));
100 if (ret > 0 && strcmp(napiProfilerParam, "true") == 0) {
101 ArkNativeEngineImpl::napiProfilerEnabled = true;
102 }
103 ArkNativeEngineImpl::napiProfilerParamReaded = true;
104 }
105 #endif
106 Local<StringRef> requireInternalName = StringRef::NewFromUtf8(vm, "requireInternal");
107 void* requireData = static_cast<void*>(this);
108
109 Local<FunctionRef> requireNapi =
110 FunctionRef::New(
111 vm,
112 [](JsiRuntimeCallInfo *info) -> Local<JSValueRef> {
113 EcmaVM *ecmaVm = info->GetVM();
114 panda::EscapeLocalScope scope(ecmaVm);
115 NativeModuleManager* moduleManager = NativeModuleManager::GetInstance();
116 ArkNativeEngineImpl* engineImpl = static_cast<ArkNativeEngineImpl*>(info->GetData());
117 Local<StringRef> moduleName(info->GetCallArgRef(0));
118 bool isAppModule = false;
119 const uint32_t lengthMax = 2;
120 if (info->GetArgsNumber() >= lengthMax) {
121 Local<BooleanRef> ret(info->GetCallArgRef(1));
122 isAppModule = ret->Value();
123 }
124 NativeModule* module;
125 if (info->GetArgsNumber() == 3) {
126 Local<StringRef> path(info->GetCallArgRef(2));
127 module =
128 moduleManager->LoadNativeModule(moduleName->ToString().c_str(), path->ToString().c_str(),
129 isAppModule, false, true);
130 } else {
131 module =
132 moduleManager->LoadNativeModule(moduleName->ToString().c_str(), nullptr, isAppModule, false,
133 true);
134 }
135
136 Local<JSValueRef> exports(JSValueRef::Undefined(ecmaVm));
137 if (module != nullptr) {
138 auto it = engineImpl->loadedModules_.find(module);
139 if (it != engineImpl->loadedModules_.end()) {
140 return scope.Escape(it->second.ToLocal(ecmaVm));
141 }
142 ArkNativeEngine* nativeEngine = new ArkNativeEngine(engineImpl, engineImpl->GetJsEngine(),
143 isAppModule);
144 std::string strModuleName = moduleName->ToString();
145 moduleManager->SetNativeEngine(strModuleName, nativeEngine);
146 MoudleNameLocker nameLocker(strModuleName);
147 if (module->jsCode != nullptr) {
148 char fileName[NAPI_PATH_MAX] = { 0 };
149 const char* name = module->name;
150 if (sprintf_s(fileName, sizeof(fileName), "lib%s.z.so/%s.js", name, name) == -1) {
151 HILOG_ERROR("sprintf_s file name failed");
152 return scope.Escape(exports);
153 }
154 HILOG_DEBUG("load js code from %{public}s", fileName);
155 NativeValue* exportObject = nativeEngine->LoadArkModule(module->jsCode,
156 module->jsCodeLen, fileName);
157 if (exportObject == nullptr) {
158 HILOG_ERROR("load module failed");
159 return scope.Escape(exports);
160 } else {
161 Global<JSValueRef> globalExports = *exportObject;
162 exports = globalExports.ToLocal(ecmaVm);
163 engineImpl->loadedModules_[module] = Global<JSValueRef>(ecmaVm, exports);
164 }
165 } else if (module->registerCallback != nullptr) {
166 NativeValue* exportObject = nativeEngine->CreateObject();
167 auto arkNativeEngine = static_cast<ArkNativeEngine*>(engineImpl->GetRootNativeEngine());
168 if (!arkNativeEngine) {
169 HILOG_ERROR("init module failed");
170 return scope.Escape(exports);
171 }
172 #ifdef ENABLE_HITRACE
173 StartTrace(HITRACE_TAG_ACE, "NAPI module init, name = " + std::string(module->name));
174 #endif
175 ArkNativeObject* exportObj = reinterpret_cast<ArkNativeObject*>(exportObject);
176 engineImpl->SetModuleName(exportObj, module->name);
177 module->registerCallback(arkNativeEngine, exportObject);
178 #ifdef ENABLE_HITRACE
179 FinishTrace(HITRACE_TAG_ACE);
180 #endif
181 Global<JSValueRef> globalExports = *exportObject;
182 exports = globalExports.ToLocal(ecmaVm);
183 engineImpl->loadedModules_[module] = Global<JSValueRef>(ecmaVm, exports);
184 } else {
185 HILOG_ERROR("init module failed");
186 return scope.Escape(exports);
187 }
188 }
189 return scope.Escape(exports);
190 },
191 nullptr,
192 requireData);
193
194 Local<FunctionRef> requireInternal =
195 FunctionRef::New(
196 vm,
197 [](JsiRuntimeCallInfo *info) -> Local<JSValueRef> {
198 EcmaVM *ecmaVm = info->GetVM();
199 panda::EscapeLocalScope scope(ecmaVm);
200 NativeModuleManager* moduleManager = NativeModuleManager::GetInstance();
201 ArkNativeEngineImpl* engineImpl = static_cast<ArkNativeEngineImpl*>(info->GetData());
202 Local<StringRef> moduleName(info->GetCallArgRef(0));
203 NativeModule* module = moduleManager->LoadNativeModule(moduleName->ToString().c_str(), nullptr, false);
204 Local<JSValueRef> exports(JSValueRef::Undefined(ecmaVm));
205 MoudleNameLocker nameLocker(moduleName->ToString());
206 if (module != nullptr && engineImpl) {
207 auto it = engineImpl->loadedModules_.find(module);
208 if (it != engineImpl->loadedModules_.end()) {
209 return scope.Escape(it->second.ToLocal(ecmaVm));
210 }
211 ArkNativeEngine* nativeEngine = new ArkNativeEngine(engineImpl, engineImpl->GetJsEngine(), false);
212 std::string strModuleName = moduleName->ToString();
213 moduleManager->SetNativeEngine(strModuleName, nativeEngine);
214
215 NativeValue* exportObject = nativeEngine->CreateObject();
216 if (exportObject != nullptr) {
217 auto arkNativeEngine = static_cast<ArkNativeEngine*>(engineImpl->GetRootNativeEngine());
218 if (!arkNativeEngine) {
219 HILOG_ERROR("exportObject is nullptr");
220 return scope.Escape(exports);
221 }
222 ArkNativeObject* exportObj = reinterpret_cast<ArkNativeObject*>(exportObject);
223 engineImpl->SetModuleName(exportObj, module->name);
224 module->registerCallback(arkNativeEngine, exportObject);
225 Global<JSValueRef> globalExports = *exportObject;
226 exports = globalExports.ToLocal(ecmaVm);
227 engineImpl->loadedModules_[module] = Global<JSValueRef>(ecmaVm, exports);
228 } else {
229 HILOG_ERROR("exportObject is nullptr");
230 return scope.Escape(exports);
231 }
232 }
233 return scope.Escape(exports);
234 },
235 nullptr,
236 requireData);
237
238 Local<ObjectRef> global = panda::JSNApi::GetGlobalObject(vm);
239 #if !defined(PREVIEW)
240 Local<StringRef> requireName = StringRef::NewFromUtf8(vm, "requireNapi");
241 global->Set(vm, requireName, requireNapi);
242 #else
243 Local<StringRef> requireNapiPreview = StringRef::NewFromUtf8(vm, "requireNapiPreview");
244 global->Set(vm, requireNapiPreview, requireNapi);
245 #endif
246 global->Set(vm, requireInternalName, requireInternal);
247 JSNApi::SetNativePtrGetter(vm, reinterpret_cast<void*>(ArkNativeFunction::GetNativePtrCallBack));
248 // need to call init of base class.
249 Init();
250 panda::JSNApi::SetLoop(vm, loop_);
251 }
252
~ArkNativeEngineImpl()253 ArkNativeEngineImpl::~ArkNativeEngineImpl()
254 {
255 // need to call deinit before base class.
256 HILOG_INFO("ArkNativeEngineImpl::~ArkNativeEngineImpl");
257 Deinit();
258
259 // Free cached objects
260 for (auto&& [module, exportObj] : loadedModules_) {
261 exportObj.FreeGlobalHandleAddr();
262 }
263 // Free callbackRef
264 if (promiseRejectCallbackRef_ != nullptr) {
265 delete promiseRejectCallbackRef_;
266 }
267 if (checkCallbackRef_ != nullptr) {
268 delete checkCallbackRef_;
269 }
270 }
271
GetModuleFromName(NativeEngine * engine,const std::string & moduleName,bool isAppModule,const std::string & id,const std::string & param,const std::string & instanceName,void ** instance)272 panda::Local<panda::ObjectRef> ArkNativeEngineImpl::GetModuleFromName(NativeEngine* engine,
273 const std::string& moduleName, bool isAppModule, const std::string& id, const std::string& param,
274 const std::string& instanceName, void** instance)
275 {
276 panda::EscapeLocalScope scope(vm_);
277 Local<ObjectRef> exports(JSValueRef::Undefined(vm_));
278 NativeModuleManager* moduleManager = NativeModuleManager::GetInstance();
279 NativeModule* module = moduleManager->LoadNativeModule(moduleName.c_str(), nullptr, isAppModule);
280 if (module != nullptr) {
281 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
282 NativeValue* idValue = chunk.New<ArkNativeString>(static_cast<ArkNativeEngine*>(engine), id.c_str(), id.size());
283 NativeValue* paramValue = chunk.New<ArkNativeString>(
284 static_cast<ArkNativeEngine*>(engine), param.c_str(), param.size());
285 NativeValue* exportObject = chunk.New<ArkNativeObject>(static_cast<ArkNativeEngine*>(engine));
286
287 NativePropertyDescriptor idProperty, paramProperty;
288 idProperty.utf8name = "id";
289 idProperty.value = idValue;
290 paramProperty.utf8name = "param";
291 paramProperty.value = paramValue;
292
293 ArkNativeObject* exportObj = reinterpret_cast<ArkNativeObject*>(exportObject);
294 SetModuleName(exportObj, module->name);
295 exportObj->DefineProperty(idProperty);
296 exportObj->DefineProperty(paramProperty);
297 MoudleNameLocker nameLocker(module->name);
298 module->registerCallback(engine, exportObject);
299
300 napi_value nExport = reinterpret_cast<napi_value>(exportObject);
301 napi_value exportInstance = nullptr;
302 napi_status status = napi_get_named_property(
303 reinterpret_cast<napi_env>(engine), nExport, instanceName.c_str(), &exportInstance);
304 if (status != napi_ok) {
305 HILOG_ERROR("GetModuleFromName napi_get_named_property status != napi_ok");
306 }
307
308 status = napi_unwrap(reinterpret_cast<napi_env>(engine), exportInstance, reinterpret_cast<void**>(instance));
309 if (status != napi_ok) {
310 HILOG_ERROR("GetModuleFromName napi_unwrap status != napi_ok");
311 }
312
313 Global<ObjectRef> globalExports = *exportObject;
314 exports = globalExports.ToLocal(vm_);
315 }
316 return scope.Escape(exports);
317 }
318
LoadModuleByName(ArkNativeEngine * engine,const std::string & moduleName,bool isAppModule,const std::string & param,const std::string & instanceName,void * instance,const std::string & path)319 panda::Local<panda::ObjectRef> ArkNativeEngineImpl::LoadModuleByName(ArkNativeEngine* engine,
320 const std::string& moduleName, bool isAppModule, const std::string& param, const std::string& instanceName,
321 void* instance, const std::string& path)
322 {
323 panda::EscapeLocalScope scope(vm_);
324 Local<ObjectRef> exports(JSValueRef::Undefined(vm_));
325 NativeModuleManager* moduleManager = NativeModuleManager::GetInstance();
326 NativeModule* module =
327 moduleManager->LoadNativeModule(moduleName.c_str(), path.empty() ? nullptr : path.c_str(), isAppModule);
328 if (module != nullptr) {
329 NativeValue* exportObject = new ArkNativeObject(static_cast<ArkNativeEngine*>(engine));
330 ArkNativeObject* exportObj = reinterpret_cast<ArkNativeObject*>(exportObject);
331
332 NativePropertyDescriptor paramProperty, instanceProperty;
333
334 NativeValue* paramValue =
335 new ArkNativeString(static_cast<ArkNativeEngine*>(engine), param.c_str(), param.size());
336 paramProperty.utf8name = "param";
337 paramProperty.value = paramValue;
338
339 auto instanceValue = new ArkNativeObject(static_cast<ArkNativeEngine*>(engine));
340 instanceValue->SetNativePointer(instance, nullptr, nullptr);
341 instanceProperty.utf8name = instanceName.c_str();
342 instanceProperty.value = instanceValue;
343
344 SetModuleName(exportObj, module->name);
345 exportObj->DefineProperty(paramProperty);
346 exportObj->DefineProperty(instanceProperty);
347
348 MoudleNameLocker nameLocker(module->name);
349 module->registerCallback(engine, exportObject);
350 Global<ObjectRef> globalExports = *exportObject;
351 exports = globalExports.ToLocal(vm_);
352 }
353 return scope.Escape(exports);
354 }
355
Loop(LoopMode mode,bool needSync)356 void ArkNativeEngineImpl::Loop(LoopMode mode, bool needSync)
357 {
358 LocalScope scope(vm_);
359 NativeEngineInterface::Loop(mode, needSync);
360 panda::JSNApi::ExecutePendingJob(vm_);
361 }
362
GetGlobal(NativeEngine * engine)363 NativeValue* ArkNativeEngineImpl::GetGlobal(NativeEngine* engine)
364 {
365 LocalScope scope(vm_);
366 Local<ObjectRef> value = panda::JSNApi::GetGlobalObject(vm_);
367 return ArkValueToNativeValue(static_cast<ArkNativeEngine*>(engine), value);
368 }
369
CreateNull(NativeEngine * engine)370 NativeValue* ArkNativeEngineImpl::CreateNull(NativeEngine* engine)
371 {
372 LocalScope scope(vm_);
373 Local<PrimitiveRef> value = JSValueRef::Null(vm_);
374 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
375 return chunk.New<ArkNativeValue>(static_cast<ArkNativeEngine*>(engine), value);
376 }
377
CreateUndefined(NativeEngine * engine)378 NativeValue* ArkNativeEngineImpl::CreateUndefined(NativeEngine* engine)
379 {
380 LocalScope scope(vm_);
381 Local<PrimitiveRef> value = JSValueRef::Undefined(vm_);
382 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
383 return chunk.New<ArkNativeValue>(static_cast<ArkNativeEngine*>(engine), value);
384 }
385
CreateBoolean(NativeEngine * engine,bool value)386 NativeValue* ArkNativeEngineImpl::CreateBoolean(NativeEngine* engine, bool value)
387 {
388 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
389 return chunk.New<ArkNativeBoolean>(static_cast<ArkNativeEngine*>(engine), value);
390 }
391
CreateNumber(NativeEngine * engine,int32_t value)392 NativeValue* ArkNativeEngineImpl::CreateNumber(NativeEngine* engine, int32_t value)
393 {
394 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
395 return chunk.New<ArkNativeNumber>(static_cast<ArkNativeEngine*>(engine), value);
396 }
397
CreateNumber(NativeEngine * engine,uint32_t value)398 NativeValue* ArkNativeEngineImpl::CreateNumber(NativeEngine* engine, uint32_t value)
399 {
400 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
401 return chunk.New<ArkNativeNumber>(static_cast<ArkNativeEngine*>(engine), value);
402 }
403
CreateNumber(NativeEngine * engine,int64_t value)404 NativeValue* ArkNativeEngineImpl::CreateNumber(NativeEngine* engine, int64_t value)
405 {
406 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
407 return chunk.New<ArkNativeNumber>(static_cast<ArkNativeEngine*>(engine), value);
408 }
409
CreateNumber(NativeEngine * engine,double value)410 NativeValue* ArkNativeEngineImpl::CreateNumber(NativeEngine* engine, double value)
411 {
412 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
413 return chunk.New<ArkNativeNumber>(static_cast<ArkNativeEngine*>(engine), value);
414 }
415
CreateBigInt(NativeEngine * engine,int64_t value)416 NativeValue* ArkNativeEngineImpl::CreateBigInt(NativeEngine* engine, int64_t value)
417 {
418 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
419 return chunk.New<ArkNativeBigInt>(static_cast<ArkNativeEngine*>(engine), value);
420 }
421
CreateBigInt(NativeEngine * engine,uint64_t value)422 NativeValue* ArkNativeEngineImpl::CreateBigInt(NativeEngine* engine, uint64_t value)
423 {
424 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
425 return chunk.New<ArkNativeBigInt>(static_cast<ArkNativeEngine*>(engine), value, true);
426 }
427
CreateString(NativeEngine * engine,const char * value,size_t length)428 NativeValue* ArkNativeEngineImpl::CreateString(NativeEngine* engine, const char* value, size_t length)
429 {
430 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
431 return chunk.New<ArkNativeString>(static_cast<ArkNativeEngine*>(engine), value, length);
432 }
433
CreateString16(NativeEngine * engine,const char16_t * value,size_t length)434 NativeValue* ArkNativeEngineImpl::CreateString16(NativeEngine* engine, const char16_t* value, size_t length)
435 {
436 return nullptr;
437 }
438
CreateSymbol(NativeEngine * engine,NativeValue * value)439 NativeValue* ArkNativeEngineImpl::CreateSymbol(NativeEngine* engine, NativeValue* value)
440 {
441 LocalScope scope(vm_);
442 Global<StringRef> str = *value;
443 Local<SymbolRef> symbol = SymbolRef::New(vm_, str.ToLocal(vm_));
444 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
445 return chunk.New<ArkNativeValue>(static_cast<ArkNativeEngine*>(engine), symbol);
446 }
447
CreateExternal(NativeEngine * engine,void * value,NativeFinalize callback,void * hint,size_t nativeBindingSize)448 NativeValue* ArkNativeEngineImpl::CreateExternal(NativeEngine* engine, void* value, NativeFinalize callback,
449 void* hint, size_t nativeBindingSize)
450 {
451 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
452 return chunk.New<ArkNativeExternal>(
453 static_cast<ArkNativeEngine*>(engine), value, callback, hint, nativeBindingSize);
454 }
455
CreateObject(NativeEngine * engine)456 NativeValue* ArkNativeEngineImpl::CreateObject(NativeEngine* engine)
457 {
458 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
459 return chunk.New<ArkNativeObject>(static_cast<ArkNativeEngine*>(engine));
460 }
461
CreateNativeBindingObject(NativeEngine * engine,void * detach,void * attach)462 NativeValue* ArkNativeEngineImpl::CreateNativeBindingObject(NativeEngine* engine, void* detach, void* attach)
463 {
464 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
465 return chunk.New<ArkNativeObject>(static_cast<ArkNativeEngine*>(engine), detach, attach);
466 }
467
CreateNBObject(NativeEngine * engine,DetachCallback detach,AttachCallback attach)468 NativeValue* ArkNativeEngineImpl::CreateNBObject(NativeEngine* engine, DetachCallback detach, AttachCallback attach)
469 {
470 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
471 return chunk.New<ArkNativeObject>(static_cast<ArkNativeEngine*>(engine), detach, attach);
472 }
473
CreateFunction(NativeEngine * engine,const char * name,size_t length,NativeCallback cb,void * value)474 NativeValue* ArkNativeEngineImpl::CreateFunction(
475 NativeEngine* engine, const char* name, size_t length, NativeCallback cb, void* value)
476 {
477 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
478 return chunk.New<ArkNativeFunction>(static_cast<ArkNativeEngine*>(engine), name, length, cb, value);
479 }
480
CreateArray(NativeEngine * engine,size_t length)481 NativeValue* ArkNativeEngineImpl::CreateArray(NativeEngine* engine, size_t length)
482 {
483 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
484 return chunk.New<ArkNativeArray>(static_cast<ArkNativeEngine*>(engine), length);
485 }
486
CreateArrayBuffer(NativeEngine * engine,void ** value,size_t length)487 NativeValue* ArkNativeEngineImpl::CreateArrayBuffer(NativeEngine* engine, void** value, size_t length)
488 {
489 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
490 return chunk.New<ArkNativeArrayBuffer>(static_cast<ArkNativeEngine*>(engine), (uint8_t**)value, length);
491 }
492
CreateArrayBufferExternal(NativeEngine * engine,void * value,size_t length,NativeFinalize cb,void * hint)493 NativeValue* ArkNativeEngineImpl::CreateArrayBufferExternal(
494 NativeEngine* engine, void* value, size_t length, NativeFinalize cb, void* hint)
495 {
496 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
497 return chunk.New<ArkNativeArrayBuffer>(static_cast<ArkNativeEngine*>(engine), (uint8_t*)value, length, cb, hint);
498 }
499
CreateTypedArray(NativeEngine * engine,NativeTypedArrayType type,NativeValue * value,size_t length,size_t offset)500 NativeValue* ArkNativeEngineImpl::CreateTypedArray(
501 NativeEngine* engine, NativeTypedArrayType type, NativeValue* value, size_t length, size_t offset)
502 {
503 LocalScope scope(vm_);
504 Global<ArrayBufferRef> globalBuffer = *value;
505 Local<ArrayBufferRef> buffer = globalBuffer.ToLocal(vm_);
506 Local<TypedArrayRef> typedArray(JSValueRef::Undefined(vm_));
507
508 switch (type) {
509 case NATIVE_INT8_ARRAY:
510 typedArray = panda::Int8ArrayRef::New(vm_, buffer, offset, length);
511 break;
512 case NATIVE_UINT8_ARRAY:
513 typedArray = panda::Uint8ArrayRef::New(vm_, buffer, offset, length);
514 break;
515 case NATIVE_UINT8_CLAMPED_ARRAY:
516 typedArray = panda::Uint8ClampedArrayRef::New(vm_, buffer, offset, length);
517 break;
518 case NATIVE_INT16_ARRAY:
519 typedArray = panda::Int16ArrayRef::New(vm_, buffer, offset, length);
520 break;
521 case NATIVE_UINT16_ARRAY:
522 typedArray = panda::Uint16ArrayRef::New(vm_, buffer, offset, length);
523 break;
524 case NATIVE_INT32_ARRAY:
525 typedArray = panda::Int32ArrayRef::New(vm_, buffer, offset, length);
526 break;
527 case NATIVE_UINT32_ARRAY:
528 typedArray = panda::Uint32ArrayRef::New(vm_, buffer, offset, length);
529 break;
530 case NATIVE_FLOAT32_ARRAY:
531 typedArray = panda::Float32ArrayRef::New(vm_, buffer, offset, length);
532 break;
533 case NATIVE_FLOAT64_ARRAY:
534 typedArray = panda::Float64ArrayRef::New(vm_, buffer, offset, length);
535 break;
536 case NATIVE_BIGINT64_ARRAY:
537 typedArray = panda::BigInt64ArrayRef::New(vm_, buffer, offset, length);
538 break;
539 case NATIVE_BIGUINT64_ARRAY:
540 typedArray = panda::BigUint64ArrayRef::New(vm_, buffer, offset, length);
541 break;
542 default:
543 return nullptr;
544 }
545 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
546 return chunk.New<ArkNativeTypedArray>(static_cast<ArkNativeEngine*>(engine), typedArray);
547 }
548
CreateDataView(NativeEngine * engine,NativeValue * value,size_t length,size_t offset)549 NativeValue* ArkNativeEngineImpl::CreateDataView(NativeEngine* engine, NativeValue* value, size_t length, size_t offset)
550 {
551 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
552 return chunk.New<ArkNativeDataView>(static_cast<ArkNativeEngine*>(engine), value, length, offset);
553 }
554
CreatePromise(NativeEngine * engine,NativeDeferred ** deferred)555 NativeValue* ArkNativeEngineImpl::CreatePromise(NativeEngine* engine, NativeDeferred** deferred)
556 {
557 LocalScope scope(vm_);
558 Local<PromiseCapabilityRef> capability = PromiseCapabilityRef::New(vm_);
559 *deferred = new ArkNativeDeferred(static_cast<ArkNativeEngine*>(engine), capability);
560
561 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
562 return chunk.New<ArkNativeObject>(static_cast<ArkNativeEngine*>(engine), capability->GetPromise(vm_));
563 }
564
CreateError(NativeEngine * engine,NativeValue * code,NativeValue * message)565 NativeValue* ArkNativeEngineImpl::CreateError(NativeEngine* engine, NativeValue* code, NativeValue* message)
566 {
567 LocalScope scope(vm_);
568 Local<JSValueRef> errorVal = panda::Exception::Error(vm_, *message);
569 if (code != nullptr) {
570 Local<StringRef> codeKey = StringRef::NewFromUtf8(vm_, "code");
571 Local<ObjectRef> errorObj(errorVal);
572 errorObj->Set(vm_, codeKey, *code);
573 }
574 return ArkValueToNativeValue(static_cast<ArkNativeEngine*>(engine), errorVal);
575 }
576
ConcurrentCallbackFunc(Local<JSValueRef> val,Local<JSValueRef> hint,void * data)577 static void ConcurrentCallbackFunc(Local<JSValueRef> val, Local<JSValueRef> hint, void *data)
578 {
579 if (data == nullptr) {
580 return;
581 }
582
583 auto engine = static_cast<ArkNativeEngine *>(data);
584 ArkNativeEngineImpl* engineImpl = static_cast<ArkNativeEngineImpl*>(engine->GetNativeEngineImpl());
585 auto concurrentCallbackFunc = engineImpl->GetConcurrentCallbackFunc();
586 if (concurrentCallbackFunc == nullptr) {
587 return;
588 }
589
590 auto value = ArkNativeEngineImpl::ArkValueToNativeValue(engine, val);
591 auto hintVal = ArkNativeEngineImpl::ArkValueToNativeValue(engine, hint);
592 concurrentCallbackFunc(engine, value, hintVal);
593 }
594
InitTaskPoolThread(NativeEngine * engine,NapiConcurrentCallback callback)595 bool ArkNativeEngineImpl::InitTaskPoolThread(NativeEngine* engine, NapiConcurrentCallback callback)
596 {
597 concurrentCallbackFunc_ = callback;
598 return JSNApi::InitForConcurrentThread(vm_, ConcurrentCallbackFunc, static_cast<void *>(engine));
599 }
600
InitTaskPoolFunc(NativeEngine * engine,NativeValue * func)601 bool ArkNativeEngineImpl::InitTaskPoolFunc(NativeEngine* engine, NativeValue* func)
602 {
603 if (func == nullptr) {
604 return false;
605 }
606 LocalScope scope(vm_);
607 Local<JSValueRef> function = JSValueRef::Undefined(vm_);
608 if (func != nullptr) {
609 Global<JSValueRef> globalObj = *func;
610 function = globalObj.ToLocal(vm_);
611 }
612 return JSNApi::InitForConcurrentFunction(vm_, function);
613 }
614
CallFunction(NativeEngine * engine,NativeValue * thisVar,NativeValue * function,NativeValue * const * argv,size_t argc)615 NativeValue* ArkNativeEngineImpl::CallFunction(
616 NativeEngine* engine, NativeValue* thisVar, NativeValue* function, NativeValue* const *argv, size_t argc)
617 {
618 if (function == nullptr) {
619 return nullptr;
620 }
621 LocalScope scope(vm_);
622 NativeScopeManager* scopeManager = engine->GetScopeManager();
623 if (scopeManager == nullptr) {
624 HILOG_ERROR("scope manager is null");
625 return nullptr;
626 }
627 NativeScope* nativeScope = scopeManager->Open();
628 Local<JSValueRef> thisObj = JSValueRef::Undefined(vm_);
629 if (thisVar != nullptr) {
630 Global<JSValueRef> globalObj = *thisVar;
631 thisObj = globalObj.ToLocal(vm_);
632 }
633 Global<FunctionRef> funcObj = *function;
634 #ifdef ENABLE_CONTAINER_SCOPE
635 auto nativeFunction = static_cast<NativeFunction*>(function->GetInterface(NativeFunction::INTERFACE_ID));
636 if (nativeFunction == nullptr) {
637 HILOG_ERROR("nativeFunction is null");
638 return nullptr;
639 }
640 auto arkNativeFunc = static_cast<ArkNativeFunction*>(nativeFunction);
641 OHOS::Ace::ContainerScope containerScope(arkNativeFunc->GetScopeId());
642 #endif
643 std::vector<Local<JSValueRef>> args;
644 args.reserve(argc);
645 for (size_t i = 0; i < argc; i++) {
646 if (argv[i] != nullptr) {
647 Global<JSValueRef> arg = *argv[i];
648 args.emplace_back(arg.ToLocal(vm_));
649 } else {
650 args.emplace_back(JSValueRef::Undefined(vm_));
651 }
652 }
653
654 Local<JSValueRef> value = funcObj->Call(vm_, thisObj, args.data(), argc);
655 if (panda::JSNApi::HasPendingException(vm_)) {
656 HandleUncaughtException(engine);
657 return nullptr;
658 }
659
660 scopeManager->Close(nativeScope);
661 return ArkValueToNativeValue(static_cast<ArkNativeEngine*>(engine), value);
662 }
663
RunScript(NativeEngine * engine,NativeValue * script)664 NativeValue* ArkNativeEngineImpl::RunScript(NativeEngine* engine, NativeValue* script)
665 {
666 // not support yet
667 return nullptr;
668 }
669
RunScriptPath(NativeEngine * engine,const char * path)670 NativeValue* ArkNativeEngineImpl::RunScriptPath(NativeEngine* engine, const char* path)
671 {
672 panda::JSExecutionScope executionScope(vm_);
673 LocalScope scope(vm_);
674 [[maybe_unused]] bool ret = panda::JSNApi::Execute(vm_, path, PANDA_MAIN_FUNCTION);
675
676 if (panda::JSNApi::HasPendingException(vm_)) {
677 HandleUncaughtException(engine);
678 return nullptr;
679 }
680 return CreateUndefined(engine);
681 }
682
RunScriptBuffer(NativeEngine * engine,const char * path,std::vector<uint8_t> & buffer,bool isBundle)683 NativeValue* ArkNativeEngineImpl::RunScriptBuffer(
684 NativeEngine* engine, const char* path, std::vector<uint8_t>& buffer, bool isBundle)
685 {
686 panda::JSExecutionScope executionScope(vm_);
687 LocalScope scope(vm_);
688 [[maybe_unused]] bool ret = false;
689 if (isBundle) {
690 ret = panda::JSNApi::Execute(vm_, buffer.data(), buffer.size(), PANDA_MAIN_FUNCTION, path);
691 } else {
692 ret = panda::JSNApi::ExecuteModuleBuffer(vm_, buffer.data(), buffer.size(), path);
693 }
694
695 if (panda::JSNApi::HasPendingException(vm_)) {
696 HandleUncaughtException(engine);
697 return nullptr;
698 }
699 return CreateUndefined(engine);
700 }
701
SetPackagePath(const std::string appLibPathKey,const std::vector<std::string> & packagePath)702 void ArkNativeEngineImpl::SetPackagePath(const std::string appLibPathKey, const std::vector<std::string>& packagePath)
703 {
704 auto moduleManager = NativeModuleManager::GetInstance();
705 if (moduleManager && !packagePath.empty()) {
706 moduleManager->SetAppLibPath(appLibPathKey, packagePath);
707 }
708 }
709
DefineClass(NativeEngine * engine,const char * name,NativeCallback callback,void * data,const NativePropertyDescriptor * properties,size_t length)710 NativeValue* ArkNativeEngineImpl::DefineClass(NativeEngine* engine, const char* name,
711 NativeCallback callback, void* data, const NativePropertyDescriptor* properties, size_t length)
712 {
713 LocalScope scope(vm_);
714 std::string className(name);
715 if (ArkNativeEngineImpl::napiProfilerEnabled) {
716 className = ArkNativeEngineImpl::tempModuleName_ + "." + name;
717 }
718 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
719 auto classConstructor = chunk.New<ArkNativeFunction>(static_cast<ArkNativeEngine*>(engine),
720 className.c_str(), callback, data);
721 SetModuleName(classConstructor, className);
722 auto classPrototype = classConstructor->GetFunctionPrototype();
723
724 for (size_t i = 0; i < length; i++) {
725 if (properties[i].attributes & NATIVE_STATIC) {
726 classConstructor->DefineProperty(properties[i]);
727 } else {
728 if (classPrototype == nullptr) {
729 HILOG_ERROR("ArkNativeEngineImpl::Class's prototype is null");
730 continue;
731 }
732 ArkNativeObject* arkNativeobj = static_cast<ArkNativeObject*>(classPrototype);
733 SetModuleName(arkNativeobj, className);
734 arkNativeobj->DefineProperty(properties[i]);
735 }
736 }
737
738 return classConstructor;
739 }
740
CreateInstance(NativeEngine * engine,NativeValue * constructor,NativeValue * const * argv,size_t argc)741 NativeValue* ArkNativeEngineImpl::CreateInstance(
742 NativeEngine* engine, NativeValue* constructor, NativeValue* const *argv, size_t argc)
743 {
744 if (constructor == nullptr) {
745 return nullptr;
746 }
747 LocalScope scope(vm_);
748 Global<FunctionRef> value = *constructor;
749
750 std::vector<Local<JSValueRef>> args;
751 args.reserve(argc);
752 for (size_t i = 0; i < argc; i++) {
753 if (argv[i] != nullptr) {
754 Global<JSValueRef> arg = *argv[i];
755 args.emplace_back(arg.ToLocal(vm_));
756 } else {
757 args.emplace_back(JSValueRef::Undefined(vm_));
758 }
759 }
760 Local<JSValueRef> instance = value->Constructor(vm_, args.data(), argc);
761 Local<ObjectRef> excep = panda::JSNApi::GetUncaughtException(vm_);
762 if (!excep.IsNull()) {
763 HILOG_ERROR("ArkNativeEngineImpl::CreateInstance occur Exception");
764 return nullptr;
765 }
766 return ArkValueToNativeValue(static_cast<ArkNativeEngine*>(engine), instance);
767 }
768
CreateReference(NativeEngine * engine,NativeValue * value,uint32_t initialRefcount,NativeFinalize callback,void * data,void * hint)769 NativeReference* ArkNativeEngineImpl::CreateReference(
770 NativeEngine* engine, NativeValue* value, uint32_t initialRefcount, NativeFinalize callback, void* data, void* hint)
771 {
772 return new ArkNativeReference(static_cast<ArkNativeEngine*>(engine), value, initialRefcount, false);
773 }
774
IsExceptionPending() const775 bool ArkNativeEngineImpl::IsExceptionPending() const
776 {
777 return lastException_ != nullptr;
778 }
779
GetAndClearLastException(NativeEngine * engine)780 NativeValue* ArkNativeEngineImpl::GetAndClearLastException(NativeEngine* engine)
781 {
782 NativeValue* temp = lastException_;
783 lastException_ = nullptr;
784 if (IsMainThread() || temp != nullptr) {
785 return temp;
786 }
787
788 // Worker need handle all exception
789 LocalScope scope(vm_);
790 Local<ObjectRef> exception = panda::JSNApi::GetAndClearUncaughtException(vm_);
791 if (exception.IsNull()) {
792 return nullptr;
793 }
794
795 return ArkValueToNativeValue(static_cast<ArkNativeEngine*>(engine), exception);
796 }
797
Throw(NativeValue * error)798 bool ArkNativeEngineImpl::Throw(NativeValue* error)
799 {
800 LocalScope scope(vm_);
801 Global<JSValueRef> errorVal = *error;
802 panda::JSNApi::ThrowException(vm_, errorVal.ToLocal(vm_));
803 lastException_ = error;
804 return true;
805 }
806
Throw(NativeEngine * engine,NativeErrorType type,const char * code,const char * message)807 bool ArkNativeEngineImpl::Throw(NativeEngine* engine, NativeErrorType type, const char* code, const char* message)
808 {
809 LocalScope scope(vm_);
810 Local<JSValueRef> error(JSValueRef::Undefined(vm_));
811 switch (type) {
812 case NATIVE_COMMON_ERROR:
813 error = panda::Exception::Error(vm_, StringRef::NewFromUtf8(vm_, message));
814 break;
815 case NATIVE_TYPE_ERROR:
816 error = panda::Exception::TypeError(vm_, StringRef::NewFromUtf8(vm_, message));
817 break;
818 case NATIVE_RANGE_ERROR:
819 error = panda::Exception::RangeError(vm_, StringRef::NewFromUtf8(vm_, message));
820 break;
821 default:
822 return false;
823 }
824 if (code != nullptr) {
825 Local<JSValueRef> codeKey = StringRef::NewFromUtf8(vm_, "code");
826 Local<JSValueRef> codeValue = StringRef::NewFromUtf8(vm_, code);
827 Local<ObjectRef> errorObj(error);
828 errorObj->Set(vm_, codeKey, codeValue);
829 }
830
831 panda::JSNApi::ThrowException(vm_, error);
832 lastException_ = ArkValueToNativeValue(static_cast<ArkNativeEngine*>(engine), error);
833 return true;
834 }
835
CreateRuntimeFunc(NativeEngine * engine,void * jsEngine)836 NativeEngine* ArkNativeEngineImpl::CreateRuntimeFunc(NativeEngine* engine, void* jsEngine)
837 {
838 panda::RuntimeOption option;
839 #if defined(OHOS_PLATFORM) && !defined(IOS_PLATFORM)
840 int arkProperties = OHOS::system::GetIntParameter<int>("persist.ark.properties", -1);
841 std::string bundleName = OHOS::system::GetParameter("persist.ark.arkbundlename", "");
842 size_t gcThreadNum = OHOS::system::GetUintParameter<size_t>("persist.ark.gcthreads", 7);
843 size_t longPauseTime = OHOS::system::GetUintParameter<size_t>("persist.ark.longpausetime", 40);
844 bool asmInterpreterEnabled = OHOS::system::GetBoolParameter("persist.ark.asminterpreter", true);
845 std::string asmOpcodeDisableRange = OHOS::system::GetParameter("persist.ark.asmopcodedisablerange", "");
846 option.SetArkProperties(arkProperties);
847 option.SetArkBundleName(bundleName);
848 option.SetGcThreadNum(gcThreadNum);
849 option.SetLongPauseTime(longPauseTime);
850 option.SetEnableAsmInterpreter(asmInterpreterEnabled);
851 option.SetAsmOpcodeDisableRange(asmOpcodeDisableRange);
852 option.SetIsWorker();
853 HILOG_INFO("ArkNativeEngineImpl::CreateRuntimeFunc ark properties = %{public}d, bundlename = %{public}s",
854 arkProperties, bundleName.c_str());
855 #endif
856 option.SetGcType(panda::RuntimeOption::GC_TYPE::GEN_GC);
857 const int64_t poolSize = 0x1000000;
858 option.SetGcPoolSize(poolSize);
859 #if !defined(PREVIEW) && !defined(IOS_PLATFORM)
860 option.SetLogLevel(panda::RuntimeOption::LOG_LEVEL::INFO);
861 #endif
862 option.SetDebuggerLibraryPath("");
863 EcmaVM* vm = panda::JSNApi::CreateJSVM(option);
864 if (vm == nullptr) {
865 return nullptr;
866 }
867 // worker adaptation mergeabc
868 const panda::ecmascript::EcmaVM* hostVM = reinterpret_cast<ArkNativeEngine*>(engine)->GetEcmaVm();
869 panda::JSNApi::SetBundle(vm, panda::JSNApi::IsBundle(const_cast<EcmaVM*>(hostVM)));
870 panda::JSNApi::SetBundleName(vm, panda::JSNApi::GetBundleName(const_cast<EcmaVM*>(hostVM)));
871 panda::JSNApi::SetModuleName(vm, panda::JSNApi::GetModuleName(const_cast<EcmaVM*>(hostVM)));
872 panda::JSNApi::SetAssetPath(vm, panda::JSNApi::GetAssetPath(const_cast<EcmaVM*>(hostVM)));
873 ArkNativeEngine* arkEngine = new ArkNativeEngine(vm, jsEngine);
874 // init callback
875 arkEngine->RegisterWorkerFunction(engine);
876
877 auto cleanEnv = [vm]() {
878 if (vm != nullptr) {
879 HILOG_INFO("cleanEnv is called");
880 panda::JSNApi::DestroyJSVM(vm);
881 }
882 };
883 arkEngine->SetCleanEnv(cleanEnv);
884
885 if (hostVM != nullptr) {
886 panda::JSNApi::addWorker(const_cast<EcmaVM*>(hostVM), vm);
887 }
888 return arkEngine;
889 }
890
CreateRuntime(NativeEngine * engine)891 void* ArkNativeEngineImpl::CreateRuntime(NativeEngine* engine)
892 {
893 return ArkNativeEngineImpl::CreateRuntimeFunc(engine, jsEngineInterface_);
894 }
895
Serialize(NativeEngine * context,NativeValue * value,NativeValue * transfer)896 NativeValue* ArkNativeEngineImpl::Serialize(NativeEngine* context, NativeValue* value, NativeValue* transfer)
897 {
898 const panda::ecmascript::EcmaVM* vm = reinterpret_cast<ArkNativeEngine*>(context)->GetEcmaVm();
899 LocalScope scope(vm);
900 Global<JSValueRef> arkValue = *value;
901 Global<JSValueRef> arkTransfer = *transfer;
902 void* result = panda::JSNApi::SerializeValue(vm, arkValue.ToLocal(vm), arkTransfer.ToLocal(vm));
903 return reinterpret_cast<NativeValue*>(result);
904 }
905
Deserialize(NativeEngine * engine,NativeEngine * context,NativeValue * recorder)906 NativeValue* ArkNativeEngineImpl::Deserialize(NativeEngine* engine, NativeEngine* context, NativeValue* recorder)
907 {
908 const panda::ecmascript::EcmaVM* vm = reinterpret_cast<ArkNativeEngine*>(context)->GetEcmaVm();
909 LocalScope scope(vm);
910 Local<JSValueRef> result = panda::JSNApi::DeserializeValue(vm, recorder, reinterpret_cast<void*>(context));
911 return ArkValueToNativeValue(static_cast<ArkNativeEngine*>(engine), result);
912 }
913
DeleteSerializationData(NativeValue * value) const914 void ArkNativeEngineImpl::DeleteSerializationData(NativeValue* value) const
915 {
916 void* data = reinterpret_cast<void*>(value);
917 panda::JSNApi::DeleteSerializationData(data);
918 }
919
920 #if defined(ECMASCRIPT_SUPPORT_CPUPROFILER)
StartCpuProfiler(const std::string & fileName)921 void ArkNativeEngineImpl::StartCpuProfiler(const std::string& fileName)
922 {
923 JSNApi::SetNativePtrGetter(vm_, reinterpret_cast<void*>(ArkNativeFunction::GetNativePtrCallBack));
924 DFXJSNApi::StartCpuProfilerForFile(vm_, fileName);
925 }
926
StopCpuProfiler()927 void ArkNativeEngineImpl::StopCpuProfiler()
928 {
929 DFXJSNApi::StopCpuProfilerForFile(vm_);
930 JSNApi::SetNativePtrGetter(vm_, nullptr);
931 }
932 #else
StartCpuProfiler(const std::string & fileName)933 void ArkNativeEngineImpl::StartCpuProfiler(const std::string& fileName)
934 {
935 HILOG_WARN("ARKCpuProfiler is not supported on windows");
936 }
937
StopCpuProfiler()938 void ArkNativeEngineImpl::StopCpuProfiler()
939 {
940 HILOG_WARN("ARKCpuProfiler is not supported on windows");
941 }
942 #endif
943 #if defined(ECMASCRIPT_SUPPORT_SNAPSHOT)
ResumeVM()944 void ArkNativeEngineImpl::ResumeVM()
945 {
946 DFXJSNApi::ResumeVM(vm_);
947 }
948
SuspendVM()949 bool ArkNativeEngineImpl::SuspendVM()
950 {
951 return DFXJSNApi::SuspendVM(vm_);
952 }
953
IsSuspended()954 bool ArkNativeEngineImpl::IsSuspended()
955 {
956 return DFXJSNApi::IsSuspended(vm_);
957 }
958
CheckSafepoint()959 bool ArkNativeEngineImpl::CheckSafepoint()
960 {
961 return DFXJSNApi::CheckSafepoint(vm_);
962 }
963 #else
ResumeVM()964 void ArkNativeEngineImpl::ResumeVM()
965 {
966 HILOG_WARN("ARK Snapshot is not supported on windows");
967 }
968
SuspendVM()969 bool ArkNativeEngineImpl::SuspendVM()
970 {
971 HILOG_WARN("ARK Snapshot is not supported on windows");
972 return false;
973 }
974
IsSuspended()975 bool ArkNativeEngineImpl::IsSuspended()
976 {
977 HILOG_WARN("ARK Snapshot is not supported on windows");
978 return false;
979 }
980
CheckSafepoint()981 bool ArkNativeEngineImpl::CheckSafepoint()
982 {
983 HILOG_WARN("ARK Snapshot is not supported on windows");
984 return false;
985 }
986 #endif
987
RunBufferScript(NativeEngine * engine,std::vector<uint8_t> & buffer)988 NativeValue* ArkNativeEngineImpl::RunBufferScript(NativeEngine* engine, std::vector<uint8_t>& buffer)
989 {
990 panda::JSExecutionScope executionScope(vm_);
991 LocalScope scope(vm_);
992 [[maybe_unused]] bool ret = panda::JSNApi::Execute(vm_, buffer.data(), buffer.size(), PANDA_MAIN_FUNCTION);
993
994 if (panda::JSNApi::HasPendingException(vm_)) {
995 HandleUncaughtException(engine);
996 return nullptr;
997 }
998 return CreateUndefined(engine);
999 }
1000
RunActor(NativeEngine * engine,std::vector<uint8_t> & buffer,const char * descriptor)1001 NativeValue* ArkNativeEngineImpl::RunActor(NativeEngine* engine, std::vector<uint8_t>& buffer, const char* descriptor)
1002 {
1003 panda::JSExecutionScope executionScope(vm_);
1004 LocalScope scope(vm_);
1005 std::string desc(descriptor);
1006 [[maybe_unused]] bool ret = false;
1007 if (panda::JSNApi::IsBundle(vm_)) {
1008 ret = panda::JSNApi::Execute(vm_, buffer.data(), buffer.size(), PANDA_MAIN_FUNCTION, desc);
1009 } else {
1010 ret = panda::JSNApi::Execute(vm_, desc, PANDA_MAIN_FUNCTION);
1011 }
1012
1013 if (panda::JSNApi::HasPendingException(vm_)) {
1014 HandleUncaughtException(engine);
1015 return nullptr;
1016 }
1017 return CreateUndefined(engine);
1018 }
1019
LoadArkModule(NativeEngine * engine,const char * str,int32_t len,const std::string & fileName)1020 NativeValue* ArkNativeEngineImpl::LoadArkModule(
1021 NativeEngine* engine, const char* str, int32_t len, const std::string& fileName)
1022 {
1023 HILOG_DEBUG("ArkNativeEngineImpl::LoadModule start, buffer = %{public}s", str);
1024 if (str == nullptr || len <= 0 || fileName.empty()) {
1025 HILOG_ERROR("fileName is nullptr or source code is nullptr");
1026 return nullptr;
1027 }
1028
1029 bool res = JSNApi::ExecuteModuleFromBuffer(vm_, str, len, fileName);
1030 if (!res) {
1031 HILOG_ERROR("Execute module failed");
1032 return nullptr;
1033 }
1034
1035 LocalScope scope(vm_);
1036 Local<ObjectRef> exportObj = JSNApi::GetExportObjectFromBuffer(vm_, fileName, "default");
1037 if (exportObj->IsNull()) {
1038 HILOG_ERROR("Get export object failed");
1039 return nullptr;
1040 }
1041
1042 HILOG_DEBUG("ArkNativeEngineImpl::LoadModule end");
1043 return ArkValueToNativeValue(static_cast<ArkNativeEngine*>(engine), exportObj);
1044 }
1045
LoadModule(NativeEngine * engine,NativeValue * str,const std::string & fileName)1046 NativeValue* ArkNativeEngineImpl::LoadModule(NativeEngine* engine, NativeValue* str, const std::string& fileName)
1047 {
1048 return nullptr;
1049 }
1050
ArkValueToNativeValue(ArkNativeEngine * engine,Local<JSValueRef> value)1051 NativeValue* ArkNativeEngineImpl::ArkValueToNativeValue(ArkNativeEngine* engine, Local<JSValueRef> value)
1052 {
1053 NativeValue* result = nullptr;
1054 NativeChunk& chunk = engine->GetNativeChunk();
1055 if (value->IsNull() || value->IsUndefined() || value->IsSymbol()) {
1056 result = chunk.New<ArkNativeValue>(engine, value);
1057 } else if (value->IsNumber()) {
1058 result = chunk.New<ArkNativeNumber>(engine, value);
1059 } else if (value->IsString()) {
1060 result = chunk.New<ArkNativeString>(engine, value);
1061 } else if (value->IsArray(engine->GetEcmaVm())) {
1062 result = chunk.New<ArkNativeArray>(engine, value);
1063 } else if (value->IsFunction()) {
1064 result = chunk.New<ArkNativeFunction>(engine, value);
1065 } else if (value->IsArrayBuffer()) {
1066 result = chunk.New<ArkNativeArrayBuffer>(engine, value);
1067 } else if (value->IsDataView()) {
1068 result = chunk.New<ArkNativeDataView>(engine, value);
1069 } else if (value->IsTypedArray()) {
1070 result = chunk.New<ArkNativeTypedArray>(engine, value);
1071 } else if (value->IsNativePointer()) {
1072 result = chunk.New<ArkNativeExternal>(engine, value);
1073 } else if (value->IsDate()) {
1074 result = chunk.New<ArkNativeDate>(engine, value);
1075 } else if (value->IsBigInt()) {
1076 result = chunk.New<ArkNativeBigInt>(engine, value);
1077 } else if (value->IsObject() || value->IsPromise()) {
1078 result = chunk.New<ArkNativeObject>(engine, value);
1079 } else if (value->IsBoolean()) {
1080 result = chunk.New<ArkNativeBoolean>(engine, value);
1081 } else {
1082 result = chunk.New<ArkNativeValue>(engine, value);
1083 }
1084 return result;
1085 }
1086
ValueToNativeValue(NativeEngine * engine,JSValueWrapper & value)1087 NativeValue* ArkNativeEngineImpl::ValueToNativeValue(NativeEngine* engine, JSValueWrapper& value)
1088 {
1089 LocalScope scope(vm_);
1090 Global<JSValueRef> arkValue = value;
1091 return ArkValueToNativeValue(static_cast<ArkNativeEngine*>(engine), arkValue.ToLocal(vm_));
1092 }
1093
ExecuteJsBin(const std::string & fileName)1094 bool ArkNativeEngineImpl::ExecuteJsBin(const std::string& fileName)
1095 {
1096 panda::JSExecutionScope executionScope(vm_);
1097 LocalScope scope(vm_);
1098 bool ret = JSNApi::Execute(vm_, fileName, PANDA_MAIN_FUNCTION);
1099 return ret;
1100 }
1101
CreateBuffer(NativeEngine * engine,void ** value,size_t length)1102 NativeValue* ArkNativeEngineImpl::CreateBuffer(NativeEngine* engine, void** value, size_t length)
1103 {
1104 return nullptr;
1105 }
1106
CreateBufferCopy(NativeEngine * engine,void ** value,size_t length,const void * data)1107 NativeValue* ArkNativeEngineImpl::CreateBufferCopy(NativeEngine* engine, void** value, size_t length, const void* data)
1108 {
1109 return nullptr;
1110 }
1111
CreateBufferExternal(NativeEngine * engine,void * value,size_t length,NativeFinalize cb,void * hint)1112 NativeValue* ArkNativeEngineImpl::CreateBufferExternal(
1113 NativeEngine* engine, void* value, size_t length, NativeFinalize cb, void* hint)
1114 {
1115 return nullptr;
1116 }
1117
CreateDate(NativeEngine * engine,double value)1118 NativeValue* ArkNativeEngineImpl::CreateDate(NativeEngine* engine, double value)
1119 {
1120 LocalScope scope(vm_);
1121 return ArkValueToNativeValue(static_cast<ArkNativeEngine*>(engine), DateRef::New(vm_, value));
1122 }
1123
CreateBigWords(NativeEngine * engine,int sign_bit,size_t word_count,const uint64_t * words)1124 NativeValue* ArkNativeEngineImpl::CreateBigWords(
1125 NativeEngine* engine, int sign_bit, size_t word_count, const uint64_t* words)
1126 {
1127 constexpr int bigintMod = 2; // 2 : used for even number judgment
1128 bool sign = false;
1129 if ((sign_bit % bigintMod) == 1) {
1130 sign = true;
1131 }
1132 uint32_t size = (uint32_t)word_count;
1133
1134 LocalScope scope(vm_);
1135 Local<JSValueRef> value = BigIntRef::CreateBigWords(vm_, sign, size, words);
1136
1137 NativeChunk& chunk = static_cast<ArkNativeEngine*>(engine)->GetNativeChunk();
1138 return chunk.New<ArkNativeBigInt>(static_cast<ArkNativeEngine*>(engine), value);
1139 }
1140
TriggerFatalException(NativeValue * error)1141 bool ArkNativeEngineImpl::TriggerFatalException(NativeValue* error)
1142 {
1143 return true;
1144 }
1145
AdjustExternalMemory(int64_t ChangeInBytes,int64_t * AdjustedValue)1146 bool ArkNativeEngineImpl::AdjustExternalMemory(int64_t ChangeInBytes, int64_t* AdjustedValue)
1147 {
1148 return true;
1149 }
1150
SetPromiseRejectCallback(NativeEngine * engine,NativeReference * rejectCallbackRef,NativeReference * checkCallbackRef)1151 void ArkNativeEngineImpl::SetPromiseRejectCallback(
1152 NativeEngine* engine, NativeReference* rejectCallbackRef, NativeReference* checkCallbackRef)
1153 {
1154 if (rejectCallbackRef == nullptr || checkCallbackRef == nullptr) {
1155 HILOG_ERROR("rejectCallbackRef or checkCallbackRef is nullptr");
1156 return;
1157 }
1158 promiseRejectCallbackRef_ = rejectCallbackRef;
1159 checkCallbackRef_ = checkCallbackRef;
1160 JSNApi::SetHostPromiseRejectionTracker(
1161 vm_, reinterpret_cast<void*>(PromiseRejectCallback), reinterpret_cast<void*>(engine));
1162 }
1163
PromiseRejectCallback(void * info)1164 void ArkNativeEngineImpl::PromiseRejectCallback(void* info)
1165 {
1166 panda::PromiseRejectInfo* promiseRejectInfo = reinterpret_cast<panda::PromiseRejectInfo*>(info);
1167 ArkNativeEngine* env = reinterpret_cast<ArkNativeEngine*>(promiseRejectInfo->GetData());
1168
1169 if (env == nullptr) {
1170 HILOG_ERROR("engine is nullptr");
1171 return;
1172 }
1173
1174 ArkNativeEngineImpl* engineImpl = static_cast<ArkNativeEngineImpl*>(env->GetNativeEngineImpl());
1175 if (engineImpl == nullptr) {
1176 HILOG_ERROR("engine impl is nullptr");
1177 return;
1178 }
1179 if (engineImpl->promiseRejectCallbackRef_ == nullptr || engineImpl->checkCallbackRef_ == nullptr) {
1180 HILOG_ERROR("promiseRejectCallbackRef or checkCallbackRef is nullptr");
1181 return;
1182 }
1183
1184 panda::ecmascript::EcmaVM* vm = engineImpl->GetEcmaVm();
1185 LocalScope scope(vm);
1186 Local<JSValueRef> promise = promiseRejectInfo->GetPromise();
1187 Local<JSValueRef> reason = promiseRejectInfo->GetReason();
1188 panda::PromiseRejectInfo::PROMISE_REJECTION_EVENT operation = promiseRejectInfo->GetOperation();
1189 Local<JSValueRef> type(IntegerRef::New(vm, static_cast<int32_t>(operation)));
1190
1191 Local<JSValueRef> args[] = {type, promise, reason};
1192 Global<FunctionRef> promiseRejectCallback = *(engineImpl->promiseRejectCallbackRef_->Get());
1193 if (!promiseRejectCallback.IsEmpty()) {
1194 promiseRejectCallback->Call(vm, JSValueRef::Undefined(vm), args, 3); // 3 args size
1195 }
1196
1197 if (operation == panda::PromiseRejectInfo::PROMISE_REJECTION_EVENT::REJECT) {
1198 Global<JSValueRef> checkCallback = *(engineImpl->checkCallbackRef_->Get());
1199 if (!checkCallback.IsEmpty()) {
1200 JSNApi::SetHostEnqueueJob(vm, checkCallback.ToLocal(vm));
1201 }
1202 }
1203 }
1204
1205 #if defined(ECMASCRIPT_SUPPORT_SNAPSHOT)
DumpHeapSnapshot(const std::string & path,bool isVmMode,DumpFormat dumpFormat)1206 void ArkNativeEngineImpl::DumpHeapSnapshot(const std::string& path, bool isVmMode, DumpFormat dumpFormat)
1207 {
1208 if (dumpFormat == DumpFormat::JSON) {
1209 DFXJSNApi::DumpHeapSnapshot(vm_, 0, path, isVmMode);
1210 }
1211 if (dumpFormat == DumpFormat::BINARY) {
1212 DFXJSNApi::DumpHeapSnapshot(vm_, 1, path, isVmMode);
1213 }
1214 if (dumpFormat == DumpFormat::OTHER) {
1215 DFXJSNApi::DumpHeapSnapshot(vm_, 2, path, isVmMode); // 2:enum is 2
1216 }
1217 }
1218
DumpHeapSnapshotExt(bool isVmMode,DumpFormat dumpFormat,bool isPrivate)1219 void ArkNativeEngineImpl::DumpHeapSnapshotExt(bool isVmMode, DumpFormat dumpFormat, bool isPrivate)
1220 {
1221 if (dumpFormat == DumpFormat::JSON) {
1222 DFXJSNApi::DumpHeapSnapshot(vm_, 0, isVmMode, isPrivate);
1223 }
1224 if (dumpFormat == DumpFormat::BINARY) {
1225 DFXJSNApi::DumpHeapSnapshot(vm_, 1, isVmMode, isPrivate);
1226 }
1227 if (dumpFormat == DumpFormat::OTHER) {
1228 DFXJSNApi::DumpHeapSnapshot(vm_, 2, isVmMode, isPrivate); // 2:enum is 2
1229 }
1230 }
1231 #else
DumpHeapSnapshot(const std::string & path,bool isVmMode,DumpFormat dumpFormat)1232 void ArkNativeEngineImpl::DumpHeapSnapshot(const std::string& path, bool isVmMode, DumpFormat dumpFormat)
1233 {
1234 HILOG_WARN("ARK does not support snapshot on windows");
1235 }
1236
DumpHeapSnapshotExt(bool isVmMode,DumpFormat dumpFormat,bool isPrivate)1237 void ArkNativeEngineImpl::DumpHeapSnapshotExt(bool isVmMode, DumpFormat dumpFormat, bool isPrivate)
1238 {
1239 HILOG_WARN("ARK does not support snapshot on windows");
1240 }
1241 #endif
1242
1243 #if !defined(PREVIEW) && !defined(IOS_PLATFORM)
BuildNativeAndJsStackTrace(std::string & stackTraceStr)1244 bool ArkNativeEngineImpl::BuildNativeAndJsStackTrace(std::string& stackTraceStr)
1245 {
1246 return DFXJSNApi::BuildNativeAndJsStackTrace(vm_, stackTraceStr);
1247 }
1248 #else
BuildNativeAndJsStackTrace(std::string & stackTraceStr)1249 bool ArkNativeEngineImpl::BuildNativeAndJsStackTrace(std::string& stackTraceStr)
1250 {
1251 HILOG_WARN("ARK does not support dfx on windows");
1252 return false;
1253 }
1254 #endif
1255
1256 #if !defined(PREVIEW) && !defined(IOS_PLATFORM)
BuildJsStackTrace(std::string & stackTraceStr)1257 bool ArkNativeEngineImpl::BuildJsStackTrace(std::string& stackTraceStr)
1258 {
1259 return DFXJSNApi::BuildJsStackTrace(vm_, stackTraceStr);
1260 }
1261 #else
BuildJsStackTrace(std::string & stackTraceStr)1262 bool ArkNativeEngineImpl::BuildJsStackTrace(std::string& stackTraceStr)
1263 {
1264 HILOG_WARN("ARK does not support dfx on windows");
1265 return false;
1266 }
1267 #endif
1268
1269 #if !defined(PREVIEW) && !defined(IOS_PLATFORM)
BuildJsStackInfoList(uint32_t tid,std::vector<JsFrameInfo> & jsFrames)1270 bool ArkNativeEngineImpl::BuildJsStackInfoList(uint32_t tid, std::vector<JsFrameInfo>& jsFrames)
1271 {
1272 std::vector<ArkJsFrameInfo> arkJsFrames;
1273 bool sign = DFXJSNApi::BuildJsStackInfoList(vm_, tid, arkJsFrames);
1274 for (auto jf : arkJsFrames) {
1275 struct JsFrameInfo jsframe;
1276 jsframe.fileName = jf.fileName;
1277 jsframe.functionName = jf.functionName;
1278 jsframe.pos = jf.pos;
1279 jsframe.nativePointer = jf.nativePointer;
1280 jsFrames.emplace_back(jsframe);
1281 }
1282 return sign;
1283 }
1284 #else
BuildJsStackInfoList(uint32_t tid,std::vector<JsFrameInfo> & jsFrames)1285 bool ArkNativeEngineImpl::BuildJsStackInfoList(uint32_t tid, std::vector<JsFrameInfo>& jsFrames)
1286 {
1287 HILOG_WARN("ARK does not support dfx on windows");
1288 return false;
1289 }
1290 #endif
1291
1292 #if !defined(PREVIEW)
DeleteWorker(NativeEngine * hostEngine,NativeEngine * workerEngine)1293 bool ArkNativeEngineImpl::DeleteWorker(NativeEngine* hostEngine, NativeEngine* workerEngine)
1294 {
1295 const panda::ecmascript::EcmaVM* hostVM = reinterpret_cast<ArkNativeEngine*>(hostEngine)->GetEcmaVm();
1296 const panda::ecmascript::EcmaVM* workerVM = reinterpret_cast<ArkNativeEngine*>(workerEngine)->GetEcmaVm();
1297 if (hostVM != nullptr && workerVM != nullptr) {
1298 return panda::JSNApi::DeleteWorker(const_cast<EcmaVM*>(hostVM), const_cast<EcmaVM*>(workerVM));
1299 }
1300 return false;
1301 }
1302 #else
DeleteWorker(NativeEngine * engine,NativeEngine * workerEngine)1303 bool ArkNativeEngineImpl::DeleteWorker(NativeEngine* engine, NativeEngine* workerEngine)
1304 {
1305 HILOG_WARN("ARK does not support dfx on windows");
1306 return false;
1307 }
1308 #endif
1309
1310 #if defined(ECMASCRIPT_SUPPORT_SNAPSHOT)
StartHeapTracking(double timeInterval,bool isVmMode)1311 bool ArkNativeEngineImpl::StartHeapTracking(double timeInterval, bool isVmMode)
1312 {
1313 return DFXJSNApi::StartHeapTracking(vm_, timeInterval, isVmMode);
1314 }
1315 #else
StartHeapTracking(double timeInterval,bool isVmMode)1316 bool ArkNativeEngineImpl::StartHeapTracking(double timeInterval, bool isVmMode)
1317 {
1318 HILOG_WARN("ARK does not support snapshot on windows");
1319 return false;
1320 }
1321 #endif
1322
1323 #if defined(ECMASCRIPT_SUPPORT_SNAPSHOT)
StopHeapTracking(const std::string & filePath)1324 bool ArkNativeEngineImpl::StopHeapTracking(const std::string& filePath)
1325 {
1326 return DFXJSNApi::StopHeapTracking(vm_, filePath);
1327 }
1328 #else
StopHeapTracking(const std::string & filePath)1329 bool ArkNativeEngineImpl::StopHeapTracking(const std::string& filePath)
1330 {
1331 HILOG_WARN("ARK does not support snapshot on windows");
1332 return false;
1333 }
1334 #endif
1335
1336 #if !defined(PREVIEW) && !defined(IOS_PLATFORM)
PrintStatisticResult()1337 void ArkNativeEngineImpl::PrintStatisticResult()
1338 {
1339 DFXJSNApi::PrintStatisticResult(vm_);
1340 }
1341
StartRuntimeStat()1342 void ArkNativeEngineImpl::StartRuntimeStat()
1343 {
1344 DFXJSNApi::StartRuntimeStat(vm_);
1345 }
1346
StopRuntimeStat()1347 void ArkNativeEngineImpl::StopRuntimeStat()
1348 {
1349 DFXJSNApi::StopRuntimeStat(vm_);
1350 }
1351
GetArrayBufferSize()1352 size_t ArkNativeEngineImpl::GetArrayBufferSize()
1353 {
1354 return DFXJSNApi::GetArrayBufferSize(vm_);
1355 }
1356
GetHeapTotalSize()1357 size_t ArkNativeEngineImpl::GetHeapTotalSize()
1358 {
1359 return DFXJSNApi::GetHeapTotalSize(vm_);
1360 }
1361
GetHeapUsedSize()1362 size_t ArkNativeEngineImpl::GetHeapUsedSize()
1363 {
1364 return DFXJSNApi::GetHeapUsedSize(vm_);
1365 }
1366
NotifyApplicationState(bool inBackground)1367 void ArkNativeEngineImpl::NotifyApplicationState(bool inBackground)
1368 {
1369 DFXJSNApi::NotifyApplicationState(vm_, inBackground);
1370 }
1371
NotifyIdleTime(int idleMicroSec)1372 void ArkNativeEngineImpl::NotifyIdleTime(int idleMicroSec)
1373 {
1374 DFXJSNApi::NotifyIdleTime(vm_, idleMicroSec);
1375 }
1376
NotifyMemoryPressure(bool inHighMemoryPressure)1377 void ArkNativeEngineImpl::NotifyMemoryPressure(bool inHighMemoryPressure)
1378 {
1379 DFXJSNApi::NotifyMemoryPressure(vm_, inHighMemoryPressure);
1380 }
1381 #else
PrintStatisticResult()1382 void ArkNativeEngineImpl::PrintStatisticResult()
1383 {
1384 HILOG_WARN("ARK does not support dfx on windows");
1385 }
1386
StartRuntimeStat()1387 void ArkNativeEngineImpl::StartRuntimeStat()
1388 {
1389 HILOG_WARN("ARK does not support dfx on windows");
1390 }
1391
StopRuntimeStat()1392 void ArkNativeEngineImpl::StopRuntimeStat()
1393 {
1394 HILOG_WARN("ARK does not support dfx on windows");
1395 }
1396
GetArrayBufferSize()1397 size_t ArkNativeEngineImpl::GetArrayBufferSize()
1398 {
1399 HILOG_WARN("ARK does not support dfx on windows");
1400 return 0;
1401 }
1402
GetHeapTotalSize()1403 size_t ArkNativeEngineImpl::GetHeapTotalSize()
1404 {
1405 HILOG_WARN("ARK does not support dfx on windows");
1406 return 0;
1407 }
1408
GetHeapUsedSize()1409 size_t ArkNativeEngineImpl::GetHeapUsedSize()
1410 {
1411 HILOG_WARN("ARK does not support dfx on windows");
1412 return 0;
1413 }
1414
NotifyApplicationState(bool inBackground)1415 void ArkNativeEngineImpl::NotifyApplicationState([[maybe_unused]] bool inBackground)
1416 {
1417 HILOG_WARN("ARK does not support dfx on windows");
1418 }
1419
NotifyIdleTime(int idleMicroSec)1420 void ArkNativeEngineImpl::NotifyIdleTime([[maybe_unused]] int idleMicroSec)
1421 {
1422 HILOG_WARN("ARK does not support dfx on windows");
1423 }
1424
NotifyMemoryPressure(bool inHighMemoryPressure)1425 void ArkNativeEngineImpl::NotifyMemoryPressure([[maybe_unused]] bool inHighMemoryPressure)
1426 {
1427 HILOG_WARN("ARK does not support dfx on windows");
1428 }
1429 #endif
1430
RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback)1431 void ArkNativeEngineImpl::RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback)
1432 {
1433 JSNApi::EnableUserUncaughtErrorHandler(vm_);
1434 uncaughtExceptionCallback_ = callback;
1435 }
1436
HandleUncaughtException(NativeEngine * engine)1437 void ArkNativeEngineImpl::HandleUncaughtException(NativeEngine* engine)
1438 {
1439 if (uncaughtExceptionCallback_ == nullptr) {
1440 return;
1441 }
1442
1443 LocalScope scope(vm_);
1444 Local<ObjectRef> exception = JSNApi::GetAndClearUncaughtException(vm_);
1445 if (!exception.IsEmpty() && !exception->IsHole()) {
1446 uncaughtExceptionCallback_(ArkValueToNativeValue(static_cast<ArkNativeEngine*>(engine), exception));
1447 }
1448 }
1449
HasPendingException()1450 bool ArkNativeEngineImpl::HasPendingException()
1451 {
1452 return panda::JSNApi::HasPendingException(vm_);
1453 }
1454
SetModuleName(ArkNativeObject * nativeObj,std::string moduleName)1455 inline void ArkNativeEngineImpl::SetModuleName(ArkNativeObject *nativeObj, std::string moduleName)
1456 {
1457 #ifdef ENABLE_HITRACE
1458 if (ArkNativeEngineImpl::napiProfilerEnabled) {
1459 nativeObj->SetModuleName(moduleName);
1460 }
1461 #endif
1462 }
1463
IsMixedDebugEnabled()1464 bool ArkNativeEngineImpl::IsMixedDebugEnabled()
1465 {
1466 return JSNApi::IsMixedDebugEnabled(vm_);
1467 }
1468
NotifyNativeCalling(const void * nativeAddress)1469 void ArkNativeEngineImpl::NotifyNativeCalling(const void *nativeAddress)
1470 {
1471 JSNApi::NotifyNativeCalling(vm_, nativeAddress);
1472 }
1473