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

Function js_std_file_readAs

deps/quickjs/quickjs-libc.c:1629–1673  ·  view source on GitHub ↗

XXX: could use less memory and go faster */

Source from the content-addressed store, hash-verified

1627
1628/* XXX: could use less memory and go faster */
1629static JSValue js_std_file_readAs(JSContext *ctx, JSValueConst this_val,
1630 int argc, JSValueConst *argv, int magic)
1631{
1632 FILE *f = js_std_file_get(ctx, this_val);
1633 int c;
1634 DynBuf dbuf;
1635 JSValue obj;
1636 uint64_t max_size64;
1637 size_t max_size;
1638 JSValueConst max_size_val;
1639
1640 if (!f)
1641 return JS_EXCEPTION;
1642
1643 if (argc >= 1)
1644 max_size_val = argv[0];
1645 else
1646 max_size_val = JS_UNDEFINED;
1647 max_size = (size_t)-1;
1648 if (!JS_IsUndefined(max_size_val)) {
1649 if (JS_ToIndex(ctx, &max_size64, max_size_val))
1650 return JS_EXCEPTION;
1651 if (max_size64 < max_size)
1652 max_size = max_size64;
1653 }
1654
1655 js_std_dbuf_init(ctx, &dbuf);
1656 while (max_size != 0) {
1657 c = fgetc(f);
1658 if (c == EOF)
1659 break;
1660 if (dbuf_putc(&dbuf, c)) {
1661 dbuf_free(&dbuf);
1662 return JS_EXCEPTION;
1663 }
1664 max_size--;
1665 }
1666 if (magic) {
1667 obj = JS_NewStringLen(ctx, (const char *)dbuf.buf, dbuf.size);
1668 } else {
1669 obj = JS_NewArrayBufferCopy(ctx, dbuf.buf, dbuf.size);
1670 }
1671 dbuf_free(&dbuf);
1672 return obj;
1673}
1674
1675static JSValue js_std_file_getByte(JSContext *ctx, JSValueConst this_val,
1676 int argc, JSValueConst *argv)

Callers

nothing calls this directly

Calls 5

js_std_file_getFunction · 0.85
JS_IsUndefinedFunction · 0.85
js_std_dbuf_initFunction · 0.85
dbuf_putcFunction · 0.85
dbuf_freeFunction · 0.85

Tested by

no test coverage detected