Skip to content

Commit 0d1ba15

Browse files
sjorsdonkersmookums
authored andcommitted
jsValueToZig for fixed sized arrays
1 parent 3cc3367 commit 0d1ba15

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
@@ -1108,6 +1108,19 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
11081108
},
11091109
else => {},
11101110
},
1111+
.array => |arr| {
1112+
// Retrieve fixed-size array as slice then copy it
1113+
const slice_type = []arr.child;
1114+
const slice_value = try self.jsValueToZig(named_function, slice_type, js_value);
1115+
if (slice_value.len != arr.len) {
1116+
// Exact length match, we could allow smaller arrays, but we would not be able to communicate how many were written
1117+
return error.InvalidArgument;
1118+
}
1119+
1120+
var result: T = undefined;
1121+
@memcpy(&result, slice_value[0..arr.len]);
1122+
return result;
1123+
},
11111124
.@"struct" => {
11121125
return try (self.jsValueToStruct(named_function, T, js_value)) orelse {
11131126
return error.InvalidArgument;
@@ -1391,6 +1404,36 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
13911404
},
13921405
else => {},
13931406
},
1407+
.array => |arr| {
1408+
// Retrieve fixed-size array as slice then probe
1409+
const slice_type = []arr.child;
1410+
switch (try self.probeJsValueToZig(named_function, slice_type, js_value)) {
1411+
.value => |slice_value| {
1412+
if (slice_value.len == arr.len) {
1413+
return .{ .ok = {} };
1414+
}
1415+
return .{ .invalid = {} };
1416+
},
1417+
.ok => {
1418+
// Exact length match, we could allow smaller arrays as .compatible, but we would not be able to communicate how many were written
1419+
if (js_value.isArray()) {
1420+
const js_arr = js_value.castTo(v8.Array);
1421+
if (js_arr.length() == arr.len) {
1422+
return .{ .ok = {} };
1423+
}
1424+
} else if (js_value.isString() and arr.child == u8) {
1425+
const str = try valueToString(self.call_arena, js_value, self.isolate, self.v8_context);
1426+
if (str.len == arr.len) {
1427+
return .{ .ok = {} };
1428+
}
1429+
}
1430+
return .{ .invalid = {} };
1431+
},
1432+
.compatible => return .{ .compatible = {} },
1433+
.coerce => return .{ .coerce = {} },
1434+
.invalid => return .{ .invalid = {} },
1435+
}
1436+
},
13941437
.@"struct" => {
13951438
// We don't want to duplicate the code for this, so we call
13961439
// the actual conversion function.

0 commit comments

Comments
 (0)