Skip to content

Commit

Permalink
fix(ios): optimize variable array usage in convertToCtxValue and jscctx
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Dec 30, 2024
1 parent 3062f42 commit 501e45a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
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

0 comments on commit 501e45a

Please sign in to comment.