Skip to content

Commit

Permalink
fix: support input_offset() (#48)
Browse files Browse the repository at this point in the history
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 #47
  • Loading branch information
chrisdickinson authored Jan 8, 2024
1 parent 85b5022 commit 25e17a4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/call-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down
11 changes: 11 additions & 0 deletions src/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Binary file added wasm/input_offset.wasm
Binary file not shown.

0 comments on commit 25e17a4

Please sign in to comment.