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
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"oxc.enable": false,
"typescript.validate.enable": false
}
124 changes: 52 additions & 72 deletions examples/basic/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@
* @param right - The second integer
* @returns The sum of left and right
*/
export function test_i32(left: number, right: number): number;
export declare function test_i32(left: number, right: number): number;

/**
* Adds two 32-bit floating-point numbers
* @param left - The first float
* @param right - The second float
* @returns The sum of left and right
*/
export function test_f32(left: number, right: number): number;
export declare function test_f32(left: number, right: number): number;

/**
* Adds two 32-bit unsigned integers
* @param left - The first unsigned integer
* @param right - The second unsigned integer
* @returns The sum of left and right
*/
export function test_u32(left: number, right: number): number;
export declare function test_u32(left: number, right: number): number;

// ============== String Functions ==============

Expand All @@ -36,35 +36,35 @@ export function test_u32(left: number, right: number): number;
* @param name - The name to greet
* @returns A greeting string "Hello, {name}!"
*/
export function hello(name: string): string;
export declare function hello(name: string): string;

/**
* A constant text string
*/
export const text: string;
export declare const text: string;

// ============== Error Functions ==============

/**
* Throws a test error
* @throws {Error} Always throws an error with reason "test"
*/
export function throw_error(): void;
export declare function throw_error(): void;

// ============== Worker Functions ==============

/**
* Calculates fibonacci number asynchronously (fire and forget)
* @param n - The fibonacci index
*/
export function fib(n: number): void;
export declare function fib(n: number): void;

/**
* Calculates fibonacci number asynchronously with Promise
* @param n - The fibonacci index
* @returns A Promise that resolves when calculation is complete
*/
export function fib_async(n: number): Promise<void>;
export declare function fib_async(n: number): Promise<void>;

// ============== Array Functions ==============

Expand All @@ -73,21 +73,23 @@ export function fib_async(n: number): Promise<void>;
* @param array - An array of numbers
* @returns The same array
*/
export function get_and_return_array(array: number[]): number[];
export declare function get_and_return_array(array: number[]): number[];

/**
* Takes a tuple array and returns it
* @param array - A tuple of [number, boolean, string]
* @returns The same tuple
*/
export function get_named_array(array: [number, boolean, string]): [number, boolean, string];
export declare function get_named_array(
array: [number, boolean, string]
): [number, boolean, string];

/**
* Takes an ArrayList and returns it
* @param array - An array of numbers
* @returns The same array
*/
export function get_arraylist(array: number[]): number[];
export declare function get_arraylist(array: number[]): number[];

// ============== Object Types ==============

Expand Down Expand Up @@ -123,34 +125,40 @@ export interface NullableField {
* @param config - Object with name, age, and is_student
* @returns The same object
*/
export function get_object(config: FullField): FullField;
export declare function get_object(config: FullField): FullField;

/**
* Takes an object with optional fields
* @param config - Object with name (required), age and is_student (optional)
* @returns Object with default values applied (age: 18, is_student: true)
*/
export function get_object_optional(config: OptionalField): OptionalField;
export declare function get_object_optional(
config: OptionalField
): OptionalField;

/**
* Takes an optional object and returns it
* @param config - Object with optional fields
* @returns The same object
*/
export function get_optional_object_and_return_optional(config: OptionalField): OptionalField;
export declare function get_optional_object_and_return_optional(
config: OptionalField
): OptionalField;

/**
* Takes an object with nullable name field
* @param config - Object with nullable name
* @returns The same object
*/
export function get_nullable_object(config: NullableField): NullableField;
export declare function get_nullable_object(
config: NullableField
): NullableField;

/**
* Returns a nullable object with null name
* @returns Object with name set to null
*/
export function return_nullable(): NullableField;
export declare function return_nullable(): NullableField;

// ============== Function Types ==============

Expand All @@ -166,36 +174,36 @@ export type CallbackFunction = (arg0: number, arg1: number) => number;
* @param cb - A callback function that takes two numbers and returns a number
* @returns The result of calling cb(1, 2)
*/
export function call_function(cb: CallbackFunction): number;
export declare function call_function(cb: CallbackFunction): number;

/**
* Adds two numbers
* @param left - The first number
* @param right - The second number
* @returns The sum of left and right
*/
export function basic_function(left: number, right: number): number;
export declare function basic_function(left: number, right: number): number;

/**
* Creates a new function that wraps basic_function
* @returns A function that adds two numbers
*/
export function create_function(): CallbackFunction;
export declare function create_function(): CallbackFunction;

// ============== Thread Safe Function ==============

/**
* Calls the thread safe function from multiple threads
* @param tsfn - A thread-safe callback function
*/
export function call_thread_safe_function(tsfn: CallbackFunction): void;
export declare function call_thread_safe_function(tsfn: CallbackFunction): void;

// ============== Class Types ==============

/**
* Basic test class with name and age properties
*/
export class TestClass {
export declare class TestClass {
constructor(name: string, age: number);
name: string;
age: number;
Expand All @@ -205,7 +213,7 @@ export class TestClass {
* Test class with custom init function
* Constructor takes (age, name) instead of field order
*/
export class TestWithInitClass {
export declare class TestWithInitClass {
constructor(age: number, name: string);
name: string;
age: number;
Expand All @@ -215,7 +223,7 @@ export class TestWithInitClass {
/**
* Test class without constructor (abstract-like)
*/
export class TestWithoutInitClass {
export declare class TestWithoutInitClass {
private constructor();
name: string;
age: number;
Expand All @@ -225,7 +233,7 @@ export class TestWithoutInitClass {
/**
* Test class with factory method
*/
export class TestFactoryClass {
export declare class TestFactoryClass {
constructor(age: number, name: string);
name: string;
age: number;
Expand All @@ -241,55 +249,27 @@ export class TestFactoryClass {
/**
* Tests hilog functionality (OpenHarmony logging)
*/
export function test_hilog(): void;
export declare function test_hilog(): void;

// ============== Module Export ==============
// ============== Buffer Functions ==============

declare const hello: {
// Number
test_i32: typeof test_i32;
test_f32: typeof test_f32;
test_u32: typeof test_u32;

// String
hello: typeof hello;
text: typeof text;

// Error
throw_error: typeof throw_error;

// Worker
fib: typeof fib;
fib_async: typeof fib_async;

// Array
get_and_return_array: typeof get_and_return_array;
get_named_array: typeof get_named_array;
get_arraylist: typeof get_arraylist;

// Object
get_object: typeof get_object;
get_object_optional: typeof get_object_optional;
get_optional_object_and_return_optional: typeof get_optional_object_and_return_optional;
get_nullable_object: typeof get_nullable_object;
return_nullable: typeof return_nullable;

// Function
call_function: typeof call_function;
basic_function: typeof basic_function;
create_function: typeof create_function;

// Thread Safe Function
call_thread_safe_function: typeof call_thread_safe_function;

// Class
TestClass: typeof TestClass;
TestWithInitClass: typeof TestWithInitClass;
TestWithoutInitClass: typeof TestWithoutInitClass;
TestFactoryClass: typeof TestFactoryClass;
/**
* Creates a new buffer
* @param size - The size of the buffer
* @returns The new buffer
*/
export declare function create_buffer(): ArrayBuffer;

// Log
test_hilog: typeof test_hilog;
};
/**
* Gets the buffer length
* @param buffer - The buffer
* @returns The buffer length
*/
export declare function get_buffer(buffer: ArrayBuffer): number;

export default hello;
/**
* Gets the buffer as a string
* @param buffer - The buffer
* @returns The buffer as a string
*/
export declare function get_buffer_as_string(buffer: ArrayBuffer): string;
13 changes: 13 additions & 0 deletions examples/basic/src/buffer.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const napi = @import("napi");

pub fn create_buffer(env: napi.Env) !napi.Buffer {
return napi.Buffer.New(env, 1024);
}

pub fn get_buffer(buf: napi.Buffer) !usize {
return buf.length();
}

pub fn get_buffer_as_string(buf: napi.Buffer) ![]u8 {
return buf.asSlice();
}
5 changes: 5 additions & 0 deletions examples/basic/src/hello.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const function = @import("function.zig");
const thread_safe_function = @import("thread_safe_function.zig");
const class = @import("class.zig");
const log = @import("log/log.zig");
const buffer = @import("buffer.zig");

pub const test_i32 = number.test_i32;
pub const test_f32 = number.test_f32;
Expand Down Expand Up @@ -46,6 +47,10 @@ pub const TestFactoryClass = class.TestFactoryClass;

pub const test_hilog = log.test_hilog;

pub const create_buffer = buffer.create_buffer;
pub const get_buffer = buffer.get_buffer;
pub const get_buffer_as_string = buffer.get_buffer_as_string;

comptime {
napi.NODE_API_MODULE("hello", @This());
}
Loading