Skip to content
Next Next commit
lib: expose setupInstance method on WASI class
  • Loading branch information
toyobayashi committed Jun 21, 2025
commit bd80e2d6c389f17c1036550ea444a5a25066c7cc
31 changes: 20 additions & 11 deletions lib/wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
const {
ERR_INVALID_ARG_VALUE,
ERR_WASI_ALREADY_STARTED,
ERR_INVALID_ARG_TYPE,
} = require('internal/errors').codes;
const {
emitExperimentalWarning,
Expand All @@ -35,13 +36,6 @@ const kBindingName = Symbol('kBindingName');
emitExperimentalWarning('WASI');


function setupInstance(self, instance) {
validateObject(instance, 'instance');
validateObject(instance.exports, 'instance.exports');

self[kInstance] = instance;
self[kSetMemory](instance.exports.memory);
}

class WASI {
constructor(options = kEmptyObject) {
Expand Down Expand Up @@ -118,14 +112,30 @@ class WASI {
this[kInstance] = undefined;
}

setupInstance(instance, memory) {
Comment thread
toyobayashi marked this conversation as resolved.
Resolved
validateObject(instance, 'instance');
validateObject(instance.exports, 'instance.exports');
Comment thread
toyobayashi marked this conversation as resolved.
Resolved

if (memory != null) {
if (!(memory instanceof WebAssembly.Memory)) {
throw new ERR_INVALID_ARG_TYPE('memory', 'WebAssembly.Memory', memory);
}
this[kSetMemory](memory);
} else {
this[kSetMemory](instance.exports.memory);
}

this[kInstance] = instance;
this[kStarted] = true;
}

// Must not export _initialize, must export _start
start(instance) {
if (this[kStarted]) {
throw new ERR_WASI_ALREADY_STARTED();
}
this[kStarted] = true;

setupInstance(this, instance);
this.setupInstance(instance);

const { _start, _initialize } = this[kInstance].exports;

Expand All @@ -148,9 +158,8 @@ class WASI {
if (this[kStarted]) {
throw new ERR_WASI_ALREADY_STARTED();
}
this[kStarted] = true;

setupInstance(this, instance);
this.setupInstance(instance);

const { _start, _initialize } = this[kInstance].exports;

Expand Down