Merge pull request #1213 from lightpanda-io/nikneym/remove-kludge
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
zig-test / zig build dev (push) Has been cancelled
zig-test / browser fetch (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled

This commit is contained in:
Halil Durak
2025-11-17 10:01:26 +03:00
committed by GitHub
2 changed files with 106 additions and 103 deletions

View File

@@ -780,41 +780,39 @@ pub fn jsValueToZig(self: *Context, comptime named_function: NamedFunction, comp
// Extracted so that it can be used in both jsValueToZig and in // Extracted so that it can be used in both jsValueToZig and in
// probeJsValueToZig. Avoids having to duplicate this logic when probing. // probeJsValueToZig. Avoids having to duplicate this logic when probing.
fn jsValueToStruct(self: *Context, comptime named_function: NamedFunction, comptime T: type, js_value: v8.Value) !?T { fn jsValueToStruct(self: *Context, comptime named_function: NamedFunction, comptime T: type, js_value: v8.Value) !?T {
if (T == js.Function) { return switch (T) {
js.Function => {
if (!js_value.isFunction()) { if (!js_value.isFunction()) {
return null; return null;
} }
return try self.createFunction(js_value); return try self.createFunction(js_value);
} },
// zig fmt: off
if (@hasDecl(T, "_TYPED_ARRAY_ID_KLUDGE")) { js.TypedArray(u8), js.TypedArray(u16), js.TypedArray(u32), js.TypedArray(u64),
const VT = @typeInfo(std.meta.fieldInfo(T, .values).type).pointer.child; js.TypedArray(i8), js.TypedArray(i16), js.TypedArray(i32), js.TypedArray(i64),
const arr = (try self.jsValueToTypedArray(VT, js_value)) orelse return null; js.TypedArray(f32), js.TypedArray(f64),
return .{ .values = arr }; // zig fmt: on
} => {
const ValueType = @typeInfo(std.meta.fieldInfo(T, .values).type).pointer.child;
if (T == js.String) { const slice = (try self.jsValueToTypedArray(ValueType, js_value)) orelse return null;
return .{ .string = try self.valueToString(js_value, .{ .allocator = self.arena }) }; return .{ .values = slice };
} },
js.String => .{ .string = try self.valueToString(js_value, .{ .allocator = self.arena }) },
const js_obj = js_value.castTo(v8.Object);
if (comptime T == js.Object) {
// Caller wants an opaque js.Object. Probably a parameter // Caller wants an opaque js.Object. Probably a parameter
// that it needs to pass back into a callback // that it needs to pass back into a callback.
return js.Object{ js.Object => js.Object{
.js_obj = js_obj, .js_obj = js_value.castTo(v8.Object),
.context = self, .context = self,
}; },
} else => {
const js_obj = js_value.castTo(v8.Object);
if (!js_value.isObject()) { if (!js_value.isObject()) {
return null; return null;
} }
const v8_context = self.v8_context; const v8_context = self.v8_context;
const isolate = self.isolate; const isolate = self.isolate;
var value: T = undefined; var value: T = undefined;
inline for (@typeInfo(T).@"struct".fields) |field| { inline for (@typeInfo(T).@"struct".fields) |field| {
const name = field.name; const name = field.name;
@@ -829,6 +827,8 @@ fn jsValueToStruct(self: *Context, comptime named_function: NamedFunction, compt
} }
} }
return value; return value;
},
};
} }
fn jsValueToTypedArray(_: *Context, comptime T: type, js_value: v8.Value) !?[]T { fn jsValueToTypedArray(_: *Context, comptime T: type, js_value: v8.Value) !?[]T {

View File

@@ -48,8 +48,6 @@ const NamedFunction = Context.NamedFunction;
// Env.JsObject. Want a TypedArray? Env.TypedArray. // Env.JsObject. Want a TypedArray? Env.TypedArray.
pub fn TypedArray(comptime T: type) type { pub fn TypedArray(comptime T: type) type {
return struct { return struct {
pub const _TYPED_ARRAY_ID_KLUDGE = true;
values: []const T, values: []const T,
pub fn dupe(self: TypedArray(T), allocator: Allocator) !TypedArray(T) { pub fn dupe(self: TypedArray(T), allocator: Allocator) !TypedArray(T) {
@@ -327,9 +325,8 @@ pub fn simpleZigValueToJs(isolate: v8.Isolate, value: anytype, comptime fail: bo
return v8.initNull(isolate).toValue(); return v8.initNull(isolate).toValue();
}, },
.@"struct" => { .@"struct" => {
const T = @TypeOf(value); switch (@TypeOf(value)) {
ArrayBuffer => {
if (T == ArrayBuffer) {
const values = value.values; const values = value.values;
const len = values.len; const len = values.len;
var array_buffer: v8.ArrayBuffer = undefined; var array_buffer: v8.ArrayBuffer = undefined;
@@ -339,16 +336,20 @@ pub fn simpleZigValueToJs(isolate: v8.Isolate, value: anytype, comptime fail: bo
array_buffer = v8.ArrayBuffer.initWithBackingStore(isolate, &backing_store.toSharedPtr()); array_buffer = v8.ArrayBuffer.initWithBackingStore(isolate, &backing_store.toSharedPtr());
return .{ .handle = array_buffer.handle }; return .{ .handle = array_buffer.handle };
} },
// zig fmt: off
if (@hasDecl(T, "_TYPED_ARRAY_ID_KLUDGE")) { TypedArray(u8), TypedArray(u16), TypedArray(u32), TypedArray(u64),
TypedArray(i8), TypedArray(i16), TypedArray(i32), TypedArray(i64),
TypedArray(f32), TypedArray(f64),
// zig fmt: on
=> {
const values = value.values; const values = value.values;
const value_type = @typeInfo(@TypeOf(values)).pointer.child; const value_type = @typeInfo(@TypeOf(values)).pointer.child;
const len = values.len; const len = values.len;
const bits = switch (@typeInfo(value_type)) { const bits = switch (@typeInfo(value_type)) {
.int => |n| n.bits, .int => |n| n.bits,
.float => |f| f.bits, .float => |f| f.bits,
else => @compileError("Invalid TypeArray type: " ++ @typeName(value_type)), else => @compileError("Invalid TypedArray type: " ++ @typeName(value_type)),
}; };
var array_buffer: v8.ArrayBuffer = undefined; var array_buffer: v8.ArrayBuffer = undefined;
@@ -388,7 +389,9 @@ pub fn simpleZigValueToJs(isolate: v8.Isolate, value: anytype, comptime fail: bo
} }
// We normally don't fail in this function unless fail == true // We normally don't fail in this function unless fail == true
// but this can never be valid. // but this can never be valid.
@compileError("Invalid TypeArray type: " ++ @typeName(value_type)); @compileError("Invalid TypedArray type: " ++ @typeName(value_type));
},
else => {},
} }
}, },
.@"union" => return simpleZigValueToJs(isolate, std.meta.activeTag(value), fail), .@"union" => return simpleZigValueToJs(isolate, std.meta.activeTag(value), fail),