forked from nuxt/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: update
with-wasm
(nuxt#2060)
- Loading branch information
Showing
7 changed files
with
54 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
<script setup> | ||
const a = ref(100) | ||
const b = ref(250) | ||
const { data } = await useAsyncData('sum', | ||
() => $fetch('/api/sum', { params: { a: a.value, b: b.value } }) | ||
) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<input v-model="a" type="number" readonly> | ||
+ | ||
<input v-model="b" type="number" readonly> | ||
= | ||
<input v-model="data.sum" type="number" readonly> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { defineNuxtConfig } from 'nuxt3' | ||
|
||
export default defineNuxtConfig({ | ||
nitro: { | ||
experiments: { | ||
wasm: true | ||
} | ||
} | ||
}) |
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |