diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2022-11-27 05:54:28 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-27 19:24:28 +0530 |
commit | ca66978a5aadddf7895b49b44076c574e7e6a0b0 (patch) | |
tree | 65d93f524bc5fea23cef4e75b02de275833158e1 /core/examples/wasm.js | |
parent | 9ffc6acdbb3326dde74c803332547b0ae33e483a (diff) |
feat(ops): fast calls for Wasm (#16776)
This PR introduces Wasm ops. These calls are optimized for entry from
Wasm land.
The `#[op(wasm)]` attribute is opt-in.
Last parameter `Option<&mut [u8]>` is the memory slice of the Wasm
module *when entered from a Fast API call*. Otherwise, the user is
expected to implement logic to obtain the memory if `None`
```rust
#[op(wasm)]
pub fn op_args_get(
offset: i32,
buffer_offset: i32,
memory: Option<&mut [u8]>,
) {
// ...
}
```
Diffstat (limited to 'core/examples/wasm.js')
-rw-r--r-- | core/examples/wasm.js | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/core/examples/wasm.js b/core/examples/wasm.js new file mode 100644 index 000000000..69e475639 --- /dev/null +++ b/core/examples/wasm.js @@ -0,0 +1,28 @@ +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. + +// asc wasm.ts --exportStart --initialMemory 6400 -O -o wasm.wasm +// deno-fmt-ignore +const bytes = new Uint8Array([ + 0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0, 0, 2, + 15, 1, 3, 111, 112, 115, 7, 111, 112, 95, 119, 97, 115, 109, 0, + 0, 3, 3, 2, 0, 0, 5, 4, 1, 0, 128, 50, 7, 36, 4, + 7, 111, 112, 95, 119, 97, 115, 109, 0, 0, 4, 99, 97, 108, 108, + 0, 1, 6, 109, 101, 109, 111, 114, 121, 2, 0, 6, 95, 115, 116, + 97, 114, 116, 0, 2, 10, 10, 2, 4, 0, 16, 0, 11, 3, 0, + 1, 11 + ]); + +const { ops } = Deno.core; + +const module = new WebAssembly.Module(bytes); +const instance = new WebAssembly.Instance(module, { ops }); +ops.op_set_wasm_mem(instance.exports.memory); + +instance.exports.call(); + +const memory = instance.exports.memory; +const view = new Uint8Array(memory.buffer); + +if (view[0] !== 69) { + throw new Error("Expected first byte to be 69"); +} |