-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples(js stdout node): add example using new api, switch wasm modu…
…le used in example
- Loading branch information
Showing
1 changed file
with
53 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,61 @@ | ||
const fs = require("fs"); | ||
const { WASI } = require("wasi"); | ||
const { StdOutAdapter } = require("@dylibso/observe-sdk-stdout"); | ||
//const { StdOutAdapter } = require("../../dist/cjs/index.js"); | ||
const { env, argv } = require('node:process') | ||
|
||
const wasi = new WASI({ | ||
version: 'preview1', | ||
args: argv.slice(1), | ||
env, | ||
}); | ||
const bytes = fs.readFileSync("../../../observe-api/test/cxx_guest_2.wasm"); | ||
|
||
const adapter = new StdOutAdapter(); | ||
const bytes = fs.readFileSync("../../test-data/test.c.instr.wasm"); | ||
adapter.start(bytes, { | ||
spanFilter: { | ||
minDurationMicroseconds: 0, | ||
} | ||
}).then((traceContext) => { | ||
const module = new WebAssembly.Module(bytes); | ||
// old (verbose) api | ||
{ | ||
const wasi = new WASI({ | ||
version: 'preview1', | ||
args: argv.slice(1), | ||
env, | ||
}); | ||
const adapter = new StdOutAdapter(); | ||
adapter.start(bytes, { | ||
spanFilter: { | ||
minDurationMicroseconds: 0, | ||
} | ||
}).then((traceContext) => { | ||
const module = new WebAssembly.Module(bytes); | ||
|
||
WebAssembly.instantiate(module, { | ||
...wasi.getImportObject(), | ||
...traceContext.getImportObject(), | ||
}).then((instance) => { | ||
traceContext.initSpanEnter(instance.exports.memory.buffer); | ||
wasi.start(instance); | ||
traceContext.stop(); | ||
}); | ||
}); | ||
} | ||
|
||
WebAssembly.instantiate(module, { | ||
...wasi.getImportObject(), | ||
...traceContext.getImportObject(), | ||
}).then((instance) => { | ||
wasi.start(instance); | ||
traceContext.stop(); | ||
// new API using instantiateWasm | ||
// SetTimeout as temporary hack as demangling multiple modules at the same time fails: | ||
// TypeError: wasm.__wbindgen_add_to_stack_pointer is not a function | ||
{ | ||
const wasi = new WASI({ | ||
version: 'preview1', | ||
args: argv.slice(1), | ||
env, | ||
}); | ||
}); | ||
const adapter = new StdOutAdapter(); | ||
setTimeout(() => { | ||
adapter.start(bytes, { | ||
spanFilter: { | ||
minDurationMicroseconds: 0, | ||
}, | ||
instantiateWasm: async (module, traceContext) => { | ||
return WebAssembly.instantiate(module, { | ||
...wasi.getImportObject(), | ||
...traceContext.getImportObject(), | ||
}); | ||
} | ||
}).then(({ collector: traceContext, instance }) => { | ||
wasi.start(instance); | ||
traceContext.stop() | ||
}); | ||
}, 5000); | ||
} |