Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): optimize variable array usage in convertToCtxValue and jscctx #4159

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions driver/js/src/napi/jsc/jsc_ctx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -801,21 +801,18 @@ std::shared_ptr<CtxValue> JSCCtx::CreateObject(const std::unordered_map<std::sha

std::shared_ptr<CtxValue> JSCCtx::CreateArray(size_t count,
std::shared_ptr<CtxValue> array[]) {
if (count < 0) {
return nullptr;
}
if (0 == count) {
if (count == 0) {
return std::make_shared<JSCCtxValue>(context_, JSObjectMakeArray(context_, 0, nullptr, nullptr));
}

JSValueRef values[count]; // NOLINT(runtime/arrays)
std::vector<JSValueRef> values(count);
for (size_t i = 0; i < count; i++) {
auto ele_value = std::static_pointer_cast<JSCCtxValue>(array[i]);
values[i] = ele_value ? ele_value->value_ : nullptr;
}

JSValueRef exception = nullptr;
JSValueRef value_ref = JSObjectMakeArray(context_, count, values, &exception);
JSValueRef value_ref = JSObjectMakeArray(context_, count, values.data(), &exception);
if (exception) {
SetException(std::make_shared<JSCCtxValue>(context_, exception));
return nullptr;
Expand Down
11 changes: 6 additions & 5 deletions framework/ios/utils/NSObject+CtxValue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,17 @@ @implementation NSArray (CtxValue)

- (CtxValuePtr)convertToCtxValue:(const CtxPtr &)context {
@autoreleasepool {
if (0 == [self count]) {
NSUInteger count = [self count];
if (0 == count) {
return context->CreateArray(0, nullptr);
}
CtxValuePtr value[[self count]];
size_t index = 0;
std::vector<CtxValuePtr> values;
values.reserve(count);
for (id obj in self) {
auto item = [obj convertToCtxValue:context];
value[index++] = std::move(item);
values.push_back(std::move(item));
}
return context->CreateArray([self count], value);
return context->CreateArray(count, values.data());
}
}

Expand Down
Loading