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
14 changes: 9 additions & 5 deletions src/AsyncContext.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,17 @@ pub fn destroy(self: AsyncContext) NapiError!void {
);
}

/// `args` must be a tuple containing only `napi.Value` objects.
/// https://nodejs.org/api/n-api.html#napi_make_callback
pub fn makeCallback(self: AsyncContext, recv: Value, func: Value, args: anytype) NapiError!Value {
var argv = argsTupleToRaw(args);
pub fn makeCallbackRaw(self: AsyncContext, recv: Value, func: Value, args: []const c.napi_value) NapiError!Value {
var result: c.napi_value = undefined;
try status.check(
c.napi_make_callback(
self.env,
self.async_context,
recv.value,
func.value,
argv.len,
if (argv.len > 0) &argv else null,
args.len,
if (args.len > 0) args.ptr else null,
&result,
),
);
Expand All @@ -47,6 +45,12 @@ pub fn makeCallback(self: AsyncContext, recv: Value, func: Value, args: anytype)
};
}

/// `args` must be a tuple containing only `napi.Value` objects.
pub fn makeCallback(self: AsyncContext, recv: Value, func: Value, args: anytype) NapiError!Value {
var argv = argsTupleToRaw(args);
return try self.makeCallbackRaw(recv, func, argv[0..]);
}

pub const CallbackScope = struct {
env: c.napi_env,
scope: c.napi_callback_scope,
Expand Down
28 changes: 18 additions & 10 deletions src/Env.zig
Original file line number Diff line number Diff line change
Expand Up @@ -638,18 +638,16 @@ pub fn getUndefined(self: Env) NapiError!Value {
//// https://nodejs.org/api/n-api.html#working-with-javascript-functions

/// Calls a JavaScript function.
/// `args` must be a tuple containing only `napi.Value` objects.
/// https://nodejs.org/api/n-api.html#napi_call_function
pub fn callFunction(self: Env, function: Value, recv: Value, args: anytype) NapiError!Value {
var argv = argsTupleToRaw(args);
pub fn callFunctionRaw(self: Env, function: Value, recv: Value, args: []const c.napi_value) NapiError!Value {
var result: c.napi_value = undefined;
try status.check(
c.napi_call_function(
self.env,
recv.value,
function.value,
argv.len,
if (argv.len > 0) &argv else null,
args.len,
if (args.len > 0) args.ptr else null,
&result,
),
);
Expand All @@ -659,6 +657,12 @@ pub fn callFunction(self: Env, function: Value, recv: Value, args: anytype) Napi
};
}

/// `args` must be a tuple containing only `napi.Value` objects.
pub fn callFunction(self: Env, function: Value, recv: Value, args: anytype) NapiError!Value {
var argv = argsTupleToRaw(args);
return try self.callFunctionRaw(function, recv, argv[0..]);
}

/// https://nodejs.org/api/n-api.html#napi_create_function
pub fn createFunction(self: Env, utf8_name: []const u8, comptime argc: usize, comptime cb: Callback(argc), data: ?*anyopaque) NapiError!Value {
var value: c.napi_value = undefined;
Expand All @@ -684,17 +688,15 @@ pub fn getNewTarget(self: Env, cb_info: c.napi_callback_info) NapiError!Value {
};
}

/// `args` must be a tuple containing only `napi.Value` objects.
/// https://nodejs.org/api/n-api.html#napi_new_instance
pub fn newInstance(self: Env, constructor: Value, args: anytype) NapiError!Value {
var argv = argsTupleToRaw(args);
pub fn newInstanceRaw(self: Env, constructor: Value, args: []const c.napi_value) NapiError!Value {
var instance: c.napi_value = undefined;
try status.check(
c.napi_new_instance(
self.env,
constructor.value,
argv.len,
if (argv.len > 0) &argv else null,
args.len,
if (args.len > 0) args.ptr else null,
&instance,
),
);
Expand All @@ -704,6 +706,12 @@ pub fn newInstance(self: Env, constructor: Value, args: anytype) NapiError!Value
};
}

/// `args` must be a tuple containing only `napi.Value` objects.
pub fn newInstance(self: Env, constructor: Value, args: anytype) NapiError!Value {
var argv = argsTupleToRaw(args);
return try self.newInstanceRaw(constructor, argv[0..]);
}

//// Object wrap
//// https://nodejs.org/api/n-api.html#object-wrap

Expand Down