Skip to content
31 changes: 31 additions & 0 deletions examples/hello-assembly/hello-assembly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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(<i32>text);// glue 'log' to print each char
}
}

function print_pointer(pointer:i32):void{
let length=load<i16>(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<i16>(pointer))
}
}

console.log("Hello AssemblyScript!")

15 changes: 15 additions & 0 deletions examples/hello-assembly/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +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

# 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)"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and could you please split this in another js file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed hello-assembly altogether. two examples are enough


# 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')
37 changes: 37 additions & 0 deletions examples/hello-string/hello-string.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script type="text/javascript">

const encoder = new TextDecoder('utf-16le');
function toUTF16StringA(pointer, size) {
let arr = new Uint8Array(buffer.slice(pointer, pointer+ size*2)); // length *2 for utf16
console.log(encoder.decode(arr));
alert(encoder.decode(arr));
}
imports = { console: {logs: toUTF16StringA}}

async function run_wasm(wasm_file = 'hello-string.wasm'){
try{
fetch(wasm_file).then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.instantiate(bytes, imports)
).then(results => {
console.log(results)
});
// memory = new WebAssembly.Memory({initial: 16384, maximum: 65536});
}catch(ex){console.error(ex);alert(ex);}
}
window.onload=run_wasm

wasm=fetch('hello-string.wasm')
ready = function ({module,instance}){
console.log(instance)
// heap = instance.exports.memory.buffer
buffer = instance.exports.memory.buffer
instance.exports.main()
}
// module=WebAssembly.compileStreaming(wasm,imports).then(ready).catch(e=>alert(e))
module=WebAssembly.instantiateStreaming(wasm,imports).then(ready).catch(e=>alert(e))
// WebAssembly.instantiate(module)
// WebAssembly.instantiateStreaming(fetch('hello-string.wasm'),imports).catch(e=>alert(e))

</script>
13 changes: 13 additions & 0 deletions examples/hello-string/hello-string.sh
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions examples/hello-string/hello-string.ts
Original file line number Diff line number Diff line change
@@ -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(<i32>text + 4, size(text), 16); // 4 offset for internal struct // Utf16
}
}

function size(pointer: string): i16 {
return load<i16>(<i32>pointer) * 2; // size=2*length for utf16
}

export function main(): void {
console.log("Hello AssemblyScript 😻 !");
}

// main()
Binary file added examples/hello-string/hello-string.wasm
Binary file not shown.
45 changes: 45 additions & 0 deletions examples/hello-string/hello-string.wast
Original file line number Diff line number Diff line change
@@ -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)
)
)
)
44 changes: 44 additions & 0 deletions examples/hello-string/wasmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env node
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this file? Did you write it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, in-lining the node code as in hello-world.sh would be a bit too much with all the imports

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use it for emcc generated code as well, thus the demangle and strange imports

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes no sense here, so I'll remove the clutter


// 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);
4 changes: 4 additions & 0 deletions examples/hello-world/hello-world.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script type="text/javascript">
imports = { console: {log_int: i => alert("wasm result="+i) }}
WebAssembly.instantiateStreaming(fetch('hello-world.wasm'),imports).catch(e=>alert(e))
</script>
20 changes: 20 additions & 0 deletions examples/hello-world/hello-world.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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);\
"

# load and run the binary in the browser:
# open "hello-world.html"

# chrome needs (local) server for example:
# sudo python -m SimpleHTTPServer 80
# open "http://localhost/hello-world.html"
25 changes: 25 additions & 0 deletions examples/hello-world/hello-world.ts
Original file line number Diff line number Diff line change
@@ -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});\
// "
Binary file added examples/hello-world/hello-world.wasm
Binary file not shown.
14 changes: 14 additions & 0 deletions examples/hello-world/hello-world.wast
Original file line number Diff line number Diff line change
@@ -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)
)
)
)