Skip to content

Commit

Permalink
feat(hermes): use std::any_cast in ios 11 when opening c++ exception
Browse files Browse the repository at this point in the history
  • Loading branch information
ilikethese committed Jan 2, 2024
1 parent c57bc36 commit d70872e
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 34 deletions.
17 changes: 10 additions & 7 deletions driver/js/include/driver/modules/module_register.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
#include "driver/napi/callback_info.h"
#include "driver/scope.h"

#define GEN_INVOKE_CB_INTERNAL(Module, Function, Name) \
static void Name(hippy::napi::CallbackInfo& info, void* data) { \
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(std::any_cast<void*>(info.GetSlot())); \
auto scope = scope_wrapper->scope.lock(); \
FOOTSTONE_CHECK(scope); \
auto target = std::static_pointer_cast<Module>(scope->GetModuleObject(#Module)); \
target->Function(info, data); \

#define GEN_INVOKE_CB_INTERNAL(Module, Function, Name) \
static void Name(hippy::napi::CallbackInfo& info, void* data) { \
std::any slot_any = info.GetSlot(); \
auto any_pointer = std::any_cast<void*>(&slot_any); \
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer)); \
auto scope = scope_wrapper->scope.lock(); \
FOOTSTONE_CHECK(scope); \
auto target = std::static_pointer_cast<Module>(scope->GetModuleObject(#Module)); \
target->Function(info, data); \
}

#ifndef REGISTER_EXTERNAL_REFERENCES
Expand Down
4 changes: 3 additions & 1 deletion driver/js/include/driver/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,9 @@ class Scope : public std::enable_shared_from_this<Scope> {
template<typename T>
std::shared_ptr<CtxValue> DefineClass(const std::shared_ptr<ClassTemplate<T>>& class_template) {
class_template->constructor_wrapper = std::make_unique<FunctionWrapper>([](CallbackInfo& info, void* data) {
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(std::any_cast<void*>(info.GetSlot()));
std::any slot_any = info.GetSlot();
auto any_pointer = std::any_cast<void*>(&slot_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto context = scope->GetContext();
Expand Down
8 changes: 6 additions & 2 deletions driver/js/src/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ Engine::Engine()
Engine::~Engine() {
FOOTSTONE_DLOG(INFO) << "~Engine";
for(auto& [key, template_map] : class_template_holder_map_) {
auto animation_template = std::any_cast<std::shared_ptr<ClassTemplate<CubicBezierAnimation>>>(template_map["Animation"]);
std::any animation_any = template_map["Animation"];
auto animation_any_pointer = std::any_cast<std::shared_ptr<ClassTemplate<CubicBezierAnimation>>>(&animation_any);
auto animation_template = static_cast<std::shared_ptr<ClassTemplate<CubicBezierAnimation>>>(*animation_any_pointer);
animation_template->holder_ctx_values.clear();
auto animation_set_template = std::any_cast<std::shared_ptr<ClassTemplate<AnimationSet>>>(template_map["AnimationSet"]);
std::any animation_set_any = template_map["AnimationSet"];
auto animation_set_any_pointer = std::any_cast<std::shared_ptr<ClassTemplate<AnimationSet>>>(&animation_set_any);
auto animation_set_template = static_cast<std::shared_ptr<ClassTemplate<AnimationSet>>>(*animation_set_any_pointer);
animation_set_template->holder_ctx_values.clear();
}
}
Expand Down
4 changes: 3 additions & 1 deletion driver/js/src/js_driver_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,9 @@ void JsDriverUtils::CallNative(hippy::napi::CallbackInfo& info, const std::funct
bool,
byte_string)>& callback) {
FOOTSTONE_DLOG(INFO) << "CallHost";
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(std::any_cast<void*>(info.GetSlot()));
std::any slot_any = info.GetSlot();
auto any_pointer = std::any_cast<void*>(&slot_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto context = scope->GetContext();
Expand Down
8 changes: 6 additions & 2 deletions driver/js/src/modules/animation_frame_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ GEN_INVOKE_CB(AnimationFrameModule, RequestAnimationFrame) // NOLINT(cert-err58-
GEN_INVOKE_CB(AnimationFrameModule, CancelAnimationFrame) // NOLINT(cert-err58-cpp)

void AnimationFrameModule::RequestAnimationFrame(hippy::napi::CallbackInfo &info, void* data) { // NOLINT(readability-convert-member-functions-to-static)
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(std::any_cast<void*>(info.GetSlot()));
std::any slot_any = info.GetSlot();
auto any_pointer = std::any_cast<void*>(&slot_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto context = scope->GetContext();
Expand Down Expand Up @@ -96,7 +98,9 @@ void AnimationFrameModule::RequestAnimationFrame(hippy::napi::CallbackInfo &info
}

void AnimationFrameModule::CancelAnimationFrame(hippy::napi::CallbackInfo &info, void* data) { // NOLINT(readability-convert-member-functions-to-static)
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(std::any_cast<void*>(info.GetSlot()));
std::any slot_any = info.GetSlot();
auto any_pointer = std::any_cast<void*>(&slot_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto context = scope->GetContext();
Expand Down
8 changes: 6 additions & 2 deletions driver/js/src/modules/animation_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,9 @@ RegisterAnimation(const std::weak_ptr<Scope>& weak_scope) {
};
animation->AddEventListener(StringViewUtils::ToStdString(StringViewUtils::ConvertEncoding(
event_name, string_view::Encoding::Utf8).utf8_value()), std::move(cb));
auto class_template_ptr = std::any_cast<std::shared_ptr<ClassTemplate<CubicBezierAnimation>>>(scope->GetClassTemplate("Animation"));
std::any animation_any = scope->GetClassTemplate("Animation");
auto any_pointer = std::any_cast<std::shared_ptr<ClassTemplate<CubicBezierAnimation>>>(&animation_any);
auto class_template_ptr = static_cast<std::shared_ptr<ClassTemplate<CubicBezierAnimation>>>(*any_pointer);
class_template_ptr->holder_ctx_values.emplace_back(func);
return nullptr;
};
Expand Down Expand Up @@ -835,7 +837,9 @@ RegisterAnimationSet(const std::weak_ptr<Scope>& weak_scope) {
};
animation_set->AddEventListener(StringViewUtils::ToStdString(StringViewUtils::ConvertEncoding(
event_name, string_view::Encoding::Utf8).utf8_value()), std::move(cb));
auto class_template_ptr = std::any_cast<std::shared_ptr<ClassTemplate<AnimationSet>>>(scope->GetClassTemplate("AnimationSet"));
std::any animation_set_any = scope->GetClassTemplate("AnimationSet");
auto any_pointer = std::any_cast<std::shared_ptr<ClassTemplate<AnimationSet>>>(&animation_set_any);
auto class_template_ptr = static_cast<std::shared_ptr<ClassTemplate<AnimationSet>>>(*any_pointer);
class_template_ptr->holder_ctx_values.emplace_back(func);
return nullptr;
};
Expand Down
4 changes: 3 additions & 1 deletion driver/js/src/modules/console_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ inline namespace module {
GEN_INVOKE_CB(ConsoleModule, Log) // NOLINT(cert-err58-cpp)

void ConsoleModule::Log(hippy::napi::CallbackInfo &info, void* data) { // NOLINT(readability-convert-member-functions-to-static)
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(std::any_cast<void*>(info.GetSlot()));
std::any slot_any = info.GetSlot();
auto any_pointer = std::any_cast<void*>(&slot_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto context = scope->GetContext();
Expand Down
8 changes: 6 additions & 2 deletions driver/js/src/modules/contextify_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ GEN_INVOKE_CB(ContextifyModule, RunInThisContext) // NOLINT(cert-err58-cpp)
GEN_INVOKE_CB(ContextifyModule, LoadUntrustedContent) // NOLINT(cert-err58-cpp)

void ContextifyModule::RunInThisContext(hippy::napi::CallbackInfo& info, void* data) { // NOLINT(readability-convert-member-functions-to-static)
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(std::any_cast<void*>(info.GetSlot()));
std::any slot_any = info.GetSlot();
auto any_pointer = std::any_cast<void*>(&slot_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
#ifdef JS_V8
Expand Down Expand Up @@ -118,7 +120,9 @@ void ContextifyModule::RunInThisContext(hippy::napi::CallbackInfo& info, void* d
void ContextifyModule::RemoveCBFunc(const string_view& uri) { cb_func_map_.erase(uri); }

void ContextifyModule::LoadUntrustedContent(CallbackInfo& info, void* data) {
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(std::any_cast<void*>(info.GetSlot()));
std::any slot_any = info.GetSlot();
auto any_pointer = std::any_cast<void*>(&slot_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto context = scope->GetContext();
Expand Down
4 changes: 3 additions & 1 deletion driver/js/src/modules/event_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ std::shared_ptr<ClassTemplate<DomEvent>> MakeEventClassTemplate(
if (!scope) {
return nullptr;
}
auto current_event =std::any_cast<std::shared_ptr<DomEvent>>(scope->GetCurrentEvent());
std::any event_any = scope->GetCurrentEvent();
auto any_pointer = std::any_cast<std::shared_ptr<DomEvent>>(&event_any);
auto current_event = static_cast<std::shared_ptr<DomEvent>>(*any_pointer);
return current_event;
};

Expand Down
4 changes: 3 additions & 1 deletion driver/js/src/modules/performance/performance_mark_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ std::shared_ptr<ClassTemplate<PerformanceMark>> RegisterPerformanceMark(const st
if (!detail.has_value()) {
return context->CreateNull();
}
return std::any_cast<std::shared_ptr<CtxValue>>(detail);
auto any_pointer = std::any_cast<std::shared_ptr<CtxValue>>(&detail);
auto detail_ctx = static_cast<std::shared_ptr<CtxValue>>(*any_pointer);
return detail_ctx;
};
class_template.properties.push_back(std::move(name_property_define));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ std::shared_ptr<ClassTemplate<PerformanceMeasure>> RegisterPerformanceMeasure(co
if (!detail.has_value()) {
return context->CreateNull();
}
return std::any_cast<std::shared_ptr<CtxValue>>(detail);
auto any_pointer = std::any_cast<std::shared_ptr<CtxValue>>(&detail);
auto detail_ctx = static_cast<std::shared_ptr<CtxValue>>(*any_pointer);
return detail_ctx;
};
class_template.properties.push_back(std::move(name_property_define));

Expand Down
16 changes: 10 additions & 6 deletions driver/js/src/modules/timer_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ void TimerModule::SetInterval(CallbackInfo& info, void* data) {
}

void TimerModule::ClearInterval(CallbackInfo& info, void* data) {
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(std::any_cast<void*>(info.GetSlot()));
std::any slot_any = info.GetSlot();
auto any_pointer = std::any_cast<void*>(&slot_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto context = scope->GetContext();
Expand All @@ -100,7 +102,9 @@ void TimerModule::ClearInterval(CallbackInfo& info, void* data) {
}

void TimerModule::RequestIdleCallback(CallbackInfo& info, void* data) {
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(std::any_cast<void*>(info.GetSlot()));
std::any slot_any = info.GetSlot();
auto any_pointer = std::any_cast<void*>(&slot_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto context = scope->GetContext();
Expand Down Expand Up @@ -173,10 +177,10 @@ void TimerModule::CancelIdleCallback(CallbackInfo& info, void* data) {

}

std::shared_ptr<hippy::napi::CtxValue> TimerModule::Start(
CallbackInfo& info,
bool repeat) {
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(std::any_cast<void*>(info.GetSlot()));
std::shared_ptr<hippy::napi::CtxValue> TimerModule::Start(CallbackInfo& info, bool repeat) {
std::any slot_any = info.GetSlot();
auto any_pointer = std::any_cast<void*>(&slot_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto context = scope->GetContext();
Expand Down
4 changes: 3 additions & 1 deletion driver/js/src/modules/ui_manager_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ inline namespace module {
GEN_INVOKE_CB(UIManagerModule, CallUIFunction)

void UIManagerModule::CallUIFunction(CallbackInfo& info, void* data) {
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(std::any_cast<void*>(info.GetSlot()));
std::any slot_any = info.GetSlot();
auto any_pointer = std::any_cast<void*>(&slot_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto context = scope->GetContext();
Expand Down
9 changes: 6 additions & 3 deletions driver/js/src/napi/hermes/hermes_ctx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ static Value InvokePropertyCallback(Runtime& runtime, const Value& this_value, c
if (!global_native_state->Get(kScopeWrapperIndex, scope_any)) {
return facebook::jsi::Value::undefined();
}
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(std::any_cast<void*>(scope_any));
auto any_pointer = std::any_cast<void*>(&scope_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
if (scope == nullptr) return facebook::jsi::Value::undefined();
auto hermes_ctx = std::static_pointer_cast<HermesCtx>(scope->GetContext());
Expand Down Expand Up @@ -127,7 +128,8 @@ static Value InvokeConstructorJsCallback(Runtime& runtime, const Value& this_val
if (!global_native_state->Get(kScopeWrapperIndex, scope_any)) {
return Value::undefined();
}
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(std::any_cast<void*>(scope_any));
auto any_pointer = std::any_cast<void*>(&scope_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
if (scope == nullptr) return facebook::jsi::Value::undefined();
auto hermes_ctx = std::static_pointer_cast<HermesCtx>(scope->GetContext());
Expand Down Expand Up @@ -204,7 +206,8 @@ static Value InvokeJsCallback(Runtime& runtime, const Value& this_value, const V
if (!global_native_state->Get(kScopeWrapperIndex, scope_any)) {
return Value::undefined();
}
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(std::any_cast<void*>(scope_any));
auto any_pointer = std::any_cast<void*>(&scope_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
if (scope == nullptr) return facebook::jsi::Value::undefined();
auto hermes_ctx = std::static_pointer_cast<HermesCtx>(scope->GetContext());
Expand Down
4 changes: 3 additions & 1 deletion driver/js/src/scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ inline namespace driver {


static void InternalBindingCallback(hippy::napi::CallbackInfo& info, void* data) {
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(std::any_cast<void*>(info.GetSlot()));
std::any slot_any = info.GetSlot();
auto any_pointer = std::any_cast<void*>(&slot_any);
auto scope_wrapper = reinterpret_cast<ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto context = scope->GetContext();
Expand Down
8 changes: 6 additions & 2 deletions framework/ios/base/executors/HippyJSExecutor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@ - (SharedCtxValuePtr)JSTurboObjectWithName:(NSString *)name {
for (size_t i = 0; i < info.Length(); ++i) {
argv.push_back(info[i]);
}
auto scope_wrapper = reinterpret_cast<hippy::ScopeWrapper*>(std::any_cast<void*>(info.GetSlot()));
std::any slot_any = info.GetSlot();
auto any_pointer = std::any_cast<void*>(&slot_any);
auto scope_wrapper = reinterpret_cast<hippy::ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto turbo_wrapper = reinterpret_cast<TurboWrapper*>(data);
Expand All @@ -320,7 +322,9 @@ - (SharedCtxValuePtr)JSTurboObjectWithName:(NSString *)name {
info.GetReturnValue()->Set(result);
}, turbo_wrapper.get());
[turbo saveTurboWrapper:name turbo:std::move(turbo_wrapper)];
auto scope_wrapper = reinterpret_cast<hippy::ScopeWrapper*>(std::any_cast<void*>(info.GetSlot()));
std::any slot_any = info.GetSlot();
auto any_pointer = std::any_cast<void*>(&slot_any);
auto scope_wrapper = reinterpret_cast<hippy::ScopeWrapper*>(static_cast<void *>(*any_pointer));
auto scope = scope_wrapper->scope.lock();
FOOTSTONE_CHECK(scope);
auto func = scope->GetContext()->CreateFunction(func_wrapper);
Expand Down

0 comments on commit d70872e

Please sign in to comment.