59
59
#define getcwd _getcwd
60
60
#define chdir _chdir
61
61
#else
62
+ #include <sys/ioctl.h>
63
+ #if !defined(__wasi__ )
62
64
#include <dlfcn.h>
63
65
#include <termios.h>
64
- #include <sys/ioctl.h>
65
66
#include <sys/resource.h>
66
67
#include <sys/wait.h>
68
+ #endif
67
69
68
70
#if defined(__APPLE__ )
69
71
typedef sig_t sighandler_t ;
@@ -76,10 +78,12 @@ typedef sig_t sighandler_t;
76
78
extern char * * environ ;
77
79
#endif
78
80
81
+ #endif /* _WIN32 */
82
+
83
+ #if !defined(_WIN32 ) && !defined(__wasi__ )
79
84
/* enable the os.Worker API. IT relies on POSIX threads */
80
85
#define USE_WORKER
81
-
82
- #endif /* _WIN32 */
86
+ #endif
83
87
84
88
#ifdef USE_WORKER
85
89
#include <pthread.h>
@@ -472,7 +476,7 @@ typedef JSModuleDef *(JSInitModuleFunc)(JSContext *ctx,
472
476
const char * module_name );
473
477
474
478
475
- #if defined(_WIN32 )
479
+ #if defined(_WIN32 ) || defined( __wasi__ )
476
480
static JSModuleDef * js_module_loader_so (JSContext * ctx ,
477
481
const char * module_name )
478
482
{
@@ -549,7 +553,7 @@ int js_module_set_import_meta(JSContext *ctx, JSValue func_val,
549
553
return -1 ;
550
554
if (!strchr (module_name , ':' )) {
551
555
strcpy (buf , "file://" );
552
- #if !defined(_WIN32 )
556
+ #if !defined(_WIN32 ) && !defined( __wasi__ )
553
557
/* realpath() cannot be used with modules compiled with qjsc
554
558
because the corresponding module source code is not
555
559
necessarily present */
@@ -816,9 +820,11 @@ static void js_std_file_finalizer(JSRuntime *rt, JSValue val)
816
820
JSSTDFile * s = JS_GetOpaque (val , js_std_file_class_id );
817
821
if (s ) {
818
822
if (s -> f && s -> close_in_finalizer ) {
823
+ #if !defined(__wasi__ )
819
824
if (s -> is_popen )
820
825
pclose (s -> f );
821
826
else
827
+ #endif
822
828
fclose (s -> f );
823
829
}
824
830
js_free_rt (rt , s );
@@ -905,6 +911,7 @@ static JSValue js_std_open(JSContext *ctx, JSValue this_val,
905
911
return JS_EXCEPTION ;
906
912
}
907
913
914
+ #if !defined(__wasi__ )
908
915
static JSValue js_std_popen (JSContext * ctx , JSValue this_val ,
909
916
int argc , JSValue * argv )
910
917
{
@@ -940,6 +947,7 @@ static JSValue js_std_popen(JSContext *ctx, JSValue this_val,
940
947
JS_FreeCString (ctx , mode );
941
948
return JS_EXCEPTION ;
942
949
}
950
+ #endif // !defined(__wasi__)
943
951
944
952
static JSValue js_std_fdopen (JSContext * ctx , JSValue this_val ,
945
953
int argc , JSValue * argv )
@@ -974,6 +982,7 @@ static JSValue js_std_fdopen(JSContext *ctx, JSValue this_val,
974
982
return JS_EXCEPTION ;
975
983
}
976
984
985
+ #if !defined(__wasi__ )
977
986
static JSValue js_std_tmpfile (JSContext * ctx , JSValue this_val ,
978
987
int argc , JSValue * argv )
979
988
{
@@ -985,6 +994,7 @@ static JSValue js_std_tmpfile(JSContext *ctx, JSValue this_val,
985
994
return JS_NULL ;
986
995
return js_new_std_file (ctx , f , TRUE, FALSE);
987
996
}
997
+ #endif
988
998
989
999
static JSValue js_std_sprintf (JSContext * ctx , JSValue this_val ,
990
1000
int argc , JSValue * argv )
@@ -1045,9 +1055,11 @@ static JSValue js_std_file_close(JSContext *ctx, JSValue this_val,
1045
1055
return JS_EXCEPTION ;
1046
1056
if (!s -> f )
1047
1057
return JS_ThrowTypeError (ctx , "invalid file handle" );
1058
+ #if !defined(__wasi__ )
1048
1059
if (s -> is_popen )
1049
1060
err = js_get_errno (pclose (s -> f ));
1050
1061
else
1062
+ #endif
1051
1063
err = js_get_errno (fclose (s -> f ));
1052
1064
s -> f = NULL ;
1053
1065
return JS_NewInt32 (ctx , err );
@@ -1277,6 +1289,7 @@ static JSValue js_std_file_putByte(JSContext *ctx, JSValue this_val,
1277
1289
}
1278
1290
1279
1291
/* urlGet */
1292
+ #if !defined(__wasi__ )
1280
1293
1281
1294
#define URL_GET_PROGRAM "curl -s -i"
1282
1295
#define URL_GET_BUF_SIZE 4096
@@ -1461,6 +1474,7 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValue this_val,
1461
1474
JS_FreeValue (ctx , response );
1462
1475
return JS_EXCEPTION ;
1463
1476
}
1477
+ #endif // !defined(__wasi__)
1464
1478
1465
1479
static JSClassDef js_std_file_class = {
1466
1480
"FILE" ,
@@ -1493,15 +1507,19 @@ static const JSCFunctionListEntry js_std_funcs[] = {
1493
1507
JS_CFUNC_DEF ("setenv" , 1 , js_std_setenv ),
1494
1508
JS_CFUNC_DEF ("unsetenv" , 1 , js_std_unsetenv ),
1495
1509
JS_CFUNC_DEF ("getenviron" , 1 , js_std_getenviron ),
1510
+ #if !defined (__wasi__ )
1496
1511
JS_CFUNC_DEF ("urlGet" , 1 , js_std_urlGet ),
1512
+ #endif
1497
1513
JS_CFUNC_DEF ("loadFile" , 1 , js_std_loadFile ),
1498
1514
JS_CFUNC_DEF ("strerror" , 1 , js_std_strerror ),
1499
1515
1500
1516
/* FILE I/O */
1501
1517
JS_CFUNC_DEF ("open" , 2 , js_std_open ),
1518
+ #if !defined (__wasi__ )
1502
1519
JS_CFUNC_DEF ("popen" , 2 , js_std_popen ),
1503
- JS_CFUNC_DEF ("fdopen" , 2 , js_std_fdopen ),
1504
1520
JS_CFUNC_DEF ("tmpfile" , 0 , js_std_tmpfile ),
1521
+ #endif
1522
+ JS_CFUNC_DEF ("fdopen" , 2 , js_std_fdopen ),
1505
1523
JS_CFUNC_MAGIC_DEF ("puts" , 1 , js_std_file_puts , 0 ),
1506
1524
JS_CFUNC_DEF ("printf" , 1 , js_std_printf ),
1507
1525
JS_CFUNC_DEF ("sprintf" , 1 , js_std_sprintf ),
@@ -1714,7 +1732,7 @@ static JSValue js_os_ttySetRaw(JSContext *ctx, JSValue this_val,
1714
1732
}
1715
1733
return JS_UNDEFINED ;
1716
1734
}
1717
- #else
1735
+ #elif !defined( __wasi__ )
1718
1736
static JSValue js_os_ttyGetWinSize (JSContext * ctx , JSValue this_val ,
1719
1737
int argc , JSValue * argv )
1720
1738
{
@@ -1773,7 +1791,7 @@ static JSValue js_os_ttySetRaw(JSContext *ctx, JSValue this_val,
1773
1791
return JS_UNDEFINED ;
1774
1792
}
1775
1793
1776
- #endif /* !_WIN32 */
1794
+ #endif /* !_WIN32 && !__wasi__ */
1777
1795
1778
1796
static JSValue js_os_remove (JSContext * ctx , JSValue this_val ,
1779
1797
int argc , JSValue * argv )
@@ -1967,7 +1985,7 @@ static JSValue js_os_signal(JSContext *ctx, JSValue this_val,
1967
1985
return JS_UNDEFINED ;
1968
1986
}
1969
1987
1970
- #ifndef _WIN32
1988
+ #if !defined( _WIN32 ) && !defined( __wasi__ )
1971
1989
static JSValue js_os_cputime (JSContext * ctx , JSValue this_val ,
1972
1990
int argc , JSValue * argv )
1973
1991
{
@@ -2658,6 +2676,7 @@ static char *realpath(const char *path, char *buf)
2658
2676
}
2659
2677
#endif
2660
2678
2679
+ #if !defined(__wasi__ )
2661
2680
/* return [path, errorcode] */
2662
2681
static JSValue js_os_realpath (JSContext * ctx , JSValue this_val ,
2663
2682
int argc , JSValue * argv )
@@ -2679,8 +2698,9 @@ static JSValue js_os_realpath(JSContext *ctx, JSValue this_val,
2679
2698
}
2680
2699
return make_string_error (ctx , buf , err );
2681
2700
}
2701
+ #endif
2682
2702
2683
- #if !defined(_WIN32 )
2703
+ #if !defined(_WIN32 ) && !defined( __wasi__ )
2684
2704
static JSValue js_os_symlink (JSContext * ctx , JSValue this_val ,
2685
2705
int argc , JSValue * argv )
2686
2706
{
@@ -3593,6 +3613,8 @@ void js_std_set_worker_new_context_func(JSContext *(*func)(JSRuntime *rt))
3593
3613
#define OS_PLATFORM "netbsd"
3594
3614
#elif defined(__FreeBSD__ )
3595
3615
#define OS_PLATFORM "freebsd"
3616
+ #elif defined(__wasi__ )
3617
+ #define OS_PLATFORM "wasi"
3596
3618
#else
3597
3619
#define OS_PLATFORM "unknown"
3598
3620
#endif
@@ -3617,8 +3639,10 @@ static const JSCFunctionListEntry js_os_funcs[] = {
3617
3639
JS_CFUNC_MAGIC_DEF ("read" , 4 , js_os_read_write , 0 ),
3618
3640
JS_CFUNC_MAGIC_DEF ("write" , 4 , js_os_read_write , 1 ),
3619
3641
JS_CFUNC_DEF ("isatty" , 1 , js_os_isatty ),
3642
+ #if !defined (__wasi__ )
3620
3643
JS_CFUNC_DEF ("ttyGetWinSize" , 1 , js_os_ttyGetWinSize ),
3621
3644
JS_CFUNC_DEF ("ttySetRaw" , 1 , js_os_ttySetRaw ),
3645
+ #endif
3622
3646
JS_CFUNC_DEF ("remove" , 1 , js_os_remove ),
3623
3647
JS_CFUNC_DEF ("rename" , 2 , js_os_rename ),
3624
3648
JS_CFUNC_MAGIC_DEF ("setReadHandler" , 2 , js_os_setReadHandler , 0 ),
@@ -3630,7 +3654,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
3630
3654
OS_FLAG (SIGILL ),
3631
3655
OS_FLAG (SIGSEGV ),
3632
3656
OS_FLAG (SIGTERM ),
3633
- #if !defined (_WIN32 )
3657
+ #if !defined (_WIN32 ) && ! defined ( __wasi__ )
3634
3658
OS_FLAG (SIGQUIT ),
3635
3659
OS_FLAG (SIGPIPE ),
3636
3660
OS_FLAG (SIGALRM ),
@@ -3668,8 +3692,10 @@ static const JSCFunctionListEntry js_os_funcs[] = {
3668
3692
JS_CFUNC_MAGIC_DEF ("stat" , 1 , js_os_stat , 0 ),
3669
3693
JS_CFUNC_DEF ("utimes" , 3 , js_os_utimes ),
3670
3694
JS_CFUNC_DEF ("sleep" , 1 , js_os_sleep ),
3695
+ #if !defined (__wasi__ )
3671
3696
JS_CFUNC_DEF ("realpath" , 1 , js_os_realpath ),
3672
- #if !defined (_WIN32 )
3697
+ #endif
3698
+ #if !defined (_WIN32 ) && !defined (__wasi__ )
3673
3699
JS_CFUNC_MAGIC_DEF ("lstat" , 1 , js_os_stat , 1 ),
3674
3700
JS_CFUNC_DEF ("symlink" , 2 , js_os_symlink ),
3675
3701
JS_CFUNC_DEF ("readlink" , 1 , js_os_readlink ),
0 commit comments