Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/napi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions src/napi/wrapper/arraybuffer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
}
}
Comment on lines +249 to +257

/// 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 {
Expand Down
21 changes: 19 additions & 2 deletions src/napi/wrapper/typedarray.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
};
Expand Down Expand Up @@ -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);
Comment on lines 203 to 206
pub const Uint16Array = TypedArray(u16);
pub const Int32Array = TypedArray(i32);
Expand Down
Loading