| 452 | #endif // __GNUC__ || __clang__ |
| 453 | |
| 454 | uint8_t *js_load_file(JSContext *ctx, size_t *pbuf_len, const char *filename) |
| 455 | { |
| 456 | FILE *f; |
| 457 | size_t n, len; |
| 458 | uint8_t *p, *buf, tmp[8192]; |
| 459 | |
| 460 | f = fopen(filename, "rb"); |
| 461 | if (!f) |
| 462 | return NULL; |
| 463 | buf = NULL; |
| 464 | len = 0; |
| 465 | do { |
| 466 | n = fread(tmp, 1, sizeof(tmp), f); |
| 467 | if (ctx) { |
| 468 | p = js_realloc(ctx, buf, len + n + 1); |
| 469 | } else { |
| 470 | p = realloc(buf, len + n + 1); |
| 471 | } |
| 472 | if (!p) { |
| 473 | if (ctx) { |
| 474 | js_free(ctx, buf); |
| 475 | } else { |
| 476 | free(buf); |
| 477 | } |
| 478 | fclose(f); |
| 479 | return NULL; |
| 480 | } |
| 481 | memcpy(&p[len], tmp, n); |
| 482 | buf = p; |
| 483 | len += n; |
| 484 | buf[len] = '\0'; |
| 485 | } while (n == sizeof(tmp)); |
| 486 | fclose(f); |
| 487 | *pbuf_len = len; |
| 488 | return buf; |
| 489 | } |
| 490 | |
| 491 | /* load and evaluate a file */ |
| 492 | static JSValue js_loadScript(JSContext *ctx, JSValueConst this_val, |
no outgoing calls