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.

esm module cache

+75 -17
+1 -1
meson.build
··· 69 69 build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() 70 70 71 71 version_conf = configuration_data() 72 - version_conf.set('ANT_VERSION', '0.0.7.10') 72 + version_conf.set('ANT_VERSION', '0.0.7.11') 73 73 version_conf.set('ANT_GIT_HASH', git_hash) 74 74 version_conf.set('ANT_BUILD_DATE', build_date) 75 75
+74 -16
src/ant.c
··· 124 124 bool is_text; 125 125 bool is_image; 126 126 struct esm_module *next; 127 + UT_hash_handle hh; 127 128 } esm_module_t; 128 129 129 130 typedef struct { 130 - esm_module_t *head; 131 + esm_module_t *modules; 131 132 int count; 132 133 } esm_module_cache_t; 133 134 ··· 7452 7453 (len > 5 && strcmp(path + len - 5, ".webp") == 0); 7453 7454 } 7454 7455 7455 - static esm_module_t *esm_find_module(const char *resolved_path) { 7456 - esm_module_t *mod = global_module_cache.head; 7457 - while (mod) { 7458 - if (strcmp(mod->resolved_path, resolved_path) == 0) return mod; 7459 - mod = mod->next; 7456 + static char *esm_canonicalize_path(const char *path) { 7457 + if (!path) return NULL; 7458 + 7459 + char *canonical = strdup(path); 7460 + if (!canonical) return NULL; 7461 + 7462 + char *src = canonical, *dst = canonical; 7463 + 7464 + while (*src) { 7465 + if (*src == '/') { 7466 + *dst++ = '/'; 7467 + while (*src == '/') src++; 7468 + 7469 + if (strncmp(src, "./", 2) == 0) { 7470 + src += 2; 7471 + } else if (strncmp(src, "../", 3) == 0) { 7472 + src += 3; 7473 + if (dst > canonical + 1) { 7474 + dst--; 7475 + while (dst > canonical && *(dst - 1) != '/') dst--; 7476 + } 7477 + } 7478 + } else { 7479 + *dst++ = *src++; 7480 + } 7481 + } 7482 + 7483 + *dst = '\0'; 7484 + 7485 + if (strlen(canonical) > 1 && canonical[strlen(canonical) - 1] == '/') { 7486 + canonical[strlen(canonical) - 1] = '\0'; 7460 7487 } 7461 - return NULL; 7488 + 7489 + return canonical; 7490 + } 7491 + 7492 + static esm_module_t *esm_find_module(const char *resolved_path) { 7493 + char *canonical_path = esm_canonicalize_path(resolved_path); 7494 + if (!canonical_path) return NULL; 7495 + 7496 + esm_module_t *mod = NULL; 7497 + HASH_FIND_STR(global_module_cache.modules, canonical_path, mod); 7498 + 7499 + free(canonical_path); 7500 + return mod; 7462 7501 } 7463 7502 7464 7503 static esm_module_t *esm_create_module(const char *path, const char *resolved_path) { 7504 + char *canonical_path = esm_canonicalize_path(resolved_path); 7505 + if (!canonical_path) return NULL; 7506 + 7507 + esm_module_t *existing_mod = NULL; 7508 + HASH_FIND_STR(global_module_cache.modules, canonical_path, existing_mod); 7509 + if (existing_mod) { 7510 + free(canonical_path); 7511 + return existing_mod; 7512 + } 7513 + 7465 7514 esm_module_t *mod = (esm_module_t *)malloc(sizeof(esm_module_t)); 7466 - if (!mod) return NULL; 7515 + if (!mod) { 7516 + free(canonical_path); 7517 + return NULL; 7518 + } 7467 7519 7468 7520 mod->path = strdup(path); 7469 - mod->resolved_path = strdup(resolved_path); 7521 + mod->resolved_path = canonical_path; 7470 7522 mod->namespace_obj = js_mkundef(); 7471 7523 mod->default_export = js_mkundef(); 7472 7524 mod->is_loaded = false; ··· 7476 7528 mod->is_image = esm_is_image(resolved_path); 7477 7529 mod->next = NULL; 7478 7530 7479 - if (global_module_cache.head == NULL) { 7480 - global_module_cache.head = mod; 7481 - } else { 7482 - esm_module_t *last = global_module_cache.head; 7483 - while (last->next) last = last->next; 7484 - last->next = mod; 7485 - } 7531 + HASH_ADD_STR(global_module_cache.modules, resolved_path, mod); 7486 7532 global_module_cache.count++; 7487 7533 7488 7534 return mod; 7535 + } 7536 + 7537 + static void esm_cleanup_module_cache(void) { 7538 + esm_module_t *current, *tmp; 7539 + HASH_ITER(hh, global_module_cache.modules, current, tmp) { 7540 + HASH_DEL(global_module_cache.modules, current); 7541 + if (current->path) free(current->path); 7542 + if (current->resolved_path) free(current->resolved_path); 7543 + free(current); 7544 + } 7545 + global_module_cache.count = 0; 7489 7546 } 7490 7547 7491 7548 static jsval_t esm_load_json(struct js *js, const char *path) { ··· 8239 8296 8240 8297 void js_destroy(struct js *js) { 8241 8298 if (js == NULL) return; 8299 + esm_cleanup_module_cache(); 8242 8300 8243 8301 if (js->owns_mem) { 8244 8302 ANT_GC_FREE((void *)((uint8_t *)js - 0));