Skip to content

Commit 1b8f74d

Browse files
committed
fix: missing import for starting function + docs: updated docs
1 parent dd1a7f5 commit 1b8f74d

File tree

7 files changed

+39
-9
lines changed

7 files changed

+39
-9
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Aeon
1+
# Aeon ⏳✨
22

33
Aeon is an extremely tiny and easy to use WebAssembly Binary Format runtime for <a href="https://github.com/thomscoder/luna" target="_blank">Luna</a>, built for demonstration and educational purposes.
44

@@ -8,11 +8,12 @@ It is not a replacement for solid runtimes like <a href="https://wasmer.io/">Was
88

99
Hence, I tried to document it as much as I could!
1010

11-
# How to use
11+
# How to use
1212
- Pass the Wasm binary, the function name and parameters to the main function
1313
- Done (you should see the result)
1414

1515
```js
16+
const startAeonRuntime = require("./runtime/start");
1617
// This binary
1718
// - takes 3 parameters of type `i32` (3, 127, 127, 127)
1819
// - outputs one `i32` result (1, 127)
@@ -24,7 +25,7 @@ const n1 = 8;
2425
const n2 = 20;
2526
const n3 = 23;
2627

27-
const result = startAeonRuntime(wasmBinary, n1, n2, n3);
28+
const result = startAeonRuntime(wasmBinary, "addNumbers", n1, n2, n3);
2829
console.log(`${n1} + ${n2} + ${n3} =`, result) // prints 51
2930
```
3031

@@ -34,4 +35,5 @@ console.log(`${n1} + ${n2} + ${n3} =`, result) // prints 51
3435

3536
# Contribute
3637
If you have any suggestion, feedback or want to add features, feel free to open issues, pull requests or fork the project.
37-
Turn it into a npm package, embed it in your next project, build beautiful tutorials...Aeon will be able (in the near future) to be open to all the possibilities to teach Web Assembly.
38+
Turn it into a npm package, embed it in your next project, build beautiful tutorials...
39+
Aeon will be able (in the near future) to be open to all the possibilities to teach Web Assembly.

example/example.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const startAeonRuntime = require("../runtime/start");
2+
3+
// Compile the ./main.wat file with https://luna-demo.vercel.app
4+
// Get the hex dump from either the middle div or the console
5+
6+
// The below example
7+
// - exports aeonAddition
8+
// - takes two parameters (i32)
9+
// - performs an addition (i32)
10+
const wasmBinary = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 7, 1, 96, 2, 127, 127, 1, 127, 3, 2, 1, 0, 7, 18, 1, 14, 34, 97, 101, 111, 110, 65, 100, 100, 105, 116, 105, 111, 110, 34, 0, 0, 10, 9, 1, 7, 0, 32, 0, 32, 1, 106, 11]);
11+
12+
const result = startAeonRuntime(wasmBinary, "aeonAddition", 2, 3)
13+
console.log("Result", result) // prints 5

example/main.wat

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
;; Module that exports a function called "aeonAddition"
2+
;; Compile this with https://luna-demo.vercel.app
3+
(module
4+
(func (export "aeonAddition") (param i32 i32) (result i32)
5+
local.get 0
6+
local.get 1
7+
i32.add)
8+
)

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
const startAeonRuntime = require("./runtime/start");
2+
13
const wasmBinary = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 8, 1, 96, 3, 127, 127, 127, 1, 127, 3, 2, 1, 0, 7, 16, 1, 12, 34, 97, 100, 100, 78, 117, 109, 98, 101, 114, 115, 34, 0, 0, 10, 9, 1, 7, 0, 32, 0, 32, 1, 32, 2, 106, 11]);
24

35
const n1 = 8;
46
const n2 = 20;
57
const n3 = 23;
68

7-
const result = startAeonRuntime(wasmBinary, n1, n2, n3);
9+
const result = startAeonRuntime(wasmBinary, "addNumbers", n1, n2, n3);
810
console.log(`${n1} + ${n2} + ${n3} =`, result)

runtime/parser.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,14 @@ const { ValType } = require("../utils/types");
7474

7575
// We parse and check the headers (MAGIC and VERSION)
7676
module.exports.checkHeader = (wasm) => {
77-
if (wasm.length < 8) {
77+
if (wasm.data.length < 8) {
7878
throw new Error(RuntimeErrors.ModuleTooShort);
7979
}
8080

81+
if (wasm.data.length == 8) {
82+
throw new Error(RuntimeErrors.ModuleIsEmpty)
83+
};
84+
8185
const magicString = String.fromCharCode(...wasm.readBytes(4));
8286
if (magicString !== "\0asm") {
8387
throw new Error(RuntimeErrors.InvalidMagicHeader);

runtime/start.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ const { createAST } = require("./ast");
22
const { invokeFunction } = require("./invoker");
33

44
const startAeonRuntime = (wasm, ...args) => {
5-
const ast = createAST(wasmBinary);
6-
const funcName = "addNumbers";
7-
const params = [...args];
5+
const ast = createAST(wasm);
6+
const [funcName, ...rest] = args;
7+
const params = rest;
88

99
const result = invokeFunction(ast, funcName, params);
1010
return result;

utils/errors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const RuntimeErrors = {
2+
ModuleIsEmpty: "Module is empty",
23
ModuleTooShort: "Module is too short",
34
InvalidMagicHeader: "Invalid magic header",
45
InvalidVersionHeader: "Invalid version header",

0 commit comments

Comments
 (0)