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 collection key normalization

+33 -21
+28 -13
src/modules/collections.c
··· 1 1 #include <stdlib.h> 2 2 #include <string.h> 3 3 #include <stdio.h> 4 + #include <math.h> 4 5 5 6 #include "ant.h" 6 7 #include "errors.h" ··· 14 15 15 16 ant_value_t g_map_iter_proto = 0; 16 17 ant_value_t g_set_iter_proto = 0; 18 + 19 + static ant_value_t normalize_map_key(ant_value_t key) { 20 + if (vtype(key) == T_NUM) { 21 + double d = tod(key); 22 + if (d == 0.0 && signbit(d)) return js_mknum(0.0); 23 + } 24 + return key; 25 + } 17 26 18 27 static const char *ant_value_to_key(ant_t *js, ant_value_t val) { 19 28 if (vtype(val) == T_STR) { ··· 72 81 if (!map_ptr) return js_mkerr(js, "Invalid Map object"); 73 82 const char *key_str = ant_value_to_key(js, args[0]); 74 83 84 + ant_value_t key_val = normalize_map_key(args[0]); 75 85 map_entry_t *entry; 86 + 76 87 HASH_FIND_STR(*map_ptr, key_str, entry); 77 88 if (entry) { 78 - entry->key_val = args[0]; 89 + entry->key_val = key_val; 79 90 entry->value = args[1]; 80 91 } else { 81 92 entry = ant_calloc(sizeof(map_entry_t)); 82 93 if (!entry) return js_mkerr(js, "out of memory"); 83 94 entry->key = strdup(key_str); 84 - entry->key_val = args[0]; 95 + entry->key_val = key_val; 85 96 entry->value = args[1]; 86 97 HASH_ADD_STR(*map_ptr, key, entry); 87 98 } 88 - 99 + 89 100 return this_val; 90 101 } 91 102 ··· 678 689 for (ant_offset_t i = 0; i < len; i++) { 679 690 ant_value_t val = js_arr_get(js, items, i); 680 691 ant_value_t cb_args[2] = { val, tov((double)i) }; 681 - ant_value_t key = sv_vm_call(js->vm, js, callback, js_mkundef(), cb_args, 2, NULL, false); 682 - if (is_err(key)) return key; 683 692 684 - const char *key_str = ant_value_to_key(js, key); 693 + ant_value_t key = normalize_map_key( 694 + sv_vm_call(js->vm, js, callback, 695 + js_mkundef(), cb_args, 2, NULL, false) 696 + ); 685 697 686 698 map_entry_t *entry; 687 - HASH_FIND_STR(*map_head, key_str, entry); 688 699 ant_value_t group; 689 - if (entry) { 690 - group = entry->value; 691 - } else { 700 + 701 + if (is_err(key)) return key; 702 + const char *key_str = ant_value_to_key(js, key); 703 + HASH_FIND_STR(*map_head, key_str, entry); 704 + 705 + if (entry) group = entry->value; else { 692 706 group = js_mkarr(js); 693 707 entry = ant_calloc(sizeof(map_entry_t)); 694 708 if (!entry) return js_mkerr(js, "out of memory"); ··· 697 711 entry->value = group; 698 712 HASH_ADD_STR(*map_head, key, entry); 699 713 } 714 + 700 715 js_arr_push(js, group, val); 701 716 } 702 717 ··· 739 754 ant_offset_t entry_len = js_arr_len(js, entry); 740 755 if (entry_len < 2) continue; 741 756 742 - ant_value_t key = js_arr_get(js, entry, 0); 757 + ant_value_t key = normalize_map_key(js_arr_get(js, entry, 0)); 743 758 ant_value_t value = js_arr_get(js, entry, 1); 744 759 const char *key_str = ant_value_to_key(js, key); 745 - 760 + 746 761 map_entry_t *map_entry; 747 762 HASH_FIND_STR(*map_head, key_str, map_entry); 748 763 if (map_entry) { ··· 750 765 map_entry->value = value; 751 766 continue; 752 767 } 753 - 768 + 754 769 map_entry = ant_calloc(sizeof(map_entry_t)); 755 770 if (!map_entry) return js_mkerr(js, "out of memory"); 756 771 map_entry->key = strdup(key_str);
+5 -8
src/streams/pipes.c
··· 147 147 else js_reject_promise(js, promise, value); 148 148 } 149 149 150 - static void pipes_ignore_promise(ant_value_t maybe_promise) { 151 - promise_mark_handled(maybe_promise); 152 - } 153 - 154 150 static void pipes_shutdown_from_source_error(ant_t *js, ant_value_t state, ant_value_t error) { 155 151 pipe_state_t *pst = pipe_get_state(state); 156 152 if (!pst || pst->settled || pst->shutting_down) return; ··· 160 156 161 157 if (!pst->prevent_abort) { 162 158 ant_value_t result = writable_stream_abort(js, pipe_state_dest(state), error); 163 - pipes_ignore_promise(result); 159 + promise_mark_handled(result); 164 160 } 165 161 166 162 pipes_settle(js, state, false, error); ··· 175 171 176 172 if (!pst->prevent_cancel) { 177 173 ant_value_t result = readable_stream_cancel(js, pipe_state_source(state), error); 178 - pipes_ignore_promise(result); 174 + promise_mark_handled(result); 179 175 } 180 176 181 177 pipes_settle(js, state, false, error); ··· 190 186 191 187 if (!pst->prevent_abort) { 192 188 ant_value_t result = writable_stream_abort(js, pipe_state_dest(state), reason); 193 - pipes_ignore_promise(result); 189 + promise_mark_handled(result); 194 190 } 191 + 195 192 if (!pst->prevent_cancel) { 196 193 ant_value_t result = readable_stream_cancel(js, pipe_state_source(state), reason); 197 - pipes_ignore_promise(result); 194 + promise_mark_handled(result); 198 195 } 199 196 200 197 pipes_settle(js, state, false, reason);