Skip to content

Commit

Permalink
fix(core): fix c++ stack overflow (#4173)
Browse files Browse the repository at this point in the history
  • Loading branch information
etkmao authored Jan 14, 2025
1 parent a2aab0f commit 028873c
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 22 deletions.
8 changes: 4 additions & 4 deletions driver/js/include/driver/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,14 @@ class Scope : public std::enable_shared_from_this<Scope> {

auto class_template = reinterpret_cast<ClassTemplate<T>*>(data);
auto len = info.Length();
std::shared_ptr<CtxValue> argv[len];
std::vector<std::shared_ptr<CtxValue>> argv(len);
for (size_t i = 0; i < len; i++) {
argv[i] = info[i];
}
auto receiver = info.GetReceiver();
auto external = info.GetData();
std::shared_ptr<CtxValue> exception = nullptr;
auto ret = class_template->constructor(receiver, static_cast<size_t>(len), argv, external, exception);
auto ret = class_template->constructor(receiver, static_cast<size_t>(len), argv.data(), external, exception);
if (exception) {
info.GetExceptionValue()->Set(exception);
return;
Expand Down Expand Up @@ -430,7 +430,7 @@ class Scope : public std::enable_shared_from_this<Scope> {
auto function = std::make_unique<FunctionWrapper>([](CallbackInfo& info, void* data) {
auto function_define = reinterpret_cast<FunctionDefine<T>*>(data);
auto len = info.Length();
std::shared_ptr<CtxValue> param[len];
std::vector<std::shared_ptr<CtxValue>> param(len);
for (size_t i = 0; i < len; i++) {
param[i] = info[i];
}
Expand All @@ -440,7 +440,7 @@ class Scope : public std::enable_shared_from_this<Scope> {
}
auto t = reinterpret_cast<T*>(info_data);
std::shared_ptr<CtxValue> exception = nullptr;
auto ret = (function_define->callback)(t, static_cast<size_t>(len), param, exception);
auto ret = (function_define->callback)(t, static_cast<size_t>(len), param.data(), exception);
if (exception) {
info.GetReturnValue()->Set(exception);
return;
Expand Down
4 changes: 2 additions & 2 deletions driver/js/src/base/js_convert_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ std::shared_ptr<CtxValue> CreateCtxValue(const std::shared_ptr<Ctx>& ctx,
} else if (value->IsArray()) {
auto array = value->ToArrayChecked();
auto len = array.size();
std::shared_ptr<CtxValue> argv[len];
std::vector<std::shared_ptr<CtxValue>> argv(len);
for (size_t i = 0; i < len; ++i) {
argv[i] = CreateCtxValue(ctx, std::make_shared<HippyValue>(array[i]));
}
return ctx->CreateArray(array.size(), argv);
return ctx->CreateArray(array.size(), argv.data());
} else if (value->IsObject()) {
auto obj = ctx->CreateObject();
auto object = value->ToObjectChecked();
Expand Down
16 changes: 8 additions & 8 deletions driver/js/src/modules/performance/performance_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,15 @@ std::shared_ptr<ClassTemplate<Performance>> RegisterPerformance(const std::weak_
}
if (argument_count == 1) {
auto entries = performance->GetEntriesByName(name);
std::shared_ptr<CtxValue> instances[entries.size()];
std::vector<std::shared_ptr<CtxValue>> instances(entries.size());
for (size_t i = 0; i < entries.size(); ++i) {
auto entry = entries[i];
auto javascript_class = scope->GetJavascriptClass(PerformanceEntry::GetSubTypeString(entry->GetSubType()));
std::shared_ptr<CtxValue> argv[] = { context->CreateString(entry->GetName()),
context->CreateNumber(static_cast<uint32_t>(entry->GetType())) };
instances[i] = context->NewInstance(javascript_class, 2, argv, entry.get());
}
return context->CreateArray(entries.size(), instances);
return context->CreateArray(entries.size(), instances.data());
}
string_view type;
flag = context->GetValueString(arguments[1], &type);
Expand All @@ -277,15 +277,15 @@ std::shared_ptr<ClassTemplate<Performance>> RegisterPerformance(const std::weak_
return nullptr;
}
auto entries = performance->GetEntriesByName(name, entry_type);
std::shared_ptr<CtxValue> instances[entries.size()];
std::vector<std::shared_ptr<CtxValue>> instances(entries.size());
for (size_t i = 0; i < entries.size(); ++i) {
auto entry = entries[i];
auto javascript_class = scope->GetJavascriptClass(PerformanceEntry::GetSubTypeString(entry->GetSubType()));
std::shared_ptr<CtxValue> argv[] = { context->CreateString(entry->GetName()),
context->CreateNumber(static_cast<uint32_t>(entry->GetType())) };
instances[i] = context->NewInstance(javascript_class, 2, argv, entry.get());
}
return context->CreateArray(entries.size(), instances);
return context->CreateArray(entries.size(), instances.data());
};
class_template.functions.emplace_back(std::move(get_entries_by_name_function_define));

Expand Down Expand Up @@ -317,15 +317,15 @@ std::shared_ptr<ClassTemplate<Performance>> RegisterPerformance(const std::weak_
return nullptr;
}
auto entries = performance->GetEntriesByType(entry_type);
std::shared_ptr<CtxValue> instances[entries.size()];
std::vector<std::shared_ptr<CtxValue>> instances(entries.size());
for (size_t i = 0; i < entries.size(); ++i) {
auto entry = entries[i];
auto javascript_class = scope->GetJavascriptClass(PerformanceEntry::GetSubTypeString(entry->GetSubType()));
std::shared_ptr<CtxValue> argv[] = { context->CreateString(entry->GetName()),
context->CreateNumber(static_cast<uint32_t>(entry->GetType())) };
instances[i] = context->NewInstance(javascript_class, 2, argv, entry.get());
}
return context->CreateArray(entries.size(), instances);
return context->CreateArray(entries.size(), instances.data());
};
class_template.functions.emplace_back(std::move(get_entries_by_type_function_define));

Expand Down Expand Up @@ -390,15 +390,15 @@ std::shared_ptr<ClassTemplate<Performance>> RegisterPerformance(const std::weak_
}
auto context = scope->GetContext();
auto entries = performance->GetEntries();
std::shared_ptr<CtxValue> instances[entries.size()];
std::vector<std::shared_ptr<CtxValue>> instances(entries.size());
for (size_t i = 0; i < entries.size(); ++i) {
auto entry = entries[i];
auto javascript_class = scope->GetJavascriptClass(PerformanceEntry::GetSubTypeString(entry->GetSubType()));
std::shared_ptr<CtxValue> argv[] = { context->CreateString(entry->GetName()),
context->CreateNumber(static_cast<uint32_t>(entry->GetType())) };
instances[i] = context->NewInstance(javascript_class, 2, argv, entry.get());
}
return context->CreateArray(entries.size(), instances);
return context->CreateArray(entries.size(), instances.data());
};
class_template.functions.emplace_back(std::move(get_entries_function_define));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ std::shared_ptr<ClassTemplate<PerformanceNavigationTiming>> RegisterPerformanceN
}
auto context = scope->GetContext();
auto bundle_info_array = thiz->GetBundleInfoArray();
std::shared_ptr<CtxValue> array[bundle_info_array.size()];
std::vector<std::shared_ptr<CtxValue>> array(bundle_info_array.size());
for (size_t i = 0; i < bundle_info_array.size(); ++i) {
auto& info = bundle_info_array[i];
auto object = context->CreateObject();
Expand All @@ -124,7 +124,7 @@ std::shared_ptr<ClassTemplate<PerformanceNavigationTiming>> RegisterPerformanceN
context->CreateNumber(info.execute_source_end_.ToEpochDelta().ToMillisecondsF()));
array[i] = object;
}
return context->CreateArray(bundle_info_array.size(), array);
return context->CreateArray(bundle_info_array.size(), array.data());
};
class_template.properties.push_back(std::move(bundle_info));

Expand Down
4 changes: 2 additions & 2 deletions driver/js/src/scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ static void InternalBindingCallback(hippy::napi::CallbackInfo& info, void* data)
}
auto len = info.Length();
auto argc = len > 1 ? (len - 1) : 0;
std::shared_ptr<CtxValue> rest_args[argc];
std::vector<std::shared_ptr<CtxValue>> rest_args(argc);
for (size_t i = 0; i < argc; ++i) {
rest_args[i] = info[i + 1];
}
auto js_object = module_object->BindFunction(scope, rest_args);
auto js_object = module_object->BindFunction(scope, rest_args.data());
info.GetReturnValue()->Set(js_object);
}

Expand Down
4 changes: 2 additions & 2 deletions framework/ios/base/executors/HippyJSExecutor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -568,13 +568,13 @@ - (void)_executeJSCall:(NSString *)method
SharedCtxValuePtr method_value = context->GetProperty(batchedbridge_value, methodName);
if (method_value) {
if (context->IsFunction(method_value)) {
SharedCtxValuePtr function_params[arguments.count];
std::vector<SharedCtxValuePtr> function_params(arguments.count);
for (NSUInteger i = 0; i < arguments.count; i++) {
id obj = arguments[i];
function_params[i] = [obj convertToCtxValue:context];
}
auto tryCatch = hippy::CreateTryCatchScope(true, context);
resultValue = context->CallFunction(method_value, context->GetGlobalObject(), arguments.count, function_params);
resultValue = context->CallFunction(method_value, context->GetGlobalObject(), arguments.count, function_params.data());
if (tryCatch->HasCaught()) {
exception = tryCatch->GetExceptionMessage();
}
Expand Down
4 changes: 2 additions & 2 deletions framework/ios/module/turbo/HippyOCTurboModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ - (id)invokeObjCMethodWithName:(NSString *)methodName
}

size_t size = static_cast<size_t>(array.count);
std::shared_ptr<hippy::napi::CtxValue> buffer[size];
std::vector<std::shared_ptr<hippy::napi::CtxValue>> buffer(size);
for (size_t idx = 0; idx < array.count; idx++) {
buffer[idx] = convertObjcObjectToCtxValue(context, array[idx], module);
}
return context->CreateArray(size, buffer);
return context->CreateArray(size, buffer.data());
}

static std::shared_ptr<hippy::napi::CtxValue> convertNSObjectToCtxValue(const std::shared_ptr<hippy::napi::Ctx> &context,
Expand Down

0 comments on commit 028873c

Please sign in to comment.