From fa644567834f3e1ca8fe7e24f6cc4638bc7c1005 Mon Sep 17 00:00:00 2001 From: Chris Dickinson Date: Mon, 8 Jan 2024 12:20:17 -0800 Subject: [PATCH] fix: support input_offset() Add support for fetching the Extism address of the input block. Notably, the input block _may_ be null, in which case the function returns `0n`. Fixes https://github.com/extism/js-sdk/issues/47 --- src/call-context.ts | 5 +++++ src/mod.test.ts | 11 +++++++++++ wasm/input_offset.wasm | Bin 0 -> 318 bytes 3 files changed, 16 insertions(+) create mode 100644 wasm/input_offset.wasm diff --git a/src/call-context.ts b/src/call-context.ts index d8e0b54..d3be7e0 100644 --- a/src/call-context.ts +++ b/src/call-context.ts @@ -199,6 +199,11 @@ export class CallContext { block?.view.setBigUint64(Number(offset), n, true); }, + input_offset: () => { + const blockIdx = this.#stack[this.#stack.length - 1][0]; + return Block.indexToAddress(blockIdx || 0); + }, + input_length: () => { return BigInt(this.#input?.byteLength ?? 0); }, diff --git a/src/mod.test.ts b/src/mod.test.ts index cdc26da..32665f6 100644 --- a/src/mod.test.ts +++ b/src/mod.test.ts @@ -505,6 +505,17 @@ if (typeof WebAssembly === 'undefined') { } }); + test('plugin can call input_offset', async () => { + const plugin = await createPlugin('http://localhost:8124/wasm/input_offset.wasm'); + try { + const input = 'hello world'; + const hw = await plugin.call('input_offset_length', input); + assert.equal(hw?.getBigUint64(0, true), input.length); + } finally { + await plugin.close(); + } + }); + test('plugin can fail gracefully', async () => { const plugin = await createPlugin('http://localhost:8124/wasm/fail.wasm'); try { diff --git a/wasm/input_offset.wasm b/wasm/input_offset.wasm new file mode 100644 index 0000000000000000000000000000000000000000..163adce509d225ba6de9e1d80ba0563d0a256e66 GIT binary patch literal 318 zcmZY4Jqp4=5Cz~jv)QOoFkmMZ@dP$%VW*v^h(XpL$Yv$G5pCAqQyWoGV>2*3hW8CJ zs|f%Nf(^)T$k&6tN4?$nPO`5q?r8PmV62;{;@mqHr^=;fx3kIt%{a_ev3CbhGw4`n zN|jdgDY%)*EYHpM|0Xh(dzL+$STG4GYHKUuP%CZ9D`W7jN#ETv8qtu*uiCK~BSDOq kVop%+!4h4pgCf(4W)eTo(A&-*s(8tQF9>%@62h140m`vjSO5S3 literal 0 HcmV?d00001