| 1548 | } |
| 1549 | |
| 1550 | static JSValue js_std_file_read_write(JSContext *ctx, JSValueConst this_val, |
| 1551 | int argc, JSValueConst *argv, int magic) |
| 1552 | { |
| 1553 | FILE *f = js_std_file_get(ctx, this_val); |
| 1554 | bool is_write = (magic != 0); |
| 1555 | uint64_t pos, len; |
| 1556 | size_t size, ret; |
| 1557 | const char *str; |
| 1558 | uint8_t *buf; |
| 1559 | |
| 1560 | if (!f) |
| 1561 | return JS_EXCEPTION; |
| 1562 | pos = 0; |
| 1563 | if (argc > 1 && JS_ToIndex(ctx, &pos, argv[1])) |
| 1564 | return JS_EXCEPTION; |
| 1565 | len = 0; |
| 1566 | if (argc > 2 && JS_ToIndex(ctx, &len, argv[2])) |
| 1567 | return JS_EXCEPTION; |
| 1568 | if (is_write && JS_IsString(argv[0])) { |
| 1569 | str = JS_ToCStringLen(ctx, &size, argv[0]); |
| 1570 | buf = (void *)str; |
| 1571 | } else { |
| 1572 | str = NULL; |
| 1573 | buf = JS_GetArrayBuffer(ctx, &size, argv[0]); |
| 1574 | } |
| 1575 | if (!buf) |
| 1576 | return JS_EXCEPTION; |
| 1577 | if (pos > size) |
| 1578 | pos = size; |
| 1579 | if (argc < 3) |
| 1580 | len = size - pos; |
| 1581 | if (pos + len > size) |
| 1582 | len = size - pos; |
| 1583 | if (is_write) { |
| 1584 | ret = fwrite(buf + pos, 1, len, f); |
| 1585 | } else { |
| 1586 | ret = fread(buf + pos, 1, len, f); |
| 1587 | } |
| 1588 | JS_FreeCString(ctx, str); |
| 1589 | return JS_NewInt64(ctx, ret); |
| 1590 | } |
| 1591 | |
| 1592 | /* XXX: could use less memory and go faster */ |
| 1593 | static JSValue js_std_file_getline(JSContext *ctx, JSValueConst this_val, |
nothing calls this directly
no test coverage detected