Skip to content

Commit

Permalink
examples(js stdout node): add example using new api, switch wasm modu…
Browse files Browse the repository at this point in the history
…le used in example
  • Loading branch information
G4Vi committed Jul 31, 2024
1 parent f1aa7ee commit e97a62d
Showing 1 changed file with 53 additions and 20 deletions.
73 changes: 53 additions & 20 deletions js/packages/observe-sdk-stdout/test/node/index.js
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);
}

0 comments on commit e97a62d

Please sign in to comment.