Skip to content

Commit

Permalink
Merge pull request #7 from ByteDream/master
Browse files Browse the repository at this point in the history
Add support for emscripten (wasm)
  • Loading branch information
khvzak authored Dec 6, 2023
2 parents f3dc037 + 3060481 commit 37fbf28
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:
os: macos-latest
- target: x86_64-pc-windows-msvc
os: windows-latest
- target: wasm32-unknown-emscripten
os: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
Expand All @@ -51,6 +53,12 @@ jobs:
sudo apt-get update -y
sudo apt-get install -y --no-install-recommends gcc-arm-linux-gnueabi libc6-dev-armel-cross
shell: bash
- name: Install emscripten (wasm32-unknown-emscripten)
if: ${{ matrix.target == 'wasm32-unknown-emscripten' }}
run: |
sudo apt-get update -y
sudo apt-get install -y --no-install-recommends emscripten
shell: bash
- name: Build ${{ matrix.lua }}
run: |
cargo build --manifest-path testcrate/Cargo.toml --target ${{ matrix.target }} --release --features ${{ matrix.lua }}
Expand All @@ -71,11 +79,21 @@ jobs:
target: x86_64-apple-darwin
- os: windows-latest
target: x86_64-pc-windows-msvc
- os: ubuntu-latest
target: wasm32-unknown-emscripten
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
target: ${{ matrix.target }}
- name: Install emscripten (wasm32-unknown-emscripten)
if: ${{ matrix.target == 'wasm32-unknown-emscripten' }}
run: |
sudo apt-get update -y
sudo apt-get install -y --no-install-recommends emscripten
echo 'CARGO_TARGET_WASM32_UNKNOWN_EMSCRIPTEN_RUNNER=node' >> $GITHUB_ENV
echo 'RUSTFLAGS="-C link-args=-sERROR_ON_UNDEFINED_SYMBOLS=0"' >> $GITHUB_ENV
shell: bash
- name: Run ${{ matrix.lua }} tests
run: |
cargo test --manifest-path testcrate/Cargo.toml --release --features ${{ matrix.lua }}
Expand Down
31 changes: 30 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Build {
let include_dir = out_dir.join("include");

let source_dir_base = Path::new(env!("CARGO_MANIFEST_DIR"));
let source_dir = match version {
let mut source_dir = match version {
Lua51 => source_dir_base.join("lua-5.1.5"),
Lua52 => source_dir_base.join("lua-5.2.4"),
Lua53 => source_dir_base.join("lua-5.3.6"),
Expand Down Expand Up @@ -104,6 +104,35 @@ impl Build {
// Defined in Lua >= 5.3
config.define("LUA_USE_WINDOWS", None);
}
_ if target.ends_with("emscripten") => {
config
.define("LUA_USE_POSIX", None)
.cpp(true)
.flag("-fexceptions"); // Enable exceptions to be caught

let cpp_source_dir = out_dir.join("cpp_source");
if cpp_source_dir.exists() {
fs::remove_dir_all(&cpp_source_dir).unwrap();
}
fs::create_dir_all(&cpp_source_dir).unwrap();

for file in fs::read_dir(&source_dir).unwrap() {
let file = file.unwrap();
let filename = file.file_name().to_string_lossy().to_string();
let src_file = source_dir.join(file.file_name());
let dst_file = cpp_source_dir.join(file.file_name());

let mut content = fs::read(src_file).unwrap();
// ljumptab.h only contains definitions and will cause errors when wrapping with
// 'extern "C"'
if filename.ends_with(".h") && !["ljumptab.h"].contains(&filename.as_str()) {
content.splice(0..0, b"extern \"C\" {\n".to_vec());
content.extend(b"\n}".to_vec())
}
fs::write(dst_file, content).unwrap();
}
source_dir = cpp_source_dir
}
_ => panic!("don't know how to build Lua for {}", target),
};

Expand Down
4 changes: 3 additions & 1 deletion testcrate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ extern "C" {
pub fn lua_tolstring(state: *mut c_void, index: c_int, len: *mut c_long) -> *const c_char;
pub fn luaL_loadstring(state: *mut c_void, s: *const c_char) -> c_int;

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

#[cfg(feature = "lua51")]
Expand Down

0 comments on commit 37fbf28

Please sign in to comment.