From 58e5faf0dab84fabf75c3c3ac926a365b445b4a1 Mon Sep 17 00:00:00 2001 From: Pannous Date: Tue, 20 Mar 2018 00:08:21 +0100 Subject: [PATCH 01/20] minimal 'hello world' example --- examples/hello-assembly/hello-assembly.ts | 21 +++++++++++++++++++++ examples/hello-assembly/test.sh | 9 +++++++++ 2 files changed, 30 insertions(+) create mode 100644 examples/hello-assembly/hello-assembly.ts create mode 100755 examples/hello-assembly/test.sh diff --git a/examples/hello-assembly/hello-assembly.ts b/examples/hello-assembly/hello-assembly.ts new file mode 100644 index 0000000000..fde313e480 --- /dev/null +++ b/examples/hello-assembly/hello-assembly.ts @@ -0,0 +1,21 @@ +namespace console { + + // imported helper to print a char in node.js + export declare function log_char(v: i32): void; + + function log(text:string):void{ + print_pointer(text);// for now glue 'log' to print each char + } +} + +function print_pointer(pointer:i32):void{ + let length=load(pointer) // print char by char + pointer+=2 // first bytes in string struct == length + while(length-->0){ + pointer+=2 // utf-16 + console.log_char(load(pointer)) + } +} + +console.log("Hello AssemblyScript!") + diff --git a/examples/hello-assembly/test.sh b/examples/hello-assembly/test.sh new file mode 100755 index 0000000000..fe911d2480 --- /dev/null +++ b/examples/hello-assembly/test.sh @@ -0,0 +1,9 @@ +asc -v || npm install --save-dev AssemblyScript/assemblyscript + +asc hello-assembly.ts -b test.wasm -t test.wast + +node -i -e "log_char = c => process.stdout.write(String.fromCodePoint(c));\ + new WebAssembly.Instance(new WebAssembly.Module(require('fs').readFileSync('test.wasm')),{console:{log_char}})" + +# instead of printing 'Hello' char by char we could also do this in node.js: +# new Buffer(instance.exports.memory.buffer).slice(8,50).toString('utf16le') \ No newline at end of file From 7a061f34fca28ac8f07ff091f70e397532d98826 Mon Sep 17 00:00:00 2001 From: Pannous Date: Tue, 20 Mar 2018 00:21:57 +0100 Subject: [PATCH 02/20] --- examples/hello-assembly/hello-assembly.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/examples/hello-assembly/hello-assembly.ts b/examples/hello-assembly/hello-assembly.ts index fde313e480..18148435a9 100644 --- a/examples/hello-assembly/hello-assembly.ts +++ b/examples/hello-assembly/hello-assembly.ts @@ -1,3 +1,12 @@ +// compile this file thus: +// asc -v || npm install --save-dev AssemblyScript/assemblyscript +// asc hello-assembly.ts -b test.wasm -t test.wast + +// run compiled wasm file in node.js: +// node -i -e "log_char = c => process.stdout.write(String.fromCodePoint(c));\ +// new WebAssembly.Instance(new WebAssembly.Module(require('fs').readFileSync('test.wasm')),{console:{log_char}})" + + namespace console { // imported helper to print a char in node.js From 4ea902d78b1eb1f2c8dcde293994935d3aa0d005 Mon Sep 17 00:00:00 2001 From: Pannous Date: Tue, 20 Mar 2018 00:22:19 +0100 Subject: [PATCH 03/20] --- hello-assembly/hello-assembly.ts | 21 +++++++++++++++++++++ hello-assembly/test.sh | 9 +++++++++ 2 files changed, 30 insertions(+) create mode 100644 hello-assembly/hello-assembly.ts create mode 100755 hello-assembly/test.sh diff --git a/hello-assembly/hello-assembly.ts b/hello-assembly/hello-assembly.ts new file mode 100644 index 0000000000..fde313e480 --- /dev/null +++ b/hello-assembly/hello-assembly.ts @@ -0,0 +1,21 @@ +namespace console { + + // imported helper to print a char in node.js + export declare function log_char(v: i32): void; + + function log(text:string):void{ + print_pointer(text);// for now glue 'log' to print each char + } +} + +function print_pointer(pointer:i32):void{ + let length=load(pointer) // print char by char + pointer+=2 // first bytes in string struct == length + while(length-->0){ + pointer+=2 // utf-16 + console.log_char(load(pointer)) + } +} + +console.log("Hello AssemblyScript!") + diff --git a/hello-assembly/test.sh b/hello-assembly/test.sh new file mode 100755 index 0000000000..fe911d2480 --- /dev/null +++ b/hello-assembly/test.sh @@ -0,0 +1,9 @@ +asc -v || npm install --save-dev AssemblyScript/assemblyscript + +asc hello-assembly.ts -b test.wasm -t test.wast + +node -i -e "log_char = c => process.stdout.write(String.fromCodePoint(c));\ + new WebAssembly.Instance(new WebAssembly.Module(require('fs').readFileSync('test.wasm')),{console:{log_char}})" + +# instead of printing 'Hello' char by char we could also do this in node.js: +# new Buffer(instance.exports.memory.buffer).slice(8,50).toString('utf16le') \ No newline at end of file From 80fe216926a0a1fceaa6a3be67f23f4ba5a05203 Mon Sep 17 00:00:00 2001 From: Pannous Date: Tue, 20 Mar 2018 00:23:32 +0100 Subject: [PATCH 04/20] --- hello-assembly/hello-assembly.ts | 21 --------------------- hello-assembly/test.sh | 9 --------- 2 files changed, 30 deletions(-) delete mode 100644 hello-assembly/hello-assembly.ts delete mode 100755 hello-assembly/test.sh diff --git a/hello-assembly/hello-assembly.ts b/hello-assembly/hello-assembly.ts deleted file mode 100644 index fde313e480..0000000000 --- a/hello-assembly/hello-assembly.ts +++ /dev/null @@ -1,21 +0,0 @@ -namespace console { - - // imported helper to print a char in node.js - export declare function log_char(v: i32): void; - - function log(text:string):void{ - print_pointer(text);// for now glue 'log' to print each char - } -} - -function print_pointer(pointer:i32):void{ - let length=load(pointer) // print char by char - pointer+=2 // first bytes in string struct == length - while(length-->0){ - pointer+=2 // utf-16 - console.log_char(load(pointer)) - } -} - -console.log("Hello AssemblyScript!") - diff --git a/hello-assembly/test.sh b/hello-assembly/test.sh deleted file mode 100755 index fe911d2480..0000000000 --- a/hello-assembly/test.sh +++ /dev/null @@ -1,9 +0,0 @@ -asc -v || npm install --save-dev AssemblyScript/assemblyscript - -asc hello-assembly.ts -b test.wasm -t test.wast - -node -i -e "log_char = c => process.stdout.write(String.fromCodePoint(c));\ - new WebAssembly.Instance(new WebAssembly.Module(require('fs').readFileSync('test.wasm')),{console:{log_char}})" - -# instead of printing 'Hello' char by char we could also do this in node.js: -# new Buffer(instance.exports.memory.buffer).slice(8,50).toString('utf16le') \ No newline at end of file From 2d44e59fa6d93b135681db69cc7dd94655e192fd Mon Sep 17 00:00:00 2001 From: Pannous Date: Tue, 20 Mar 2018 00:35:14 +0100 Subject: [PATCH 05/20] --- examples/hello-assembly/hello-assembly.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/examples/hello-assembly/hello-assembly.ts b/examples/hello-assembly/hello-assembly.ts index 18148435a9..ba00af7261 100644 --- a/examples/hello-assembly/hello-assembly.ts +++ b/examples/hello-assembly/hello-assembly.ts @@ -6,22 +6,21 @@ // node -i -e "log_char = c => process.stdout.write(String.fromCodePoint(c));\ // new WebAssembly.Instance(new WebAssembly.Module(require('fs').readFileSync('test.wasm')),{console:{log_char}})" - namespace console { // imported helper to print a char in node.js export declare function log_char(v: i32): void; - function log(text:string):void{ - print_pointer(text);// for now glue 'log' to print each char - } + function log(text:string):void{ + print_pointer(text);// glue 'log' to print each char + } } function print_pointer(pointer:i32):void{ - let length=load(pointer) // print char by char - pointer+=2 // first bytes in string struct == length + let length=load(pointer) + pointer+=2 // first bytes in string struct encode length while(length-->0){ - pointer+=2 // utf-16 + pointer+=2 //print utf-16 char by char console.log_char(load(pointer)) } } From e9ea57fca46114243679cddb5f7397de3d017d26 Mon Sep 17 00:00:00 2001 From: Pannous Date: Tue, 20 Mar 2018 10:25:54 +0100 Subject: [PATCH 06/20] --- examples/hello-assembly/hello-assembly.ts | 6 ++++-- examples/hello-assembly/test.sh | 14 ++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/examples/hello-assembly/hello-assembly.ts b/examples/hello-assembly/hello-assembly.ts index ba00af7261..0fd29df304 100644 --- a/examples/hello-assembly/hello-assembly.ts +++ b/examples/hello-assembly/hello-assembly.ts @@ -3,8 +3,10 @@ // asc hello-assembly.ts -b test.wasm -t test.wast // run compiled wasm file in node.js: -// node -i -e "log_char = c => process.stdout.write(String.fromCodePoint(c));\ -// new WebAssembly.Instance(new WebAssembly.Module(require('fs').readFileSync('test.wasm')),{console:{log_char}})" +// node -i -e "\ +// log_char = c => process.stdout.write(String.fromCodePoint(c));\ +// binary = require('fs').readFileSync('test.wasm')\ +// new WebAssembly.Instance(new WebAssembly.Module(binary),{console:{log_char}})" namespace console { diff --git a/examples/hello-assembly/test.sh b/examples/hello-assembly/test.sh index fe911d2480..ffec325afb 100755 --- a/examples/hello-assembly/test.sh +++ b/examples/hello-assembly/test.sh @@ -1,9 +1,15 @@ +# install https://github.com/AssemblyScript/assemblyscript if not present asc -v || npm install --save-dev AssemblyScript/assemblyscript +# compile Typescript to native WebAssembly asc hello-assembly.ts -b test.wasm -t test.wast -node -i -e "log_char = c => process.stdout.write(String.fromCodePoint(c));\ - new WebAssembly.Instance(new WebAssembly.Module(require('fs').readFileSync('test.wasm')),{console:{log_char}})" +# load and run the binary in node.js +node -i -e "\ + log_char = c => process.stdout.write(String.fromCodePoint(c));\ + binary = require('fs').readFileSync('test.wasm');\ + module = new WebAssembly.Module(binary);\ + new WebAssembly.Instance(module,{console:{log_char}})" -# instead of printing 'Hello' char by char we could also do this in node.js: -# new Buffer(instance.exports.memory.buffer).slice(8,50).toString('utf16le') \ No newline at end of file +# instead of printing 'Hello' char by char with the little glue function log_char, +# we could also do new Buffer(instance.exports.memory.buffer).slice(8,50).toString('utf16le') \ No newline at end of file From 09bff59b24ba5d3da8c5a5fa6bb16e32bdcd2246 Mon Sep 17 00:00:00 2001 From: Pannous Date: Tue, 20 Mar 2018 10:34:23 +0100 Subject: [PATCH 07/20] --- examples/hello-assembly/test.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/hello-assembly/test.sh b/examples/hello-assembly/test.sh index ffec325afb..0d3e61432d 100755 --- a/examples/hello-assembly/test.sh +++ b/examples/hello-assembly/test.sh @@ -6,10 +6,10 @@ asc hello-assembly.ts -b test.wasm -t test.wast # load and run the binary in node.js node -i -e "\ - log_char = c => process.stdout.write(String.fromCodePoint(c));\ binary = require('fs').readFileSync('test.wasm');\ module = new WebAssembly.Module(binary);\ - new WebAssembly.Instance(module,{console:{log_char}})" + imports={console:{log_char: c => process.stdout.write(String.fromCodePoint(c))}};\ + new WebAssembly.Instance(module,imports)" # instead of printing 'Hello' char by char with the little glue function log_char, # we could also do new Buffer(instance.exports.memory.buffer).slice(8,50).toString('utf16le') \ No newline at end of file From e74b93a318972c80570bcfc07fbc9e0a87cc8539 Mon Sep 17 00:00:00 2001 From: Pannous Date: Tue, 20 Mar 2018 10:35:36 +0100 Subject: [PATCH 08/20] --- examples/hello-assembly/test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/hello-assembly/test.sh b/examples/hello-assembly/test.sh index 0d3e61432d..b037cf53ee 100755 --- a/examples/hello-assembly/test.sh +++ b/examples/hello-assembly/test.sh @@ -9,7 +9,7 @@ node -i -e "\ binary = require('fs').readFileSync('test.wasm');\ module = new WebAssembly.Module(binary);\ imports={console:{log_char: c => process.stdout.write(String.fromCodePoint(c))}};\ - new WebAssembly.Instance(module,imports)" + instance= new WebAssembly.Instance(module,imports)" # instead of printing 'Hello' char by char with the little glue function log_char, # we could also do new Buffer(instance.exports.memory.buffer).slice(8,50).toString('utf16le') \ No newline at end of file From 99ea99a3f165c5f78df8023ce5ab28f6a907995b Mon Sep 17 00:00:00 2001 From: Pannous Date: Tue, 20 Mar 2018 10:46:58 +0100 Subject: [PATCH 09/20] added simple-most hello-world for node.js --- examples/hello-world/hello-world.sh | 16 ++++++++++++++++ examples/hello-world/hello-world.ts | 25 +++++++++++++++++++++++++ examples/hello-world/hello-world.wast | 14 ++++++++++++++ 3 files changed, 55 insertions(+) create mode 100755 examples/hello-world/hello-world.sh create mode 100644 examples/hello-world/hello-world.ts create mode 100644 examples/hello-world/hello-world.wast diff --git a/examples/hello-world/hello-world.sh b/examples/hello-world/hello-world.sh new file mode 100755 index 0000000000..47c53b1bf8 --- /dev/null +++ b/examples/hello-world/hello-world.sh @@ -0,0 +1,16 @@ +# install https://github.com/AssemblyScript/assemblyscript if not present +asc -v || npm install --save-dev AssemblyScript/assemblyscript + +# compile Typescript to native WebAssembly +asc hello-world.ts -b hello-world.wasm -t hello-world.wast + +# load and run the binary in node.js +node -i -e "\ + binary = require('fs').readFileSync('hello-world.wasm');\ + module = new WebAssembly.Module(binary);\ + imports = {console :{log_int : i => console.log(i) }} + instance= new WebAssembly.Instance(module,imports);\ + " + +# instead of printing 'Hello' char by char with the little glue function log_char, +# we could also do new Buffer(instance.exports.memory.buffer).slice(8,50).toString('utf16le') \ No newline at end of file diff --git a/examples/hello-world/hello-world.ts b/examples/hello-world/hello-world.ts new file mode 100644 index 0000000000..149e203222 --- /dev/null +++ b/examples/hello-world/hello-world.ts @@ -0,0 +1,25 @@ +// Hello World +// compile Typescript to native WebAssembly + +// imported helper to print a number in node.js +namespace console { + export declare function log_int(v: i32): void; +} + +console.log_int(42) + +// to run this example : + +// install AssemblyScript (https://github.com/AssemblyScript/assemblyscript) if not present +// asc -v || npm install --save-dev AssemblyScript/assemblyscript + +// compile Typescript to native WebAssembly +// asc hello-world.ts -b hello-world.wasm -t hello-world.wast + +// run compiled wasm file in node.js: +// node -i -e "\ +// binary = require('fs').readFileSync('hello-world.wasm');\ +// module = new WebAssembly.Module(binary);\ +// imports = {console :{log_int : i => console.log(i) }};\ +// instance = new WebAssembly.Instance(module,imports});\ +// " diff --git a/examples/hello-world/hello-world.wast b/examples/hello-world/hello-world.wast new file mode 100644 index 0000000000..718df74e94 --- /dev/null +++ b/examples/hello-world/hello-world.wast @@ -0,0 +1,14 @@ +(module + (type $iv (func (param i32))) + (type $v (func)) + (import "console" "log_int" (func $hello-world/console.log_int (param i32))) + (global $HEAP_BASE i32 (i32.const 4)) + (memory $0 1) + (export "memory" (memory $0)) + (start $start) + (func $start (; 1 ;) (type $v) + (call $hello-world/console.log_int + (i32.const 42) + ) + ) +) From cb3819edef5785e085c248e1b6b28ba4f045fe7e Mon Sep 17 00:00:00 2001 From: Pannous Date: Tue, 20 Mar 2018 11:18:56 +0100 Subject: [PATCH 10/20] --- examples/hello-world/hello-world.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/hello-world/hello-world.sh b/examples/hello-world/hello-world.sh index 47c53b1bf8..2bf96fc188 100755 --- a/examples/hello-world/hello-world.sh +++ b/examples/hello-world/hello-world.sh @@ -12,5 +12,9 @@ node -i -e "\ instance= new WebAssembly.Instance(module,imports);\ " -# instead of printing 'Hello' char by char with the little glue function log_char, -# we could also do new Buffer(instance.exports.memory.buffer).slice(8,50).toString('utf16le') \ No newline at end of file +# load and run the binary in the browser: +# open "hello-world.html" + +# chrome needs 'proper' server for example: +# open "http://localhost/hello-world.html" +# sudo python -m SimpleHTTPServer 80 From f9975ceee1220d96215ee81b8a8d7b856c9132c2 Mon Sep 17 00:00:00 2001 From: Pannous Date: Tue, 20 Mar 2018 11:19:14 +0100 Subject: [PATCH 11/20] --- examples/hello-world/hello-world.html | 4 ++++ examples/hello-world/hello-world.wasm | Bin 0 -> 127 bytes 2 files changed, 4 insertions(+) create mode 100644 examples/hello-world/hello-world.html create mode 100644 examples/hello-world/hello-world.wasm diff --git a/examples/hello-world/hello-world.html b/examples/hello-world/hello-world.html new file mode 100644 index 0000000000..53b92b7286 --- /dev/null +++ b/examples/hello-world/hello-world.html @@ -0,0 +1,4 @@ + \ No newline at end of file diff --git a/examples/hello-world/hello-world.wasm b/examples/hello-world/hello-world.wasm new file mode 100644 index 0000000000000000000000000000000000000000..ce2511b5baa3548b9f34fbd4e9d9aafe4f9fa46d GIT binary patch literal 127 zcmXZUy$ZrG6b8`mOWP#0i{R+&BK|x>pTq_YD%|8k5)da}U5D<6gHRIz(3^!{EP&b4 z-85H~=tLjaS8hOO$e|->?O)CP Date: Tue, 20 Mar 2018 11:39:14 +0100 Subject: [PATCH 12/20] --- examples/hello-world/hello-world.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/hello-world/hello-world.html b/examples/hello-world/hello-world.html index 53b92b7286..17e504d149 100644 --- a/examples/hello-world/hello-world.html +++ b/examples/hello-world/hello-world.html @@ -1,4 +1,4 @@ \ No newline at end of file From d310f29850f80e2ac762c37ccd97ba1e0e19b2f5 Mon Sep 17 00:00:00 2001 From: Pannous Date: Sun, 25 Mar 2018 18:19:18 +0200 Subject: [PATCH 13/20] added hello world for strings: hello-string --- examples/hello-string/hello-string.html | 37 ++++++++ examples/hello-string/hello-string.ts | 30 +++++++ examples/hello-string/hello-string.wasm | Bin 0 -> 292 bytes examples/hello-string/hello-string.wast | 45 ++++++++++ examples/hello-string/test.sh | 13 +++ examples/hello-string/wasmx | 110 ++++++++++++++++++++++++ 6 files changed, 235 insertions(+) create mode 100644 examples/hello-string/hello-string.html create mode 100644 examples/hello-string/hello-string.ts create mode 100644 examples/hello-string/hello-string.wasm create mode 100644 examples/hello-string/hello-string.wast create mode 100755 examples/hello-string/test.sh create mode 100755 examples/hello-string/wasmx diff --git a/examples/hello-string/hello-string.html b/examples/hello-string/hello-string.html new file mode 100644 index 0000000000..91444206e0 --- /dev/null +++ b/examples/hello-string/hello-string.html @@ -0,0 +1,37 @@ + \ No newline at end of file diff --git a/examples/hello-string/hello-string.ts b/examples/hello-string/hello-string.ts new file mode 100644 index 0000000000..6361f78788 --- /dev/null +++ b/examples/hello-string/hello-string.ts @@ -0,0 +1,30 @@ +// compile this file thus: +// asc -v || npm install --save-dev AssemblyScript/assemblyscript +// asc hello-string.ts -b test.wasm -t test.wast + +// run compiled wasm file in node.js: +// node -i -e "\ +// log_char = c => process.stdout.write(String.fromCodePoint(c));\ +// binary = require('fs').readFileSync('test.wasm')\ +// new WebAssembly.Instance(new WebAssembly.Module(binary),{console:{log_char}})" + +namespace console { + + // imported helper to print a string in node.js or browser + // export declare function logs(string_pointer: i32, length: i32): void; + export declare function logs(string_pointer: i32, size: i32, encoding: i16): void; // size=2*length for utf16 + + function log(text:string):void{ + logs(text+4,size(text),16);// 4 offset for internal struct // Utf16 + } +} + +function size(pointer:string):i16{ + return load(pointer)*2 // size=2*length for utf16 +} + +export function main():void{ + console.log("Hello AssemblyScript 😻 !") +} + +// main() \ No newline at end of file diff --git a/examples/hello-string/hello-string.wasm b/examples/hello-string/hello-string.wasm new file mode 100644 index 0000000000000000000000000000000000000000..4b77f9ed4c199b67f403d92dd7f1c25b904160bd GIT binary patch literal 292 zcmY+9F>k^!6ol{DCWv~#5lh+VTB&sFQW(6kvoa)&EE;6TRh$lV@!#y!-_jR}f#~#x zclQlVjivn;W}mSBaqX0Yaash8|nb=km>H3qui&j_t|Oey`g<`M@ucvc)yB$h+K2Rm0B z(V$0*FASXSf8YNWcmf*L)xpt8Vg2d6k1uKH+wS~&tGyj-Q#pxJ`{Tsp#JZ#96dsrT E1F%d(qW}N^ literal 0 HcmV?d00001 diff --git a/examples/hello-string/hello-string.wast b/examples/hello-string/hello-string.wast new file mode 100644 index 0000000000..24adfd9a5e --- /dev/null +++ b/examples/hello-string/hello-string.wast @@ -0,0 +1,45 @@ +(module + (type $v (func)) + (type $iv (func (param i32))) + (type $ii (func (param i32) (result i32))) + (type $iiiv (func (param i32 i32 i32))) + (import "console" "logs" (func $hello-string/console.logs (param i32 i32 i32))) + (global $HEAP_BASE i32 (i32.const 60)) + (memory $0 1) + (data (i32.const 4) "\19\00\00\00H\00e\00l\00l\00o\00 \00A\00s\00s\00e\00m\00b\00l\00y\00S\00c\00r\00i\00p\00t\00 \00=\d8;\de\t\00!\00") + (export "main" (func $hello-string/main)) + (export "memory" (memory $0)) + (func $hello-string/size (; 1 ;) (type $ii) (param $0 i32) (result i32) + (return + (i32.shr_s + (i32.shl + (i32.mul + (i32.load16_s + (get_local $0) + ) + (i32.const 2) + ) + (i32.const 16) + ) + (i32.const 16) + ) + ) + ) + (func $hello-string/console.log (; 2 ;) (type $iv) (param $0 i32) + (call $hello-string/console.logs + (i32.add + (get_local $0) + (i32.const 4) + ) + (call $hello-string/size + (get_local $0) + ) + (i32.const 16) + ) + ) + (func $hello-string/main (; 3 ;) (type $v) + (call $hello-string/console.log + (i32.const 4) + ) + ) +) diff --git a/examples/hello-string/test.sh b/examples/hello-string/test.sh new file mode 100755 index 0000000000..a3d4faea38 --- /dev/null +++ b/examples/hello-string/test.sh @@ -0,0 +1,13 @@ +# install https://github.com/AssemblyScript/assemblyscript if not present +# asc -v || npm install --save-dev AssemblyScript/assemblyscript + +# compile Typescript to native WebAssembly +asc hello-string.ts -b hello-string.wasm -t hello-string.wast + +# load and run the binary in node.js +wasmx hello-string.wasm + +# in firefox: +# firefox hello-string.html + +# todo : chrome needs to fetch() from (local) server \ No newline at end of file diff --git a/examples/hello-string/wasmx b/examples/hello-string/wasmx new file mode 100755 index 0000000000..bf0ea720ca --- /dev/null +++ b/examples/hello-string/wasmx @@ -0,0 +1,110 @@ +#!/usr/bin/env node + +file=arg=process.argv[2] + +backtrace=function (print=1){ + try{throw new Error()}catch(ex){if(print)console.error(trimStack(ex,1)); else return trimStack(ex)} +} + +demangle=function (line){ // todo: proper cpp demangle lol + // return line + if(!line.match("at "))return line + var [at,name,fun]=line.trim().split(' ') + if(!name)return line + type=name.match(/E(.*)/) + type=type||name.match(/PK(.*)/) + type=type?type[1]||"":"" + name=name.replace(/plE/g,'E')// constructor + name=name.replace(/E.*?$/g,'') + name=name.replace(/PK.*?$/g,'') + name=name.replace(/_Z\d/,'') + name=name.replace(/_ZN\d/,'') + name=name.replace(/\d/g,'.') + name=name.replace("_","") + // if(!type) + // return " at "+name + type=type.replace("v","") + type=type.replace("PK","*") + type=type.replace("i","int,") + type=type.replace("f","float,") + type=type.replace("c","char,") + type=type.replace(/,$/,'') + return file.replace(".wasm"," ")+name+"("+type+")" +} + +trimStack=function (ex,more=0) { + // console.error(ex) + let keep = true + let stack = ex.stack?ex.stack.split("\n"):(ex.message||"").split("\n") + let caller = trimStack.caller.name; + ex.stack = stack.filter(x => { + if(caller&&x.match(caller))return 0 + if (x.match("Object.")) keep = false + if (x.match("Module._compile")) keep = false + if (x.match("modulus.exports")) keep = false// todo + if(!keep && x.match("at "))more-- + return keep||more>0 + }).map(demangle).join("\n")//.replace(/at _/g,"at ") + return ex +} + + +// console.log(file) +// console.log() +binary = require('fs').readFileSync(file) +module = new WebAssembly.Module(binary) + +// memory = new WebAssembly.Memory({initial: 256, maximum: 256}); +// table = new WebAssembly.Table({initial: 10, maximum:10, element: "anyfunc"}); + +memory = new WebAssembly.Memory({initial: 16384, maximum: 65536}); +table = new WebAssembly.Table({initial: 256, element: "anyfunc"}); +buffer=new Buffer(memory.buffer) // node only! + +nop = x=>0 +log = x => console.log(x) + +if(typeof(TextDecoder)!='undefined'){// WEB + const encoder = new TextDecoder('utf-16le'); + string =function toUTF16StringA(pointer, size) { + let arr = new Uint8Array(heap.subarray(pointer, pointer + size)); + console.log(encoder.decode(arr)); + } +}else{ // NODE.js + string = function(pointer,length=-1, encoding=0){ + if(length<=0)while(buffer[pointer+ ++length]); // auto determine length + if(encoding) encoding='utf16le' + else encoding='utf8' // todo better + decoded = buffer.slice(pointer,pointer+length).toString(encoding) + return decoded + } +} + +// logs = (x,len) => console.log(string(x,len)) +logs = function(pointer,len,encoding){ console.log(string(pointer,len,encoding)) } +logc = x => process.stdout.write(x?String.fromCodePoint(x):"\n"), + +imports={ + console:{log_char: c => process.stdout.write(String.fromCodePoint(c)), + logc, _logc:logc, logs, _logs:logs, logi: log, _logi:log, log, _log:log, + }, global:{NaN,Infinity}, + env: {memory,table,abort:nop, nullFunc_X: log, abortStackOverflow:nop, + DYNAMICTOP_PTR: 100, + // STACKTOP:0,STACK_MAX:1000,enlargeMemory:log,getTotalMemory:log,abortOnCannotGrowMemory:log, + tempDoublePtr: 0, ABORT: 2, memoryBase: 0, tableBase: 0, + logc, _logc:logc, logs, _logs:logs, logi: log, _logi:log, log, _log:log, + logx: x => console.log(hex(x)), + _raise:x=>{throw new Error(string(x))}, + backtrace:x=>console.error(backtrace()), + }, +} +try{ + instance= new WebAssembly.Instance(module,imports) + args=process.argv.slice(3,process.argv.length) + let main = instance.exports.main || instance.exports._main + if(instance.exports.memory) + buffer = new Buffer(instance.exports.memory.buffer) // node only + if (main) console.log(">>>", result=main(process.argc,args)||0) +}catch(ex){console.error(trimStack(ex));} + +// console.log(Module.getValue(buffer+i*nByte, 'i32')); \ No newline at end of file From ca3462aa6c48580d59b1650d185d73993ab4916b Mon Sep 17 00:00:00 2001 From: Pannous Date: Mon, 26 Mar 2018 09:55:04 +0200 Subject: [PATCH 14/20] removed demangle --- examples/hello-string/wasmx | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/examples/hello-string/wasmx b/examples/hello-string/wasmx index bf0ea720ca..4adb9488c3 100755 --- a/examples/hello-string/wasmx +++ b/examples/hello-string/wasmx @@ -7,29 +7,7 @@ backtrace=function (print=1){ } demangle=function (line){ // todo: proper cpp demangle lol - // return line - if(!line.match("at "))return line - var [at,name,fun]=line.trim().split(' ') - if(!name)return line - type=name.match(/E(.*)/) - type=type||name.match(/PK(.*)/) - type=type?type[1]||"":"" - name=name.replace(/plE/g,'E')// constructor - name=name.replace(/E.*?$/g,'') - name=name.replace(/PK.*?$/g,'') - name=name.replace(/_Z\d/,'') - name=name.replace(/_ZN\d/,'') - name=name.replace(/\d/g,'.') - name=name.replace("_","") - // if(!type) - // return " at "+name - type=type.replace("v","") - type=type.replace("PK","*") - type=type.replace("i","int,") - type=type.replace("f","float,") - type=type.replace("c","char,") - type=type.replace(/,$/,'') - return file.replace(".wasm"," ")+name+"("+type+")" + return line } trimStack=function (ex,more=0) { From 1da819016c360e1ca8b36303e223d79ea0577de4 Mon Sep 17 00:00:00 2001 From: Pannous Date: Mon, 26 Mar 2018 09:58:52 +0200 Subject: [PATCH 15/20] little cleanup --- .../hello-string/{test.sh => hello-string.sh} | 2 +- examples/hello-string/wasmx | 63 +++++++++---------- 2 files changed, 29 insertions(+), 36 deletions(-) rename examples/hello-string/{test.sh => hello-string.sh} (93%) diff --git a/examples/hello-string/test.sh b/examples/hello-string/hello-string.sh similarity index 93% rename from examples/hello-string/test.sh rename to examples/hello-string/hello-string.sh index a3d4faea38..0e79473556 100755 --- a/examples/hello-string/test.sh +++ b/examples/hello-string/hello-string.sh @@ -5,7 +5,7 @@ asc hello-string.ts -b hello-string.wasm -t hello-string.wast # load and run the binary in node.js -wasmx hello-string.wasm +./wasmx hello-string.wasm # in firefox: # firefox hello-string.html diff --git a/examples/hello-string/wasmx b/examples/hello-string/wasmx index 4adb9488c3..c43d7f77aa 100755 --- a/examples/hello-string/wasmx +++ b/examples/hello-string/wasmx @@ -2,40 +2,10 @@ file=arg=process.argv[2] -backtrace=function (print=1){ - try{throw new Error()}catch(ex){if(print)console.error(trimStack(ex,1)); else return trimStack(ex)} -} - -demangle=function (line){ // todo: proper cpp demangle lol - return line -} - -trimStack=function (ex,more=0) { - // console.error(ex) - let keep = true - let stack = ex.stack?ex.stack.split("\n"):(ex.message||"").split("\n") - let caller = trimStack.caller.name; - ex.stack = stack.filter(x => { - if(caller&&x.match(caller))return 0 - if (x.match("Object.")) keep = false - if (x.match("Module._compile")) keep = false - if (x.match("modulus.exports")) keep = false// todo - if(!keep && x.match("at "))more-- - return keep||more>0 - }).map(demangle).join("\n")//.replace(/at _/g,"at ") - return ex -} - - -// console.log(file) -// console.log() binary = require('fs').readFileSync(file) module = new WebAssembly.Module(binary) -// memory = new WebAssembly.Memory({initial: 256, maximum: 256}); -// table = new WebAssembly.Table({initial: 10, maximum:10, element: "anyfunc"}); - -memory = new WebAssembly.Memory({initial: 16384, maximum: 65536}); +memory = new WebAssembly.Memory({initial: 10, maximum: 65536}); table = new WebAssembly.Table({initial: 256, element: "anyfunc"}); buffer=new Buffer(memory.buffer) // node only! @@ -51,14 +21,13 @@ if(typeof(TextDecoder)!='undefined'){// WEB }else{ // NODE.js string = function(pointer,length=-1, encoding=0){ if(length<=0)while(buffer[pointer+ ++length]); // auto determine length - if(encoding) encoding='utf16le' - else encoding='utf8' // todo better + if(encoding==16) encoding='utf16le' + else encoding='utf8' decoded = buffer.slice(pointer,pointer+length).toString(encoding) return decoded } } -// logs = (x,len) => console.log(string(x,len)) logs = function(pointer,len,encoding){ console.log(string(pointer,len,encoding)) } logc = x => process.stdout.write(x?String.fromCodePoint(x):"\n"), @@ -85,4 +54,28 @@ try{ if (main) console.log(">>>", result=main(process.argc,args)||0) }catch(ex){console.error(trimStack(ex));} -// console.log(Module.getValue(buffer+i*nByte, 'i32')); \ No newline at end of file +// console.log(Module.getValue(buffer+i*nByte, 'i32')); + + +backtrace=function (print=1){ + try{throw new Error()}catch(ex){if(print)console.error(trimStack(ex,1)); else return trimStack(ex)} +} + +demangle=function (line){ // todo: proper cpp demangle lol + return line +} + +trimStack=function (ex,more=0) { + let keep = true + let stack = ex.stack?ex.stack.split("\n"):(ex.message||"").split("\n") + let caller = trimStack.caller.name; + ex.stack = stack.filter(x => { + if(caller&&x.match(caller))return 0 + if (x.match("Object.")) keep = false + if (x.match("Module._compile")) keep = false + if (x.match("modulus.exports")) keep = false// todo + if(!keep && x.match("at "))more-- + return keep||more>0 + }).map(demangle).join("\n")//.replace(/at _/g,"at ") + return ex +} From 0221a27cbda804b603667ebb69794301288c6566 Mon Sep 17 00:00:00 2001 From: Pannous Date: Mon, 26 Mar 2018 10:11:46 +0200 Subject: [PATCH 16/20] cleanup --- examples/hello-string/wasmx | 103 +++++++++------------------- examples/hello-world/hello-world.sh | 4 +- 2 files changed, 34 insertions(+), 73 deletions(-) diff --git a/examples/hello-string/wasmx b/examples/hello-string/wasmx index c43d7f77aa..873f3ee694 100755 --- a/examples/hello-string/wasmx +++ b/examples/hello-string/wasmx @@ -1,81 +1,42 @@ #!/usr/bin/env node -file=arg=process.argv[2] - -binary = require('fs').readFileSync(file) -module = new WebAssembly.Module(binary) - -memory = new WebAssembly.Memory({initial: 10, maximum: 65536}); -table = new WebAssembly.Table({initial: 256, element: "anyfunc"}); -buffer=new Buffer(memory.buffer) // node only! - -nop = x=>0 -log = x => console.log(x) - -if(typeof(TextDecoder)!='undefined'){// WEB - const encoder = new TextDecoder('utf-16le'); - string =function toUTF16StringA(pointer, size) { - let arr = new Uint8Array(heap.subarray(pointer, pointer + size)); - console.log(encoder.decode(arr)); - } -}else{ // NODE.js - string = function(pointer,length=-1, encoding=0){ - if(length<=0)while(buffer[pointer+ ++length]); // auto determine length - if(encoding==16) encoding='utf16le' - else encoding='utf8' - decoded = buffer.slice(pointer,pointer+length).toString(encoding) - return decoded +// Load and execute wasm files + +let file=process.argv[2] + +function string(pointer,length=-1, encoding=0){ + if(typeof(TextDecoder)!='undefined'){// WEB + const encoder = new TextDecoder('utf-16le'); + let string =function toUTF16StringA(pointer, size) { + let arr = new Uint8Array(heap.subarray(pointer, pointer + size)); + return encoder.decode(arr); + } + }else{ // NODE.js + if(length<=0)while(buffer[pointer+ ++length]); // auto determine length + if(encoding==16) encoding='utf16le' + else encoding='utf8' + decoded = buffer.slice(pointer,pointer+length).toString(encoding) + return decoded } } -logs = function(pointer,len,encoding){ console.log(string(pointer,len,encoding)) } -logc = x => process.stdout.write(x?String.fromCodePoint(x):"\n"), -imports={ - console:{log_char: c => process.stdout.write(String.fromCodePoint(c)), - logc, _logc:logc, logs, _logs:logs, logi: log, _logi:log, log, _log:log, - }, global:{NaN,Infinity}, - env: {memory,table,abort:nop, nullFunc_X: log, abortStackOverflow:nop, - DYNAMICTOP_PTR: 100, - // STACKTOP:0,STACK_MAX:1000,enlargeMemory:log,getTotalMemory:log,abortOnCannotGrowMemory:log, - tempDoublePtr: 0, ABORT: 2, memoryBase: 0, tableBase: 0, - logc, _logc:logc, logs, _logs:logs, logi: log, _logi:log, log, _log:log, - logx: x => console.log(hex(x)), - _raise:x=>{throw new Error(string(x))}, - backtrace:x=>console.error(backtrace()), - }, +let imports={ + console:{ + logs: function(pointer,len,encoding){ console.log(string(pointer,len,encoding)) } + } } -try{ - instance= new WebAssembly.Instance(module,imports) - args=process.argv.slice(3,process.argv.length) - let main = instance.exports.main || instance.exports._main - if(instance.exports.memory) - buffer = new Buffer(instance.exports.memory.buffer) // node only - if (main) console.log(">>>", result=main(process.argc,args)||0) -}catch(ex){console.error(trimStack(ex));} -// console.log(Module.getValue(buffer+i*nByte, 'i32')); - - -backtrace=function (print=1){ - try{throw new Error()}catch(ex){if(print)console.error(trimStack(ex,1)); else return trimStack(ex)} +function load_wasm(file, run_main=true){ + binary = require('fs').readFileSync(file) + module = new WebAssembly.Module(binary) + instance= new WebAssembly.Instance(module,imports) + args=process.argv.slice(3,process.argv.length) + let main = run_main && instance.exports.main + if(instance.exports.memory) + buffer = new Buffer(instance.exports.memory.buffer) // node only + if (main) + console.log(result=main(process.argc,args)||"") } -demangle=function (line){ // todo: proper cpp demangle lol - return line -} - -trimStack=function (ex,more=0) { - let keep = true - let stack = ex.stack?ex.stack.split("\n"):(ex.message||"").split("\n") - let caller = trimStack.caller.name; - ex.stack = stack.filter(x => { - if(caller&&x.match(caller))return 0 - if (x.match("Object.")) keep = false - if (x.match("Module._compile")) keep = false - if (x.match("modulus.exports")) keep = false// todo - if(!keep && x.match("at "))more-- - return keep||more>0 - }).map(demangle).join("\n")//.replace(/at _/g,"at ") - return ex -} +load_wasm(file) diff --git a/examples/hello-world/hello-world.sh b/examples/hello-world/hello-world.sh index 2bf96fc188..013064912a 100755 --- a/examples/hello-world/hello-world.sh +++ b/examples/hello-world/hello-world.sh @@ -15,6 +15,6 @@ node -i -e "\ # load and run the binary in the browser: # open "hello-world.html" -# chrome needs 'proper' server for example: -# open "http://localhost/hello-world.html" +# chrome needs (local) server for example: # sudo python -m SimpleHTTPServer 80 +# open "http://localhost/hello-world.html" From e1fa956928a2684aa93d6c0d8a88ae9270a1992c Mon Sep 17 00:00:00 2001 From: Pannous Date: Mon, 26 Mar 2018 10:13:53 +0200 Subject: [PATCH 17/20] prettier --- examples/hello-string/hello-string.ts | 23 ++++++---- examples/hello-string/wasmx | 66 ++++++++++++++------------- 2 files changed, 47 insertions(+), 42 deletions(-) mode change 100755 => 100644 examples/hello-string/wasmx diff --git a/examples/hello-string/hello-string.ts b/examples/hello-string/hello-string.ts index 6361f78788..b73be11e30 100644 --- a/examples/hello-string/hello-string.ts +++ b/examples/hello-string/hello-string.ts @@ -9,22 +9,25 @@ // new WebAssembly.Instance(new WebAssembly.Module(binary),{console:{log_char}})" namespace console { - // imported helper to print a string in node.js or browser - // export declare function logs(string_pointer: i32, length: i32): void; - export declare function logs(string_pointer: i32, size: i32, encoding: i16): void; // size=2*length for utf16 + // export declare function logs(string_pointer: i32, length: i32): void; + export declare function logs( + string_pointer: i32, + size: i32, + encoding: i16 + ): void; // size=2*length for utf16 - function log(text:string):void{ - logs(text+4,size(text),16);// 4 offset for internal struct // Utf16 + function log(text: string): void { + logs(text + 4, size(text), 16); // 4 offset for internal struct // Utf16 } } -function size(pointer:string):i16{ - return load(pointer)*2 // size=2*length for utf16 +function size(pointer: string): i16 { + return load(pointer) * 2; // size=2*length for utf16 } -export function main():void{ - console.log("Hello AssemblyScript 😻 !") +export function main(): void { + console.log("Hello AssemblyScript 😻 !"); } -// main() \ No newline at end of file +// main() diff --git a/examples/hello-string/wasmx b/examples/hello-string/wasmx old mode 100755 new mode 100644 index 873f3ee694..e14351aaf9 --- a/examples/hello-string/wasmx +++ b/examples/hello-string/wasmx @@ -2,41 +2,43 @@ // Load and execute wasm files -let file=process.argv[2] +let file = process.argv[2]; -function string(pointer,length=-1, encoding=0){ - if(typeof(TextDecoder)!='undefined'){// WEB - const encoder = new TextDecoder('utf-16le'); - let string =function toUTF16StringA(pointer, size) { - let arr = new Uint8Array(heap.subarray(pointer, pointer + size)); - return encoder.decode(arr); - } - }else{ // NODE.js - if(length<=0)while(buffer[pointer+ ++length]); // auto determine length - if(encoding==16) encoding='utf16le' - else encoding='utf8' - decoded = buffer.slice(pointer,pointer+length).toString(encoding) - return decoded - } +function string(pointer, length = -1, encoding = 0) { + if (typeof TextDecoder != "undefined") { + // WEB + const encoder = new TextDecoder("utf-16le"); + let string = function toUTF16StringA(pointer, size) { + let arr = new Uint8Array(heap.subarray(pointer, pointer + size)); + return encoder.decode(arr); + }; + } else { + // NODE.js + if (length <= 0) while (buffer[pointer + ++length]); // auto determine length + if (encoding == 16) encoding = "utf16le"; + else encoding = "utf8"; + decoded = buffer.slice(pointer, pointer + length).toString(encoding); + return decoded; + } } +let imports = { + console: { + logs: function(pointer, len, encoding) { + console.log(string(pointer, len, encoding)); + } + } +}; -let imports={ - console:{ - logs: function(pointer,len,encoding){ console.log(string(pointer,len,encoding)) } - } +function load_wasm(file, run_main = true) { + binary = require("fs").readFileSync(file); + module = new WebAssembly.Module(binary); + instance = new WebAssembly.Instance(module, imports); + args = process.argv.slice(3, process.argv.length); + let main = run_main && instance.exports.main; + if (instance.exports.memory) + buffer = new Buffer(instance.exports.memory.buffer); // node only + if (main) console.log((result = main(process.argc, args) || "")); } -function load_wasm(file, run_main=true){ - binary = require('fs').readFileSync(file) - module = new WebAssembly.Module(binary) - instance= new WebAssembly.Instance(module,imports) - args=process.argv.slice(3,process.argv.length) - let main = run_main && instance.exports.main - if(instance.exports.memory) - buffer = new Buffer(instance.exports.memory.buffer) // node only - if (main) - console.log(result=main(process.argc,args)||"") -} - -load_wasm(file) +load_wasm(file); From c80f1995678dd4188c15c1dc7d8de2b00425a2f5 Mon Sep 17 00:00:00 2001 From: Pannous Date: Mon, 26 Mar 2018 10:24:54 +0200 Subject: [PATCH 18/20] removed hello-assembly/ --- examples/hello-assembly/hello-assembly.ts | 31 ----------------------- examples/hello-assembly/test.sh | 15 ----------- 2 files changed, 46 deletions(-) delete mode 100644 examples/hello-assembly/hello-assembly.ts delete mode 100755 examples/hello-assembly/test.sh diff --git a/examples/hello-assembly/hello-assembly.ts b/examples/hello-assembly/hello-assembly.ts deleted file mode 100644 index 0fd29df304..0000000000 --- a/examples/hello-assembly/hello-assembly.ts +++ /dev/null @@ -1,31 +0,0 @@ -// compile this file thus: -// asc -v || npm install --save-dev AssemblyScript/assemblyscript -// asc hello-assembly.ts -b test.wasm -t test.wast - -// run compiled wasm file in node.js: -// node -i -e "\ -// log_char = c => process.stdout.write(String.fromCodePoint(c));\ -// binary = require('fs').readFileSync('test.wasm')\ -// new WebAssembly.Instance(new WebAssembly.Module(binary),{console:{log_char}})" - -namespace console { - - // imported helper to print a char in node.js - export declare function log_char(v: i32): void; - - function log(text:string):void{ - print_pointer(text);// glue 'log' to print each char - } -} - -function print_pointer(pointer:i32):void{ - let length=load(pointer) - pointer+=2 // first bytes in string struct encode length - while(length-->0){ - pointer+=2 //print utf-16 char by char - console.log_char(load(pointer)) - } -} - -console.log("Hello AssemblyScript!") - diff --git a/examples/hello-assembly/test.sh b/examples/hello-assembly/test.sh deleted file mode 100755 index b037cf53ee..0000000000 --- a/examples/hello-assembly/test.sh +++ /dev/null @@ -1,15 +0,0 @@ -# install https://github.com/AssemblyScript/assemblyscript if not present -asc -v || npm install --save-dev AssemblyScript/assemblyscript - -# compile Typescript to native WebAssembly -asc hello-assembly.ts -b test.wasm -t test.wast - -# load and run the binary in node.js -node -i -e "\ - binary = require('fs').readFileSync('test.wasm');\ - module = new WebAssembly.Module(binary);\ - imports={console:{log_char: c => process.stdout.write(String.fromCodePoint(c))}};\ - instance= new WebAssembly.Instance(module,imports)" - -# instead of printing 'Hello' char by char with the little glue function log_char, -# we could also do new Buffer(instance.exports.memory.buffer).slice(8,50).toString('utf16le') \ No newline at end of file From 3b83387929c49f018553f902a0f9f456a3e44b2a Mon Sep 17 00:00:00 2001 From: Pannous Date: Mon, 26 Mar 2018 10:26:35 +0200 Subject: [PATCH 19/20] --- examples/hello-world/hello-world.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/hello-world/hello-world.sh b/examples/hello-world/hello-world.sh index 013064912a..57ebac197c 100755 --- a/examples/hello-world/hello-world.sh +++ b/examples/hello-world/hello-world.sh @@ -5,11 +5,11 @@ asc -v || npm install --save-dev AssemblyScript/assemblyscript asc hello-world.ts -b hello-world.wasm -t hello-world.wast # load and run the binary in node.js -node -i -e "\ - binary = require('fs').readFileSync('hello-world.wasm');\ - module = new WebAssembly.Module(binary);\ +node -i -e " + binary = require('fs').readFileSync('hello-world.wasm'); + module = new WebAssembly.Module(binary); imports = {console :{log_int : i => console.log(i) }} - instance= new WebAssembly.Instance(module,imports);\ + instance= new WebAssembly.Instance(module,imports); " # load and run the binary in the browser: From cb5453d3e00a9887e7d2d25332d26c5070e4ec45 Mon Sep 17 00:00:00 2001 From: Pannous Date: Mon, 26 Mar 2018 10:30:38 +0200 Subject: [PATCH 20/20] --- examples/hello-world/hello-world.sh | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/examples/hello-world/hello-world.sh b/examples/hello-world/hello-world.sh index 57ebac197c..ca574b6513 100755 --- a/examples/hello-world/hello-world.sh +++ b/examples/hello-world/hello-world.sh @@ -1,10 +1,11 @@ # install https://github.com/AssemblyScript/assemblyscript if not present asc -v || npm install --save-dev AssemblyScript/assemblyscript -# compile Typescript to native WebAssembly +echo compile Typescript to native WebAssembly : +echo asc hello-world.ts -b hello-world.wasm -t hello-world.wast asc hello-world.ts -b hello-world.wasm -t hello-world.wast -# load and run the binary in node.js +echo load and run the binary in node.js node -i -e " binary = require('fs').readFileSync('hello-world.wasm'); module = new WebAssembly.Module(binary); @@ -12,9 +13,11 @@ node -i -e " instance= new WebAssembly.Instance(module,imports); " -# load and run the binary in the browser: +echo load and run the binary in firefox: +echo open "hello-world.html" # open "hello-world.html" -# chrome needs (local) server for example: -# sudo python -m SimpleHTTPServer 80 -# open "http://localhost/hello-world.html" +echo chrome needs (local) server for example: +echo sudo python -m SimpleHTTPServer 80 +echo then +echo open "http://localhost/hello-world.html"