Skip to content

Commit 8fbf559

Browse files
Merge pull request #573 from lightpanda-io/typed_arrays
add support for mapping integer typed arrays into zig slices
2 parents b8ac930 + a3323dc commit 8fbf559

File tree

2 files changed

+165
-14
lines changed

2 files changed

+165
-14
lines changed

src/runtime/js.zig

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,7 +1935,7 @@ fn Caller(comptime E: type) type {
19351935
if (last_parameter_type_info == .pointer and last_parameter_type_info.pointer.size == .slice) {
19361936
const slice_type = last_parameter_type_info.pointer.child;
19371937
const corresponding_js_value = info.getArg(@as(u32, @intCast(last_js_parameter)));
1938-
if (corresponding_js_value.isArray() == false and slice_type != u8) {
1938+
if (corresponding_js_value.isArray() == false and corresponding_js_value.isTypedArray() == false and slice_type != u8) {
19391939
is_variadic = true;
19401940
if (js_parameter_count == 0) {
19411941
@field(args, tupleFieldName(params_to_map.len + offset - 1)) = &.{};
@@ -2008,6 +2008,70 @@ fn Caller(comptime E: type) type {
20082008
}
20092009
},
20102010
.slice => {
2011+
if (js_value.isTypedArray()) {
2012+
const buffer_view = js_value.castTo(v8.ArrayBufferView);
2013+
const buffer = buffer_view.getBuffer();
2014+
const backing_store = v8.BackingStore.sharedPtrGet(&buffer.getBackingStore());
2015+
const data = backing_store.getData();
2016+
const byte_len = backing_store.getByteLength();
2017+
2018+
switch (ptr.child) {
2019+
u8 => {
2020+
// need this sentinel check to keep the compiler happy
2021+
if (ptr.sentinel() == null) {
2022+
if (js_value.isUint8Array() or js_value.isUint8ClampedArray()) {
2023+
const arr_ptr = @as([*]u8, @alignCast(@ptrCast(data)));
2024+
return arr_ptr[0..byte_len];
2025+
}
2026+
}
2027+
},
2028+
i8 => {
2029+
if (js_value.isInt8Array()) {
2030+
const arr_ptr = @as([*]i8, @alignCast(@ptrCast(data)));
2031+
return arr_ptr[0..byte_len];
2032+
}
2033+
},
2034+
u16 => {
2035+
if (js_value.isUint16Array()) {
2036+
const arr_ptr = @as([*]u16, @alignCast(@ptrCast(data)));
2037+
return arr_ptr[0 .. byte_len / 2];
2038+
}
2039+
},
2040+
i16 => {
2041+
if (js_value.isInt16Array()) {
2042+
const arr_ptr = @as([*]i16, @alignCast(@ptrCast(data)));
2043+
return arr_ptr[0 .. byte_len / 2];
2044+
}
2045+
},
2046+
u32 => {
2047+
if (js_value.isUint32Array()) {
2048+
const arr_ptr = @as([*]u32, @alignCast(@ptrCast(data)));
2049+
return arr_ptr[0 .. byte_len / 4];
2050+
}
2051+
},
2052+
i32 => {
2053+
if (js_value.isInt32Array()) {
2054+
const arr_ptr = @as([*]i32, @alignCast(@ptrCast(data)));
2055+
return arr_ptr[0 .. byte_len / 4];
2056+
}
2057+
},
2058+
u64 => {
2059+
if (js_value.isBigUint64Array()) {
2060+
const arr_ptr = @as([*]u64, @alignCast(@ptrCast(data)));
2061+
return arr_ptr[0 .. byte_len / 8];
2062+
}
2063+
},
2064+
i64 => {
2065+
if (js_value.isBigInt64Array()) {
2066+
const arr_ptr = @as([*]i64, @alignCast(@ptrCast(data)));
2067+
return arr_ptr[0 .. byte_len / 8];
2068+
}
2069+
},
2070+
else => {},
2071+
}
2072+
return error.InvalidArgument;
2073+
}
2074+
20112075
if (ptr.child == u8) {
20122076
if (ptr.sentinel()) |s| {
20132077
if (comptime s == 0) {
@@ -2018,18 +2082,6 @@ fn Caller(comptime E: type) type {
20182082
}
20192083
}
20202084

2021-
// TODO: TypedArray
2022-
// if (js_value.isArrayBufferView()) {
2023-
// const abv = js_value.castTo(v8.ArrayBufferView);
2024-
// const ab = abv.getBuffer();
2025-
// const bs = v8.BackingStore.sharedPtrGet(&ab.getBackingStore());
2026-
// const data = bs.getData();
2027-
// var arr = @as([*]i32, @alignCast(@ptrCast(data)))[0..2];
2028-
// std.debug.print("{d} {d} {d}\n", .{arr[0], arr[1], bs.getByteLength()});
2029-
// arr[1] = 3333;
2030-
// return &.{};
2031-
// }
2032-
20332085
if (!js_value.isArray()) {
20342086
return error.InvalidArgument;
20352087
}
@@ -2104,7 +2156,7 @@ fn Caller(comptime E: type) type {
21042156
else => {},
21052157
}
21062158

2107-
@compileError(std.fmt.comptimePrint("{s} has an unsupported parameter type: {s}", .{ named_function.full_name, @typeName(T) }));
2159+
@compileError(named_function.full_name ++ " has an unsupported parameter type: " ++ @typeName(T));
21082160
}
21092161

21102162
fn jsIntToZig(comptime T: type, js_value: v8.Value, context: v8.Context) !T {

src/runtime/test_primitive_types.zig

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,54 @@ const Primitives = struct {
104104
pub fn _echoStringZ(_: *const Primitives, a: [:0]const u8) []const u8 {
105105
return a;
106106
}
107+
108+
pub fn _int8(_: *const Primitives, arr: []i8) void {
109+
for (arr) |*a| {
110+
a.* -= @intCast(arr.len);
111+
}
112+
}
113+
114+
pub fn _uint8(_: *const Primitives, arr: []u8) void {
115+
for (arr) |*a| {
116+
a.* += @intCast(arr.len);
117+
}
118+
}
119+
120+
pub fn _int16(_: *const Primitives, arr: []i16) void {
121+
for (arr) |*a| {
122+
a.* -= @intCast(arr.len);
123+
}
124+
}
125+
126+
pub fn _uint16(_: *const Primitives, arr: []u16) void {
127+
for (arr) |*a| {
128+
a.* += @intCast(arr.len);
129+
}
130+
}
131+
132+
pub fn _int32(_: *const Primitives, arr: []i32) void {
133+
for (arr) |*a| {
134+
a.* -= @intCast(arr.len);
135+
}
136+
}
137+
138+
pub fn _uint32(_: *const Primitives, arr: []u32) void {
139+
for (arr) |*a| {
140+
a.* += @intCast(arr.len);
141+
}
142+
}
143+
144+
pub fn _int64(_: *const Primitives, arr: []i64) void {
145+
for (arr) |*a| {
146+
a.* -= @intCast(arr.len);
147+
}
148+
}
149+
150+
pub fn _uint64(_: *const Primitives, arr: []u64) void {
151+
for (arr) |*a| {
152+
a.* += @intCast(arr.len);
153+
}
154+
}
107155
};
108156

109157
const testing = @import("testing.zig");
@@ -185,4 +233,55 @@ test "JS: primitive types" {
185233
.{ "p.echoString('over 9000!');", "over 9000!" },
186234
.{ "p.echoStringZ('Teg');", "Teg" },
187235
}, .{});
236+
237+
// typed arrays
238+
try runner.testCases(&.{
239+
.{ "let arr_i8 = new Int8Array([-10, -20, -30]);", "undefined" },
240+
.{ "p.int8(arr_i8)", "undefined" },
241+
.{ "arr_i8;", "-13,-23,-33" },
242+
243+
.{ "let arr_u8 = new Uint8Array([10, 20, 30]);", "undefined" },
244+
.{ "p.uint8(arr_u8)", "undefined" },
245+
.{ "arr_u8;", "13,23,33" },
246+
247+
.{ "let arr_i16 = new Int16Array([-1000, -2000, -3000]);", "undefined" },
248+
.{ "p.int16(arr_i16)", "undefined" },
249+
.{ "arr_i16;", "-1003,-2003,-3003" },
250+
251+
.{ "let arr_u16 = new Uint16Array([1000, 2000, 3000]);", "undefined" },
252+
.{ "p.uint16(arr_u16)", "undefined" },
253+
.{ "arr_u16;", "1003,2003,3003" },
254+
255+
.{ "let arr_i32 = new Int32Array([-1000000, -2000000, -3000000]);", "undefined" },
256+
.{ "p.int32(arr_i32)", "undefined" },
257+
.{ "arr_i32;", "-1000003,-2000003,-3000003" },
258+
259+
.{ "let arr_u32 = new Uint32Array([1000000, 2000000, 3000000]);", "undefined" },
260+
.{ "p.uint32(arr_u32)", "undefined" },
261+
.{ "arr_u32;", "1000003,2000003,3000003" },
262+
263+
.{ "let arr_i64 = new BigInt64Array([-1000000000n, -2000000000n, -3000000000n]);", "undefined" },
264+
.{ "p.int64(arr_i64)", "undefined" },
265+
.{ "arr_i64;", "-1000000003,-2000000003,-3000000003" },
266+
267+
.{ "let arr_u64 = new BigUint64Array([1000000000n, 2000000000n, 3000000000n]);", "undefined" },
268+
.{ "p.uint64(arr_u64)", "undefined" },
269+
.{ "arr_u64;", "1000000003,2000000003,3000000003" },
270+
271+
.{ "try { p.int8(arr_u8) } catch(e) { e instanceof TypeError; }", "true" },
272+
.{ "try { p.intu8(arr_i8) } catch(e) { e instanceof TypeError; }", "true" },
273+
.{ "try { p.intu8(arr_u32) } catch(e) { e instanceof TypeError; }", "true" },
274+
275+
.{ "try { p.int16(arr_u8) } catch(e) { e instanceof TypeError; }", "true" },
276+
.{ "try { p.intu16(arr_i16) } catch(e) { e instanceof TypeError; }", "true" },
277+
.{ "try { p.int16(arr_i64) } catch(e) { e instanceof TypeError; }", "true" },
278+
279+
.{ "try { p.int32(arr_u32) } catch(e) { e instanceof TypeError; }", "true" },
280+
.{ "try { p.intu32(arr_i32) } catch(e) { e instanceof TypeError; }", "true" },
281+
.{ "try { p.intu32(arr_u32) } catch(e) { e instanceof TypeError; }", "true" },
282+
283+
.{ "try { p.int64(arr_u64) } catch(e) { e instanceof TypeError; }", "true" },
284+
.{ "try { p.intu64(arr_i64) } catch(e) { e instanceof TypeError; }", "true" },
285+
.{ "try { p.intu64(arr_u32) } catch(e) { e instanceof TypeError; }", "true" },
286+
}, .{});
188287
}

0 commit comments

Comments
 (0)