From 26e993110b9b057ad1d40d852b6727af8715213f Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Mon, 18 Jul 2022 19:56:00 -0700 Subject: [PATCH] Move web platform system calls into a separate file. --- Makefile | 1 + web/fini.fs | 123 ------------------------------------------ web/platform.fs | 141 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+), 123 deletions(-) create mode 100644 web/platform.fs diff --git a/Makefile b/Makefile index f12e018..c47eb9f 100644 --- a/Makefile +++ b/Makefile @@ -276,6 +276,7 @@ $(GEN)/web_sys.js: $(GEN)/dump_web_opcodes | $(GEN) $< sys >$@ WEB_BOOT = $(COMMON_PHASE1e) \ + web/platform.fs \ $(COMMON_PHASE2) \ web/fini.fs $(GEN)/web_boot.js: tools/source_to_string.js $(WEB_BOOT) | $(GEN) diff --git a/web/fini.fs b/web/fini.fs index 97a23d7..dddaf66 100644 --- a/web/fini.fs +++ b/web/fini.fs @@ -18,129 +18,6 @@ transfer internals-builtins forth definitions internals ( Bring a forth to the top of the vocabulary. ) : ok ." uEforth" raw-ok ; - -: jseval! ( a n index -- ) 0 call ; - -r| -(function(sp) { - var n = i32[sp>>2]; sp -= 4; - var a = i32[sp>>2]; sp -= 4; - var text = GetString(a, n); - eval(text); - return sp; -}) -| 1 jseval! -: jseval ( a n -- ) 1 call ; - -r" - globalObj.inbuffer = []; - if (!globalObj['write']) { - var con = document.getElementById('console'); - if (con === null) { - con = document.createElement('pre'); - con.id = 'console'; - document.body.appendChild(con); - } - window.outbuffer = ''; - window.onkeypress = function(e) { - globalObj.inbuffer.push(e.keyCode); - }; - window.onkeydown = function(e) { - if (e.keyCode == 8) { - globalObj.inbuffer.push(e.keyCode); - } - }; - } -" jseval - -r| -(function(sp) { - var n = i32[sp>>2]; sp -= 4; - var a = i32[sp>>2]; sp -= 4; - if (globalObj['write']) { - var text = GetString(a, n); - write(text); - } else { - 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; -}) -| 2 jseval! -: web-type ( a n -- ) 2 call yield ; -' web-type is type - -r| -(function(sp) { - if (globalObj['readline'] && !globalObj.inbuffer.length) { - var line = readline(); - for (var i = 0; i < line.length; ++i) { - globalObj.inbuffer.push(line.charCodeAt(i)); - } - globalObj.inbuffer.push(13); - } - if (globalObj.inbuffer.length) { - sp += 4; i32[sp>>2] = globalObj.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) { - if (globalObj['readline']) { - return -1; - } - sp += 4; i32[sp>>2] = globalObj.inbuffer.length ? -1 : 0; - return sp; -}) -| 4 jseval! -: web-key? ( -- f ) yield 4 call ; -' web-key? is key? - -r| -(function(sp) { - var val = i32[sp>>2]; sp -= 4; - if (globalObj['quit']) { - quit(val); - } else { - Init(); - } - return sp; -}) -| 5 jseval! -: terminate ( n -- ) 5 call ; -: bye 0 terminate ; - -r| -(function(sp) { - if (globalObj['write']) { - sp += 4; i32[sp>>2] = 0; // Disable echo. - } else { - sp += 4; i32[sp>>2] = -1; // Enable echo. - } - return sp; -}) -| 6 jseval! 6 call echo ! - -: page 12 emit ; - transfer forth forth ok diff --git a/web/platform.fs b/web/platform.fs new file mode 100644 index 0000000..027dfc2 --- /dev/null +++ b/web/platform.fs @@ -0,0 +1,141 @@ +\ Copyright 2022 Bradley D. Nelson +\ +\ Licensed under the Apache License, Version 2.0 (the "License"); +\ you may not use this file except in compliance with the License. +\ You may obtain a copy of the License at +\ +\ http://www.apache.org/licenses/LICENSE-2.0 +\ +\ Unless required by applicable law or agreed to in writing, software +\ distributed under the License is distributed on an "AS IS" BASIS, +\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +\ See the License for the specific language governing permissions and +\ limitations under the License. + +vocabulary web web definitions + +: jseval! ( a n index -- ) 0 call ; + +r| +(function(sp) { + var n = i32[sp>>2]; sp -= 4; + var a = i32[sp>>2]; sp -= 4; + var text = GetString(a, n); + eval(text); + return sp; +}) +| 1 jseval! +: jseval ( a n -- ) 1 call ; + +r" + globalObj.inbuffer = []; + if (!globalObj['write']) { + var con = document.getElementById('console'); + if (con === null) { + con = document.createElement('pre'); + con.id = 'console'; + document.body.appendChild(con); + } + window.outbuffer = ''; + window.onkeypress = function(e) { + globalObj.inbuffer.push(e.keyCode); + }; + window.onkeydown = function(e) { + if (e.keyCode == 8) { + globalObj.inbuffer.push(e.keyCode); + } + }; + } +" jseval + +r| +(function(sp) { + var n = i32[sp>>2]; sp -= 4; + var a = i32[sp>>2]; sp -= 4; + if (globalObj['write']) { + var text = GetString(a, n); + write(text); + } else { + 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; +}) +| 2 jseval! +: web-type ( a n -- ) 2 call yield ; +' web-type is type + +r| +(function(sp) { + if (globalObj['readline'] && !globalObj.inbuffer.length) { + var line = readline(); + for (var i = 0; i < line.length; ++i) { + globalObj.inbuffer.push(line.charCodeAt(i)); + } + globalObj.inbuffer.push(13); + } + if (globalObj.inbuffer.length) { + sp += 4; i32[sp>>2] = globalObj.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) { + if (globalObj['readline']) { + return -1; + } + sp += 4; i32[sp>>2] = globalObj.inbuffer.length ? -1 : 0; + return sp; +}) +| 4 jseval! +: web-key? ( -- f ) yield 4 call ; +' web-key? is key? + +r| +(function(sp) { + var val = i32[sp>>2]; sp -= 4; + if (globalObj['quit']) { + quit(val); + } else { + Init(); + } + return sp; +}) +| 5 jseval! +: terminate ( n -- ) 5 call ; + +r| +(function(sp) { + if (globalObj['write']) { + sp += 4; i32[sp>>2] = 0; // Disable echo. + } else { + sp += 4; i32[sp>>2] = -1; // Enable echo. + } + return sp; +}) +| 6 jseval! 6 call echo ! + +forth definitions web + +: bye 0 terminate ; +: page 12 emit ; + +forth definitions