@@ -478,14 +478,41 @@ static JSValue js_loadScript(JSContext *ctx, JSValue this_val,
478478 return ret ;
479479}
480480
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 */
482501static JSValue js_std_loadFile (JSContext * ctx , JSValue this_val ,
483502 int argc , JSValue * argv )
484503{
485504 uint8_t * buf ;
486505 const char * filename ;
487- JSValue ret ;
506+ JSValue ret , options_obj ;
488507 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+ }
489516
490517 filename = JS_ToCString (ctx , argv [0 ]);
491518 if (!filename )
@@ -494,8 +521,13 @@ static JSValue js_std_loadFile(JSContext *ctx, JSValue this_val,
494521 JS_FreeCString (ctx , filename );
495522 if (!buf )
496523 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+
499531 return ret ;
500532}
501533
@@ -822,21 +854,6 @@ static int interrupt_handler(JSRuntime *rt, void *opaque)
822854 return (os_pending_signals >> SIGINT ) & 1 ;
823855}
824856
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-
840857static JSValue js_evalScript (JSContext * ctx , JSValue this_val ,
841858 int argc , JSValue * argv )
842859{
0 commit comments