diff --git a/extlib/benz/include/picrin/lib.h b/extlib/benz/include/picrin/lib.h index c2d0b420..50cd45fe 100644 --- a/extlib/benz/include/picrin/lib.h +++ b/extlib/benz/include/picrin/lib.h @@ -16,6 +16,7 @@ struct pic_lib { struct pic_dict *exports; }; +#define pic_lib_p(o) (pic_type(o) == PIC_TT_LIB) #define pic_lib_ptr(o) ((struct pic_lib *)pic_ptr(o)) #if defined(__cplusplus) diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index 245e4780..cc1eb503 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -312,6 +312,60 @@ pic_lib_define_library(pic_state *pic) return pic_undef_value(); } +static pic_value +pic_lib_make_library(pic_state *pic) +{ + pic_value name; + + pic_get_args(pic, "o", &name); + + return pic_obj_value(pic_make_library(pic, name)); +} + +static pic_value +pic_lib_find_library(pic_state *pic) +{ + pic_value name; + struct pic_lib *lib; + + pic_get_args(pic, "o", &name); + + if ((lib = pic_find_library(pic, name)) == NULL) { + return pic_false_value(); + } + return pic_obj_value(lib); +} + +static pic_value +pic_lib_library_exports(pic_state *pic) +{ + pic_value lib, exports = pic_nil_value(); + pic_sym *sym; + xh_entry *it; + + pic_get_args(pic, "o", &lib); + + pic_assert_type(pic, lib, lib); + + pic_dict_for_each (sym, pic_lib_ptr(lib)->exports, it) { + pic_push(pic, pic_obj_value(sym), exports); + } + + return exports; +} + +static pic_value +pic_lib_library_environment(pic_state *pic) +{ + pic_value lib; + + pic_get_args(pic, "o", &lib); + + pic_assert_type(pic, lib, lib); + + return pic_obj_value(pic_lib_ptr(lib)->env); +} + void pic_init_lib(pic_state *pic) { @@ -321,4 +375,9 @@ pic_init_lib(pic_state *pic) pic_defmacro(pic, pic->sIMPORT, pic->uIMPORT, pic_lib_import); pic_defmacro(pic, pic->sEXPORT, pic->uEXPORT, pic_lib_export); pic_defmacro(pic, pic->sDEFINE_LIBRARY, pic->uDEFINE_LIBRARY, pic_lib_define_library); + + pic_defun(pic, "make-library", pic_lib_make_library); + pic_defun(pic, "find-library", pic_lib_find_library); + pic_defun(pic, "library-exports", pic_lib_library_exports); + pic_defun(pic, "library-environment", pic_lib_library_environment); } diff --git a/piclib/picrin/base.scm b/piclib/picrin/base.scm index 66ad69e5..edbac8c1 100644 --- a/piclib/picrin/base.scm +++ b/piclib/picrin/base.scm @@ -252,6 +252,11 @@ variable? variable=?) + (export make-library + find-library + library-exports + library-environment) + (export call-with-current-continuation call/cc dynamic-wind