Skip to content

Missing Feature: Easily compile from string in javascript in browser? #1219

@robertleeplummerjr

Description

@robertleeplummerjr

We can generate functions in javascript, it would seem you could as well create a micro-function using webassembly as well. The easiest way I've seen to do it is to read from an array buffer, and it just seemed very overly complex.

The example I've seen is this:

fetch('simple.wasm').then(response =>
  response.arrayBuffer()
).then(bytes => {
  let mod = new WebAssembly.Module(bytes);
  let instance = new WebAssembly.Instance(mod, importObject);
  instance.exports.exported_func();
})

(taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance)

Why cannot we have this as part of the spec?:

const rawMod = `
(module
  (func $add (param $n0 i32) (param $n1 i32) (result i32)
    get_local $n0
    get_local $n1
    i32.add
  )

  (export "add" (func $add))
)`;
let mod = new WebAssembly.Module(rawMod);

It is perfectly feasible to have an API that generates WASM on the fly, why isn't this a part of the spec? If it is, part of the spec, and I've misread something, can you point me to where that is?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions