From ae5a79e151718204eebc2334494e1056825b60f1 Mon Sep 17 00:00:00 2001 From: Steve Dunham Date: Tue, 5 Nov 2024 20:20:20 -0800 Subject: [PATCH] move newt to web worker --- playground/build | 12 +++++------ playground/index.html | 2 -- playground/src/main.ts | 30 +++++++++++---------------- playground/src/{shim.ts => worker.ts} | 22 ++++++++++++++++++++ 4 files changed, 40 insertions(+), 26 deletions(-) rename playground/src/{shim.ts => worker.ts} (83%) diff --git a/playground/build b/playground/build index 40f0c39..873a455 100755 --- a/playground/build +++ b/playground/build @@ -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 diff --git a/playground/index.html b/playground/index.html index 50a1941..d0bd34e 100644 --- a/playground/index.html +++ b/playground/index.html @@ -5,8 +5,6 @@ Newt Playground - -
diff --git a/playground/src/main.ts b/playground/src/main.ts index 5908909..3270c60 100644 --- a/playground/src/main.ts +++ b/playground/src/main.ts @@ -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*(.*)/); diff --git a/playground/src/shim.ts b/playground/src/worker.ts similarity index 83% rename from playground/src/shim.ts rename to playground/src/worker.ts index 35dfae4..ef00ce0 100644 --- a/playground/src/shim.ts +++ b/playground/src/worker.ts @@ -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}) +}