/* Copyright 2015-2024 * Kaz Kylheku * Vancouver, Canada * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include "config.h" #include "lib.h" #include "eval.h" #include "stream.h" #include "hash.h" #include "gc.h" #include "debug.h" #include "txr.h" #include "autoload.h" int opt_dbg_autoload; val trace_loaded; static val autoload_hash[al_max + 1]; static val autoload_reg_hash; static void autload_set_impl(al_ns_t ns, val *name, val fun, val package) { for (; *name; name++) { val sym = intern(*name, package); if (fun) sethash(autoload_hash[ns], sym, fun); else remhash(autoload_hash[ns], sym); } } void autoload_set(al_ns_t ns, val *name, val fun) { autload_set_impl(ns, name, fun, user_package); } static void autoload_sys_set(al_ns_t ns, val *name, val fun) { autload_set_impl(ns, name, fun, system_package); } static void autoload_key_set(al_ns_t ns, val *name, val fun) { autload_set_impl(ns, name, fun, keyword_package); } static void intern_only(val *name) { for (; *name; name++) intern(*name, user_package); } static val place_set_entries(val fun) { val sys_name[] = { lit("get-fun-getter-setter"), lit("get-mb"), lit("get-vb"), lit("register-simple-accessor"), nil }; val vname[] = { lit("*place-clobber-expander*"), lit("*place-update-expander*"), lit("*place-delete-expander*"), lit("*place-macro*"), nil }; val name[] = { lit("get-update-expander"), lit("get-clobber-expander"), lit("get-delete-expander"), lit("place-form-p"), lit("rlet"), lit("slet"), lit("alet"), lit("with-gensyms"), lit("call-update-expander"), lit("call-clobber-expander"), lit("call-delete-expander"), lit("with-update-expander"), lit("with-clobber-expander"), lit("with-delete-expander"), lit("set"), lit("pset"), lit("zap"), lit("flip"), lit("inc"), lit("dec"), lit("pinc"), lit("pdec"), lit("push"), lit("pop"), lit("swap"), lit("shift"), lit("rotate"), lit("test-set"), lit("test-clear"), lit("compare-swap"), lit("test-inc"), lit("test-dec"), lit("pushnew"), lit("del"), lit("lset"), lit("upd"), lit("ensure"), lit("defplace"), lit("define-place-macro"), lit("define-modify-macro"), lit("placelet"), lit("placelet*"), lit("read-once"), lit("define-accessor"), lit("with-slots"), lit("macroexpand-place"), lit("macroexpand-1-place"), nil }; autoload_sys_set(al_fun, sys_name, fun); autoload_set(al_var, vname, fun); autoload_set(al_fun, name, fun); return nil; } static val place_instantiate(void) { load(scat2(stdlib_path, lit("place"))); return nil; } static val ver_set_entries(val fun) { val vname[] = { lit("*lib-version*"), lit("lib-version"), nil }; autoload_set(al_var, vname, fun); return nil; } static val ver_instantiate(void) { load(scat2(stdlib_path, lit("ver"))); return nil; } static val ifa_set_entries(val fun) { val name[] = { lit("ifa"), lit("whena"), lit("conda"), lit("condlet"), lit("it"), nil }; autoload_set(al_fun, name, fun); return nil; } static val ifa_instantiate(void) { load(scat2(stdlib_path, lit("ifa"))); return nil; } static val txr_case_set_entries(val fun) { val name[] = { lit("txr-if"), lit("txr-when"), lit("txr-case"), nil }; autoload_set(al_fun, name, fun); return nil; } static val txr_case_instantiate(void) { load(scat2(stdlib_path, lit("txr-case"))); return nil; } static val with_resources_set_entries(val fun) { val name[] = { lit("with-resources"), lit("with-objects"), nil }; autoload_set(al_fun, name, fun); return nil; } static val with_resources_instantiate(void) { load(scat2(stdlib_path, lit("with-resources"))); return nil; } static val path_test_set_entries(val fun) { val name[] = { lit("path-exists-p"), lit("path-file-p"), lit("path-dir-p"), lit("path-symlink-p"), lit("path-blkdev-p"), lit("path-chrdev-p"), lit("path-sock-p"), lit("path-pipe-p"), lit("path-pipe-p"), lit("path-setgid-p"), lit("path-setuid-p"), lit("path-sticky-p"), lit("path-mine-p"), lit("path-my-group-p"), lit("path-executable-to-me-p"), lit("path-writable-to-me-p"), lit("path-readable-to-me-p"), lit("path-read-writable-to-me-p"), lit("path-safe-sticky-dir"), lit("path-components-safe"), lit("path-newer"), lit("path-older"), lit("path-same-object"), lit("path-private-to-me-p"), lit("path-strictly-private-to-me-p"), lit("path-dir-empty"), lit("rel-path"), lit("path-equal"), nil }; autoload_set(al_fun, name, fun); return nil; } static val path_test_instantiate(void) { load(scat2(stdlib_path, lit("path-test"))); return nil; } static val struct_set_entries(val fun) { val sys_name[] = { lit("define-method"), lit("rslotset"), nil }; val name[] = { lit("defstruct"), lit("qref"), lit("uref"), lit("new"), lit("lnew"), lit("new*"), lit("lnew*"), lit("meth"), lit("umeth"), lit("usl"), lit("defmeth"), lit("rslot"), lit("define-struct-prelude"), lit("define-struct-clause"), lit("macroexpand-struct-clause"), nil }; val vname[] = { lit("*struct-clause-expander*"), nil }; autoload_sys_set(al_fun, sys_name, fun); autoload_set(al_fun, name, fun); autoload_set(al_var, vname, fun); if (fun) sethash(autoload_hash[al_fun], struct_lit_s, fun); else remhash(autoload_hash[al_fun], struct_lit_s); return nil; } static val struct_instantiate(void) { load(scat2(stdlib_path, lit("struct"))); return nil; } static val with_stream_set_entries(val fun) { val name[] = { lit("with-out-string-stream"), lit("with-out-strlist-stream"), lit("with-out-buf-stream"), lit("with-in-string-stream"), lit("with-in-string-byte-stream"), lit("with-in-buf-stream"), lit("with-stream"), nil }; autoload_set(al_fun, name, fun); return nil; } static val with_stream_instantiate(void) { load(scat2(stdlib_path, lit("with-stream"))); return nil; } static val hash_set_entries(val fun) { val name[] = { lit("with-hash-iter"), nil }; autoload_set(al_fun, name, fun); return nil; } static val hash_instantiate(void) { load(scat2(stdlib_path, lit("hash"))); return nil; } static val except_set_entries(val fun) { val name[] = { lit("catch"), lit("catch*"), lit("catch**"), lit("handle"), lit("handle*"), lit("ignwarn"), lit("macro-time-ignwarn"), nil }; autoload_set(al_fun, name, fun); return nil; } static val except_instantiate(void) { load(scat2(stdlib_path, lit("except"))); return nil; } static val type_set_entries(val fun) { val name[] = { lit("typecase"), lit("etypecase"), nil }; autoload_set(al_fun, name, fun); return nil; } static val type_instantiate(void) { load(scat2(stdlib_path, lit("type"))); return nil; } static val yield_set_entries(val fun) { val sys_name[] = { lit("obtain-impl"), nil }; val name[] = { lit("obtain"), lit("obtain-block"), lit("yield-from"), lit("yield"), lit("obtain*"), lit("obtain*-block"), lit("suspend"), lit("hlet"), lit("hlet*"), nil }; autoload_sys_set(al_fun, sys_name, fun); autoload_set(al_fun, name, fun); return nil; } static val yield_instantiate(void) { load(scat2(stdlib_path, lit("yield"))); return nil; } static val awk_set_entries(val fun) { val sys_sname[] = { lit("awk-state"), nil }; val name[] = { lit("awk"), nil }; val name_noload[] = { lit("rec"), lit("orec"), lit("f"), lit("nf"), lit("nr"), lit("fnr"), lit("arg"), lit("fname"), lit("rs"), lit("krs"), lit("fs"), lit("ft"), lit("fw"), lit("kfs"), lit("ofs"), lit("ors"), lit("next"), lit("again"), lit("next-file"), lit("rng"), lit("-rng"), lit("rng-"), lit("-rng-"), lit("--rng"), lit("--rng-"), lit("rng+"), lit("-rng+"), lit("--rng+"), lit("ff"), lit("f"), lit("mf"), lit("fconv"), lit("->"), lit("->>"), lit("<-"), lit("!>"), lit("= 185) autoload_reg(op_instantiate, op_set_entries); autoload_reg(save_exe_instantiate, save_exe_set_entries); autoload_reg(defset_instantiate, defset_set_entries); autoload_reg(copy_file_instantiate, copy_file_set_entries); autoload_reg(arith_each_instantiate, arith_each_set_entries); autoload_reg(each_prod_instantiate, each_prod_set_entries); autoload_reg(quips_instantiate, quips_set_entries); autoload_reg(match_instantiate, match_set_entries); autoload_reg(doc_instantiate, doc_set_entries); autoload_reg(pic_instantiate, pic_set_entries); autoload_reg(constfun_instantiate, constfun_set_entries); autoload_reg(expander_let_instantiate, expander_let_set_entries); autoload_reg(load_args_instantiate, load_args_set_entries); autoload_reg(csort_instantiate, csort_set_entries); autoload_reg(glob_instantiate, glob_set_entries); reg_fun(intern(lit("autoload-try-fun"), system_package), func_n1(autoload_try_fun)); } static val autoload_try(al_ns_t ns, val sym) { val fun = gethash(autoload_hash[ns], sym); if (fun) { val check = gethash(autoload_reg_hash, fun); if (check) { unsigned ds = debug_clear(opt_dbg_autoload ? 0 : DBG_ENABLE); val saved_dyn_env = dyn_env; int saved_compat = opt_compat; remhash(autoload_reg_hash, fun); dyn_env = make_env(nil, nil, dyn_env); env_vbind(dyn_env, package_s, system_package); env_vbind(dyn_env, package_alist_s, packages); opt_compat = 0; funcall(fun); opt_compat = saved_compat; dyn_env = saved_dyn_env; debug_restore(ds); return t; } } return nil; } val autoload_try_fun(val sym) { return autoload_try(al_fun, sym); } val autoload_try_var(val sym) { return autoload_try(al_var, sym); } val autoload_try_fun_var(val sym) { uses_or2; return or2(autoload_try_fun(sym), autoload_try_var(sym)); } val autoload_try_slot(val sym) { return autoload_try(al_slot, sym); } val autoload_try_struct(val sym) { return autoload_try(al_struct, sym); } val autoload_try_keyword(val sym) { return autoload_try(al_key, sym); } void autoload_intern(val *namearray) { intern_only(namearray); }