From 5dbf1cf8d495cad76119dccdbaeba2af9b3eec67 Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Thu, 14 Jul 2022 16:08:35 -0700 Subject: [PATCH] Some keyboard input on web. --- web/fini.fs | 72 ++++++++++++++++++++++++++++++++++++--------- web/web.template.js | 9 +++--- 2 files changed, 63 insertions(+), 18 deletions(-) diff --git a/web/fini.fs b/web/fini.fs index 9ce5f7d..26265f7 100644 --- a/web/fini.fs +++ b/web/fini.fs @@ -29,35 +29,79 @@ r| eval(text); return sp; }) -| 2 jseval! -: jseval ( a n -- ) 2 call ; +| 1 jseval! +: jseval ( a n -- ) 1 call ; -r| +r" if (!globalObj['write']) { - var console = document.createElement('pre'); - console.id = 'console'; - document.body.appendChild(console); + var con = document.createElement('pre'); + con.id = 'console'; + document.body.appendChild(con); + window.inbuffer = []; + window.outbuffer = ''; + window.onkeypress = function(e) { + window.inbuffer.push(e.keyCode); + }; + window.onkeydown = function(e) { + if (e.keyCode == 8) { + window.inbuffer.push(e.keyCode); + } + }; } -| jseval +" jseval r| (function(sp) { var n = i32[sp>>2]; sp -= 4; var a = i32[sp>>2]; sp -= 4; - var text = GetString(a, n); if (globalObj['write']) { + var text = GetString(a, n); write(text); } else { - var console = document.getElementById('console'); - console.innerText += text.replace(/[\r]/g, ''); + var con = document.getElementById('console'); + for (var i = 0; i < n; ++i) { + var ch = u8[a + i]; + if (ch == 12) { + window.outbuffer = ''; + } else if (ch == 8) { + window.outbuffer = window.outbuffer.slice(0, -1); + } else if (ch == 13) { + } else { + window.outbuffer += String.fromCharCode(ch); + } + } + con.innerText = window.outbuffer + String.fromCharCode(0x2592); + window.scrollTo(0, document.body.scrollHeight); } return sp; }) -| 1 jseval! +| 2 jseval! +: web-type ( a n -- ) 2 call ; +' web-type is type -: web-type 1 call ; ' web-type is type -: web-key yield 0 ; ' web-key is key -: web-key? yield 0 ; ' web-key? is key? +r| +(function(sp) { + if (window.inbuffer.length) { + sp += 4; i32[sp>>2] = window.inbuffer.shift(); + } else { + sp += 4; i32[sp>>2] = 0; + } + return sp; +}) +| 3 jseval! +: web-key ( -- n ) begin yield 3 call dup if exit then drop again ; +' web-key is key + +r| +(function(sp) { + sp += 4; i32[sp>>2] = window.inbuffer.length ? -1 : 0; + return sp; +}) +| 4 jseval! +: web-key? ( -- f ) yield 4 call ; +' web-key? is key? + +: page 12 emit ; transfer forth forth diff --git a/web/web.template.js b/web/web.template.js index 2a0306d..70a7d97 100644 --- a/web/web.template.js +++ b/web/web.template.js @@ -358,7 +358,7 @@ function Evaluate1(rp) { fp += 4; f32[fp>>2] = f32[n>>2]; } } else { - console.log('CANT FIND: ' + GetString(name, len)); + if (DEBUGGING) { console.log('CANT FIND: ' + GetString(name, len)); } sp += 4; i32[sp>>2] = name; sp += 4; i32[sp>>2] = len; sp += 4; i32[sp>>2] = -1; @@ -575,9 +575,10 @@ var globalObj = getGlobalObj(); var module = VM(globalObj, ffi, heap); Init(); -setTimeout(function() { +function run() { module.run(); - console.log('yield'); -}, 10); + setTimeout(run, 1); +} +setTimeout(run, 1); })();