MCPcopy Create free account
hub / github.com/buke/quickjs-go / js_std_loadFile

Function js_std_loadFile

deps/quickjs/quickjs-libc.c:536–568  ·  view source on GitHub ↗

load a file as a UTF-8 encoded string or Uint8Array */

Source from the content-addressed store, hash-verified

534
535/* load a file as a UTF-8 encoded string or Uint8Array */
536static 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
570static JSValue js_std_writeFile(JSContext *ctx, JSValueConst this_val,
571 int argc, JSValueConst *argv)

Callers

nothing calls this directly

Calls 3

get_bool_optionFunction · 0.85
JS_ToCStringFunction · 0.85
js_load_fileFunction · 0.85

Tested by

no test coverage detected