From 07c7a204621e9300f72e463b2ea8336cedf9c400 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Sun, 21 Nov 2021 16:58:47 +0100 Subject: [PATCH] examples: update `with-wasm` (#2060) --- examples/with-wasm/app.vue | 19 +++++++++++++++++++ examples/with-wasm/nuxt.config.ts | 9 +++++++++ examples/with-wasm/server/api/hello.ts | 9 --------- examples/with-wasm/server/api/sample.wasm | Bin 37 -> 0 bytes examples/with-wasm/server/api/sum.ts | 19 +++++++++++++++++++ examples/with-wasm/server/wasm/sum.wasm | Bin 0 -> 57 bytes examples/with-wasm/server/wasm/sum.wat | 7 +++++++ 7 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 examples/with-wasm/app.vue create mode 100644 examples/with-wasm/nuxt.config.ts delete mode 100644 examples/with-wasm/server/api/hello.ts delete mode 100644 examples/with-wasm/server/api/sample.wasm create mode 100644 examples/with-wasm/server/api/sum.ts create mode 100755 examples/with-wasm/server/wasm/sum.wasm create mode 100644 examples/with-wasm/server/wasm/sum.wat 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 a956defa3a00013db5a3cbb85db6e120f646f5dd..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 37 scmZQbEY4+QU|?WmWlUgTtY>CoWMJoDWXVm;%wu5SVq;`saAf8N09oY(O#lD@ 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 0000000000000000000000000000000000000000..7267db6252285d7a5282ddc866780b552b749625 GIT binary patch literal 57 zcmWN{F$#b%5Jb^G8(~nvGvsKduo2cKSnt*K)sHb#fUGmBR4s(9Hs6G8mFaZdJIC^j Jyuu>>&Ijpb2Pyyn literal 0 HcmV?d00001 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))