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.

update collections to use canonical value

+55 -11
+1
include/modules/collections.h
··· 7 7 8 8 typedef struct map_entry { 9 9 char *key; 10 + ant_value_t key_val; 10 11 ant_value_t value; 11 12 UT_hash_handle hh; 12 13 } map_entry_t;
+1 -5
src/ant.c
··· 1288 1288 if (!first) n += cpy(buf + n, REMAIN(n, len), ",\n", 2); 1289 1289 first = false; 1290 1290 n += add_indent(buf + n, REMAIN(n, len), stringify_indent); 1291 - 1292 - size_t key_len = strlen(entry->key); 1293 - n += cpy(buf + n, REMAIN(n, len), "'", 1); 1294 - n += cpy(buf + n, REMAIN(n, len), entry->key, key_len); 1295 - n += cpy(buf + n, REMAIN(n, len), "'", 1); 1291 + n += tostr(js, entry->key_val, buf + n, REMAIN(n, len)); 1296 1292 n += cpy(buf + n, REMAIN(n, len), " => ", 4); 1297 1293 n += tostr(js, entry->value, buf + n, REMAIN(n, len)); 1298 1294 }
+8 -3
src/modules/collections.c
··· 75 75 map_entry_t *entry; 76 76 HASH_FIND_STR(*map_ptr, key_str, entry); 77 77 if (entry) { 78 + entry->key_val = args[0]; 78 79 entry->value = args[1]; 79 80 } else { 80 81 entry = ant_calloc(sizeof(map_entry_t)); 81 82 if (!entry) return js_mkerr(js, "out of memory"); 82 83 entry->key = strdup(key_str); 84 + entry->key_val = args[0]; 83 85 entry->value = args[1]; 84 86 HASH_ADD_STR(*map_ptr, key, entry); 85 87 } ··· 171 173 if (map_ptr && *map_ptr) { 172 174 map_entry_t *entry, *tmp; 173 175 HASH_ITER(hh, *map_ptr, entry, tmp) { 174 - ant_value_t k = js_mkstr(js, entry->key, strlen(entry->key)); 176 + ant_value_t k = entry->key_val; 175 177 ant_value_t call_args[3] = { entry->value, k, this_val }; 176 178 ant_value_t result = sv_vm_call(js->vm, js, callback, js_mkundef(), call_args, 3, NULL, false); 177 179 if (is_err(result)) return result; ··· 190 192 *out = entry->value; 191 193 break; 192 194 case ITER_TYPE_MAP_KEYS: 193 - *out = js_mkstr(js, entry->key, strlen(entry->key)); 195 + *out = entry->key_val; 194 196 break; 195 197 case ITER_TYPE_MAP_ENTRIES: { 196 198 ant_value_t pair = js_mkarr(js); 197 - js_arr_push(js, pair, js_mkstr(js, entry->key, strlen(entry->key))); 199 + js_arr_push(js, pair, entry->key_val); 198 200 js_arr_push(js, pair, entry->value); 199 201 *out = pair; 200 202 break; ··· 691 693 entry = ant_calloc(sizeof(map_entry_t)); 692 694 if (!entry) return js_mkerr(js, "out of memory"); 693 695 entry->key = strdup(key_str); 696 + entry->key_val = key; 694 697 entry->value = group; 695 698 HASH_ADD_STR(*map_head, key, entry); 696 699 } ··· 743 746 map_entry_t *map_entry; 744 747 HASH_FIND_STR(*map_head, key_str, map_entry); 745 748 if (map_entry) { 749 + map_entry->key_val = key; 746 750 map_entry->value = value; 747 751 continue; 748 752 } ··· 750 754 map_entry = ant_calloc(sizeof(map_entry_t)); 751 755 if (!map_entry) return js_mkerr(js, "out of memory"); 752 756 map_entry->key = strdup(key_str); 757 + map_entry->key_val = key; 753 758 map_entry->value = value; 754 759 HASH_ADD_STR(*map_head, key, map_entry); 755 760 }
+2 -1
src/modules/structured-clone.c
··· 168 168 169 169 map_entry_t *ne = ant_calloc(sizeof(map_entry_t)); 170 170 if (!ne) return js_mkerr(js, "out of memory"); 171 - ne->key = strdup(e->key); 171 + ne->key = strdup(e->key); 172 + ne->key_val = e->key_val; 172 173 ne->value = vc; 173 174 HASH_ADD_STR(*new_head, key, ne); 174 175 }}
+41
src/silver/compiler.c
··· 118 118 sv_deferred_export_t *deferred_exports; 119 119 int deferred_export_count; 120 120 int deferred_export_cap; 121 + 122 + struct const_dedup_entry *const_dedup; 121 123 } sv_compiler_t; 124 + 125 + typedef struct const_dedup_entry { 126 + const char *str; 127 + size_t len; 128 + int index; 129 + UT_hash_handle hh; 130 + } const_dedup_entry_t; 122 131 123 132 static sv_func_t *compile_function_body( 124 133 sv_compiler_t *enclosing, ··· 258 267 } 259 268 260 269 static int add_constant(sv_compiler_t *c, ant_value_t val) { 270 + if (vtype(val) == T_STR) { 271 + ant_offset_t slen; 272 + ant_offset_t off = vstr(c->js, val, &slen); 273 + const char *sptr = (const char *)(uintptr_t)off; 274 + 275 + const_dedup_entry_t *found = NULL; 276 + HASH_FIND(hh, c->const_dedup, sptr, (size_t)slen, found); 277 + if (found) return found->index; 278 + 279 + int idx = c->const_count; 280 + if (c->const_count >= c->const_cap) { 281 + c->const_cap = c->const_cap ? c->const_cap * 2 : 16; 282 + c->constants = realloc(c->constants, (size_t)c->const_cap * sizeof(ant_value_t)); 283 + } 284 + c->constants[c->const_count++] = val; 285 + 286 + const_dedup_entry_t *entry = malloc(sizeof(const_dedup_entry_t)); 287 + if (entry) { 288 + entry->str = sptr; 289 + entry->len = (size_t)slen; 290 + entry->index = idx; 291 + HASH_ADD_KEYPTR(hh, c->const_dedup, entry->str, entry->len, entry); 292 + } 293 + return idx; 294 + } 295 + 261 296 if (c->const_count >= c->const_cap) { 262 297 c->const_cap = c->const_cap ? c->const_cap * 2 : 16; 263 298 c->constants = realloc(c->constants, (size_t)c->const_cap * sizeof(ant_value_t)); ··· 4385 4420 free(comp.loops); 4386 4421 free(comp.srcpos); 4387 4422 free(comp.slot_types); 4423 + 4424 + { 4425 + const_dedup_entry_t *e, *tmp; 4426 + HASH_ITER(hh, comp.const_dedup, e, tmp) { 4427 + HASH_DEL(comp.const_dedup, e); free(e); 4428 + }} 4388 4429 4389 4430 return func; 4390 4431 }
+2 -2
src/silver/ops/iteration.h
··· 167 167 value = entry->value; 168 168 break; 169 169 case ITER_TYPE_MAP_KEYS: 170 - value = js_mkstr(js, entry->key, strlen(entry->key)); 170 + value = entry->key_val; 171 171 break; 172 172 case ITER_TYPE_MAP_ENTRIES: { 173 173 ant_value_t pair = js_mkarr(js); 174 - js_arr_push(js, pair, js_mkstr(js, entry->key, strlen(entry->key))); 174 + js_arr_push(js, pair, entry->key_val); 175 175 js_arr_push(js, pair, entry->value); 176 176 value = pair; 177 177 break;