Design: WAT syntax as a valid JavaScript subset

Created on 16 Mar 2019  ·  4Comments  ·  Source: WebAssembly/design

To permit comma separated S-expressions within the text format to allow the WAT syntax to be a valid parse-time JavaScript subset syntax like JSON is to JavaScript.

This would allow you to write the following example:

(func $swap (param i32 i32) (result i32 i32) (get_local 1) (get_local 0))

As the following:

(func, $swap, (param, i32, i32), (result, i32, i32), (get_local, 1) (get_local, 0))

One application of this is being able to write valid WAT syntax in JavaScript functions

function add (a, b) {
   (i32.const, a)
   (i32.const, b)
   (i32.const, add)
}

That you can add.toString() to extract valid WAT source code, that can make it easier to interleave WAT source code within JavaScript without the need for template literals when prototyping.

Most helpful comment

I'm really not sure we want to have JS-compatible WAST. Mostly because from a parser perpective it would be really hard to tell if you are writing WAST or JS.

I would rather use JS's template literals:

function add (a, b) {
   wast`
     (i32.const, a)
     (i32.const, b)
     (i32.const, add)
   `
}

All 4 comments

S-expressions make me think of Lisp and because of that I would expect ' to mean quote and , to mean unquote.

This is an interesting idea, but I'm not sure we'll want to modify the text format to support it.

I'm really not sure we want to have JS-compatible WAST. Mostly because from a parser perpective it would be really hard to tell if you are writing WAST or JS.

I would rather use JS's template literals:

function add (a, b) {
   wast`
     (i32.const, a)
     (i32.const, b)
     (i32.const, add)
   `
}

You could knock up a WAT DSL in CoffeeScript very easily. Just to offer another option.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mfateev picture mfateev  ·  5Comments

jfbastien picture jfbastien  ·  6Comments

Artur-A picture Artur-A  ·  3Comments

spidoche picture spidoche  ·  4Comments

beriberikix picture beriberikix  ·  7Comments