mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 23:23:28 +00:00
Merge pull request #573 from lightpanda-io/typed_arrays
add support for mapping integer typed arrays into zig slices
This commit is contained in:
@@ -1935,7 +1935,7 @@ fn Caller(comptime E: type) type {
|
|||||||
if (last_parameter_type_info == .pointer and last_parameter_type_info.pointer.size == .slice) {
|
if (last_parameter_type_info == .pointer and last_parameter_type_info.pointer.size == .slice) {
|
||||||
const slice_type = last_parameter_type_info.pointer.child;
|
const slice_type = last_parameter_type_info.pointer.child;
|
||||||
const corresponding_js_value = info.getArg(@as(u32, @intCast(last_js_parameter)));
|
const corresponding_js_value = info.getArg(@as(u32, @intCast(last_js_parameter)));
|
||||||
if (corresponding_js_value.isArray() == false and slice_type != u8) {
|
if (corresponding_js_value.isArray() == false and corresponding_js_value.isTypedArray() == false and slice_type != u8) {
|
||||||
is_variadic = true;
|
is_variadic = true;
|
||||||
if (js_parameter_count == 0) {
|
if (js_parameter_count == 0) {
|
||||||
@field(args, tupleFieldName(params_to_map.len + offset - 1)) = &.{};
|
@field(args, tupleFieldName(params_to_map.len + offset - 1)) = &.{};
|
||||||
@@ -2008,6 +2008,70 @@ fn Caller(comptime E: type) type {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
.slice => {
|
.slice => {
|
||||||
|
if (js_value.isTypedArray()) {
|
||||||
|
const buffer_view = js_value.castTo(v8.ArrayBufferView);
|
||||||
|
const buffer = buffer_view.getBuffer();
|
||||||
|
const backing_store = v8.BackingStore.sharedPtrGet(&buffer.getBackingStore());
|
||||||
|
const data = backing_store.getData();
|
||||||
|
const byte_len = backing_store.getByteLength();
|
||||||
|
|
||||||
|
switch (ptr.child) {
|
||||||
|
u8 => {
|
||||||
|
// need this sentinel check to keep the compiler happy
|
||||||
|
if (ptr.sentinel() == null) {
|
||||||
|
if (js_value.isUint8Array() or js_value.isUint8ClampedArray()) {
|
||||||
|
const arr_ptr = @as([*]u8, @alignCast(@ptrCast(data)));
|
||||||
|
return arr_ptr[0..byte_len];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
i8 => {
|
||||||
|
if (js_value.isInt8Array()) {
|
||||||
|
const arr_ptr = @as([*]i8, @alignCast(@ptrCast(data)));
|
||||||
|
return arr_ptr[0..byte_len];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
u16 => {
|
||||||
|
if (js_value.isUint16Array()) {
|
||||||
|
const arr_ptr = @as([*]u16, @alignCast(@ptrCast(data)));
|
||||||
|
return arr_ptr[0 .. byte_len / 2];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
i16 => {
|
||||||
|
if (js_value.isInt16Array()) {
|
||||||
|
const arr_ptr = @as([*]i16, @alignCast(@ptrCast(data)));
|
||||||
|
return arr_ptr[0 .. byte_len / 2];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
u32 => {
|
||||||
|
if (js_value.isUint32Array()) {
|
||||||
|
const arr_ptr = @as([*]u32, @alignCast(@ptrCast(data)));
|
||||||
|
return arr_ptr[0 .. byte_len / 4];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
i32 => {
|
||||||
|
if (js_value.isInt32Array()) {
|
||||||
|
const arr_ptr = @as([*]i32, @alignCast(@ptrCast(data)));
|
||||||
|
return arr_ptr[0 .. byte_len / 4];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
u64 => {
|
||||||
|
if (js_value.isBigUint64Array()) {
|
||||||
|
const arr_ptr = @as([*]u64, @alignCast(@ptrCast(data)));
|
||||||
|
return arr_ptr[0 .. byte_len / 8];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
i64 => {
|
||||||
|
if (js_value.isBigInt64Array()) {
|
||||||
|
const arr_ptr = @as([*]i64, @alignCast(@ptrCast(data)));
|
||||||
|
return arr_ptr[0 .. byte_len / 8];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
return error.InvalidArgument;
|
||||||
|
}
|
||||||
|
|
||||||
if (ptr.child == u8) {
|
if (ptr.child == u8) {
|
||||||
if (ptr.sentinel()) |s| {
|
if (ptr.sentinel()) |s| {
|
||||||
if (comptime s == 0) {
|
if (comptime s == 0) {
|
||||||
@@ -2018,18 +2082,6 @@ fn Caller(comptime E: type) type {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: TypedArray
|
|
||||||
// if (js_value.isArrayBufferView()) {
|
|
||||||
// const abv = js_value.castTo(v8.ArrayBufferView);
|
|
||||||
// const ab = abv.getBuffer();
|
|
||||||
// const bs = v8.BackingStore.sharedPtrGet(&ab.getBackingStore());
|
|
||||||
// const data = bs.getData();
|
|
||||||
// var arr = @as([*]i32, @alignCast(@ptrCast(data)))[0..2];
|
|
||||||
// std.debug.print("{d} {d} {d}\n", .{arr[0], arr[1], bs.getByteLength()});
|
|
||||||
// arr[1] = 3333;
|
|
||||||
// return &.{};
|
|
||||||
// }
|
|
||||||
|
|
||||||
if (!js_value.isArray()) {
|
if (!js_value.isArray()) {
|
||||||
return error.InvalidArgument;
|
return error.InvalidArgument;
|
||||||
}
|
}
|
||||||
@@ -2104,7 +2156,7 @@ fn Caller(comptime E: type) type {
|
|||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
|
|
||||||
@compileError(std.fmt.comptimePrint("{s} has an unsupported parameter type: {s}", .{ named_function.full_name, @typeName(T) }));
|
@compileError(named_function.full_name ++ " has an unsupported parameter type: " ++ @typeName(T));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn jsIntToZig(comptime T: type, js_value: v8.Value, context: v8.Context) !T {
|
fn jsIntToZig(comptime T: type, js_value: v8.Value, context: v8.Context) !T {
|
||||||
|
|||||||
@@ -104,6 +104,54 @@ const Primitives = struct {
|
|||||||
pub fn _echoStringZ(_: *const Primitives, a: [:0]const u8) []const u8 {
|
pub fn _echoStringZ(_: *const Primitives, a: [:0]const u8) []const u8 {
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn _int8(_: *const Primitives, arr: []i8) void {
|
||||||
|
for (arr) |*a| {
|
||||||
|
a.* -= @intCast(arr.len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn _uint8(_: *const Primitives, arr: []u8) void {
|
||||||
|
for (arr) |*a| {
|
||||||
|
a.* += @intCast(arr.len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn _int16(_: *const Primitives, arr: []i16) void {
|
||||||
|
for (arr) |*a| {
|
||||||
|
a.* -= @intCast(arr.len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn _uint16(_: *const Primitives, arr: []u16) void {
|
||||||
|
for (arr) |*a| {
|
||||||
|
a.* += @intCast(arr.len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn _int32(_: *const Primitives, arr: []i32) void {
|
||||||
|
for (arr) |*a| {
|
||||||
|
a.* -= @intCast(arr.len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn _uint32(_: *const Primitives, arr: []u32) void {
|
||||||
|
for (arr) |*a| {
|
||||||
|
a.* += @intCast(arr.len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn _int64(_: *const Primitives, arr: []i64) void {
|
||||||
|
for (arr) |*a| {
|
||||||
|
a.* -= @intCast(arr.len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn _uint64(_: *const Primitives, arr: []u64) void {
|
||||||
|
for (arr) |*a| {
|
||||||
|
a.* += @intCast(arr.len);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const testing = @import("testing.zig");
|
const testing = @import("testing.zig");
|
||||||
@@ -185,4 +233,55 @@ test "JS: primitive types" {
|
|||||||
.{ "p.echoString('over 9000!');", "over 9000!" },
|
.{ "p.echoString('over 9000!');", "over 9000!" },
|
||||||
.{ "p.echoStringZ('Teg');", "Teg" },
|
.{ "p.echoStringZ('Teg');", "Teg" },
|
||||||
}, .{});
|
}, .{});
|
||||||
|
|
||||||
|
// typed arrays
|
||||||
|
try runner.testCases(&.{
|
||||||
|
.{ "let arr_i8 = new Int8Array([-10, -20, -30]);", "undefined" },
|
||||||
|
.{ "p.int8(arr_i8)", "undefined" },
|
||||||
|
.{ "arr_i8;", "-13,-23,-33" },
|
||||||
|
|
||||||
|
.{ "let arr_u8 = new Uint8Array([10, 20, 30]);", "undefined" },
|
||||||
|
.{ "p.uint8(arr_u8)", "undefined" },
|
||||||
|
.{ "arr_u8;", "13,23,33" },
|
||||||
|
|
||||||
|
.{ "let arr_i16 = new Int16Array([-1000, -2000, -3000]);", "undefined" },
|
||||||
|
.{ "p.int16(arr_i16)", "undefined" },
|
||||||
|
.{ "arr_i16;", "-1003,-2003,-3003" },
|
||||||
|
|
||||||
|
.{ "let arr_u16 = new Uint16Array([1000, 2000, 3000]);", "undefined" },
|
||||||
|
.{ "p.uint16(arr_u16)", "undefined" },
|
||||||
|
.{ "arr_u16;", "1003,2003,3003" },
|
||||||
|
|
||||||
|
.{ "let arr_i32 = new Int32Array([-1000000, -2000000, -3000000]);", "undefined" },
|
||||||
|
.{ "p.int32(arr_i32)", "undefined" },
|
||||||
|
.{ "arr_i32;", "-1000003,-2000003,-3000003" },
|
||||||
|
|
||||||
|
.{ "let arr_u32 = new Uint32Array([1000000, 2000000, 3000000]);", "undefined" },
|
||||||
|
.{ "p.uint32(arr_u32)", "undefined" },
|
||||||
|
.{ "arr_u32;", "1000003,2000003,3000003" },
|
||||||
|
|
||||||
|
.{ "let arr_i64 = new BigInt64Array([-1000000000n, -2000000000n, -3000000000n]);", "undefined" },
|
||||||
|
.{ "p.int64(arr_i64)", "undefined" },
|
||||||
|
.{ "arr_i64;", "-1000000003,-2000000003,-3000000003" },
|
||||||
|
|
||||||
|
.{ "let arr_u64 = new BigUint64Array([1000000000n, 2000000000n, 3000000000n]);", "undefined" },
|
||||||
|
.{ "p.uint64(arr_u64)", "undefined" },
|
||||||
|
.{ "arr_u64;", "1000000003,2000000003,3000000003" },
|
||||||
|
|
||||||
|
.{ "try { p.int8(arr_u8) } catch(e) { e instanceof TypeError; }", "true" },
|
||||||
|
.{ "try { p.intu8(arr_i8) } catch(e) { e instanceof TypeError; }", "true" },
|
||||||
|
.{ "try { p.intu8(arr_u32) } catch(e) { e instanceof TypeError; }", "true" },
|
||||||
|
|
||||||
|
.{ "try { p.int16(arr_u8) } catch(e) { e instanceof TypeError; }", "true" },
|
||||||
|
.{ "try { p.intu16(arr_i16) } catch(e) { e instanceof TypeError; }", "true" },
|
||||||
|
.{ "try { p.int16(arr_i64) } catch(e) { e instanceof TypeError; }", "true" },
|
||||||
|
|
||||||
|
.{ "try { p.int32(arr_u32) } catch(e) { e instanceof TypeError; }", "true" },
|
||||||
|
.{ "try { p.intu32(arr_i32) } catch(e) { e instanceof TypeError; }", "true" },
|
||||||
|
.{ "try { p.intu32(arr_u32) } catch(e) { e instanceof TypeError; }", "true" },
|
||||||
|
|
||||||
|
.{ "try { p.int64(arr_u64) } catch(e) { e instanceof TypeError; }", "true" },
|
||||||
|
.{ "try { p.intu64(arr_i64) } catch(e) { e instanceof TypeError; }", "true" },
|
||||||
|
.{ "try { p.intu64(arr_u32) } catch(e) { e instanceof TypeError; }", "true" },
|
||||||
|
}, .{});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user