Merge pull request #1571 from lightpanda-io/nikneym/persisted-typed-arrays

Persisted typed arrays
This commit is contained in:
Karl Seguin
2026-02-19 15:46:10 +08:00
committed by GitHub
4 changed files with 42 additions and 16 deletions

View File

@@ -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 == // == Profiler ==
pub fn startCpuProfiler(self: *Context) void { pub fn startCpuProfiler(self: *Context) void {
if (comptime !IS_DEBUG) { if (comptime !IS_DEBUG) {

View File

@@ -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 { pub fn runMicrotasks(self: *const Local) void {
self.isolate.performMicrotasksCheckpoint(); self.isolate.performMicrotasksCheckpoint();
} }
@@ -306,12 +312,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.local(self).handle };
}, },
inline inline

View File

@@ -106,9 +106,25 @@ pub fn ArrayBufferRef(comptime kind: ArrayType) type {
.float64 => f64, .float64 => f64,
}; };
local: *const Local,
handle: *const v8.Value, 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)) { const bits = switch (@typeInfo(BackingInt)) {
.int => |n| n.bits, .int => |n| n.bits,
.float => |f| f.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).?), .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 };
} }
}; };
} }

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 = try page.js.local.?.createTypedArray(.uint8_clamped, size).persist(),
}); });
} }
@@ -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;
} }