diff --git a/examples/with-wasm/app.vue b/examples/with-wasm/app.vue new file mode 100644 index 0000000000..bf6076269e --- /dev/null +++ b/examples/with-wasm/app.vue @@ -0,0 +1,19 @@ + + + + diff --git a/examples/with-wasm/nuxt.config.ts b/examples/with-wasm/nuxt.config.ts new file mode 100644 index 0000000000..e5636a1234 --- /dev/null +++ b/examples/with-wasm/nuxt.config.ts @@ -0,0 +1,9 @@ +import { defineNuxtConfig } from 'nuxt3' + +export default defineNuxtConfig({ + nitro: { + experiments: { + wasm: true + } + } +}) diff --git a/examples/with-wasm/server/api/hello.ts b/examples/with-wasm/server/api/hello.ts deleted file mode 100644 index 02cc0e4b2f..0000000000 --- a/examples/with-wasm/server/api/hello.ts +++ /dev/null @@ -1,9 +0,0 @@ -import sample from './sample.wasm' - -export default async () => { - const { instance } = await sample({}) - - return { - result: instance.exports.main() - } -} diff --git a/examples/with-wasm/server/api/sample.wasm b/examples/with-wasm/server/api/sample.wasm deleted file mode 100644 index a956defa3a..0000000000 Binary files a/examples/with-wasm/server/api/sample.wasm and /dev/null differ diff --git a/examples/with-wasm/server/api/sum.ts b/examples/with-wasm/server/api/sum.ts new file mode 100644 index 0000000000..062cd3aaf0 --- /dev/null +++ b/examples/with-wasm/server/api/sum.ts @@ -0,0 +1,19 @@ +import { useQuery, lazyHandle } from 'h3' + +export default lazyHandle(async () => { + const { exports: { sum } } = await loadWasmInstance( + // @ts-ignore + () => import('~/server/wasm/sum.wasm') + ) + + return (req) => { + const { a = 0, b = 0 } = useQuery(req) + return { sum: sum(a, b) } + } +}) + +async function loadWasmInstance (importFn, imports = {}) { + const init = await importFn().then(m => m.default || m) + const { instance } = await init(imports) + return instance +} diff --git a/examples/with-wasm/server/wasm/sum.wasm b/examples/with-wasm/server/wasm/sum.wasm new file mode 100755 index 0000000000..7267db6252 Binary files /dev/null and b/examples/with-wasm/server/wasm/sum.wasm differ diff --git a/examples/with-wasm/server/wasm/sum.wat b/examples/with-wasm/server/wasm/sum.wat new file mode 100644 index 0000000000..71d6e2859e --- /dev/null +++ b/examples/with-wasm/server/wasm/sum.wat @@ -0,0 +1,7 @@ +;; https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_text_format +;; https://webassembly.github.io/wabt/demo/wat2wasm/ +(module + (func (export "sum") (param i32 i32) (result i32) + local.get 0 + local.get 1 + i32.add))