mirror of
https://github.com/openharmony/ace_napi.git
synced 2026-07-21 16:55:22 -04:00
Support actor function
When the worker thread is started the part shared by the virtual machine is not recreated only the part that cannot be shared is created issue:https://gitee.com/openharmony/js_worker_module/issues/I4SMW8 Signed-off-by: y00576111 <yaojian16@huawei.com> Change-Id: I0237c1d71e903f2a3807fa02c7d915ab3f694f24
This commit is contained in:
@@ -31,6 +31,8 @@ NAPI_EXTERN napi_status napi_delete_serialization_data(napi_env env, napi_value
|
||||
NAPI_EXTERN napi_status napi_get_exception_info_for_worker(napi_env env, napi_value obj);
|
||||
NAPI_EXTERN napi_status napi_run_buffer_script(napi_env env, std::vector<uint8_t>& buffer, napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_run_script_path(napi_env env, const char* patch, napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_run_actor(napi_env env, std::vector<uint8_t>& buffer,
|
||||
const char* descriptor, napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_set_promise_rejection_callback(napi_env env, napi_ref ref, napi_ref checkRef);
|
||||
|
||||
NAPI_EXTERN napi_status napi_is_arguments_object(napi_env env, napi_value value, bool* result);
|
||||
|
||||
@@ -635,15 +635,27 @@ NativeValue* ArkNativeEngine::RunBufferScript(std::vector<uint8_t>& buffer)
|
||||
{
|
||||
panda::JSExecutionScope executionScope(vm_);
|
||||
LocalScope scope(vm_);
|
||||
bool ret = panda::JSNApi::Execute(vm_, buffer.data(), buffer.size(), PANDA_MAIN_FUNCTION);
|
||||
[[maybe_unused]] bool ret = panda::JSNApi::Execute(vm_, buffer.data(), buffer.size(), PANDA_MAIN_FUNCTION);
|
||||
|
||||
Local<ObjectRef> excep = panda::JSNApi::GetAndClearUncaughtException(vm_);
|
||||
if (!excep.IsNull()) {
|
||||
Local<StringRef> exceptionMsg = excep->ToString(vm_);
|
||||
exceptionStr_ = exceptionMsg->ToString();
|
||||
return nullptr;
|
||||
}
|
||||
return CreateUndefined();
|
||||
}
|
||||
|
||||
if (!ret) {
|
||||
NativeValue* ArkNativeEngine::RunActor(std::vector<uint8_t>& buffer, const char* descriptor)
|
||||
{
|
||||
panda::JSExecutionScope executionScope(vm_);
|
||||
LocalScope scope(vm_);
|
||||
std::string desc(descriptor);
|
||||
[[maybe_unused]] bool ret = panda::JSNApi::Execute(vm_, buffer.data(), buffer.size(), PANDA_MAIN_FUNCTION, desc);
|
||||
Local<ObjectRef> excep = panda::JSNApi::GetAndClearUncaughtException(vm_);
|
||||
if (!excep.IsNull()) {
|
||||
Local<StringRef> exceptionMsg = excep->ToString(vm_);
|
||||
exceptionStr_ = exceptionMsg->ToString();
|
||||
return nullptr;
|
||||
}
|
||||
return CreateUndefined();
|
||||
|
||||
@@ -133,6 +133,7 @@ public:
|
||||
NativeValue* RunScript(NativeValue* script) override;
|
||||
// Run buffer script
|
||||
NativeValue* RunBufferScript(std::vector<uint8_t>& buffer) override;
|
||||
NativeValue* RunActor(std::vector<uint8_t>& buffer, const char *descriptor) override;
|
||||
// Set lib path
|
||||
void SetPackagePath(const std::string& packagePath);
|
||||
// Define native class
|
||||
|
||||
@@ -282,6 +282,11 @@ NativeValue* JerryScriptNativeEngine::RunBufferScript(std::vector<uint8_t>& buff
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
NativeValue* JerryScriptNativeEngine::RunActor(std::vector<uint8_t>& buffer, const char *descriptor)
|
||||
{
|
||||
return RunBufferScript(buffer);
|
||||
}
|
||||
|
||||
NativeValue* JerryScriptNativeEngine::DefineClass(
|
||||
const char* name, NativeCallback callback, void* data, const NativePropertyDescriptor* properties, size_t length)
|
||||
{
|
||||
|
||||
@@ -67,6 +67,7 @@ public:
|
||||
size_t argc) override;
|
||||
virtual NativeValue* RunScript(NativeValue* script) override;
|
||||
virtual NativeValue* RunBufferScript(std::vector<uint8_t>& buffer) override;
|
||||
virtual NativeValue* RunActor(std::vector<uint8_t>& buffer, const char *descriptor) override;
|
||||
virtual NativeValue* DefineClass(const char* name,
|
||||
NativeCallback callback,
|
||||
void* data,
|
||||
|
||||
@@ -483,6 +483,11 @@ NativeValue* QuickJSNativeEngine::RunBufferScript(std::vector<uint8_t>& buffer)
|
||||
return JSValueToNativeValue(this, result);
|
||||
}
|
||||
|
||||
NativeValue* QuickJSNativeEngine::RunActor(std::vector<uint8_t>& buffer, const char *descriptor)
|
||||
{
|
||||
return RunBufferScript(buffer);
|
||||
}
|
||||
|
||||
NativeValue* QuickJSNativeEngine::LoadModule(NativeValue* str, const std::string& fileName)
|
||||
{
|
||||
if (str == nullptr || fileName.empty()) {
|
||||
|
||||
@@ -101,6 +101,7 @@ public:
|
||||
|
||||
virtual NativeValue* RunScript(NativeValue* script) override;
|
||||
virtual NativeValue* RunBufferScript(std::vector<uint8_t>& buffer) override;
|
||||
virtual NativeValue* RunActor(std::vector<uint8_t>& buffer, const char *descriptor) override;
|
||||
|
||||
void SetPackagePath(const std::string& packagePath);
|
||||
|
||||
|
||||
@@ -496,6 +496,11 @@ NativeValue* V8NativeEngine::RunBufferScript(std::vector<uint8_t>& buffer)
|
||||
return RunScript(script);
|
||||
}
|
||||
|
||||
NativeValue* V8NativeEngine::RunActor(std::vector<uint8_t>& buffer, const char *descriptor)
|
||||
{
|
||||
return RunBufferScript(buffer);
|
||||
}
|
||||
|
||||
namespace {
|
||||
v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* path)
|
||||
{
|
||||
|
||||
@@ -160,6 +160,7 @@ public:
|
||||
virtual NativeValue* RunScript(NativeValue* script) override;
|
||||
// Run buffer script
|
||||
virtual NativeValue* RunBufferScript(std::vector<uint8_t>& buffer) override;
|
||||
virtual NativeValue* RunActor(std::vector<uint8_t>& buffer, const char *descriptor) override;
|
||||
// Define native class
|
||||
virtual NativeValue* DefineClass(const char* name,
|
||||
NativeCallback callback,
|
||||
|
||||
@@ -1782,6 +1782,17 @@ NAPI_EXTERN napi_status napi_run_buffer_script(napi_env env, std::vector<uint8_t
|
||||
return napi_clear_last_error(env);
|
||||
}
|
||||
|
||||
NAPI_EXTERN napi_status napi_run_actor(napi_env env, std::vector<uint8_t>& buffer,
|
||||
const char* descriptor, napi_value* result)
|
||||
{
|
||||
CHECK_ENV(env);
|
||||
CHECK_ARG(env, result);
|
||||
auto engine = reinterpret_cast<NativeEngine*>(env);
|
||||
auto resultValue = engine->RunActor(buffer, descriptor);
|
||||
*result = reinterpret_cast<napi_value>(resultValue);
|
||||
return napi_clear_last_error(env);
|
||||
}
|
||||
|
||||
// Memory management
|
||||
NAPI_INNER_EXTERN napi_status napi_adjust_external_memory(
|
||||
napi_env env, int64_t change_in_bytes, int64_t* adjusted_value)
|
||||
|
||||
@@ -345,10 +345,10 @@ bool NativeEngine::CallInitWorkerFunc(NativeEngine* engine)
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool NativeEngine::CallGetAssetFunc(const std::string& uri, std::vector<uint8_t>& content)
|
||||
bool NativeEngine::CallGetAssetFunc(const std::string& uri, std::vector<uint8_t>& content, std::string& ami)
|
||||
{
|
||||
if (getAssetFunc_ != nullptr) {
|
||||
getAssetFunc_(uri, content);
|
||||
getAssetFunc_(uri, content, ami);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -457,10 +457,11 @@ NativeValue* NativeEngine::RunScript(const char* path)
|
||||
{
|
||||
std::vector<uint8_t> scriptContent;
|
||||
std::string pathStr(path);
|
||||
if (!CallGetAssetFunc(pathStr, scriptContent)) {
|
||||
std::string ami;
|
||||
if (!CallGetAssetFunc(pathStr, scriptContent, ami)) {
|
||||
HILOG_ERROR("Get asset error");
|
||||
return nullptr;
|
||||
}
|
||||
HILOG_INFO("asset size is %{public}zu", scriptContent.size());
|
||||
return RunBufferScript(scriptContent);
|
||||
return RunActor(scriptContent, ami.c_str());
|
||||
}
|
||||
@@ -94,7 +94,7 @@ private:
|
||||
using PostTask = std::function<void(bool needSync)>;
|
||||
using CleanEnv = std::function<void()>;
|
||||
using InitWorkerFunc = std::function<void(NativeEngine* engine)>;
|
||||
using GetAssetFunc = std::function<void(const std::string& uri, std::vector<uint8_t>& content)>;
|
||||
using GetAssetFunc = std::function<void(const std::string& uri, std::vector<uint8_t>& content, std::string& ami)>;
|
||||
using OffWorkerFunc = std::function<void(NativeEngine* engine)>;
|
||||
|
||||
class NAPI_EXPORT NativeEngine {
|
||||
@@ -159,6 +159,7 @@ public:
|
||||
size_t argc) = 0;
|
||||
virtual NativeValue* RunScript(NativeValue* script) = 0;
|
||||
virtual NativeValue* RunBufferScript(std::vector<uint8_t>& buffer) = 0;
|
||||
virtual NativeValue* RunActor(std::vector<uint8_t>& buffer, const char* descriptor) = 0;
|
||||
virtual NativeValue* DefineClass(const char* name,
|
||||
NativeCallback callback,
|
||||
void* data,
|
||||
@@ -246,7 +247,7 @@ public:
|
||||
NativeAsyncCompleteCallback completeCallback);
|
||||
// call init worker func
|
||||
virtual bool CallInitWorkerFunc(NativeEngine* engine);
|
||||
virtual bool CallGetAssetFunc(const std::string& uri, std::vector<uint8_t>& content);
|
||||
virtual bool CallGetAssetFunc(const std::string& uri, std::vector<uint8_t>& content, std::string& ami);
|
||||
virtual bool CallOffWorkerFunc(NativeEngine* engine);
|
||||
virtual bool CallWorkerAsyncWorkFunc(NativeEngine* engine);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user