From da324bdf703e94a0978ee75703fb2726d7eb2af1 Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Tue, 21 Feb 2023 21:08:03 -0800 Subject: [PATCH] Adding script load. Still need to enhance to support script src. --- Makefile | 9 ++++++++- web/fini.fs | 4 ++++ web/platform.fs | 15 +++++++++++++++ web/script_test.html | 26 ++++++++++++++++++++++++++ web/web.template.js | 19 +++++++++++++++++-- 5 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 web/script_test.html diff --git a/Makefile b/Makefile index ab100a4..4997bda 100644 --- a/Makefile +++ b/Makefile @@ -346,7 +346,11 @@ $(RES)/ueforth_res64.res: windows/ueforth.rc $(RES)/eforth.ico # ---- WEB ---- web: web_target web_tests -web_target: $(WEB)/terminal.html $(WEB)/lazy_terminal.html $(WEB)/ueforth.js +web_target: \ + $(WEB)/terminal.html \ + $(WEB)/lazy_terminal.html \ + $(WEB)/script_test.html \ + $(WEB)/ueforth.js $(WEB): mkdir -p $(WEB) @@ -357,6 +361,9 @@ $(WEB)/terminal.html: web/terminal.html | $(WEB) $(WEB)/lazy_terminal.html: web/lazy_terminal.html | $(WEB) cp $< $@ +$(WEB)/script_test.html: web/script_test.html | $(WEB) + cp $< $@ + $(WEB)/ueforth.js: \ web/fuse_web.js \ web/web.template.js \ diff --git a/web/fini.fs b/web/fini.fs index be391df..9ee7cb4 100644 --- a/web/fini.fs +++ b/web/fini.fs @@ -24,4 +24,8 @@ forth definitions internals : ok ." uEforth" raw-ok ; transfer forth forth + +( Try to run script tags if any. ) +web scripts scripts# forth evaluate + ok diff --git a/web/platform.fs b/web/platform.fs index 428e334..56798fa 100644 --- a/web/platform.fs +++ b/web/platform.fs @@ -791,6 +791,17 @@ JSWORD: release { handle } context.ReleaseHandle(handle); ~ +JSWORD: importScripts { dst dst_limit -- n } + if (context.scripts === undefined) { + return 0; + } + var data = context.scripts; + for (var i = 0; i < dst_limit && i < data.length; ++i) { + u8[dst + i] = data[i]; + } + return data.length; +~ + r~ context.audio_context = null; context.audio_channels = []; @@ -916,6 +927,10 @@ JSWORD: random { n -- n } return Math.floor(Math.random() * n); ~ +0 0 importScripts constant scripts# +create scripts scripts# allot +scripts scripts# importScripts drop + forth definitions web : ms-ticks ms-ticks ; diff --git a/web/script_test.html b/web/script_test.html new file mode 100644 index 0000000..e9e3b03 --- /dev/null +++ b/web/script_test.html @@ -0,0 +1,26 @@ + + + + + + diff --git a/web/web.template.js b/web/web.template.js index c526ca0..1476122 100644 --- a/web/web.template.js +++ b/web/web.template.js @@ -233,6 +233,21 @@ function Builtin(name, flags, vocab, opcode) { builtins.push([name, flags | BUILTIN_MARK, vocab, opcode]); } +function LoadScripts() { + if (globalObj.write) { + return; + } + var text = ''; + var tags = document.getElementsByTagName('script'); + for (var i = 0; i < tags.length; ++i) { + if (tags[i].type == 'text/forth') { + text += tags[i].text + '\n'; + } + } + var encoder = new TextEncoder(); + context.scripts = encoder.encode(text); +} + function SetupBuiltins() { for (var i = 0; i < builtins.length; ++i) { var name = builtins[i][0]; @@ -608,6 +623,7 @@ function run() { } function Start() { + LoadScripts(); Init(); setTimeout(run, 0); } @@ -620,8 +636,7 @@ if (globalObj.write) { context.Start = Start; } else { window.addEventListener('load', function() { - Init(); - setTimeout(run, 0); + Start(); }); } }