From 2426abd17a327b29c80220bf886bcf8d5e4418b4 Mon Sep 17 00:00:00 2001 From: Halil Durak Date: Tue, 17 Feb 2026 16:35:12 +0300 Subject: [PATCH] introduce persisted typed arrays --- src/browser/js/Context.zig | 8 +++++++- src/browser/js/Local.zig | 11 ++++++----- src/browser/js/js.zig | 33 ++++++++++++++++++++++++++++---- src/browser/webapi/ImageData.zig | 6 +++--- 4 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/browser/js/Context.zig b/src/browser/js/Context.zig index ca4e83d4..7a8ef1f5 100644 --- a/src/browser/js/Context.zig +++ b/src/browser/js/Context.zig @@ -1046,8 +1046,14 @@ pub const FinalizerCallback = struct { }; /// 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 -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); } diff --git a/src/browser/js/Local.zig b/src/browser/js/Local.zig index 7beab5c9..156c2214 100644 --- a/src/browser/js/Local.zig +++ b/src/browser/js/Local.zig @@ -306,12 +306,13 @@ pub fn zigValueToJs(self: *const Local, value: anytype, comptime opts: CallOpts) js.Value => return value, js.Exception => return .{ .local = self, .handle = isolate.throwException(value.handle) }, - js.ArrayBufferRef(.int8), js.ArrayBufferRef(.uint8), js.ArrayBufferRef(.uint8_clamped), - js.ArrayBufferRef(.int16), js.ArrayBufferRef(.uint16), - js.ArrayBufferRef(.int32), js.ArrayBufferRef(.uint32), - js.ArrayBufferRef(.float16), js.ArrayBufferRef(.float32), js.ArrayBufferRef(.float64), + js.ArrayBufferRef(.int8, .global), js.ArrayBufferRef(.uint8, .global), + js.ArrayBufferRef(.uint8_clamped, .global),js.ArrayBufferRef(.int16, .global), + js.ArrayBufferRef(.uint16, .global), js.ArrayBufferRef(.int32, .global), + 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 diff --git a/src/browser/js/js.zig b/src/browser/js/js.zig index acb58a27..252d0a3d 100644 --- a/src/browser/js/js.zig +++ b/src/browser/js/js.zig @@ -90,7 +90,13 @@ pub const ArrayType = enum(u8) { 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 { const Self = @This(); @@ -106,7 +112,10 @@ pub fn ArrayBufferRef(comptime kind: ArrayType) type { .float64 => f64, }; - handle: *const v8.Value, + handle: switch (state) { + .local => *const v8.Value, + .global => v8.Global, + }, pub fn init(isolate: Isolate, size: usize) Self { 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).?; } - 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).?), .uint8 => @ptrCast(v8.v8__Uint8Array__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).?), }; - 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).?, + }; } }; } diff --git a/src/browser/webapi/ImageData.zig b/src/browser/webapi/ImageData.zig index eed265fc..153130e5 100644 --- a/src/browser/webapi/ImageData.zig +++ b/src/browser/webapi/ImageData.zig @@ -29,7 +29,7 @@ const Page = @import("../Page.zig"); const ImageData = @This(); _width: u32, _height: u32, -_data: js.ArrayBufferRef(.uint8_clamped), +_data: js.ArrayBufferRef(.uint8_clamped, .global), pub const ConstructorSettings = struct { /// Specifies the color space of the image data. @@ -74,7 +74,7 @@ pub fn constructor( return page._factory.create(ImageData{ ._width = width, ._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"); } -pub fn getData(self: *const ImageData) js.ArrayBufferRef(.uint8_clamped) { +pub fn getData(self: *const ImageData) js.ArrayBufferRef(.uint8_clamped, .global) { return self._data; }