Skip to content

Instantly share code, notes, and snippets.

@alexcrichton
Created September 11, 2025 23:21
Show Gist options
  • Save alexcrichton/a87eb1ce5206445936bd198720f34e0c to your computer and use it in GitHub Desktop.
Save alexcrichton/a87eb1ce5206445936bd198720f34e0c to your computer and use it in GitHub Desktop.
diff --git a/lua-5.4.8/lauxlib.c b/lua-5.4.8/lauxlib.c
index 923105e..6df05d0 100644
--- a/lua-5.4.8/lauxlib.c
+++ b/lua-5.4.8/lauxlib.c
@@ -265,7 +265,7 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
#if !defined(l_inspectstat) /* { */
-#if defined(LUA_USE_POSIX)
+#if defined(LUA_USE_POSIX) && !defined(__wasi__)
#include <sys/wait.h>
diff --git a/lua-5.4.8/liolib.c b/lua-5.4.8/liolib.c
index c5075f3..fa33d8e 100644
--- a/lua-5.4.8/liolib.c
+++ b/lua-5.4.8/liolib.c
@@ -94,7 +94,7 @@ static int l_checkmode (const char *mode) {
#if !defined(l_getc) /* { */
-#if defined(LUA_USE_POSIX)
+#if defined(LUA_USE_POSIX) && !defined(__wasi__)
#define l_getc(f) getc_unlocked(f)
#define l_lockfile(f) flockfile(f)
#define l_unlockfile(f) funlockfile(f)
@@ -281,14 +281,19 @@ static int io_open (lua_State *L) {
/*
** function to close 'popen' files
*/
+#if !defined(__wasi__)
static int io_pclose (lua_State *L) {
LStream *p = tolstream(L);
errno = 0;
return luaL_execresult(L, l_pclose(L, p->f));
}
+#endif
static int io_popen (lua_State *L) {
+#if defined(__wasi__)
+ return luaL_error(L, "not supported on WASI");
+#else
const char *filename = luaL_checkstring(L, 1);
const char *mode = luaL_optstring(L, 2, "r");
LStream *p = newprefile(L);
@@ -297,14 +302,19 @@ static int io_popen (lua_State *L) {
p->f = l_popen(L, filename, mode);
p->closef = &io_pclose;
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
+#endif
}
static int io_tmpfile (lua_State *L) {
+#if defined(__wasi__)
+ return luaL_error(L, "not supported on WASI");
+#else
LStream *p = newfile(L);
errno = 0;
p->f = tmpfile();
return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
+#endif
}
diff --git a/lua-5.4.8/loslib.c b/lua-5.4.8/loslib.c
index ba80d72..aea53da 100644
--- a/lua-5.4.8/loslib.c
+++ b/lua-5.4.8/loslib.c
@@ -100,6 +100,7 @@
** ===================================================================
*/
#if !defined(lua_tmpnam) /* { */
+#if !defined(__wasi__) /* { */
#if defined(LUA_USE_POSIX) /* { */
@@ -123,6 +124,7 @@
#define LUA_TMPNAMBUFSIZE L_tmpnam
#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
+#endif /* } */
#endif /* } */
#endif /* } */
@@ -130,7 +132,7 @@
#if !defined(l_system)
-#if defined(LUA_USE_IOS)
+#if defined(LUA_USE_IOS) || defined(__wasi__)
/* Despite claiming to be ISO C, iOS does not implement 'system'. */
#define l_system(cmd) ((cmd) == NULL ? 0 : -1)
#else
@@ -169,6 +171,9 @@ static int os_rename (lua_State *L) {
static int os_tmpname (lua_State *L) {
+#if defined(__wasi__)
+ return luaL_error(L, "not supported on WASI");
+#else
char buff[LUA_TMPNAMBUFSIZE];
int err;
lua_tmpnam(buff, err);
@@ -176,6 +181,7 @@ static int os_tmpname (lua_State *L) {
return luaL_error(L, "unable to generate a unique filename");
lua_pushstring(L, buff);
return 1;
+#endif
}
@@ -186,7 +192,11 @@ static int os_getenv (lua_State *L) {
static int os_clock (lua_State *L) {
+#if defined(__wasi__)
+ lua_pushnumber(L, (lua_Number)-1);
+#else
lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
+#endif
return 1;
}
@@ -415,7 +425,9 @@ static const luaL_Reg syslib[] = {
{"rename", os_rename},
{"setlocale", os_setlocale},
{"time", os_time},
+#if !defined(__wasi__)
{"tmpname", os_tmpname},
+#endif
{NULL, NULL}
};
diff --git a/lua-5.4.8/ltablib.c b/lua-5.4.8/ltablib.c
index e6bc4d0..39d1b51 100644
--- a/lua-5.4.8/ltablib.c
+++ b/lua-5.4.8/ltablib.c
@@ -244,6 +244,9 @@ typedef unsigned int IdxT;
** is to copy them to an array of a known type and use the array values.
*/
static unsigned int l_randomizePivot (void) {
+#if defined(__wasi__)
+ return ~0;
+#else
clock_t c = clock();
time_t t = time(NULL);
unsigned int buff[sof(c) + sof(t)];
@@ -253,6 +256,7 @@ static unsigned int l_randomizePivot (void) {
for (i = 0; i < sof(buff); i++)
rnd += buff[i];
return rnd;
+#endif
}
#endif /* } */
diff --git a/src/lib.rs b/src/lib.rs
index 01c77ed..9d187e1 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -130,6 +130,7 @@ impl Build {
}
}
+ let mut libs = vec![version.lib_name().to_string()];
match target {
_ if target.contains("linux") => {
config.define("LUA_USE_LINUX", None);
@@ -187,6 +188,16 @@ impl Build {
}
source_dir = cpp_source_dir
}
+ _ if target.contains("wasi") => {
+ config.define("LUA_USE_POSIX", None);
+ config.flag("-mllvm").flag("-wasm-enable-eh");
+ config.flag("-mllvm").flag("-wasm-use-legacy-eh=false");
+ config.flag("-mllvm").flag("-wasm-enable-sjlj");
+ libs.push("setjmp".to_string());
+
+ config.define("_WASI_EMULATED_SIGNAL", None);
+ libs.push("wasi-emulated-signal".to_string());
+ }
_ => Err(format!("don't know how to build Lua for {target}"))?,
}
@@ -232,7 +243,7 @@ impl Build {
Ok(Artifacts {
include_dir,
lib_dir,
- libs: vec![version.lib_name().to_string()],
+ libs,
})
}
}
@@ -280,7 +291,7 @@ impl Artifacts {
pub fn print_cargo_metadata(&self) {
println!("cargo:rustc-link-search=native={}", self.lib_dir.display());
for lib in self.libs.iter() {
- println!("cargo:rustc-link-lib=static={lib}");
+ println!("cargo:rustc-link-lib=static:-bundle={lib}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment