-
- Notifications
You must be signed in to change notification settings - Fork 678
minimal 'hello world' example #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 17 commits
58e5faf 7a061f3 4ea902d 80fe216 2d44e59 e9ea57f 09bff59 e74b93a 99ea99a cb3819e f9975ce e8f8daa d310f29 ca3462a 1da8190 0221a27 e1fa956 c80f199 3b83387 cb5453d File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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!") | ||
| |
| 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)" | ||
| | ||
| # 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') | ||
| 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> |
| 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 |
| 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() |
| 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) | ||
| ) | ||
| ) | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #!/usr/bin/env node | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this file? Did you write it? Author There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Author There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Author There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| 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> |
| 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" |
| 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});\ | ||
| // " |
| 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) | ||
| ) | ||
| ) | ||
| ) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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