mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-21 21:25:11 +00:00
examples: update with-wasm
(#2060)
This commit is contained in:
parent
583679f9c8
commit
07c7a20462
19
examples/with-wasm/app.vue
Normal file
19
examples/with-wasm/app.vue
Normal file
@ -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>
|
9
examples/with-wasm/nuxt.config.ts
Normal file
9
examples/with-wasm/nuxt.config.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { defineNuxtConfig } from 'nuxt3'
|
||||
|
||||
export default defineNuxtConfig({
|
||||
nitro: {
|
||||
experiments: {
|
||||
wasm: true
|
||||
}
|
||||
}
|
||||
})
|
@ -1,9 +0,0 @@
|
||||
import sample from './sample.wasm'
|
||||
|
||||
export default async () => {
|
||||
const { instance } = await sample({})
|
||||
|
||||
return {
|
||||
result: instance.exports.main()
|
||||
}
|
||||
}
|
Binary file not shown.
19
examples/with-wasm/server/api/sum.ts
Normal file
19
examples/with-wasm/server/api/sum.ts
Normal file
@ -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
|
||||
}
|
BIN
examples/with-wasm/server/wasm/sum.wasm
Executable file
BIN
examples/with-wasm/server/wasm/sum.wasm
Executable file
Binary file not shown.
7
examples/with-wasm/server/wasm/sum.wat
Normal file
7
examples/with-wasm/server/wasm/sum.wat
Normal file
@ -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))
|
Loading…
Reference in New Issue
Block a user