C++JS = ?

Valeriy Kuzmin

Lilt, 2019

Games

Video/audio processing

IDE

Virtualization

VR

Data visualisation

Recognition

Language interpreters

Cryptography

Asm.js

Code example

function module() {
  "use asm"; // begin asm.js
 
  function fib(n) {
    n = n|0; // type annotation, int n
    var a = 0, b = 1, t = 0; 
    while( (n|0) >= 0) {
        t = b;
        b = a + b|0; // expression type annotation (int) (a + b)
        a = t;
        n = n - 1|0;
    }
    return b|0; // result type annotation int fib
  }
 
  return { fib: fib }; // export
}


module().fib(5); // 8th number, 13

- "Low-level" subset of JavaScript

- Primarily linear work with memory (no objects usage!)

- Typed

Features

- Can be optimized well

- Compilation target for C/C++/Rust

Demo: UT3 in Firefox

Browser support 15.07.2019

- Single error means de-optimizing into JS

- Huge source size, hard to parse

- Hard to extend because it's JS-based

What is bad about asm.js

WebAssembly

WebAssembly is...

- W3C - developed specification

- Text and binary format of source code

- Compilation target for С/С++/Rust/Kotlin/more

- Safe and portable

Basically, a shortcut to JS optimizations

WebAssembly is not...

- JS replacement

- A language of itself, at least writable one

- Compilations target for TypeScript, Coffescript...

- Compilation target Java, C# *

- Magic solution to optimize bad code code

Previous example: fib-asm.js

function module() {
  "use asm"; // begin asm.js
 
  function fib(n) {
    n = n|0; // type annotation, int n
    var a = 0, b = 1, t = 0; 
    while( (n|0) >= 0) {
        t = b;
        b = a + b|0; // expression type annotation, (int) (a + b)
        a = t;
        n = n - 1|0;
    }
    return b|0; // return type annotation, int fib
  }
 
  return { fib: fib }; // export
}


module().fib(5); // 8th number, 13

Example: fib.wast

(module
  (export "fib" (func $fib))
  (func $fib (param $n i32) (result i32)
    (local $a i32) (local $b i32) (local $t i32) // int a, b, t
    (loop $while-in (block $while-out // while
        (if (i32.eqz (i32.ge_s // n >= 0
            (get_local $n) 
            (i32.const 0)))
          (br $while-out))
        (block
          (set_local $t (get_local $b)) // t = b
          (set_local $b (i32.add (get_local $a) (get_local $b))) // b = a + b
          (set_local $a (get_local $t)) // a = t
          (set_local $n (i32.sub (get_local $n) (i32.const 1))) // n = n - 1
        )
        (br $while-in)
      )
    )
    (return (get_local $b)) // return b
  )
)

bin/asm2wasm --no-opts fib.asm.js

Example: fib.wasm

00000000  00 61 73 6d 0c 00 00 00  01 86 80 80 80 00 01 40  |.asm...........@|
00000010  01 01 01 01 02 c4 80 80  80 00 04 03 65 6e 76 06  |............env.|
00000020  6d 65 6d 6f 72 79 02 01  80 02 80 02 03 65 6e 76  |memory.......env|
00000030  05 74 61 62 6c 65 01 20  01 00 00 03 65 6e 76 0a  |.table. ....env.|
00000040  6d 65 6d 6f 72 79 42 61  73 65 03 01 00 03 65 6e  |memoryBase....en|
00000050  76 09 74 61 62 6c 65 42  61 73 65 03 01 00 03 82  |v.tableBase.....|
00000060  80 80 80 00 01 00 07 87  80 80 80 00 01 03 66 69  |..............fi|
00000070  62 00 00 09 81 80 80 80  00 00 0a bc 80 80 80 00  |b...............|
00000080  01 b6 80 80 80 00 01 03  01 01 01 02 00 01 00 14  |................|
00000090  00 10 00 54 5a 03 00 06  01 0f 01 00 14 02 15 03  |...TZ...........|
000000a0  14 01 14 02 40 15 02 14  03 15 01 14 00 10 01 41  |....@..........A|
000000b0  15 00 0f 06 01 0f 0f 14  02 09 0f 0f              |............|

 bin/wasm-as fib.wast | hexdump -C

188 байт

What you can do with WASM right now:

- Compile useful C libraries and use them in browser

- Use SQLLite in browser

- Render PDF with PDFium (C++ lib)

- Run LUA

- And even run Safari inside Chrome

What you cannot (yet) do

- Use SIMD without bugs

- Garbage collection integration

- Work with DOM / WebAPI without JS

- Exceptions without performance overhead

- Work with 64-bit ops and memory

Support 15.07.2019

Demo: Angry bots

Demo: hello С in browser

Tools used

- emscrippten sdk, "incoming" branch, c2f4e65c

- ubuntu 16.04, cmake, build-essentials

- binaryen, bb1c44a3

- firefox 53.02

hello.c

#include <stdio.h>

int main() {
        printf("hello, world!\n");
        return 0;
}

01-compile

02-run

The future is near

Useful stuff

  • https://www.youtube.com/watch?v=vmzz17JGPHI
  • http://webassembly.org/
  • http://asmjs.org/ 
  • https://kripken.github.io/emscripten-site/index.html
  • https://github.com/WebAssembly/binaryen

Thanks!

Questions?

malcoriel

malcoriel@gmail.com

wasm-intro-at-lilt

By Valeriy Kuzmin

wasm-intro-at-lilt

C++JS. WebAssembly tech overview for Lilt

  • 373