Skip to content

Commit d3522e0

Browse files
authored
Merge pull request #1213 from lightpanda-io/nikneym/remove-kludge
2 parents d15a384 + 5417a8d commit d3522e0

File tree

2 files changed

+105
-102
lines changed

2 files changed

+105
-102
lines changed

src/browser/js/Context.zig

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -780,55 +780,55 @@ pub fn jsValueToZig(self: *Context, comptime named_function: NamedFunction, comp
780780
// Extracted so that it can be used in both jsValueToZig and in
781781
// probeJsValueToZig. Avoids having to duplicate this logic when probing.
782782
fn jsValueToStruct(self: *Context, comptime named_function: NamedFunction, comptime T: type, js_value: v8.Value) !?T {
783-
if (T == js.Function) {
784-
if (!js_value.isFunction()) {
785-
return null;
786-
}
787-
return try self.createFunction(js_value);
788-
}
789-
790-
if (@hasDecl(T, "_TYPED_ARRAY_ID_KLUDGE")) {
791-
const VT = @typeInfo(std.meta.fieldInfo(T, .values).type).pointer.child;
792-
const arr = (try self.jsValueToTypedArray(VT, js_value)) orelse return null;
793-
return .{ .values = arr };
794-
}
795-
796-
if (T == js.String) {
797-
return .{ .string = try self.valueToString(js_value, .{ .allocator = self.arena }) };
798-
}
799-
800-
const js_obj = js_value.castTo(v8.Object);
783+
return switch (T) {
784+
js.Function => {
785+
if (!js_value.isFunction()) {
786+
return null;
787+
}
801788

802-
if (comptime T == js.Object) {
789+
return try self.createFunction(js_value);
790+
},
791+
// zig fmt: off
792+
js.TypedArray(u8), js.TypedArray(u16), js.TypedArray(u32), js.TypedArray(u64),
793+
js.TypedArray(i8), js.TypedArray(i16), js.TypedArray(i32), js.TypedArray(i64),
794+
js.TypedArray(f32), js.TypedArray(f64),
795+
// zig fmt: on
796+
=> {
797+
const ValueType = @typeInfo(std.meta.fieldInfo(T, .values).type).pointer.child;
798+
const slice = (try self.jsValueToTypedArray(ValueType, js_value)) orelse return null;
799+
return .{ .values = slice };
800+
},
801+
js.String => .{ .string = try self.valueToString(js_value, .{ .allocator = self.arena }) },
803802
// Caller wants an opaque js.Object. Probably a parameter
804-
// that it needs to pass back into a callback
805-
return js.Object{
806-
.js_obj = js_obj,
803+
// that it needs to pass back into a callback.
804+
js.Object => js.Object{
805+
.js_obj = js_value.castTo(v8.Object),
807806
.context = self,
808-
};
809-
}
810-
811-
if (!js_value.isObject()) {
812-
return null;
813-
}
814-
815-
const v8_context = self.v8_context;
816-
const isolate = self.isolate;
807+
},
808+
else => {
809+
const js_obj = js_value.castTo(v8.Object);
810+
if (!js_value.isObject()) {
811+
return null;
812+
}
817813

818-
var value: T = undefined;
819-
inline for (@typeInfo(T).@"struct".fields) |field| {
820-
const name = field.name;
821-
const key = v8.String.initUtf8(isolate, name);
822-
if (js_obj.has(v8_context, key.toValue())) {
823-
@field(value, name) = try self.jsValueToZig(named_function, field.type, try js_obj.getValue(v8_context, key));
824-
} else if (@typeInfo(field.type) == .optional) {
825-
@field(value, name) = null;
826-
} else {
827-
const dflt = field.defaultValue() orelse return null;
828-
@field(value, name) = dflt;
829-
}
830-
}
831-
return value;
814+
const v8_context = self.v8_context;
815+
const isolate = self.isolate;
816+
var value: T = undefined;
817+
inline for (@typeInfo(T).@"struct".fields) |field| {
818+
const name = field.name;
819+
const key = v8.String.initUtf8(isolate, name);
820+
if (js_obj.has(v8_context, key.toValue())) {
821+
@field(value, name) = try self.jsValueToZig(named_function, field.type, try js_obj.getValue(v8_context, key));
822+
} else if (@typeInfo(field.type) == .optional) {
823+
@field(value, name) = null;
824+
} else {
825+
const dflt = field.defaultValue() orelse return null;
826+
@field(value, name) = dflt;
827+
}
828+
}
829+
return value;
830+
},
831+
};
832832
}
833833

834834
fn jsValueToTypedArray(_: *Context, comptime T: type, js_value: v8.Value) !?[]T {

src/browser/js/js.zig

Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ const NamedFunction = Context.NamedFunction;
4848
// Env.JsObject. Want a TypedArray? Env.TypedArray.
4949
pub fn TypedArray(comptime T: type) type {
5050
return struct {
51-
pub const _TYPED_ARRAY_ID_KLUDGE = true;
52-
5351
values: []const T,
5452

5553
pub fn dupe(self: TypedArray(T), allocator: Allocator) !TypedArray(T) {
@@ -327,68 +325,73 @@ pub fn simpleZigValueToJs(isolate: v8.Isolate, value: anytype, comptime fail: bo
327325
return v8.initNull(isolate).toValue();
328326
},
329327
.@"struct" => {
330-
const T = @TypeOf(value);
331-
332-
if (T == ArrayBuffer) {
333-
const values = value.values;
334-
const len = values.len;
335-
var array_buffer: v8.ArrayBuffer = undefined;
336-
const backing_store = v8.BackingStore.init(isolate, len);
337-
const data: [*]u8 = @ptrCast(@alignCast(backing_store.getData()));
338-
@memcpy(data[0..len], @as([]const u8, @ptrCast(values))[0..len]);
339-
array_buffer = v8.ArrayBuffer.initWithBackingStore(isolate, &backing_store.toSharedPtr());
340-
341-
return .{ .handle = array_buffer.handle };
342-
}
343-
344-
if (@hasDecl(T, "_TYPED_ARRAY_ID_KLUDGE")) {
345-
const values = value.values;
346-
const value_type = @typeInfo(@TypeOf(values)).pointer.child;
347-
const len = values.len;
348-
const bits = switch (@typeInfo(value_type)) {
349-
.int => |n| n.bits,
350-
.float => |f| f.bits,
351-
else => @compileError("Invalid TypeArray type: " ++ @typeName(value_type)),
352-
};
353-
354-
var array_buffer: v8.ArrayBuffer = undefined;
355-
if (len == 0) {
356-
array_buffer = v8.ArrayBuffer.init(isolate, 0);
357-
} else {
358-
const buffer_len = len * bits / 8;
359-
const backing_store = v8.BackingStore.init(isolate, buffer_len);
328+
switch (@TypeOf(value)) {
329+
ArrayBuffer => {
330+
const values = value.values;
331+
const len = values.len;
332+
var array_buffer: v8.ArrayBuffer = undefined;
333+
const backing_store = v8.BackingStore.init(isolate, len);
360334
const data: [*]u8 = @ptrCast(@alignCast(backing_store.getData()));
361-
@memcpy(data[0..buffer_len], @as([]const u8, @ptrCast(values))[0..buffer_len]);
335+
@memcpy(data[0..len], @as([]const u8, @ptrCast(values))[0..len]);
362336
array_buffer = v8.ArrayBuffer.initWithBackingStore(isolate, &backing_store.toSharedPtr());
363-
}
364337

365-
switch (@typeInfo(value_type)) {
366-
.int => |n| switch (n.signedness) {
367-
.unsigned => switch (n.bits) {
368-
8 => return v8.Uint8Array.init(array_buffer, 0, len).toValue(),
369-
16 => return v8.Uint16Array.init(array_buffer, 0, len).toValue(),
370-
32 => return v8.Uint32Array.init(array_buffer, 0, len).toValue(),
371-
64 => return v8.BigUint64Array.init(array_buffer, 0, len).toValue(),
372-
else => {},
338+
return .{ .handle = array_buffer.handle };
339+
},
340+
// zig fmt: off
341+
TypedArray(u8), TypedArray(u16), TypedArray(u32), TypedArray(u64),
342+
TypedArray(i8), TypedArray(i16), TypedArray(i32), TypedArray(i64),
343+
TypedArray(f32), TypedArray(f64),
344+
// zig fmt: on
345+
=> {
346+
const values = value.values;
347+
const value_type = @typeInfo(@TypeOf(values)).pointer.child;
348+
const len = values.len;
349+
const bits = switch (@typeInfo(value_type)) {
350+
.int => |n| n.bits,
351+
.float => |f| f.bits,
352+
else => @compileError("Invalid TypedArray type: " ++ @typeName(value_type)),
353+
};
354+
355+
var array_buffer: v8.ArrayBuffer = undefined;
356+
if (len == 0) {
357+
array_buffer = v8.ArrayBuffer.init(isolate, 0);
358+
} else {
359+
const buffer_len = len * bits / 8;
360+
const backing_store = v8.BackingStore.init(isolate, buffer_len);
361+
const data: [*]u8 = @ptrCast(@alignCast(backing_store.getData()));
362+
@memcpy(data[0..buffer_len], @as([]const u8, @ptrCast(values))[0..buffer_len]);
363+
array_buffer = v8.ArrayBuffer.initWithBackingStore(isolate, &backing_store.toSharedPtr());
364+
}
365+
366+
switch (@typeInfo(value_type)) {
367+
.int => |n| switch (n.signedness) {
368+
.unsigned => switch (n.bits) {
369+
8 => return v8.Uint8Array.init(array_buffer, 0, len).toValue(),
370+
16 => return v8.Uint16Array.init(array_buffer, 0, len).toValue(),
371+
32 => return v8.Uint32Array.init(array_buffer, 0, len).toValue(),
372+
64 => return v8.BigUint64Array.init(array_buffer, 0, len).toValue(),
373+
else => {},
374+
},
375+
.signed => switch (n.bits) {
376+
8 => return v8.Int8Array.init(array_buffer, 0, len).toValue(),
377+
16 => return v8.Int16Array.init(array_buffer, 0, len).toValue(),
378+
32 => return v8.Int32Array.init(array_buffer, 0, len).toValue(),
379+
64 => return v8.BigInt64Array.init(array_buffer, 0, len).toValue(),
380+
else => {},
381+
},
373382
},
374-
.signed => switch (n.bits) {
375-
8 => return v8.Int8Array.init(array_buffer, 0, len).toValue(),
376-
16 => return v8.Int16Array.init(array_buffer, 0, len).toValue(),
377-
32 => return v8.Int32Array.init(array_buffer, 0, len).toValue(),
378-
64 => return v8.BigInt64Array.init(array_buffer, 0, len).toValue(),
383+
.float => |f| switch (f.bits) {
384+
32 => return v8.Float32Array.init(array_buffer, 0, len).toValue(),
385+
64 => return v8.Float64Array.init(array_buffer, 0, len).toValue(),
379386
else => {},
380387
},
381-
},
382-
.float => |f| switch (f.bits) {
383-
32 => return v8.Float32Array.init(array_buffer, 0, len).toValue(),
384-
64 => return v8.Float64Array.init(array_buffer, 0, len).toValue(),
385388
else => {},
386-
},
387-
else => {},
388-
}
389-
// We normally don't fail in this function unless fail == true
390-
// but this can never be valid.
391-
@compileError("Invalid TypeArray type: " ++ @typeName(value_type));
389+
}
390+
// We normally don't fail in this function unless fail == true
391+
// but this can never be valid.
392+
@compileError("Invalid TypedArray type: " ++ @typeName(value_type));
393+
},
394+
else => {},
392395
}
393396
},
394397
.@"union" => return simpleZigValueToJs(isolate, std.meta.activeTag(value), fail),

0 commit comments

Comments
 (0)