Skip to content

Commit

Permalink
trying to fix AS cart in web host
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumer committed Oct 19, 2024
1 parent 73ea5ac commit 2e4b68c
Show file tree
Hide file tree
Showing 16 changed files with 731 additions and 726 deletions.
1 change: 1 addition & 0 deletions cart/as/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ await writeFile('build/tmp.ts', (await readFile('null0.ts')) + '\n// user-code:\

const { error, stdout, stderr, stats } = await asc.main([
'build/tmp.ts',
'--debug',
'--runtime', 'minimal',
'--config', './node_modules/@assemblyscript/wasi-shim/asconfig.json',
'--optimizeLevel', '3',
Expand Down
Binary file modified docs/cart/colorbars.null0
Binary file not shown.
Binary file modified docs/cart/draw.null0
Binary file not shown.
Binary file modified docs/cart/example-as.null0
Binary file not shown.
Binary file modified docs/cart/filesystem.null0
Binary file not shown.
Binary file modified docs/cart/flappybird.null0
Binary file not shown.
Binary file modified docs/cart/hello.null0
Binary file not shown.
Binary file modified docs/cart/input.null0
Binary file not shown.
Binary file modified docs/cart/justlog.null0
Binary file not shown.
Binary file modified docs/cart/sound.null0
Binary file not shown.
Binary file modified docs/cart/tracker.null0
Binary file not shown.
Binary file modified docs/cart/typesizes.null0
Binary file not shown.
8 changes: 4 additions & 4 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
<body>
<p>Here are some example carts:</p>

<null0-cart src="cart/input.null0" fps></null0-cart>
<null0-cart src="cart/colorbars.null0" fps></null0-cart>
<null0-cart src="cart/draw.null0" fps></null0-cart>
<!-- <null0-cart src="cart/input.null0" fps></null0-cart> -->
<!-- <null0-cart src="cart/colorbars.null0" fps></null0-cart> -->
<!-- <null0-cart src="cart/draw.null0" fps></null0-cart> -->
<!-- <null0-cart src="cart/hello.null0" fps></null0-cart> -->
<!-- <null0-cart src="cart/flappybird.null0" fps></null0-cart> -->
<null0-cart src="cart/sound.null0" fps></null0-cart>
<!-- <null0-cart src="cart/sound.null0" fps></null0-cart> -->
<null0-cart src="cart/example-as.null0" fps></null0-cart>
<!-- <null0-cart src="cart/typesizes.null0" fps></null0-cart> -->
<!-- <null0-cart src="cart/filesystem.null0"></null0-cart> -->
Expand Down
254 changes: 127 additions & 127 deletions docs/null0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import loadCart from "./wasm/null0.mjs";
import wireCartToHost from "./null0_wasm.js";
import loadCart from './wasm/null0.mjs'
import wireCartToHost from './null0_wasm.js'

export const Buttons = {
A: 7,
Expand All @@ -13,57 +13,57 @@ export const Buttons = {
UP: 1,
DOWN: 3,
LEFT: 4,
RIGHT: 2,
};
RIGHT: 2
}

// get just the null0 wasm-host (emscripten)
export async function getHost(
export async function getHost (
cartUrl,
canvas = document.body.appendChild(document.createElement("canvas")),
canvas = document.body.appendChild(document.createElement('canvas'))
) {
const cartname = cartUrl.split("/").pop().split(".")[0];
const cartname = cartUrl.split('/').pop().split('.')[0]

const host = await loadCart({
canvas,
preRun: async function ({ FS }) {
await FS.createPreloadedFile("/", cartname, cartUrl, true, false);
await FS.createPreloadedFile('/', cartname, cartUrl, true, false)
},
// for some reason it strips off .null0
arguments: [`/${cartname}`],
});
arguments: [`/${cartname}`]
})

// read a file from inside the cart
host.readCartFile = (filename) => {
const filenamePtr = host._malloc(filename.length + 1);
host.stringToUTF8(filename, filenamePtr, filename.length + 1);
const bytesHostPtr = host._malloc(4);
const retPtr = host._null0_file_read(filenamePtr, bytesHostPtr);
const filenamePtr = host._malloc(filename.length + 1)
host.stringToUTF8(filename, filenamePtr, filename.length + 1)
const bytesHostPtr = host._malloc(4)
const retPtr = host._null0_file_read(filenamePtr, bytesHostPtr)
const r = host.HEAPU8.slice(
retPtr,
retPtr + host.HEAPU32[bytesHostPtr / 4],
);
host._free(bytesHostPtr);
host._free(filenamePtr);
host._free(retPtr);
return r;
};

return host;
retPtr + host.HEAPU32[bytesHostPtr / 4]
)
host._free(bytesHostPtr)
host._free(filenamePtr)
host._free(retPtr)
return r
}

return host
}

// setup a cart with engine
export async function setupCart(
export async function setupCart (
url,
canvas = document.body.appendChild(document.createElement("canvas")),
canvas = document.body.appendChild(document.createElement('canvas'))
) {
const host = await getHost(url, canvas);
const out = { host };
const cart = {};
const host = await getHost(url, canvas)
const out = { host }
const cart = {}

canvas.width = 320;
canvas.height = 240;
canvas.setAttribute("tabindex", 0);
canvas.addEventListener("click", () => canvas.focus());
canvas.width = 320
canvas.height = 240
canvas.setAttribute('tabindex', 0)
canvas.addEventListener('click', () => canvas.focus())

// TODO: focus/unfocus handlers for sound

Expand All @@ -72,171 +72,171 @@ export async function setupCart(

// minimal WASI that only allows console logging
wasi_snapshot_preview1: {
fd_write(fd, iovsPtr, iovsLength, bytesWrittenPtr) {
fd_write (fd, iovsPtr, iovsLength, bytesWrittenPtr) {
const iovs = new Uint32Array(
cart.memory.buffer,
iovsPtr,
iovsLength * 2,
);
iovsLength * 2
)
if (fd === 1 || fd === 2) {
// stdout/stderr
let text = "";
let totalBytesWritten = 0;
let text = ''
let totalBytesWritten = 0
for (let i = 0; i < iovsLength * 2; i += 2) {
const offset = iovs[i];
const length = iovs[i + 1];
text += cart.getString(offset, length);
totalBytesWritten += length;
const offset = iovs[i]
const length = iovs[i + 1]
text += cart.getString(offset, length)
totalBytesWritten += length
}
cart.view.setInt32(bytesWrittenPtr, totalBytesWritten, true);
cart.view.setInt32(bytesWrittenPtr, totalBytesWritten, true)

// not exactly right, since it will add newlines, but this covers printf and stuff
text = text.replace(/\n*$/, "");
text = text.replace(/\n*$/, '')
if (fd === 1) {
console.log(text);
console.log(text)
} else {
console.error(text);
console.error(text)
}
}
return 0;
return 0
},

fd_close() {
return 0;
fd_close () {
return 0
},

fd_fdstat_get() {
return 0;
fd_fdstat_get () {
return 0
},

fd_seek() {
return 0;
fd_seek () {
return 0
},

proc_exit(status) {
console.error("exit", status);
return 0;
},
},
};
proc_exit (status) {
console.error('exit', status)
return 0
}
}
}

const wasmBytes = host.readCartFile("main.wasm");
const cartMod = await WebAssembly.compile(wasmBytes);
const { exports } = await WebAssembly.instantiate(cartMod, imports);
const wasmBytes = host.readCartFile('main.wasm')
const cartMod = await WebAssembly.compile(wasmBytes)
const { exports } = await WebAssembly.instantiate(cartMod, imports)

for (const k of Object.keys(exports)) {
cart[k] = exports[k];
cart[k] = exports[k]
}

const d = new TextDecoder();
const d = new TextDecoder()
cart.getString = (offset, length) =>
d.decode(exports.memory.buffer.slice(offset, offset + length));
cart.view = new DataView(exports.memory.buffer);
d.decode(exports.memory.buffer.slice(offset, offset + length))
cart.view = new DataView(exports.memory.buffer)

out.cart = cart;
out.cart = cart

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

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

if (cart.buttonDown) {
canvas.addEventListener("keydown", ({ key }) => {
if (key === "z") {
cart.buttonDown(Buttons.B);
canvas.addEventListener('keydown', ({ key }) => {
if (key === 'z') {
cart.buttonDown(Buttons.B)
}
if (key === "x") {
cart.buttonDown(Buttons.A);
if (key === 'x') {
cart.buttonDown(Buttons.A)
}
if (key === "a") {
cart.buttonDown(Buttons.Y);
if (key === 'a') {
cart.buttonDown(Buttons.Y)
}
if (key === "s") {
cart.buttonDown(Buttons.X);
if (key === 's') {
cart.buttonDown(Buttons.X)
}
if (key === "Shift") {
cart.buttonDown(Buttons.SELECT);
if (key === 'Shift') {
cart.buttonDown(Buttons.SELECT)
}
if (key === "Enter") {
cart.buttonDown(Buttons.START);
if (key === 'Enter') {
cart.buttonDown(Buttons.START)
}
if (key === "q") {
cart.buttonDown(Buttons.L);
if (key === 'q') {
cart.buttonDown(Buttons.L)
}
if (key === "w") {
cart.buttonDown(Buttons.R);
if (key === 'w') {
cart.buttonDown(Buttons.R)
}
if (key === "ArrowUp") {
cart.buttonDown(Buttons.UP);
if (key === 'ArrowUp') {
cart.buttonDown(Buttons.UP)
}
if (key === "ArrowDown") {
cart.buttonDown(Buttons.DOWN);
if (key === 'ArrowDown') {
cart.buttonDown(Buttons.DOWN)
}
if (key === "ArrowLeft") {
cart.buttonDown(Buttons.LEFT);
if (key === 'ArrowLeft') {
cart.buttonDown(Buttons.LEFT)
}
if (key === "ArrowRight") {
cart.buttonDown(Buttons.RIGHT);
if (key === 'ArrowRight') {
cart.buttonDown(Buttons.RIGHT)
}
});
})
}

if (cart.buttonUp) {
canvas.addEventListener("keyup", ({ key }) => {
if (key === "z") {
cart.buttonUp(Buttons.B);
canvas.addEventListener('keyup', ({ key }) => {
if (key === 'z') {
cart.buttonUp(Buttons.B)
}
if (key === "x") {
cart.buttonUp(Buttons.A);
if (key === 'x') {
cart.buttonUp(Buttons.A)
}
if (key === "a") {
cart.buttonUp(Buttons.Y);
if (key === 'a') {
cart.buttonUp(Buttons.Y)
}
if (key === "s") {
cart.buttonUp(Buttons.X);
if (key === 's') {
cart.buttonUp(Buttons.X)
}
if (key === "Shift") {
cart.buttonUp(Buttons.SELECT);
if (key === 'Shift') {
cart.buttonUp(Buttons.SELECT)
}
if (key === "Enter") {
cart.buttonUp(Buttons.START);
if (key === 'Enter') {
cart.buttonUp(Buttons.START)
}
if (key === "q") {
cart.buttonUp(Buttons.L);
if (key === 'q') {
cart.buttonUp(Buttons.L)
}
if (key === "w") {
cart.buttonUp(Buttons.R);
if (key === 'w') {
cart.buttonUp(Buttons.R)
}
if (key === "ArrowUp") {
cart.buttonUp(Buttons.UP);
if (key === 'ArrowUp') {
cart.buttonUp(Buttons.UP)
}
if (key === "ArrowDown") {
cart.buttonUp(Buttons.DOWN);
if (key === 'ArrowDown') {
cart.buttonUp(Buttons.DOWN)
}
if (key === "ArrowLeft") {
cart.buttonUp(Buttons.LEFT);
if (key === 'ArrowLeft') {
cart.buttonUp(Buttons.LEFT)
}
if (key === "ArrowRight") {
cart.buttonUp(Buttons.RIGHT);
if (key === 'ArrowRight') {
cart.buttonUp(Buttons.RIGHT)
}
});
})
}

const cartUpdate = () => {
if (cart.update) {
cart.update();
cart.update()
}
window.requestAnimationFrame(cartUpdate);
window.requestAnimationFrame(cartUpdate)
if (cart.frameCallback) {
cart.frameCallback();
cart.frameCallback()
}
};
window.requestAnimationFrame(cartUpdate);
}
window.requestAnimationFrame(cartUpdate)

return out;
return out
}

export default setupCart;
export default setupCart
Loading

0 comments on commit 2e4b68c

Please sign in to comment.