ballercat / walt
- четверг, 14 декабря 2017 г. в 03:15:03
⚡️ Walt is a JavaScript-like syntax for WebAssembly text format 🚧 WIP 🚧
Walt |
Alternative Syntax for WebAssembly |
Demo
.walt
files compile directly to WebAssembly binary format.
Highlights:
Writing zero-overhead, optimized WebAssembly is pretty tough to do. The syntax for .wat
files is terse and difficult to work with directly. If you do not wish to use a systems language like C or Rust,
then you're kind of out of luck. Your best bet (currently) is to write very plain C code, compile that to .wast and then optimize that result. Then you're ready to compile that into the final WebAssembly binary. This is an
attempt to take C/Rust out of the equation and write 'as close to the metal' as possible without loosing readability.
I feel like this is currently a problem. Most Web engineers are not familiar with the C family languages or Rust. It's a barrier for wide spread adoption of WebAssembly. A competent Front-end engineer should be able to edit WebAssembly as easily as any other systems programmer.
Provide a thin layer of syntax sugar on top of .wat
text format. Preferably porting as much of JavaScript syntax to WebAssembly as possible. This improved syntax should give direct control over
the WebAssembly output. Meaning there should be minimal to none post optimization to be done to the wast code generated. The re-use of JavaScript semantics is intentional as I do not wish to create a brand new language.
Here is what an example of a .walt
module which exports a recursive Fibonacci function looks like:
export function fibonacci(n: i32): i32 {
if (n == 0)
return 0;
if (n == 1)
return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
When this code is ran through the walt compiler you end up with a buffer which can be used to create a WebAssembly module with a fibonacci
export just as you would expect. All done with familiar JS syntax and without any external binary toolkits! A working demo of this exists in the fibonacci-spec.js
unit test file.
.walt
to .wasm
directly.walt
files to importable JavaScript modulesInitial release of WAlt has very few keywords.
let
, const
, type
- Declarationsimport
, export
- Imports and exportsfunction
, return
- Functions and return statementsi32
, i64
, f32
, f64
- Built in typesvoid
- is a custom label used to indicate functions which return nothing (because it's easy to parse). It's compiled out of the final binary.module
- reserved for memory
, 'Memory' - reserved for All s-expression-syntax words are reserved and can be written directly into .walt
scripts.
Initial grammar definition is provided in the /docs/grammar.md
WAlt splits its syntax into statements and expressions (like JavaScript).
WAlt will support C99 style comments. Inline comments are supported currently ('//') but not comment blocks
(/* */
).
Everything in WAlt as in WebAssembly must have a Type. Function are no exception to the rule. When a function is declared it's type is hoisted by the compiler behind the scenes. A function type is a list of parameters and a result type.
import { log: Log } from 'console';
type Log = (i32) => void
It is possible to import custom functions and use wasm functions as callbacks.
import { log: Log } from 'env';
import { setTimeout: Later } from 'env';
type Log = (i32) => void;
type Later = (Function, i32) => void;
function echo(): void {
log(42);
}
export function echoLater(x: i32): void {
setTimeout(echo, 200);
}
WebAssembly.Table
import to be provided in the imports object.Keep in mind that the Function
parameter is encoded into a i32
table index. This means that the setTimeout
function
must be a wrapper which can get the real wasm function pointer from table object. Like so:
{
setTimeout: (tableIndex, timeout) => {
const pointer = tableInstance.get(tableIndex);
setTimeout(pointer, timeout);
}
}
Simple rules about objects and arrays.
new
or delete
type
keyword is used to create a new user-type. Types can be object or function types.Mainly these makes it easier to write a compiler for WAlt. Interop between JavaScript and WAlt becomes simpler as well and the "syntax sugar" is kept to a minimum on top of the existing WebAssembly functionality.
Before using arrays or objects memory must be declared
const memory: Memory = { 'initial': 0 };
Array example:
// Unlike objects arrays do not require custom types and can be declared in-place
const intArr: i32[] = 0;
// There are no static array sizes and they can be read/written to at any index
intArr[0] = 2;
// Keep in mind that out-of-bounds memory access will result in a runtime error
intArr[255] = 10;
Object example:
// Object types are js-like objects with key value pairs of object properties
// and corresponding built-in basic types (i32, f32, i64, f64)
type FooType = { 'foo': i32 };
// Objects must be initialized with an address
// NOTE: WAlt runtime will _not_ perform any safety checks on this address
const foo: FooType = 0;
// Property lookups are performed as string subscripts
foo['foo'] = 200;
// Because objects are compiled down to a single integer address, they can be freely
// passed around to other functions or put into other objects
someOtherFunction(foo); // (i32) => void
Every WAlt file is compiled into a stand alone module. module
is a reserved keyword
With an implemented loader it will be possible to pipe the output to wasm-loader
allowing for code like this:
import makeCounter from './counter'; // <-- a .walt file
makeCounter() // returns a Promise
.then(result => {
console.log(result.exports.counter()); // 0, 1, 2, 3 etc.,
});