Skip to content

Commit

Permalink
got basic as working
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumer committed Sep 15, 2024
1 parent 8b13da2 commit df1cb1a
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 56 deletions.
4 changes: 3 additions & 1 deletion cart/as/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
This is an example null0 cart written in [assemblyscript](https://www.assemblyscript.org/).

You can make your own by editing files in [src/](src). `npm run build` will build your cart in `build/`. Change the package-name in package.json to change the name of the cart.

## TODO

- look into [better build](https://github.com/notnullgames/null0-ideas/blob/main/projects/assemblyscript/tools/build.mjs)
- look into [better cross-plaform build](https://github.com/notnullgames/null0-ideas/blob/main/projects/assemblyscript/tools/build.mjs)
16 changes: 0 additions & 16 deletions cart/as/asconfig.json

This file was deleted.

22 changes: 1 addition & 21 deletions cart/as/null0.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,2 @@
// include this in your null0 cart
// asc src/index.ts --target release --use trace=_null0_trace --use abort=_null0_abort --use seed=_null0_seed --lib ./null0.ts

// log a string
@external("null0", "trace")
declare function _null0_real_trace(text: ArrayBuffer): void

// these are called by language
// https://www.assemblyscript.org/concepts.html#special-imports
// TODO: would WASI help with these?

export function _null0_trace(message: String): void {
_null0_real_trace(String.UTF8.encode(message, true));
}

export function _null0_abort(message: String, fileName: String, line: u32, column: u32): void {
_null0_real_trace(String.UTF8.encode("ABORT: " + message, true));
}

export function _null0_seed(): f64 {
return 1.0;
}
// asc --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json --lib ./null0.ts src/main.ts
16 changes: 14 additions & 2 deletions cart/as/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions cart/as/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"name": "@null0/example-as",
"name": "example-as",
"private": true,
"version": "0.0.0",
"scripts": {
"build": "asc src/index.ts --exportStart=_null0_init --target release --use trace=_null0_trace --use abort=_null0_abort --use seed=_null0_seed --lib ./null0.ts && mkdir -p build/cart/assets && cp build/main.wasm build/cart && cp src/assets/* build/cart/assets && cd build/cart && zip -r ../example.null0 .",
"start": "../../build/host/null0 ./build/example.null0",
"build": "asc --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json --lib ./null0.ts --optimizeLevel 3 --outFile build/cart/main.wasm src/main.ts",
"postbuild": "mkdir -p build/cart/assets && cp src/assets/* build/cart/assets && cd build/cart && zip -r ../${npm_package_name}.null0 . && cp ../${npm_package_name}.null0 ../../../../docs/cart/",
"start": "../../build/host/null0 ./build/${npm_package_name}.null0",
"prestart": "npm run build"
},
"license": "MIT",
"devDependencies": {
"@assemblyscript/wasi-shim": "^0.1.0",
"assemblyscript": "^0.27.29"
},
"type": "module",
Expand Down
4 changes: 2 additions & 2 deletions cart/as/src/index.ts → cart/as/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// called when the cart is loaded
export function load(): void {
trace("hello from AS");
console.log("hello from AS");
}

// called on every frame
export function update(): void {
trace("update");
console.log("update");
}
6 changes: 2 additions & 4 deletions cart/as/src/tsconfig.json → cart/as/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"extends": "assemblyscript/std/assembly.json",
"include": [
"./**/*.ts"
]
}
"include": ["./src/**/*.ts"]
}
Binary file added docs/cart/example-as.null0
Binary file not shown.
4 changes: 0 additions & 4 deletions docs/null0.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ export async function setupCart(

out.cart = cart;

if (cart._null0_init) {
cart._null0_init();
}

if (cart._start) {
cart._start();
}
Expand Down
6 changes: 3 additions & 3 deletions null0_api/src/null0_api_wamr.h
Original file line number Diff line number Diff line change
Expand Up @@ -833,13 +833,13 @@ bool null0_init() {
cart_keyUp = wasm_runtime_lookup_function(module_inst, "keyUp");
cart_keyDown = wasm_runtime_lookup_function(module_inst, "keyDown");

// this is for AS, since it needs a seperate function called
// this is for hosts that seperate init
wasm_function_inst_t cart_init = NULL;
cart_init = wasm_runtime_lookup_function(module_inst, "_null0_init");
cart_init = wasm_runtime_lookup_function(module_inst, "load");
if (cart_init != NULL) {
if (!wasm_runtime_call_wasm(exec_env, cart_init, 0, NULL)) {
// not fatal, but this will help with troubleshooting
printf("init: %s\n", wasm_runtime_get_exception(module_inst));
printf("load: %s\n", wasm_runtime_get_exception(module_inst));
}
}

Expand Down

0 comments on commit df1cb1a

Please sign in to comment.