MCPcopy Create free account
hub / github.com/cocos/cocos-engine / evalString

Function evalString

native/cocos/platform/openharmony/napi/NapiHelper.cpp:483–530  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

481}
482
483Napi::Value evalString(const Napi::CallbackInfo &info){
484 Napi::Env env = info.Env();
485 size_t argc = info.Length();
486 if (argc != 1) {
487 Napi::Error::New(env, "Wrong argument count, 1 expected!").ThrowAsJavaScriptException();
488 return env.Undefined();
489 }
490
491 if (!info[0].IsString()) {
492 Napi::TypeError::New(env, "string expected!").ThrowAsJavaScriptException();
493 return env.Undefined();
494 }
495
496 std::string value = info[0].As<Napi::String>().ToString();
497
498 if (!se::ScriptEngine::getInstance()->isValid()) {
499 CC_LOG_WARNING("ScriptEngine has not been initialized");
500 return env.Undefined();
501 }
502
503 size_t length = value.length();
504 char* cValue = new char[length + 1];
505 strcpy(cValue, value.c_str());
506
507 BaseEngine::SchedulerPtr scheduler =
508 CC_CURRENT_APPLICATION() ? CC_CURRENT_APPLICATION()->getEngine()->getScheduler() : nullptr;
509 if (!scheduler) {
510 delete[] cValue;
511 return env.Undefined();
512 }
513 Napi::Value result = env.Undefined();
514 if(gMainThreadId == std::this_thread::get_id()) {
515 scheduler->performFunctionInCocosThread([cValue, length]() {
516 se::AutoHandleScope hs;
517 se::Value ret;
518 se::ScriptEngine::getInstance()->evalString(cValue, length, &ret);
519 delete[] cValue;
520 });
521 } else {
522 se::AutoHandleScope hs;
523 se::Value ret;
524 se::ScriptEngine::getInstance()->evalString(cValue, length, &ret);
525 delete[] cValue;
526 sevalue_to_napivalue(ret, &result, env);
527 }
528
529 return result;
530}
531
532/* static */
533Napi::Object NapiHelper::init(Napi::Env env, Napi::Object exports) {

Callers 2

runScriptMethod · 0.85
runScriptMethod · 0.85

Calls 13

EnvMethod · 0.80
LengthMethod · 0.80
UndefinedMethod · 0.80
IsStringMethod · 0.80
getEngineMethod · 0.80
sevalue_to_napivalueFunction · 0.70
ToStringMethod · 0.45
isValidMethod · 0.45
lengthMethod · 0.45
getSchedulerMethod · 0.45

Tested by

no test coverage detected