examples: update with-wasm (#2060)

This commit is contained in:
pooya parsa 2021-11-21 16:58:47 +01:00 committed by GitHub
parent 583679f9c8
commit 07c7a20462
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 54 additions and 9 deletions

View 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>

View File

@ -0,0 +1,9 @@
import { defineNuxtConfig } from 'nuxt3'
export default defineNuxtConfig({
nitro: {
experiments: {
wasm: true
}
}
})

View File

@ -1,9 +0,0 @@
import sample from './sample.wasm'
export default async () => {
const { instance } = await sample({})
return {
result: instance.exports.main()
}
}

View 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
}

Binary file not shown.

View 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))