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 jshdl_t type for handling sizes and lengths

+59 -78
+3 -2
include/internal.h
··· 71 71 int for_let_stack_cap; 72 72 73 73 jsval_t *gc_roots; 74 - int gc_roots_len; 75 - int gc_roots_cap; 74 + jshdl_t gc_roots_len; 75 + jshdl_t gc_roots_cap; 76 76 77 77 jsval_t ascii_char_cache[128]; 78 78 bool ascii_cache_init; ··· 111 111 double tod(jsval_t v); 112 112 113 113 jsoff_t lkp(struct js *js, jsval_t obj, const char *buf, size_t len); 114 + jsoff_t lkp_proto(struct js *js, jsval_t obj, const char *buf, size_t len); 114 115 jsoff_t vstr(struct js *js, jsval_t value, jsoff_t *len); 115 116 116 117 jsval_t mkval(uint8_t type, uint64_t data);
+2 -1
include/types.h
··· 2 2 #define TYPES_H 3 3 4 4 #include <stdint.h> 5 + #include <stddef.h> 5 6 6 7 struct js; 7 8 8 9 typedef struct js ant_t; 9 10 typedef unsigned long long u64; 10 11 11 - typedef int jshdl_t; 12 + typedef size_t jshdl_t; 12 13 typedef uint64_t jsoff_t; 13 14 typedef uint64_t jsval_t; 14 15
+4 -5
src/ant.c
··· 738 738 static inline bool push_this(jsval_t this_value); 739 739 static inline jsval_t pop_this(void); 740 740 741 - static jsoff_t lkp_proto(struct js *js, jsval_t obj, const char *key, size_t len); 742 - static jsoff_t lkp_interned(struct js *js, jsval_t obj, const char *search_intern, size_t len); 743 - 744 741 static jsval_t get_prototype_for_type(struct js *js, uint8_t type); 745 742 static jsval_t get_ctor_proto(struct js *js, const char *name, size_t len); 743 + 746 744 static jsval_t setprop(struct js *js, jsval_t obj, jsval_t k, jsval_t v); 745 + static jsoff_t lkp_interned(struct js *js, jsval_t obj, const char *search_intern, size_t len); 747 746 748 747 static descriptor_entry_t *lookup_descriptor(jsoff_t obj_off, const char *key, size_t klen); 749 748 static const char *bigint_digits(struct js *js, jsval_t v, size_t *len); ··· 4729 4728 } 4730 4729 } 4731 4730 4732 - static jsoff_t lkp_proto(struct js *js, jsval_t obj, const char *key, size_t len) { 4731 + jsoff_t lkp_proto(struct js *js, jsval_t obj, const char *key, size_t len) { 4733 4732 uint8_t t = vtype(obj); 4734 4733 const char *key_intern = intern_string(key, len); 4735 4734 ··· 22379 22378 op_off(c, &c->js->for_let_stack[i].prop_off); 22380 22379 } 22381 22380 22382 - for (int i = 0; i < c->js->gc_roots_len; i++) op_val(c, &c->js->gc_roots[i]); 22381 + for (jshdl_t i = 0; i < c->js->gc_roots_len; i++) op_val(c, &c->js->gc_roots[i]); 22383 22382 if (c->js->ascii_cache_init) for (int i = 0; i < 128; i++) op_val(c, &c->js->ascii_char_cache[i]); 22384 22383 } 22385 22384
+28 -48
src/modules/fetch.c
··· 66 66 } 67 67 } 68 68 69 + static jsval_t fetch_fail_oom(struct js *js, jsval_t promise, fetch_request_t *req, bool close_http, const char *msg, size_t len) { 70 + jsval_t err = js_mkstr(js, msg, len); 71 + js_reject_promise(js, promise, err); 72 + if (close_http) tlsuv_http_close(&req->http_client, NULL); 73 + remove_pending_request(req); free_fetch_request(req); 74 + return js_mkundef(); 75 + } 76 + 69 77 static jsval_t response_text(struct js *js, jsval_t *args, int nargs) { 70 78 jsval_t this = js_getthis(js); 71 79 jsval_t body = js_get_slot(js, this, SLOT_DATA); ··· 220 228 utarray_push_back(pending_requests, &req); 221 229 222 230 const char *scheme_end = strstr(url_str, "://"); 223 - if (!scheme_end) { 224 - jsval_t err = js_mkstr(js, "Invalid URL: no scheme", 22); 225 - js_reject_promise(js, promise, err); 226 - remove_pending_request(req); 227 - free_fetch_request(req); 228 - return js_mkundef(); 229 - } 231 + if (!scheme_end) return fetch_fail_oom(js, promise, req, false, "Invalid URL: no scheme", 22); 230 232 231 233 const char *host_start = scheme_end + 3; 232 234 const char *path_start = strchr(host_start, '/'); ··· 234 236 235 237 for (const char *p = host_start; p < (path_start ? path_start : host_start + strlen(host_start)); p++) { 236 238 if (*p == '@') at_in_host = p; 237 - } 238 - if (at_in_host) host_start = at_in_host + 1; 239 + } if (at_in_host) host_start = at_in_host + 1; 239 240 240 241 size_t scheme_len = scheme_end - url_str; 241 242 size_t host_len = path_start ? (size_t)(path_start - host_start) : strlen(host_start); 242 - const char *path = path_start ? path_start : "/"; 243 243 244 - if (host_len == 0) { 245 - jsval_t err = js_mkstr(js, "Invalid URL: no host", 20); 246 - js_reject_promise(js, promise, err); 247 - remove_pending_request(req); 248 - free_fetch_request(req); 249 - return js_mkundef(); 250 - } 244 + const char *path = path_start ? path_start : "/"; 245 + if (host_len == 0) return fetch_fail_oom(js, promise, req, false, "Invalid URL: no host", 20); 251 246 252 247 char *host_url = calloc(1, scheme_len + 3 + host_len + 1); 253 - if (!host_url) { 254 - jsval_t err = js_mkstr(js, "Out of memory", 13); 255 - js_reject_promise(js, promise, err); 256 - remove_pending_request(req); 257 - free_fetch_request(req); 258 - return js_mkundef(); 259 - } 248 + if (!host_url) return fetch_fail_oom(js, promise, req, false, "Out of memory", 13); 260 249 snprintf(host_url, scheme_len + 3 + host_len + 1, "%.*s://%.*s", (int)scheme_len, url_str, (int)host_len, host_start); 261 250 262 - int rc = tlsuv_http_init(fetch_loop, &req->http_client, host_url); 263 - free(host_url); 264 - 265 - if (rc != 0) { 266 - jsval_t err = js_mkstr(js, "Failed to initialize HTTP client", 33); 267 - js_reject_promise(js, promise, err); 268 - remove_pending_request(req); 269 - free_fetch_request(req); 270 - return js_mkundef(); 271 - } 251 + int rc = tlsuv_http_init(fetch_loop, &req->http_client, host_url); free(host_url); 252 + if (rc != 0) return fetch_fail_oom(js, promise, req, false, "Failed to initialize HTTP client", 33); 272 253 273 254 req->http_client.data = req; 274 - 275 255 req->method = NULL; 276 256 req->body = NULL; 277 257 req->body_len = 0; ··· 280 260 jsval_t method_val = js_get(js, options_val, "method"); 281 261 if (vtype(method_val) == T_STR) { 282 262 char *str = js_getstr(js, method_val, NULL); 283 - if (str) req->method = strdup(str); 263 + if (str) { 264 + req->method = strdup(str); 265 + if (!req->method) return fetch_fail_oom(js, promise, req, true, "Out of memory", 13); 266 + } 284 267 } 285 268 286 269 jsval_t body_val = js_get(js, options_val, "body"); 287 270 if (vtype(body_val) == T_STR) { 288 271 size_t len; 289 272 char *str = js_getstr(js, body_val, &len); 290 - if (str) { 291 - req->body = memcpy(malloc(len), str, len); 273 + if (str && len > 0) { 274 + req->body = malloc(len); 275 + if (!req->body) return fetch_fail_oom(js, promise, req, true, "Out of memory", 13); 276 + memcpy(req->body, str, len); 292 277 req->body_len = len; 293 278 } 294 279 } 295 280 } 296 281 297 - if (!req->method) req->method = strdup("GET"); 298 - req->http_req = tlsuv_http_req(&req->http_client, req->method, path, resp_cb, req); 299 - 300 - if (!req->http_req) { 301 - jsval_t err = js_mkstr(js, "Failed to create HTTP request", 30); 302 - js_reject_promise(js, promise, err); 303 - tlsuv_http_close(&req->http_client, NULL); 304 - remove_pending_request(req); 305 - free_fetch_request(req); 306 - return js_mkundef(); 282 + if (!req->method) { 283 + req->method = strdup("GET"); 284 + if (!req->method) return fetch_fail_oom(js, promise, req, true, "Out of memory", 13); 307 285 } 308 286 287 + req->http_req = tlsuv_http_req(&req->http_client, req->method, path, resp_cb, req); 288 + if (!req->http_req) return fetch_fail_oom(js, promise, req, true, "Failed to create HTTP request", 30); 309 289 req->http_req->data = req; 310 290 311 291 char user_agent[256];
+15 -11
src/modules/readline.c
··· 348 348 349 349 static void handle_char_input(rl_interface_t *iface, char c) { 350 350 if (iface->line_len < MAX_LINE_LENGTH - 1) { 351 - memmove(iface->line_buffer + iface->line_pos + 1, 352 - iface->line_buffer + iface->line_pos, 353 - iface->line_len - iface->line_pos + 1); 351 + memmove( 352 + iface->line_buffer + iface->line_pos + 1, 353 + iface->line_buffer + iface->line_pos, 354 + iface->line_len - iface->line_pos + 1 355 + ); 356 + 354 357 iface->line_buffer[iface->line_pos] = c; 355 358 iface->line_pos++; 356 359 iface->line_len++; 357 360 358 361 if (iface->line_pos == iface->line_len) { 362 + int cols = get_terminal_cols(); 363 + int prompt_len = (int)strlen(iface->prompt); 364 + int total_cols = prompt_len + iface->line_len; 365 + int rows = total_cols > 0 ? total_cols / cols + 1 : 1; 366 + if (rows > iface->last_render_rows) iface->last_render_rows = rows; 359 367 printf("%c", c); 360 368 fflush(stdout); 361 - } else { 362 - refresh_line(iface); 363 - } 369 + } else refresh_line(iface); 364 370 } 365 371 } 366 372 ··· 1122 1128 if (iface->tab_size < 1) iface->tab_size = 1; 1123 1129 1124 1130 jsval_t completer_val = js_get(js, options, "completer"); 1125 - iface->completer = (vtype(completer_val) == T_FUNC) ? completer_val : js_mkundef(); 1131 + int ctype = vtype(completer_val); 1132 + iface->completer = (ctype == T_FUNC || ctype == T_CFUNC) ? completer_val : js_mkundef(); 1126 1133 1127 1134 jsval_t history_val = js_get(js, options, "history"); 1128 1135 if (is_special_object(history_val)) { ··· 1140 1147 if (line) rl_history_add(&iface->history, line, false); 1141 1148 } 1142 1149 } 1143 - } else { 1144 - rl_history_init(&iface->history, iface->history_size); 1145 - } 1146 - 1150 + } else rl_history_init(&iface->history, iface->history_size); 1147 1151 HASH_ADD(hh, interfaces, id, sizeof(uint64_t), iface); 1148 1152 1149 1153 jsval_t obj = js_mkobj(js);
+4 -7
src/modules/reflect.c
··· 60 60 char *key_str = js_getstr(js, key, &key_len); 61 61 if (!key_str) return js_mkfalse(); 62 62 63 - jsoff_t off = lkp(js, target, key_str, key_len); 63 + jsoff_t off = lkp_proto(js, target, key_str, key_len); 64 64 return off > 0 ? js_mktrue() : js_mkfalse(); 65 65 } 66 66 ··· 75 75 76 76 if (vtype(key) != T_STR) return js_mkfalse(); 77 77 78 - size_t key_len; 79 - char *key_str = js_getstr(js, key, &key_len); 78 + char *key_str = js_getstr(js, key, NULL); 80 79 if (!key_str) return js_mkfalse(); 81 80 82 - jsval_t result = js_setprop(js, target, key, js_mkundef()); 83 - (void)result; 84 - 85 - return js_mktrue(); 81 + bool deleted = js_del(js, target, key_str); 82 + return deleted ? js_mktrue() : js_mkfalse(); 86 83 } 87 84 88 85 static jsval_t reflect_own_keys(struct js *js, jsval_t *args, int nargs) {
-1
src/modules/textcodec.c
··· 2 2 #include <stdio.h> 3 3 #include <string.h> 4 4 5 - #include "internal.h" 6 5 #include "runtime.h" 7 6 #include "errors.h" 8 7 #include "internal.h"
+1 -1
src/modules/uri.c
··· 323 323 324 324 if (is_escape_unreserved(c)) { 325 325 out[out_len++] = (char)c; 326 - } else out_len += sprintf(out + out_len, "%%%02X", c); 326 + } else out_len += (size_t)snprintf(out + out_len, out_cap - out_len, "%%%02X", c); 327 327 } 328 328 329 329 out[out_len] = '\0';
+2 -2
src/roots.c
··· 5 5 6 6 jshdl_t js_root(struct js *js, jsval_t val) { 7 7 if (js->gc_roots_len >= js->gc_roots_cap) { 8 - int new_cap = js->gc_roots_cap ? js->gc_roots_cap * 2 : GC_ROOTS_INITIAL_CAP; 8 + jshdl_t new_cap = js->gc_roots_cap ? js->gc_roots_cap * 2 : GC_ROOTS_INITIAL_CAP; 9 9 jsval_t *new_roots = ant_realloc(js->gc_roots, new_cap * sizeof(jsval_t)); 10 10 if (!new_roots) { 11 11 fprintf(stderr, "FATAL: Out of memory allocating GC roots\n"); ··· 14 14 js->gc_roots = new_roots; 15 15 js->gc_roots_cap = new_cap; 16 16 } 17 - int idx = js->gc_roots_len++; 17 + jshdl_t idx = js->gc_roots_len++; 18 18 js->gc_roots[idx] = val; 19 19 return idx; 20 20 }