@@ -478,14 +478,41 @@ static JSValue js_loadScript(JSContext *ctx, JSValue this_val,
478
478
return ret ;
479
479
}
480
480
481
- /* load a file as a UTF-8 encoded string */
481
+ static int get_bool_option (JSContext * ctx , BOOL * pbool ,
482
+ JSValue obj ,
483
+ const char * option )
484
+ {
485
+ JSValue val ;
486
+ val = JS_GetPropertyStr (ctx , obj , option );
487
+ if (JS_IsException (val ))
488
+ return -1 ;
489
+ if (!JS_IsUndefined (val )) {
490
+ * pbool = JS_ToBool (ctx , val );
491
+ }
492
+ JS_FreeValue (ctx , val );
493
+ return 0 ;
494
+ }
495
+
496
+ static void free_buf (JSRuntime * rt , void * opaque , void * ptr ) {
497
+ js_free_rt (rt , ptr );
498
+ }
499
+
500
+ /* load a file as a UTF-8 encoded string or Uint8Array */
482
501
static JSValue js_std_loadFile (JSContext * ctx , JSValue this_val ,
483
502
int argc , JSValue * argv )
484
503
{
485
504
uint8_t * buf ;
486
505
const char * filename ;
487
- JSValue ret ;
506
+ JSValue ret , options_obj ;
488
507
size_t buf_len ;
508
+ BOOL binary = FALSE;
509
+
510
+ if (argc >= 2 ) {
511
+ options_obj = argv [1 ];
512
+ if (get_bool_option (ctx , & binary , options_obj ,
513
+ "binary" ))
514
+ return JS_EXCEPTION ;
515
+ }
489
516
490
517
filename = JS_ToCString (ctx , argv [0 ]);
491
518
if (!filename )
@@ -494,8 +521,13 @@ static JSValue js_std_loadFile(JSContext *ctx, JSValue this_val,
494
521
JS_FreeCString (ctx , filename );
495
522
if (!buf )
496
523
return JS_NULL ;
497
- ret = JS_NewStringLen (ctx , (char * )buf , buf_len );
498
- js_free (ctx , buf );
524
+ if (binary ) {
525
+ ret = JS_NewUint8Array (ctx , buf , buf_len , free_buf , NULL , FALSE);
526
+ } else {
527
+ ret = JS_NewStringLen (ctx , (char * )buf , buf_len );
528
+ js_free (ctx , buf );
529
+ }
530
+
499
531
return ret ;
500
532
}
501
533
@@ -822,21 +854,6 @@ static int interrupt_handler(JSRuntime *rt, void *opaque)
822
854
return (os_pending_signals >> SIGINT ) & 1 ;
823
855
}
824
856
825
- static int get_bool_option (JSContext * ctx , BOOL * pbool ,
826
- JSValue obj ,
827
- const char * option )
828
- {
829
- JSValue val ;
830
- val = JS_GetPropertyStr (ctx , obj , option );
831
- if (JS_IsException (val ))
832
- return -1 ;
833
- if (!JS_IsUndefined (val )) {
834
- * pbool = JS_ToBool (ctx , val );
835
- }
836
- JS_FreeValue (ctx , val );
837
- return 0 ;
838
- }
839
-
840
857
static JSValue js_evalScript (JSContext * ctx , JSValue this_val ,
841
858
int argc , JSValue * argv )
842
859
{
0 commit comments