Skip to content

Commit f02ba03

Browse files
committed
Add wasi patch to Lua 5.5.0
1 parent 8beeece commit f02ba03

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

lua-5.5.0/lauxlib.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
268268

269269
#if !defined(l_inspectstat) /* { */
270270

271-
#if defined(LUA_USE_POSIX)
271+
#if defined(LUA_USE_POSIX) && !defined(__wasi__)
272272

273273
#include <sys/wait.h>
274274

lua-5.5.0/liolib.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static int l_checkmode (const char *mode) {
9393

9494
#if !defined(l_getc) /* { */
9595

96-
#if defined(LUA_USE_POSIX)
96+
#if defined(LUA_USE_POSIX) && !defined(__wasi__)
9797
#define l_getc(f) getc_unlocked(f)
9898
#define l_lockfile(f) flockfile(f)
9999
#define l_unlockfile(f) funlockfile(f)
@@ -280,14 +280,17 @@ static int io_open (lua_State *L) {
280280
/*
281281
** function to close 'popen' files
282282
*/
283+
#if !defined(__wasi__)
283284
static int io_pclose (lua_State *L) {
284285
LStream *p = tolstream(L);
285286
errno = 0;
286287
return luaL_execresult(L, l_pclose(L, p->f));
287288
}
289+
#endif
288290

289291

290292
static int io_popen (lua_State *L) {
293+
#if !defined(__wasi__)
291294
const char *filename = luaL_checkstring(L, 1);
292295
const char *mode = luaL_optstring(L, 2, "r");
293296
LStream *p = newprefile(L);
@@ -296,14 +299,21 @@ static int io_popen (lua_State *L) {
296299
p->f = l_popen(L, filename, mode);
297300
p->closef = &io_pclose;
298301
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
302+
#else
303+
luaL_error(L, "not supported on WASI");
304+
#endif
299305
}
300306

301307

302308
static int io_tmpfile (lua_State *L) {
309+
#if !defined(__wasi__)
303310
LStream *p = newfile(L);
304311
errno = 0;
305312
p->f = tmpfile();
306313
return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
314+
#else
315+
luaL_error(L, "not supported on WASI");
316+
#endif
307317
}
308318

309319

lua-5.5.0/loslib.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
** ===================================================================
102102
*/
103103
#if !defined(lua_tmpnam) /* { */
104+
#if !defined(__wasi__) /* { */
104105

105106
#if defined(LUA_USE_POSIX) /* { */
106107

@@ -124,14 +125,15 @@
124125
#define LUA_TMPNAMBUFSIZE L_tmpnam
125126
#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
126127

128+
#endif /* } */
127129
#endif /* } */
128130

129131
#endif /* } */
130132
/* }================================================================== */
131133

132134

133135
#if !defined(l_system)
134-
#if defined(LUA_USE_IOS)
136+
#if defined(LUA_USE_IOS) || defined(__wasi__)
135137
/* Despite claiming to be ISO C, iOS does not implement 'system'. */
136138
#define l_system(cmd) ((cmd) == NULL ? 0 : -1)
137139
#else
@@ -170,13 +172,17 @@ static int os_rename (lua_State *L) {
170172

171173

172174
static int os_tmpname (lua_State *L) {
175+
#if !defined(__wasi__)
173176
char buff[LUA_TMPNAMBUFSIZE];
174177
int err;
175178
lua_tmpnam(buff, err);
176179
if (l_unlikely(err))
177180
return luaL_error(L, "unable to generate a unique filename");
178181
lua_pushstring(L, buff);
179182
return 1;
183+
#else
184+
luaL_error(L, "not supported on WASI");
185+
#endif
180186
}
181187

182188

@@ -187,8 +193,12 @@ static int os_getenv (lua_State *L) {
187193

188194

189195
static int os_clock (lua_State *L) {
196+
#if !defined(__wasi__)
190197
lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
191198
return 1;
199+
#else
200+
luaL_error(L, "not supported on WASI");
201+
#endif
192202
}
193203

194204

lua-5.5.0/ltablib.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#define ltablib_c
88
#define LUA_LIB
99

10+
#if defined(__wasi__)
11+
#include <stdlib.h>
12+
#endif
13+
1014
#include "lprefix.h"
1115

1216

0 commit comments

Comments
 (0)