Skip to main content

Interface: NativePointer

Opaque pointer object. Stores a native void* with full precision. Null pointers are represented as JavaScript null.

Properties

value

readonly value: bigint

The raw address as a bigint. Paired with createPointer, which rebuilds a pointer from this value: createPointer(p.value) reproduces p exactly.

This is the supported way to move a pointer to a Worker. A NativePointer is not structured-cloneable, so you cannot postMessage it directly; send p.value (a bigint, which clones by value) and call createPointer() on the other side. It works because txiki.js Workers are threads of the same process and share one address space — the address is valid on both sides.

The address is a plain number: nothing keeps the memory it refers to alive, and nothing makes the target C API safe to call from another thread. The sender must ensure the memory outlives every use, and that touching it off-thread is actually allowed by that library (many — e.g. SDL rendering — are single-threaded or main-thread-only).

Methods

equals()

equals(other): boolean

Returns true if both pointers refer to the same address.

Parameters

other

NativePointer | null

Returns

boolean


offset()

offset(n): NativePointer

Returns a new pointer offset by n bytes.

Parameters

n

number

Returns

NativePointer


toArrayBuffer()

toArrayBuffer(byteLength, byteOffset?): ExternalArrayBuffer

Like NativePointer.toUint8Array, but returns a zero-copy ExternalArrayBuffer. The same lifetime caveats apply.

Parameters

byteLength

number

byteOffset?

number

Returns

ExternalArrayBuffer


toString()

toString(): string

Returns hex string representation, e.g. "0x7fff5a2b3c00".

Returns

string


toUint8Array()

toUint8Array(byteLength, byteOffset?): Uint8Array

Returns a zero-copy Uint8Array of byteLength bytes that aliases the native memory starting at this pointer (plus an optional byteOffset). No data is copied: reads and writes go straight to the underlying memory. Its .buffer is an ExternalArrayBuffer, so the view can be invalidated with view.buffer.detach().

The view does not keep the memory alive and the runtime never frees it. The caller is responsible for ensuring the memory outlives every view over it; accessing a view after the memory has been freed, moved or reallocated is undefined behaviour and can crash the process.

Parameters

byteLength

number

byteOffset?

number

Returns

Uint8Array