Adding script load.

Still need to enhance to support script src.
This commit is contained in:
Brad Nelson
2023-02-21 21:08:03 -08:00
parent d2c62a35b9
commit da324bdf70
5 changed files with 70 additions and 3 deletions

View File

@ -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 \

View File

@ -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

View File

@ -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 ;

26
web/script_test.html Normal file
View File

@ -0,0 +1,26 @@
<!DOCTYPE html>
<!--
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.
-->
<script type="text/forth">
: square dup * ;
4 square . cr
</script>
<script src="ueforth.js"></script>
<script type="text/forth">
colors
bye
</script>

View File

@ -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();
});
}
}