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.sh b/examples/hello-string/hello-string.sh new file mode 100755 index 0000000000..0e79473556 --- /dev/null +++ b/examples/hello-string/hello-string.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/hello-string.ts b/examples/hello-string/hello-string.ts new file mode 100644 index 0000000000..b73be11e30 --- /dev/null +++ b/examples/hello-string/hello-string.ts @@ -0,0 +1,33 @@ +// 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() diff --git a/examples/hello-string/hello-string.wasm b/examples/hello-string/hello-string.wasm new file mode 100644 index 0000000000..4b77f9ed4c Binary files /dev/null and b/examples/hello-string/hello-string.wasm differ 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/wasmx b/examples/hello-string/wasmx new file mode 100644 index 0000000000..e14351aaf9 --- /dev/null +++ b/examples/hello-string/wasmx @@ -0,0 +1,44 @@ +#!/usr/bin/env node + +// 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; + } +} + +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) || "")); +} + +load_wasm(file); diff --git a/examples/hello-world/hello-world.html b/examples/hello-world/hello-world.html new file mode 100644 index 0000000000..17e504d149 --- /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.sh b/examples/hello-world/hello-world.sh new file mode 100755 index 0000000000..ca574b6513 --- /dev/null +++ b/examples/hello-world/hello-world.sh @@ -0,0 +1,23 @@ +# install https://github.com/AssemblyScript/assemblyscript if not present +asc -v || npm install --save-dev AssemblyScript/assemblyscript + +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 + +echo 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); + " + +echo load and run the binary in firefox: +echo open "hello-world.html" +# open "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" 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.wasm b/examples/hello-world/hello-world.wasm new file mode 100644 index 0000000000..ce2511b5ba Binary files /dev/null and b/examples/hello-world/hello-world.wasm differ 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) + ) + ) +)