commit 955bccb6ff0ddac5f00c120eaaf24f101dc9b6e2
parent 68e023df044ff14fac6599209d31591c10480dc8
Author: binarycat <binarycat@envs.net>
Date: Fri, 24 Jun 2022 19:20:56 -0400
Autoloading
I put together some autoloading functionality, now the system keeps track of what files are
loaded and can check against those to conditionally load files.
I put the main bit of code in lib/autoload.fs as it currently depends on s= from lib/str.fs
This maybe should be moved into core so you don't have to worry about trying to load the autoloader,
but that would require moving s= with it.
Committer note: adjusted slightly. autoloading is now directly in core.
Diffstat:
3 files changed, 25 insertions(+), 3 deletions(-)
diff --git a/boot.fs b/boot.fs
@@ -37,9 +37,11 @@ create _ $100 allot
: tocstr ( str -- a ) c@+ >r _ r@ move 0 _ r> + c! _ ;
: fclose ( fd -- ) 6 ( close ) swap 0 0 ( close fd 0 0 ) lnxcall drop ;
create _ 'C' c, 'a' c, 'n' c, ''' c, 't' c, $20 c, 'o' c, 'p' c, 'e' c, 'n' c,
-: fopen ( fname -- fd )
- tocstr 5 ( open ) swap 0 0 ( open cstr noflag O_RDONLY ) lnxcall
+: zfopen ( zfname -- fd )
+ 5 ( open ) swap 0 0 ( open cstr noflag O_RDONLY ) lnxcall
dup 0< if _ 10 rtype abort then ;
+: fopen ( fname -- fd )
+ tocstr zfopen ;
create _ 1 allot
: fread ( fd -- c-or-0 ) 3 ( read ) swap _ 1 lnxcall 1 = if _ c@ else 0 then ;
@@ -53,5 +55,8 @@ _fds value 'curfd
: fin< f< ?dup not if ( EOF )
fd~ fd@ not if ['] iin< to in< then $20 then
fecho if dup emit then ;
-: f<< word fopen >fd ['] fin< to in< ;
+0 value floaded
+: fload here >r floaded , dup c@ 1+ move, 0 c,
+ r> dup to floaded 5 + zfopen >fd ['] fin< to in< ;
+: f<< word fload ;
f<< init.fs
diff --git a/fs/lib/core.fs b/fs/lib/core.fs
@@ -86,6 +86,18 @@ create _ ," 0123456789abcdef"
: .x2 dup 8 rshift .x1 .x1 ;
: .x dup 16 rshift .x2 .x2 ;
+\ Autoloading
+\ entries in the floaded list have both a length byte and
+\ a nul termination byte. the nul termination is used only
+\ by the linux syscall, and may be removed in the future.
+
+: floaded? ( str -- f )
+ floaded begin dup while 2dup 4 +
+ 2dup nl> stype nl> stype nl>
+ s= if 2drop 1 exit then @ repeat 2drop 0 ;
+: .floaded floaded begin dup while dup 4 + stype nl> @ repeat drop ;
+: ?f<< word dup floaded? if drop else fload then ;
+
\ Diagnostic
: psdump scnt not if exit then
scnt >A begin dup .x spc> >r scnt not until
diff --git a/fs/tests/lib/core.fs b/fs/tests/lib/core.fs
@@ -39,4 +39,9 @@ bar 102 #eq
: baz ;
' baz prevword ' bar #eq
+\ autoloading
+floaded #
+
+S" some_nonexistant_file" floaded? not #
+S" lib/core.fs" floaded? #
testend