diff --git a/Nasal/globals.nas b/Nasal/globals.nas index a3f93e9af..96af408eb 100644 --- a/Nasal/globals.nas +++ b/Nasal/globals.nas @@ -110,3 +110,32 @@ printlog = func(level, args...) { } +## +# Load and execute ~/.fgfs/Nasal/*.nas files in alphabetic order +# after all $FG_ROOT/Nasal/*.nas files were loaded. +# +_setlistener("/sim/signals/nasal-dir-initialized", func { + var path = getprop("/sim/fg-home") ~ "/Nasal"; + if ((var dir = directory(path)) == nil) return; + foreach (var file; sort(dir, cmp)) { + if (substr(file, -4) != ".nas") continue; + + var module = substr(file, 0, size(file) - 4); + file = path ~ "/" ~ file; + printlog("info", ">>> executing local Nasal file ", file); + + if (!contains(globals, module)) var locals = globals[module] = {}; + elsif (typeof(globals[module]) == "hash") var locals = globals[module]; + else var locals = {}; + + var err = []; + var code = call(func { compile(io.readfile(file), file) }, nil, err); + if (size(err)) { + print(file ~ ": " ~ err[0]); + continue; + } + call(bind(code, globals), nil, nil, locals, err); + debug.printerror(err); + } +}); +