Skip to content

Commit

Permalink
fix output race condition (fixes #4)
Browse files Browse the repository at this point in the history
use a unified callback for both stdout and stderr
  • Loading branch information
katrinafyi committed Aug 27, 2024
1 parent c06f5fd commit 645700e
Show file tree
Hide file tree
Showing 4 changed files with 6,960 additions and 7 deletions.
7 changes: 4 additions & 3 deletions web/aslp.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const parseIntSafe = (s, radix) => {


const _write = (isError) => s => requestAnimationFrame(() => {
if (!s) return;
const span = document.createElement('span');
const data = new Uint8Array(s.length);
for (let i = 0; i < s.length; i++) {
Expand All @@ -51,6 +52,7 @@ const _write = (isError) => s => requestAnimationFrame(() => {
if (isError)
span.classList.add('stderr');
outputArea.appendChild(span);
// console.log('recv', s);
return 0;
});
const write = { out: _write(false), err: _write(true), };
Expand All @@ -61,8 +63,7 @@ const stoneworker = Comlink.wrap(new Worker('worker-stone.js', { type: 'module'
await worker.boop(Comlink.proxy(console.log));

await worker.init(
Comlink.proxy(write.out),
Comlink.proxy(write.err)
Comlink.proxy((iserr, s) => iserr ? write.err(s) : write.out(s))
);

console.log('ready');
Expand Down Expand Up @@ -267,11 +268,11 @@ const init = async () => {
if (!resp.ok) throw new Error('fetch failure');
const buf = await resp.arrayBuffer();
await worker.unmarshal(Comlink.transfer(buf));
loadingText.classList.add('invisible');

if (urlData.size > 0) {
await submit();
}
loadingText.classList.add('invisible');
};


Expand Down
50 changes: 50 additions & 0 deletions web/cache.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
globalThis.module = { exports: {} };
await import('./builder.bc.js');
console.log('hi');
console.log(JSON.stringify(Object.keys(globalThis.module.exports)));

import * as pako from './lib/pako.esm.mjs';

const IS_NODE = typeof self !== 'object';
const fs = IS_NODE ? await import('fs') : null;
const path = IS_NODE ? await import('path') : null;

export const marshal = () => {
if (!IS_NODE) throw Error("marshalling only supported on node");

console.log('marshalling aslp environment...');
const arr = pako.gzip(libASL_builder.marshal(libASL_builder.force()));

let marshalUrl;
if (IS_NODE) {
fs.writeFileSync('aslp.heap', Buffer.from(arr));
marshalUrl = path.resolve('.', 'aslp.heap');

} else {
marshalUrl = URL.createObjectURL(
new File(
[arr],
'aslp.heap',
{ lastModified: Date.now(), type: 'application/octet-stream' }));

}
console.log(marshalUrl);
};


export const unmarshal = async (/* ArrayBuffer */ buf) => {
try {
const arr = pako.ungzip(new Uint8Array(buf));

libASL_web.unmarshal(arr);
console.log(`heap loaded: ${arr.length} bytes, ${buf.byteLength} compressed`);

} catch (e) {
console.warn('failed to fetch heap');
console.warn(e);
}
};

if (IS_NODE) {
marshal();
}
Loading

0 comments on commit 645700e

Please sign in to comment.