@@ -1519,25 +1519,41 @@ static JSValue js_std_file_read_write(JSContext *ctx, JSValueConst this_val,
15191519 int argc , JSValueConst * argv , int magic )
15201520{
15211521 FILE * f = js_std_file_get (ctx , this_val );
1522+ bool is_write = (magic != 0 );
15221523 uint64_t pos , len ;
15231524 size_t size , ret ;
1525+ const char * str ;
15241526 uint8_t * buf ;
15251527
15261528 if (!f )
15271529 return JS_EXCEPTION ;
1528- if (JS_ToIndex (ctx , & pos , argv [1 ]))
1530+ pos = 0 ;
1531+ if (argc > 1 && JS_ToIndex (ctx , & pos , argv [1 ]))
15291532 return JS_EXCEPTION ;
1530- if (JS_ToIndex (ctx , & len , argv [2 ]))
1533+ len = 0 ;
1534+ if (argc > 2 && JS_ToIndex (ctx , & len , argv [2 ]))
15311535 return JS_EXCEPTION ;
1532- buf = JS_GetArrayBuffer (ctx , & size , argv [0 ]);
1536+ if (is_write && JS_IsString (argv [0 ])) {
1537+ str = JS_ToCStringLen (ctx , & size , argv [0 ]);
1538+ buf = (void * )str ;
1539+ } else {
1540+ str = NULL ;
1541+ buf = JS_GetArrayBuffer (ctx , & size , argv [0 ]);
1542+ }
15331543 if (!buf )
15341544 return JS_EXCEPTION ;
1545+ if (pos > size )
1546+ pos = size ;
1547+ if (argc < 3 )
1548+ len = size - pos ;
15351549 if (pos + len > size )
1536- return JS_ThrowRangeError ( ctx , "read/write array buffer overflow" ) ;
1537- if (magic )
1550+ len = size - pos ;
1551+ if (is_write ) {
15381552 ret = fwrite (buf + pos , 1 , len , f );
1539- else
1553+ } else {
15401554 ret = fread (buf + pos , 1 , len , f );
1555+ }
1556+ JS_FreeCString (ctx , str );
15411557 return JS_NewInt64 (ctx , ret );
15421558}
15431559
@@ -1909,8 +1925,8 @@ static const JSCFunctionListEntry js_std_file_proto_funcs[] = {
19091925 JS_CFUNC_DEF ("fileno" , 0 , js_std_file_fileno ),
19101926 JS_CFUNC_DEF ("error" , 0 , js_std_file_error ),
19111927 JS_CFUNC_DEF ("clearerr" , 0 , js_std_file_clearerr ),
1912- JS_CFUNC_MAGIC_DEF ("read" , 3 , js_std_file_read_write , 0 ),
1913- JS_CFUNC_MAGIC_DEF ("write" , 3 , js_std_file_read_write , 1 ),
1928+ JS_CFUNC_MAGIC_DEF ("read" , 1 , js_std_file_read_write , 0 ),
1929+ JS_CFUNC_MAGIC_DEF ("write" , 1 , js_std_file_read_write , 1 ),
19141930 JS_CFUNC_DEF ("getline" , 0 , js_std_file_getline ),
19151931 JS_CFUNC_MAGIC_DEF ("readAsArrayBuffer" , 0 , js_std_file_readAs , 0 ),
19161932 JS_CFUNC_MAGIC_DEF ("readAsString" , 0 , js_std_file_readAs , 1 ),
0 commit comments