diff --git a/wasm/Colors.civet b/wasm/Colors.civet index 8b2daaf..3697b51 100644 --- a/wasm/Colors.civet +++ b/wasm/Colors.civet @@ -1,5 +1,5 @@ class Colors - entries: Array<{ color: string, div?: HTMLDivElement, threadNumber?: number }> = [ + entries: { color: string, div?: HTMLDivElement, threadNumber?: number }[] = [ { color: 'rgb(0, 173, 0)' }, { color: 'rgb(255, 40, 255)' }, { color: 'rgb(49, 151, 255)' }, @@ -26,12 +26,11 @@ class Colors entry := (@entries.find &.threadNumber is threadNumber) ?? @entries.find not &.threadNumber? if entry? entry.threadNumber = threadNumber - unless entry.div? - entry.div = document.createElement 'div' - entry.div.classList.add 'highlight' + entry.div ?= + document.createElement 'div' + ||> .classList.add 'highlight' // @ts-expect-error What do you mean style is readonly - entry.div.style = `--highlight-color: ${entry.color};` - entry.div + ||> .style = `--highlight-color: ${entry.color};` allColors() @entries.map .color @@ -58,8 +57,7 @@ class Colors replacement := @entries.find not &.threadNumber? return false unless replacement? entry.div?.style.setProperty '--highlight-color', replacement.color - replacement.div = entry.div - replacement.threadNumber = entry.threadNumber + replacement{div,threadNumber} = entry @entries.splice idx, 1 true diff --git a/wasm/in.civet b/wasm/in.civet index 5f70937..00f7036 100644 --- a/wasm/in.civet +++ b/wasm/in.civet @@ -18,15 +18,16 @@ declare global PAUSE_TEXT: string WORKER_URL: string get isPaused(): boolean + playPause: => worker: Worker | null .= null stdoutBuffer: number[] .= [] stderrBuffer: number[] .= [] threads: Thread[] .= [] -let resolve: () => void +let resolve: => delay .= 500 interval .= -1 -let step: () => void +let step: => decoder := new TextDecoder colors := new Colors @@ -52,7 +53,7 @@ generateContracted := => temp := Math.ceil Math.sqrt(2 * programLength) - 1.5 minLength := 1 + temp * (temp + 1) / 2 if programLength > minLength - programText = programText.replace new RegExp(`\\.{0,${programLength - minLength}}$`), '' + programText |>= .replace new RegExp(`\\.{0,${programLength - minLength}}$`), '' programText.replace /^#!/u, '#\n!' contractInput := :void => @@ -76,7 +77,7 @@ generateURL := :void => copyURL := :void => url := elements.urlOut.textContent! - (elements.urlOut.setSelectionRange as (undefined | HTMLInputElement['setSelectionRange']))? 0, url.length + (elements.urlOut.setSelectionRange as HTMLInputElement.'setSelectionRange'?)? 0, url.length navigator.clipboard.writeText url elements.copyAlert.className = '' setTimeout :void -> elements.copyAlert.className = 'hide-slow' @@ -161,7 +162,7 @@ createWorker := (name: string) => => [3, [threadNum, thread]] threads[threadNum] = thread try - if threadCount is threads.reduce (x) => x + 1, 0 + if threadCount is threads.reduce (1+), 0 renderThreads() else step() @@ -212,8 +213,7 @@ play := :void => Object.defineProperty window, 'isPaused', get: -> interval is -1 -playPause := :void => - if window.isPaused then play() else pause() +window.playPause = => if window.isPaused then play() else pause() faster := :void => clearInterval interval @@ -229,7 +229,7 @@ slower := :void => elements.faster.disabled = false elements.slower.disabled = delay >= maxDelay -debugProgram := :Promise => +debugProgram := => await expandBase() elements.debugProgram.hidden = false @@ -255,7 +255,7 @@ debugProgram := :Promise => elements.debugInfo.hidden = false if not elements.playPause.style?.width elements.playPause.textContent = window.PAUSE_TEXT - setTimeout :void => + setTimeout => elements.playPause.style.width = `${elements.playPause.offsetWidth}px` pause() @@ -268,22 +268,21 @@ debugProgram := :Promise => elements.program.hidden = false pause() -renderThreads := :void => +renderThreads := => elements.threads.innerHTML = '' threads.forEach (thread, idx): void => row := document.createElement 'tr' highlightDiv := colors.getHighlightDiv(idx) ?? throw new TypeError( "You haven't selected enough colors for that many threads!" ) - color := highlightDiv.style.getPropertyValue '--highlight-color' - row.style.backgroundColor = color + row.style.backgroundColor = highlightDiv.style.getPropertyValue '--highlight-color' highlightIndex thread.x, thread.y, highlightDiv - threadIdEl := document.createElement 'td' - threadIdEl.textContent = String idx - row.appendChild threadIdEl - threadContentsEl := document.createElement 'td' - threadContentsEl.textContent = thread.stack.join ',\u2009' - row.appendChild threadContentsEl + document.createElement 'td' + ||> .textContent = String idx + |> row.appendChild + document.createElement 'td' + ||> .textContent = thread.stack.join ',\u2009' + |> row.appendChild elements.threads.appendChild row changeHighlightColor := (before: string | number, after: string) => @@ -319,9 +318,7 @@ hideUrl := :void => elements.urlButton.onclick = generateURL elements.program.addEventListener 'input', hideUrl, { +passive } elements.includeInput.addEventListener 'change', hideUrl, { +passive } -elements.stdin.addEventListener 'change', :void => - hideUrl() if elements.includeInput.checked -, { +passive } +elements.stdin.addEventListener 'change', => hideUrl() if elements.includeInput.checked, { +passive } toHex := (rgba: string) => return rgba if /^#[0-9a-d]{6}$/i.test rgba @@ -335,54 +332,60 @@ toHex := (rgba: string) => .join '' createColorDiv := (color: string, i: number) => - input := document.createElement 'input' - input.type = 'color' - input.value = toHex color - input.addEventListener 'change', :void -> - unless changeHighlightColor i, @value - @value = toHex color - elements.stderr.innerText += "You can't have two threads of the same color!\n" - , { +passive } - - removeButton := document.createElement 'button' div := document.createElement 'div' - div.appendChild input - div.appendChild removeButton - - removeButton.type = 'button' - removeButton.title = 'remove color' - removeButton.addEventListener 'click', :void => - if colors.removeColor i - updateColorPicker() - renderThreads() - , { +passive } - removeButton.appendChild document.createTextNode 'x' + + document.createElement 'input' + ||> .type = 'color' + ||> .value = toHex color + ||> .addEventListener + 'change' + :void -> + unless changeHighlightColor i, @value + @value = toHex color + elements.stderr.innerText += "You can't have two threads of the same color!\n" + { +passive } + |> div.appendChild + + document.createElement 'button' + ||> .type = 'button' + ||> .title = 'remove color' + ||> .addEventListener + 'click' + :void => + if colors.removeColor i + updateColorPicker() + renderThreads() + { +passive } + ||> .appendChild document.createTextNode 'x' + |> div.appendChild + div updateColorPicker := :void => elements.colorPicker.innerHTML = '' - colors.allColors().forEach (color, i): void => - div := createColorDiv color, i - elements.colorPicker.appendChild div - - if not elements.addColorButton? - button := document.createElement 'button' - button.id = 'add-color-button' - button.type = 'button' - button.title = 'add color' - button.appendChild document.createTextNode '+' - button.addEventListener 'click', :void -> - let color: string - loop - red := Math.round 255 * Math.random() - green := Math.round 255 * Math.random() - blue := Math.round 255 * Math.random() - color = `rgb(${red}, ${green}, ${blue})` - break if colors.addColor color - elements.colorPicker.insertBefore createColorDiv(color, colors.count() - 1), @ - , { +passive } - // @ts-expect-error shut up - elements.addColorButton = button + colors.allColors().forEach (color, i): void -> + elements.colorPicker.appendChild createColorDiv color, i + + elements.addColorButton ?= + document.createElement 'button' + ||> .id = 'add-color-button' + ||> .type = 'button' + ||> .title = 'add color' + ||> .appendChild document.createTextNode '+' + ||> .addEventListener + 'click' + :void -> + let color: string + loop + red := Math.round 255 * Math.random() + green := Math.round 255 * Math.random() + blue := Math.round 255 * Math.random() + color = `rgb(${red}, ${green}, ${blue})` + break if colors.addColor color + elements.colorPicker.insertBefore createColorDiv(color, colors.count() - 1), @ + { +passive } + // https://github.com/DanielXMoore/Civet/issues/959 + |> (x) => x as any elements.colorPicker.appendChild elements.addColorButton toggleColorPicker := :void => @@ -419,7 +422,7 @@ elements.debugHeader.addEventListener 'mousedown', (e): void => document.onmouseup = :void -> @onmousemove = null @onmouseup = null -elements.debugHeader.addEventListener 'touchstart', (e): void=> +elements.debugHeader.addEventListener 'touchstart', (e): void => if not isHeader e.target return e.preventDefault() @@ -448,7 +451,7 @@ do elements.urlButton.onclick = generateURL if location.hash.length > 1 - elements.program.value = decodeURIComponent location.hash.slice 1 + elements.program.value = decodeURIComponent location.hash[1...] params := new URLSearchParams location.search inputParam := params.get 'i' diff --git a/wasm/library.civet b/wasm/library.civet index 9f94713..53dd285 100644 --- a/wasm/library.civet +++ b/wasm/library.civet @@ -14,7 +14,7 @@ mergeInto LibraryManager.library, postMessage [3, [thread_number, { 'x': x, 'y': y, - 'stack': Array::map.call HEAP32.slice(stack >> 2, (stack >> 2) + stack_depth), (x) -> x << 8 >> 8 + 'stack': Array::map.call HEAP32[stack >> 2...(stack >> 2) + stack_depth], (x) -> x << 8 >> 8 }]] new Promise (r) => @['__trilangle_resolve'] = r diff --git a/wasm/package-lock.json b/wasm/package-lock.json index a45114c..7ffc38b 100644 --- a/wasm/package-lock.json +++ b/wasm/package-lock.json @@ -5,7 +5,7 @@ "packages": { "": { "dependencies": { - "@danielx/civet": "~0.6.68", + "@danielx/civet": "~0.6.69", "sass": "^1.64.1", "terser": "^5.19.2" }, @@ -174,9 +174,9 @@ } }, "node_modules/@danielx/civet": { - "version": "0.6.68", - "resolved": "https://registry.npmjs.org/@danielx/civet/-/civet-0.6.68.tgz", - "integrity": "sha512-CiP0mhqX0G+S+lg6butqs0SOHhJNrjWPtfuJPVHC4vvFIn3jGDgb0b5qaFopvFvLFTSnjnMyAlTzFq46Tb+G8g==", + "version": "0.6.69", + "resolved": "https://registry.npmjs.org/@danielx/civet/-/civet-0.6.69.tgz", + "integrity": "sha512-eY5aDEg+tAgk9sZjGv7TSxNcm1rUdPBZYGFu7G/fN9xsHeT1SKv2dRdKG2aUJxd753f60luF4KFu7rFBLUEuvg==", "dependencies": { "@cspotcode/source-map-support": "^0.8.1", "@typescript/vfs": "^1.5.0", @@ -4299,9 +4299,9 @@ "requires": {} }, "@danielx/civet": { - "version": "0.6.68", - "resolved": "https://registry.npmjs.org/@danielx/civet/-/civet-0.6.68.tgz", - "integrity": "sha512-CiP0mhqX0G+S+lg6butqs0SOHhJNrjWPtfuJPVHC4vvFIn3jGDgb0b5qaFopvFvLFTSnjnMyAlTzFq46Tb+G8g==", + "version": "0.6.69", + "resolved": "https://registry.npmjs.org/@danielx/civet/-/civet-0.6.69.tgz", + "integrity": "sha512-eY5aDEg+tAgk9sZjGv7TSxNcm1rUdPBZYGFu7G/fN9xsHeT1SKv2dRdKG2aUJxd753f60luF4KFu7rFBLUEuvg==", "requires": { "@cspotcode/source-map-support": "^0.8.1", "@typescript/vfs": "^1.5.0", diff --git a/wasm/package.json b/wasm/package.json index 4f44178..f75d8d8 100644 --- a/wasm/package.json +++ b/wasm/package.json @@ -1,6 +1,6 @@ { "dependencies": { - "@danielx/civet": "~0.6.68", + "@danielx/civet": "~0.6.69", "sass": "^1.64.1", "terser": "^5.19.2" }, diff --git a/wasm/worker.civet b/wasm/worker.civet index c62e71b..e1726ef 100644 --- a/wasm/worker.civet +++ b/wasm/worker.civet @@ -1,7 +1,7 @@ -declare class ExitStatus extends Error {} +declare class ExitStatus < Error declare var Module: - preInit: () => void - onRuntimeInitialized: () => void + preInit: => + onRuntimeInitialized: => noExitRuntime: boolean ccall( func: string, @@ -17,13 +17,13 @@ declare var FS: stderr: (char: number | null) => void ): void -inputIndex .= 0 +let inputIndex: number ready .= false let stdinBuffer: Uint8Array programText .= '' -let funcName: string | undefined +let funcName?: string encoder := new TextEncoder -signals := new Map void> +signals := new Map> halfReady := (arg?: string): void => funcName = arg ?? funcName @@ -45,7 +45,7 @@ Module['preInit'] = -> Module['onRuntimeInitialized'] = halfReady Module['noExitRuntime'] = true -callInterpreter := (warnings: 0 | 1, disassemble: 0 | 1, expand: 0 | 1) => :Promise => +callInterpreter := (warnings: 0 | 1, disassemble: 0 | 1, expand: 0 | 1) => => inputIndex = 0 try await Module['ccall'] 'wasm_entrypoint', @@ -65,8 +65,8 @@ signals.set 'debugProgram', callInterpreter 0, 0, 1 // @ts-expect-error __trilangle_resolve is set in library.civet signals.set 'step', => @['__trilangle_resolve']() -@onmessage = (event: MessageEvent<[string, string, string]>) -> +@onmessage = (event: MessageEvent<[string] | [string, string, string]>) -> if event.data.length > 1 stdinBuffer = encoder.encode event.data.2 - programText = event.data.1 + programText = event.data.1! halfReady event.data.0