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.

add finalizer to request/response

+84 -26
+21 -1
src/modules/headers.c
··· 73 73 return (hdr_list_t *)js_get_native(obj, HEADERS_NATIVE_TAG); 74 74 } 75 75 76 + static void headers_finalize(ant_t *js, ant_object_t *obj) { 77 + ant_value_t value = js_obj_from_ptr(obj); 78 + hdr_list_t *list = get_list(value); 79 + list_free(list); 80 + js_clear_native(value, HEADERS_NATIVE_TAG); 81 + } 82 + 83 + static void headers_iter_finalize(ant_t *js, ant_object_t *obj) { 84 + ant_value_t value = js_obj_from_ptr(obj); 85 + free(js_get_native(value, HEADERS_ITER_NATIVE_TAG)); 86 + js_clear_native(value, HEADERS_ITER_NATIVE_TAG); 87 + } 88 + 76 89 static headers_guard_t get_guard(ant_value_t obj) { 77 90 ant_value_t slot = js_get_slot(obj, SLOT_HEADERS_GUARD); 78 91 if (vtype(slot) != T_NUM) return HEADERS_GUARD_NONE; ··· 517 530 518 531 static ant_value_t make_headers_iter(ant_t *js, ant_value_t headers_obj, int kind) { 519 532 hdr_list_t *l = get_list(headers_obj); 533 + if (!l) return js_mkerr(js, "Invalid Headers object"); 520 534 521 535 hdr_iter_t *st = ant_calloc(sizeof(hdr_iter_t)); 522 536 if (!st) return js_mkerr(js, "out of memory"); 523 - st->list = l ? l : list_new(); 537 + 538 + st->list = l; 524 539 st->kind = kind; 525 540 526 541 ant_value_t iter = js_mkobj(js); 527 542 js_set_proto_init(iter, g_headers_iter_proto); 528 543 js_set_native(iter, st, HEADERS_ITER_NATIVE_TAG); 544 + js_set_finalizer(iter, headers_iter_finalize); 545 + js_set_slot_wb(js, iter, SLOT_AUX, headers_obj); 546 + 529 547 return iter; 530 548 } 531 549 ··· 816 834 817 835 js_set_slot(obj, SLOT_BRAND, js_mknum(BRAND_HEADERS)); 818 836 js_set_native(obj, l, HEADERS_NATIVE_TAG); 837 + js_set_finalizer(obj, headers_finalize); 819 838 js_set_slot(obj, SLOT_HEADERS_GUARD, js_mknum(HEADERS_GUARD_NONE)); 820 839 821 840 return obj; ··· 829 848 js_set_proto_init(obj, g_headers_proto); 830 849 js_set_slot(obj, SLOT_BRAND, js_mknum(BRAND_HEADERS)); 831 850 js_set_native(obj, l, HEADERS_NATIVE_TAG); 851 + js_set_finalizer(obj, headers_finalize); 832 852 js_set_slot(obj, SLOT_HEADERS_GUARD, js_mknum(HEADERS_GUARD_NONE)); 833 853 834 854 return obj;
+32 -10
src/modules/request.c
··· 73 73 free(d); 74 74 } 75 75 76 + static void request_finalize(ant_t *js, ant_object_t *obj) { 77 + ant_value_t value = js_obj_from_ptr(obj); 78 + request_data_t *data = get_data(value); 79 + data_free(data); 80 + js_clear_native(value, REQUEST_NATIVE_TAG); 81 + } 82 + 83 + static void request_clear_and_free(ant_value_t obj, request_data_t *data) { 84 + if (obj) js_clear_native(obj, REQUEST_NATIVE_TAG); 85 + data_free(data); 86 + } 87 + 76 88 static request_data_t *data_new_with(const char *method, const char *mode) { 77 89 request_data_t *d = calloc(1, sizeof(request_data_t)); 78 90 if (!d) return NULL; ··· 104 116 105 117 static ant_value_t request_create_object(ant_t *js, request_data_t *req, ant_value_t headers_obj, bool create_signal) { 106 118 ant_value_t obj = js_mkobj(js); 119 + 107 120 ant_value_t hdrs = is_object_type(headers_obj) 108 121 ? headers_obj 109 122 : headers_create_empty(js); 110 123 124 + if (is_err(hdrs)) { 125 + data_free(req); 126 + return hdrs; 127 + } 128 + 111 129 js_set_proto_init(obj, g_request_proto); 112 130 js_set_slot(obj, SLOT_BRAND, js_mknum(BRAND_REQUEST)); 113 131 js_set_native(obj, req, REQUEST_NATIVE_TAG); 132 + js_set_finalizer(obj, request_finalize); 114 133 115 134 headers_set_guard(hdrs, 116 135 strcmp(req->mode, "no-cors") == 0 ··· 940 959 js_set_proto_init(obj, g_request_proto); 941 960 js_set_slot(obj, SLOT_BRAND, js_mknum(BRAND_REQUEST)); 942 961 js_set_native(obj, nd, REQUEST_NATIVE_TAG); 962 + js_set_finalizer(obj, request_finalize); 943 963 944 964 js_set_slot_wb(js, obj, SLOT_REQUEST_HEADERS, new_headers); 945 965 js_set_slot(obj, SLOT_REQUEST_ABORT_REASON, js_mkundef()); ··· 1299 1319 1300 1320 js_set_slot(obj, SLOT_BRAND, js_mknum(BRAND_REQUEST)); 1301 1321 js_set_native(obj, req, REQUEST_NATIVE_TAG); 1322 + js_set_finalizer(obj, request_finalize); 1302 1323 js_set_slot(obj, SLOT_REQUEST_ABORT_REASON, js_mkundef()); 1303 1324 1304 1325 signal = abort_signal_create_dependent(js, input_signal); 1305 - if (is_err(signal)) { data_free(req); return signal; } 1326 + if (is_err(signal)) { request_clear_and_free(obj, req); return signal; } 1306 1327 js_set_slot_wb(js, obj, SLOT_REQUEST_SIGNAL, signal); 1307 1328 1308 1329 headers = request_create_ctor_headers(js, input); 1309 - if (is_err(headers)) { data_free(req); return headers; } 1330 + if (is_err(headers)) { request_clear_and_free(obj, req); return headers; } 1310 1331 1311 1332 if (init_provided) { 1312 1333 headers = request_apply_init_headers(js, init, headers); 1313 - if (is_err(headers)) { data_free(req); return headers; } 1334 + if (is_err(headers)) { request_clear_and_free(obj, req); return headers; } 1314 1335 } 1315 1336 1316 1337 headers_set_guard(headers, ··· 1324 1345 1325 1346 if (init_provided) { 1326 1347 step = request_parse_duplex(js, init, &duplex_provided); 1327 - if (is_err(step)) { data_free(req); return step; } 1348 + if (is_err(step)) { request_clear_and_free(obj, req); return step; } 1328 1349 } 1329 1350 1330 1351 step = request_apply_ctor_body( ··· 1333 1354 ); 1334 1355 1335 1356 if (is_err(step)) { 1336 - data_free(req); 1357 + request_clear_and_free(obj, req); 1337 1358 return step; 1338 1359 } 1339 1360 ··· 1377 1398 js_set_proto_init(obj, g_request_proto); 1378 1399 js_set_slot(obj, SLOT_BRAND, js_mknum(BRAND_REQUEST)); 1379 1400 js_set_native(obj, req, REQUEST_NATIVE_TAG); 1401 + js_set_finalizer(obj, request_finalize); 1380 1402 js_set_slot(obj, SLOT_REQUEST_ABORT_REASON, js_mkundef()); 1381 1403 1382 1404 signal = abort_signal_create_dependent(js, input_signal); 1383 - if (is_err(signal)) { data_free(req); return signal; } 1405 + if (is_err(signal)) { request_clear_and_free(obj, req); return signal; } 1384 1406 js_set_slot_wb(js, obj, SLOT_REQUEST_SIGNAL, signal); 1385 1407 1386 1408 headers = request_create_ctor_headers(js, input); 1387 - if (is_err(headers)) { data_free(req); return headers; } 1409 + if (is_err(headers)) { request_clear_and_free(obj, req); return headers; } 1388 1410 1389 1411 if (init_provided) { 1390 1412 headers = request_apply_init_headers(js, init, headers); 1391 - if (is_err(headers)) { data_free(req); return headers; } 1413 + if (is_err(headers)) { request_clear_and_free(obj, req); return headers; } 1392 1414 } 1393 1415 1394 1416 headers_set_guard(headers, ··· 1401 1423 1402 1424 if (init_provided) { 1403 1425 step = request_parse_duplex(js, init, &duplex_provided); 1404 - if (is_err(step)) { data_free(req); return step; } 1426 + if (is_err(step)) { request_clear_and_free(obj, req); return step; } 1405 1427 } 1406 1428 1407 1429 step = request_apply_ctor_body(js, obj, input, init, init_provided, duplex_provided, req, src, headers); 1408 1430 if (is_err(step)) { 1409 - data_free(req); 1431 + request_clear_and_free(obj, req); 1410 1432 return step; 1411 1433 } 1412 1434
+31 -15
src/modules/response.c
··· 52 52 free(d); 53 53 } 54 54 55 + static void response_finalize(ant_t *js, ant_object_t *obj) { 56 + ant_value_t value = js_obj_from_ptr(obj); 57 + response_data_t *data = get_data(value); 58 + data_free(data); 59 + js_clear_native(value, RESPONSE_NATIVE_TAG); 60 + } 61 + 62 + static void response_clear_and_free(ant_value_t obj, response_data_t *data) { 63 + if (obj) js_clear_native(obj, RESPONSE_NATIVE_TAG); 64 + data_free(data); 65 + } 66 + 55 67 static response_data_t *data_new(void) { 56 68 response_data_t *d = calloc(1, sizeof(response_data_t)); 57 69 if (!d) return NULL; ··· 719 731 static ant_value_t response_new(headers_guard_t guard) { 720 732 ant_t *js = rt->js; 721 733 response_data_t *resp = data_new(); 734 + 722 735 ant_value_t obj = 0; 723 736 ant_value_t headers = 0; 724 737 725 738 if (!resp) return js_mkerr(js, "out of memory"); 726 739 obj = js_mkobj(js); 740 + 727 741 js_set_proto_init(obj, g_response_proto); 728 742 js_set_slot(obj, SLOT_BRAND, js_mknum(BRAND_RESPONSE)); 729 743 js_set_native(obj, resp, RESPONSE_NATIVE_TAG); 744 + js_set_finalizer(obj, response_finalize); 730 745 731 746 headers = headers_create_empty(js); 732 747 if (is_err(headers)) { 733 - data_free(resp); 748 + response_clear_and_free(obj, resp); 734 749 return headers; 735 750 } 736 751 737 752 headers_set_guard(headers, guard); 738 753 headers_apply_guard(headers); 754 + 739 755 js_set_slot_wb(js, obj, SLOT_RESPONSE_HEADERS, headers); 740 756 js_set_slot_wb(js, obj, SLOT_RESPONSE_BODY_STREAM, js_mkundef()); 757 + 741 758 return obj; 742 759 } 743 760 ··· 760 777 761 778 step = response_init_common(js, obj, init, body, HEADERS_GUARD_RESPONSE); 762 779 if (is_err(step)) { 763 - data_free(get_data(obj)); 780 + response_clear_and_free(obj, get_data(obj)); 764 781 return step; 765 782 } 766 783 ··· 779 796 free(resp->type); 780 797 resp->type = strdup(type ? type : "default"); 781 798 if (!resp->type) { 782 - data_free(resp); 799 + response_clear_and_free(obj, resp); 783 800 return js_mkerr(js, "out of memory"); 784 801 } 785 802 ··· 787 804 free(resp->status_text); 788 805 resp->status_text = strdup(status_text ? status_text : ""); 789 806 if (!resp->status_text) { 790 - data_free(resp); 807 + response_clear_and_free(obj, resp); 791 808 return js_mkerr(js, "out of memory"); 792 809 } 793 810 ··· 795 812 } 796 813 797 814 static ant_value_t js_response_error(ant_t *js, ant_value_t *args, int nargs) { 798 - (void)args; 799 - (void)nargs; 800 815 ant_value_t obj = response_create_static(js, "error", 0, "", HEADERS_GUARD_IMMUTABLE); 801 816 if (is_err(obj)) return obj; 802 817 return obj; ··· 876 891 877 892 step = response_init_common(js, obj, init, stringify, HEADERS_GUARD_RESPONSE); 878 893 if (is_err(step)) { 879 - data_free(get_data(obj)); 894 + response_clear_and_free(obj, get_data(obj)); 880 895 return step; 881 896 } 882 897 ··· 1055 1070 js_set_proto_init(obj, g_response_proto); 1056 1071 js_set_slot(obj, SLOT_BRAND, js_mknum(BRAND_RESPONSE)); 1057 1072 js_set_native(obj, nd, RESPONSE_NATIVE_TAG); 1073 + js_set_finalizer(obj, response_finalize); 1058 1074 js_set_slot_wb(js, obj, SLOT_RESPONSE_HEADERS, new_headers); 1059 1075 js_set_slot_wb(js, obj, SLOT_RESPONSE_BODY_STREAM, js_mkundef()); 1060 1076 ··· 1093 1109 free(resp->type); 1094 1110 resp->type = strdup(type ? type : "default"); 1095 1111 if (!resp->type) { 1096 - data_free(resp); 1112 + response_clear_and_free(obj, resp); 1097 1113 return js_mkerr(js, "out of memory"); 1098 1114 } 1099 1115 ··· 1101 1117 free(resp->status_text); 1102 1118 resp->status_text = strdup(status_text ? status_text : ""); 1103 1119 if (!resp->status_text) { 1104 - data_free(resp); 1120 + response_clear_and_free(obj, resp); 1105 1121 return js_mkerr(js, "out of memory"); 1106 1122 } 1107 1123 1108 1124 if (body_len > 0) { 1109 1125 resp->body_data = malloc(body_len); 1110 1126 if (!resp->body_data) { 1111 - data_free(resp); 1127 + response_clear_and_free(obj, resp); 1112 1128 return js_mkerr(js, "out of memory"); 1113 1129 } 1114 1130 memcpy(resp->body_data, body, body_len); ··· 1121 1137 1122 1138 headers = is_object_type(headers_obj) ? headers_obj : headers_create_empty(js); 1123 1139 if (is_err(headers)) { 1124 - data_free(resp); 1140 + response_clear_and_free(obj, resp); 1125 1141 return headers; 1126 1142 } 1127 1143 ··· 1162 1178 free(resp->status_text); 1163 1179 resp->status_text = strdup(status_text ? status_text : ""); 1164 1180 if (!resp->status_text) { 1165 - data_free(resp); 1181 + response_clear_and_free(obj, resp); 1166 1182 return js_mkerr(js, "out of memory"); 1167 1183 } 1168 1184 ··· 1181 1197 if (body_len > 0) { 1182 1198 resp->body_data = malloc(body_len); 1183 1199 if (!resp->body_data) { 1184 - data_free(resp); 1200 + response_clear_and_free(obj, resp); 1185 1201 return js_mkerr(js, "out of memory"); 1186 1202 } 1187 1203 memcpy(resp->body_data, body, body_len); ··· 1193 1209 1194 1210 resp->body_type = body_type ? strdup(body_type) : NULL; 1195 1211 if (body_type && !resp->body_type) { 1196 - data_free(resp); 1212 + response_clear_and_free(obj, resp); 1197 1213 return js_mkerr(js, "out of memory"); 1198 1214 } 1199 1215 1200 1216 headers = is_object_type(headers_obj) ? headers_obj : headers_create_empty(js); 1201 1217 if (is_err(headers)) { 1202 - data_free(resp); 1218 + response_clear_and_free(obj, resp); 1203 1219 return headers; 1204 1220 } 1205 1221