Skip to content

Commit 560a30c

Browse files
committed
Support new Luau require system
This commit introduces the `Require` trait that be used to change `require` behaviour. By default mlua implements behaviour same as `ReplRequirer` in the original Luau. Unfortunately binary Luau modules are no longer supported by the new system.
1 parent 117f837 commit 560a30c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+757
-385
lines changed

.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ jobs:
194194
matrix:
195195
os: [ubuntu-latest, macos-latest]
196196
rust: [stable]
197-
lua: [lua54, lua53, lua52, lua51, luajit, luau]
197+
lua: [lua54, lua53, lua52, lua51, luajit]
198198
include:
199199
- os: ubuntu-latest
200200
target: x86_64-unknown-linux-gnu

Cargo.toml

+1-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ lua52 = ["ffi/lua52"]
3232
lua51 = ["ffi/lua51"]
3333
luajit = ["ffi/luajit"]
3434
luajit52 = ["luajit", "ffi/luajit52"]
35-
luau = ["ffi/luau", "dep:libloading"]
35+
luau = ["ffi/luau"]
3636
luau-jit = ["luau", "ffi/luau-codegen"]
3737
luau-vector4 = ["luau", "ffi/luau-vector4"]
3838
vendored = ["ffi/vendored"]
@@ -61,9 +61,6 @@ rustversion = "1.0"
6161

6262
ffi = { package = "mlua-sys", version = "0.6.6", path = "mlua-sys" }
6363

64-
[target.'cfg(unix)'.dependencies]
65-
libloading = { version = "0.8", optional = true }
66-
6764
[dev-dependencies]
6865
trybuild = "1.0"
6966
hyper = { version = "1.2", features = ["full"] }

mlua-sys/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ cfg-if = "1.0"
4040
pkg-config = "0.3.17"
4141
lua-src = { version = ">= 547.0.0, < 547.1.0", optional = true }
4242
luajit-src = { version = ">= 210.5.0, < 210.6.0", optional = true }
43-
luau0-src = { git = "https://github.com/mlua-rs/luau-src-rs", rev = "f89e9f2", optional = true }
43+
luau0-src = { git = "https://github.com/mlua-rs/luau-src-rs", rev = "37e1e34", optional = true }
4444

4545
[lints.rust]
4646
unexpected_cfgs = { level = "allow", check-cfg = ['cfg(raw_dylib)'] }

mlua-sys/build/main_inner.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ cfg_if::cfg_if! {
1111
}
1212

1313
fn main() {
14-
#[cfg(all(feature = "luau", feature = "module", windows))]
15-
compile_error!("Luau does not support `module` mode on Windows");
14+
#[cfg(all(feature = "luau", feature = "module"))]
15+
compile_error!("Luau does not support `module` mode");
1616

1717
#[cfg(all(feature = "module", feature = "vendored"))]
1818
compile_error!("`vendored` and `module` features are mutually exclusive");

mlua-sys/src/luau/luarequire.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub struct luarequire_Configuration {
101101

102102
// Executes the module and places the result on the stack. Returns the number of results placed on the
103103
// stack.
104-
pub load: unsafe extern "C" fn(
104+
pub load: unsafe extern "C-unwind" fn(
105105
L: *mut lua_State,
106106
ctx: *mut c_void,
107107
chunkname: *const c_char,

src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ mod conversion;
7676
mod error;
7777
mod function;
7878
mod hook;
79-
#[cfg(feature = "luau")]
79+
#[cfg(any(feature = "luau", doc))]
8080
mod luau;
8181
mod memory;
8282
mod multi;
@@ -130,6 +130,7 @@ pub use crate::{
130130
buffer::Buffer,
131131
chunk::{CompileConstant, Compiler},
132132
function::CoverageInfo,
133+
luau::{NavigateError, Require},
133134
vector::Vector,
134135
};
135136

src/luau/mod.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ use std::ffi::CStr;
22
use std::os::raw::c_int;
33

44
use crate::error::Result;
5-
use crate::state::Lua;
5+
use crate::state::{ExtraData, Lua, LuaOptions};
6+
7+
pub use require::{NavigateError, Require};
68

79
// Since Luau has some missing standard functions, we re-implement them here
810

911
impl Lua {
10-
pub(crate) unsafe fn configure_luau(&self) -> Result<()> {
12+
pub(crate) unsafe fn configure_luau(&self, mut options: LuaOptions) -> Result<()> {
1113
let globals = self.globals();
1214

1315
globals.raw_set("collectgarbage", self.create_c_function(lua_collectgarbage)?)?;
@@ -18,11 +20,13 @@ impl Lua {
1820
globals.raw_set("_VERSION", format!("Luau {version}"))?;
1921
}
2022

21-
Ok(())
22-
}
23+
// Enable `require` function
24+
let requirer = (options.requirer.take()).unwrap_or_else(|| Box::new(require::TextRequirer::new()));
25+
self.exec_raw::<()>((), |state| {
26+
let requirer_ptr = (*ExtraData::get(state)).set_requirer(requirer);
27+
ffi::luaopen_require(state, require::init_config, requirer_ptr as *mut _);
28+
})?;
2329

24-
pub(crate) fn disable_c_modules(&self) -> Result<()> {
25-
package::disable_dylibs(self);
2630
Ok(())
2731
}
2832
}
@@ -64,6 +68,4 @@ unsafe extern "C-unwind" fn lua_collectgarbage(state: *mut ffi::lua_State) -> c_
6468
}
6569
}
6670

67-
pub(crate) use package::register_package_module;
68-
69-
mod package;
71+
mod require;

src/luau/package.rs

-270
This file was deleted.

0 commit comments

Comments
 (0)