2023-01-26 15:14:37 -06:00
|
|
|
import './snippet_emulator.css'
|
|
|
|
|
import React, {useState, useCallback, useEffect} from 'react'
|
2023-01-27 01:25:01 -06:00
|
|
|
import {WasmCPU, assemble_snippet, source_map, NovaForth} from '../pkg/vweb.js'
|
2023-01-26 15:14:37 -06:00
|
|
|
|
2023-01-26 22:50:24 -06:00
|
|
|
export default function({ children }) {
|
|
|
|
|
if (React.Children.count(children) !== 1) {
|
|
|
|
|
throw 'Expects a single text node as a child'
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-26 15:14:37 -06:00
|
|
|
const [editing, setEditing] = useState(false) // Whether we're editing the snippet or running it
|
2023-01-26 22:50:24 -06:00
|
|
|
const [src, setSrc] = useState('') // The source code currently set
|
2023-01-26 15:14:37 -06:00
|
|
|
const [message, setMessage] = useState('') // A status / error message
|
|
|
|
|
const onChangeSrc = useCallback((e) => setSrc(e.target.value), []) // Callback for the textarea
|
|
|
|
|
const [binary, setBinary] = useState(null) // The assembled binary
|
2023-01-27 01:25:01 -06:00
|
|
|
const [sourceMap, setSourceMap] = useState(null) // A map from byte address to line number
|
|
|
|
|
const [cpu, setCpu] = useState(new WasmCPU()) // The actual CPU emulator itself
|
|
|
|
|
const [activeLine, setActiveLine] = useState(null) // The (0-based) index of the
|
|
|
|
|
|
|
|
|
|
const updateActiveLine = useCallback(() => {
|
|
|
|
|
sourceMap && setActiveLine(sourceMap.get(cpu.pc()))
|
|
|
|
|
}, [cpu, sourceMap])
|
|
|
|
|
|
|
|
|
|
const reset = useCallback(() => {
|
|
|
|
|
cpu.reset()
|
|
|
|
|
cpu.start()
|
|
|
|
|
updateActiveLine()
|
|
|
|
|
}, [cpu, updateActiveLine])
|
2023-01-26 15:14:37 -06:00
|
|
|
|
|
|
|
|
// A callback to build the binary from source
|
|
|
|
|
const rebuild = useCallback((code) => {
|
|
|
|
|
try {
|
|
|
|
|
const bin = assemble_snippet(code)
|
|
|
|
|
const sm = source_map(code)
|
|
|
|
|
setBinary(bin)
|
2023-01-27 01:25:01 -06:00
|
|
|
setSourceMap(sm.Ok || {})
|
2023-01-26 15:14:37 -06:00
|
|
|
setMessage(`Assembled ${bin.length} bytes`)
|
|
|
|
|
return true
|
|
|
|
|
} catch(e) {
|
|
|
|
|
setMessage(e.message)
|
|
|
|
|
}
|
|
|
|
|
}, [])
|
|
|
|
|
|
2023-01-27 01:25:01 -06:00
|
|
|
const step = useCallback(() => {
|
|
|
|
|
cpu.tick()
|
|
|
|
|
updateActiveLine()
|
|
|
|
|
}, [cpu, updateActiveLine])
|
|
|
|
|
|
2023-01-26 22:50:24 -06:00
|
|
|
// On load, clean the source and build the stuff
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const lines = React.Children.toArray(children)[0].split('\n')
|
|
|
|
|
const cleaned = lines.map(l => l.trim()).join('\n')
|
|
|
|
|
setSrc(cleaned)
|
|
|
|
|
rebuild(cleaned)
|
2023-01-27 01:25:01 -06:00
|
|
|
}, [children])
|
|
|
|
|
|
|
|
|
|
// Load and reset the CPU when the assembled binary changes
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (binary) {
|
|
|
|
|
cpu.load(binary)
|
|
|
|
|
reset()
|
|
|
|
|
}
|
|
|
|
|
}, [cpu, binary, reset])
|
2023-01-26 15:14:37 -06:00
|
|
|
|
|
|
|
|
if (editing) {
|
|
|
|
|
return (
|
|
|
|
|
<div className='snippetEmulator'>
|
|
|
|
|
<textarea value={src} onChange={onChangeSrc}></textarea>
|
|
|
|
|
<div className='message'>{message}</div>
|
|
|
|
|
<div className='buttons'>
|
|
|
|
|
<a className='build' onClick={() => { rebuild(src) && setEditing(false) }}>[Build]</a>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
return (
|
|
|
|
|
<div className='snippetEmulator'>
|
2023-01-27 01:25:01 -06:00
|
|
|
<SourceDisplay src={src} activeLine={activeLine}/>
|
2023-01-26 15:14:37 -06:00
|
|
|
<div className='message'>{message}</div>
|
|
|
|
|
<div className='buttons'>
|
2023-01-27 01:25:01 -06:00
|
|
|
<a className='step' onClick={step}>[Step]</a>
|
2023-01-26 15:14:37 -06:00
|
|
|
<a className='step'>[Run]</a>
|
2023-01-27 01:25:01 -06:00
|
|
|
<a className='reset' onClick={reset}>[Reset]</a>
|
2023-01-26 15:14:37 -06:00
|
|
|
<a className='edit' onClick={() => { setEditing(true); setMessage('') }}>[Edit]</a>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-27 01:25:01 -06:00
|
|
|
function SourceDisplay({ src, activeLine }) {
|
2023-01-26 15:14:37 -06:00
|
|
|
const lines = src.split('\n')
|
|
|
|
|
const lineDivs = lines.map((line, i) => {
|
2023-01-27 01:25:01 -06:00
|
|
|
// Loop indices are from 0, activeLine numbers are from 1
|
|
|
|
|
return (<div key={`line_${i}`} className={i + 1 === activeLine ? 'highlight' : ''}>{line || <> </>}</div>)
|
2023-01-26 15:14:37 -06:00
|
|
|
})
|
|
|
|
|
return (
|
|
|
|
|
<div className='src'>{lineDivs}</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function StackDisplay({ data, call }) {
|
|
|
|
|
|
|
|
|
|
}
|