- }
-})
-
-const channels = [{
- read: (writeAddr, maxBytes) => {
- const maxChars = maxBytes >> 1
- const bufView = new Uint16Array(wasmMem.buffer, writeAddr, maxChars)
- let i
- for (i = 0; i < maxChars && i < stdin.length; i++)
- bufView[i] = stdin.charCodeAt(i)
- stdin = stdin.substring(maxChars)
- return i << 1
- },
- write: (readAddr, maxBytes) =>
- output.print(String.fromCharCode.apply(
- null,
- new Uint16Array(wasmMem.buffer, readAddr, maxBytes)
- ))
-}]
-const simstack = []
-const rstack = []
-const dictionary = {
- ';': 1,
- 'LIT': 2,
- RINIT: 3,
- WORD: 696,
- KEY: 5,
- DUP: 6,
- '+': 7,
- 'NOOP2': 8,
- '.': 9,
- '@': 10,
- '!': 11,
- EXECUTE: 12,
- NOOP: 13,
- 'JZ:': 14,
- 'JNZ:': 15,
- DROP: 16,
- 'WS?': 17,
- 'JMP:': 18,
- 'WPUTC': 19,
- 'WB0': 20,
- 'FIND': 21,
- 'NUMBER': 22,
- 'W!LEN': 23,
- 'J-1:': 24,
- 'BYE': 25,
- 'SWAP': 26,
- 'WORDS': 27,
- 'HERE': 28,
- 'DEFINE': 29,
- ':': 900,
- 'MODE': 384,
- 'WBUF': 256,
- 'EXECUTE-MODE': 800,
- 'QUIT': 600,
- 'INTERPRET': 512
-}
-const wasmImport = {
- env: {
- pop: () => simstack.pop(),
- push: (val) => simstack.push(val),
- rinit: () => rstack.length = 0,
- rpop: () => rstack.pop(),
- rpush: (val) => rstack.push(val),
- sys_write: (channel, fromBuffer) => {
- if (channels[channel] === undefined)
- return
- const maxBytes = new DataView(
- wasmMem.buffer,
- fromBuffer,
- 4
- ).getUint32(0,true)
- console.log(`write ch:${channel} addr:${fromBuffer} len:${maxBytes}`)
- channels[channel].write(fromBuffer + 4, maxBytes)
- },
- sys_read: (channel, toBuffer) => {
- console.log(`read ch:${channel} buf:${toBuffer} current: ${stdin}`)
- const lenView = new DataView(wasmMem.buffer, toBuffer, 8)
- const maxBytes = lenView.getUint32(0,true)
- console.log(`read blen:${lenView.getUint32(0,true)} cstrlen:${lenView.getUint32(4,true)}`)
- /* If the channel is undefined, or if there isn't enough room in
- * toBuffer for even one character, then, if there is enough
- * room for an int write a zero, exit */
- if (channels[channel] === undefined || maxBytes < 6) {
- if (maxBytes >= 4)
- lenView.setUint32(4,0,true)
- }
- const numBytes = channels[channel].read(toBuffer + 8, maxBytes - 4)
- lenView.setUint32(4,numBytes,true);
- console.log(`read wrote ${lenView.getUint32(4,true)} bytes, remainder: ${stdin}`)
- console.log(`read blen:${lenView.getUint32(0,true)} cstrlen:${lenView.getUint32(4,true)}`) },
- sys_listen: (reqAddr, cbAddr) => {
- //TODO: call into the module to wasm fn "event" to push an event
- //to forth, which pushes esi to the return stack and queues the
- //callback function provided from listen. reqaddr could be
- //"fetch", in which case no channel is used. otherwise very
- //similar to sys_fetch.
- },
- sys_fetch: (channel, reqAddr) => {
- //TODO: map to fetch promise, write to channel buffer,
- //javascript "fetch" as fallback, explicit handles for
- //"textEntry" or any third party protocols like activitypub
- console.log(`fetch ${channel} ${reqAddr}`)