Skip to content
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

Runtime update #12

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions runtime/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Aeon - A runtime for Luna ⏳✨

Aeon is an extremely tiny, but easy to use WebAssembly runtime, built for demonstration and educational purposes.
It is so tiny that supports only additions, subtractions and i32 integers.
It is so tiny that supports only additions, subtractions and i32 integers.

It is part of the <a href="https://github.com/thomscoder/luna" target="_blank">Luna</a> project, so they integrate very very well.
Its purpose is to give an high-level overview of how to build a custom WebAssembly runtime.
Expand All @@ -18,9 +18,9 @@ Aeon was built with Luna in mind and they travel together so whatever Luna can c
(Check the `./example/example.js`)

```js
import startAeonRuntime from "./runtime/start"
// This binary
// - takes 3 parameters of type `i32` (3, 127, 127, 127)
import createAeonRuntime from "./runtime/start"
// This binary
// - takes 3 parameters of type `i32` (3, 127, 127, 127)
// - outputs one `i32` result (1, 127)
// - exports a function called "addNumbers" (12, 34, 97, 100, 100, 78, 117, 109, 98, 101, 114, 115, 34)
// - that adds them all (0, 32, 0, 32, 1, 32, 2, 106, 11)
Expand All @@ -30,7 +30,8 @@ const n1 = 8;
const n2 = 20;
const n3 = 23;

const result = startAeonRuntime(wasmBinary, "addNumbers", n1, n2, n3);
const runtime = createAeonRuntime(wasmBinary);
const result = runtime("addNumbers", n1, n2, n3);
console.log(`${n1} + ${n2} + ${n3} =`, result) // prints 51
```

Expand All @@ -44,4 +45,4 @@ There are tons and tons of feature they could implement. I try to keep them as m

If you have any suggestion, feedback or want to add features, feel free to open issues, pull requests or fork the project.
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.
Aeon will be able (in the near future) to be open to all the possibilities to teach Web Assembly.
10 changes: 6 additions & 4 deletions runtime/example/example.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import startAeonRuntime from "../runtime/start.js";
import createAeonRuntime from "../runtime/start.js";

// Compile the ./main.wat file with https://luna-demo.vercel.app
// Get the hex dump from either the middle div or the console

// The below example
// The below example
// - exports aeonAddition
// - takes two parameters (i32)
// - performs an addition (i32)
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]);

const result = startAeonRuntime(wasmBinary, "aeonAddition", 2, 3)
console.log("Result", result) // prints 5
const runtime = createAeonRuntime(wasmBinary)
const result = runtime("aeonAddition", 2, 3);

console.log("Result", result) // prints 5
7 changes: 4 additions & 3 deletions runtime/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import startAeonRuntime from "./runtime/start.js";
import createAeonRuntime from "./runtime/start.js";

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]);

const n1 = 8;
const n2 = 20;
const n3 = 23;

const result = startAeonRuntime(wasmBinary, "addNumbers", n1, n2, n3);
console.log(`${n1} + ${n2} + ${n3} =`, result)
const runtime = createAeonRuntime(wasmBinary);
const result = runtime("addNumbers", n1, n2, n3);
console.log(`${n1} + ${n2} + ${n3} =`, result)
2 changes: 1 addition & 1 deletion runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "runtime",
"version": "1.0.0",
"version": "1.1.0",
"description": "",
"main": "index.js",
"type": "module",
Expand Down
15 changes: 7 additions & 8 deletions runtime/runtime/start.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { createAST } from "./ast.js";
import { invokeFunction } from "./invoker.js";

const startAeonRuntime = (wasm, ...args) => {
const ast = createAST(wasm);
const [funcName, ...rest] = args;
const params = rest;

const result = invokeFunction(ast, funcName, params);
return result;
/**
@param wasm - A wasm binary.
*/
const createAeonRuntime = (wasm) => {
const ast = createAST(wasm);
return (funcName, params) => invokeFunction(ast, funcName, params);
}

export default startAeonRuntime;
export default createAeonRuntime;