Skip to content

Commit 4be7fa1

Browse files
committed
support Blob.arrayBuffer
Also adds support for `ArrayBuffer` to js.zig.
1 parent 5785c14 commit 4be7fa1

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/browser/file/Blob.zig

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,13 @@ fn writeBlobParts(
139139
}
140140
}
141141

142-
// TODO: Blob.arrayBuffer.
143-
// https://developer.mozilla.org/en-US/docs/Web/API/Blob/arrayBuffer
142+
/// Returns a Promise that resolves with the contents of the blob
143+
/// as binary data contained in an ArrayBuffer.
144+
pub fn _arrayBuffer(self: *const Blob, page: *Page) !js.Promise {
145+
const resolver = page.js.createPromiseResolver(.none);
146+
try resolver.resolve(js.ArrayBuffer{ .values = self.slice });
147+
return resolver.promise();
148+
}
144149

145150
/// Returns a ReadableStream which upon reading returns the data
146151
/// contained within the Blob.

src/browser/js/js.zig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ pub fn TypedArray(comptime T: type) type {
5858
};
5959
}
6060

61+
pub const ArrayBuffer = struct {
62+
values: []const u8,
63+
};
64+
6165
pub const PromiseResolver = struct {
6266
context: *Context,
6367
resolver: v8.PromiseResolver,
@@ -324,6 +328,19 @@ pub fn simpleZigValueToJs(isolate: v8.Isolate, value: anytype, comptime fail: bo
324328
},
325329
.@"struct" => {
326330
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+
327344
if (@hasDecl(T, "_TYPED_ARRAY_ID_KLUDGE")) {
328345
const values = value.values;
329346
const value_type = @typeInfo(@TypeOf(values)).pointer.child;

0 commit comments

Comments
 (0)