WebAssembly Text Format (.WAT)

So I've been thinking about making a JS-NATIVE variant which makes it so you can write WebAssembly directly as the body of a function.

Looking at the WebAssembly Text Format, it seems Ren-C is rather well-geared to be able to just LOAD it.

(module
  (table 2 funcref)
  (func $f1 (result i32)
    i32.const 42)
  (func $f2 (result i32)
    i32.const 13)
  (elem (i32.const 0) $f1 $f2)
  (type $return_i32 (func (result i32)))
  (func (export "callByIndex") (param $i i32) (result i32)
    local.get $i
    call_indirect (type $return_i32))
)

It's cool to be able to have that "just work" and wind up with a code format that you can manipulate, compose, process, analyze... all using mechanisms that are a sunk cost.

Historical Rebol didn't have $var-words and tu.ple (Redbol allows dots in names, so something like i32.const would be legal, but you'd have no particular conveniences to pick it apart.

Here's the full grammar.

I've seen some examples where the symbols have more embedded $ signs, like $FUNCSIG$ii:

(module
 (type $FUNCSIG$ii (func (param i32) (result i32)))
 (import "env" "puts" (func $puts (param i32) (result i32)))
 (table 0 anyfunc)
 (memory $0 1)
 (data (i32.const 16) "Hello World\00")
..........further text....
..........

It occurs to me that we can allow this--kind of like how we allow embedded apostrophes--or apostrophes at the tail of words.

>> word: first [f$]
== f$

>> word? word
== ~true~  ; anti

>> to var-word! word
== $f$

It's ugly but if it doesn't create any mechanical paradoxes or contradictions.

What can't be legal is things like $ or $$ as a WORD!, because you start down a rabbit hole of asking if $$ is its own WORD! or the VAR-WORD! variation of $. So you can't have a leading $ followed by another $. But other limits are arbitrary...and here's a reason to allow them.

1 Like

WebAssembly uses semicolons for comments, so it really is darn near loadable.

We now have $0 as legal under a different type, which is a "Tied Integer" and not a MONEY!.

But in WebAssembly this is an identifier, and it can be anything like $0Q.

In WebAssembly text format (WAT), a legal identifier (name) starts with a $ and can be followed by any sequence of UTF-8 characters except whitespace, delimiters, or comment characters.

Common valid characters:

  • Letters (a-z, A-Z)
  • Digits (0-9)
  • Underscore (_)
  • Hyphen (-)
  • Most Unicode characters

Invalid characters:

  • Whitespace (space, tab, newline)
  • Parentheses: (, )
  • Semicolon (;)
  • Double quote (")
  • Comment start (;; or (;)

Examples of valid identifiers:

$foo
$bar_123
$αβγ
$my-var

Examples of invalid identifiers:

$foo bar   ; contains space
$foo)      ; contains parenthesis
$foo;      ; contains semicolon

Reference:
WebAssembly spec: Names

WebAssembly also has weird things like:

(f32.const -0x0p+0)

You can get ugly FUSED! things out of that, or hex denoted like 0x10DECAFBAD20, which don't make any real sense but you don't have to make sense. Just turn the FUSED! into a TEXT! and process it. (Or TO BINARY!, and if you give TO BINARY! a fused it will see what it can do for you.)

FUSED! seems like a compromise for those who wanted non-LOADable things to load as some mystery thing to decode (what Geomol called KWATZ!). How it breaks into parts may or may not be meaningful. If it's meaningful, great--leverage that. If it's not, convert it and process what you convert it to.

1 Like