diff --git a/src/browser/js/Context.zig b/src/browser/js/Context.zig index ca4e83d4..de9c4849 100644 --- a/src/browser/js/Context.zig +++ b/src/browser/js/Context.zig @@ -1045,12 +1045,6 @@ pub const FinalizerCallback = struct { } }; -/// Creates a new typed array. Memory is owned by JS context. -/// 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) { - return .init(self.isolate, size); -} - // == Profiler == pub fn startCpuProfiler(self: *Context) void { if (comptime !IS_DEBUG) { diff --git a/src/browser/js/Local.zig b/src/browser/js/Local.zig index 7beab5c9..e9d9d9d9 100644 --- a/src/browser/js/Local.zig +++ b/src/browser/js/Local.zig @@ -75,6 +75,12 @@ pub fn newArray(self: *const Local, len: u32) js.Array { }; } +/// Creates a new typed array. Memory is owned by JS context. +/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Typed_arrays +pub fn createTypedArray(self: *const Local, comptime array_type: js.ArrayType, size: usize) js.ArrayBufferRef(array_type) { + return .init(self, size); +} + pub fn runMicrotasks(self: *const Local) void { self.isolate.performMicrotasksCheckpoint(); } @@ -306,12 +312,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.local(self).handle }; }, inline diff --git a/src/browser/js/js.zig b/src/browser/js/js.zig index acb58a27..112cc596 100644 --- a/src/browser/js/js.zig +++ b/src/browser/js/js.zig @@ -106,9 +106,25 @@ pub fn ArrayBufferRef(comptime kind: ArrayType) type { .float64 => f64, }; + local: *const Local, handle: *const v8.Value, - pub fn init(isolate: Isolate, size: usize) Self { + /// Persisted typed array. + pub const Global = struct { + handle: v8.Global, + + pub fn deinit(self: *Global) void { + v8.v8__Global__Reset(&self.handle); + } + + pub fn local(self: *const Global, l: *const Local) Self { + return .{ .local = l, .handle = v8.v8__Global__Get(&self.handle, l.isolate.handle).? }; + } + }; + + pub fn init(local: *const Local, size: usize) Self { + const ctx = local.ctx; + const isolate = ctx.isolate; const bits = switch (@typeInfo(BackingInt)) { .int => |n| n.bits, .float => |f| f.bits, @@ -138,7 +154,16 @@ pub fn ArrayBufferRef(comptime kind: ArrayType) type { .float64 => @ptrCast(v8.v8__Float64Array__New(array_buffer, 0, size).?), }; - return .{ .handle = handle }; + return .{ .local = local, .handle = handle }; + } + + pub fn persist(self: *const Self) !Global { + var ctx = self.local.ctx; + var global: v8.Global = undefined; + v8.v8__Global__New(ctx.isolate.handle, self.handle, &global); + try ctx.global_values.append(ctx.arena, global); + + return .{ .handle = global }; } }; } diff --git a/src/browser/webapi/ImageData.zig b/src/browser/webapi/ImageData.zig index eed265fc..05fcae6c 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 = try page.js.local.?.createTypedArray(.uint8_clamped, size).persist(), }); } @@ -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; }