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.

improve DataView clones

+94 -14
+6
include/modules/buffer.h
··· 62 62 const char *type_name, ant_value_t arraybuffer_obj 63 63 ); 64 64 65 + ant_value_t create_dataview_with_buffer( 66 + ant_t *js, ArrayBufferData *buffer, 67 + size_t byte_offset, size_t byte_length, 68 + ant_value_t arraybuffer_obj 69 + ); 70 + 65 71 size_t buffer_get_external_memory(void); 66 72 bool buffer_is_dataview(ant_value_t obj); 67 73 bool buffer_source_get_bytes(ant_t *js, ant_value_t value, const uint8_t **out, size_t *len);
+27
src/modules/buffer.c
··· 625 625 free_array_buffer_data(buffer); return result; 626 626 } 627 627 628 + ant_value_t create_dataview_with_buffer( 629 + ant_t *js, ArrayBufferData *buffer, 630 + size_t byte_offset, size_t byte_length, 631 + ant_value_t arraybuffer_obj 632 + ) { 633 + DataViewData *dv_data = ta_arena_alloc(sizeof(DataViewData)); 634 + if (!dv_data) return js_mkerr(js, "Failed to allocate DataView"); 635 + 636 + dv_data->buffer = buffer; 637 + dv_data->byte_offset = byte_offset; 638 + dv_data->byte_length = byte_length; 639 + buffer->ref_count++; 640 + 641 + ant_value_t obj = js_mkobj(js); 642 + ant_value_t proto = js_get_ctor_proto(js, "DataView", 8); 643 + if (is_special_object(proto)) js_set_proto_init(obj, proto); 644 + 645 + js_set_slot(obj, SLOT_BRAND, js_mknum(BRAND_DATAVIEW)); 646 + js_set_slot(obj, SLOT_DATA, ANT_PTR(dv_data)); 647 + js_mkprop_fast(js, obj, "buffer", 6, arraybuffer_obj); 648 + js_set_descriptor(js, obj, "buffer", 6, 0); 649 + js_set(js, obj, "byteLength", js_mknum((double)byte_length)); 650 + js_set(js, obj, "byteOffset", js_mknum((double)byte_offset)); 651 + 652 + return obj; 653 + } 654 + 628 655 typedef struct { 629 656 ant_value_t *values; 630 657 size_t length;
+61 -14
src/modules/structured-clone.c
··· 62 62 return names[i]; 63 63 } 64 64 65 + static ant_value_t sc_clone_typed_array( 66 + ant_t *js, ant_value_t key, TypedArrayData *ta_data, sc_entry_t **seen 67 + ) { 68 + ant_value_t existing = sc_lookup(seen, key); 69 + if (vtype(existing) != T_UNDEF) return existing; 70 + 71 + if (!ta_data || !ta_data->buffer) return js_throw( 72 + js, make_dom_exception(js, "TypedArray could not be cloned", "DataCloneError") 73 + ); 74 + 75 + ArrayBufferData *new_buf = create_array_buffer_data(ta_data->byte_length); 76 + if (!new_buf) return js_mkerr(js, "out of memory"); 77 + if (ta_data->byte_length > 0) { 78 + memcpy(new_buf->data, ta_data->buffer->data + ta_data->byte_offset, ta_data->byte_length); 79 + } 80 + 81 + ant_value_t clone = create_typed_array( 82 + js, ta_data->type, new_buf, 0, 83 + ta_data->length, sc_ta_type_name(ta_data->type) 84 + ); 85 + 86 + sc_add(seen, key, clone); 87 + return clone; 88 + } 89 + 65 90 static ant_value_t sc_clone_rec(ant_t *js, ant_value_t val, sc_entry_t **seen, sc_entry_t **transfer) { 66 91 uint8_t t = vtype(val); 92 + ant_value_t slot = js_mkundef(); 93 + TypedArrayData *ta_data = NULL; 67 94 68 95 if (t == T_UNDEF || t == T_NULL || t == T_BOOL || t == T_NUM || t == T_BIGINT || t == T_STR) return val; 69 96 if (t == T_SYMBOL) return js_throw(js, make_dom_exception(js, "Symbol cannot be serialized", "DataCloneError")); 70 97 71 - if (t == T_TYPEDARRAY) { 72 - ant_value_t existing = sc_lookup(seen, val); 73 - if (vtype(existing) != T_UNDEF) return existing; 98 + if (is_object_type(val)) { 99 + slot = js_get_slot(val, SLOT_BUFFER); 100 + ta_data = (TypedArrayData *)js_gettypedarray(slot); 101 + } 74 102 75 - TypedArrayData *ta = (TypedArrayData *)js_gettypedarray(val); 76 - if (!ta || !ta->buffer) 77 - return js_throw(js, make_dom_exception(js, "TypedArray could not be cloned", "DataCloneError")); 78 - 79 - ArrayBufferData *new_buf = create_array_buffer_data(ta->byte_length); 80 - if (!new_buf) return js_mkerr(js, "out of memory"); 81 - if (ta->byte_length > 0) memcpy(new_buf->data, ta->buffer->data + ta->byte_offset, ta->byte_length); 103 + if (ta_data) { 104 + return sc_clone_typed_array(js, val, ta_data, seen); 105 + } 82 106 83 - ant_value_t clone = create_typed_array(js, ta->type, new_buf, 0, ta->length, sc_ta_type_name(ta->type)); 84 - sc_add(seen, val, clone); 85 - 86 - return clone; 107 + if (t == T_TYPEDARRAY) { 108 + return sc_clone_typed_array(js, val, (TypedArrayData *)js_gettypedarray(val), seen); 87 109 } 88 110 89 111 if (t == T_FUNC || t == T_CFUNC || t == T_CLOSURE) ··· 123 145 sc_add(seen, val, clone); 124 146 return clone; 125 147 }} 148 + 149 + if (buffer_is_dataview(val)) { 150 + ant_value_t dv_data_val = js_get_slot(val, SLOT_DATA); 151 + if (vtype(dv_data_val) != T_NUM) 152 + return js_throw(js, make_dom_exception(js, "DataView could not be cloned", "DataCloneError")); 153 + 154 + DataViewData *dv = (DataViewData *)(uintptr_t)js_getnum(dv_data_val); 155 + if (!dv || !dv->buffer) 156 + return js_throw(js, make_dom_exception(js, "DataView could not be cloned", "DataCloneError")); 157 + 158 + ArrayBufferData *new_buf = create_array_buffer_data(dv->byte_length); 159 + if (!new_buf) return js_mkerr(js, "out of memory"); 160 + if (dv->byte_length > 0) memcpy(new_buf->data, dv->buffer->data + dv->byte_offset, dv->byte_length); 161 + 162 + ant_value_t ab_obj = create_arraybuffer_obj(js, new_buf); 163 + ant_value_t clone = create_dataview_with_buffer(js, new_buf, 0, dv->byte_length, ab_obj); 164 + if (is_err(clone)) { 165 + free_array_buffer_data(new_buf); 166 + return clone; 167 + } 168 + 169 + sc_add(seen, val, clone); 170 + free_array_buffer_data(new_buf); 171 + return clone; 172 + } 126 173 127 174 if (t == T_ARR) { 128 175 ant_value_t clone = js_mkarr(js);