introduce persisted typed arrays

This commit is contained in:
Halil Durak
2026-02-17 16:35:12 +03:00
parent db4a97743f
commit 2426abd17a
4 changed files with 45 additions and 13 deletions

View File

@@ -1046,8 +1046,14 @@ pub const FinalizerCallback = struct {
}; };
/// Creates a new typed array. Memory is owned by JS context. /// Creates a new typed array. Memory is owned by JS context.
/// If storing the type in a Zig type is desired, prefer `.global` state to persist.
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Typed_arrays /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Typed_arrays
pub fn createTypedArray(self: *Context, comptime array_type: js.ArrayType, size: usize) js.ArrayBufferRef(array_type) { pub fn createTypedArray(
self: *const Context,
comptime array_type: js.ArrayType,
comptime state: js.ArrayBufferState,
size: usize,
) js.ArrayBufferRef(array_type, state) {
return .init(self.isolate, size); return .init(self.isolate, size);
} }

View File

@@ -306,12 +306,13 @@ pub fn zigValueToJs(self: *const Local, value: anytype, comptime opts: CallOpts)
js.Value => return value, js.Value => return value,
js.Exception => return .{ .local = self, .handle = isolate.throwException(value.handle) }, js.Exception => return .{ .local = self, .handle = isolate.throwException(value.handle) },
js.ArrayBufferRef(.int8), js.ArrayBufferRef(.uint8), js.ArrayBufferRef(.uint8_clamped), js.ArrayBufferRef(.int8, .global), js.ArrayBufferRef(.uint8, .global),
js.ArrayBufferRef(.int16), js.ArrayBufferRef(.uint16), js.ArrayBufferRef(.uint8_clamped, .global),js.ArrayBufferRef(.int16, .global),
js.ArrayBufferRef(.int32), js.ArrayBufferRef(.uint32), js.ArrayBufferRef(.uint16, .global), js.ArrayBufferRef(.int32, .global),
js.ArrayBufferRef(.float16), js.ArrayBufferRef(.float32), js.ArrayBufferRef(.float64), js.ArrayBufferRef(.uint32, .global), js.ArrayBufferRef(.float16, .global),
js.ArrayBufferRef(.float32, .global), js.ArrayBufferRef(.float64, .global),
=> { => {
return .{ .local = self, .handle = value.handle }; return .{ .local = self, .handle = value.localHandle(self) };
}, },
inline inline

View File

@@ -90,7 +90,13 @@ pub const ArrayType = enum(u8) {
float64, float64,
}; };
pub fn ArrayBufferRef(comptime kind: ArrayType) type { pub const ArrayBufferState = enum(u1) { local, global };
/// If `state` is `global`; a persisted, global typed array is created.
pub fn ArrayBufferRef(
comptime kind: ArrayType,
comptime state: ArrayBufferState,
) type {
return struct { return struct {
const Self = @This(); const Self = @This();
@@ -106,7 +112,10 @@ pub fn ArrayBufferRef(comptime kind: ArrayType) type {
.float64 => f64, .float64 => f64,
}; };
handle: *const v8.Value, handle: switch (state) {
.local => *const v8.Value,
.global => v8.Global,
},
pub fn init(isolate: Isolate, size: usize) Self { pub fn init(isolate: Isolate, size: usize) Self {
const bits = switch (@typeInfo(BackingInt)) { const bits = switch (@typeInfo(BackingInt)) {
@@ -125,7 +134,7 @@ pub fn ArrayBufferRef(comptime kind: ArrayType) type {
array_buffer = v8.v8__ArrayBuffer__New2(isolate.handle, &backing_store_ptr).?; array_buffer = v8.v8__ArrayBuffer__New2(isolate.handle, &backing_store_ptr).?;
} }
const handle: *const v8.Value = switch (comptime kind) { const local_handle: *const v8.Value = switch (comptime kind) {
.int8 => @ptrCast(v8.v8__Int8Array__New(array_buffer, 0, size).?), .int8 => @ptrCast(v8.v8__Int8Array__New(array_buffer, 0, size).?),
.uint8 => @ptrCast(v8.v8__Uint8Array__New(array_buffer, 0, size).?), .uint8 => @ptrCast(v8.v8__Uint8Array__New(array_buffer, 0, size).?),
.uint8_clamped => @ptrCast(v8.v8__Uint8ClampedArray__New(array_buffer, 0, size).?), .uint8_clamped => @ptrCast(v8.v8__Uint8ClampedArray__New(array_buffer, 0, size).?),
@@ -138,7 +147,23 @@ pub fn ArrayBufferRef(comptime kind: ArrayType) type {
.float64 => @ptrCast(v8.v8__Float64Array__New(array_buffer, 0, size).?), .float64 => @ptrCast(v8.v8__Float64Array__New(array_buffer, 0, size).?),
}; };
return .{ .handle = handle }; switch (comptime state) {
.local => return .{ .handle = local_handle },
.global => {
// We need a global handle if state is `global`.
var global_handle: v8.Global = undefined;
v8.v8__Global__New(isolate.handle, local_handle, &global_handle);
return .{ .handle = global_handle };
},
}
}
/// Returns appropriate local handle.
pub fn localHandle(self: *const Self, scope: *const Local) *const v8.Value {
return switch (comptime state) {
.local => return self.handle,
.global => v8.v8__Global__Get(&self.handle, scope.isolate.handle).?,
};
} }
}; };
} }

View File

@@ -29,7 +29,7 @@ const Page = @import("../Page.zig");
const ImageData = @This(); const ImageData = @This();
_width: u32, _width: u32,
_height: u32, _height: u32,
_data: js.ArrayBufferRef(.uint8_clamped), _data: js.ArrayBufferRef(.uint8_clamped, .global),
pub const ConstructorSettings = struct { pub const ConstructorSettings = struct {
/// Specifies the color space of the image data. /// Specifies the color space of the image data.
@@ -74,7 +74,7 @@ pub fn constructor(
return page._factory.create(ImageData{ return page._factory.create(ImageData{
._width = width, ._width = width,
._height = height, ._height = height,
._data = page.js.createTypedArray(.uint8_clamped, size), ._data = page.js.createTypedArray(.uint8_clamped, .global, size),
}); });
} }
@@ -94,7 +94,7 @@ pub fn getColorSpace(_: *const ImageData) String {
return comptime .wrap("srgb"); return comptime .wrap("srgb");
} }
pub fn getData(self: *const ImageData) js.ArrayBufferRef(.uint8_clamped) { pub fn getData(self: *const ImageData) js.ArrayBufferRef(.uint8_clamped, .global) {
return self._data; return self._data;
} }