|
| 1 | +# AssemblyScript_on_WAMR |
| 2 | +This project is based on [Wasm Micro Runtime](https://github.com/bytecodealliance/wasm-micro-runtime) (WAMR) and [AssemblyScript](https://github.com/AssemblyScript/assemblyscript). It implements some of the `wamr app framework` in *assemblyscript*, which allows you to write some applications in *assemblyscript* and dynamically installed on *WAMR Runtime* |
| 3 | + |
| 4 | +## Building |
| 5 | +To build the samples in this repo, you need `npm` on your system |
| 6 | +``` bash |
| 7 | +sudo apt install npm |
| 8 | +``` |
| 9 | + |
| 10 | +Then install all the dependencies under the repo's root dir |
| 11 | +``` bash |
| 12 | +cd $repo_root |
| 13 | +npm install |
| 14 | +``` |
| 15 | + |
| 16 | +Use the command to build all samples: |
| 17 | +``` bash |
| 18 | +npm run build:all |
| 19 | +``` |
| 20 | +or you can build every sample individually: |
| 21 | +``` bash |
| 22 | +npm run build:timer |
| 23 | +npm run build:publisher |
| 24 | +npm run build:subscriber |
| 25 | +# ... |
| 26 | +``` |
| 27 | +You will get the compiled wasm file under `build` folder |
| 28 | + |
| 29 | +Please refer to [package.json](./package.json) for more commands. |
| 30 | + |
| 31 | +## Run |
| 32 | +These applications require WAMR's application framework, you need to build WAMR first. |
| 33 | + |
| 34 | +``` bash |
| 35 | +cd ${WAMR_ROOT}/samples/simple |
| 36 | +./build.sh |
| 37 | +``` |
| 38 | + |
| 39 | +You will get two executable files under `out` folder: |
| 40 | + |
| 41 | +`simple`: The wamr runtime with application framework |
| 42 | + |
| 43 | +`host_tool`: The tool used to dynamically install/uninstall applications |
| 44 | + |
| 45 | +1. Start the runtime: |
| 46 | + ``` bash |
| 47 | + ./simple -s |
| 48 | + ``` |
| 49 | + |
| 50 | +2. Install the compiled wasm file using `host_tool`: |
| 51 | + ``` bash |
| 52 | + ./host_tool -i app_name -f your_compiled_wasm_file.wasm |
| 53 | + ``` |
| 54 | +You can also use the WAMR's AoT compiler `wamrc` to compile the wasm bytecode into native code before you run them. Please refer to this [guide](../README.md#build-wamrc-aot-compiler) to build and install `WAMR AoT compiler`. |
| 55 | +
|
| 56 | +After installing `wamrc`, you can compile the wasm file using command: |
| 57 | +``` bash |
| 58 | +wamrc -o file_name.aot file_name.wasm |
| 59 | +``` |
| 60 | +and you can install the AoT file to the runtime: |
| 61 | +``` bash |
| 62 | +./host_tool -i app_name -f your_compiled_aot_file.aot |
| 63 | +``` |
| 64 | +
|
| 65 | +## Development |
| 66 | +You can develop your own application based on the `wamr_app_lib` APIs. |
| 67 | +
|
| 68 | +### Console APIs |
| 69 | +``` typescript |
| 70 | +function log(a: string): void; |
| 71 | +function log_number(a: number): void; |
| 72 | +``` |
| 73 | +
|
| 74 | +### Timer APIs |
| 75 | +``` typescript |
| 76 | +function setTimeout(cb: () => void, timeout: i32): user_timer; |
| 77 | +function setInterval(cb: () => void, timeout: i32): user_timer; |
| 78 | +function timer_cancel(timer: user_timer): void; |
| 79 | +function timer_restart(timer: user_timer, interval: number): void; |
| 80 | +function now(): i32; |
| 81 | +
|
| 82 | +// export to runtime |
| 83 | +function on_timer_callback(on_timer_id: i32): void; |
| 84 | +``` |
| 85 | +
|
| 86 | +### Request APIs |
| 87 | +``` typescript |
| 88 | +// register handler |
| 89 | +function register_resource_handler(url: string, |
| 90 | + request_handle: request_handler_f): void; |
| 91 | +// request |
| 92 | +function post(url: string, payload: ArrayBuffer, payload_len: number, |
| 93 | + tag: string, cb: (resp: wamr_response) => void): void; |
| 94 | +function get(url: string, tag: string, |
| 95 | + cb: (resp: wamr_response) => void): void; |
| 96 | +function put(url: string, payload: ArrayBuffer, payload_len: number, tag: string, |
| 97 | + cb: (resp: wamr_response) => void): void; |
| 98 | +function del(url: string, tag: string, |
| 99 | + cb: (resp: wamr_response) => void): void; |
| 100 | +
|
| 101 | +// response |
| 102 | +function make_response_for_request(req: wamr_request): wamr_response; |
| 103 | +function api_response_send(resp: wamr_response): void; |
| 104 | +
|
| 105 | +// event |
| 106 | +function publish_event(url: string, fmt: number, |
| 107 | + payload: ArrayBuffer, payload_len: number): void; |
| 108 | +function subscribe_event(url: string, cb: request_handler_f): void; |
| 109 | +
|
| 110 | +// export to runtime |
| 111 | +function on_request(buffer_offset: i32, size: i32): void; |
| 112 | +function on_response(buffer_offset : i32, size: i32): void; |
| 113 | +``` |
| 114 | +
|
| 115 | +You should export the `on_timer_callback`, `on_request` and `on_response` in your application entry file, refer to the samples for example. |
| 116 | +
|
| 117 | +To build your application, you can use `asc`: |
| 118 | +``` bash |
| 119 | +asc app.ts -b build/app.wasm -t build/app.wat --sourceMap --validate --optimize |
| 120 | +``` |
| 121 | +or you can add a command into [package.json](./package.json): |
| 122 | +``` json |
| 123 | +"build:app": "asc app.ts -b build/app.wasm -t build/app.wat --sourceMap --validate --optimize", |
| 124 | +``` |
0 commit comments