Skip to content

Commit

Permalink
Add lua emscripten support
Browse files Browse the repository at this point in the history
  • Loading branch information
bytedream committed Dec 14, 2023
1 parent 2022de2 commit 83c38e7
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 12 deletions.
12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,26 @@ ffi = { package = "mlua-sys", version = "0.4.0", path = "mlua-sys" }
[target.'cfg(unix)'.dependencies]
libloading = { version = "0.8", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
ffi = { package = "mlua-sys", version = "0.4.0", path = "mlua-sys", features = ["vendored"] }

[dev-dependencies]
rustyline = "12.0"
criterion = { version = "0.5", features = ["async_tokio"] }
trybuild = "1.0"
futures = "0.3.5"
hyper = { version = "0.14", features = ["client", "server"] }
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1.0", features = ["full"] }
tokio = { version = "1.0", features = ["macros", "rt", "time"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
maplit = "1.0"
tempfile = "3"
static_assertions = "1.0"

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
criterion = { version = "0.5", features = ["async_tokio"] }
rustyline = "12.0"
tokio = { version = "1.0", features = ["full"] }

[[bench]]
name = "benchmark"
harness = false
Expand Down
3 changes: 3 additions & 0 deletions examples/async_http_client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(target_arch = "wasm32")]
compile_error!("Not available for wasm");

use std::collections::HashMap;

use hyper::body::{Body as HyperBody, HttpBody as _};
Expand Down
2 changes: 1 addition & 1 deletion examples/async_http_reqwest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use mlua::{chunk, ExternalResult, Lua, LuaSerdeExt, Result};

#[tokio::main]
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
let lua = Lua::new();

Expand Down
3 changes: 3 additions & 0 deletions examples/async_http_server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(target_arch = "wasm32")]
compile_error!("Not available for wasm");

use std::future::Future;
use std::net::SocketAddr;
use std::pin::Pin;
Expand Down
3 changes: 3 additions & 0 deletions examples/async_tcp_server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(target_arch = "wasm32")]
compile_error!("Not available for wasm");

use std::io;
use std::net::SocketAddr;
use std::rc::Rc;
Expand Down
2 changes: 2 additions & 0 deletions examples/repl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! This example shows a simple read-evaluate-print-loop (REPL).
#[cfg(target_arch = "wasm32")]
compile_error!("Not available for wasm");

use mlua::{Error, Lua, MultiValue};
use rustyline::DefaultEditor;
Expand Down
6 changes: 6 additions & 0 deletions mlua-sys/src/lua51/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ unsafe fn compat53_pushfuncname(L: *mut lua_State, ar: *mut lua_Debug) {
// lua ported functions
//

#[inline(always)]
pub unsafe fn lua_error(L: *mut lua_State) -> ! {
lua_error_(L);
unreachable!()
}

#[inline(always)]
pub unsafe fn lua_absindex(L: *mut lua_State, mut idx: c_int) -> c_int {
if idx < 0 && idx > LUA_REGISTRYINDEX {
Expand Down
2 changes: 1 addition & 1 deletion mlua-sys/src/lua51/lauxlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extern "C-unwind" {
pub fn luaL_checkudata(L: *mut lua_State, ud: c_int, tname: *const c_char) -> *mut c_void;

pub fn luaL_where(L: *mut lua_State, lvl: c_int);
pub fn luaL_error(L: *mut lua_State, fmt: *const c_char, ...) -> !;
pub fn luaL_error(L: *mut lua_State, fmt: *const c_char, ...) -> c_int;

pub fn luaL_checkoption(
L: *mut lua_State,
Expand Down
3 changes: 2 additions & 1 deletion mlua-sys/src/lua51/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ extern "C-unwind" {
//
#[cfg_attr(all(windows, raw_dylib), link(name = "lua51", kind = "raw-dylib"))]
extern "C-unwind" {
pub fn lua_error(L: *mut lua_State) -> !;
#[link_name = "lua_error"]
pub fn lua_error_(L: *mut lua_State) -> c_int;
pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
pub fn lua_concat(L: *mut lua_State, n: c_int);
pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc;
Expand Down
6 changes: 6 additions & 0 deletions mlua-sys/src/lua52/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ unsafe fn compat53_reverse(L: *mut lua_State, mut a: c_int, mut b: c_int) {
// lua ported functions
//

#[inline(always)]
pub unsafe fn lua_error(L: *mut crate::lua_State) -> ! {
crate::lua_error_(L);
unreachable!()
}

pub unsafe fn lua_rotate(L: *mut lua_State, mut idx: c_int, mut n: c_int) {
idx = lua_absindex(L, idx);
if n > 0 {
Expand Down
2 changes: 1 addition & 1 deletion mlua-sys/src/lua52/lauxlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extern "C-unwind" {
pub fn luaL_checkudata(L: *mut lua_State, ud: c_int, tname: *const c_char) -> *mut c_void;

pub fn luaL_where(L: *mut lua_State, lvl: c_int);
pub fn luaL_error(L: *mut lua_State, fmt: *const c_char, ...) -> !;
pub fn luaL_error(L: *mut lua_State, fmt: *const c_char, ...) -> c_int;

pub fn luaL_checkoption(
L: *mut lua_State,
Expand Down
10 changes: 8 additions & 2 deletions mlua-sys/src/lua52/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ extern "C-unwind" {
//
// Miscellaneous functions
//
pub fn lua_error(L: *mut lua_State) -> !;
#[link_name = "lua_error"]
pub fn lua_error_(L: *mut lua_State) -> c_int;
pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
pub fn lua_concat(L: *mut lua_State, n: c_int);
pub fn lua_len(L: *mut lua_State, idx: c_int);
Expand Down Expand Up @@ -453,7 +454,12 @@ extern "C-unwind" {
pub fn lua_upvalueid(L: *mut lua_State, fidx: c_int, n: c_int) -> *mut c_void;
pub fn lua_upvaluejoin(L: *mut lua_State, fidx1: c_int, n1: c_int, fidx2: c_int, n2: c_int);

pub fn lua_sethook(L: *mut lua_State, func: Option<lua_Hook>, mask: c_int, count: c_int);
pub fn lua_sethook(
L: *mut lua_State,
func: Option<lua_Hook>,
mask: c_int,
count: c_int,
) -> c_int;
pub fn lua_gethook(L: *mut lua_State) -> Option<lua_Hook>;
pub fn lua_gethookmask(L: *mut lua_State) -> c_int;
pub fn lua_gethookcount(L: *mut lua_State) -> c_int;
Expand Down
6 changes: 6 additions & 0 deletions mlua-sys/src/lua53/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ use std::os::raw::c_int;

use super::lua::*;

#[inline(always)]
pub unsafe fn lua_error(L: *mut lua_State) -> ! {
lua_error_(L);
unreachable!()
}

#[inline(always)]
pub unsafe fn lua_resume(
L: *mut lua_State,
Expand Down
3 changes: 2 additions & 1 deletion mlua-sys/src/lua53/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ extern "C-unwind" {
//
// Miscellaneous functions
//
pub fn lua_error(L: *mut lua_State) -> !;
#[link_name = "lua_error"]
pub fn lua_error_(L: *mut lua_State) -> c_int;
pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
pub fn lua_concat(L: *mut lua_State, n: c_int);
pub fn lua_len(L: *mut lua_State, idx: c_int);
Expand Down
15 changes: 15 additions & 0 deletions mlua-sys/src/lua54/compat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! MLua compatibility layer for Lua 5.4
use super::lua::*;
use std::os::raw::c_int;

#[inline(always)]
pub unsafe fn lua_error(L: *mut lua_State) -> ! {
lua_error_(L);
unreachable!()
}

#[inline(always)]
pub unsafe fn lua_rawlen(L: *mut lua_State, idx: c_int) -> usize {
lua_rawlen_(L, idx) as usize
}
6 changes: 4 additions & 2 deletions mlua-sys/src/lua54/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ extern "C-unwind" {
pub fn lua_tointegerx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Integer;
pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
pub fn lua_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
pub fn lua_rawlen(L: *mut lua_State, idx: c_int) -> usize;
#[link_name = "lua_rawlen"]
pub fn lua_rawlen_(L: *mut lua_State, idx: c_int) -> lua_Unsigned;
pub fn lua_tocfunction(L: *mut lua_State, idx: c_int) -> Option<lua_CFunction>;
pub fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
pub fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State;
Expand Down Expand Up @@ -336,7 +337,8 @@ extern "C-unwind" {
//
// Miscellaneous functions
//
pub fn lua_error(L: *mut lua_State) -> !;
#[link_name = "lua_error"]
pub fn lua_error_(L: *mut lua_State) -> c_int;
pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
pub fn lua_concat(L: *mut lua_State, n: c_int);
pub fn lua_len(L: *mut lua_State, idx: c_int);
Expand Down
2 changes: 2 additions & 0 deletions mlua-sys/src/lua54/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! Low level bindings to Lua 5.4.
pub use compat::*;
pub use lauxlib::*;
pub use lua::*;
pub use lualib::*;

pub mod compat;
pub mod lauxlib;
pub mod lua;
pub mod lualib;
3 changes: 3 additions & 0 deletions tests/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#![allow(unused_imports)]

use std::fs;
use std::io;

use mlua::{Lua, Result};

#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_chunk_path() -> Result<()> {
let lua = Lua::new();

Expand Down

0 comments on commit 83c38e7

Please sign in to comment.