Val

A handle to a JS value, valid only within the current call.

pub fn show(env: napi.Env, value: napi.Val) !napi.Val {
    if (try value.isArray(env)) {
        const len = try value.getArrayLength(env);
        return env.toJs(len);
    }
    return env.toJs("not an array");
}

Val is extern struct so a []const Val is castable to [*]const napi_value for direct N-API calls.

Conversion

MethodReturnsPurpose
to(env: Env, comptime T: type)!TConvert to any supported Zig type.

Type checks

MethodReturnsPurpose
typeOf(env)!napi.c.napi_valuetype.string, .number, .object, .function, etc.
strictEquals(env, other)!boolJS ===.
isArray(env)!bool
isArrayBuffer(env)!bool
isBuffer(env)!bool
isTypedArray(env)!bool
isDate(env)!bool
isPromise(env)!bool
hasNamedProperty(env, key: [:0]const u8)!boolProperty existence check.

Property access

MethodReturnsPurpose
getProperty(env, key: napi.Val)!napi.ValDynamic-key get.
setProperty(env, key, value)!voidDynamic-key set.
getNamedProperty(env, key: [:0]const u8)!napi.ValCompile-time-key get (faster, common case).
setNamedProperty(env, key, value)!voidCompile-time-key set.

Array access

MethodReturnsPurpose
getElement(env, index: u32)!napi.ValIndex get.
setElement(env, index: u32, value)!voidIndex set.
getArrayLength(env)!u32Array length.

String access

MethodReturnsPurpose
getStringLength(env)!usizeUTF-8 byte length of a JS string. Does not allocate. Probe-only.

BigInt access

The auto-conversion path for i54..i64 and u54..u64 throws a RangeError when the source BigInt does not fit. To inspect the lossless flag yourself, take a napi.Val and use these methods:

MethodReturnsPurpose
getBigIntI64(env)!BigIntFit(i64){ value: i64, lossless: bool }. lossless = false on overflow or sign mismatch.
getBigIntU64(env)!BigIntFit(u64){ value: u64, lossless: bool }. lossless = false if negative or > u64::max.
pub fn clamp(env: napi.Env, v: napi.Val) !i64 {
    const r = try v.getBigIntI64(env);
    if (r.lossless) return r.value;
    return if (r.value > 0) std.math.maxInt(i64) else std.math.minInt(i64);
}

Buffer access

MethodReturnsPurpose
getArrayBufferData(env)![]u8Slice into ArrayBuffer's backing memory.
getBufferData(env)![]u8Slice into Node.js Buffer's backing memory.

External and Date

MethodReturnsPurpose
getExternalData(env)!?*anyopaqueUnwrap an external pointer.
getDateValue(env)!f64Date as epoch milliseconds.