From d69c77a0cf6f3a9df9973f90fa81d2233aca33cf Mon Sep 17 00:00:00 2001 From: richerfu Date: Thu, 28 May 2026 09:48:57 +0800 Subject: [PATCH] Add ArrayBuffer detach and Uint8ClampedArray wrappers --- src/napi.zig | 1 + src/napi/wrapper/arraybuffer.zig | 23 +++++++++++++++++++++++ src/napi/wrapper/typedarray.zig | 21 +++++++++++++++++++-- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/napi.zig b/src/napi.zig index dd83795..f2a0d9b 100644 --- a/src/napi.zig +++ b/src/napi.zig @@ -59,6 +59,7 @@ pub const ArrayBuffer = arraybuffer.ArrayBuffer; pub const TypedArray = typedarray.TypedArray; pub const Int8Array = typedarray.Int8Array; pub const Uint8Array = typedarray.Uint8Array; +pub const Uint8ClampedArray = typedarray.Uint8ClampedArray; pub const Int16Array = typedarray.Int16Array; pub const Uint16Array = typedarray.Uint16Array; pub const Int32Array = typedarray.Int32Array; diff --git a/src/napi/wrapper/arraybuffer.zig b/src/napi/wrapper/arraybuffer.zig index 3a63f9c..95fe24a 100644 --- a/src/napi/wrapper/arraybuffer.zig +++ b/src/napi/wrapper/arraybuffer.zig @@ -3,6 +3,7 @@ const napi = @import("napi-sys").napi_sys; const Env = @import("../env.zig").Env; const NapiError = @import("error.zig"); const GlobalAllocator = @import("../util/allocator.zig"); +const options = @import("../options.zig"); pub const ArrayBuffer = struct { env: napi.napi_env, @@ -244,6 +245,28 @@ pub const ArrayBuffer = struct { pub fn length(self: ArrayBuffer) usize { return self.len; } + + /// Detach the ArrayBuffer. + pub fn detach(self: ArrayBuffer) !void { + comptime options.requireNapiVersion(.v7); + + const status = napi.napi_detach_arraybuffer(self.env, self.raw); + if (status != napi.napi_ok) { + return NapiError.Error.fromStatus(NapiError.Status.New(status)); + } + } + + /// Check whether the ArrayBuffer has been detached. + pub fn isDetached(self: ArrayBuffer) !bool { + comptime options.requireNapiVersion(.v7); + + var result = false; + const status = napi.napi_is_detached_arraybuffer(self.env, self.raw, &result); + if (status != napi.napi_ok) { + return NapiError.Error.fromStatus(NapiError.Status.New(status)); + } + return result; + } }; const ArrayBufferCreateStatus = struct { diff --git a/src/napi/wrapper/typedarray.zig b/src/napi/wrapper/typedarray.zig index 0d46d30..5c0cba9 100644 --- a/src/napi/wrapper/typedarray.zig +++ b/src/napi/wrapper/typedarray.zig @@ -68,11 +68,27 @@ fn validateElementType(comptime T: type) void { } pub fn TypedArray(comptime T: type) type { + return TypedArrayWithRawType(T, defaultTypeFor(T)); +} + +fn validateRawTypeForElementType(comptime T: type, comptime raw_type: napi.napi_typedarray_type) void { + if (raw_type == defaultTypeFor(T)) { + return; + } + if (T == u8 and raw_type == napi.napi_uint8_clamped_array) { + return; + } + @compileError("Unsupported TypedArray raw type for element type: " ++ @typeName(T)); +} + +fn TypedArrayWithRawType(comptime T: type, comptime raw_type: napi.napi_typedarray_type) type { validateElementType(T); + validateRawTypeForElementType(T, raw_type); return struct { pub const is_napi_typedarray = true; pub const element_type = T; + pub const raw_typedarray_type = raw_type; env: napi.napi_env, raw: napi.napi_value, @@ -128,7 +144,7 @@ pub fn TypedArray(comptime T: type) type { var raw: napi.napi_value = undefined; const status = napi.napi_create_typedarray( env.raw, - defaultTypeFor(T), + raw_type, len, arraybuffer.raw, byte_offset, @@ -144,7 +160,7 @@ pub fn TypedArray(comptime T: type) type { .raw = raw, .data = if (len == 0) &[_]T{} else @ptrCast(@alignCast(arraybuffer.data + byte_offset)), .len = len, - .typedarray_type = defaultTypeFor(T), + .typedarray_type = raw_type, .byte_offset = byte_offset, .arraybuffer = arraybuffer, }; @@ -186,6 +202,7 @@ pub fn TypedArray(comptime T: type) type { pub const Int8Array = TypedArray(i8); pub const Uint8Array = TypedArray(u8); +pub const Uint8ClampedArray = TypedArrayWithRawType(u8, napi.napi_uint8_clamped_array); pub const Int16Array = TypedArray(i16); pub const Uint16Array = TypedArray(u16); pub const Int32Array = TypedArray(i32);