MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

canonical module

+17 -4
+17 -4
src/esm/library.c
··· 8 8 typedef struct ant_library_entry { 9 9 char name[256]; 10 10 ant_library_init_fn init_fn; 11 + ant_value_t cached_ns; 12 + bool ns_initialized; 13 + struct ant_library_entry *canonical; 11 14 UT_hash_handle hh; 12 15 } ant_library_entry_t; 13 16 ··· 16 19 void ant_register_library(ant_library_init_fn init_fn, const char *name, ...) { 17 20 va_list args; 18 21 const char *alias = name; 22 + ant_library_entry_t *canonical_entry = NULL; 19 23 20 24 va_start(args, name); 21 25 while (alias != NULL) { ··· 25 29 strncpy(lib->name, alias, sizeof(lib->name) - 1); 26 30 lib->name[sizeof(lib->name) - 1] = '\0'; 27 31 lib->init_fn = init_fn; 32 + lib->ns_initialized = false; 33 + 34 + if (!canonical_entry) canonical_entry = lib; 35 + lib->canonical = canonical_entry; 28 36 29 37 HASH_ADD_STR(library_registry, name, lib); 30 38 alias = va_arg(args, const char *); ··· 46 54 47 55 void ant_library_foreach(ant_library_iter_fn cb, void *userdata) { 48 56 ant_library_entry_t *lib, *tmp; 49 - HASH_ITER(hh, library_registry, lib, tmp) { 50 - if (!strchr(lib->name, ':')) cb(lib->name, userdata); 51 - } 57 + HASH_ITER(hh, library_registry, lib, tmp) if (!strchr(lib->name, ':')) cb(lib->name, userdata); 52 58 } 53 59 54 60 ant_value_t js_esm_load_registered_library(ant_t *js, const char *specifier, size_t spec_len, bool *loaded) { ··· 58 64 return js_mkundef(); 59 65 } 60 66 if (loaded) *loaded = true; 61 - return lib->init_fn(js); 67 + 68 + ant_library_entry_t *canon = lib->canonical; 69 + if (canon->ns_initialized) return canon->cached_ns; 70 + 71 + canon->cached_ns = canon->init_fn(js); 72 + canon->ns_initialized = true; 73 + 74 + return canon->cached_ns; 62 75 }