This commit is contained in:
Nicolas Lepage 2020-06-14 15:34:58 +02:00
parent b25c20e8f5
commit 21d93ddbbb
No known key found for this signature in database
GPG Key ID: B0879E35E66D8F6F
2 changed files with 6 additions and 6 deletions

View File

@ -1,5 +1,5 @@
window.wasmhttp = {
register: async (wasm, { scope, base = '', swUrl = 'sw.js', argv = [] } = {}) => {
register: async (wasm, { scope, base = '', swUrl = 'sw.js', args = [] } = {}) => {
const options = {}
if (scope) options.scope = scope
//FIXME register once (beware of changing scope ?)
@ -9,7 +9,7 @@ window.wasmhttp = {
type: 'wasmhttp.register',
wasm,
base,
argv,
args,
})
}
}

8
sw.js
View File

@ -1,13 +1,13 @@
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.14.3/misc/wasm/wasm_exec.js')
// FIXME use an options object with { env, argv }
const startWasm = async (wasm, WASMHTTP_HANDLER_ID, WASMHTTP_PATH, argv) => {
const startWasm = async (wasm, WASMHTTP_HANDLER_ID, WASMHTTP_PATH, args) => {
const go = new Go()
go.env = {
WASMHTTP_HANDLER_ID,
WASMHTTP_PATH,
}
go.argv = argv
go.argv = [wasm, ...args]
const { instance } = await WebAssembly.instantiateStreaming(fetch(wasm), go.importObject)
return go.run(instance)
}
@ -52,7 +52,7 @@ addEventListener('message', async ({ data }) => {
if (data.type !== 'wasmhttp.register') return
const { wasm, base, argv } = data
const { wasm, base, args } = data
let path = new URL(registration.scope).pathname
if (base && base !== '') path = `${trimEnd(path, '/')}/${trimStart(base, '/')}`
@ -63,7 +63,7 @@ addEventListener('message', async ({ data }) => {
const handlerId = `${nextHandlerId++}`
const handler = new Promise(resolve => handlerResolvers[handlerId] = resolve)
startWasm(wasm, handlerId, path, argv)
startWasm(wasm, handlerId, path, args)
running.add(key)
// FIXME try catch