From 6dc0909b95a25d83a269250b54af881dbe5f13f9 Mon Sep 17 00:00:00 2001 From: wwwcg Date: Wed, 5 Mar 2025 17:45:24 +0800 Subject: [PATCH] feat(hermes): context code optimization --- driver/js/src/napi/hermes/hermes_ctx.cc | 84 +++++++++++++------------ 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/driver/js/src/napi/hermes/hermes_ctx.cc b/driver/js/src/napi/hermes/hermes_ctx.cc index 23efc762ffc..0088089c543 100644 --- a/driver/js/src/napi/hermes/hermes_ctx.cc +++ b/driver/js/src/napi/hermes/hermes_ctx.cc @@ -63,7 +63,7 @@ constexpr char kProxyTargetObject[] = "proxy_target_object"; constexpr char kIsProxyObject[] = "is_proxy_object"; constexpr char kJsonStringify[] = "function(obj) { return JSON.stringify(obj); }"; -// 确保返回数据以空字符结尾 +// Ensure that the returned data ends with a null character static uint8_t* EnsureNullTerminated(const uint8_t* data, size_t len) { if (!data || len == 0) { return nullptr; @@ -268,33 +268,33 @@ static Value InvokeJsCallback(Runtime& runtime, const Value& this_value, const V auto hermes_ctx = std::static_pointer_cast(scope->GetContext()); if (hermes_ctx == nullptr) return facebook::jsi::Value::undefined(); - CallbackInfo cb_info; - cb_info.SetSlot(scope_any); - - if (this_value.isObject()) { - auto instance_object = this_value.asObject(runtime); - - if (instance_object.hasNativeState(runtime)) { - auto local_native_state = instance_object.getNativeState(runtime); - auto data = local_native_state->Get(); - cb_info.SetData(data); - } - - if (instance_object.hasProperty(runtime, kProtoKey)) { - auto proto_object = instance_object.getProperty(runtime, kProtoKey).asObject(runtime); - if (proto_object.hasNativeState(runtime)) { - auto local_native_state = proto_object.getNativeState(runtime); - void *data; - if (instance_object.hasProperty(runtime, kUniqueIdInLocalStateKey)) { - int uniqueID = (int)instance_object.getProperty(runtime, kUniqueIdInLocalStateKey).asNumber(); - data = local_native_state->Get(uniqueID); - } else { - data = local_native_state->Get(); - } - cb_info.SetData(data); - } + CallbackInfo cb_info; + cb_info.SetSlot(scope_any); + + if (this_value.isObject()) { + auto instance_object = this_value.asObject(runtime); + + if (instance_object.hasNativeState(runtime)) { + auto local_native_state = instance_object.getNativeState(runtime); + auto data = local_native_state->Get(); + cb_info.SetData(data); + } + + if (instance_object.hasProperty(runtime, kProtoKey)) { + auto proto_object = instance_object.getProperty(runtime, kProtoKey).asObject(runtime); + if (proto_object.hasNativeState(runtime)) { + auto local_native_state = proto_object.getNativeState(runtime); + void *data; + if (instance_object.hasProperty(runtime, kUniqueIdInLocalStateKey)) { + int uniqueID = (int)instance_object.getProperty(runtime, kUniqueIdInLocalStateKey).asNumber(); + data = local_native_state->Get(uniqueID); + } else { + data = local_native_state->Get(); } + cb_info.SetData(data); + } } + } if (this_value.isObject()) { cb_info.SetReceiver(std::make_shared(runtime, this_value.asObject(runtime))); @@ -333,7 +333,7 @@ static Value InvokeJsCallback(Runtime& runtime, const Value& this_value, const V } void fatalHandler(const std::string &message) { - FOOTSTONE_LOG(FATAL) << "Received Hermes Fatal Error: %s\n" << message.c_str(); + FOOTSTONE_LOG(FATAL) << "Received Hermes Fatal Error: %s\n" << message.c_str(); } constexpr char kHippyHermes[] = "HippyHermesBridge"; @@ -379,11 +379,11 @@ HermesCtx::~HermesCtx() { #ifdef ENABLE_INSPECTOR void HermesCtx::SetupDebugAgent(facebook::hermes::debugger::EnqueueRuntimeTaskFunc enqueueRuntimeTask, facebook::hermes::cdp::OutboundMessageFunc messageCallback) { - cdpDebugAPI_ = CDPDebugAPI::create(*runtime_); - cdpAgent_ = CDPAgent::create(0, - *cdpDebugAPI_, - enqueueRuntimeTask, - messageCallback); + cdpDebugAPI_ = CDPDebugAPI::create(*runtime_); + cdpAgent_ = CDPAgent::create(0, + *cdpDebugAPI_, + enqueueRuntimeTask, + messageCallback); } #endif /* ENABLE_INSPECTOR */ @@ -395,19 +395,21 @@ std::shared_ptr HermesCtx::DefineProxy(const std::unique_ptr HermesCtx::DefineProxyHandler(const std::unique_ptr& proxy_handler) { auto proxy_getter = facebook::jsi::Function::createFromHostFunction( *runtime_, PropNameID::forAscii(*runtime_, ""), 3, - [proxy_handler_pointer = proxy_handler.get()](Runtime& runtime, const Value& this_value, const Value* args, + [proxy_handler_pointer = proxy_handler.get()](Runtime& runtime, + const Value& this_value, + const Value* args, size_t count) -> Value { // proxy get method argument (target, property, receiver) if (count != 3) return Value::undefined(); if (!args[1].isString()) return Value::undefined(); std::string method_name = args[1].toString(runtime).utf8(runtime); - // 获取 Proxy 对象的原始 target 对象 + // Get the original target of the Proxy object if (method_name == kProxyTargetObject) return Value(runtime, args[0]); - // 判断是否是 Proxy 对象 + // Check whether it is a Proxy object if (method_name == kIsProxyObject) return Value(true); - // 返回 jsi 需要的函数, 现在只支持了函数的返回 + // Return functions required by jsi, now only function returns are supported return facebook::jsi::Function::createFromHostFunction( runtime, PropNameID::forAscii(runtime, ""), 1, [proxy_handler_pointer = proxy_handler_pointer, method_name](Runtime& runtime, const Value& this_value, @@ -1134,11 +1136,13 @@ std::shared_ptr HermesCtx::RunScript(const string_view& data, const st // MARK: - Exception Handle -// TODO: add throw exception -void HermesCtx::ThrowException(const std::shared_ptr& exception) {} +void HermesCtx::ThrowException(const std::shared_ptr& exception) { + exception_ = (std::static_pointer_cast(exception)); +} -// TODO: add throw exception -void HermesCtx::ThrowException(const string_view& exception) {} +void HermesCtx::ThrowException(const string_view& exception) { + ThrowException(CreateException(exception)); +} std::shared_ptr HermesCtx::CreateTryCatchScope(bool enable, std::shared_ptr ctx) { return std::make_shared(enable, ctx);