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 @@ -25,6 +25,7 @@ pub const NapiVersion = options.NapiVersion;
pub const selectedNapiVersion = options.selectedNapiVersion;
pub const experimentalEnabled = options.experimentalEnabled;
pub const Env = env.Env;
pub const NapiValue = value.NapiValue;
pub const Object = value.Object;
pub const Number = value.Number;
pub const String = value.String;
Expand Down
56 changes: 56 additions & 0 deletions src/napi/env.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const napi = @import("napi-sys").napi_sys;
const Undefined = @import("./value/undefined.zig").Undefined;
const Null = @import("./value/null.zig").Null;
const Object = @import("./value/object.zig").Object;
const String = @import("./value/string.zig").String;
const NapiValue = @import("./value.zig").NapiValue;
const NapiError = @import("./wrapper/error.zig");
const native_wrap = @import("./wrapper/native_wrap.zig");
const options = @import("./options.zig");

pub const Env = struct {
raw: napi.napi_env,
Expand All @@ -24,6 +29,57 @@ pub const Env = struct {
return Null.from_raw(self.raw, result);
}

pub fn getNapiVersion(self: Env) u32 {
var result: u32 = 0;
_ = napi.napi_get_version(self.raw, &result);
return result;
}

pub fn getGlobal(self: Env) !Object {
var result: napi.napi_value = undefined;
const status = napi.napi_get_global(self.raw, &result);
if (status != napi.napi_ok) {
return NapiError.Error.fromStatus(NapiError.Status.New(status));
}
return Object.from_raw(self.raw, result);
}

pub fn createSymbol(self: Env, description: []const u8) !NapiValue {
const description_value = String.New(self, description);
var result: napi.napi_value = undefined;
const status = napi.napi_create_symbol(self.raw, description_value.raw, &result);
if (status != napi.napi_ok) {
return NapiError.Error.fromStatus(NapiError.Status.New(status));
}
return NapiValue.from_raw(self.raw, result);
}

pub fn createDate(self: Env, value: f64) !Object {
comptime options.requireNapiVersion(.v5);

var result: napi.napi_value = undefined;
const status = napi.napi_create_date(self.raw, value, &result);
if (status != napi.napi_ok) {
return NapiError.Error.fromStatus(NapiError.Status.New(status));
}
return Object.from_raw(self.raw, result);
}

pub fn isExceptionPending(self: Env) bool {
var result = false;
_ = napi.napi_is_exception_pending(self.raw, &result);
return result;
}

pub fn getAndClearLastException(self: Env) !NapiValue {
var result: napi.napi_value = undefined;
const status = napi.napi_get_and_clear_last_exception(self.raw, &result);
if (status != napi.napi_ok) {
return NapiError.Error.fromStatus(NapiError.Status.New(status));
}
return NapiValue.from_raw(self.raw, result);
}

pub fn wrap(self: Env, js_object: anytype, payload: anytype) !void {
return self.wrapWithSizeHint(js_object, payload, 0);
}
Expand Down
6 changes: 4 additions & 2 deletions src/napi/util/napi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ fn valueMatchesType(env: napi.napi_env, raw: napi.napi_value, comptime T: type)
switch (T) {
NapiValue.Number => return napiTypeOf(env, raw) == napi.napi_number,
NapiValue.String => return napiTypeOf(env, raw) == napi.napi_string,
NapiValue.NapiValue => return true,
NapiValue.BigInt => {
comptime options.requireNapiVersion(.v6);
return napiTypeOf(env, raw) == napi.napi_bigint;
Expand Down Expand Up @@ -432,6 +433,7 @@ pub const Napi = struct {
helper.isReference(T) or
helper.isExternal(T) or
helper.isAbortSignal(T) or
T == NapiValue.NapiValue or
T == NapiValue.BigInt or
T == NapiValue.Bool or
T == NapiValue.Number or
Expand Down Expand Up @@ -479,7 +481,7 @@ 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, DataView => {
NapiValue.NapiValue, 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 @@ -644,7 +646,7 @@ pub const Napi = struct {
}

switch (value_type) {
NapiValue.BigInt, NapiValue.Bool, NapiValue.Number, NapiValue.String, NapiValue.Object, NapiValue.Promise, NapiValue.Array, NapiValue.Undefined, NapiValue.Null, Buffer, ArrayBuffer, DataView => {
NapiValue.NapiValue, 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
Loading