Skip to content

Commit 37fbf28

Browse files
authored
Merge pull request #7 from ByteDream/master
Add support for emscripten (wasm)
2 parents f3dc037 + 3060481 commit 37fbf28

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

.github/workflows/main.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ jobs:
2828
os: macos-latest
2929
- target: x86_64-pc-windows-msvc
3030
os: windows-latest
31+
- target: wasm32-unknown-emscripten
32+
os: ubuntu-latest
3133
steps:
3234
- uses: actions/checkout@v3
3335
- uses: dtolnay/rust-toolchain@stable
@@ -51,6 +53,12 @@ jobs:
5153
sudo apt-get update -y
5254
sudo apt-get install -y --no-install-recommends gcc-arm-linux-gnueabi libc6-dev-armel-cross
5355
shell: bash
56+
- name: Install emscripten (wasm32-unknown-emscripten)
57+
if: ${{ matrix.target == 'wasm32-unknown-emscripten' }}
58+
run: |
59+
sudo apt-get update -y
60+
sudo apt-get install -y --no-install-recommends emscripten
61+
shell: bash
5462
- name: Build ${{ matrix.lua }}
5563
run: |
5664
cargo build --manifest-path testcrate/Cargo.toml --target ${{ matrix.target }} --release --features ${{ matrix.lua }}
@@ -71,11 +79,21 @@ jobs:
7179
target: x86_64-apple-darwin
7280
- os: windows-latest
7381
target: x86_64-pc-windows-msvc
82+
- os: ubuntu-latest
83+
target: wasm32-unknown-emscripten
7484
steps:
7585
- uses: actions/checkout@v3
7686
- uses: dtolnay/rust-toolchain@stable
7787
with:
7888
target: ${{ matrix.target }}
89+
- name: Install emscripten (wasm32-unknown-emscripten)
90+
if: ${{ matrix.target == 'wasm32-unknown-emscripten' }}
91+
run: |
92+
sudo apt-get update -y
93+
sudo apt-get install -y --no-install-recommends emscripten
94+
echo 'CARGO_TARGET_WASM32_UNKNOWN_EMSCRIPTEN_RUNNER=node' >> $GITHUB_ENV
95+
echo 'RUSTFLAGS="-C link-args=-sERROR_ON_UNDEFINED_SYMBOLS=0"' >> $GITHUB_ENV
96+
shell: bash
7997
- name: Run ${{ matrix.lua }} tests
8098
run: |
8199
cargo test --manifest-path testcrate/Cargo.toml --release --features ${{ matrix.lua }}

src/lib.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl Build {
5656
let include_dir = out_dir.join("include");
5757

5858
let source_dir_base = Path::new(env!("CARGO_MANIFEST_DIR"));
59-
let source_dir = match version {
59+
let mut source_dir = match version {
6060
Lua51 => source_dir_base.join("lua-5.1.5"),
6161
Lua52 => source_dir_base.join("lua-5.2.4"),
6262
Lua53 => source_dir_base.join("lua-5.3.6"),
@@ -104,6 +104,35 @@ impl Build {
104104
// Defined in Lua >= 5.3
105105
config.define("LUA_USE_WINDOWS", None);
106106
}
107+
_ if target.ends_with("emscripten") => {
108+
config
109+
.define("LUA_USE_POSIX", None)
110+
.cpp(true)
111+
.flag("-fexceptions"); // Enable exceptions to be caught
112+
113+
let cpp_source_dir = out_dir.join("cpp_source");
114+
if cpp_source_dir.exists() {
115+
fs::remove_dir_all(&cpp_source_dir).unwrap();
116+
}
117+
fs::create_dir_all(&cpp_source_dir).unwrap();
118+
119+
for file in fs::read_dir(&source_dir).unwrap() {
120+
let file = file.unwrap();
121+
let filename = file.file_name().to_string_lossy().to_string();
122+
let src_file = source_dir.join(file.file_name());
123+
let dst_file = cpp_source_dir.join(file.file_name());
124+
125+
let mut content = fs::read(src_file).unwrap();
126+
// ljumptab.h only contains definitions and will cause errors when wrapping with
127+
// 'extern "C"'
128+
if filename.ends_with(".h") && !["ljumptab.h"].contains(&filename.as_str()) {
129+
content.splice(0..0, b"extern \"C\" {\n".to_vec());
130+
content.extend(b"\n}".to_vec())
131+
}
132+
fs::write(dst_file, content).unwrap();
133+
}
134+
source_dir = cpp_source_dir
135+
}
107136
_ => panic!("don't know how to build Lua for {}", target),
108137
};
109138

testcrate/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ extern "C" {
77
pub fn lua_tolstring(state: *mut c_void, index: c_int, len: *mut c_long) -> *const c_char;
88
pub fn luaL_loadstring(state: *mut c_void, s: *const c_char) -> c_int;
99

10-
#[cfg(any(feature = "lua52", feature = "lua53", feature = "lua54"))]
10+
#[cfg(feature = "lua52")]
1111
pub fn lua_getglobal(state: *mut c_void, k: *const c_char);
12+
#[cfg(any(feature = "lua53", feature = "lua54"))]
13+
pub fn lua_getglobal(state: *mut c_void, k: *const c_char) -> c_int;
1214
}
1315

1416
#[cfg(feature = "lua51")]

0 commit comments

Comments
 (0)