From d9a51dc3b77a916fb7ca5563589605e9f53ca1b8 Mon Sep 17 00:00:00 2001 From: huanghan18 Date: Fri, 25 Apr 2025 17:46:11 +0800 Subject: [PATCH] Adapter for 13.2 V8 Signed-off-by: huanghan18 --- BUILD.gn | 2 +- CMakeLists.txt | 6 +- build_jsvm_inter.sh | 2 + src/js_native_api_v8.cpp | 146 ++++++++++++++++++++++----------------- 4 files changed, 88 insertions(+), 68 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index e1857c9..72f2da1 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -153,7 +153,7 @@ action("build_libjsvm") { "--target_platform", "$target_platform", "--prefix", - rebase_path("$clang_base_path/bin"), + rebase_path("//vendor/huawei/binary/artifacts/js_engine_url/llvm/bin"), "--sysroot", rebase_path("$musl_sysroot"), "--is_asan", diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f46741..03a26c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.28) project(JSVM) # 设置C++标准 -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED True) # 添加编译选项 @@ -35,7 +35,7 @@ add_compile_definitions( -DENABLE_INSPECTOR -DHAVE_OPENSSL=1 -DTARGET_OHOS - -DENABLE_HISYSEVENT + # -DENABLE_HISYSEVENT ) # 添加头文件 @@ -106,6 +106,7 @@ target_link_libraries(jsvm libssl_openssl.z.so libv8_shared.so libhilog.so + c++_static ) else() target_link_libraries(jsvm @@ -121,5 +122,6 @@ target_link_libraries(jsvm libssl_openssl.z.so libv8_shared.so libhilog.so + c++_static ) endif() diff --git a/build_jsvm_inter.sh b/build_jsvm_inter.sh index 33cbb49..7840366 100755 --- a/build_jsvm_inter.sh +++ b/build_jsvm_inter.sh @@ -129,6 +129,7 @@ do_env() { # alway rebuild mkdir -p ${workdir} + argurment+=" -I${PREFIX}/../include/c++/v1 -D__MUSL__ -D_LIBCPP_HAS_MUSL_LIBC -DOHOS_JS_ENGINE" argurment+=" -fstack-protector-strong" argurment+=" -Wl,-z,noexecstack" argurment+=" -Wl,-z,relro" @@ -157,6 +158,7 @@ do_env() { elif [[ "${TARGET_CPU}" = "x86_64" ]]; then cflags="--target=x86_64-linux-ohos" cflags+=" --sysroot=${SYSROOT}" + cflags+=" -isystem ${SYSROOT}/usr/include/x86_64-linux-ohos" cflags+=" -DV8_OS_OH=1" ARCH="x86_64" else diff --git a/src/js_native_api_v8.cpp b/src/js_native_api_v8.cpp index 7ba4d7c..99d6444 100644 --- a/src/js_native_api_v8.cpp +++ b/src/js_native_api_v8.cpp @@ -583,7 +583,6 @@ public: void GetArgs(JSVM_Value* buffer, size_t bufferlength) override {} -protected: void NameSetterInvokeCallback() { auto context = cbinfo.GetIsolate()->GetCurrentContext(); @@ -619,27 +618,28 @@ protected: this->SetReturnValue(result); } } - - void NameGetterInvokeCallback() + void IndexSetterInvokeCallback() { auto context = cbinfo.GetIsolate()->GetCurrentContext(); auto env = v8impl::GetContextEnv(context); - auto getterCb = propertyHandler->namedGetterCallback; + auto indexSetterCb = propertyHandler->indexedSetterCallback; JSVM_Value innerData = nullptr; - if (propertyHandler->namedPropertyData != nullptr) { + if (propertyHandler->indexedPropertyData != nullptr) { v8impl::UserReference* reference = - reinterpret_cast(propertyHandler->namedPropertyData); + reinterpret_cast(propertyHandler->indexedPropertyData); innerData = v8impl::JsValueFromV8LocalValue(reference->Get()); } + bool exceptionOccurred = false; JSVM_Value result = nullptr; - JSVM_Value name = JsValueFromV8LocalValue(property); + JSVM_Value v8Index = JsValueFromV8LocalValue(v8::Integer::NewFromUnsigned(env->isolate, index)); + JSVM_Value v8Value = JsValueFromV8LocalValue(value); JSVM_Value thisArg = this->This(); env->CallIntoModule( [&](JSVM_Env env) { - if (getterCb) { - result = getterCb(env, name, thisArg, innerData); + if (indexSetterCb) { + result = indexSetterCb(env, v8Index, v8Value, thisArg, innerData); } }, [&](JSVM_Env env, v8::Local v8Value) { @@ -654,11 +654,12 @@ protected: } } - void NameDeleterInvokeCallback() +protected: + inline void NameGetterInvokeCallback() { auto context = cbinfo.GetIsolate()->GetCurrentContext(); auto env = v8impl::GetContextEnv(context); - auto deleterCb = propertyHandler->nameDeleterCallback; + auto getterCb = propertyHandler->namedGetterCallback; JSVM_Value innerData = nullptr; if (propertyHandler->namedPropertyData != nullptr) { @@ -666,15 +667,14 @@ protected: reinterpret_cast(propertyHandler->namedPropertyData); innerData = v8impl::JsValueFromV8LocalValue(reference->Get()); } - bool exceptionOccurred = false; JSVM_Value result = nullptr; JSVM_Value name = JsValueFromV8LocalValue(property); JSVM_Value thisArg = this->This(); env->CallIntoModule( [&](JSVM_Env env) { - if (deleterCb) { - result = deleterCb(env, name, thisArg, innerData); + if (getterCb) { + result = getterCb(env, name, thisArg, innerData); } }, [&](JSVM_Env env, v8::Local v8Value) { @@ -685,17 +685,15 @@ protected: env->isolate->ThrowException(v8Value); }); if (!exceptionOccurred && (result != nullptr)) { - if (v8impl::V8LocalValueFromJsValue(result)->IsBoolean()) { - this->SetReturnValue(result); - } + this->SetReturnValue(result); } } - void NameEnumeratorInvokeCallback() + void NameDeleterInvokeCallback() { auto context = cbinfo.GetIsolate()->GetCurrentContext(); auto env = v8impl::GetContextEnv(context); - auto enumeratorCb = propertyHandler->namedEnumeratorCallback; + auto deleterCb = propertyHandler->nameDeleterCallback; JSVM_Value innerData = nullptr; if (propertyHandler->namedPropertyData != nullptr) { @@ -706,11 +704,12 @@ protected: bool exceptionOccurred = false; JSVM_Value result = nullptr; + JSVM_Value name = JsValueFromV8LocalValue(property); JSVM_Value thisArg = this->This(); env->CallIntoModule( [&](JSVM_Env env) { - if (enumeratorCb) { - result = enumeratorCb(env, thisArg, innerData); + if (deleterCb) { + result = deleterCb(env, name, thisArg, innerData); } }, [&](JSVM_Env env, v8::Local v8Value) { @@ -721,34 +720,32 @@ protected: env->isolate->ThrowException(v8Value); }); if (!exceptionOccurred && (result != nullptr)) { - if (v8impl::V8LocalValueFromJsValue(result)->IsArray()) { + if (v8impl::V8LocalValueFromJsValue(result)->IsBoolean()) { this->SetReturnValue(result); } } } - void IndexSetterInvokeCallback() + void NameEnumeratorInvokeCallback() { auto context = cbinfo.GetIsolate()->GetCurrentContext(); auto env = v8impl::GetContextEnv(context); - auto indexSetterCb = propertyHandler->indexedSetterCallback; + auto enumeratorCb = propertyHandler->namedEnumeratorCallback; JSVM_Value innerData = nullptr; - if (propertyHandler->indexedPropertyData != nullptr) { + if (propertyHandler->namedPropertyData != nullptr) { v8impl::UserReference* reference = - reinterpret_cast(propertyHandler->indexedPropertyData); + reinterpret_cast(propertyHandler->namedPropertyData); innerData = v8impl::JsValueFromV8LocalValue(reference->Get()); } bool exceptionOccurred = false; JSVM_Value result = nullptr; - JSVM_Value v8Index = JsValueFromV8LocalValue(v8::Integer::NewFromUnsigned(env->isolate, index)); - JSVM_Value v8Value = JsValueFromV8LocalValue(value); JSVM_Value thisArg = this->This(); env->CallIntoModule( [&](JSVM_Env env) { - if (indexSetterCb) { - result = indexSetterCb(env, v8Index, v8Value, thisArg, innerData); + if (enumeratorCb) { + result = enumeratorCb(env, thisArg, innerData); } }, [&](JSVM_Env env, v8::Local v8Value) { @@ -759,7 +756,9 @@ protected: env->isolate->ThrowException(v8Value); }); if (!exceptionOccurred && (result != nullptr)) { - this->SetReturnValue(result); + if (v8impl::V8LocalValueFromJsValue(result)->IsArray()) { + this->SetReturnValue(result); + } } } @@ -881,24 +880,29 @@ protected: template class PropertyCallbackWrapper : public PropertyCallbackWrapperBase { public: - static void NameSetterInvoke(v8::Local property, - v8::Local value, - const v8::PropertyCallbackInfo& info) + static v8::Intercepted NameSetterInvoke(v8::Local property, + v8::Local value, + const v8::PropertyCallbackInfo& info) { - PropertyCallbackWrapper propertyCbWrapper(property, value, info); + PropertyCallbackWrapper propertyCbWrapper(property, value, info); propertyCbWrapper.NameSetterInvokeCallback(); + return v8::Intercepted::kYes; } - static void NameGetterInvoke(v8::Local property, const v8::PropertyCallbackInfo& info) + static v8::Intercepted NameGetterInvoke(v8::Local property, + const v8::PropertyCallbackInfo& info) { PropertyCallbackWrapper propertyCbWrapper(property, v8::Local(), info); propertyCbWrapper.NameGetterInvokeCallback(); + return v8::Intercepted::kYes; } - static void NameDeleterInvoke(v8::Local property, const v8::PropertyCallbackInfo& info) + static v8::Intercepted NameDeleterInvoke(v8::Local property, + const v8::PropertyCallbackInfo& info) { PropertyCallbackWrapper propertyCbWrapper(property, v8::Local(), info); propertyCbWrapper.NameDeleterInvokeCallback(); + return v8::Intercepted::kYes; } static void NameEnumeratorInvoke(const v8::PropertyCallbackInfo& info) @@ -907,24 +911,27 @@ public: propertyCbWrapper.NameEnumeratorInvokeCallback(); } - static void IndexSetterInvoke(uint32_t index, - v8::Local value, - const v8::PropertyCallbackInfo& info) + static v8::Intercepted IndexSetterInvoke(uint32_t index, + v8::Local value, + const v8::PropertyCallbackInfo& info) { - PropertyCallbackWrapper propertyCbWrapper(index, value, info); + PropertyCallbackWrapper propertyCbWrapper(index, value, info); propertyCbWrapper.IndexSetterInvokeCallback(); + return v8::Intercepted::kYes; } - static void IndexGetterInvoke(uint32_t index, const v8::PropertyCallbackInfo& info) + static v8::Intercepted IndexGetterInvoke(uint32_t index, const v8::PropertyCallbackInfo& info) { PropertyCallbackWrapper propertyCbWrapper(index, v8::Local(), info); propertyCbWrapper.IndexGetterInvokeCallback(); + return v8::Intercepted::kYes; } - static void IndexDeleterInvoke(uint32_t index, const v8::PropertyCallbackInfo& info) + static v8::Intercepted IndexDeleterInvoke(uint32_t index, const v8::PropertyCallbackInfo& info) { PropertyCallbackWrapper propertyCbWrapper(index, v8::Local(), info); propertyCbWrapper.IndexDeleterInvokeCallback(); + return v8::Intercepted::kYes; } static void IndexEnumeratorInvoke(const v8::PropertyCallbackInfo& info) @@ -946,16 +953,25 @@ public: {} /*virtual*/ - void SetReturnValue(JSVM_Value value) override - { - v8::Local val = v8impl::V8LocalValueFromJsValue(value).As(); - cbinfo.GetReturnValue().Set(val); - } + void SetReturnValue(JSVM_Value value) override; protected: const v8::PropertyCallbackInfo& cbinfo; }; +template<> +void PropertyCallbackWrapper::SetReturnValue(JSVM_Value value) +{ + (void)value; +} + +template +void PropertyCallbackWrapper::SetReturnValue(JSVM_Value value) +{ + v8::Local val = v8impl::V8LocalValueFromJsValue(value).As(); + cbinfo.GetReturnValue().Set(val); +} + JSVM_Status Wrap(JSVM_Env env, JSVM_Value jsObject, void* nativeObject, @@ -1009,6 +1025,8 @@ JSVM_Status OH_JSVM_Init(const JSVM_InitOptions* options) OHOS_API_CALL(platform::ohos::ReportKeyThread(platform::ohos::ThreadRole::IMPORTANT_DISPLAY)); v8::V8::InitializePlatform(v8impl::g_platform.get()); + OHOS_API_CALL(platform::ohos::SetSecurityMode()); + if (options && options->argc && options->argv) { v8::V8::SetFlagsFromCommandLine(options->argc, options->argv, options->removeFlags); } @@ -1248,7 +1266,7 @@ v8::ScriptOrigin CreateScriptOrigin(v8::Isolate* isolate, v8::Local auto options = v8::PrimitiveArray::New(isolate, kOptionsLength); options->Set(isolate, 0, v8::Uint32::New(isolate, kOptionsMagicConstant)); options->Set(isolate, 1, resourceName); - return v8::ScriptOrigin(isolate, resourceName, 0, 0, false, -1, v8::Local(), false, false, + return v8::ScriptOrigin(resourceName, 0, 0, false, -1, v8::Local(), false, false, type == v8::ScriptType::kModule, options); } @@ -1319,8 +1337,8 @@ JSVM_Status OH_JSVM_CompileScriptWithOrigin(JSVM_Env env, ? v8::Local() : v8::String::NewFromUtf8(isolate, origin->sourceMapUrl).ToLocalChecked().As(); auto resourceName = v8::String::NewFromUtf8(isolate, origin->resourceName).ToLocalChecked(); - v8::ScriptOrigin scriptOrigin(isolate, resourceName, origin->resourceLineOffset, origin->resourceColumnOffset, - false, -1, sourceMapUrl); + v8::ScriptOrigin scriptOrigin(resourceName, origin->resourceLineOffset, origin->resourceColumnOffset, false, -1, + sourceMapUrl); v8::ScriptCompiler::CachedData* cache = cachedData ? new v8::ScriptCompiler::CachedData(cachedData, cachedDataLength) : nullptr; @@ -1380,7 +1398,7 @@ public: ? v8::String::NewFromUtf8(isolate, jsvmOrigin->sourceMapUrl).ToLocalChecked().As() : v8::Local(); auto resourceName = v8::String::NewFromUtf8(isolate, sourceString.c_str()).ToLocalChecked(); - v8Origin = new v8::ScriptOrigin(isolate, resourceName, jsvmOrigin ? jsvmOrigin->resourceLineOffset : 0, + v8Origin = new v8::ScriptOrigin(resourceName, jsvmOrigin ? jsvmOrigin->resourceLineOffset : 0, jsvmOrigin ? jsvmOrigin->resourceColumnOffset : 0, false, -1, sourceMapUrl); if (enableSourceMap && sourceMapPtr) { v8impl::SetFileToSourceMapMapping(jsvmOrigin->resourceName, sourceMapPtr); @@ -1867,8 +1885,7 @@ JSVM_Status OH_JSVM_DefineClass(JSVM_Env env, STATUS_CALL(v8impl::FunctionCallbackWrapper::NewTemplate(env, p->setter, &setterTpl)); } - tpl->PrototypeTemplate()->SetAccessorProperty(propertyName, getterTpl, setterTpl, attributes, - v8::AccessControl::DEFAULT); + tpl->PrototypeTemplate()->SetAccessorProperty(propertyName, getterTpl, setterTpl, attributes); } else if (p->method != nullptr) { v8::Local t; if (p->attributes & JSVM_NO_RECEIVER_CHECK) { @@ -4153,8 +4170,7 @@ JSVM_Status OH_JSVM_DefineClassWithPropertyHandler(JSVM_Env env, STATUS_CALL(v8impl::FunctionCallbackWrapper::NewTemplate(env, p->setter, &setterTpl)); } - tpl->PrototypeTemplate()->SetAccessorProperty(propertyName, getterTpl, setterTpl, attributes, - v8::AccessControl::DEFAULT); + tpl->PrototypeTemplate()->SetAccessorProperty(propertyName, getterTpl, setterTpl, attributes); } else if (p->method != nullptr) { v8::Local t; if (p->attributes & JSVM_NO_RECEIVER_CHECK) { @@ -4179,12 +4195,12 @@ JSVM_Status OH_JSVM_DefineClassWithPropertyHandler(JSVM_Env env, v8::Local cbdata = v8impl::CallbackBundle::New(env, propertyHandleCfg); // register named property handler - v8::NamedPropertyHandlerConfiguration namedPropertyHandler; + v8::NamedPropertyHandlerConfiguration namedPropertyHandler(nullptr); if (propertyHandlerCfg->genericNamedPropertyGetterCallback) { namedPropertyHandler.getter = v8impl::PropertyCallbackWrapper::NameGetterInvoke; } if (propertyHandlerCfg->genericNamedPropertySetterCallback) { - namedPropertyHandler.setter = v8impl::PropertyCallbackWrapper::NameSetterInvoke; + namedPropertyHandler.setter = v8impl::PropertyCallbackWrapper::NameSetterInvoke; } if (propertyHandlerCfg->genericNamedPropertyDeleterCallback) { namedPropertyHandler.deleter = v8impl::PropertyCallbackWrapper::NameDeleterInvoke; @@ -4201,7 +4217,7 @@ JSVM_Status OH_JSVM_DefineClassWithPropertyHandler(JSVM_Env env, indexPropertyHandler.getter = v8impl::PropertyCallbackWrapper::IndexGetterInvoke; } if (propertyHandlerCfg->genericIndexedPropertySetterCallback) { - indexPropertyHandler.setter = v8impl::PropertyCallbackWrapper::IndexSetterInvoke; + indexPropertyHandler.setter = v8impl::PropertyCallbackWrapper::IndexSetterInvoke; } if (propertyHandlerCfg->genericIndexedPropertyDeleterCallback) { indexPropertyHandler.deleter = v8impl::PropertyCallbackWrapper::IndexDeleterInvoke; @@ -5108,7 +5124,7 @@ JSVM_GCType GetJSVMGCType(v8::GCType gcType) switch (gcType) { case v8::GCType::kGCTypeScavenge: return JSVM_GC_TYPE_SCAVENGE; - case v8::GCType::kGCTypeMinorMarkCompact: + case v8::GCType::kGCTypeMinorMarkSweep: return JSVM_GC_TYPE_MINOR_MARK_COMPACT; case v8::GCType::kGCTypeMarkSweepCompact: return JSVM_GC_TYPE_MARK_SWEEP_COMPACT; @@ -5127,7 +5143,7 @@ static v8::GCType GetV8GCType(JSVM_GCType gcType) case JSVM_GC_TYPE_SCAVENGE: return v8::GCType::kGCTypeScavenge; case JSVM_GC_TYPE_MINOR_MARK_COMPACT: - return v8::GCType::kGCTypeMinorMarkCompact; + return v8::GCType::kGCTypeMinorMarkSweep; case JSVM_GC_TYPE_MARK_SWEEP_COMPACT: return v8::GCType::kGCTypeMarkSweepCompact; case JSVM_GC_TYPE_INCREMENTAL_MARKING: @@ -5439,7 +5455,7 @@ JSVM_Status ProcessPropertyHandler(JSVM_Env env, v8::Local cbdata = v8impl::CallbackBundle::New(env, *propertyHandlerCfgStruct); // register named property handler - v8::NamedPropertyHandlerConfiguration namedPropertyHandler; + v8::NamedPropertyHandlerConfiguration namedPropertyHandler(nullptr); if (propertyHandlerCfg->genericNamedPropertyGetterCallback) { namedPropertyHandler.getter = v8impl::PropertyCallbackWrapper::NameGetterInvoke; } @@ -5456,7 +5472,7 @@ JSVM_Status ProcessPropertyHandler(JSVM_Env env, tpl->InstanceTemplate()->SetHandler(namedPropertyHandler); // register indexed property handle - v8::IndexedPropertyHandlerConfiguration indexPropertyHandler; + v8::IndexedPropertyHandlerConfiguration indexPropertyHandler(nullptr); if (propertyHandlerCfg->genericIndexedPropertyGetterCallback) { indexPropertyHandler.getter = v8impl::PropertyCallbackWrapper::IndexGetterInvoke; } @@ -5594,8 +5610,8 @@ JSVM_Status OH_JSVM_DefineClassWithOptions(JSVM_Env env, STATUS_CALL(v8impl::FunctionCallbackWrapper::NewTemplate(env, p->setter, &setterTpl)); } - tpl->PrototypeTemplate()->SetAccessorProperty(propertyName, getterTpl, setterTpl, attributes, - v8::AccessControl::DEFAULT); + tpl->PrototypeTemplate()->SetAccessorProperty(propertyName, getterTpl, setterTpl, attributes); + } else if (p->method != nullptr) { v8::Local temp; STATUS_CALL( -- Gitee