mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-28 22:53:28 +00:00
runtime: fix returning an empty array crash
This commit is contained in:
@@ -3469,11 +3469,16 @@ fn simpleZigValueToJs(isolate: v8.Isolate, value: anytype, comptime fail: bool)
|
||||
else => @compileError("Invalid TypeArray type: " ++ @typeName(value_type)),
|
||||
};
|
||||
|
||||
const buffer_len = len * bits / 8;
|
||||
const backing_store = v8.BackingStore.init(isolate, buffer_len);
|
||||
const data: [*]u8 = @alignCast(@ptrCast(backing_store.getData()));
|
||||
@memcpy(data[0..buffer_len], @as([]const u8, @ptrCast(values))[0..buffer_len]);
|
||||
const array_buffer = v8.ArrayBuffer.initWithBackingStore(isolate, &backing_store.toSharedPtr());
|
||||
var array_buffer: v8.ArrayBuffer = undefined;
|
||||
if (len == 0) {
|
||||
array_buffer = v8.ArrayBuffer.init(isolate, 0);
|
||||
} else {
|
||||
const buffer_len = len * bits / 8;
|
||||
const backing_store = v8.BackingStore.init(isolate, buffer_len);
|
||||
const data: [*]u8 = @alignCast(@ptrCast(backing_store.getData()));
|
||||
@memcpy(data[0..buffer_len], @as([]const u8, @ptrCast(values))[0..buffer_len]);
|
||||
array_buffer = v8.ArrayBuffer.initWithBackingStore(isolate, &backing_store.toSharedPtr());
|
||||
}
|
||||
|
||||
switch (@typeInfo(value_type)) {
|
||||
.int => |n| switch (n.signedness) {
|
||||
|
||||
@@ -118,6 +118,10 @@ const Primitives = struct {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn _returnEmptyUint8(_: *const Primitives) Env.TypedArray(u8) {
|
||||
return .{ .values = &.{} };
|
||||
}
|
||||
|
||||
pub fn _returnUint8(_: *const Primitives) Env.TypedArray(u8) {
|
||||
return .{ .values = &.{ 10, 20, 250 } };
|
||||
}
|
||||
@@ -325,6 +329,7 @@ test "JS: primitive types" {
|
||||
.{ "try { p.intu64(arr_i64) } catch(e) { e instanceof TypeError; }", "true" },
|
||||
.{ "try { p.intu64(arr_u32) } catch(e) { e instanceof TypeError; }", "true" },
|
||||
|
||||
.{ "p.returnEmptyUint8()", "" },
|
||||
.{ "p.returnUint8()", "10,20,250" },
|
||||
.{ "p.returnInt8()", "10,-20,-120" },
|
||||
.{ "p.returnUint16()", "10,200,2050" },
|
||||
|
||||
Reference in New Issue
Block a user