Skip to content

Commit 4bb1c49

Browse files
sjorsdonkersmookums
authored andcommitted
jsValueToZig for fixed sized arrays
1 parent 426cfd6 commit 4bb1c49

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/browser/fetch/Headers.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ headers: HeaderHashMap = .empty,
6262
// 3. Another Headers object.
6363
pub const HeadersInit = union(enum) {
6464
// List of Pairs of []const u8
65-
strings: []const []const []const u8,
65+
strings: []const [2][]const u8,
6666
headers: *Headers,
6767
};
6868

src/runtime/js.zig

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,19 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
11301130
},
11311131
else => {},
11321132
},
1133+
.array => |arr| {
1134+
// Retrieve fixed-size array as slice then copy it
1135+
const slice_type = []arr.child;
1136+
const slice_value = try self.jsValueToZig(named_function, slice_type, js_value);
1137+
if (slice_value.len != arr.len) {
1138+
// Exact length match, we could allow smaller arrays, but we would not be able to communicate how many were written
1139+
return error.InvalidArgument;
1140+
}
1141+
1142+
var result: T = undefined;
1143+
@memcpy(&result, slice_value[0..arr.len]);
1144+
return result;
1145+
},
11331146
.@"struct" => {
11341147
return try (self.jsValueToStruct(named_function, T, js_value)) orelse {
11351148
return error.InvalidArgument;
@@ -1413,6 +1426,36 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
14131426
},
14141427
else => {},
14151428
},
1429+
.array => |arr| {
1430+
// Retrieve fixed-size array as slice then probe
1431+
const slice_type = []arr.child;
1432+
switch (try self.probeJsValueToZig(named_function, slice_type, js_value)) {
1433+
.value => |slice_value| {
1434+
if (slice_value.len == arr.len) {
1435+
return .{ .ok = {} };
1436+
}
1437+
return .{ .invalid = {} };
1438+
},
1439+
.ok => {
1440+
// Exact length match, we could allow smaller arrays as .compatible, but we would not be able to communicate how many were written
1441+
if (js_value.isArray()) {
1442+
const js_arr = js_value.castTo(v8.Array);
1443+
if (js_arr.length() == arr.len) {
1444+
return .{ .ok = {} };
1445+
}
1446+
} else if (js_value.isString() and arr.child == u8) {
1447+
const str = try valueToString(self.call_arena, js_value, self.isolate, self.v8_context);
1448+
if (str.len == arr.len) {
1449+
return .{ .ok = {} };
1450+
}
1451+
}
1452+
return .{ .invalid = {} };
1453+
},
1454+
.compatible => return .{ .compatible = {} },
1455+
.coerce => return .{ .coerce = {} },
1456+
.invalid => return .{ .invalid = {} },
1457+
}
1458+
},
14161459
.@"struct" => {
14171460
// We don't want to duplicate the code for this, so we call
14181461
// the actual conversion function.

0 commit comments

Comments
 (0)