Skip to content

Commit 5a10651

Browse files
support app framework base library in assemblyscript (#164)
1 parent b5cbc02 commit 5a10651

File tree

17 files changed

+1083
-40
lines changed

17 files changed

+1083
-40
lines changed

assembly-script/README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
```

assembly-script/package-lock.json

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assembly-script/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "assembly_script",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"build:request_handler": "asc samples/request_handler.ts -b build/request_handler.wasm -t build/request_handler.wat --sourceMap --validate --optimize",
9+
"build:request_sender": "asc samples/request_sender.ts -b build/request_sender.wasm -t build/request_sender.wat --sourceMap --validate --optimize",
10+
"build:timer": "asc samples/timer.ts -b build/timer.wasm -t build/timer.wat --sourceMap --validate --optimize",
11+
"build:publisher": "asc samples/event_publisher.ts -b build/event_publisher.wasm -t build/event_publisher.wat --sourceMap --validate --optimize",
12+
"build:subscriber": "asc samples/event_subscriber.ts -b build/event_subscriber.wasm -t build/event_subscriber.wat --sourceMap --validate --optimize",
13+
"build:all": "npm run build:request_handler; npm run build:request_sender; npm run build:timer; npm run build:subscriber; npm run build:publisher"
14+
},
15+
"author": "",
16+
"license": "ISC",
17+
"devDependencies": {
18+
"assemblyscript": "^0.8.1"
19+
}
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
// The entry file of your WebAssembly module.
7+
import * as console from "../wamr_app_lib/console"
8+
import * as timer from "../wamr_app_lib/timer"
9+
import * as request from "../wamr_app_lib/request"
10+
11+
function publish_overheat_event(): void {
12+
var payload = String.UTF8.encode("warning: temperature is over high");
13+
request.publish_event("alert/overheat", 0, payload, payload.byteLength);
14+
}
15+
16+
export function on_init() : void {
17+
timer.setInterval(publish_overheat_event, 2000);
18+
}
19+
20+
export function on_destroy() : void {
21+
22+
}
23+
24+
25+
/* Function below are requred by wamr runtime, don't remove or modify them */
26+
export function _on_timer_callback(on_timer_id: i32): void {
27+
timer.on_timer_callback(on_timer_id);
28+
}
29+
30+
export function _on_request(buffer_offset: i32, size: i32): void {
31+
request.on_request(buffer_offset, size);
32+
}
33+
34+
export function _on_response(buffer_offset : i32, size: i32): void {
35+
request.on_response(buffer_offset, size);
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
// The entry file of your WebAssembly module.
7+
import * as console from "../wamr_app_lib/console"
8+
import * as timer from "../wamr_app_lib/timer"
9+
import * as request from "../wamr_app_lib/request"
10+
11+
export function on_init() : void {
12+
request.subscribe_event("alert/overheat", (req) => {
13+
console.log("### user over heat event handler called:");
14+
15+
console.log("");
16+
console.log(" " + String.UTF8.decode(req.payload) + "\n");
17+
})
18+
}
19+
20+
export function on_destroy() : void {
21+
22+
}
23+
24+
25+
/* Function below are requred by wamr runtime, don't remove or modify them */
26+
export function _on_timer_callback(on_timer_id: i32): void {
27+
timer.on_timer_callback(on_timer_id);
28+
}
29+
30+
export function _on_request(buffer_offset: i32, size: i32): void {
31+
request.on_request(buffer_offset, size);
32+
}
33+
34+
export function _on_response(buffer_offset : i32, size: i32): void {
35+
request.on_response(buffer_offset, size);
36+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
// The entry file of your WebAssembly module.
7+
import * as console from "../wamr_app_lib/console"
8+
import * as timer from "../wamr_app_lib/timer"
9+
import * as request from "../wamr_app_lib/request"
10+
11+
export function on_init() : void {
12+
request.register_resource_handler("/test", (req) => {
13+
console.log("### Req: /test " + String.UTF8.decode(req.payload));
14+
15+
console.log(" request payload:");
16+
console.log(" " + String.UTF8.decode(req.payload) + "\n");
17+
18+
var resp = request.make_response_for_request(req);
19+
resp.set_payload(String.UTF8.encode("Ok"), 2);
20+
request.api_response_send(resp);
21+
});
22+
}
23+
24+
export function on_destroy() : void {
25+
26+
}
27+
28+
29+
/* Function below are requred by wamr runtime, don't remove or modify them */
30+
export function _on_timer_callback(on_timer_id: i32): void {
31+
timer.on_timer_callback(on_timer_id);
32+
}
33+
34+
export function _on_request(buffer_offset: i32, size: i32): void {
35+
request.on_request(buffer_offset, size);
36+
}
37+
38+
export function _on_response(buffer_offset : i32, size: i32): void {
39+
request.on_response(buffer_offset, size);
40+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
// The entry file of your WebAssembly module.
7+
import * as console from "../wamr_app_lib/console"
8+
import * as timer from "../wamr_app_lib/timer"
9+
import * as request from "../wamr_app_lib/request"
10+
11+
export function on_init() : void {
12+
var payload = String.UTF8.encode("test message");
13+
request.post("/test", payload, payload.byteLength, "", (resp) => {
14+
if (resp != null) {
15+
console.log("Post Success");
16+
17+
if (resp.payload != null) {
18+
console.log(" response payload:")
19+
console.log(" " + String.UTF8.decode(resp.payload!) + "\n");
20+
}
21+
}
22+
else
23+
console.log("Post Timeout");
24+
});
25+
}
26+
27+
export function on_destroy() : void {
28+
29+
}
30+
31+
32+
/* Function below are requred by wamr runtime, don't remove or modify them */
33+
export function _on_timer_callback(on_timer_id: i32): void {
34+
timer.on_timer_callback(on_timer_id);
35+
}
36+
37+
export function _on_request(buffer_offset: i32, size: i32): void {
38+
request.on_request(buffer_offset, size);
39+
}
40+
41+
export function _on_response(buffer_offset : i32, size: i32): void {
42+
request.on_response(buffer_offset, size);
43+
}

assembly-script/samples/timer.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
// The entry file of your WebAssembly module.
7+
import * as console from '../wamr_app_lib/console'
8+
import * as timer from '../wamr_app_lib/timer'
9+
10+
/* clousure is not implemented yet, we need to declare global variables
11+
so that they can be accessed inside a callback function */
12+
var cnt = 0;
13+
var my_timer: timer.user_timer;
14+
15+
export function on_init(): void {
16+
/* The callback function will be called every 2 second,
17+
and will stop after 10 calls */
18+
my_timer = timer.setInterval(() => {
19+
cnt ++;
20+
console.log((cnt * 2).toString() + " seconds passed");
21+
22+
if (cnt >= 10) {
23+
timer.timer_cancel(my_timer);
24+
console.log("Stop Timer");
25+
}
26+
}, 2000);
27+
}
28+
29+
export function on_destroy(): void {
30+
31+
}
32+
33+
/* Function below are requred by wamr runtime, don't remove or modify them */
34+
export function _on_timer_callback(on_timer_id: i32): void {
35+
timer.on_timer_callback(on_timer_id);
36+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "../node_modules/assemblyscript/std/assembly.json",
3+
"include": [
4+
"./**/*.ts"
5+
]
6+
}

0 commit comments

Comments
 (0)