diff options
author | 2023-02-16 03:11:22 -0800 | |
---|---|---|
committer | 2023-02-16 03:11:22 -0800 | |
commit | ce01a31e944c274eae7058e250ec4e84a661f50c (patch) | |
tree | 4d67a606c669510fbe8296fc359cfdfa6669737c | |
parent | 37fafc208f59b46de3876ef535fea7551fc65241 (diff) | |
download | bun-ce01a31e944c274eae7058e250ec4e84a661f50c.tar.gz bun-ce01a31e944c274eae7058e250ec4e84a661f50c.tar.zst bun-ce01a31e944c274eae7058e250ec4e84a661f50c.zip |
[napi] Fix crash in creating arrays > 8 elements long
-rw-r--r-- | src/napi/napi.zig | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/src/napi/napi.zig b/src/napi/napi.zig index 602e7e286..f044e2b06 100644 --- a/src/napi/napi.zig +++ b/src/napi/napi.zig @@ -238,19 +238,18 @@ const prefilled_undefined_args_array: [128]JSC.JSValue = brk: { }; pub export fn napi_create_array_with_length(env: napi_env, length: usize, result: *napi_value) napi_status { log("napi_create_array_with_length", .{}); - if (length < prefilled_undefined_args_array.len) { - result.* = JSValue.c(JSC.C.JSObjectMakeArray(env.ref(), length, @ptrCast([*]const JSC.C.JSValueRef, &prefilled_undefined_args_array[0..length]), null)); - return .ok; - } + const len = @intCast(u32, length); + + const array = JSC.JSValue.createEmptyArray(env, len); + array.ensureStillAlive(); - const allocator = env.bunVM().allocator; - var undefined_args = allocator.alloc(JSC.C.JSValueRef, length) catch return genericFailure(); - defer allocator.free(undefined_args); - for (undefined_args) |_, i| { - undefined_args[i] = JSValue.jsUndefined().asObjectRef(); + var i: u32 = 0; + while (i < len) : (i += 1) { + array.putIndex(env, i, JSValue.jsUndefined()); } - result.* = JSValue.c(JSC.C.JSObjectMakeArray(env.ref(), length, undefined_args.ptr, null)); + array.ensureStillAlive(); + result.* = array; return .ok; } pub export fn napi_create_double(_: napi_env, value: f64, result: *napi_value) napi_status { |