move newt to web worker

This commit is contained in:
2024-11-05 20:20:20 -08:00
parent 182876d16b
commit ae5a79e151
4 changed files with 40 additions and 26 deletions

View File

@@ -1,9 +1,9 @@
#!/bin/sh
echo Builds the workMain.js and copies newt.js
echo monaco worker
esbuild --bundle node_modules/monaco-editor/esm/vs/editor/editor.worker.js > public/workerMain.js
# bare javascript, it fakes node api for the idris code in newt.js
esbuild src/shim.ts > public/shim.js
cp ../build/exec/newt.js public
# uncomment to make this smaller
# esbuild --minify ../build/exec/newt.min.js > public/newt.js
echo newt worker
esbuild src/worker.ts > public/worker.js
echo newt
cat ../build/exec/newt.js |grep -v '^#'>> public/worker.js
# esbuild --minify ../build/exec/newt.min.js > public/newt.js

View File

@@ -5,8 +5,6 @@
<link rel="stylesheet" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Newt Playground</title>
<script src="shim.js"></script>
<script src="newt.js"></script>
</head>
<body>
<div id="app">

View File

@@ -1,4 +1,3 @@
// import "./style.css";
import { newtConfig, newtTokens } from "./monarch.ts";
import * as monaco from "monaco-editor";
@@ -6,6 +5,8 @@ monaco.languages.register({ id: "newt" });
monaco.languages.setMonarchTokensProvider("newt", newtTokens);
monaco.languages.setLanguageConfiguration("newt", newtConfig);
const newtWorker = new Worker("worker.js")//new URL("worker.js", import.meta.url))
self.MonacoEnvironment = {
getWorkerUrl(moduleId, label) {
console.log("Get worker", moduleId);
@@ -32,31 +33,24 @@ const editor = monaco.editor.create(container, {
let timeout: number | undefined;
function run(s: string) {
console.log("run", s);
process.argv = ["", "", "src/Main.newt", "-o", "out.js"];
console.log("args", process.argv);
files["src/Main.newt"] = s;
result.innerHTML = "";
stdout = ''
newtMain();
processOutput();
function run(src: string) {
newtWorker.postMessage({src})
}
let stdout = ''
// We'll want to collect and put info in the monaco
process.stdout.write = (s) => {
stdout += s
};
newtWorker.onmessage = (ev) => {
console.log('worker sent', ev.data)
let {output, javascript} = ev.data
processOutput(output);
}
// Adapted from the vscode extension, but types are slightly different
// and positions are 1-based.
const processOutput = () => {
result.innerText = stdout
const processOutput = (output: string) => {
result.innerText = output
let model = editor.getModel()!
let markers: monaco.editor.IMarkerData[] = []
let lines = stdout.split('\n')
let lines = output.split('\n')
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const match = line.match(/(INFO|ERROR) at \((\d+), (\d+)\):\s*(.*)/);

View File

@@ -110,3 +110,25 @@ const process: Process = {
};
const require = (x: string) => shim[x];
// Maybe the shim goes here and we append newt...
let stdout = ''
// We'll want to collect and put info in the monaco
process.stdout.write = (s) => {
stdout += s
};
onmessage = function (e) {
console.log('worker got', e.data)
let {src} = e.data
process.argv = ["", "", "src/Main.newt", "-o", "out.js"];
console.log("args", process.argv);
files["src/Main.newt"] = src;
stdout = ''
newtMain();
let javascript = files['out.js']
let output = stdout
console.log('WORKER POSTS', {javascript, output})
postMessage({javascript, output})
}