Skip to content
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
23 changes: 18 additions & 5 deletions src/runtime/js.zig
Original file line number Diff line number Diff line change
Expand Up @@ -976,49 +976,57 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
// need this sentinel check to keep the compiler happy
if (ptr.sentinel() == null) {
if (force_u8 or js_value.isUint8Array() or js_value.isUint8ClampedArray()) {
if (byte_len == 0) return &[_]u8{};
const arr_ptr = @as([*]u8, @alignCast(@ptrCast(data)));
return arr_ptr[0..byte_len];
}
}
},
i8 => {
if (js_value.isInt8Array()) {
if (byte_len == 0) return &[_]i8{};
const arr_ptr = @as([*]i8, @alignCast(@ptrCast(data)));
return arr_ptr[0..byte_len];
}
},
u16 => {
if (js_value.isUint16Array()) {
if (byte_len == 0) return &[_]u16{};
const arr_ptr = @as([*]u16, @alignCast(@ptrCast(data)));
return arr_ptr[0 .. byte_len / 2];
}
},
i16 => {
if (js_value.isInt16Array()) {
if (byte_len == 0) return &[_]i16{};
const arr_ptr = @as([*]i16, @alignCast(@ptrCast(data)));
return arr_ptr[0 .. byte_len / 2];
}
},
u32 => {
if (js_value.isUint32Array()) {
if (byte_len == 0) return &[_]u32{};
const arr_ptr = @as([*]u32, @alignCast(@ptrCast(data)));
return arr_ptr[0 .. byte_len / 4];
}
},
i32 => {
if (js_value.isInt32Array()) {
if (byte_len == 0) return &[_]i32{};
const arr_ptr = @as([*]i32, @alignCast(@ptrCast(data)));
return arr_ptr[0 .. byte_len / 4];
}
},
u64 => {
if (js_value.isBigUint64Array()) {
if (byte_len == 0) return &[_]u64{};
const arr_ptr = @as([*]u64, @alignCast(@ptrCast(data)));
return arr_ptr[0 .. byte_len / 8];
}
},
i64 => {
if (js_value.isBigInt64Array()) {
if (byte_len == 0) return &[_]i64{};
const arr_ptr = @as([*]i64, @alignCast(@ptrCast(data)));
return arr_ptr[0 .. byte_len / 8];
}
Expand Down Expand Up @@ -3469,11 +3477,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) {
Expand Down
9 changes: 9 additions & 0 deletions src/runtime/test_primitive_types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 } };
}
Expand Down Expand Up @@ -277,6 +281,10 @@ test "JS: primitive types" {

// typed arrays
try runner.testCases(&.{
.{ "let empty_arr = new Int8Array([]);", "undefined" },
.{ "p.int8(empty_arr)", "undefined" },
.{ "empty_arr;", "" },

.{ "let arr_i8 = new Int8Array([-10, -20, -30]);", "undefined" },
.{ "p.int8(arr_i8)", "undefined" },
.{ "arr_i8;", "-13,-23,-33" },
Expand Down Expand Up @@ -325,6 +333,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" },
Expand Down