Skip to content

Instantly share code, notes, and snippets.

@christianlavoie

christianlavoie/Main.mjs

Last active Jul 27, 2020
Embed
What would you like to do?
import * as rts from "./_build/rts.mjs";
import module from "./_build/Main.wasm.mjs";
import req from "./_build/Main.req.mjs";
module.then(m =>
rts.newAsteriusInstance(Object.assign(req, {module: m})))
.then(i => {
i.exports.main()
.catch(err => {
if (!(err.startsWith('ExitSuccess') || err.startsWith('ExitFailure ')))
i.fs.writeSync(2, `Main: ${err}`)
});
self.postMessage(['INITIALIZED'].concat(Object.keys(i.exports)));
self.onmessage = function(e) {
self.postMessage(['RECEIVED'].concat(e.data));
if (e.data[0] == 'calc') {
let p = i.exports.runParser(e.data[1]);
p.then(v => {
self.postMessage(['CALC VALUE', (' ' + v).slice(1)])
}).catch(r => {
self.postMessage(['CALC CRASH', (' ' + r).slice(1)])
});
} else {
self.postMessage(['RECEIVED UNKNOWN MESSAGE'].concat(e.data));
}
};
setInterval(function () {
let stdout = i.stdio.stdout();
if (stdout != "") self.postMessage(['STDOUT', stdout]);
let stderr = i.stdio.stderr();
if (stderr != "") self.postMessage(['STDERR', stderr]);
}, 1000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment