Env

Contents

The N-API environment plus the per-call arena allocator.

const napi = @import("napi");

pub fn handler(env: napi.Env) !napi.Val {
    const obj = try env.createObject();
    try obj.setNamedProperty(env, "ok", try env.toJs(true));
    return obj;
}

Env is recognized by type. As the first parameter of a top-level function, the second parameter of an init, or the second parameter of a class method, it does not consume a JS argument.

Allocation

MethodReturnsPurpose
allocator()std.mem.AllocatorPer-call arena allocator. Freed when the function returns.

See Memory model.

Conversion

MethodReturnsPurpose
toJs(value)!napi.ValConvert any convertible Zig type to a JS value.

Primitives

MethodReturnsJS result
createBoolean(v: bool)!napi.ValBoolean
createInt32(v: i32)!napi.ValNumber
createUint32(v: u32)!napi.ValNumber
createInt64(v: i64)!napi.ValNumber
createFloat64(v: f64)!napi.ValNumber
createBigintInt64(v: i64)!napi.ValBigInt
createBigintUint64(v: u64)!napi.ValBigInt
createString(s: []const u8)!napi.ValString

Singletons

MethodReturnsJS result
createNull()!napi.Valnull
createUndefined()!napi.Valundefined
getGlobal()!napi.ValglobalThis

Containers

MethodReturnsPurpose
createObject()!napi.ValEmpty object.
createArray()!napi.ValEmpty array.
createArrayWithLength(len: u32)!napi.ValArray pre-sized to len.
createSymbol(description: ?napi.Val)!napi.ValSymbol with optional description.
createDate(time_ms: f64)!napi.ValDate from epoch milliseconds.

Buffers

MethodReturnsPurpose
createArrayBuffer(len: usize)!ArrayBufferReturns { .val, .data }. data is []u8 into the backing memory.
createBuffer(len: usize)!ArrayBufferSame shape, but a Node.js Buffer.
createTypedArray(typ, len, ab, offset)!napi.ValTypedArray view over an existing ArrayBuffer.
createExternalArrayBuffer(ptr, len, finalize, hint)!napi.ValArrayBuffer over externally-owned memory.
pub const ArrayBuffer = struct {
    val: napi.Val,
    data: []u8,
};

External handles

MethodReturnsPurpose
createExternal(ptr, finalize, hint)!napi.ValWrap an opaque Zig pointer.

Pair with Val.getExternalData(env) to unwrap.

Functions and references

MethodReturnsPurpose
createFunction(name, callback, data)!napi.ValNative-backed JS function.
createReference(val: napi.Val)!napi.RefStrong GC reference (prevents collection).

Promises and async

MethodReturnsPurpose
createPromise()!PromiseReturns { .promise, .deferred }.
runWorker(name, context)!napi.ValBackground work, returns a JS Promise.
pub const Promise = struct {
    promise: napi.Val,
    deferred: napi.Deferred,
};

See Workers and Promises.

Errors

MethodReturnsPurpose
throwError(msg)voidThrow a JS Error.
throwTypeError(msg)voidThrow a JS TypeError.
throwRangeError(msg)voidThrow a JS RangeError.
throwValue(val: napi.Val)!voidThrow an existing JS value.
createError(message)!napi.ValConstruct a JS Error without throwing it.
isExceptionPending()boolWhether an exception is currently pending.

After throw*, return any error from your function to abort. See Errors.

Version info

MethodReturnsPurpose
getVersion()!u32N-API version supported by Node.
getNodeVersion()!*const c.napi_node_versionNode version info.