@thi.ng/wasm-api
Version:
Generic, modular, extensible API bridge and infrastructure for hybrid JS & WebAssembly projects
49 lines (48 loc) • 932 B
JavaScript
class Pointer {
constructor(mem, base, fn) {
this.mem = mem;
this.base = base;
this.fn = fn;
}
get addr() {
this.mem.ensureMemory();
return this.mem.u32[this.base >>> 2];
}
set addr(addr) {
this.mem.ensureMemory();
this.mem.u32[this.base >>> 2] = addr;
}
get isNull() {
return this.addr === 0;
}
deref() {
const addr = this.addr;
return addr ? this.fn(addr) : void 0;
}
}
class Pointer64 {
constructor(mem, base, fn) {
this.mem = mem;
this.base = base;
this.fn = fn;
}
get addr() {
this.mem.ensureMemory();
return this.mem.u64[Number(this.base >> BigInt(3))];
}
set addr(addr) {
this.mem.ensureMemory();
this.mem.u64[Number(this.base >> BigInt(3))] = addr;
}
get isNull() {
return this.addr === 0n;
}
deref() {
const addr = this.addr;
return addr ? this.fn(addr) : void 0;
}
}
export {
Pointer,
Pointer64
};