Mozilla TechSpeaker.
Python and JS Developer
Author of Beginning Git and GitHub (Apress, 2020).
Savior of Hyrule
Mathematical application
NASA World Wind
....until JavaScript
But it has its problems...
Source: Lin Clark © 2017.
function sum(a, b) {
return a + b;
}
Pretty simple, right?
It "should be a single" CPU instruction
- Let lref be the result of evaluating AdditiveExpression.
- Let lval be GetValue(lref).
- Let rref be the result of evaluating MultiplicativeExpression.
- Let rval be GetValue(rref).
- Let lprim be ToPrimitive(lval).
- Let rprim be ToPrimitive(rval).
- If Type(lprim) is String or Type(rprim) is String, then
- Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)
- Return the result of applying the addition operation to ToNumber(lprim) and ToNumber(rprim).
You have to do some sacrifices
Understand Programming languages
Understand binary or low level code.
Source: Lin Clark © 2017.
Check it with WASM Code Explorer
(module
(type $type0 (func (param i32)))
(type $type1 (func))
(import "sys" "print" (func $import0 (param i32)))
(memory (;0;) 200 200)
(export "memory" (memory 0))
(export "main" (func $func1))
(func $func1
i32.const 0
call $import0
)
(data (i32.const 0)
"Hello, world\00"
)
)
Hello World in WAT
Each supported language's compiler will generate a .wasm file from the compiler.
rustc --target=wasm32-unknown-uknown project.rs -o project.html
GOOS=js GOARCH=wasm go build -o main.wasm
It's a toolchain to take high languages code to WASM
Execute
emcc project.c -s WASM=1 -o project.html
You'll get at least these 3 files:
Comunicate code between JS and WASM
C
#include <math.h>
extern "C" {
int int_sqrt(int x) {
return sqrt(x);
}
}
JavaScript
int_sqrt = Module.cwrap('int_sqrt', 'number', ['number'])
int_sqrt(12)
int_sqrt(28)
With emscripten_run_script()
emscripten_run_script(
"alert('I love alerts')"
);
With EM_ASM()
#include <emscripten.h>
int main() {
EM_ASM(
alert('DO NOT USE ALERTS!');
throw 'execution ready';
);
return 0;
}}
Applies video effects on realtime
A website to learn C;
code is executed at browser level
Source: Twitter.
WordPress' new editor uses a parser written in Rust.
Zend interpreter migrated to WASM
More info at: https://github.com/oraoto/pib
mariot.rahona.mg