Skip to content
Merged
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
51 changes: 51 additions & 0 deletions examples/basic/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,54 @@ export declare function get_arraybuffer(arraybuffer: ArrayBuffer): number;
export declare function get_arraybuffer_as_string(
arraybuffer: ArrayBuffer
): string;

// ============== TypedArray Functions ==============

/**
* Creates a Uint8Array with 4 bytes: [1, 2, 3, 4]
* @returns The created typed array
*/
export declare function create_uint8_typedarray(): Uint8Array;

/**
* Gets the length of a Uint8Array
* @param array - The typed array
* @returns The element count
*/
export declare function get_uint8_typedarray_length(array: Uint8Array): number;

/**
* Sums all items in a Float32Array
* @param array - The typed array
* @returns The sum of all elements
*/
export declare function sum_float32_typedarray(array: Float32Array): number;

// ============== DataView Functions ==============

/**
* Creates a DataView with 4 bytes initialized to 0x78, 0x56, 0x34, 0x12
* @returns The created data view
*/
export declare function create_dataview(): DataView;

/**
* Gets the byte length of a DataView
* @param view - The data view
* @returns The byte length
*/
export declare function get_dataview_length(view: DataView): number;

/**
* Gets the first byte of a DataView
* @param view - The data view
* @returns The first byte, or 0 if empty
*/
export declare function get_dataview_first_byte(view: DataView): number;

/**
* Reads the first 4 bytes of a DataView as a little-endian uint32
* @param view - The data view
* @returns The uint32 value
*/
export declare function get_dataview_uint32_le(view: DataView): number;
22 changes: 22 additions & 0 deletions examples/basic/src/dataview.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const napi = @import("napi");

pub fn create_dataview(env: napi.Env) !napi.DataView {
var view = try napi.DataView.New(env, 4);
try view.setUint32(0, 0x12345678, true);
return view;
}

pub fn get_dataview_length(view: napi.DataView) usize {
return view.byteLength();
}

pub fn get_dataview_first_byte(view: napi.DataView) u8 {
if (view.byteLength() == 0) {
return 0;
}
return view.asConstSlice()[0];
}

pub fn get_dataview_uint32_le(view: napi.DataView) !u32 {
return try view.getUint32(0, true);
}
11 changes: 11 additions & 0 deletions examples/basic/src/hello.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const class = @import("class.zig");
const log = @import("log/log.zig");
const buffer = @import("buffer.zig");
const arraybuffer = @import("arraybuffer.zig");
const typedarray = @import("typedarray.zig");
const dataview = @import("dataview.zig");

pub const test_i32 = number.test_i32;
pub const test_f32 = number.test_f32;
Expand Down Expand Up @@ -56,6 +58,15 @@ pub const create_arraybuffer = arraybuffer.create_arraybuffer;
pub const get_arraybuffer = arraybuffer.get_arraybuffer;
pub const get_arraybuffer_as_string = arraybuffer.get_arraybuffer_as_string;

pub const create_uint8_typedarray = typedarray.create_uint8_typedarray;
pub const get_uint8_typedarray_length = typedarray.get_uint8_typedarray_length;
pub const sum_float32_typedarray = typedarray.sum_float32_typedarray;

pub const create_dataview = dataview.create_dataview;
pub const get_dataview_length = dataview.get_dataview_length;
pub const get_dataview_first_byte = dataview.get_dataview_first_byte;
pub const get_dataview_uint32_le = dataview.get_dataview_uint32_le;

comptime {
napi.NODE_API_MODULE("hello", @This());
}
17 changes: 17 additions & 0 deletions examples/basic/src/typedarray.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const napi = @import("napi");

pub fn create_uint8_typedarray(env: napi.Env) !napi.Uint8Array {
return napi.Uint8Array.copy(env, &[_]u8{ 1, 2, 3, 4 });
}

pub fn get_uint8_typedarray_length(array: napi.Uint8Array) usize {
return array.length();
}

pub fn sum_float32_typedarray(array: napi.Float32Array) f32 {
var sum: f32 = 0;
for (array.asConstSlice()) |item| {
sum += item;
}
return sum;
}
53 changes: 52 additions & 1 deletion harmony_example/entry/src/main/cpp/types/libhello/Index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,55 @@ export declare function get_buffer(buffer: ArrayBuffer): number;
* @param buffer - The buffer
* @returns The buffer as a string
*/
export declare function get_buffer_as_string(buffer: ArrayBuffer): string;
export declare function get_buffer_as_string(buffer: ArrayBuffer): string;

// ============== TypedArray Functions ==============

/**
* Creates a Uint8Array with 4 bytes: [1, 2, 3, 4]
* @returns The created typed array
*/
export declare function create_uint8_typedarray(): Uint8Array;

/**
* Gets the length of a Uint8Array
* @param array - The typed array
* @returns The element count
*/
export declare function get_uint8_typedarray_length(array: Uint8Array): number;

/**
* Sums all items in a Float32Array
* @param array - The typed array
* @returns The sum of all elements
*/
export declare function sum_float32_typedarray(array: Float32Array): number;

// ============== DataView Functions ==============

/**
* Creates a DataView with 4 bytes initialized to 0x78, 0x56, 0x34, 0x12
* @returns The created data view
*/
export declare function create_dataview(): DataView;

/**
* Gets the byte length of a DataView
* @param view - The data view
* @returns The byte length
*/
export declare function get_dataview_length(view: DataView): number;

/**
* Gets the first byte of a DataView
* @param view - The data view
* @returns The first byte, or 0 if empty
*/
export declare function get_dataview_first_byte(view: DataView): number;

/**
* Reads the first 4 bytes of a DataView as a little-endian uint32
* @param view - The data view
* @returns The uint32 value
*/
export declare function get_dataview_uint32_le(view: DataView): number;
14 changes: 14 additions & 0 deletions src/napi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const thread_safe_function = @import("./napi/wrapper/thread_safe_function.zig");
const class = @import("./napi/wrapper/class.zig");
const buffer = @import("./napi/wrapper/buffer.zig");
const arraybuffer = @import("./napi/wrapper/arraybuffer.zig");
const typedarray = @import("./napi/wrapper/typedarray.zig");
const dataview = @import("./napi/wrapper/dataview.zig");

pub const napi_sys = @import("napi-sys");
pub const Env = env.Env;
Expand Down Expand Up @@ -36,6 +38,18 @@ pub const Class = class.Class;
pub const ClassWithoutInit = class.ClassWithoutInit;
pub const Buffer = buffer.Buffer;
pub const ArrayBuffer = arraybuffer.ArrayBuffer;
pub const TypedArray = typedarray.TypedArray;
pub const Int8Array = typedarray.Int8Array;
pub const Uint8Array = typedarray.Uint8Array;
pub const Int16Array = typedarray.Int16Array;
pub const Uint16Array = typedarray.Uint16Array;
pub const Int32Array = typedarray.Int32Array;
pub const Uint32Array = typedarray.Uint32Array;
pub const Float32Array = typedarray.Float32Array;
pub const Float64Array = typedarray.Float64Array;
pub const BigInt64Array = typedarray.BigInt64Array;
pub const BigUint64Array = typedarray.BigUint64Array;
pub const DataView = dataview.DataView;

pub const NODE_API_MODULE = module.NODE_API_MODULE;
pub const NODE_API_MODULE_WITH_INIT = module.NODE_API_MODULE_WITH_INIT;
8 changes: 8 additions & 0 deletions src/napi/util/helper.zig
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ pub fn isThreadSafeFunction(comptime T: type) bool {
return false;
}

pub fn isTypedArray(comptime T: type) bool {
return @hasDecl(T, "is_napi_typedarray");
}

pub fn isDataView(comptime T: type) bool {
return @hasDecl(T, "is_napi_dataview");
}

pub fn isArrayList(comptime T: type) bool {
const info = @typeInfo(T);
if (info != .@"struct") {
Expand Down
17 changes: 15 additions & 2 deletions src/napi/util/napi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ const ThreadSafeFunction = @import("../wrapper/thread_safe_function.zig").Thread
const class = @import("../wrapper/class.zig");
const Buffer = @import("../wrapper/buffer.zig").Buffer;
const ArrayBuffer = @import("../wrapper/arraybuffer.zig").ArrayBuffer;
const DataView = @import("../wrapper/dataview.zig").DataView;

pub const Napi = struct {
pub fn from_napi_value(env: napi.napi_env, raw: napi.napi_value, comptime T: type) T {
const infos = @typeInfo(T);
switch (T) {
NapiValue.BigInt, NapiValue.Number, NapiValue.String, NapiValue.Object, NapiValue.Promise, NapiValue.Array, NapiValue.Undefined, NapiValue.Null, Buffer, ArrayBuffer => {
NapiValue.BigInt, NapiValue.Number, NapiValue.String, NapiValue.Object, NapiValue.Promise, NapiValue.Array, NapiValue.Undefined, NapiValue.Null, Buffer, ArrayBuffer, DataView => {
return T.from_raw(env, raw);
},
else => {
Expand Down Expand Up @@ -91,6 +92,12 @@ pub const Napi = struct {
}
return Function(args_type, return_type).from_raw(env, raw);
}
if (comptime helper.isTypedArray(T)) {
return T.from_raw(env, raw);
}
if (comptime helper.isDataView(T)) {
return T.from_raw(env, raw);
}

if (comptime helper.isTuple(T)) {
return NapiValue.Array.from_napi_value(env, raw, T);
Expand Down Expand Up @@ -136,7 +143,7 @@ pub const Napi = struct {
const infos = @typeInfo(value_type);

switch (value_type) {
NapiValue.BigInt, NapiValue.Bool, NapiValue.Number, NapiValue.String, NapiValue.Object, NapiValue.Promise, NapiValue.Array, NapiValue.Undefined, NapiValue.Null, Buffer, ArrayBuffer => {
NapiValue.BigInt, NapiValue.Bool, NapiValue.Number, NapiValue.String, NapiValue.Object, NapiValue.Promise, NapiValue.Array, NapiValue.Undefined, NapiValue.Null, Buffer, ArrayBuffer, DataView => {
return value.raw;
},
// If value is already a napi_value, return it directly
Expand Down Expand Up @@ -194,9 +201,15 @@ pub const Napi = struct {
if (comptime helper.isNapiFunction(value_type)) {
return value.raw;
}
if (comptime helper.isTypedArray(value_type)) {
return value.raw;
}
if (comptime helper.isThreadSafeFunction(value_type)) {
@compileError("ThreadSafeFunction is not supported for to_napi_value");
}
if (comptime helper.isDataView(value_type)) {
return value.raw;
}
if (comptime helper.isTuple(value_type)) {
const array = try NapiValue.Array.New(Env.from_raw(env), value);
return array.raw;
Expand Down
Loading