load a file as a UTF-8 encoded string or Uint8Array */
| 534 | |
| 535 | /* load a file as a UTF-8 encoded string or Uint8Array */ |
| 536 | static JSValue js_std_loadFile(JSContext *ctx, JSValueConst this_val, |
| 537 | int argc, JSValueConst *argv) |
| 538 | { |
| 539 | uint8_t *buf; |
| 540 | const char *filename; |
| 541 | JSValueConst options_obj; |
| 542 | JSValue ret; |
| 543 | size_t buf_len; |
| 544 | bool binary = false; |
| 545 | |
| 546 | if (argc >= 2) { |
| 547 | options_obj = argv[1]; |
| 548 | if (get_bool_option(ctx, &binary, options_obj, |
| 549 | "binary")) |
| 550 | return JS_EXCEPTION; |
| 551 | } |
| 552 | |
| 553 | filename = JS_ToCString(ctx, argv[0]); |
| 554 | if (!filename) |
| 555 | return JS_EXCEPTION; |
| 556 | buf = js_load_file(ctx, &buf_len, filename); |
| 557 | JS_FreeCString(ctx, filename); |
| 558 | if (!buf) |
| 559 | return JS_NULL; |
| 560 | if (binary) { |
| 561 | ret = JS_NewUint8Array(ctx, buf, buf_len, free_buf, NULL, false); |
| 562 | } else { |
| 563 | ret = JS_NewStringLen(ctx, (char *)buf, buf_len); |
| 564 | js_free(ctx, buf); |
| 565 | } |
| 566 | |
| 567 | return ret; |
| 568 | } |
| 569 | |
| 570 | static JSValue js_std_writeFile(JSContext *ctx, JSValueConst this_val, |
| 571 | int argc, JSValueConst *argv) |
nothing calls this directly
no test coverage detected