Skip to content

Commit 4c5df37

Browse files
committed
jsValueToZig for fixed sized arrays
1 parent 429dc4d commit 4c5df37

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
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: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,18 +331,16 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
331331

332332
fn promiseRejectCallback(v8_msg: v8.C_PromiseRejectMessage) callconv(.c) void {
333333
const msg = v8.PromiseRejectMessage.initFromC(v8_msg);
334-
const isolate = msg.getPromise().toObject().getIsolate();
334+
const isolate = msg.getPromise().toObject().getIsolate();
335335
const v8_context = isolate.getCurrentContext();
336336
const context: *JsContext = @ptrFromInt(v8_context.getEmbedderData(1).castTo(v8.BigInt).getUint64());
337337

338338
const value =
339-
if (msg.getValue()) |v8_value| valueToString(context.call_arena, v8_value, isolate, v8_context) catch |err| @errorName(err)
340-
else "no value";
339+
if (msg.getValue()) |v8_value| valueToString(context.call_arena, v8_value, isolate, v8_context) catch |err| @errorName(err) else "no value";
341340

342-
log.debug(.js, "unhandled rejection", .{.value =value});
341+
log.debug(.js, "unhandled rejection", .{ .value = value });
343342
}
344343

345-
346344
// ExecutionWorld closely models a JS World.
347345
// https://chromium.googlesource.com/chromium/src/+/master/third_party/blink/renderer/bindings/core/v8/V8BindingDesign.md#World
348346
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/ExecutionWorld
@@ -1110,6 +1108,19 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
11101108
},
11111109
else => {},
11121110
},
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+
},
11131124
.@"struct" => {
11141125
return try (self.jsValueToStruct(named_function, T, js_value)) orelse {
11151126
return error.InvalidArgument;
@@ -1393,6 +1404,36 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
13931404
},
13941405
else => {},
13951406
},
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+
},
13961437
.@"struct" => {
13971438
// We don't want to duplicate the code for this, so we call
13981439
// the actual conversion function.

0 commit comments

Comments
 (0)