From 23e6c8dd59841ee093fa507ed7046119124bc5d0 Mon Sep 17 00:00:00 2001 From: wangyimin Date: Wed, 21 May 2025 14:59:56 +0800 Subject: [PATCH] support multi context Signed-off-by: yanghaiming --- src/inspector/js_native_api_v8_inspector.cpp | 6 ++--- src/js_native_api_v8.cpp | 23 +++++++++----------- src/platform/platform.h | 6 +++-- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/inspector/js_native_api_v8_inspector.cpp b/src/inspector/js_native_api_v8_inspector.cpp index 30eb6b7..6c9e4c2 100644 --- a/src/inspector/js_native_api_v8_inspector.cpp +++ b/src/inspector/js_native_api_v8_inspector.cpp @@ -1205,21 +1205,21 @@ int FindAvailablePort() if (sockfd < 0) { continue; } - fdsan_exchange_owner_tag(sockfd, 0, LOG_DOMAIN); + OHOS_CALL(fdsan_exchange_owner_tag(sockfd, 0, LOG_DOMAIN)); struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(port); if (bind(sockfd, reinterpret_cast(&addr), sizeof(addr)) < 0) { - fdsan_close_with_tag(sockfd, LOG_DOMAIN); + OHOS_SELECT(fdsan_close_with_tag(sockfd, LOG_DOMAIN), close(sockfd)); if (errno == EADDRINUSE) { continue; } else { break; } } - fdsan_close_with_tag(sockfd, LOG_DOMAIN); + OHOS_SELECT(fdsan_close_with_tag(sockfd, LOG_DOMAIN), close(sockfd)); return port; } return invalidPort; diff --git a/src/js_native_api_v8.cpp b/src/js_native_api_v8.cpp index c6680b5..dbe7cab 100644 --- a/src/js_native_api_v8.cpp +++ b/src/js_native_api_v8.cpp @@ -208,8 +208,6 @@ static std::vector externalReferenceRegistry; static std::unordered_map sourceMapUrlMap; -static std::unique_ptr defaultArrayBufferAllocator; - static std::unique_ptr g_trace_stream; constexpr uint32_t TRACE_CATEGORY_COUNT = 7; @@ -227,11 +225,10 @@ constexpr uint32_t DEFAULT_CATEGORY_COUNT = 4; static constexpr JSVM_TraceCategory DEFAULT_CATAGORIES[] = { JSVM_TRACE_VM, JSVM_TRACE_EXECUTE, JSVM_TRACE_COMPILE, JSVM_TRACE_RUNTIME }; -static v8::ArrayBuffer::Allocator* GetOrCreateDefaultArrayBufferAllocator() +static inline v8::ArrayBuffer::Allocator* GetOrCreateDefaultArrayBufferAllocator() { - if (!defaultArrayBufferAllocator) { - defaultArrayBufferAllocator.reset(v8::ArrayBuffer::Allocator::NewDefaultAllocator()); - } + static std::unique defaultArrayBufferAllocator( + v8::ArrayBuffer::Allocator::NewDefaultAllocator()); return defaultArrayBufferAllocator.get(); } @@ -1023,14 +1020,14 @@ JSVM_Status OH_JSVM_Init(const JSVM_InitOptions* options) } initialized.store(true); - OHOS_API_CALL(platform::ohos::WriteHisysevent()); - OHOS_API_CALL(platform::ohos::ReportKeyThread(platform::ohos::ThreadRole::IMPORTANT_DISPLAY)); + OHOS_CALL(platform::ohos::WriteHisysevent()); + OHOS_CALL(platform::ohos::ReportKeyThread(platform::ohos::ThreadRole::IMPORTANT_DISPLAY)); v8::V8::InitializePlatform(v8impl::g_platform.get()); if (options && options->argc && options->argv) { v8::V8::SetFlagsFromCommandLine(options->argc, options->argv, options->removeFlags); } - OHOS_API_CALL(platform::ohos::SetSecurityMode()); + OHOS_CALL(platform::ohos::SetSecurityMode()); v8::V8::Initialize(); const auto cb = v8impl::FunctionCallbackWrapper::Invoke; @@ -1052,7 +1049,7 @@ JSVM_Status OH_JSVM_GetVM(JSVM_Env env, JSVM_VM* result) JSVM_Status OH_JSVM_CreateVM(const JSVM_CreateVMOptions* options, JSVM_VM* result) { - OHOS_API_CALL(platform::ohos::ReportKeyThread(platform::ohos::ThreadRole::USER_INTERACT)); + OHOS_CALL(platform::ohos::ReportKeyThread(platform::ohos::ThreadRole::USER_INTERACT)); v8::Isolate::CreateParams createParams; auto externalReferences = v8impl::externalReferenceRegistry.data(); @@ -4819,7 +4816,7 @@ JSVM_Status OH_JSVM_CompileWasmModule(JSVM_Env env, { JSVM_PREAMBLE(env); // add jit mode check - RETURN_STATUS_IF_FALSE(env, platform::ohos::InJitMode(), JSVM_JIT_MODE_EXPECTED); + RETURN_STATUS_IF_FALSE(env, OHOS_SELECT(platform::ohos::InJitMode(), true), JSVM_JIT_MODE_EXPECTED); CHECK_ARG(env, wasmBytecode); RETURN_STATUS_IF_FALSE(env, wasmBytecodeLength > 0, JSVM_INVALID_ARG); v8::MaybeLocal maybeModule; @@ -4849,7 +4846,7 @@ JSVM_Status OH_JSVM_CompileWasmFunction(JSVM_Env env, { JSVM_PREAMBLE(env); // add jit mode check - RETURN_STATUS_IF_FALSE(env, platform::ohos::InJitMode(), JSVM_JIT_MODE_EXPECTED); + RETURN_STATUS_IF_FALSE(env, OHOS_SELECT(platform::ohos::InJitMode(), true), JSVM_JIT_MODE_EXPECTED); CHECK_ARG(env, wasmModule); CHECK_SCOPE(env, wasmModule); v8::Local val = v8impl::V8LocalValueFromJsValue(wasmModule); @@ -4892,7 +4889,7 @@ JSVM_Status OH_JSVM_CreateWasmCache(JSVM_Env env, JSVM_Value wasmModule, const u { JSVM_PREAMBLE(env); // add jit mode check - RETURN_STATUS_IF_FALSE(env, platform::ohos::InJitMode(), JSVM_JIT_MODE_EXPECTED); + RETURN_STATUS_IF_FALSE(env, OHOS_SELECT(platform::ohos::InJitMode(), true), JSVM_JIT_MODE_EXPECTED); CHECK_ARG(env, wasmModule); CHECK_ARG(env, data); CHECK_ARG(env, length); diff --git a/src/platform/platform.h b/src/platform/platform.h index d417692..f006dd9 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -24,9 +24,11 @@ #ifdef TARGET_OHOS #include "platform/platform_ohos.h" -#define OHOS_API_CALL(api_call) api_call +#define OHOS_CALL(api_call) api_call +#define OHOS_SELECT(expr_ohos, expr_non_ohos) expr_ohos #else -#define OHOS_API_CALL(api_call) +#define OHOS_CALL(api_call) +#define OHOS_SELECT(expr_ohos, expr_non_ohos) expr_non_ohos #endif namespace platform { -- Gitee