diff --git a/driver/js/src/napi/jsc/jsc_ctx.cc b/driver/js/src/napi/jsc/jsc_ctx.cc index 293cc106e6e..6e391e512bc 100644 --- a/driver/js/src/napi/jsc/jsc_ctx.cc +++ b/driver/js/src/napi/jsc/jsc_ctx.cc @@ -801,21 +801,18 @@ std::shared_ptr JSCCtx::CreateObject(const std::unordered_map JSCCtx::CreateArray(size_t count, std::shared_ptr array[]) { - if (count < 0) { - return nullptr; - } - if (0 == count) { + if (count == 0) { return std::make_shared(context_, JSObjectMakeArray(context_, 0, nullptr, nullptr)); } - - JSValueRef values[count]; // NOLINT(runtime/arrays) + + std::vector values(count); for (size_t i = 0; i < count; i++) { auto ele_value = std::static_pointer_cast(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(context_, exception)); return nullptr; diff --git a/framework/ios/utils/NSObject+CtxValue.mm b/framework/ios/utils/NSObject+CtxValue.mm index 2dbd3939d65..06445135134 100644 --- a/framework/ios/utils/NSObject+CtxValue.mm +++ b/framework/ios/utils/NSObject+CtxValue.mm @@ -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 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()); } }