@@ -536,6 +536,68 @@ static JSValue js_std_loadFile(JSContext *ctx, JSValue this_val,
536536    return  ret ;
537537}
538538
539+ static  JSValue  js_std_writeFile (JSContext  * ctx , JSValue  this_val ,
540+                                 int  argc , JSValue  * argv )
541+ {
542+     const  char  * filename ;
543+     const  char  * mode ;
544+     const  void  * buf ;
545+     size_t  len , n ;
546+     JSValue  data , val , ret ;
547+     bool  release , unref ;
548+     FILE  * fp ;
549+ 
550+     ret  =  JS_EXCEPTION ;
551+     len  =  0 ;
552+     buf  =  "" ;
553+     mode  =  "w" ;
554+     data  =  argv [1 ];
555+     unref  =  false;
556+     release  =  false;
557+     filename  =  JS_ToCString (ctx , argv [0 ]);
558+     if  (!filename )
559+         return  JS_EXCEPTION ;
560+     if  (JS_IsObject (data )) {
561+         val  =  JS_GetPropertyStr (ctx , data , "buffer" );
562+         if  (JS_IsException (val ))
563+             goto exception ;
564+         if  (JS_IsArrayBuffer (val )) {
565+             data  =  val ;
566+             unref  =  true;
567+         } else  {
568+             JS_FreeValue (ctx , val );
569+         }
570+     }
571+     if  (JS_IsArrayBuffer (data )) {
572+         buf  =  JS_GetArrayBuffer (ctx , & len , data );
573+         mode  =  "wb" ;
574+     } else  if  (!JS_IsUndefined (data )) {
575+         buf  =  JS_ToCStringLen (ctx , & len , data );
576+         release  =  true;
577+     }
578+     if  (!buf )
579+         goto exception ;
580+     fp  =  fopen (filename , mode );
581+     if  (!fp ) {
582+         JS_ThrowPlainError (ctx , "error opening %s for writing" , filename );
583+         goto exception ;
584+     }
585+     n  =  fwrite (buf , len , 1 , fp );
586+     fclose (fp );
587+     if  (n  !=  1 ) {
588+         JS_ThrowPlainError (ctx , "error writing to %s" , filename );
589+         goto exception ;
590+     }
591+     ret  =  JS_UNDEFINED ;
592+ exception :
593+     JS_FreeCString (ctx , filename );
594+     if  (release )
595+         JS_FreeCString (ctx , buf );
596+     if  (unref )
597+         JS_FreeValue (ctx , data );
598+     return  ret ;
599+ }
600+ 
539601typedef  JSModuleDef  * (JSInitModuleFunc )(JSContext  * ctx ,
540602                                        const  char  * module_name );
541603
@@ -1674,6 +1736,7 @@ static const JSCFunctionListEntry js_std_funcs[] = {
16741736    JS_CFUNC_DEF ("urlGet" , 1 , js_std_urlGet  ),
16751737#endif 
16761738    JS_CFUNC_DEF ("loadFile" , 1 , js_std_loadFile  ),
1739+     JS_CFUNC_DEF ("writeFile" , 2 , js_std_writeFile  ),
16771740    JS_CFUNC_DEF ("strerror" , 1 , js_std_strerror  ),
16781741
16791742    /* FILE I/O */ 
0 commit comments