Skip to content

Commit

Permalink
More civet updates (and more issues!)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrk24 committed Feb 8, 2024
1 parent 2a36349 commit c153b31
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 92 deletions.
14 changes: 6 additions & 8 deletions wasm/Colors.civet
Original file line number Diff line number Diff line change
@@ -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)' },
Expand All @@ -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
Expand All @@ -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

Expand Down
135 changes: 69 additions & 66 deletions wasm/in.civet
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 =>
Expand All @@ -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'
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -229,7 +229,7 @@ slower := :void =>
elements.faster.disabled = false
elements.slower.disabled = delay >= maxDelay

debugProgram := :Promise<void> =>
debugProgram := =>
await expandBase()

elements.debugProgram.hidden = false
Expand All @@ -255,7 +255,7 @@ debugProgram := :Promise<void> =>
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()

Expand All @@ -268,22 +268,21 @@ debugProgram := :Promise<void> =>
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) =>
Expand Down Expand Up @@ -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
Expand All @@ -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 =>
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion wasm/library.civet
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 7 additions & 7 deletions wasm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dependencies": {
"@danielx/civet": "~0.6.68",
"@danielx/civet": "~0.6.69",
"sass": "^1.64.1",
"terser": "^5.19.2"
},
Expand Down
18 changes: 9 additions & 9 deletions wasm/worker.civet
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<string | undefined, () => void>
signals := new Map<string?, =>>

halfReady := (arg?: string): void =>
funcName = arg ?? funcName
Expand All @@ -45,7 +45,7 @@ Module['preInit'] = ->
Module['onRuntimeInitialized'] = halfReady
Module['noExitRuntime'] = true

callInterpreter := (warnings: 0 | 1, disassemble: 0 | 1, expand: 0 | 1) => :Promise<void> =>
callInterpreter := (warnings: 0 | 1, disassemble: 0 | 1, expand: 0 | 1) => =>
inputIndex = 0
try
await Module['ccall'] 'wasm_entrypoint',
Expand All @@ -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

0 comments on commit c153b31

Please sign in to comment.