/* * cat/module.c -- Cat_Module implementation. * * Naming rule: symbols here are cat_module_* / CAT_MODULE_*. */ #include "cat/module.h" #include "cat/compiler.h" #include "cat/bytecode.h" #include "cat/vm.h" #include "cat/stdlib/stdlib.h" #include "cat/stdlib/build.h" #include "cat/types/table.h" #include "cat/types/ascii_text.h" #include #include CAT_MODULE_Module *cat_module_new(const char *name) { CAT_MODULE_Module *m = calloc(1, sizeof(*m)); if (m == NULL) return NULL; m->name = strdup(name ? name : "module"); return m; } int cat_module_compile(CAT_MODULE_Module *mod, const char *source, CAT_ERROR_Error *err) { mod->main = cat_compiler_compile(source, err); return mod->main ? 0 : -1; } int cat_module_run(CAT_MODULE_Module *mod, CAT_VALUE_Value **out, CAT_ERROR_Error *err) { CAT_VM_State *vm; int rc; if (mod->main == NULL) return (cat_error_raise(err, "module has no compiled code"), -1); vm = cat_vm_new(); if (vm == NULL) return -1; cat_stdlib_stdlib_install(vm); cat_stdlib_build_install(vm); rc = cat_vm_run(vm, mod->main, out); if (rc != 0) cat_error_raise(err, "%s", vm->err.message); cat_vm_free(vm); return rc; } void cat_module_free(CAT_MODULE_Module *mod) { if (mod == NULL) return; cat_bytecode_function_free(mod->main); free(mod->name); free(mod); } /* The directory part of a path (everything before the last '/'), or * "." when there is none. Returned in a static-per-call buffer owned * by the caller's `buf`. */ static void module_dir(const char *path, char *buf, size_t cap) { const char *slash; if (path == NULL || path[0] == '\0') { snprintf(buf, cap, "."); return; } slash = strrchr(path, '/'); if (slash == NULL) { snprintf(buf, cap, "."); return; } if (slash == path) snprintf(buf, cap, "/"); else snprintf(buf, cap, "%.*s", (int)(slash - path), path); } CAT_VALUE_Value *cat_module_table(const char *name, const char *path) { CAT_VALUE_Value *t = cat_types_table_new(); CAT_VALUE_Value *nv; CAT_ERROR_Error e; char dir[512]; cat_error_clear(&e); nv = cat_types_ascii_text_new(name ? name : "main"); cat_types_table_set(t, "name", nv, &e); cat_value_release(nv); /* `path`/`dir` reflect where this unit came from, when known. */ if (path != NULL && path[0] != '\0') { cat_error_clear(&e); nv = cat_types_ascii_text_new(path); cat_types_table_set(t, "path", nv, &e); cat_value_release(nv); module_dir(path, dir, sizeof(dir)); cat_error_clear(&e); nv = cat_types_ascii_text_new(dir); cat_types_table_set(t, "dir", nv, &e); cat_value_release(nv); } return t; } void cat_module_enter(CAT_VM_State *vm, const char *name, const char *path) { CAT_VALUE_Value *prev; if (vm == NULL) return; prev = cat_vm_get_global(vm, "module"); if (vm->nmodule < (int)(sizeof(vm->module_stack) / sizeof(vm->module_stack[0]))) { /* Keep the saved module alive while it is off the global. */ cat_value_retain(prev); vm->module_stack[vm->nmodule++] = prev; } cat_vm_set_global_owned(vm, "module", cat_module_table(name, path)); } void cat_module_leave(CAT_VM_State *vm) { CAT_VALUE_Value *prev; if (vm == NULL || vm->nmodule <= 0) return; prev = vm->module_stack[--vm->nmodule]; cat_vm_set_global(vm, "module", prev); cat_value_release(prev); /* drop the stack's reference */ }