Skip to content

Commit cc8041d

Browse files
committed
Add support for WASI targets to Lua sources
This commit is an attempt to work towards mlua-rs/mlua#366 and support WASI targets in the `mlua` crate. This requires that the Lua source code can be compiled to WASI targets such as `wasm32-wasip{1,2}`. The C toolchain used for this is [wasi-sdk] and does not support all that Lua requires out-of-the-box. This necessitates some edits to Lua sources to exclude exposing unsupported functions to scripts. This additionally updates CI to test the `wasm32-wasip2` target inside of Wasmtime. This currently requires a `dev` release of Wasmtime (builds from `main`) but the Wasmtime 37.0.0 release next week will suffice once it's available due to WebAssembly exception-handling support. I've also dropped the Emscripten tests here as CI wasn't actually testing emscripten (it forgot `--target`) and the tests are otherwise broken if re-enabled. Installation of dependencies on CI has additionally been refactored to a shared "composite" action between the build/test workflows to avoid duplication between them. [wasi-sdk]: https://github.com/WebAssembly/wasi-sdk
1 parent 8ebb964 commit cc8041d

File tree

18 files changed

+206
-68
lines changed

18 files changed

+206
-68
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: 'Setup various CI dependencies'
2+
inputs:
3+
target:
4+
description: 'rust target being set up'
5+
required: true
6+
wasi_sdk:
7+
description: 'wasi-sdk version to install'
8+
default: 27
9+
wasmtime:
10+
description: 'wasmtime version to install'
11+
default: dev
12+
13+
runs:
14+
using: composite
15+
steps:
16+
- uses: dtolnay/rust-toolchain@stable
17+
with:
18+
target: ${{ inputs.target }}
19+
- run: echo "CARGO_BUILD_TARGET=${{ matrix.target }}" >> $GITHUB_ENV
20+
shell: bash
21+
22+
- name: Install GCC (i686-unknown-linux-gnu)
23+
if: ${{ inputs.target == 'i686-unknown-linux-gnu' }}
24+
run: |
25+
sudo apt-get update -y
26+
sudo apt-get install -y --no-install-recommends gcc-multilib
27+
shell: bash
28+
- name: Install GCC (aarch64-unknown-linux-gnu)
29+
if: ${{ inputs.target == 'aarch64-unknown-linux-gnu' }}
30+
run: |
31+
sudo apt-get update -y
32+
sudo apt-get install -y --no-install-recommends gcc-aarch64-linux-gnu libc6-dev-arm64-cross
33+
shell: bash
34+
- name: Install GCC (arm-unknown-linux-gnueabi)
35+
if: ${{ inputs.target == 'arm-unknown-linux-gnueabi' }}
36+
run: |
37+
sudo apt-get update -y
38+
sudo apt-get install -y --no-install-recommends gcc-arm-linux-gnueabi libc6-dev-armel-cross
39+
shell: bash
40+
- name: Install GCC (x86_64-pc-windows-gnu)
41+
if: ${{ inputs.target == 'x86_64-pc-windows-gnu' }}
42+
run: |
43+
sudo apt-get update -y
44+
sudo apt-get install -y --no-install-recommends gcc-mingw-w64-x86-64
45+
shell: bash
46+
- name: Install emscripten (wasm32-unknown-emscripten)
47+
if: ${{ inputs.target == 'wasm32-unknown-emscripten' }}
48+
run: |
49+
sudo apt-get update -y
50+
sudo apt-get install -y --no-install-recommends emscripten
51+
shell: bash
52+
53+
- name: Install wasi-sdk/Wasmtime (wasm32-wasip2)
54+
if: ${{ inputs.target == 'wasm32-wasip2' }}
55+
working-directory: ${{ runner.tool_cache }}
56+
run: |
57+
curl -LO https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${{ inputs.wasi_sdk }}/wasi-sdk-${{ inputs.wasi_sdk }}.0-x86_64-linux.tar.gz
58+
tar xf wasi-sdk-${{ inputs.wasi_sdk }}.0-x86_64-linux.tar.gz
59+
WASI_SDK_PATH=`pwd`/wasi-sdk-${{ inputs.wasi_sdk }}.0-x86_64-linux
60+
echo "WASI_SDK_PATH=$WASI_SDK_PATH" >> $GITHUB_ENV
61+
echo "CC_wasm32_wasip2=$WASI_SDK_PATH/bin/clang" >> $GITHUB_ENV
62+
echo "CARGO_TARGET_WASM32_WASIP2_LINKER=$WASI_SDK_PATH/bin/clang" >> $GITHUB_ENV
63+
echo "CARGO_TARGET_WASM32_WASIP2_RUSTFLAGS=-Clink-arg=-Wl,--export=cabi_realloc" >> $GITHUB_ENV
64+
65+
curl -LO https://github.com/bytecodealliance/wasmtime/releases/download/${{ inputs.wasmtime }}/wasmtime-${{ inputs.wasmtime }}-x86_64-linux.tar.xz
66+
tar xf wasmtime-${{ inputs.wasmtime }}-x86_64-linux.tar.xz
67+
echo "CARGO_TARGET_WASM32_WASIP2_RUNNER=`pwd`/wasmtime-${{ inputs.wasmtime }}-x86_64-linux/wasmtime -W exceptions" >> $GITHUB_ENV
68+
shell: bash

.github/workflows/main.yml

Lines changed: 15 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
- x86_64-pc-windows-gnu
1818
- x86_64-pc-windows-msvc
1919
- wasm32-unknown-emscripten
20+
- wasm32-wasip2
2021
lua: [lua54, lua53, lua52, lua51]
2122
include:
2223
- target: x86_64-unknown-linux-gnu
@@ -37,44 +38,16 @@ jobs:
3738
os: windows-latest
3839
- target: wasm32-unknown-emscripten
3940
os: ubuntu-latest
41+
- target: wasm32-wasip2
42+
os: ubuntu-latest
4043
steps:
4144
- uses: actions/checkout@main
42-
- uses: dtolnay/rust-toolchain@stable
45+
- uses: ./.github/actions/setup-deps
4346
with:
4447
target: ${{ matrix.target }}
45-
- name: Install GCC (i686-unknown-linux-gnu)
46-
if: ${{ matrix.target == 'i686-unknown-linux-gnu' }}
47-
run: |
48-
sudo apt-get update -y
49-
sudo apt-get install -y --no-install-recommends gcc-multilib
50-
shell: bash
51-
- name: Install GCC (aarch64-unknown-linux-gnu)
52-
if: ${{ matrix.target == 'aarch64-unknown-linux-gnu' }}
53-
run: |
54-
sudo apt-get update -y
55-
sudo apt-get install -y --no-install-recommends gcc-aarch64-linux-gnu libc6-dev-arm64-cross
56-
shell: bash
57-
- name: Install GCC (arm-unknown-linux-gnueabi)
58-
if: ${{ matrix.target == 'arm-unknown-linux-gnueabi' }}
59-
run: |
60-
sudo apt-get update -y
61-
sudo apt-get install -y --no-install-recommends gcc-arm-linux-gnueabi libc6-dev-armel-cross
62-
shell: bash
63-
- name: Install GCC (x86_64-pc-windows-gnu)
64-
if: ${{ matrix.target == 'x86_64-pc-windows-gnu' }}
65-
run: |
66-
sudo apt-get update -y
67-
sudo apt-get install -y --no-install-recommends gcc-mingw-w64-x86-64
68-
shell: bash
69-
- name: Install emscripten (wasm32-unknown-emscripten)
70-
if: ${{ matrix.target == 'wasm32-unknown-emscripten' }}
71-
run: |
72-
sudo apt-get update -y
73-
sudo apt-get install -y --no-install-recommends emscripten
74-
shell: bash
7548
- name: Build ${{ matrix.lua }}
7649
run: |
77-
cargo build --manifest-path testcrate/Cargo.toml --target ${{ matrix.target }} --release --features ${{ matrix.lua }}
50+
cargo build --manifest-path testcrate/Cargo.toml --release --features ${{ matrix.lua }}
7851
shell: bash
7952

8053
test:
@@ -83,7 +56,11 @@ jobs:
8356
needs: build
8457
strategy:
8558
matrix:
86-
os: [ubuntu-latest, macos-latest, windows-latest]
59+
target:
60+
- x86_64-unknown-linux-gnu
61+
- x86_64-apple-darwin
62+
- x86_64-pc-windows-msvc
63+
- wasm32-wasip2
8764
lua: [lua54, lua53, lua52, lua51]
8865
include:
8966
- os: ubuntu-latest
@@ -92,9 +69,11 @@ jobs:
9269
target: x86_64-apple-darwin
9370
- os: windows-latest
9471
target: x86_64-pc-windows-msvc
72+
- os: ubuntu-latest
73+
target: wasm32-wasip2
9574
steps:
9675
- uses: actions/checkout@main
97-
- uses: dtolnay/rust-toolchain@stable
76+
- uses: ./.github/actions/setup-deps
9877
with:
9978
target: ${{ matrix.target }}
10079
- name: Run ${{ matrix.lua }} tests
@@ -103,12 +82,11 @@ jobs:
10382
shell: bash
10483

10584
test_standalone:
106-
name: Test
85+
name: Test Standalone
10786
runs-on: ${{ matrix.os }}
10887
needs: build
10988
strategy:
11089
matrix:
111-
os: [ubuntu-latest, macos-latest, windows-latest]
11290
include:
11391
- os: ubuntu-latest
11492
target: x86_64-unknown-linux-gnu
@@ -118,36 +96,13 @@ jobs:
11896
target: x86_64-pc-windows-msvc
11997
steps:
12098
- uses: actions/checkout@main
121-
- uses: dtolnay/rust-toolchain@stable
99+
- uses: ./.github/actions/setup-deps
122100
with:
123101
target: ${{ matrix.target }}
124102
- name: Run standalone tests
125103
run: |
126104
cargo test
127105
128-
test_emscripten:
129-
name: Test emscripten
130-
runs-on: ubuntu-latest
131-
needs: build
132-
strategy:
133-
matrix:
134-
lua: [lua54, lua53, lua52, lua51]
135-
steps:
136-
- uses: actions/checkout@main
137-
- uses: dtolnay/rust-toolchain@stable
138-
with:
139-
target: wasm32-unknown-emscripten
140-
- name: Install emscripten
141-
run: |
142-
sudo apt-get update -y
143-
sudo apt-get install -y --no-install-recommends emscripten
144-
shell: bash
145-
- name: Run ${{ matrix.lua }} tests
146-
run: |
147-
export CARGO_TARGET_WASM32_UNKNOWN_EMSCRIPTEN_RUNNER=node
148-
cargo test --manifest-path testcrate/Cargo.toml --release --features ${{ matrix.lua }}
149-
shell: bash
150-
151106
rustfmt:
152107
name: Rustfmt
153108
runs-on: ubuntu-latest

lua-5.1.5/liolib.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,13 @@ static int io_popen (lua_State *L) {
181181

182182

183183
static int io_tmpfile (lua_State *L) {
184+
#if !defined(__wasi__)
184185
FILE **pf = newfile(L);
185186
*pf = tmpfile();
186187
return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;
188+
#else
189+
luaL_error(L, "not supported on WASI");
190+
#endif
187191
}
188192

189193

lua-5.1.5/loslib.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ static int os_pushresult (lua_State *L, int i, const char *filename) {
3636

3737

3838
static int os_execute (lua_State *L) {
39+
#if !defined(__wasi__)
3940
lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
4041
return 1;
42+
#else
43+
luaL_error(L, "not supported on WASI");
44+
#endif
4145
}
4246

4347

@@ -55,13 +59,17 @@ static int os_rename (lua_State *L) {
5559

5660

5761
static int os_tmpname (lua_State *L) {
62+
#if !defined(__wasi__)
5863
char buff[LUA_TMPNAMBUFSIZE];
5964
int err;
6065
lua_tmpnam(buff, err);
6166
if (err)
6267
return luaL_error(L, "unable to generate a unique filename");
6368
lua_pushstring(L, buff);
6469
return 1;
70+
#else
71+
luaL_error(L, "not supported on WASI");
72+
#endif
6573
}
6674

6775

@@ -72,8 +80,12 @@ static int os_getenv (lua_State *L) {
7280

7381

7482
static int os_clock (lua_State *L) {
83+
#if !defined(__wasi__)
7584
lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
7685
return 1;
86+
#else
87+
luaL_error(L, "not supported on WASI");
88+
#endif
7789
}
7890

7991

lua-5.1.5/luaconf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@
5454
#if defined(LUA_USE_POSIX)
5555
#define LUA_USE_MKSTEMP
5656
#define LUA_USE_ISATTY
57+
#if !defined(__wasi__)
5758
#define LUA_USE_POPEN
59+
#endif
5860
#define LUA_USE_ULONGJMP
5961
#endif
6062

lua-5.2.4/lauxlib.c

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

225225
#if !defined(inspectstat) /* { */
226226

227-
#if defined(LUA_USE_POSIX)
227+
#if defined(LUA_USE_POSIX) && !defined(__wasi__)
228228

229229
#include <sys/wait.h>
230230

lua-5.2.4/liolib.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,13 @@ static int io_popen (lua_State *L) {
254254

255255

256256
static int io_tmpfile (lua_State *L) {
257+
#if !defined(__wasi__)
257258
LStream *p = newfile(L);
258259
p->f = tmpfile();
259260
return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
261+
#else
262+
luaL_error(L, "not supported on WASI");
263+
#endif
260264
}
261265

262266

lua-5.2.4/loslib.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878

7979

8080
static int os_execute (lua_State *L) {
81+
#if !defined(__wasi__)
8182
const char *cmd = luaL_optstring(L, 1, NULL);
8283
int stat = system(cmd);
8384
if (cmd != NULL)
@@ -86,6 +87,9 @@ static int os_execute (lua_State *L) {
8687
lua_pushboolean(L, stat); /* true if there is a shell */
8788
return 1;
8889
}
90+
#else
91+
luaL_error(L, "not supported on WASI");
92+
#endif
8993
}
9094

9195

@@ -103,13 +107,17 @@ static int os_rename (lua_State *L) {
103107

104108

105109
static int os_tmpname (lua_State *L) {
110+
#if !defined(__wasi__)
106111
char buff[LUA_TMPNAMBUFSIZE];
107112
int err;
108113
lua_tmpnam(buff, err);
109114
if (err)
110115
return luaL_error(L, "unable to generate a unique filename");
111116
lua_pushstring(L, buff);
112117
return 1;
118+
#else
119+
luaL_error(L, "not supported on WASI");
120+
#endif
113121
}
114122

115123

@@ -120,8 +128,12 @@ static int os_getenv (lua_State *L) {
120128

121129

122130
static int os_clock (lua_State *L) {
131+
#if !defined(__wasi__)
123132
lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
124133
return 1;
134+
#else
135+
luaL_error(L, "not supported on WASI");
136+
#endif
125137
}
126138

127139

lua-5.2.4/luaconf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@
6868
#if defined(LUA_USE_POSIX)
6969
#define LUA_USE_MKSTEMP
7070
#define LUA_USE_ISATTY
71+
#if !defined(__wasi__)
7172
#define LUA_USE_POPEN
73+
#endif
7274
#define LUA_USE_ULONGJMP
7375
#define LUA_USE_GMTIME_R
7476
#endif

lua-5.3.6/lauxlib.c

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

252252
#if !defined(l_inspectstat) /* { */
253253

254-
#if defined(LUA_USE_POSIX)
254+
#if defined(LUA_USE_POSIX) && !defined(__wasi__)
255255

256256
#include <sys/wait.h>
257257

0 commit comments

Comments
 (0)