diff --git a/lang/module.c b/lang/module.c index 3e20057..3222bca 100644 --- a/lang/module.c +++ b/lang/module.c @@ -28,26 +28,14 @@ // Size of a stack in elemenets #define STACK_SIZE 64 +// Section 1: Module dependency resolution + // A stack of ModuleInfo pointers struct module_stack { const ModuleInfo *elems[STACK_SIZE]; int next; }; -// A stack of Objects -struct object_stack { - Object elems[STACK_SIZE]; - int next; -}; - -// A mapping mapping between ModuleInfo and Object -// Each stack element should have a corresponding element -// in the other stack at the same index -struct module_mapping { - struct module_stack infos; - struct object_stack objects; -}; - // Finds a ModuleInfo by name static const ModuleInfo *info_by_name(const char *name) { for (unsigned int i = 0; i < ARRAY_SIZE(modules_list); ++i) { @@ -114,6 +102,22 @@ // The traversal stack is now empty, all done! } +// Section 2: Runtime module creation + +// A stack of Objects +struct object_stack { + Object elems[STACK_SIZE]; + int next; +}; + +// A mapping mapping between ModuleInfo and Object +// Each stack element should have a corresponding element +// in the other stack at the same index +struct module_mapping { + struct module_stack infos; + struct object_stack objects; +}; + // Finds a module object by its info using a module_mapping Object get_module_by_info( struct module_mapping *mapping, const ModuleInfo *info) { @@ -127,6 +131,7 @@ } // Creates a module and stores it in a mapping +// Assumes all dependencies already exist Object create_module( VmState state, struct module_mapping *mapping, const ModuleInfo *info) { int use_count = 0; @@ -169,6 +174,7 @@ abort_print("Modules too big to track"); if (sizeof(modules_list) > sizeof(mapping.objects.elems)) abort_print("Modules too big to load"); + // Traverse modules then create them in the correct order traverse_module_uses(info, &mapping.infos); create_new_modules(state, &mapping); Object module = get_module_by_info(&mapping, info);