mirror of
https://github.com/mlua-rs/mlua
synced 2026-06-08 16:05:43 +00:00
Compare commits
80 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4f56575e05 | |||
| 35fa76263e | |||
| 446d63a77e | |||
| 8d8d521721 | |||
| 0d31a1caa6 | |||
| 3dc58cdfc9 | |||
| 5724b5f112 | |||
| d64d9719c6 | |||
| 75475fc9a8 | |||
| f8fe9246bb | |||
| 5c54361236 | |||
| ec227f9056 | |||
| 93a1a55aaa | |||
| a020b2b5b2 | |||
| 2c756e5958 | |||
| c702077028 | |||
| 08545224f4 | |||
| e122f90837 | |||
| c638d90b02 | |||
| 930fd9c00f | |||
| 2a8db87132 | |||
| cbae4fe59c | |||
| c68e3c4f41 | |||
| 02d4ceff34 | |||
| 98339c57e6 | |||
| 2331995e28 | |||
| c07bdce250 | |||
| 084a85c3d8 | |||
| 735aa22be9 | |||
| 5479546b27 | |||
| 179c54f297 | |||
| f9ae4bf05f | |||
| 9e16e18132 | |||
| 7535a23fa2 | |||
| 3787ff9e8c | |||
| 0a2a70c15a | |||
| 81d7c81532 | |||
| 0453029765 | |||
| 7b777d074e | |||
| c6cd1c53c3 | |||
| 669349d704 | |||
| c086c144d0 | |||
| 640cb2c182 | |||
| 03a4068d55 | |||
| 9f6c78532f | |||
| 8aecc83f53 | |||
| 4891b6535c | |||
| fa343c2c69 | |||
| 6d5e735bed | |||
| ac315fd80b | |||
| f95161c6e0 | |||
| 4bc846a119 | |||
| a3ca95fc8f | |||
| ae4897ab2e | |||
| 4ac87c7208 | |||
| 04d8106676 | |||
| b6cdf32f16 | |||
| ad9bc36764 | |||
| 4b8c26e682 | |||
| 7839c4438c | |||
| 529361fcbc | |||
| 4dddf3c18d | |||
| fb0c0d9ee9 | |||
| 235c32006c | |||
| b65901e444 | |||
| 91fe02da45 | |||
| 8274b5fa88 | |||
| 762e677a70 | |||
| 5b5f1e4669 | |||
| e582e7c57f | |||
| 3714da5ec8 | |||
| ca69be07ff | |||
| 16951e3628 | |||
| 8bb2b444ab | |||
| fc1570d2d7 | |||
| fce85381c6 | |||
| 3088516851 | |||
| 5162a0f46e | |||
| 640d27697d | |||
| da4404baa5 |
@@ -6,18 +6,18 @@ jobs:
|
||||
name: coverage
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: xd009642/tarpaulin
|
||||
image: xd009642/tarpaulin:develop-nightly
|
||||
options: --security-opt seccomp=unconfined
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@main
|
||||
|
||||
- name: Generate coverage report
|
||||
run: |
|
||||
cargo tarpaulin --out xml --tests --exclude-files benches/* --exclude-files mlua-sys/src/*/*
|
||||
cargo +nightly tarpaulin --verbose --out xml --tests --exclude-files benches/* --exclude-files mlua-sys/src/*/*
|
||||
|
||||
- name: Upload report to codecov.io
|
||||
uses: codecov/codecov-action@v3
|
||||
uses: codecov/codecov-action@v4
|
||||
with:
|
||||
token: ${{secrets.CODECOV_TOKEN}}
|
||||
fail_ci_if_error: false
|
||||
|
||||
+56
-30
@@ -7,18 +7,18 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-22.04, macos-latest, windows-latest]
|
||||
os: [ubuntu-latest, macos-latest, windows-latest]
|
||||
rust: [stable]
|
||||
lua: [lua54, lua53, lua52, lua51, luajit, luau, luau-jit, luau-vector4]
|
||||
include:
|
||||
- os: ubuntu-22.04
|
||||
- os: ubuntu-latest
|
||||
target: x86_64-unknown-linux-gnu
|
||||
- os: macos-latest
|
||||
target: x86_64-apple-darwin
|
||||
- os: windows-latest
|
||||
target: x86_64-pc-windows-msvc
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@main
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
toolchain: ${{ matrix.rust }}
|
||||
@@ -27,11 +27,11 @@ jobs:
|
||||
- name: Build ${{ matrix.lua }} vendored
|
||||
run: |
|
||||
cargo build --features "${{ matrix.lua }},vendored"
|
||||
cargo build --features "${{ matrix.lua }},vendored,async,serialize,macros"
|
||||
cargo build --features "${{ matrix.lua }},vendored,async,serialize,macros,send"
|
||||
cargo build --features "${{ matrix.lua }},vendored,async,serialize,macros,anyhow"
|
||||
cargo build --features "${{ matrix.lua }},vendored,async,serialize,macros,anyhow,send"
|
||||
shell: bash
|
||||
- name: Build ${{ matrix.lua }} pkg-config
|
||||
if: ${{ matrix.os == 'ubuntu-22.04' }}
|
||||
if: ${{ matrix.os == 'ubuntu-latest' }}
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y --no-install-recommends liblua5.4-dev liblua5.3-dev liblua5.2-dev liblua5.1-0-dev libluajit-5.1-dev
|
||||
@@ -45,7 +45,7 @@ jobs:
|
||||
matrix:
|
||||
lua: [lua54, lua53, lua52, lua51, luajit]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@main
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
toolchain: stable
|
||||
@@ -55,13 +55,13 @@ jobs:
|
||||
|
||||
build_aarch64_cross_ubuntu:
|
||||
name: Cross-compile to aarch64-unknown-linux-gnu
|
||||
runs-on: ubuntu-22.04
|
||||
runs-on: ubuntu-latest
|
||||
needs: build
|
||||
strategy:
|
||||
matrix:
|
||||
lua: [lua54, lua53, lua52, lua51, luajit]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@main
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
toolchain: stable
|
||||
@@ -77,13 +77,13 @@ jobs:
|
||||
|
||||
build_armv7_cross_ubuntu:
|
||||
name: Cross-compile to armv7-unknown-linux-gnueabihf
|
||||
runs-on: ubuntu-22.04
|
||||
runs-on: ubuntu-latest
|
||||
needs: build
|
||||
strategy:
|
||||
matrix:
|
||||
lua: [lua54, lua53, lua52, lua51]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@main
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
toolchain: stable
|
||||
@@ -103,18 +103,18 @@ jobs:
|
||||
needs: build
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-22.04, macos-latest, windows-latest]
|
||||
os: [ubuntu-latest, macos-latest, windows-latest]
|
||||
rust: [stable, nightly]
|
||||
lua: [lua54, lua53, lua52, lua51, luajit, luajit52, luau, luau-jit, luau-vector4]
|
||||
include:
|
||||
- os: ubuntu-22.04
|
||||
- os: ubuntu-latest
|
||||
target: x86_64-unknown-linux-gnu
|
||||
- os: macos-latest
|
||||
target: x86_64-apple-darwin
|
||||
- os: windows-latest
|
||||
target: x86_64-pc-windows-msvc
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@main
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
toolchain: ${{ matrix.rust }}
|
||||
@@ -123,8 +123,8 @@ jobs:
|
||||
- name: Run ${{ matrix.lua }} tests
|
||||
run: |
|
||||
cargo test --features "${{ matrix.lua }},vendored"
|
||||
cargo test --features "${{ matrix.lua }},vendored,async,serialize,macros"
|
||||
cargo test --features "${{ matrix.lua }},vendored,async,serialize,macros,send"
|
||||
cargo test --features "${{ matrix.lua }},vendored,async,serialize,macros,anyhow"
|
||||
cargo test --features "${{ matrix.lua }},vendored,async,serialize,macros,anyhow,send"
|
||||
shell: bash
|
||||
- name: Run compile tests (macos lua54)
|
||||
if: ${{ matrix.os == 'macos-latest' && matrix.lua == 'lua54' }}
|
||||
@@ -139,14 +139,14 @@ jobs:
|
||||
needs: build
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-22.04]
|
||||
os: [ubuntu-latest]
|
||||
rust: [nightly]
|
||||
lua: [lua54, lua53, lua52, lua51, luajit, luau, luau-jit, luau-vector4]
|
||||
include:
|
||||
- os: ubuntu-22.04
|
||||
- os: ubuntu-latest
|
||||
target: x86_64-unknown-linux-gnu
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@main
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
toolchain: ${{ matrix.rust }}
|
||||
@@ -160,22 +160,48 @@ jobs:
|
||||
env:
|
||||
RUSTFLAGS: -Z sanitizer=address
|
||||
|
||||
test_with_memory_limit:
|
||||
name: Test with memory limit
|
||||
runs-on: ${{ matrix.os }}
|
||||
needs: build
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest]
|
||||
rust: [nightly]
|
||||
lua: [lua54, lua53, lua52, lua51, luajit, luau, luau-jit, luau-vector4]
|
||||
include:
|
||||
- os: ubuntu-latest
|
||||
target: x86_64-unknown-linux-gnu
|
||||
steps:
|
||||
- uses: actions/checkout@main
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
toolchain: ${{ matrix.rust }}
|
||||
target: ${{ matrix.target }}
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- name: Run ${{ matrix.lua }} tests with forced memory limit
|
||||
run: |
|
||||
cargo test --tests --features "${{ matrix.lua }},vendored,async,send,serialize,macros"
|
||||
shell: bash
|
||||
env:
|
||||
RUSTFLAGS: --cfg=force_memory_limit
|
||||
|
||||
test_modules:
|
||||
name: Test modules
|
||||
runs-on: ${{ matrix.os }}
|
||||
needs: build
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-22.04, macos-latest]
|
||||
os: [ubuntu-latest, macos-latest]
|
||||
rust: [stable]
|
||||
lua: [lua54, lua53, lua52, lua51, luajit, luau]
|
||||
include:
|
||||
- os: ubuntu-22.04
|
||||
- os: ubuntu-latest
|
||||
target: x86_64-unknown-linux-gnu
|
||||
- os: macos-latest
|
||||
target: x86_64-apple-darwin
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@main
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
toolchain: ${{ matrix.rust }}
|
||||
@@ -199,7 +225,7 @@ jobs:
|
||||
shell: msys2 {0}
|
||||
steps:
|
||||
- uses: msys2/setup-msys2@v2
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@main
|
||||
- name: Install Rust & Lua
|
||||
run: |
|
||||
pacman -S --noconfirm mingw-w64-x86_64-rust mingw-w64-x86_64-lua mingw-w64-x86_64-luajit mingw-w64-x86_64-pkg-config
|
||||
@@ -210,13 +236,13 @@ jobs:
|
||||
|
||||
test_wasm32_emscripten:
|
||||
name: Test on wasm32-unknown-emscripten
|
||||
runs-on: ubuntu-22.04
|
||||
runs-on: ubuntu-latest
|
||||
needs: build
|
||||
strategy:
|
||||
matrix:
|
||||
lua: [lua54, lua53, lua52, lua51, luau]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@main
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
toolchain: stable
|
||||
@@ -232,9 +258,9 @@ jobs:
|
||||
|
||||
rustfmt:
|
||||
name: Rustfmt
|
||||
runs-on: ubuntu-22.04
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@main
|
||||
- uses: dtolnay/rust-toolchain@nightly
|
||||
with:
|
||||
components: rustfmt
|
||||
@@ -242,12 +268,12 @@ jobs:
|
||||
|
||||
clippy:
|
||||
name: Clippy
|
||||
runs-on: ubuntu-22.04
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
lua: [lua54, lua53, lua52, lua51, luajit, luau, luau-jit, luau-vector4]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@main
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
toolchain: nightly
|
||||
@@ -255,4 +281,4 @@ jobs:
|
||||
- uses: giraffate/clippy-action@v1
|
||||
with:
|
||||
reporter: 'github-pr-review'
|
||||
clippy_flags: --features "${{ matrix.lua }},vendored,async,send,serialize,macros"
|
||||
clippy_flags: --features "${{ matrix.lua }},vendored,async,send,serialize,macros,anyhow"
|
||||
|
||||
@@ -1,3 +1,25 @@
|
||||
## v0.10.0 (Oct 25th, 2024)
|
||||
|
||||
Changes since v0.10.0-rc.1
|
||||
|
||||
- Added `error-send` feature flag (disabled by default) to require `Send + Sync` for `Error`
|
||||
- Some performance improvements
|
||||
|
||||
## v0.10.0-rc.1
|
||||
|
||||
- `Lua::scope` is back
|
||||
- Support yielding from hooks for Lua 5.3+
|
||||
- Support setting metatable for Lua builtin types (number/string/function/etc)
|
||||
- Added `LuaNativeFn`/`LuaNativeFnMut`/`LuaNativeAsyncFn` traits for using in `Function::wrap`
|
||||
- Added `Error::chain` method to return iterator over nested errors
|
||||
- Added `Lua::exec_raw` helper to execute low-level Lua C API code
|
||||
- Added `Either<L, R>` enum to combine two types into a single one
|
||||
- Added a new `Buffer` type for Luau
|
||||
- Added `Value::is_error` and `Value::as_error` helpers
|
||||
- Added `Value::Other` variant to represent unknown Lua types (eg LuaJIT CDATA)
|
||||
- Added (optional) `anyhow` feature to implement `IntoLua` for `anyhow::Error`
|
||||
- Added `IntoLua`/`FromLua` for `OsString`/`OsStr` and `PathBuf`/`Path`
|
||||
|
||||
## v0.10.0-beta.2
|
||||
|
||||
- Updated `ThreadStatus` enum to include `Running` and `Finished` variants.
|
||||
|
||||
+8
-4
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "mlua"
|
||||
version = "0.10.0-beta.2" # remember to update mlua_derive
|
||||
version = "0.10.0" # remember to update mlua_derive
|
||||
authors = ["Aleksandr Orlenko <zxteam@pm.me>", "kyren <catherine@kyju.org>"]
|
||||
rust-version = "1.79.0"
|
||||
edition = "2021"
|
||||
@@ -38,13 +38,16 @@ luau-vector4 = ["luau", "ffi/luau-vector4"]
|
||||
vendored = ["ffi/vendored"]
|
||||
module = ["dep:mlua_derive", "ffi/module"]
|
||||
async = ["dep:futures-util"]
|
||||
send = ["parking_lot/send_guard"]
|
||||
send = ["parking_lot/send_guard", "error-send"]
|
||||
error-send = []
|
||||
serialize = ["dep:serde", "dep:erased-serde", "dep:serde-value"]
|
||||
macros = ["mlua_derive/macros"]
|
||||
anyhow = ["dep:anyhow", "error-send"]
|
||||
|
||||
[dependencies]
|
||||
mlua_derive = { version = "=0.10.0-beta.1", optional = true, path = "mlua_derive" }
|
||||
mlua_derive = { version = "=0.10.0", optional = true, path = "mlua_derive" }
|
||||
bstr = { version = "1.0", features = ["std"], default-features = false }
|
||||
either = "1.0"
|
||||
num-traits = { version = "0.2.14" }
|
||||
rustc-hash = "2.0"
|
||||
futures-util = { version = "0.3", optional = true, default-features = false, features = ["std"] }
|
||||
@@ -52,8 +55,9 @@ serde = { version = "1.0", optional = true }
|
||||
erased-serde = { version = "0.4", optional = true }
|
||||
serde-value = { version = "0.7", optional = true }
|
||||
parking_lot = { version = "0.12", features = ["arc_lock"] }
|
||||
anyhow = { version = "1.0", optional = true }
|
||||
|
||||
ffi = { package = "mlua-sys", version = "0.6.3", path = "mlua-sys" }
|
||||
ffi = { package = "mlua-sys", version = "0.6.4", path = "mlua-sys" }
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
libloading = { version = "0.8", optional = true }
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
[docs.rs]: https://docs.rs/mlua
|
||||
[Coverage Status]: https://codecov.io/gh/mlua-rs/mlua/branch/main/graph/badge.svg?token=99339FS1CG
|
||||
[codecov.io]: https://codecov.io/gh/mlua-rs/mlua
|
||||
[MSRV]: https://img.shields.io/badge/rust-1.71+-brightgreen.svg?&logo=rust
|
||||
[MSRV]: https://img.shields.io/badge/rust-1.79+-brightgreen.svg?&logo=rust
|
||||
|
||||
[Guided Tour] | [Benchmarks] | [FAQ]
|
||||
|
||||
@@ -17,7 +17,9 @@
|
||||
[Benchmarks]: https://github.com/khvzak/script-bench-rs
|
||||
[FAQ]: FAQ.md
|
||||
|
||||
# The main branch is the v0.10, development version of `mlua`. Please see the [v0.9](https://github.com/mlua-rs/mlua/tree/v0.9) branch for the stable versions of `mlua`.
|
||||
> **Note**
|
||||
>
|
||||
> See v0.10 [release notes](https://github.com/khvzak/mlua/blob/main/docs/release_notes/v0.10.md).
|
||||
|
||||
`mlua` is bindings to [Lua](https://www.lua.org) programming language for Rust with a goal to provide
|
||||
_safe_ (as far as it's possible), high level, easy to use, practical and flexible API.
|
||||
@@ -51,8 +53,10 @@ Below is a list of the available feature flags. By default `mlua` does not enabl
|
||||
* `module`: enable module mode (building loadable `cdylib` library for Lua)
|
||||
* `async`: enable async/await support (any executor can be used, eg. [tokio] or [async-std])
|
||||
* `send`: make `mlua::Lua: Send + Sync` (adds [`Send`] requirement to `mlua::Function` and `mlua::UserData`)
|
||||
* `error-send`: make `mlua:Error: Send + Sync`
|
||||
* `serialize`: add serialization and deserialization support to `mlua` types using [serde] framework
|
||||
* `macros`: enable procedural macros (such as `chunk!`)
|
||||
* `anyhow`: enable `anyhow::Error` conversion into Lua
|
||||
|
||||
[5.4]: https://www.lua.org/manual/5.4/manual.html
|
||||
[5.3]: https://www.lua.org/manual/5.3/manual.html
|
||||
@@ -128,7 +132,7 @@ Add to `Cargo.toml` :
|
||||
|
||||
``` toml
|
||||
[dependencies]
|
||||
mlua = { version = "0.9.9", features = ["lua54", "vendored"] }
|
||||
mlua = { version = "0.10.0", features = ["lua54", "vendored"] }
|
||||
```
|
||||
|
||||
`main.rs`
|
||||
@@ -163,7 +167,7 @@ Add to `Cargo.toml` :
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
mlua = { version = "0.9.9", features = ["lua54", "module"] }
|
||||
mlua = { version = "0.10.0", features = ["lua54", "module"] }
|
||||
```
|
||||
|
||||
`lib.rs` :
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
## mlua v0.10 release notes
|
||||
|
||||
The v0.10 version of mlua has goal to improve the user experience while keeping the same performance and safety guarantees.
|
||||
This document highlights the most notable features. For a full list of changes, see the [CHANGELOG].
|
||||
|
||||
[CHANGELOG]: https://github.com/khvzak/mlua/blob/main/CHANGELOG.md
|
||||
|
||||
### New features
|
||||
|
||||
#### `'static` Lua types
|
||||
|
||||
In previous mlua versions, it was required to have a `'lua` lifetime attached to every Lua value. v0.9 introduced (experimental) owned types that are `'static` without a lifetime attached, but they kept strong references to the Lua instance.
|
||||
In v0.10 all Lua types are `'static` and have only weak reference to the Lua instance. It means they are more flexible and can be used in more places without worrying about memory leaks.
|
||||
|
||||
#### Truly `send` feature
|
||||
|
||||
In this version Lua is `Send + Sync` when the `send` feature flag is enabled (previously was only `Send`). It means Lua instance and their values can be safely shared between threads and used in multi threaded async contexts.
|
||||
|
||||
```rust
|
||||
let lua = Lua::new();
|
||||
|
||||
lua.globals().set("i", 0)?;
|
||||
let func = lua.load("i = i + ...").into_function()?;
|
||||
|
||||
std::thread::scope(|s| {
|
||||
s.spawn(|| {
|
||||
for i in 0..5 {
|
||||
func.call::<()>(i).unwrap();
|
||||
}
|
||||
});
|
||||
s.spawn(|| {
|
||||
for i in 0..5 {
|
||||
func.call::<()>(i).unwrap();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
assert_eq!(lua.globals().get::<i32>("i")?, 20);
|
||||
```
|
||||
|
||||
Under the hood, to synchronize access to the Lua state, mlua uses [`ReentrantMutex`] which can be recursively locked by a single thread. Only one thread can execute Lua code at a time, but it's possible to share Lua values between threads.
|
||||
|
||||
This has some performance penalties (about 10-20%) compared to the lock free mode. This flag is disabled by default and does not supported in module mode.
|
||||
|
||||
[`ReentrantMutex`]: https://docs.rs/parking_lot/latest/parking_lot/type.ReentrantMutex.html
|
||||
|
||||
#### Register Rust functions with variable number of arguments
|
||||
|
||||
The new traits `LuaNativeFn`/`LuaNativeFnMut`/`LuaNativeAsyncFn` have been introduced to provide a way to register Rust functions with variable number of arguments in Lua, without needing to pass all arguments as a tuple.
|
||||
|
||||
They are used by `Function::wrap`/`Function::wrap_mut`/`Function::wrap_async` methods:
|
||||
|
||||
```rust
|
||||
let add = Function::wrap(|a: i64, b: i64| Ok(a + b));
|
||||
|
||||
lua.globals().set("add", add).unwrap();
|
||||
|
||||
// Prints 50
|
||||
lua.load(r#"print(add(5, 45))"#).exec().unwrap();
|
||||
```
|
||||
|
||||
To wrap functions that return direct value (non-`Result`) you can use `Function::wrap_raw` method.
|
||||
|
||||
#### Setting metatable for Lua builtin types
|
||||
|
||||
For Lua builtin types (like `string`, `function`, `number`, etc.) that have a shared metatable for all instances, it's now possible to set a custom metatable for them.
|
||||
|
||||
```rust
|
||||
let mt = lua.create_table()?;
|
||||
mt.set("__tostring", lua.create_function(|_, b: bool| Ok(if b { "2" } else { "0" }))?)?;
|
||||
lua.set_type_metatable::<bool>(Some(mt));
|
||||
lua.load("assert(tostring(true) == '2')").exec().unwrap();
|
||||
```
|
||||
|
||||
### Improvements
|
||||
|
||||
#### New `ObjectLike` trait
|
||||
|
||||
The `ObjectLike` trait is a combination of the `AnyUserDataExt` and `TableExt` traits used in previous versions. It provides a unified interface for working with Lua tables and userdata.
|
||||
|
||||
#### `Either<L, R>` enum
|
||||
|
||||
The `Either<L, R>` enum is a simple enum that can hold either `L` or `R` value. It's useful when you need to return or receive one of two types in a function.
|
||||
This type implements `IntoLua` and `FromLua` traits and can generate a meaningful error message when conversion fails.
|
||||
|
||||
```rust
|
||||
let func = Function::wrap(|x: Either<i32, String>| Ok(format!("received: {x}")));
|
||||
|
||||
lua.globals().set("func", func).unwrap();
|
||||
|
||||
// Prints: received: 123
|
||||
lua.load(r#"print(func(123))"#).exec().unwrap();
|
||||
|
||||
// Prints: bad argument #1: error converting Lua table to Either<i32, String>
|
||||
lua.load(r#"print(pcall(func, {}))"#).exec().unwrap();
|
||||
```
|
||||
|
||||
#### `Lua::exec_raw` helper to execute low-level Lua C API code
|
||||
|
||||
For advanced users, it's now possible to execute low-level Lua C API code using the `Lua::exec_raw` method.
|
||||
|
||||
```rust
|
||||
let t = lua.create_sequence_from([1, 2, 3, 4, 5])?;
|
||||
let sum: i64 = unsafe {
|
||||
lua.exec_raw(&t, |state| {
|
||||
// top of the stack: table `t`
|
||||
let mut sum = 0;
|
||||
// push nil as the first key
|
||||
mlua::ffi::lua_pushnil(state);
|
||||
while mlua::ffi::lua_next(state, -2) != 0 {
|
||||
sum += mlua::ffi::lua_tointeger(state, -1);
|
||||
// Remove the value, keep the key for the next iteration
|
||||
mlua::ffi::lua_pop(state, 1);
|
||||
}
|
||||
mlua::ffi::lua_pop(state, 1);
|
||||
mlua::ffi::lua_pushinteger(state, sum);
|
||||
// top of the stack: sum
|
||||
})
|
||||
}?;
|
||||
assert_eq!(sum, 15);
|
||||
```
|
||||
|
||||
The `exec_raw` method is longjmp-safe. It's not recommended to move `Drop` types into the closure to avoid possible memory leaks.
|
||||
|
||||
#### `anyhow` feature flag
|
||||
|
||||
The new `anyhow` feature flag adds `IntoLua` and `Into<mlua::Error>` implementation for the `anyhow::Error` type.
|
||||
|
||||
```rust
|
||||
let f = lua.create_function(|_, ()| {
|
||||
Err(anyhow!("error message"))?;
|
||||
Ok(())
|
||||
})?;
|
||||
```
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "mlua-sys"
|
||||
version = "0.6.3"
|
||||
version = "0.6.4"
|
||||
authors = ["Aleksandr Orlenko <zxteam@pm.me>"]
|
||||
rust-version = "1.71"
|
||||
edition = "2021"
|
||||
@@ -40,7 +40,7 @@ cfg-if = "1.0"
|
||||
pkg-config = "0.3.17"
|
||||
lua-src = { version = ">= 547.0.0, < 547.1.0", optional = true }
|
||||
luajit-src = { version = ">= 210.5.0, < 210.6.0", optional = true }
|
||||
luau0-src = { version = "0.10.0", optional = true }
|
||||
luau0-src = { version = "0.11.0", optional = true }
|
||||
|
||||
[lints.rust]
|
||||
unexpected_cfgs = { level = "allow", check-cfg = ['cfg(raw_dylib)'] }
|
||||
|
||||
@@ -176,7 +176,7 @@ pub unsafe fn lua_rotate(L: *mut lua_State, mut idx: c_int, mut n: c_int) {
|
||||
#[inline(always)]
|
||||
pub unsafe fn lua_copy(L: *mut lua_State, fromidx: c_int, toidx: c_int) {
|
||||
let abs_to = lua_absindex(L, toidx);
|
||||
luaL_checkstack(L, 1, cstr!("not enough stack slots"));
|
||||
luaL_checkstack(L, 1, cstr!("not enough stack slots available"));
|
||||
lua_pushvalue(L, fromidx);
|
||||
lua_replace(L, abs_to);
|
||||
}
|
||||
@@ -314,7 +314,7 @@ pub unsafe fn lua_rawseti(L: *mut lua_State, idx: c_int, n: lua_Integer) {
|
||||
#[inline(always)]
|
||||
pub unsafe fn lua_rawsetp(L: *mut lua_State, idx: c_int, p: *const c_void) {
|
||||
let abs_i = lua_absindex(L, idx);
|
||||
luaL_checkstack(L, 1, cstr!("not enough stack slots"));
|
||||
luaL_checkstack(L, 1, cstr!("not enough stack slots available"));
|
||||
lua_pushlightuserdata(L, p as *mut c_void);
|
||||
lua_insert(L, -2);
|
||||
lua_rawset(L, abs_i);
|
||||
@@ -444,7 +444,7 @@ pub unsafe fn luaL_loadbufferx(
|
||||
#[inline(always)]
|
||||
pub unsafe fn luaL_len(L: *mut lua_State, idx: c_int) -> lua_Integer {
|
||||
let mut isnum = 0;
|
||||
luaL_checkstack(L, 1, cstr!("not enough stack slots"));
|
||||
luaL_checkstack(L, 1, cstr!("not enough stack slots available"));
|
||||
lua_len(L, idx);
|
||||
let res = lua_tointegerx(L, -1, &mut isnum);
|
||||
lua_pop(L, 1);
|
||||
@@ -526,14 +526,14 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, mut idx: c_int, len: *mut usize)
|
||||
|
||||
#[inline(always)]
|
||||
pub unsafe fn luaL_setmetatable(L: *mut lua_State, tname: *const c_char) {
|
||||
luaL_checkstack(L, 1, cstr!("not enough stack slots"));
|
||||
luaL_checkstack(L, 1, cstr!("not enough stack slots available"));
|
||||
luaL_getmetatable(L, tname);
|
||||
lua_setmetatable(L, -2);
|
||||
}
|
||||
|
||||
pub unsafe fn luaL_getsubtable(L: *mut lua_State, idx: c_int, fname: *const c_char) -> c_int {
|
||||
let abs_i = lua_absindex(L, idx);
|
||||
luaL_checkstack(L, 3, cstr!("not enough stack slots"));
|
||||
luaL_checkstack(L, 3, cstr!("not enough stack slots available"));
|
||||
lua_pushstring_(L, fname);
|
||||
if lua_gettable(L, abs_i) == LUA_TTABLE {
|
||||
return 1;
|
||||
|
||||
@@ -108,7 +108,7 @@ pub unsafe fn lua_rotate(L: *mut lua_State, mut idx: c_int, mut n: c_int) {
|
||||
#[inline(always)]
|
||||
pub unsafe fn lua_copy(L: *mut lua_State, fromidx: c_int, toidx: c_int) {
|
||||
let abs_to = lua_absindex(L, toidx);
|
||||
luaL_checkstack(L, 1, cstr!("not enough stack slots"));
|
||||
luaL_checkstack(L, 1, cstr!("not enough stack slots available"));
|
||||
lua_pushvalue(L, fromidx);
|
||||
lua_replace(L, abs_to);
|
||||
}
|
||||
@@ -217,7 +217,7 @@ pub unsafe fn lua_rawseti(L: *mut lua_State, idx: c_int, n: lua_Integer) {
|
||||
#[inline(always)]
|
||||
pub unsafe fn lua_rawsetp(L: *mut lua_State, idx: c_int, p: *const c_void) {
|
||||
let abs_i = lua_absindex(L, idx);
|
||||
luaL_checkstack(L, 1, cstr!("not enough stack slots"));
|
||||
luaL_checkstack(L, 1, cstr!("not enough stack slots available"));
|
||||
lua_pushlightuserdata(L, p as *mut c_void);
|
||||
lua_insert(L, -2);
|
||||
lua_rawset(L, abs_i);
|
||||
@@ -381,7 +381,7 @@ pub unsafe fn luaL_loadbuffer(
|
||||
#[inline(always)]
|
||||
pub unsafe fn luaL_len(L: *mut lua_State, idx: c_int) -> lua_Integer {
|
||||
let mut isnum = 0;
|
||||
luaL_checkstack(L, 1, cstr!("not enough stack slots"));
|
||||
luaL_checkstack(L, 1, cstr!("not enough stack slots available"));
|
||||
lua_len(L, idx);
|
||||
let res = lua_tointegerx(L, -1, &mut isnum);
|
||||
lua_pop(L, 1);
|
||||
@@ -463,14 +463,14 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, mut idx: c_int, len: *mut usize)
|
||||
|
||||
#[inline(always)]
|
||||
pub unsafe fn luaL_setmetatable(L: *mut lua_State, tname: *const c_char) {
|
||||
luaL_checkstack(L, 1, cstr!("not enough stack slots"));
|
||||
luaL_checkstack(L, 1, cstr!("not enough stack slots available"));
|
||||
luaL_getmetatable(L, tname);
|
||||
lua_setmetatable(L, -2);
|
||||
}
|
||||
|
||||
pub unsafe fn luaL_getsubtable(L: *mut lua_State, idx: c_int, fname: *const c_char) -> c_int {
|
||||
let abs_i = lua_absindex(L, idx);
|
||||
luaL_checkstack(L, 3, cstr!("not enough stack slots"));
|
||||
luaL_checkstack(L, 3, cstr!("not enough stack slots available"));
|
||||
lua_pushstring_(L, fname);
|
||||
if lua_gettable(L, abs_i) == LUA_TTABLE {
|
||||
return 1;
|
||||
|
||||
@@ -526,6 +526,9 @@ pub struct lua_Callbacks {
|
||||
pub debuginterrupt: Option<unsafe extern "C-unwind" fn(L: *mut lua_State, ar: *mut lua_Debug)>,
|
||||
/// gets called when protected call results in an error
|
||||
pub debugprotectederror: Option<unsafe extern "C-unwind" fn(L: *mut lua_State)>,
|
||||
|
||||
/// gets called when memory is allocated
|
||||
pub onallocate: Option<unsafe extern "C-unwind" fn(L: *mut lua_State, osize: usize, nsize: usize)>,
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
@@ -535,4 +538,5 @@ extern "C" {
|
||||
// Functions from customization lib
|
||||
extern "C" {
|
||||
pub fn luau_setfflag(name: *const c_char, value: c_int) -> c_int;
|
||||
pub fn lua_getmetatablepointer(L: *mut lua_State, idx: c_int) -> *const c_void;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "mlua_derive"
|
||||
version = "0.10.0-beta.1"
|
||||
version = "0.10.0"
|
||||
authors = ["Aleksandr Orlenko <zxteam@pm.me>"]
|
||||
edition = "2021"
|
||||
description = "Procedural macros for the mlua crate."
|
||||
|
||||
@@ -20,7 +20,7 @@ pub fn from_lua(input: TokenStream) -> TokenStream {
|
||||
::mlua::Value::UserData(ud) => Ok(ud.borrow::<Self>()?.clone()),
|
||||
_ => Err(::mlua::Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: #ident_str,
|
||||
to: #ident_str.to_string(),
|
||||
message: None,
|
||||
}),
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
#[cfg(feature = "serialize")]
|
||||
use serde::ser::{Serialize, Serializer};
|
||||
|
||||
use crate::types::ValueRef;
|
||||
|
||||
/// A Luau buffer type.
|
||||
///
|
||||
/// See the buffer [documentation] for more information.
|
||||
///
|
||||
/// [documentation]: https://luau.org/library#buffer-library
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Buffer(pub(crate) ValueRef);
|
||||
|
||||
#[cfg_attr(not(feature = "luau"), allow(unused))]
|
||||
impl Buffer {
|
||||
/// Copies the buffer data into a new `Vec<u8>`.
|
||||
pub fn to_vec(&self) -> Vec<u8> {
|
||||
unsafe { self.as_slice().to_vec() }
|
||||
}
|
||||
|
||||
/// Returns the length of the buffer.
|
||||
pub fn len(&self) -> usize {
|
||||
unsafe { self.as_slice().len() }
|
||||
}
|
||||
|
||||
/// Returns `true` if the buffer is empty.
|
||||
#[doc(hidden)]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len() == 0
|
||||
}
|
||||
|
||||
/// Reads given number of bytes from the buffer at the given offset.
|
||||
///
|
||||
/// Offset is 0-based.
|
||||
#[track_caller]
|
||||
pub fn read_bytes<const N: usize>(&self, offset: usize) -> [u8; N] {
|
||||
let data = unsafe { self.as_slice() };
|
||||
let mut bytes = [0u8; N];
|
||||
bytes.copy_from_slice(&data[offset..offset + N]);
|
||||
bytes
|
||||
}
|
||||
|
||||
/// Writes given bytes to the buffer at the given offset.
|
||||
///
|
||||
/// Offset is 0-based.
|
||||
#[track_caller]
|
||||
pub fn write_bytes(&self, offset: usize, bytes: &[u8]) {
|
||||
let data = unsafe {
|
||||
let (buf, size) = self.as_raw_parts();
|
||||
std::slice::from_raw_parts_mut(buf, size)
|
||||
};
|
||||
data[offset..offset + bytes.len()].copy_from_slice(bytes);
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn as_slice(&self) -> &[u8] {
|
||||
let (buf, size) = self.as_raw_parts();
|
||||
std::slice::from_raw_parts(buf, size)
|
||||
}
|
||||
|
||||
#[cfg(feature = "luau")]
|
||||
unsafe fn as_raw_parts(&self) -> (*mut u8, usize) {
|
||||
let lua = self.0.lua.lock();
|
||||
let mut size = 0usize;
|
||||
let buf = ffi::lua_tobuffer(lua.ref_thread(), self.0.index, &mut size);
|
||||
mlua_assert!(!buf.is_null(), "invalid Luau buffer");
|
||||
(buf as *mut u8, size)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "luau"))]
|
||||
unsafe fn as_raw_parts(&self) -> (*mut u8, usize) {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl Serialize for Buffer {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
|
||||
serializer.serialize_bytes(unsafe { self.as_slice() })
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "luau")]
|
||||
impl crate::types::LuaType for Buffer {
|
||||
const TYPE_ID: std::os::raw::c_int = ffi::LUA_TBUFFER;
|
||||
}
|
||||
+8
-11
@@ -9,12 +9,11 @@ use crate::error::{Error, Result};
|
||||
use crate::function::Function;
|
||||
use crate::state::{Lua, WeakLua};
|
||||
use crate::table::Table;
|
||||
use crate::value::{FromLuaMulti, IntoLuaMulti};
|
||||
use crate::traits::{FromLuaMulti, IntoLuaMulti};
|
||||
|
||||
/// Trait for types [loadable by Lua] and convertible to a [`Chunk`]
|
||||
///
|
||||
/// [loadable by Lua]: https://www.lua.org/manual/5.4/manual.html#3.3.2
|
||||
/// [`Chunk`]: crate::Chunk
|
||||
pub trait AsChunk<'a> {
|
||||
/// Returns optional chunk name
|
||||
fn name(&self) -> Option<StdString> {
|
||||
@@ -95,8 +94,6 @@ impl AsChunk<'static> for PathBuf {
|
||||
}
|
||||
|
||||
/// Returned from [`Lua::load`] and is used to finalize loading and executing Lua main chunks.
|
||||
///
|
||||
/// [`Lua::load`]: crate::Lua::load
|
||||
#[must_use = "`Chunk`s do nothing unless one of `exec`, `eval`, `call`, or `into_function` are called on them"]
|
||||
pub struct Chunk<'a> {
|
||||
pub(crate) lua: WeakLua,
|
||||
@@ -241,7 +238,7 @@ impl Compiler {
|
||||
|
||||
/// Compiles the `source` into bytecode.
|
||||
///
|
||||
/// Returns `Error::SyntaxError` if the source code is invalid.
|
||||
/// Returns [`Error::SyntaxError`] if the source code is invalid.
|
||||
pub fn compile(&self, source: impl AsRef<[u8]>) -> Result<Vec<u8>> {
|
||||
use std::os::raw::c_int;
|
||||
use std::ptr;
|
||||
@@ -361,7 +358,7 @@ impl<'a> Chunk<'a> {
|
||||
///
|
||||
/// Requires `feature = "async"`
|
||||
///
|
||||
/// [`exec`]: #method.exec
|
||||
/// [`exec`]: Chunk::exec
|
||||
#[cfg(feature = "async")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
|
||||
pub async fn exec_async(self) -> Result<()> {
|
||||
@@ -393,7 +390,7 @@ impl<'a> Chunk<'a> {
|
||||
///
|
||||
/// Requires `feature = "async"`
|
||||
///
|
||||
/// [`eval`]: #method.eval
|
||||
/// [`eval`]: Chunk::eval
|
||||
#[cfg(feature = "async")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
|
||||
pub async fn eval_async<R>(self) -> Result<R>
|
||||
@@ -422,7 +419,7 @@ impl<'a> Chunk<'a> {
|
||||
///
|
||||
/// Requires `feature = "async"`
|
||||
///
|
||||
/// [`call`]: #method.call
|
||||
/// [`call`]: Chunk::call
|
||||
#[cfg(feature = "async")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
|
||||
pub async fn call_async<R>(self, args: impl IntoLuaMulti) -> Result<R>
|
||||
@@ -432,7 +429,7 @@ impl<'a> Chunk<'a> {
|
||||
self.into_function()?.call_async(args).await
|
||||
}
|
||||
|
||||
/// Load this chunk into a regular `Function`.
|
||||
/// Load this chunk into a regular [`Function`].
|
||||
///
|
||||
/// This simply compiles the chunk without actually executing it.
|
||||
#[cfg_attr(not(feature = "luau"), allow(unused_mut))]
|
||||
@@ -481,7 +478,7 @@ impl<'a> Chunk<'a> {
|
||||
if let Ok(ref source) = self.source {
|
||||
if self.detect_mode() == ChunkMode::Text {
|
||||
let lua = self.lua.lock();
|
||||
if let Some(cache) = lua.app_data_ref::<ChunksCache>() {
|
||||
if let Some(cache) = lua.app_data_ref_unguarded::<ChunksCache>() {
|
||||
if let Some(data) = cache.0.get(source.as_ref()) {
|
||||
self.source = Ok(Cow::Owned(data.clone()));
|
||||
self.mode = Some(ChunkMode::Binary);
|
||||
@@ -498,7 +495,7 @@ impl<'a> Chunk<'a> {
|
||||
if let Ok(ref binary_source) = self.source {
|
||||
if self.detect_mode() == ChunkMode::Binary {
|
||||
let lua = self.lua.lock();
|
||||
if let Some(mut cache) = lua.app_data_mut::<ChunksCache>() {
|
||||
if let Some(mut cache) = lua.app_data_mut_unguarded::<ChunksCache>() {
|
||||
cache.0.insert(text_source, binary_source.as_ref().to_vec());
|
||||
} else {
|
||||
let mut cache = ChunksCache(HashMap::new());
|
||||
|
||||
+197
-49
@@ -1,12 +1,13 @@
|
||||
use std::borrow::Cow;
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::ffi::{CStr, CString, OsStr, OsString};
|
||||
use std::hash::{BuildHasher, Hash};
|
||||
use std::os::raw::c_int;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::string::String as StdString;
|
||||
use std::{slice, str};
|
||||
|
||||
use bstr::{BStr, BString};
|
||||
use bstr::{BStr, BString, ByteSlice, ByteVec};
|
||||
use num_traits::cast;
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
@@ -15,9 +16,10 @@ use crate::state::{Lua, RawLua};
|
||||
use crate::string::String;
|
||||
use crate::table::Table;
|
||||
use crate::thread::Thread;
|
||||
use crate::types::{LightUserData, MaybeSend, RegistryKey};
|
||||
use crate::traits::{FromLua, IntoLua, ShortTypeName as _};
|
||||
use crate::types::{Either, LightUserData, MaybeSend, RegistryKey};
|
||||
use crate::userdata::{AnyUserData, UserData};
|
||||
use crate::value::{FromLua, IntoLua, Nil, Value};
|
||||
use crate::value::{Nil, Value};
|
||||
|
||||
impl IntoLua for Value {
|
||||
#[inline]
|
||||
@@ -72,7 +74,7 @@ impl FromLua for String {
|
||||
lua.coerce_string(value)?
|
||||
.ok_or_else(|| Error::FromLuaConversionError {
|
||||
from: ty,
|
||||
to: "string",
|
||||
to: "string".to_string(),
|
||||
message: Some("expected string or number".to_string()),
|
||||
})
|
||||
}
|
||||
@@ -116,7 +118,7 @@ impl FromLua for Table {
|
||||
Value::Table(table) => Ok(table),
|
||||
_ => Err(Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "table",
|
||||
to: "table".to_string(),
|
||||
message: None,
|
||||
}),
|
||||
}
|
||||
@@ -150,7 +152,7 @@ impl FromLua for Function {
|
||||
Value::Function(table) => Ok(table),
|
||||
_ => Err(Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "function",
|
||||
to: "function".to_string(),
|
||||
message: None,
|
||||
}),
|
||||
}
|
||||
@@ -184,7 +186,7 @@ impl FromLua for Thread {
|
||||
Value::Thread(t) => Ok(t),
|
||||
_ => Err(Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "thread",
|
||||
to: "thread".to_string(),
|
||||
message: None,
|
||||
}),
|
||||
}
|
||||
@@ -218,7 +220,7 @@ impl FromLua for AnyUserData {
|
||||
Value::UserData(ud) => Ok(ud),
|
||||
_ => Err(Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "userdata",
|
||||
to: "userdata".to_string(),
|
||||
message: None,
|
||||
}),
|
||||
}
|
||||
@@ -241,18 +243,22 @@ impl IntoLua for Error {
|
||||
|
||||
impl FromLua for Error {
|
||||
#[inline]
|
||||
fn from_lua(value: Value, lua: &Lua) -> Result<Error> {
|
||||
fn from_lua(value: Value, _: &Lua) -> Result<Error> {
|
||||
match value {
|
||||
Value::Error(err) => Ok(*err),
|
||||
val => Ok(Error::runtime(
|
||||
lua.coerce_string(val)?
|
||||
.and_then(|s| Some(s.to_str().ok()?.to_owned()))
|
||||
.unwrap_or_else(|| "<unprintable error>".to_owned()),
|
||||
)),
|
||||
val => Ok(Error::runtime(val.to_string()?)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "anyhow")]
|
||||
impl IntoLua for anyhow::Error {
|
||||
#[inline]
|
||||
fn into_lua(self, _: &Lua) -> Result<Value> {
|
||||
Ok(Value::Error(Box::new(Error::from(self))))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoLua for RegistryKey {
|
||||
#[inline]
|
||||
fn into_lua(self, lua: &Lua) -> Result<Value> {
|
||||
@@ -336,7 +342,7 @@ impl FromLua for LightUserData {
|
||||
Value::LightUserData(ud) => Ok(ud),
|
||||
_ => Err(Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "light userdata",
|
||||
to: "lightuserdata".to_string(),
|
||||
message: None,
|
||||
}),
|
||||
}
|
||||
@@ -344,7 +350,7 @@ impl FromLua for LightUserData {
|
||||
}
|
||||
|
||||
#[cfg(feature = "luau")]
|
||||
impl IntoLua for crate::types::Vector {
|
||||
impl IntoLua for crate::Vector {
|
||||
#[inline]
|
||||
fn into_lua(self, _: &Lua) -> Result<Value> {
|
||||
Ok(Value::Vector(self))
|
||||
@@ -352,14 +358,51 @@ impl IntoLua for crate::types::Vector {
|
||||
}
|
||||
|
||||
#[cfg(feature = "luau")]
|
||||
impl FromLua for crate::types::Vector {
|
||||
impl FromLua for crate::Vector {
|
||||
#[inline]
|
||||
fn from_lua(value: Value, _: &Lua) -> Result<Self> {
|
||||
match value {
|
||||
Value::Vector(v) => Ok(v),
|
||||
_ => Err(Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "vector",
|
||||
to: "vector".to_string(),
|
||||
message: None,
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "luau")]
|
||||
impl IntoLua for crate::Buffer {
|
||||
#[inline]
|
||||
fn into_lua(self, _: &Lua) -> Result<Value> {
|
||||
Ok(Value::Buffer(self))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "luau")]
|
||||
impl IntoLua for &crate::Buffer {
|
||||
#[inline]
|
||||
fn into_lua(self, _: &Lua) -> Result<Value> {
|
||||
Ok(Value::Buffer(self.clone()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
|
||||
lua.push_ref(&self.0);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "luau")]
|
||||
impl FromLua for crate::Buffer {
|
||||
#[inline]
|
||||
fn from_lua(value: Value, _: &Lua) -> Result<Self> {
|
||||
match value {
|
||||
Value::Buffer(buf) => Ok(buf),
|
||||
_ => Err(Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "buffer".to_string(),
|
||||
message: None,
|
||||
}),
|
||||
}
|
||||
@@ -386,7 +429,7 @@ impl FromLua for StdString {
|
||||
.coerce_string(value)?
|
||||
.ok_or_else(|| Error::FromLuaConversionError {
|
||||
from: ty,
|
||||
to: "String",
|
||||
to: Self::type_name(),
|
||||
message: Some("expected string or number".to_string()),
|
||||
})?
|
||||
.to_str()?
|
||||
@@ -405,7 +448,7 @@ impl FromLua for StdString {
|
||||
.map(|s| s.to_owned())
|
||||
.map_err(|e| Error::FromLuaConversionError {
|
||||
from: "string",
|
||||
to: "String",
|
||||
to: Self::type_name(),
|
||||
message: Some(e.to_string()),
|
||||
});
|
||||
}
|
||||
@@ -448,7 +491,7 @@ impl FromLua for Box<str> {
|
||||
.coerce_string(value)?
|
||||
.ok_or_else(|| Error::FromLuaConversionError {
|
||||
from: ty,
|
||||
to: "Box<str>",
|
||||
to: Self::type_name(),
|
||||
message: Some("expected string or number".to_string()),
|
||||
})?
|
||||
.to_str()?
|
||||
@@ -472,7 +515,7 @@ impl FromLua for CString {
|
||||
.coerce_string(value)?
|
||||
.ok_or_else(|| Error::FromLuaConversionError {
|
||||
from: ty,
|
||||
to: "CString",
|
||||
to: Self::type_name(),
|
||||
message: Some("expected string or number".to_string()),
|
||||
})?;
|
||||
|
||||
@@ -480,7 +523,7 @@ impl FromLua for CString {
|
||||
Ok(s) => Ok(s.into()),
|
||||
Err(_) => Err(Error::FromLuaConversionError {
|
||||
from: ty,
|
||||
to: "CString",
|
||||
to: Self::type_name(),
|
||||
message: Some("invalid C-style string".to_string()),
|
||||
}),
|
||||
}
|
||||
@@ -514,18 +557,12 @@ impl FromLua for BString {
|
||||
match value {
|
||||
Value::String(s) => Ok((*s.as_bytes()).into()),
|
||||
#[cfg(feature = "luau")]
|
||||
Value::UserData(ud) if ud.1 == crate::types::SubtypeId::Buffer => unsafe {
|
||||
let lua = ud.0.lua.lock();
|
||||
let mut size = 0usize;
|
||||
let buf = ffi::lua_tobuffer(lua.ref_thread(), ud.0.index, &mut size);
|
||||
mlua_assert!(!buf.is_null(), "invalid Luau buffer");
|
||||
Ok(slice::from_raw_parts(buf as *const u8, size).into())
|
||||
},
|
||||
Value::Buffer(buf) => unsafe { Ok(buf.as_slice().into()) },
|
||||
_ => Ok((*lua
|
||||
.coerce_string(value)?
|
||||
.ok_or_else(|| Error::FromLuaConversionError {
|
||||
from: ty,
|
||||
to: "BString",
|
||||
to: Self::type_name(),
|
||||
message: Some("expected string or number".to_string()),
|
||||
})?
|
||||
.as_bytes())
|
||||
@@ -563,6 +600,61 @@ impl IntoLua for &BStr {
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoLua for OsString {
|
||||
#[inline]
|
||||
fn into_lua(self, lua: &Lua) -> Result<Value> {
|
||||
self.as_os_str().into_lua(lua)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromLua for OsString {
|
||||
#[inline]
|
||||
fn from_lua(value: Value, lua: &Lua) -> Result<Self> {
|
||||
let ty = value.type_name();
|
||||
let bs = BString::from_lua(value, lua)?;
|
||||
Vec::from(bs)
|
||||
.into_os_string()
|
||||
.map_err(|err| Error::FromLuaConversionError {
|
||||
from: ty,
|
||||
to: "OsString".into(),
|
||||
message: Some(err.to_string()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoLua for &OsStr {
|
||||
#[inline]
|
||||
fn into_lua(self, lua: &Lua) -> Result<Value> {
|
||||
let s = <[u8]>::from_os_str(self).ok_or_else(|| Error::ToLuaConversionError {
|
||||
from: "OsStr".into(),
|
||||
to: "string",
|
||||
message: Some("invalid utf-8 encoding".into()),
|
||||
})?;
|
||||
Ok(Value::String(lua.create_string(s)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoLua for PathBuf {
|
||||
#[inline]
|
||||
fn into_lua(self, lua: &Lua) -> Result<Value> {
|
||||
self.as_os_str().into_lua(lua)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromLua for PathBuf {
|
||||
#[inline]
|
||||
fn from_lua(value: Value, lua: &Lua) -> Result<Self> {
|
||||
OsString::from_lua(value, lua).map(PathBuf::from)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoLua for &Path {
|
||||
#[inline]
|
||||
fn into_lua(self, lua: &Lua) -> Result<Value> {
|
||||
self.as_os_str().into_lua(lua)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn push_bytes_into_stack<T>(this: T, lua: &RawLua) -> Result<()>
|
||||
where
|
||||
@@ -588,7 +680,7 @@ macro_rules! lua_convert_int {
|
||||
.or_else(|| cast(self).map(Value::Number))
|
||||
// This is impossible error because conversion to Number never fails
|
||||
.ok_or_else(|| Error::ToLuaConversionError {
|
||||
from: stringify!($x),
|
||||
from: stringify!($x).to_string(),
|
||||
to: "number",
|
||||
message: Some("out of range".to_owned()),
|
||||
})
|
||||
@@ -619,7 +711,7 @@ macro_rules! lua_convert_int {
|
||||
lua.coerce_number(value)?
|
||||
.ok_or_else(|| Error::FromLuaConversionError {
|
||||
from: ty,
|
||||
to: stringify!($x),
|
||||
to: stringify!($x).to_string(),
|
||||
message: Some(
|
||||
"expected number or string coercible to number".to_string(),
|
||||
),
|
||||
@@ -630,7 +722,7 @@ macro_rules! lua_convert_int {
|
||||
})
|
||||
.ok_or_else(|| Error::FromLuaConversionError {
|
||||
from: ty,
|
||||
to: stringify!($x),
|
||||
to: stringify!($x).to_string(),
|
||||
message: Some("out of range".to_owned()),
|
||||
})
|
||||
}
|
||||
@@ -644,7 +736,7 @@ macro_rules! lua_convert_int {
|
||||
if ok != 0 {
|
||||
return cast(i).ok_or_else(|| Error::FromLuaConversionError {
|
||||
from: "integer",
|
||||
to: stringify!($x),
|
||||
to: stringify!($x).to_string(),
|
||||
message: Some("out of range".to_owned()),
|
||||
});
|
||||
}
|
||||
@@ -676,7 +768,7 @@ macro_rules! lua_convert_float {
|
||||
fn into_lua(self, _: &Lua) -> Result<Value> {
|
||||
cast(self)
|
||||
.ok_or_else(|| Error::ToLuaConversionError {
|
||||
from: stringify!($x),
|
||||
from: stringify!($x).to_string(),
|
||||
to: "number",
|
||||
message: Some("out of range".to_string()),
|
||||
})
|
||||
@@ -691,13 +783,13 @@ macro_rules! lua_convert_float {
|
||||
lua.coerce_number(value)?
|
||||
.ok_or_else(|| Error::FromLuaConversionError {
|
||||
from: ty,
|
||||
to: stringify!($x),
|
||||
to: stringify!($x).to_string(),
|
||||
message: Some("expected number or string coercible to number".to_string()),
|
||||
})
|
||||
.and_then(|n| {
|
||||
cast(n).ok_or_else(|| Error::FromLuaConversionError {
|
||||
from: ty,
|
||||
to: stringify!($x),
|
||||
to: stringify!($x).to_string(),
|
||||
message: Some("number out of range".to_string()),
|
||||
})
|
||||
})
|
||||
@@ -712,7 +804,7 @@ macro_rules! lua_convert_float {
|
||||
if ok != 0 {
|
||||
return cast(i).ok_or_else(|| Error::FromLuaConversionError {
|
||||
from: "number",
|
||||
to: stringify!($x),
|
||||
to: stringify!($x).to_string(),
|
||||
message: Some("out of range".to_owned()),
|
||||
});
|
||||
}
|
||||
@@ -756,7 +848,7 @@ where
|
||||
match value {
|
||||
#[cfg(feature = "luau")]
|
||||
#[rustfmt::skip]
|
||||
Value::Vector(v) if N == crate::types::Vector::SIZE => unsafe {
|
||||
Value::Vector(v) if N == crate::Vector::SIZE => unsafe {
|
||||
use std::{mem, ptr};
|
||||
let mut arr: [mem::MaybeUninit<T>; N] = mem::MaybeUninit::uninit().assume_init();
|
||||
ptr::write(arr[0].as_mut_ptr() , T::from_lua(Value::Number(v.x() as _), _lua)?);
|
||||
@@ -771,13 +863,13 @@ where
|
||||
vec.try_into()
|
||||
.map_err(|vec: Vec<T>| Error::FromLuaConversionError {
|
||||
from: "table",
|
||||
to: "Array",
|
||||
message: Some(format!("expected table of length {}, got {}", N, vec.len())),
|
||||
to: Self::type_name(),
|
||||
message: Some(format!("expected table of length {N}, got {}", vec.len())),
|
||||
})
|
||||
}
|
||||
_ => Err(Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "Array",
|
||||
to: Self::type_name(),
|
||||
message: Some("expected table".to_string()),
|
||||
}),
|
||||
}
|
||||
@@ -812,7 +904,7 @@ impl<T: FromLua> FromLua for Vec<T> {
|
||||
Value::Table(table) => table.sequence_values().collect(),
|
||||
_ => Err(Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "Vec",
|
||||
to: Self::type_name(),
|
||||
message: Some("expected table".to_string()),
|
||||
}),
|
||||
}
|
||||
@@ -834,7 +926,7 @@ impl<K: Eq + Hash + FromLua, V: FromLua, S: BuildHasher + Default> FromLua for H
|
||||
} else {
|
||||
Err(Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "HashMap",
|
||||
to: Self::type_name(),
|
||||
message: Some("expected table".to_string()),
|
||||
})
|
||||
}
|
||||
@@ -856,7 +948,7 @@ impl<K: Ord + FromLua, V: FromLua> FromLua for BTreeMap<K, V> {
|
||||
} else {
|
||||
Err(Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "BTreeMap",
|
||||
to: Self::type_name(),
|
||||
message: Some("expected table".to_string()),
|
||||
})
|
||||
}
|
||||
@@ -880,7 +972,7 @@ impl<T: Eq + Hash + FromLua, S: BuildHasher + Default> FromLua for HashSet<T, S>
|
||||
Value::Table(table) => table.pairs::<T, Value>().map(|res| res.map(|(k, _)| k)).collect(),
|
||||
_ => Err(Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "HashSet",
|
||||
to: Self::type_name(),
|
||||
message: Some("expected table".to_string()),
|
||||
}),
|
||||
}
|
||||
@@ -904,7 +996,7 @@ impl<T: Ord + FromLua> FromLua for BTreeSet<T> {
|
||||
Value::Table(table) => table.pairs::<T, Value>().map(|res| res.map(|(k, _)| k)).collect(),
|
||||
_ => Err(Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "BTreeSet",
|
||||
to: Self::type_name(),
|
||||
message: Some("expected table".to_string()),
|
||||
}),
|
||||
}
|
||||
@@ -947,3 +1039,59 @@ impl<T: FromLua> FromLua for Option<T> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<L: IntoLua, R: IntoLua> IntoLua for Either<L, R> {
|
||||
#[inline]
|
||||
fn into_lua(self, lua: &Lua) -> Result<Value> {
|
||||
match self {
|
||||
Either::Left(l) => l.into_lua(lua),
|
||||
Either::Right(r) => r.into_lua(lua),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
|
||||
match self {
|
||||
Either::Left(l) => l.push_into_stack(lua),
|
||||
Either::Right(r) => r.push_into_stack(lua),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<L: FromLua, R: FromLua> FromLua for Either<L, R> {
|
||||
#[inline]
|
||||
fn from_lua(value: Value, lua: &Lua) -> Result<Self> {
|
||||
let value_type_name = value.type_name();
|
||||
// Try the left type first
|
||||
match L::from_lua(value.clone(), lua) {
|
||||
Ok(l) => Ok(Either::Left(l)),
|
||||
// Try the right type
|
||||
Err(_) => match R::from_lua(value, lua).map(Either::Right) {
|
||||
Ok(r) => Ok(r),
|
||||
Err(_) => Err(Error::FromLuaConversionError {
|
||||
from: value_type_name,
|
||||
to: Self::type_name(),
|
||||
message: None,
|
||||
}),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn from_stack(idx: c_int, lua: &RawLua) -> Result<Self> {
|
||||
match L::from_stack(idx, lua) {
|
||||
Ok(l) => Ok(Either::Left(l)),
|
||||
Err(_) => match R::from_stack(idx, lua).map(Either::Right) {
|
||||
Ok(r) => Ok(r),
|
||||
Err(_) => {
|
||||
let value_type_name = CStr::from_ptr(ffi::luaL_typename(lua.state(), idx));
|
||||
Err(Error::FromLuaConversionError {
|
||||
from: value_type_name.to_str().unwrap(),
|
||||
to: Self::type_name(),
|
||||
message: None,
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+131
-50
@@ -9,6 +9,12 @@ use std::sync::Arc;
|
||||
|
||||
use crate::private::Sealed;
|
||||
|
||||
#[cfg(feature = "error-send")]
|
||||
type DynStdError = dyn StdError + Send + Sync;
|
||||
|
||||
#[cfg(not(feature = "error-send"))]
|
||||
type DynStdError = dyn StdError;
|
||||
|
||||
/// Error type returned by `mlua` methods.
|
||||
#[derive(Debug, Clone)]
|
||||
#[non_exhaustive]
|
||||
@@ -42,11 +48,11 @@ pub enum Error {
|
||||
GarbageCollectorError(StdString),
|
||||
/// Potentially unsafe action in safe mode.
|
||||
SafetyError(StdString),
|
||||
/// Setting memory limit is not available.
|
||||
/// Memory control is not available.
|
||||
///
|
||||
/// This error can only happen when Lua state was not created by us and does not have the
|
||||
/// custom allocator attached.
|
||||
MemoryLimitNotAvailable,
|
||||
MemoryControlNotAvailable,
|
||||
/// A mutable callback has triggered Lua code that has called the same mutable callback again.
|
||||
///
|
||||
/// This is an error because a mutable callback can only be borrowed mutably once.
|
||||
@@ -61,10 +67,12 @@ pub enum Error {
|
||||
///
|
||||
/// Due to the way `mlua` works, it should not be directly possible to run out of stack space
|
||||
/// during normal use. The only way that this error can be triggered is if a `Function` is
|
||||
/// called with a huge number of arguments, or a rust callback returns a huge number of return
|
||||
/// called with a huge number of arguments, or a Rust callback returns a huge number of return
|
||||
/// values.
|
||||
StackError,
|
||||
/// Too many arguments to `Function::bind`.
|
||||
/// Too many arguments to [`Function::bind`].
|
||||
///
|
||||
/// [`Function::bind`]: crate::Function::bind
|
||||
BindError,
|
||||
/// Bad argument received from Lua (usually when calling a function).
|
||||
///
|
||||
@@ -83,7 +91,7 @@ pub enum Error {
|
||||
/// A Rust value could not be converted to a Lua value.
|
||||
ToLuaConversionError {
|
||||
/// Name of the Rust type that could not be converted.
|
||||
from: &'static str,
|
||||
from: String,
|
||||
/// Name of the Lua type that could not be created.
|
||||
to: &'static str,
|
||||
/// A message indicating why the conversion failed in more detail.
|
||||
@@ -94,7 +102,7 @@ pub enum Error {
|
||||
/// Name of the Lua type that could not be converted.
|
||||
from: &'static str,
|
||||
/// Name of the Rust type that could not be created.
|
||||
to: &'static str,
|
||||
to: String,
|
||||
/// A string containing more detailed error information.
|
||||
message: Option<StdString>,
|
||||
},
|
||||
@@ -189,7 +197,7 @@ pub enum Error {
|
||||
/// Returning `Err(ExternalError(...))` from a Rust callback will raise the error as a Lua
|
||||
/// error. The Rust code that originally invoked the Lua code then receives a `CallbackError`,
|
||||
/// from which the original error (and a stack traceback) can be recovered.
|
||||
ExternalError(Arc<dyn StdError + Send + Sync>),
|
||||
ExternalError(Arc<DynStdError>),
|
||||
/// An error with additional context.
|
||||
WithContext {
|
||||
/// A string containing additional context.
|
||||
@@ -205,21 +213,21 @@ pub type Result<T> = StdResult<T, Error>;
|
||||
#[cfg(not(tarpaulin_include))]
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
Error::SyntaxError { ref message, .. } => write!(fmt, "syntax error: {message}"),
|
||||
Error::RuntimeError(ref msg) => write!(fmt, "runtime error: {msg}"),
|
||||
Error::MemoryError(ref msg) => {
|
||||
match self {
|
||||
Error::SyntaxError { message, .. } => write!(fmt, "syntax error: {message}"),
|
||||
Error::RuntimeError(msg) => write!(fmt, "runtime error: {msg}"),
|
||||
Error::MemoryError(msg) => {
|
||||
write!(fmt, "memory error: {msg}")
|
||||
}
|
||||
#[cfg(any(feature = "lua53", feature = "lua52"))]
|
||||
Error::GarbageCollectorError(ref msg) => {
|
||||
Error::GarbageCollectorError(msg) => {
|
||||
write!(fmt, "garbage collector error: {msg}")
|
||||
}
|
||||
Error::SafetyError(ref msg) => {
|
||||
Error::SafetyError(msg) => {
|
||||
write!(fmt, "safety error: {msg}")
|
||||
},
|
||||
Error::MemoryLimitNotAvailable => {
|
||||
write!(fmt, "setting memory limit is not available")
|
||||
Error::MemoryControlNotAvailable => {
|
||||
write!(fmt, "memory control is not available")
|
||||
}
|
||||
Error::RecursiveMutCallback => write!(fmt, "mutable callback called recursively"),
|
||||
Error::CallbackDestructed => write!(
|
||||
@@ -234,7 +242,7 @@ impl fmt::Display for Error {
|
||||
fmt,
|
||||
"too many arguments to Function::bind"
|
||||
),
|
||||
Error::BadArgument { ref to, pos, ref name, ref cause } => {
|
||||
Error::BadArgument { to, pos, name, cause } => {
|
||||
if let Some(name) = name {
|
||||
write!(fmt, "bad argument `{name}`")?;
|
||||
} else {
|
||||
@@ -245,18 +253,18 @@ impl fmt::Display for Error {
|
||||
}
|
||||
write!(fmt, ": {cause}")
|
||||
},
|
||||
Error::ToLuaConversionError { from, to, ref message } => {
|
||||
Error::ToLuaConversionError { from, to, message } => {
|
||||
write!(fmt, "error converting {from} to Lua {to}")?;
|
||||
match *message {
|
||||
match message {
|
||||
None => Ok(()),
|
||||
Some(ref message) => write!(fmt, " ({message})"),
|
||||
Some(message) => write!(fmt, " ({message})"),
|
||||
}
|
||||
}
|
||||
Error::FromLuaConversionError { from, to, ref message } => {
|
||||
Error::FromLuaConversionError { from, to, message } => {
|
||||
write!(fmt, "error converting Lua {from} to {to}")?;
|
||||
match *message {
|
||||
match message {
|
||||
None => Ok(()),
|
||||
Some(ref message) => write!(fmt, " ({message})"),
|
||||
Some(message) => write!(fmt, " ({message})"),
|
||||
}
|
||||
}
|
||||
Error::CoroutineUnresumable => write!(fmt, "coroutine is non-resumable"),
|
||||
@@ -264,21 +272,21 @@ impl fmt::Display for Error {
|
||||
Error::UserDataDestructed => write!(fmt, "userdata has been destructed"),
|
||||
Error::UserDataBorrowError => write!(fmt, "error borrowing userdata"),
|
||||
Error::UserDataBorrowMutError => write!(fmt, "error mutably borrowing userdata"),
|
||||
Error::MetaMethodRestricted(ref method) => write!(fmt, "metamethod {method} is restricted"),
|
||||
Error::MetaMethodTypeError { ref method, type_name, ref message } => {
|
||||
Error::MetaMethodRestricted(method) => write!(fmt, "metamethod {method} is restricted"),
|
||||
Error::MetaMethodTypeError { method, type_name, message } => {
|
||||
write!(fmt, "metamethod {method} has unsupported type {type_name}")?;
|
||||
match *message {
|
||||
match message {
|
||||
None => Ok(()),
|
||||
Some(ref message) => write!(fmt, " ({message})"),
|
||||
Some(message) => write!(fmt, " ({message})"),
|
||||
}
|
||||
}
|
||||
Error::MismatchedRegistryKey => {
|
||||
write!(fmt, "RegistryKey used from different Lua state")
|
||||
}
|
||||
Error::CallbackError { ref cause, ref traceback } => {
|
||||
Error::CallbackError { cause, traceback } => {
|
||||
// Trace errors down to the root
|
||||
let (mut cause, mut full_traceback) = (cause, None);
|
||||
while let Error::CallbackError { cause: ref cause2, traceback: ref traceback2 } = **cause {
|
||||
while let Error::CallbackError { cause: cause2, traceback: traceback2 } = &**cause {
|
||||
cause = cause2;
|
||||
full_traceback = Some(traceback2);
|
||||
}
|
||||
@@ -302,15 +310,15 @@ impl fmt::Display for Error {
|
||||
write!(fmt, "previously resumed panic returned again")
|
||||
}
|
||||
#[cfg(feature = "serialize")]
|
||||
Error::SerializeError(ref err) => {
|
||||
Error::SerializeError(err) => {
|
||||
write!(fmt, "serialize error: {err}")
|
||||
},
|
||||
#[cfg(feature = "serialize")]
|
||||
Error::DeserializeError(ref err) => {
|
||||
Error::DeserializeError(err) => {
|
||||
write!(fmt, "deserialize error: {err}")
|
||||
},
|
||||
Error::ExternalError(ref err) => write!(fmt, "{err}"),
|
||||
Error::WithContext { ref context, ref cause } => {
|
||||
Error::ExternalError(err) => err.fmt(fmt),
|
||||
Error::WithContext { context, cause } => {
|
||||
writeln!(fmt, "{context}")?;
|
||||
write!(fmt, "{cause}")
|
||||
}
|
||||
@@ -320,18 +328,15 @@ impl fmt::Display for Error {
|
||||
|
||||
impl StdError for Error {
|
||||
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
||||
match *self {
|
||||
match self {
|
||||
// An error type with a source error should either return that error via source or
|
||||
// include that source's error message in its own Display output, but never both.
|
||||
// https://blog.rust-lang.org/inside-rust/2021/07/01/What-the-error-handling-project-group-is-working-towards.html
|
||||
// Given that we include source to fmt::Display implementation for `CallbackError`, this call
|
||||
// returns nothing.
|
||||
Error::CallbackError { .. } => None,
|
||||
Error::ExternalError(ref err) => err.source(),
|
||||
Error::WithContext { ref cause, .. } => match cause.as_ref() {
|
||||
Error::ExternalError(err) => err.source(),
|
||||
_ => None,
|
||||
},
|
||||
Error::ExternalError(err) => err.source(),
|
||||
Error::WithContext { cause, .. } => Self::source(cause),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -346,7 +351,7 @@ impl Error {
|
||||
|
||||
/// Wraps an external error object.
|
||||
#[inline]
|
||||
pub fn external<T: Into<Box<dyn StdError + Send + Sync>>>(err: T) -> Self {
|
||||
pub fn external<T: Into<Box<DynStdError>>>(err: T) -> Self {
|
||||
Error::ExternalError(err.into().into())
|
||||
}
|
||||
|
||||
@@ -357,10 +362,25 @@ impl Error {
|
||||
{
|
||||
match self {
|
||||
Error::ExternalError(err) => err.downcast_ref(),
|
||||
Error::WithContext { cause, .. } => match cause.as_ref() {
|
||||
Error::ExternalError(err) => err.downcast_ref(),
|
||||
_ => None,
|
||||
},
|
||||
Error::WithContext { cause, .. } => Self::downcast_ref(cause),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// An iterator over the chain of nested errors wrapped by this Error.
|
||||
pub fn chain(&self) -> impl Iterator<Item = &(dyn StdError + 'static)> {
|
||||
Chain {
|
||||
root: self,
|
||||
current: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the parent of this error.
|
||||
#[doc(hidden)]
|
||||
pub fn parent(&self) -> Option<&Error> {
|
||||
match self {
|
||||
Error::CallbackError { cause, .. } => Some(cause.as_ref()),
|
||||
Error::WithContext { cause, .. } => Some(cause.as_ref()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -374,15 +394,15 @@ impl Error {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn from_lua_conversion<'a>(
|
||||
pub(crate) fn from_lua_conversion(
|
||||
from: &'static str,
|
||||
to: &'static str,
|
||||
message: impl Into<Option<&'a str>>,
|
||||
to: impl ToString,
|
||||
message: impl Into<Option<String>>,
|
||||
) -> Self {
|
||||
Error::FromLuaConversionError {
|
||||
from,
|
||||
to,
|
||||
message: message.into().map(|s| s.into()),
|
||||
to: to.to_string(),
|
||||
message: message.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -392,7 +412,7 @@ pub trait ExternalError {
|
||||
fn into_lua_err(self) -> Error;
|
||||
}
|
||||
|
||||
impl<E: Into<Box<dyn StdError + Send + Sync>>> ExternalError for E {
|
||||
impl<E: Into<Box<DynStdError>>> ExternalError for E {
|
||||
fn into_lua_err(self) -> Error {
|
||||
Error::external(self)
|
||||
}
|
||||
@@ -446,7 +466,7 @@ impl ErrorContext for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ErrorContext for StdResult<T, Error> {
|
||||
impl<T> ErrorContext for Result<T> {
|
||||
fn context<C: fmt::Display>(self, context: C) -> Self {
|
||||
self.map_err(|err| err.context(context))
|
||||
}
|
||||
@@ -487,3 +507,64 @@ impl serde::de::Error for Error {
|
||||
Self::DeserializeError(msg.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "anyhow")]
|
||||
impl From<anyhow::Error> for Error {
|
||||
fn from(err: anyhow::Error) -> Self {
|
||||
match err.downcast::<Self>() {
|
||||
Ok(err) => err,
|
||||
Err(err) => Error::external(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Chain<'a> {
|
||||
root: &'a Error,
|
||||
current: Option<&'a (dyn StdError + 'static)>,
|
||||
}
|
||||
|
||||
impl<'a> Iterator for Chain<'a> {
|
||||
type Item = &'a (dyn StdError + 'static);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
loop {
|
||||
let error: Option<&dyn StdError> = match self.current {
|
||||
None => {
|
||||
self.current = Some(self.root);
|
||||
self.current
|
||||
}
|
||||
Some(current) => match current.downcast_ref::<Error>()? {
|
||||
Error::BadArgument { cause, .. }
|
||||
| Error::CallbackError { cause, .. }
|
||||
| Error::WithContext { cause, .. } => {
|
||||
self.current = Some(&**cause);
|
||||
self.current
|
||||
}
|
||||
Error::ExternalError(err) => {
|
||||
self.current = Some(&**err);
|
||||
self.current
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
};
|
||||
|
||||
// Skip `ExternalError` as it only wraps the underlying error
|
||||
// without meaningful context
|
||||
if let Some(Error::ExternalError(_)) = error?.downcast_ref::<Error>() {
|
||||
continue;
|
||||
}
|
||||
|
||||
return self.current;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod assertions {
|
||||
use super::*;
|
||||
|
||||
#[cfg(not(feature = "error-send"))]
|
||||
static_assertions::assert_not_impl_any!(Error: Send, Sync);
|
||||
#[cfg(feature = "send")]
|
||||
static_assertions::assert_impl_all!(Error: Send, Sync);
|
||||
}
|
||||
|
||||
+81
-25
@@ -5,20 +5,22 @@ use std::{mem, ptr, slice};
|
||||
use crate::error::{Error, Result};
|
||||
use crate::state::Lua;
|
||||
use crate::table::Table;
|
||||
use crate::types::{Callback, MaybeSend, ValueRef};
|
||||
use crate::traits::{FromLuaMulti, IntoLua, IntoLuaMulti, LuaNativeFn, LuaNativeFnMut};
|
||||
use crate::types::{Callback, LuaType, MaybeSend, ValueRef};
|
||||
use crate::util::{
|
||||
assert_stack, check_stack, linenumber_to_usize, pop_error, ptr_to_lossy_str, ptr_to_str, StackGuard,
|
||||
};
|
||||
use crate::value::{FromLuaMulti, IntoLua, IntoLuaMulti, Value};
|
||||
use crate::value::Value;
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
use {
|
||||
crate::traits::LuaNativeAsyncFn,
|
||||
crate::types::AsyncCallback,
|
||||
std::future::{self, Future},
|
||||
};
|
||||
|
||||
/// Handle to an internal Lua function.
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Function(pub(crate) ValueRef);
|
||||
|
||||
/// Contains information about a function.
|
||||
@@ -387,9 +389,9 @@ impl Function {
|
||||
/// If `strip` is true, the binary representation may not include all debug information
|
||||
/// about the function, to save space.
|
||||
///
|
||||
/// For Luau a [Compiler] can be used to compile Lua chunks to bytecode.
|
||||
/// For Luau a [`Compiler`] can be used to compile Lua chunks to bytecode.
|
||||
///
|
||||
/// [Compiler]: crate::chunk::Compiler
|
||||
/// [`Compiler`]: crate::chunk::Compiler
|
||||
#[cfg(not(feature = "luau"))]
|
||||
#[cfg_attr(docsrs, doc(cfg(not(feature = "luau"))))]
|
||||
pub fn dump(&self, strip: bool) -> Vec<u8> {
|
||||
@@ -488,10 +490,10 @@ impl Function {
|
||||
///
|
||||
/// Copies the function prototype and all its upvalues to the
|
||||
/// newly created function.
|
||||
///
|
||||
/// This function returns shallow clone (same handle) for Rust/C functions.
|
||||
///
|
||||
/// Requires `feature = "luau"`
|
||||
#[cfg(feature = "luau")]
|
||||
#[cfg(any(feature = "luau", doc))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
|
||||
pub fn deep_clone(&self) -> Self {
|
||||
let lua = self.0.lua.lock();
|
||||
@@ -507,12 +509,6 @@ impl Function {
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Function {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.0 == other.0
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct WrappedFunction(pub(crate) Callback);
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
@@ -522,31 +518,65 @@ impl Function {
|
||||
/// Wraps a Rust function or closure, returning an opaque type that implements [`IntoLua`]
|
||||
/// trait.
|
||||
#[inline]
|
||||
pub fn wrap<A, R, F>(func: F) -> impl IntoLua
|
||||
pub fn wrap<F, A, R>(func: F) -> impl IntoLua
|
||||
where
|
||||
F: LuaNativeFn<A, Output = Result<R>> + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
R: IntoLuaMulti,
|
||||
F: Fn(&Lua, A) -> Result<R> + MaybeSend + 'static,
|
||||
{
|
||||
WrappedFunction(Box::new(move |lua, nargs| unsafe {
|
||||
let args = A::from_stack_args(nargs, 1, None, lua)?;
|
||||
func(lua.lua(), args)?.push_into_stack_multi(lua)
|
||||
func.call(args)?.push_into_stack_multi(lua)
|
||||
}))
|
||||
}
|
||||
|
||||
/// Wraps a Rust mutable closure, returning an opaque type that implements [`IntoLua`] trait.
|
||||
#[inline]
|
||||
pub fn wrap_mut<A, R, F>(func: F) -> impl IntoLua
|
||||
pub fn wrap_mut<F, A, R>(func: F) -> impl IntoLua
|
||||
where
|
||||
F: LuaNativeFnMut<A, Output = Result<R>> + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
R: IntoLuaMulti,
|
||||
F: FnMut(&Lua, A) -> Result<R> + MaybeSend + 'static,
|
||||
{
|
||||
let func = RefCell::new(func);
|
||||
WrappedFunction(Box::new(move |lua, nargs| unsafe {
|
||||
let mut func = func.try_borrow_mut().map_err(|_| Error::RecursiveMutCallback)?;
|
||||
let args = A::from_stack_args(nargs, 1, None, lua)?;
|
||||
func(lua.lua(), args)?.push_into_stack_multi(lua)
|
||||
func.call(args)?.push_into_stack_multi(lua)
|
||||
}))
|
||||
}
|
||||
|
||||
/// Wraps a Rust function or closure, returning an opaque type that implements [`IntoLua`]
|
||||
/// trait.
|
||||
///
|
||||
/// This function is similar to [`Function::wrap`] but any returned `Result` will be converted
|
||||
/// to a `ok, err` tuple without throwing an exception.
|
||||
#[inline]
|
||||
pub fn wrap_raw<F, A>(func: F) -> impl IntoLua
|
||||
where
|
||||
F: LuaNativeFn<A> + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
{
|
||||
WrappedFunction(Box::new(move |lua, nargs| unsafe {
|
||||
let args = A::from_stack_args(nargs, 1, None, lua)?;
|
||||
func.call(args).push_into_stack_multi(lua)
|
||||
}))
|
||||
}
|
||||
|
||||
/// Wraps a Rust mutable closure, returning an opaque type that implements [`IntoLua`] trait.
|
||||
///
|
||||
/// This function is similar to [`Function::wrap_mut`] but any returned `Result` will be
|
||||
/// converted to a `ok, err` tuple without throwing an exception.
|
||||
#[inline]
|
||||
pub fn wrap_raw_mut<F, A>(func: F) -> impl IntoLua
|
||||
where
|
||||
F: LuaNativeFnMut<A> + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
{
|
||||
let func = RefCell::new(func);
|
||||
WrappedFunction(Box::new(move |lua, nargs| unsafe {
|
||||
let mut func = func.try_borrow_mut().map_err(|_| Error::RecursiveMutCallback)?;
|
||||
let args = A::from_stack_args(nargs, 1, None, lua)?;
|
||||
func.call(args).push_into_stack_multi(lua)
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -554,23 +584,45 @@ impl Function {
|
||||
/// trait.
|
||||
#[cfg(feature = "async")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
|
||||
pub fn wrap_async<A, R, F, FR>(func: F) -> impl IntoLua
|
||||
pub fn wrap_async<F, A, R>(func: F) -> impl IntoLua
|
||||
where
|
||||
F: LuaNativeAsyncFn<A, Output = Result<R>> + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
R: IntoLuaMulti,
|
||||
F: Fn(Lua, A) -> FR + MaybeSend + 'static,
|
||||
FR: Future<Output = Result<R>> + MaybeSend + 'static,
|
||||
{
|
||||
WrappedAsyncFunction(Box::new(move |rawlua, nargs| unsafe {
|
||||
let args = match A::from_stack_args(nargs, 1, None, rawlua) {
|
||||
Ok(args) => args,
|
||||
Err(e) => return Box::pin(future::ready(Err(e))),
|
||||
};
|
||||
let lua = rawlua.lua().clone();
|
||||
let fut = func(lua.clone(), args);
|
||||
let lua = rawlua.lua();
|
||||
let fut = func.call(args);
|
||||
Box::pin(async move { fut.await?.push_into_stack_multi(lua.raw_lua()) })
|
||||
}))
|
||||
}
|
||||
|
||||
/// Wraps a Rust async function or closure, returning an opaque type that implements [`IntoLua`]
|
||||
/// trait.
|
||||
///
|
||||
/// This function is similar to [`Function::wrap_async`] but any returned `Result` will be
|
||||
/// converted to a `ok, err` tuple without throwing an exception.
|
||||
#[cfg(feature = "async")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
|
||||
pub fn wrap_raw_async<F, A>(func: F) -> impl IntoLua
|
||||
where
|
||||
F: LuaNativeAsyncFn<A> + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
{
|
||||
WrappedAsyncFunction(Box::new(move |rawlua, nargs| unsafe {
|
||||
let args = match A::from_stack_args(nargs, 1, None, rawlua) {
|
||||
Ok(args) => args,
|
||||
Err(e) => return Box::pin(future::ready(Err(e))),
|
||||
};
|
||||
let lua = rawlua.lua();
|
||||
let fut = func.call(args);
|
||||
Box::pin(async move { fut.await.push_into_stack_multi(lua.raw_lua()) })
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoLua for WrappedFunction {
|
||||
@@ -588,6 +640,10 @@ impl IntoLua for WrappedAsyncFunction {
|
||||
}
|
||||
}
|
||||
|
||||
impl LuaType for Function {
|
||||
const TYPE_ID: c_int = ffi::LUA_TFUNCTION;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod assertions {
|
||||
use super::*;
|
||||
|
||||
+14
-9
@@ -16,9 +16,9 @@ use crate::util::{linenumber_to_usize, ptr_to_lossy_str, ptr_to_str};
|
||||
/// The `Debug` structure is provided as a parameter to the hook function set with
|
||||
/// [`Lua::set_hook`]. You may call the methods on this structure to retrieve information about the
|
||||
/// Lua code executing at the time that the hook function was called. Further information can be
|
||||
/// found in the Lua [documentation][lua_doc].
|
||||
/// found in the Lua [documentation].
|
||||
///
|
||||
/// [lua_doc]: https://www.lua.org/manual/5.4/manual.html#lua_Debug
|
||||
/// [documentation]: https://www.lua.org/manual/5.4/manual.html#lua_Debug
|
||||
/// [`Lua::set_hook`]: crate::Lua::set_hook
|
||||
pub struct Debug<'a> {
|
||||
lua: EitherLua<'a>,
|
||||
@@ -66,7 +66,7 @@ impl<'a> Debug<'a> {
|
||||
|
||||
/// Returns the specific event that triggered the hook.
|
||||
///
|
||||
/// For [Lua 5.1] `DebugEvent::TailCall` is used for return events to indicate a return
|
||||
/// For [Lua 5.1] [`DebugEvent::TailCall`] is used for return events to indicate a return
|
||||
/// from a function that did a tail call.
|
||||
///
|
||||
/// [Lua 5.1]: https://www.lua.org/manual/5.1/manual.html#pdf-LUA_HOOKTAILRET
|
||||
@@ -184,8 +184,8 @@ impl<'a> Debug<'a> {
|
||||
);
|
||||
#[cfg(feature = "luau")]
|
||||
mlua_assert!(
|
||||
ffi::lua_getinfo(self.lua.state(), self.level, cstr!("a"), self.ar.get()) != 0,
|
||||
"lua_getinfo failed with `a`"
|
||||
ffi::lua_getinfo(self.lua.state(), self.level, cstr!("au"), self.ar.get()) != 0,
|
||||
"lua_getinfo failed with `au`"
|
||||
);
|
||||
|
||||
#[cfg(not(feature = "luau"))]
|
||||
@@ -198,8 +198,8 @@ impl<'a> Debug<'a> {
|
||||
};
|
||||
#[cfg(feature = "luau")]
|
||||
let stack = DebugStack {
|
||||
num_ups: (*self.ar.get()).nupvals as i32,
|
||||
num_params: (*self.ar.get()).nparams as i32,
|
||||
num_ups: (*self.ar.get()).nupvals,
|
||||
num_params: (*self.ar.get()).nparams,
|
||||
is_vararg: (*self.ar.get()).isvararg != 0,
|
||||
};
|
||||
stack
|
||||
@@ -262,10 +262,15 @@ pub struct DebugSource<'a> {
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct DebugStack {
|
||||
pub num_ups: i32,
|
||||
/// Number of upvalues.
|
||||
pub num_ups: u8,
|
||||
/// Number of parameters.
|
||||
///
|
||||
/// Requires `feature = "lua54/lua53/lua52/luau"`
|
||||
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52", feature = "luau"))]
|
||||
pub num_params: i32,
|
||||
pub num_params: u8,
|
||||
/// Whether the function is a vararg function.
|
||||
///
|
||||
/// Requires `feature = "lua54/lua53/lua52/luau"`
|
||||
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52", feature = "luau"))]
|
||||
pub is_vararg: bool,
|
||||
|
||||
+27
-37
@@ -32,41 +32,32 @@
|
||||
//! [`serde::Serialize`] or [`serde::Deserialize`] can be converted.
|
||||
//! For convenience, additional functionality to handle `NULL` values and arrays is provided.
|
||||
//!
|
||||
//! The [`Value`] enum implements [`serde::Serialize`] trait to support serializing Lua values
|
||||
//! (including [`UserData`]) into Rust values.
|
||||
//! The [`Value`] enum and other types implement [`serde::Serialize`] trait to support serializing
|
||||
//! Lua values into Rust values.
|
||||
//!
|
||||
//! Requires `feature = "serialize"`.
|
||||
//!
|
||||
//! # Async/await support
|
||||
//!
|
||||
//! The [`create_async_function`] allows creating non-blocking functions that returns [`Future`].
|
||||
//! Lua code with async capabilities can be executed by [`call_async`] family of functions or
|
||||
//! polling [`AsyncThread`] using any runtime (eg. Tokio).
|
||||
//! The [`Lua::create_async_function`] allows creating non-blocking functions that returns
|
||||
//! [`Future`]. Lua code with async capabilities can be executed by [`Function::call_async`] family
|
||||
//! of functions or polling [`AsyncThread`] using any runtime (eg. Tokio).
|
||||
//!
|
||||
//! Requires `feature = "async"`.
|
||||
//!
|
||||
//! # `Send` requirement
|
||||
//! # `Send` and `Sync` support
|
||||
//!
|
||||
//! By default `mlua` is `!Send`. This can be changed by enabling `feature = "send"` that adds
|
||||
//! `Send` requirement to [`Function`]s and [`UserData`].
|
||||
//! `Send` requirement to Rust functions and [`UserData`] types.
|
||||
//!
|
||||
//! In this case [`Lua`] object and their types can be send or used from other threads. Internally
|
||||
//! access to Lua VM is synchronized using a reentrant mutex that can be locked many times within
|
||||
//! the same thread.
|
||||
//!
|
||||
//! [Lua programming language]: https://www.lua.org/
|
||||
//! [`Lua`]: crate::Lua
|
||||
//! [executing]: crate::Chunk::exec
|
||||
//! [evaluating]: crate::Chunk::eval
|
||||
//! [globals]: crate::Lua::globals
|
||||
//! [`IntoLua`]: crate::IntoLua
|
||||
//! [`FromLua`]: crate::FromLua
|
||||
//! [`IntoLuaMulti`]: crate::IntoLuaMulti
|
||||
//! [`FromLuaMulti`]: crate::FromLuaMulti
|
||||
//! [`Function`]: crate::Function
|
||||
//! [`UserData`]: crate::UserData
|
||||
//! [`UserDataFields`]: crate::UserDataFields
|
||||
//! [`UserDataMethods`]: crate::UserDataMethods
|
||||
//! [`LuaSerdeExt`]: crate::LuaSerdeExt
|
||||
//! [`Value`]: crate::Value
|
||||
//! [`create_async_function`]: crate::Lua::create_async_function
|
||||
//! [`call_async`]: crate::Function::call_async
|
||||
//! [`AsyncThread`]: crate::AsyncThread
|
||||
//! [`Future`]: std::future::Future
|
||||
//! [`serde::Serialize`]: https://docs.serde.rs/serde/ser/trait.Serialize.html
|
||||
//! [`serde::Deserialize`]: https://docs.serde.rs/serde/de/trait.Deserialize.html
|
||||
@@ -78,6 +69,7 @@
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
|
||||
mod buffer;
|
||||
mod chunk;
|
||||
mod conversion;
|
||||
mod error;
|
||||
@@ -87,7 +79,7 @@ mod hook;
|
||||
mod luau;
|
||||
mod memory;
|
||||
mod multi;
|
||||
// mod scope;
|
||||
mod scope;
|
||||
mod state;
|
||||
mod stdlib;
|
||||
mod string;
|
||||
@@ -98,6 +90,7 @@ mod types;
|
||||
mod userdata;
|
||||
mod util;
|
||||
mod value;
|
||||
mod vector;
|
||||
|
||||
pub mod prelude;
|
||||
|
||||
@@ -108,34 +101,35 @@ pub use crate::chunk::{AsChunk, Chunk, ChunkMode};
|
||||
pub use crate::error::{Error, ErrorContext, ExternalError, ExternalResult, Result};
|
||||
pub use crate::function::{Function, FunctionInfo};
|
||||
pub use crate::hook::{Debug, DebugEvent, DebugNames, DebugSource, DebugStack};
|
||||
pub use crate::multi::Variadic;
|
||||
pub use crate::multi::{MultiValue, Variadic};
|
||||
pub use crate::scope::Scope;
|
||||
pub use crate::state::{GCMode, Lua, LuaOptions};
|
||||
// pub use crate::scope::Scope;
|
||||
pub use crate::stdlib::StdLib;
|
||||
pub use crate::string::{BorrowedBytes, BorrowedStr, String};
|
||||
pub use crate::table::{Table, TablePairs, TableSequence};
|
||||
pub use crate::thread::{Thread, ThreadStatus};
|
||||
pub use crate::traits::ObjectLike;
|
||||
pub use crate::types::{AppDataRef, AppDataRefMut, Integer, LightUserData, MaybeSend, Number, RegistryKey};
|
||||
pub use crate::traits::{
|
||||
FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, LuaNativeFn, LuaNativeFnMut, ObjectLike,
|
||||
};
|
||||
pub use crate::types::{
|
||||
AppDataRef, AppDataRefMut, Either, Integer, LightUserData, MaybeSend, Number, RegistryKey, VmState,
|
||||
};
|
||||
pub use crate::userdata::{
|
||||
AnyUserData, MetaMethod, UserData, UserDataFields, UserDataMetatable, UserDataMethods, UserDataRef,
|
||||
UserDataRefMut, UserDataRegistry,
|
||||
};
|
||||
pub use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, MultiValue, Nil, Value};
|
||||
pub use crate::value::{Nil, Value};
|
||||
|
||||
#[cfg(not(feature = "luau"))]
|
||||
pub use crate::hook::HookTriggers;
|
||||
|
||||
#[cfg(any(feature = "luau", doc))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
|
||||
pub use crate::{
|
||||
chunk::Compiler,
|
||||
function::CoverageInfo,
|
||||
types::{Vector, VmState},
|
||||
};
|
||||
pub use crate::{buffer::Buffer, chunk::Compiler, function::CoverageInfo, vector::Vector};
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
pub use crate::thread::AsyncThread;
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
|
||||
pub use crate::{thread::AsyncThread, traits::LuaNativeAsyncFn};
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
#[doc(inline)]
|
||||
@@ -198,10 +192,6 @@ extern crate mlua_derive;
|
||||
/// - The `//` (floor division) operator is unusable, as its start a comment.
|
||||
///
|
||||
/// Everything else should work.
|
||||
///
|
||||
/// [`AsChunk`]: crate::AsChunk
|
||||
/// [`UserData`]: crate::UserData
|
||||
/// [`IntoLua`]: crate::IntoLua
|
||||
#[cfg(feature = "macros")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
|
||||
pub use mlua_derive::chunk;
|
||||
|
||||
+4
-3
@@ -9,7 +9,8 @@ use crate::chunk::ChunkMode;
|
||||
use crate::error::Result;
|
||||
use crate::state::Lua;
|
||||
use crate::table::Table;
|
||||
use crate::value::{IntoLua, Value};
|
||||
use crate::traits::IntoLua;
|
||||
use crate::value::Value;
|
||||
|
||||
#[cfg(unix)]
|
||||
use {libloading::Library, rustc_hash::FxHashMap};
|
||||
@@ -19,7 +20,7 @@ use {libloading::Library, rustc_hash::FxHashMap};
|
||||
//
|
||||
|
||||
#[cfg(unix)]
|
||||
const TARGET_MLUA_LUAU_ABI_VERSION: u32 = 1;
|
||||
const TARGET_MLUA_LUAU_ABI_VERSION: u32 = 2;
|
||||
|
||||
#[cfg(all(unix, feature = "module"))]
|
||||
#[no_mangle]
|
||||
@@ -202,7 +203,7 @@ fn lua_loader(lua: &Lua, modname: StdString) -> Result<Value> {
|
||||
match fs::read(&file_path) {
|
||||
Ok(buf) => {
|
||||
return lua
|
||||
.load(&buf)
|
||||
.load(buf)
|
||||
.set_name(format!("={}", file_path.display()))
|
||||
.set_mode(ChunkMode::Text)
|
||||
.into_function()
|
||||
|
||||
+90
-9
@@ -1,14 +1,17 @@
|
||||
use std::collections::{vec_deque, VecDeque};
|
||||
use std::iter::FromIterator;
|
||||
use std::mem;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::os::raw::c_int;
|
||||
use std::result::Result as StdResult;
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::state::{Lua, RawLua};
|
||||
use crate::traits::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti};
|
||||
use crate::util::check_stack;
|
||||
use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, MultiValue, Nil};
|
||||
use crate::value::{Nil, Value};
|
||||
|
||||
/// Result is convertible to `MultiValue` following the common Lua idiom of returning the result
|
||||
/// Result is convertible to [`MultiValue`] following the common Lua idiom of returning the result
|
||||
/// on success, or in the case of an error, returning `nil` and an error message.
|
||||
impl<T: IntoLua, E: IntoLua> IntoLuaMulti for StdResult<T, E> {
|
||||
#[inline]
|
||||
@@ -89,6 +92,80 @@ impl<T: FromLua> FromLuaMulti for T {
|
||||
}
|
||||
}
|
||||
|
||||
/// Multiple Lua values used for both argument passing and also for multiple return values.
|
||||
#[derive(Default, Debug, Clone)]
|
||||
pub struct MultiValue(VecDeque<Value>);
|
||||
|
||||
impl Deref for MultiValue {
|
||||
type Target = VecDeque<Value>;
|
||||
|
||||
#[inline]
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for MultiValue {
|
||||
#[inline]
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl MultiValue {
|
||||
/// Creates an empty `MultiValue` containing no values.
|
||||
#[inline]
|
||||
pub const fn new() -> MultiValue {
|
||||
MultiValue(VecDeque::new())
|
||||
}
|
||||
|
||||
/// Creates an empty `MultiValue` container with space for at least `capacity` elements.
|
||||
pub fn with_capacity(capacity: usize) -> MultiValue {
|
||||
MultiValue(VecDeque::with_capacity(capacity))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn from_lua_iter<T: IntoLua>(lua: &Lua, iter: impl IntoIterator<Item = T>) -> Result<Self> {
|
||||
let iter = iter.into_iter();
|
||||
let mut multi_value = MultiValue::with_capacity(iter.size_hint().0);
|
||||
for value in iter {
|
||||
multi_value.push_back(value.into_lua(lua)?);
|
||||
}
|
||||
Ok(multi_value)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromIterator<Value> for MultiValue {
|
||||
#[inline]
|
||||
fn from_iter<I: IntoIterator<Item = Value>>(iter: I) -> Self {
|
||||
let mut multi_value = MultiValue::new();
|
||||
multi_value.extend(iter);
|
||||
multi_value
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoIterator for MultiValue {
|
||||
type Item = Value;
|
||||
type IntoIter = vec_deque::IntoIter<Value>;
|
||||
|
||||
#[inline]
|
||||
fn into_iter(mut self) -> Self::IntoIter {
|
||||
let deque = mem::take(&mut self.0);
|
||||
mem::forget(self);
|
||||
deque.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoIterator for &'a MultiValue {
|
||||
type Item = &'a Value;
|
||||
type IntoIter = vec_deque::Iter<'a, Value>;
|
||||
|
||||
#[inline]
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.0.iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoLuaMulti for MultiValue {
|
||||
#[inline]
|
||||
fn into_lua_multi(self, _: &Lua) -> Result<MultiValue> {
|
||||
@@ -126,9 +203,6 @@ impl FromLuaMulti for MultiValue {
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// [`FromLua`]: crate::FromLua
|
||||
/// [`MultiValue`]: crate::MultiValue
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Variadic<T>(Vec<T>);
|
||||
|
||||
@@ -213,10 +287,7 @@ macro_rules! impl_tuple {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn from_stack_multi(nvals: c_int, lua: &RawLua) -> Result<Self> {
|
||||
if nvals > 0 {
|
||||
ffi::lua_pop(lua.state(), nvals);
|
||||
}
|
||||
unsafe fn from_stack_multi(_nvals: c_int, _lua: &RawLua) -> Result<Self> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -342,3 +413,13 @@ impl_tuple!(A B C D E F G H I J K L M);
|
||||
impl_tuple!(A B C D E F G H I J K L M N);
|
||||
impl_tuple!(A B C D E F G H I J K L M N O);
|
||||
impl_tuple!(A B C D E F G H I J K L M N O P);
|
||||
|
||||
#[cfg(test)]
|
||||
mod assertions {
|
||||
use super::*;
|
||||
|
||||
#[cfg(not(feature = "send"))]
|
||||
static_assertions::assert_not_impl_any!(MultiValue: Send);
|
||||
#[cfg(feature = "send")]
|
||||
static_assertions::assert_impl_all!(MultiValue: Send, Sync);
|
||||
}
|
||||
|
||||
+14
-13
@@ -2,17 +2,18 @@
|
||||
|
||||
#[doc(no_inline)]
|
||||
pub use crate::{
|
||||
AnyUserData as LuaAnyUserData, Chunk as LuaChunk, Error as LuaError, ErrorContext as LuaErrorContext,
|
||||
ExternalError as LuaExternalError, ExternalResult as LuaExternalResult, FromLua, FromLuaMulti,
|
||||
Function as LuaFunction, FunctionInfo as LuaFunctionInfo, GCMode as LuaGCMode, Integer as LuaInteger,
|
||||
IntoLua, IntoLuaMulti, LightUserData as LuaLightUserData, Lua, LuaOptions, MetaMethod as LuaMetaMethod,
|
||||
MultiValue as LuaMultiValue, Nil as LuaNil, Number as LuaNumber, ObjectLike as LuaObjectLike,
|
||||
RegistryKey as LuaRegistryKey, Result as LuaResult, StdLib as LuaStdLib, String as LuaString,
|
||||
Table as LuaTable, TablePairs as LuaTablePairs, TableSequence as LuaTableSequence, Thread as LuaThread,
|
||||
ThreadStatus as LuaThreadStatus, UserData as LuaUserData, UserDataFields as LuaUserDataFields,
|
||||
UserDataMetatable as LuaUserDataMetatable, UserDataMethods as LuaUserDataMethods,
|
||||
UserDataRef as LuaUserDataRef, UserDataRefMut as LuaUserDataRefMut,
|
||||
UserDataRegistry as LuaUserDataRegistry, Value as LuaValue,
|
||||
AnyUserData as LuaAnyUserData, Chunk as LuaChunk, Either as LuaEither, Error as LuaError,
|
||||
ErrorContext as LuaErrorContext, ExternalError as LuaExternalError, ExternalResult as LuaExternalResult,
|
||||
FromLua, FromLuaMulti, Function as LuaFunction, FunctionInfo as LuaFunctionInfo, GCMode as LuaGCMode,
|
||||
Integer as LuaInteger, IntoLua, IntoLuaMulti, LightUserData as LuaLightUserData, Lua, LuaNativeFn,
|
||||
LuaNativeFnMut, LuaOptions, MetaMethod as LuaMetaMethod, MultiValue as LuaMultiValue, Nil as LuaNil,
|
||||
Number as LuaNumber, ObjectLike as LuaObjectLike, RegistryKey as LuaRegistryKey, Result as LuaResult,
|
||||
StdLib as LuaStdLib, String as LuaString, Table as LuaTable, TablePairs as LuaTablePairs,
|
||||
TableSequence as LuaTableSequence, Thread as LuaThread, ThreadStatus as LuaThreadStatus,
|
||||
UserData as LuaUserData, UserDataFields as LuaUserDataFields, UserDataMetatable as LuaUserDataMetatable,
|
||||
UserDataMethods as LuaUserDataMethods, UserDataRef as LuaUserDataRef,
|
||||
UserDataRefMut as LuaUserDataRefMut, UserDataRegistry as LuaUserDataRegistry, Value as LuaValue,
|
||||
VmState as LuaVmState,
|
||||
};
|
||||
|
||||
#[cfg(not(feature = "luau"))]
|
||||
@@ -21,11 +22,11 @@ pub use crate::HookTriggers as LuaHookTriggers;
|
||||
|
||||
#[cfg(feature = "luau")]
|
||||
#[doc(no_inline)]
|
||||
pub use crate::{CoverageInfo as LuaCoverageInfo, Vector as LuaVector, VmState as LuaVmState};
|
||||
pub use crate::{CoverageInfo as LuaCoverageInfo, Vector as LuaVector};
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
#[doc(no_inline)]
|
||||
pub use crate::AsyncThread as LuaAsyncThread;
|
||||
pub use crate::{AsyncThread as LuaAsyncThread, LuaNativeAsyncFn};
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
#[doc(no_inline)]
|
||||
|
||||
+119
-796
File diff suppressed because it is too large
Load Diff
+11
-15
@@ -1,3 +1,5 @@
|
||||
//! Deserialize Lua values to a Rust data structure.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::os::raw::c_void;
|
||||
use std::rc::Rc;
|
||||
@@ -94,12 +96,12 @@ impl Options {
|
||||
}
|
||||
|
||||
impl Deserializer {
|
||||
/// Creates a new Lua Deserializer for the `Value`.
|
||||
/// Creates a new Lua Deserializer for the [`Value`].
|
||||
pub fn new(value: Value) -> Self {
|
||||
Self::new_with_options(value, Options::default())
|
||||
}
|
||||
|
||||
/// Creates a new Lua Deserializer for the `Value` with custom options.
|
||||
/// Creates a new Lua Deserializer for the [`Value`] with custom options.
|
||||
pub fn new_with_options(value: Value, options: Options) -> Self {
|
||||
Deserializer {
|
||||
value,
|
||||
@@ -145,19 +147,13 @@ impl<'de> serde::Deserializer<'de> for Deserializer {
|
||||
serde_userdata(ud, |value| value.deserialize_any(visitor))
|
||||
}
|
||||
#[cfg(feature = "luau")]
|
||||
Value::UserData(ud) if ud.1 == crate::types::SubtypeId::Buffer => unsafe {
|
||||
let lua = ud.0.lua.lock();
|
||||
let mut size = 0usize;
|
||||
let buf = ffi::lua_tobuffer(lua.ref_thread(), ud.0.index, &mut size);
|
||||
mlua_assert!(!buf.is_null(), "invalid Luau buffer");
|
||||
let buf = std::slice::from_raw_parts(buf as *const u8, size);
|
||||
visitor.visit_bytes(buf)
|
||||
},
|
||||
Value::Buffer(buf) => visitor.visit_bytes(unsafe { buf.as_slice() }),
|
||||
Value::Function(_)
|
||||
| Value::Thread(_)
|
||||
| Value::UserData(_)
|
||||
| Value::LightUserData(_)
|
||||
| Value::Error(_) => {
|
||||
| Value::Error(_)
|
||||
| Value::Other(_) => {
|
||||
if self.options.deny_unsupported_types {
|
||||
let msg = format!("unsupported value type `{}`", self.value.type_name());
|
||||
Err(de::Error::custom(msg))
|
||||
@@ -424,7 +420,7 @@ impl<'de> de::SeqAccess<'de> for SeqDeserializer<'_> {
|
||||
|
||||
#[cfg(feature = "luau")]
|
||||
struct VecDeserializer {
|
||||
vec: crate::types::Vector,
|
||||
vec: crate::Vector,
|
||||
next: usize,
|
||||
options: Options,
|
||||
visited: Rc<RefCell<FxHashSet<*const c_void>>>,
|
||||
@@ -450,7 +446,7 @@ impl<'de> de::SeqAccess<'de> for VecDeserializer {
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> Option<usize> {
|
||||
Some(crate::types::Vector::SIZE)
|
||||
Some(crate::Vector::SIZE)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -463,7 +459,7 @@ impl<'a> MapPairs<'a> {
|
||||
pub(crate) fn new(t: &'a Table, sort_keys: bool) -> Result<Self> {
|
||||
if sort_keys {
|
||||
let mut pairs = t.pairs::<Value, Value>().collect::<Result<Vec<_>>>()?;
|
||||
pairs.sort_by(|(a, _), (b, _)| b.cmp(a)); // reverse order as we pop values from the end
|
||||
pairs.sort_by(|(a, _), (b, _)| b.sort_cmp(a)); // reverse order as we pop values from the end
|
||||
Ok(MapPairs::Vec(pairs))
|
||||
} else {
|
||||
Ok(MapPairs::Iter(t.pairs::<Value, Value>()))
|
||||
@@ -504,7 +500,7 @@ struct MapDeserializer<'a> {
|
||||
processed: usize,
|
||||
}
|
||||
|
||||
impl<'a> MapDeserializer<'a> {
|
||||
impl MapDeserializer<'_> {
|
||||
fn next_key_deserializer(&mut self) -> Result<Option<Deserializer>> {
|
||||
loop {
|
||||
match self.pairs.next() {
|
||||
|
||||
@@ -106,8 +106,6 @@ pub trait LuaSerdeExt: Sealed {
|
||||
///
|
||||
/// Requires `feature = "serialize"`
|
||||
///
|
||||
/// [`Value`]: crate::Value
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
@@ -133,8 +131,6 @@ pub trait LuaSerdeExt: Sealed {
|
||||
///
|
||||
/// Requires `feature = "serialize"`
|
||||
///
|
||||
/// [`Value`]: crate::Value
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
@@ -164,8 +160,6 @@ pub trait LuaSerdeExt: Sealed {
|
||||
///
|
||||
/// Requires `feature = "serialize"`
|
||||
///
|
||||
/// [`Value`]: crate::Value
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
|
||||
+7
-4
@@ -1,10 +1,13 @@
|
||||
//! Serialize a Rust data structure into Lua value.
|
||||
|
||||
use serde::{ser, Serialize};
|
||||
|
||||
use super::LuaSerdeExt;
|
||||
use crate::error::{Error, Result};
|
||||
use crate::state::Lua;
|
||||
use crate::table::Table;
|
||||
use crate::value::{IntoLua, Value};
|
||||
use crate::traits::IntoLua;
|
||||
use crate::value::Value;
|
||||
|
||||
/// A struct for serializing Rust values into Lua values.
|
||||
#[derive(Debug)]
|
||||
@@ -266,7 +269,7 @@ impl<'a> ser::Serializer for Serializer<'a> {
|
||||
#[inline]
|
||||
fn serialize_tuple_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeTupleStruct> {
|
||||
#[cfg(feature = "luau")]
|
||||
if name == "Vector" && len == crate::types::Vector::SIZE {
|
||||
if name == "Vector" && len == crate::Vector::SIZE {
|
||||
return Ok(SerializeSeq::new_vector(self.lua, self.options));
|
||||
}
|
||||
_ = name;
|
||||
@@ -340,7 +343,7 @@ impl<'a> ser::Serializer for Serializer<'a> {
|
||||
pub struct SerializeSeq<'a> {
|
||||
lua: &'a Lua,
|
||||
#[cfg(feature = "luau")]
|
||||
vector: Option<crate::types::Vector>,
|
||||
vector: Option<crate::Vector>,
|
||||
table: Option<Table>,
|
||||
next: usize,
|
||||
options: Options,
|
||||
@@ -362,7 +365,7 @@ impl<'a> SerializeSeq<'a> {
|
||||
const fn new_vector(lua: &'a Lua, options: Options) -> Self {
|
||||
Self {
|
||||
lua,
|
||||
vector: Some(crate::types::Vector::zero()),
|
||||
vector: Some(crate::Vector::zero()),
|
||||
table: None,
|
||||
next: 0,
|
||||
options,
|
||||
|
||||
+235
-153
@@ -2,7 +2,7 @@ use std::any::TypeId;
|
||||
use std::cell::RefCell;
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::Deref;
|
||||
use std::os::raw::{c_int, c_void};
|
||||
use std::os::raw::c_int;
|
||||
use std::panic::Location;
|
||||
use std::result::Result as StdResult;
|
||||
use std::{fmt, mem, ptr};
|
||||
@@ -12,27 +12,34 @@ use crate::error::{Error, Result};
|
||||
use crate::function::Function;
|
||||
use crate::hook::Debug;
|
||||
use crate::memory::MemoryState;
|
||||
// use crate::scope::Scope;
|
||||
use crate::multi::MultiValue;
|
||||
use crate::scope::Scope;
|
||||
use crate::stdlib::StdLib;
|
||||
use crate::string::String;
|
||||
use crate::table::Table;
|
||||
use crate::thread::Thread;
|
||||
use crate::traits::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti};
|
||||
use crate::types::{
|
||||
AppDataRef, AppDataRefMut, ArcReentrantMutexGuard, Integer, LightUserData, MaybeSend, Number,
|
||||
ReentrantMutex, ReentrantMutexGuard, RegistryKey, XRc, XWeak,
|
||||
AppDataRef, AppDataRefMut, ArcReentrantMutexGuard, Integer, LuaType, MaybeSend, Number, ReentrantMutex,
|
||||
ReentrantMutexGuard, RegistryKey, VmState, XRc, XWeak,
|
||||
};
|
||||
use crate::userdata::{AnyUserData, UserData, UserDataProxy, UserDataRegistry, UserDataVariant};
|
||||
use crate::util::{assert_stack, check_stack, push_string, push_table, rawset_field, StackGuard};
|
||||
use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, MultiValue, Nil, Value};
|
||||
use crate::userdata::{AnyUserData, UserData, UserDataProxy, UserDataRegistry, UserDataStorage};
|
||||
use crate::util::{
|
||||
assert_stack, check_stack, protect_lua_closure, push_string, push_table, rawset_field, StackGuard,
|
||||
};
|
||||
use crate::value::{Nil, Value};
|
||||
|
||||
#[cfg(not(feature = "luau"))]
|
||||
use crate::hook::HookTriggers;
|
||||
|
||||
#[cfg(any(feature = "luau", doc))]
|
||||
use crate::{chunk::Compiler, types::VmState};
|
||||
use crate::{buffer::Buffer, chunk::Compiler};
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
use std::future::{self, Future};
|
||||
use {
|
||||
crate::types::LightUserData,
|
||||
std::future::{self, Future},
|
||||
};
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
use serde::Serialize;
|
||||
@@ -164,12 +171,10 @@ impl Lua {
|
||||
/// Creates a new Lua state and loads the **safe** subset of the standard libraries.
|
||||
///
|
||||
/// # Safety
|
||||
/// The created Lua state would have _some_ safety guarantees and would not allow to load unsafe
|
||||
/// The created Lua state will have _some_ safety guarantees and will not allow to load unsafe
|
||||
/// standard libraries or C modules.
|
||||
///
|
||||
/// See [`StdLib`] documentation for a list of unsafe modules that cannot be loaded.
|
||||
///
|
||||
/// [`StdLib`]: crate::StdLib
|
||||
pub fn new() -> Lua {
|
||||
mlua_expect!(
|
||||
Self::new_with(StdLib::ALL_SAFE, LuaOptions::default()),
|
||||
@@ -180,7 +185,7 @@ impl Lua {
|
||||
/// Creates a new Lua state and loads all the standard libraries.
|
||||
///
|
||||
/// # Safety
|
||||
/// The created Lua state would not have safety guarantees and would allow to load C modules.
|
||||
/// The created Lua state will not have safety guarantees and will allow to load C modules.
|
||||
pub unsafe fn unsafe_new() -> Lua {
|
||||
Self::unsafe_new_with(StdLib::ALL, LuaOptions::default())
|
||||
}
|
||||
@@ -190,12 +195,10 @@ impl Lua {
|
||||
/// Use the [`StdLib`] flags to specify the libraries you want to load.
|
||||
///
|
||||
/// # Safety
|
||||
/// The created Lua state would have _some_ safety guarantees and would not allow to load unsafe
|
||||
/// The created Lua state will have _some_ safety guarantees and will not allow to load unsafe
|
||||
/// standard libraries or C modules.
|
||||
///
|
||||
/// See [`StdLib`] documentation for a list of unsafe modules that cannot be loaded.
|
||||
///
|
||||
/// [`StdLib`]: crate::StdLib
|
||||
pub fn new_with(libs: StdLib, options: LuaOptions) -> Result<Lua> {
|
||||
#[cfg(not(feature = "luau"))]
|
||||
if libs.contains(StdLib::DEBUG) {
|
||||
@@ -215,7 +218,7 @@ impl Lua {
|
||||
if libs.contains(StdLib::PACKAGE) {
|
||||
mlua_expect!(lua.disable_c_modules(), "Error disabling C modules");
|
||||
}
|
||||
unsafe { lua.lock().set_safe() };
|
||||
lua.lock().mark_safe();
|
||||
|
||||
Ok(lua)
|
||||
}
|
||||
@@ -226,8 +229,6 @@ impl Lua {
|
||||
///
|
||||
/// # Safety
|
||||
/// The created Lua state will not have safety guarantees and allow to load C modules.
|
||||
///
|
||||
/// [`StdLib`]: crate::StdLib
|
||||
pub unsafe fn unsafe_new_with(libs: StdLib, options: LuaOptions) -> Lua {
|
||||
// Workaround to avoid stripping a few unused Lua symbols that could be imported
|
||||
// by C modules in unsafe mode
|
||||
@@ -276,7 +277,56 @@ impl Lua {
|
||||
}
|
||||
}
|
||||
|
||||
/// FIXME: Deprecated load_from_std_lib
|
||||
/// Calls provided function passing a raw lua state.
|
||||
///
|
||||
/// The arguments will be pushed onto the stack before calling the function.
|
||||
///
|
||||
/// This method ensures that the Lua instance is locked while the function is called
|
||||
/// and restores Lua stack after the function returns.
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// # use mlua::{Lua, Result};
|
||||
/// # fn main() -> Result<()> {
|
||||
/// let lua = Lua::new();
|
||||
/// let n: i32 = unsafe {
|
||||
/// let nums = (3, 4, 5);
|
||||
/// lua.exec_raw(nums, |state| {
|
||||
/// let n = ffi::lua_gettop(state);
|
||||
/// let mut sum = 0;
|
||||
/// for i in 1..=n {
|
||||
/// sum += ffi::lua_tointeger(state, i);
|
||||
/// }
|
||||
/// ffi::lua_pop(state, n);
|
||||
/// ffi::lua_pushinteger(state, sum);
|
||||
/// })
|
||||
/// }?;
|
||||
/// assert_eq!(n, 12);
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
#[allow(clippy::missing_safety_doc)]
|
||||
pub unsafe fn exec_raw<R: FromLuaMulti>(
|
||||
&self,
|
||||
args: impl IntoLuaMulti,
|
||||
f: impl FnOnce(*mut ffi::lua_State),
|
||||
) -> Result<R> {
|
||||
let lua = self.lock();
|
||||
let state = lua.state();
|
||||
let _sg = StackGuard::new(state);
|
||||
let stack_start = ffi::lua_gettop(state);
|
||||
let nargs = args.push_into_stack_multi(&lua)?;
|
||||
check_stack(state, 3)?;
|
||||
protect_lua_closure::<_, ()>(state, nargs, ffi::LUA_MULTRET, f)?;
|
||||
let nresults = ffi::lua_gettop(state) - stack_start;
|
||||
R::from_stack_multi(nresults, &lua)
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[deprecated(since = "0.10.0", note = "please use `load_std_libs` instead")]
|
||||
pub fn load_from_std_lib(&self, libs: StdLib) -> Result<()> {
|
||||
self.load_std_libs(libs)
|
||||
}
|
||||
|
||||
/// Loads the specified subset of the standard libraries into an existing Lua state.
|
||||
///
|
||||
@@ -365,7 +415,7 @@ impl Lua {
|
||||
// Make sure that Lua is initialized
|
||||
let mut lua = Self::init_from_ptr(state);
|
||||
lua.collect_garbage = false;
|
||||
// `Lua` is no longer needed and must be dropped at this point to avoid possible memory leak
|
||||
// `Lua` is no longer needed and must be dropped at this point to avoid memory leak
|
||||
// in case of possible longjmp (lua_error) below
|
||||
drop(lua);
|
||||
|
||||
@@ -410,6 +460,7 @@ impl Lua {
|
||||
///
|
||||
/// ```
|
||||
/// # use mlua::{Lua, Result};
|
||||
/// # #[cfg(feature = "luau")]
|
||||
/// # fn main() -> Result<()> {
|
||||
/// let lua = Lua::new();
|
||||
///
|
||||
@@ -422,10 +473,13 @@ impl Lua {
|
||||
/// assert_eq!(lua.globals().get::<Option<u32>>("var")?, None);
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
///
|
||||
/// # #[cfg(not(feature = "luau"))]
|
||||
/// # fn main() {}
|
||||
/// ```
|
||||
///
|
||||
/// Requires `feature = "luau"`
|
||||
#[cfg(any(feature = "luau", docsrs))]
|
||||
#[cfg(any(feature = "luau", doc))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
|
||||
pub fn sandbox(&self, enabled: bool) -> Result<()> {
|
||||
let lua = self.lock();
|
||||
@@ -450,7 +504,7 @@ impl Lua {
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets a 'hook' function that will periodically be called as Lua code executes.
|
||||
/// Sets a hook function that will periodically be called as Lua code executes.
|
||||
///
|
||||
/// When exactly the hook function is called depends on the contents of the `triggers`
|
||||
/// parameter, see [`HookTriggers`] for more details.
|
||||
@@ -462,7 +516,7 @@ impl Lua {
|
||||
///
|
||||
/// This method sets a hook function for the current thread of this Lua instance.
|
||||
/// If you want to set a hook function for another thread (coroutine), use
|
||||
/// [`Thread::set_hook()`] instead.
|
||||
/// [`Thread::set_hook`] instead.
|
||||
///
|
||||
/// Please note you cannot have more than one hook function set at a time for this Lua instance.
|
||||
///
|
||||
@@ -471,12 +525,12 @@ impl Lua {
|
||||
/// Shows each line number of code being executed by the Lua interpreter.
|
||||
///
|
||||
/// ```
|
||||
/// # use mlua::{Lua, HookTriggers, Result};
|
||||
/// # use mlua::{Lua, HookTriggers, Result, VmState};
|
||||
/// # fn main() -> Result<()> {
|
||||
/// let lua = Lua::new();
|
||||
/// lua.set_hook(HookTriggers::EVERY_LINE, |_lua, debug| {
|
||||
/// println!("line {}", debug.curr_line());
|
||||
/// Ok(())
|
||||
/// Ok(VmState::Continue)
|
||||
/// });
|
||||
///
|
||||
/// lua.load(r#"
|
||||
@@ -487,19 +541,18 @@ impl Lua {
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// [`HookTriggers`]: crate::HookTriggers
|
||||
/// [`HookTriggers.every_nth_instruction`]: crate::HookTriggers::every_nth_instruction
|
||||
#[cfg(not(feature = "luau"))]
|
||||
#[cfg_attr(docsrs, doc(cfg(not(feature = "luau"))))]
|
||||
pub fn set_hook<F>(&self, triggers: HookTriggers, callback: F)
|
||||
where
|
||||
F: Fn(&Lua, Debug) -> Result<()> + MaybeSend + 'static,
|
||||
F: Fn(&Lua, Debug) -> Result<VmState> + MaybeSend + 'static,
|
||||
{
|
||||
let lua = self.lock();
|
||||
unsafe { lua.set_thread_hook(lua.state(), triggers, callback) };
|
||||
}
|
||||
|
||||
/// Removes any hook previously set by [`Lua::set_hook()`] or [`Thread::set_hook()`].
|
||||
/// Removes any hook previously set by [`Lua::set_hook`] or [`Thread::set_hook`].
|
||||
///
|
||||
/// This function has no effect if a hook was not previously set.
|
||||
#[cfg(not(feature = "luau"))]
|
||||
@@ -521,7 +574,7 @@ impl Lua {
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets an 'interrupt' function that will periodically be called by Luau VM.
|
||||
/// Sets an interrupt function that will periodically be called by Luau VM.
|
||||
///
|
||||
/// Any Luau code is guaranteed to call this handler "eventually"
|
||||
/// (in practice this can happen at any function call or at any loop iteration).
|
||||
@@ -540,6 +593,7 @@ impl Lua {
|
||||
/// ```
|
||||
/// # use std::sync::{Arc, atomic::{AtomicU64, Ordering}};
|
||||
/// # use mlua::{Lua, Result, ThreadStatus, VmState};
|
||||
/// # #[cfg(feature = "luau")]
|
||||
/// # fn main() -> Result<()> {
|
||||
/// let lua = Lua::new();
|
||||
/// let count = Arc::new(AtomicU64::new(0));
|
||||
@@ -562,8 +616,11 @@ impl Lua {
|
||||
/// }
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
///
|
||||
/// # #[cfg(not(feature = "luau"))]
|
||||
/// # fn main() {}
|
||||
/// ```
|
||||
#[cfg(any(feature = "luau", docsrs))]
|
||||
#[cfg(any(feature = "luau", doc))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
|
||||
pub fn set_interrupt<F>(&self, callback: F)
|
||||
where
|
||||
@@ -601,10 +658,10 @@ impl Lua {
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes any 'interrupt' previously set by `set_interrupt`.
|
||||
/// Removes any interrupt function previously set by `set_interrupt`.
|
||||
///
|
||||
/// This function has no effect if an 'interrupt' was not previously set.
|
||||
#[cfg(any(feature = "luau", docsrs))]
|
||||
#[cfg(any(feature = "luau", doc))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
|
||||
pub fn remove_interrupt(&self) {
|
||||
let lua = self.lock();
|
||||
@@ -624,7 +681,7 @@ impl Lua {
|
||||
F: Fn(&Lua, &str, bool) -> Result<()> + MaybeSend + 'static,
|
||||
{
|
||||
use std::ffi::CStr;
|
||||
use std::os::raw::c_char;
|
||||
use std::os::raw::{c_char, c_void};
|
||||
use std::string::String as StdString;
|
||||
|
||||
unsafe extern "C-unwind" fn warn_proc(ud: *mut c_void, msg: *const c_char, tocont: c_int) {
|
||||
@@ -725,8 +782,8 @@ impl Lua {
|
||||
|
||||
/// Sets a memory limit (in bytes) on this Lua state.
|
||||
///
|
||||
/// Once an allocation occurs that would pass this memory limit,
|
||||
/// a `Error::MemoryError` is generated instead.
|
||||
/// Once an allocation occurs that would pass this memory limit, a `Error::MemoryError` is
|
||||
/// generated instead.
|
||||
/// Returns previous limit (zero means no limit).
|
||||
///
|
||||
/// Does not work in module mode where Lua state is managed externally.
|
||||
@@ -735,12 +792,12 @@ impl Lua {
|
||||
unsafe {
|
||||
match MemoryState::get(lua.main_state) {
|
||||
mem_state if !mem_state.is_null() => Ok((*mem_state).set_memory_limit(limit)),
|
||||
_ => Err(Error::MemoryLimitNotAvailable),
|
||||
_ => Err(Error::MemoryControlNotAvailable),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the garbage collector is currently running automatically.
|
||||
/// Returns `true` if the garbage collector is currently running automatically.
|
||||
///
|
||||
/// Requires `feature = "lua54/lua53/lua52/luau"`
|
||||
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52", feature = "luau"))]
|
||||
@@ -775,7 +832,7 @@ impl Lua {
|
||||
|
||||
/// Steps the garbage collector one indivisible step.
|
||||
///
|
||||
/// Returns true if this has finished a collection cycle.
|
||||
/// Returns `true` if this has finished a collection cycle.
|
||||
pub fn gc_step(&self) -> Result<bool> {
|
||||
self.gc_step_kbytes(0)
|
||||
}
|
||||
@@ -794,9 +851,9 @@ impl Lua {
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the 'pause' value of the collector.
|
||||
/// Sets the `pause` value of the collector.
|
||||
///
|
||||
/// Returns the previous value of 'pause'. More information can be found in the Lua
|
||||
/// Returns the previous value of `pause`. More information can be found in the Lua
|
||||
/// [documentation].
|
||||
///
|
||||
/// For Luau this parameter sets GC goal
|
||||
@@ -812,9 +869,9 @@ impl Lua {
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the 'step multiplier' value of the collector.
|
||||
/// Sets the `step multiplier` value of the collector.
|
||||
///
|
||||
/// Returns the previous value of the 'step multiplier'. More information can be found in the
|
||||
/// Returns the previous value of the `step multiplier`. More information can be found in the
|
||||
/// Lua [documentation].
|
||||
///
|
||||
/// [documentation]: https://www.lua.org/manual/5.4/manual.html#2.5
|
||||
@@ -955,9 +1012,10 @@ impl Lua {
|
||||
}
|
||||
}
|
||||
|
||||
/// Create and return an interned Lua string. Lua strings can be arbitrary `[u8]` data including
|
||||
/// embedded nulls, so in addition to `&str` and `&String`, you can also pass plain `&[u8]`
|
||||
/// here.
|
||||
/// Create and return an interned Lua string.
|
||||
///
|
||||
/// Lua strings can be arbitrary `[u8]` data including embedded nulls, so in addition to `&str`
|
||||
/// and `&String`, you can also pass plain `&[u8]` here.
|
||||
#[inline]
|
||||
pub fn create_string(&self, s: impl AsRef<[u8]>) -> Result<String> {
|
||||
unsafe { self.lock().create_string(s) }
|
||||
@@ -968,44 +1026,45 @@ impl Lua {
|
||||
/// Requires `feature = "luau"`
|
||||
///
|
||||
/// [buffer]: https://luau-lang.org/library#buffer-library
|
||||
#[cfg(feature = "luau")]
|
||||
pub fn create_buffer(&self, buf: impl AsRef<[u8]>) -> Result<AnyUserData> {
|
||||
use crate::types::SubtypeId;
|
||||
|
||||
#[cfg(any(feature = "luau", doc))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
|
||||
pub fn create_buffer(&self, buf: impl AsRef<[u8]>) -> Result<Buffer> {
|
||||
let lua = self.lock();
|
||||
let state = lua.state();
|
||||
unsafe {
|
||||
if lua.unlikely_memory_error() {
|
||||
crate::util::push_buffer(lua.ref_thread(), buf.as_ref(), false)?;
|
||||
return Ok(AnyUserData(lua.pop_ref_thread(), SubtypeId::Buffer));
|
||||
return Ok(Buffer(lua.pop_ref_thread()));
|
||||
}
|
||||
|
||||
let _sg = StackGuard::new(state);
|
||||
check_stack(state, 4)?;
|
||||
check_stack(state, 3)?;
|
||||
crate::util::push_buffer(state, buf.as_ref(), true)?;
|
||||
Ok(AnyUserData(lua.pop_ref(), SubtypeId::Buffer))
|
||||
Ok(Buffer(lua.pop_ref()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates and returns a new empty table.
|
||||
#[inline]
|
||||
pub fn create_table(&self) -> Result<Table> {
|
||||
self.create_table_with_capacity(0, 0)
|
||||
}
|
||||
|
||||
/// Creates and returns a new empty table, with the specified capacity.
|
||||
/// `narr` is a hint for how many elements the table will have as a sequence;
|
||||
/// `nrec` is a hint for how many other elements the table will have.
|
||||
///
|
||||
/// - `narr` is a hint for how many elements the table will have as a sequence.
|
||||
/// - `nrec` is a hint for how many other elements the table will have.
|
||||
///
|
||||
/// Lua may use these hints to preallocate memory for the new table.
|
||||
pub fn create_table_with_capacity(&self, narr: usize, nrec: usize) -> Result<Table> {
|
||||
unsafe { self.lock().create_table_with_capacity(narr, nrec) }
|
||||
}
|
||||
|
||||
/// Creates a table and fills it with values from an iterator.
|
||||
pub fn create_table_from<K, V, I>(&self, iter: I) -> Result<Table>
|
||||
pub fn create_table_from<K, V>(&self, iter: impl IntoIterator<Item = (K, V)>) -> Result<Table>
|
||||
where
|
||||
K: IntoLua,
|
||||
V: IntoLua,
|
||||
I: IntoIterator<Item = (K, V)>,
|
||||
{
|
||||
let lua = self.lock();
|
||||
let state = lua.state();
|
||||
@@ -1032,10 +1091,9 @@ impl Lua {
|
||||
}
|
||||
|
||||
/// Creates a table from an iterator of values, using `1..` as the keys.
|
||||
pub fn create_sequence_from<T, I>(&self, iter: I) -> Result<Table>
|
||||
pub fn create_sequence_from<T>(&self, iter: impl IntoIterator<Item = T>) -> Result<Table>
|
||||
where
|
||||
T: IntoLua,
|
||||
I: IntoIterator<Item = T>,
|
||||
{
|
||||
unsafe { self.lock().create_sequence_from(iter) }
|
||||
}
|
||||
@@ -1082,9 +1140,6 @@ impl Lua {
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// [`IntoLua`]: crate::IntoLua
|
||||
/// [`IntoLuaMulti`]: crate::IntoLuaMulti
|
||||
pub fn create_function<F, A, R>(&self, func: F) -> Result<Function>
|
||||
where
|
||||
F: Fn(&Lua, A) -> Result<R> + MaybeSend + 'static,
|
||||
@@ -1099,10 +1154,7 @@ impl Lua {
|
||||
|
||||
/// Wraps a Rust mutable closure, creating a callable Lua function handle to it.
|
||||
///
|
||||
/// This is a version of [`create_function`] that accepts a FnMut argument. Refer to
|
||||
/// [`create_function`] for more information about the implementation.
|
||||
///
|
||||
/// [`create_function`]: #method.create_function
|
||||
/// This is a version of [`Lua::create_function`] that accepts a `FnMut` argument.
|
||||
pub fn create_function_mut<F, A, R>(&self, func: F) -> Result<Function>
|
||||
where
|
||||
F: FnMut(&Lua, A) -> Result<R> + MaybeSend + 'static,
|
||||
@@ -1131,9 +1183,9 @@ impl Lua {
|
||||
/// call `yield()` passing internal representation of a `Poll::Pending` value.
|
||||
///
|
||||
/// The function must be called inside Lua coroutine ([`Thread`]) to be able to suspend its
|
||||
/// execution. An executor should be used to poll [`AsyncThread`] and mlua will take a
|
||||
/// provided Waker in that case. Otherwise noop waker will be used if try to call the
|
||||
/// function outside of Rust executors.
|
||||
/// execution. An executor should be used to poll [`AsyncThread`] and mlua will take a provided
|
||||
/// Waker in that case. Otherwise noop waker will be used if try to call the function outside of
|
||||
/// Rust executors.
|
||||
///
|
||||
/// The family of `call_async()` functions takes care about creating [`Thread`].
|
||||
///
|
||||
@@ -1162,7 +1214,6 @@ impl Lua {
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// [`Thread`]: crate::Thread
|
||||
/// [`AsyncThread`]: crate::AsyncThread
|
||||
#[cfg(feature = "async")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
|
||||
@@ -1201,7 +1252,7 @@ impl Lua {
|
||||
where
|
||||
T: UserData + MaybeSend + 'static,
|
||||
{
|
||||
unsafe { self.lock().make_userdata(UserDataVariant::new(data)) }
|
||||
unsafe { self.lock().make_userdata(UserDataStorage::new(data)) }
|
||||
}
|
||||
|
||||
/// Creates a Lua userdata object from a custom serializable userdata type.
|
||||
@@ -1214,12 +1265,12 @@ impl Lua {
|
||||
where
|
||||
T: UserData + Serialize + MaybeSend + 'static,
|
||||
{
|
||||
unsafe { self.lock().make_userdata(UserDataVariant::new_ser(data)) }
|
||||
unsafe { self.lock().make_userdata(UserDataStorage::new_ser(data)) }
|
||||
}
|
||||
|
||||
/// Creates a Lua userdata object from a custom Rust type.
|
||||
///
|
||||
/// You can register the type using [`Lua::register_userdata_type()`] to add fields or methods
|
||||
/// You can register the type using [`Lua::register_userdata_type`] to add fields or methods
|
||||
/// _before_ calling this method.
|
||||
/// Otherwise, the userdata object will have an empty metatable.
|
||||
///
|
||||
@@ -1229,12 +1280,12 @@ impl Lua {
|
||||
where
|
||||
T: MaybeSend + 'static,
|
||||
{
|
||||
unsafe { self.lock().make_any_userdata(UserDataVariant::new(data)) }
|
||||
unsafe { self.lock().make_any_userdata(UserDataStorage::new(data)) }
|
||||
}
|
||||
|
||||
/// Creates a Lua userdata object from a custom serializable Rust type.
|
||||
///
|
||||
/// See [`Lua::create_any_userdata()`] for more details.
|
||||
/// See [`Lua::create_any_userdata`] for more details.
|
||||
///
|
||||
/// Requires `feature = "serialize"`
|
||||
#[cfg(feature = "serialize")]
|
||||
@@ -1244,26 +1295,26 @@ impl Lua {
|
||||
where
|
||||
T: Serialize + MaybeSend + 'static,
|
||||
{
|
||||
unsafe { (self.lock()).make_any_userdata(UserDataVariant::new_ser(data)) }
|
||||
unsafe { (self.lock()).make_any_userdata(UserDataStorage::new_ser(data)) }
|
||||
}
|
||||
|
||||
/// Registers a custom Rust type in Lua to use in userdata objects.
|
||||
///
|
||||
/// This methods provides a way to add fields or methods to userdata objects of a type `T`.
|
||||
pub fn register_userdata_type<T: 'static>(&self, f: impl FnOnce(&mut UserDataRegistry<T>)) -> Result<()> {
|
||||
let mut registry = const { UserDataRegistry::new() };
|
||||
let type_id = TypeId::of::<T>();
|
||||
let mut registry = UserDataRegistry::new(type_id);
|
||||
f(&mut registry);
|
||||
|
||||
let lua = self.lock();
|
||||
unsafe {
|
||||
// Deregister the type if it already registered
|
||||
let type_id = TypeId::of::<T>();
|
||||
if let Some(&table_id) = (*lua.extra.get()).registered_userdata.get(&type_id) {
|
||||
if let Some(&table_id) = (*lua.extra.get()).registered_userdata_t.get(&type_id) {
|
||||
ffi::luaL_unref(lua.state(), ffi::LUA_REGISTRYINDEX, table_id);
|
||||
}
|
||||
|
||||
// Register the type
|
||||
lua.register_userdata_metatable(registry)?;
|
||||
lua.create_userdata_metatable(registry)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -1306,27 +1357,73 @@ impl Lua {
|
||||
T: UserData + 'static,
|
||||
{
|
||||
let ud = UserDataProxy::<T>(PhantomData);
|
||||
unsafe { self.lock().make_userdata(UserDataVariant::new(ud)) }
|
||||
unsafe { self.lock().make_userdata(UserDataStorage::new(ud)) }
|
||||
}
|
||||
|
||||
/// Sets the metatable for a Luau builtin vector type.
|
||||
#[cfg(any(feature = "luau", doc))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
|
||||
pub fn set_vector_metatable(&self, metatable: Option<Table>) {
|
||||
/// Sets the metatable for a Lua builtin type.
|
||||
///
|
||||
/// The metatable will be shared by all values of the given type.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Change metatable for Lua boolean type:
|
||||
///
|
||||
/// ```
|
||||
/// # use mlua::{Lua, Result, Function};
|
||||
/// # fn main() -> Result<()> {
|
||||
/// # let lua = Lua::new();
|
||||
/// let mt = lua.create_table()?;
|
||||
/// mt.set("__tostring", lua.create_function(|_, b: bool| Ok(if b { "2" } else { "0" }))?)?;
|
||||
/// lua.set_type_metatable::<bool>(Some(mt));
|
||||
/// lua.load("assert(tostring(true) == '2')").exec()?;
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
#[allow(private_bounds)]
|
||||
pub fn set_type_metatable<T: LuaType>(&self, metatable: Option<Table>) {
|
||||
let lua = self.lock();
|
||||
let state = lua.state();
|
||||
unsafe {
|
||||
let _sg = StackGuard::new(state);
|
||||
assert_stack(state, 2);
|
||||
|
||||
#[cfg(not(feature = "luau-vector4"))]
|
||||
ffi::lua_pushvector(state, 0., 0., 0.);
|
||||
#[cfg(feature = "luau-vector4")]
|
||||
ffi::lua_pushvector(state, 0., 0., 0., 0.);
|
||||
match T::TYPE_ID {
|
||||
ffi::LUA_TBOOLEAN => {
|
||||
ffi::lua_pushboolean(state, 0);
|
||||
}
|
||||
ffi::LUA_TLIGHTUSERDATA => {
|
||||
ffi::lua_pushlightuserdata(state, ptr::null_mut());
|
||||
}
|
||||
ffi::LUA_TNUMBER => {
|
||||
ffi::lua_pushnumber(state, 0.);
|
||||
}
|
||||
#[cfg(feature = "luau")]
|
||||
ffi::LUA_TVECTOR => {
|
||||
#[cfg(not(feature = "luau-vector4"))]
|
||||
ffi::lua_pushvector(state, 0., 0., 0.);
|
||||
#[cfg(feature = "luau-vector4")]
|
||||
ffi::lua_pushvector(state, 0., 0., 0., 0.);
|
||||
}
|
||||
ffi::LUA_TSTRING => {
|
||||
ffi::lua_pushstring(state, b"\0" as *const u8 as *const _);
|
||||
}
|
||||
ffi::LUA_TFUNCTION => match self.load("function() end").eval::<Function>() {
|
||||
Ok(func) => lua.push_ref(&func.0),
|
||||
Err(_) => return,
|
||||
},
|
||||
ffi::LUA_TTHREAD => {
|
||||
ffi::lua_newthread(state);
|
||||
}
|
||||
#[cfg(feature = "luau")]
|
||||
ffi::LUA_TBUFFER => {
|
||||
ffi::lua_newbuffer(state, 0);
|
||||
}
|
||||
_ => return,
|
||||
}
|
||||
match metatable {
|
||||
Some(metatable) => lua.push_ref(&metatable.0),
|
||||
None => ffi::lua_pushnil(state),
|
||||
};
|
||||
}
|
||||
ffi::lua_setmetatable(state, -2);
|
||||
}
|
||||
}
|
||||
@@ -1346,9 +1443,10 @@ impl Lua {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a handle to the active `Thread`. For calls to `Lua` this will be the main Lua
|
||||
/// thread, for parameters given to a callback, this will be whatever Lua thread called the
|
||||
/// callback.
|
||||
/// Returns a handle to the active `Thread`.
|
||||
///
|
||||
/// For calls to `Lua` this will be the main Lua thread, for parameters given to a callback,
|
||||
/// this will be whatever Lua thread called the callback.
|
||||
pub fn current_thread(&self) -> Thread {
|
||||
let lua = self.lock();
|
||||
let state = lua.state();
|
||||
@@ -1360,35 +1458,22 @@ impl Lua {
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls the given function with a `Scope` parameter, giving the function the ability to create
|
||||
/// userdata and callbacks from rust types that are !Send or non-'static.
|
||||
/// Calls the given function with a [`Scope`] parameter, giving the function the ability to
|
||||
/// create userdata and callbacks from Rust types that are `!Send` or non-`'static`.
|
||||
///
|
||||
/// The lifetime of any function or userdata created through `Scope` lasts only until the
|
||||
/// The lifetime of any function or userdata created through [`Scope`] lasts only until the
|
||||
/// completion of this method call, on completion all such created values are automatically
|
||||
/// dropped and Lua references to them are invalidated. If a script accesses a value created
|
||||
/// through `Scope` outside of this method, a Lua error will result. Since we can ensure the
|
||||
/// lifetime of values created through `Scope`, and we know that `Lua` cannot be sent to another
|
||||
/// thread while `Scope` is live, it is safe to allow !Send datatypes and whose lifetimes only
|
||||
/// outlive the scope lifetime.
|
||||
///
|
||||
/// Inside the scope callback, all handles created through Scope will share the same unique 'lua
|
||||
/// lifetime of the parent `Lua`. This allows scoped and non-scoped values to be mixed in
|
||||
/// API calls, which is very useful (e.g. passing a scoped userdata to a non-scoped function).
|
||||
/// However, this also enables handles to scoped values to be trivially leaked from the given
|
||||
/// callback. This is not dangerous, though! After the callback returns, all scoped values are
|
||||
/// invalidated, which means that though references may exist, the Rust types backing them have
|
||||
/// dropped. `Function` types will error when called, and `AnyUserData` will be typeless. It
|
||||
/// would be impossible to prevent handles to scoped values from escaping anyway, since you
|
||||
/// would always be able to smuggle them through Lua state.
|
||||
// pub fn scope<'lua, 'scope, R>(
|
||||
// &'lua self,
|
||||
// f: impl FnOnce(&Scope<'lua, 'scope>) -> Result<R>,
|
||||
// ) -> Result<R>
|
||||
// where
|
||||
// 'lua: 'scope,
|
||||
// {
|
||||
// f(&Scope::new(self))
|
||||
// }
|
||||
/// through [`Scope`] outside of this method, a Lua error will result. Since we can ensure the
|
||||
/// lifetime of values created through [`Scope`], and we know that [`Lua`] cannot be sent to
|
||||
/// another thread while [`Scope`] is live, it is safe to allow `!Send` data types and whose
|
||||
/// lifetimes only outlive the scope lifetime.
|
||||
pub fn scope<'env, R>(
|
||||
&self,
|
||||
f: impl for<'scope> FnOnce(&'scope mut Scope<'scope, 'env>) -> Result<R>,
|
||||
) -> Result<R> {
|
||||
f(&mut Scope::new(self.lock_arc()))
|
||||
}
|
||||
|
||||
/// Attempts to coerce a Lua value into a String in a manner consistent with Lua's internal
|
||||
/// behavior.
|
||||
@@ -1474,35 +1559,41 @@ impl Lua {
|
||||
})
|
||||
}
|
||||
|
||||
/// Converts a value that implements `IntoLua` into a `Value` instance.
|
||||
/// Converts a value that implements [`IntoLua`] into a [`Value`] instance.
|
||||
#[inline]
|
||||
pub fn pack(&self, t: impl IntoLua) -> Result<Value> {
|
||||
t.into_lua(self)
|
||||
}
|
||||
|
||||
/// Converts a `Value` instance into a value that implements `FromLua`.
|
||||
/// Converts a [`Value`] instance into a value that implements [`FromLua`].
|
||||
#[inline]
|
||||
pub fn unpack<T: FromLua>(&self, value: Value) -> Result<T> {
|
||||
T::from_lua(value, self)
|
||||
}
|
||||
|
||||
/// Converts a value that implements `IntoLuaMulti` into a `MultiValue` instance.
|
||||
/// Converts a value that implements [`IntoLua`] into a [`FromLua`] variant.
|
||||
#[inline]
|
||||
pub fn convert<U: FromLua>(&self, value: impl IntoLua) -> Result<U> {
|
||||
U::from_lua(value.into_lua(self)?, self)
|
||||
}
|
||||
|
||||
/// Converts a value that implements [`IntoLuaMulti`] into a [`MultiValue`] instance.
|
||||
#[inline]
|
||||
pub fn pack_multi(&self, t: impl IntoLuaMulti) -> Result<MultiValue> {
|
||||
t.into_lua_multi(self)
|
||||
}
|
||||
|
||||
/// Converts a `MultiValue` instance into a value that implements `FromLuaMulti`.
|
||||
/// Converts a [`MultiValue`] instance into a value that implements [`FromLuaMulti`].
|
||||
#[inline]
|
||||
pub fn unpack_multi<T: FromLuaMulti>(&self, value: MultiValue) -> Result<T> {
|
||||
T::from_lua_multi(value, self)
|
||||
}
|
||||
|
||||
/// Set a value in the Lua registry based on a string name.
|
||||
/// Set a value in the Lua registry based on a string key.
|
||||
///
|
||||
/// This value will be available to rust from all `Lua` instances which share the same main
|
||||
/// This value will be available to Rust from all Lua instances which share the same main
|
||||
/// state.
|
||||
pub fn set_named_registry_value(&self, name: &str, t: impl IntoLua) -> Result<()> {
|
||||
pub fn set_named_registry_value(&self, key: &str, t: impl IntoLua) -> Result<()> {
|
||||
let lua = self.lock();
|
||||
let state = lua.state();
|
||||
unsafe {
|
||||
@@ -1510,15 +1601,15 @@ impl Lua {
|
||||
check_stack(state, 5)?;
|
||||
|
||||
lua.push(t)?;
|
||||
rawset_field(state, ffi::LUA_REGISTRYINDEX, name)
|
||||
rawset_field(state, ffi::LUA_REGISTRYINDEX, key)
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a value from the Lua registry based on a string name.
|
||||
/// Get a value from the Lua registry based on a string key.
|
||||
///
|
||||
/// Any Lua instance which shares the underlying main state may call this method to
|
||||
/// get a value previously set by [`Lua::set_named_registry_value`].
|
||||
pub fn named_registry_value<T>(&self, name: &str) -> Result<T>
|
||||
pub fn named_registry_value<T>(&self, key: &str) -> Result<T>
|
||||
where
|
||||
T: FromLua,
|
||||
{
|
||||
@@ -1529,7 +1620,7 @@ impl Lua {
|
||||
check_stack(state, 3)?;
|
||||
|
||||
let protect = !lua.unlikely_memory_error();
|
||||
push_string(state, name.as_bytes(), protect)?;
|
||||
push_string(state, key.as_bytes(), protect)?;
|
||||
ffi::lua_rawget(state, ffi::LUA_REGISTRYINDEX);
|
||||
|
||||
T::from_stack(-1, &lua)
|
||||
@@ -1538,14 +1629,15 @@ impl Lua {
|
||||
|
||||
/// Removes a named value in the Lua registry.
|
||||
///
|
||||
/// Equivalent to calling [`Lua::set_named_registry_value`] with a value of Nil.
|
||||
pub fn unset_named_registry_value(&self, name: &str) -> Result<()> {
|
||||
self.set_named_registry_value(name, Nil)
|
||||
/// Equivalent to calling [`Lua::set_named_registry_value`] with a value of [`Nil`].
|
||||
#[inline]
|
||||
pub fn unset_named_registry_value(&self, key: &str) -> Result<()> {
|
||||
self.set_named_registry_value(key, Nil)
|
||||
}
|
||||
|
||||
/// Place a value in the Lua registry with an auto-generated key.
|
||||
///
|
||||
/// This value will be available to Rust from all `Lua` instances which share the same main
|
||||
/// This value will be available to Rust from all Lua instances which share the same main
|
||||
/// state.
|
||||
///
|
||||
/// Be warned, garbage collection of values held inside the registry is not automatic, see
|
||||
@@ -1587,7 +1679,7 @@ impl Lua {
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a value from the Lua registry by its `RegistryKey`
|
||||
/// Get a value from the Lua registry by its [`RegistryKey`]
|
||||
///
|
||||
/// Any Lua instance which shares the underlying main state may call this method to get a value
|
||||
/// previously placed by [`Lua::create_registry_value`].
|
||||
@@ -1622,9 +1714,7 @@ impl Lua {
|
||||
return Err(Error::MismatchedRegistryKey);
|
||||
}
|
||||
|
||||
unsafe {
|
||||
ffi::luaL_unref(lua.state(), ffi::LUA_REGISTRYINDEX, key.take());
|
||||
}
|
||||
unsafe { ffi::luaL_unref(lua.state(), ffi::LUA_REGISTRYINDEX, key.take()) };
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1670,8 +1760,8 @@ impl Lua {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns true if the given [`RegistryKey`] was created by a [`Lua`] which shares the
|
||||
/// underlying main state with this [`Lua`] instance.
|
||||
/// Returns true if the given [`RegistryKey`] was created by a Lua which shares the
|
||||
/// underlying main state with this Lua instance.
|
||||
///
|
||||
/// Other than this, methods that accept a [`RegistryKey`] will return
|
||||
/// [`Error::MismatchedRegistryKey`] if passed a [`RegistryKey`] that was not created with a
|
||||
@@ -1743,14 +1833,14 @@ impl Lua {
|
||||
/// - `Err(data)` if the data object of type `T` was not inserted because the container is
|
||||
/// currently borrowed.
|
||||
///
|
||||
/// See [`Lua::set_app_data()`] for examples.
|
||||
/// See [`Lua::set_app_data`] for examples.
|
||||
pub fn try_set_app_data<T: MaybeSend + 'static>(&self, data: T) -> StdResult<Option<T>, T> {
|
||||
let lua = self.lock();
|
||||
let extra = unsafe { &*lua.extra.get() };
|
||||
extra.app_data.try_insert(data)
|
||||
}
|
||||
|
||||
/// Gets a reference to an application data object stored by [`Lua::set_app_data()`] of type
|
||||
/// Gets a reference to an application data object stored by [`Lua::set_app_data`] of type
|
||||
/// `T`.
|
||||
///
|
||||
/// # Panics
|
||||
@@ -1764,7 +1854,7 @@ impl Lua {
|
||||
extra.app_data.borrow(Some(guard))
|
||||
}
|
||||
|
||||
/// Gets a mutable reference to an application data object stored by [`Lua::set_app_data()`] of
|
||||
/// Gets a mutable reference to an application data object stored by [`Lua::set_app_data`] of
|
||||
/// type `T`.
|
||||
///
|
||||
/// # Panics
|
||||
@@ -1789,22 +1879,13 @@ impl Lua {
|
||||
extra.app_data.remove()
|
||||
}
|
||||
|
||||
/// Pushes a value that implements `IntoLua` onto the Lua stack.
|
||||
///
|
||||
/// Uses 2 stack spaces, does not call checkstack.
|
||||
#[doc(hidden)]
|
||||
#[inline(always)]
|
||||
pub unsafe fn push(&self, value: impl IntoLua) -> Result<()> {
|
||||
self.lock().push(value)
|
||||
}
|
||||
|
||||
/// Returns an internal `Poll::Pending` constant used for executing async callbacks.
|
||||
#[cfg(feature = "async")]
|
||||
#[doc(hidden)]
|
||||
#[inline(always)]
|
||||
pub fn poll_pending() -> LightUserData {
|
||||
static ASYNC_POLL_PENDING: u8 = 0;
|
||||
LightUserData(&ASYNC_POLL_PENDING as *const u8 as *mut c_void)
|
||||
LightUserData(&ASYNC_POLL_PENDING as *const u8 as *mut std::os::raw::c_void)
|
||||
}
|
||||
|
||||
// Luau version located in `luau/mod.rs`
|
||||
@@ -1853,6 +1934,7 @@ impl Lua {
|
||||
/// Returns a handle to the unprotected Lua state without any synchronization.
|
||||
///
|
||||
/// This is useful where we know that the lock is already held by the caller.
|
||||
#[cfg(feature = "async")]
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn raw_lua(&self) -> &RawLua {
|
||||
&*self.raw.data_ptr()
|
||||
|
||||
+2
-2
@@ -35,7 +35,7 @@ pub(crate) struct ExtraData {
|
||||
pub(super) weak: MaybeUninit<WeakLua>,
|
||||
pub(super) owned: bool,
|
||||
|
||||
pub(super) registered_userdata: FxHashMap<TypeId, c_int>,
|
||||
pub(super) registered_userdata_t: FxHashMap<TypeId, c_int>,
|
||||
pub(super) registered_userdata_mt: FxHashMap<*const c_void, Option<TypeId>>,
|
||||
pub(super) last_checked_userdata_mt: (*const c_void, Option<TypeId>),
|
||||
|
||||
@@ -144,7 +144,7 @@ impl ExtraData {
|
||||
lua: MaybeUninit::uninit(),
|
||||
weak: MaybeUninit::uninit(),
|
||||
owned,
|
||||
registered_userdata: FxHashMap::default(),
|
||||
registered_userdata_t: FxHashMap::default(),
|
||||
registered_userdata_mt: FxHashMap::default(),
|
||||
last_checked_userdata_mt: (ptr::null(), None),
|
||||
registry_unref_list: Arc::new(Mutex::new(Some(Vec::new()))),
|
||||
|
||||
+128
-96
@@ -16,18 +16,19 @@ use crate::stdlib::StdLib;
|
||||
use crate::string::String;
|
||||
use crate::table::Table;
|
||||
use crate::thread::Thread;
|
||||
use crate::traits::IntoLua;
|
||||
use crate::types::{
|
||||
AppDataRef, AppDataRefMut, Callback, CallbackUpvalue, DestructedUserdata, Integer, LightUserData,
|
||||
MaybeSend, ReentrantMutex, RegistryKey, SubtypeId, ValueRef, XRc,
|
||||
MaybeSend, ReentrantMutex, RegistryKey, ValueRef, XRc,
|
||||
};
|
||||
use crate::userdata::{AnyUserData, MetaMethod, UserData, UserDataRegistry, UserDataVariant};
|
||||
use crate::userdata::{AnyUserData, MetaMethod, UserData, UserDataRegistry, UserDataStorage};
|
||||
use crate::util::{
|
||||
assert_stack, check_stack, get_destructed_userdata_metatable, get_internal_userdata, get_main_state,
|
||||
get_userdata, init_error_registry, init_internal_metatable, init_userdata_metatable, pop_error,
|
||||
push_internal_userdata, push_string, push_table, rawset_field, safe_pcall, safe_xpcall, short_type_name,
|
||||
StackGuard, WrappedFailure,
|
||||
get_metatable_ptr, get_userdata, init_error_registry, init_internal_metatable, init_userdata_metatable,
|
||||
pop_error, push_internal_userdata, push_string, push_table, rawset_field, safe_pcall, safe_xpcall,
|
||||
short_type_name, StackGuard, WrappedFailure,
|
||||
};
|
||||
use crate::value::{FromLuaMulti, IntoLua, MultiValue, Nil, Value};
|
||||
use crate::value::{Nil, Value};
|
||||
|
||||
use super::extra::ExtraData;
|
||||
use super::{Lua, LuaOptions, WeakLua};
|
||||
@@ -37,12 +38,15 @@ use crate::hook::{Debug, HookTriggers};
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
use {
|
||||
crate::multi::MultiValue,
|
||||
crate::traits::FromLuaMulti,
|
||||
crate::types::{AsyncCallback, AsyncCallbackUpvalue, AsyncPollUpvalue},
|
||||
std::ptr::NonNull,
|
||||
std::task::{Context, Poll, Waker},
|
||||
};
|
||||
|
||||
/// An inner Lua struct which holds a raw Lua state.
|
||||
#[doc(hidden)]
|
||||
pub struct RawLua {
|
||||
// The state is dynamic and depends on context
|
||||
pub(super) state: Cell<*mut ffi::lua_State>,
|
||||
@@ -83,8 +87,11 @@ impl RawLua {
|
||||
unsafe { (*self.extra.get()).weak() }
|
||||
}
|
||||
|
||||
/// Returns a pointer to the current Lua state.
|
||||
///
|
||||
/// The pointer refers to the active Lua coroutine and depends on the context.
|
||||
#[inline(always)]
|
||||
pub(crate) fn state(&self) -> *mut ffi::lua_State {
|
||||
pub fn state(&self) -> *mut ffi::lua_State {
|
||||
self.state.get()
|
||||
}
|
||||
|
||||
@@ -122,7 +129,7 @@ impl RawLua {
|
||||
let extra = rawlua.lock().extra.get();
|
||||
|
||||
mlua_expect!(
|
||||
load_from_std_lib(state, libs),
|
||||
load_std_libs(state, libs),
|
||||
"Error during loading standard libraries"
|
||||
);
|
||||
(*extra).libs |= libs;
|
||||
@@ -231,8 +238,8 @@ impl RawLua {
|
||||
|
||||
/// Marks the Lua state as safe.
|
||||
#[inline(always)]
|
||||
pub(super) unsafe fn set_safe(&self) {
|
||||
(*self.extra.get()).safe = true;
|
||||
pub(super) fn mark_safe(&self) {
|
||||
unsafe { (*self.extra.get()).safe = true };
|
||||
}
|
||||
|
||||
/// Loads the specified subset of the standard libraries into an existing Lua state.
|
||||
@@ -256,7 +263,7 @@ impl RawLua {
|
||||
));
|
||||
}
|
||||
|
||||
let res = load_from_std_lib(self.main_state, libs);
|
||||
let res = load_std_libs(self.main_state, libs);
|
||||
|
||||
// If `package` library loaded into a safe lua state then disable C modules
|
||||
let curr_libs = (*self.extra.get()).libs;
|
||||
@@ -278,7 +285,7 @@ impl RawLua {
|
||||
/// See [`Lua::app_data_ref`]
|
||||
#[track_caller]
|
||||
#[inline]
|
||||
pub(crate) fn app_data_ref<T: 'static>(&self) -> Option<AppDataRef<T>> {
|
||||
pub(crate) fn app_data_ref_unguarded<T: 'static>(&self) -> Option<AppDataRef<T>> {
|
||||
let extra = unsafe { &*self.extra.get() };
|
||||
extra.app_data.borrow(None)
|
||||
}
|
||||
@@ -286,7 +293,7 @@ impl RawLua {
|
||||
/// See [`Lua::app_data_mut`]
|
||||
#[track_caller]
|
||||
#[inline]
|
||||
pub(crate) fn app_data_mut<T: 'static>(&self) -> Option<AppDataRefMut<T>> {
|
||||
pub(crate) fn app_data_mut_unguarded<T: 'static>(&self) -> Option<AppDataRefMut<T>> {
|
||||
let extra = unsafe { &*self.extra.get() };
|
||||
extra.app_data.borrow_mut(None)
|
||||
}
|
||||
@@ -351,8 +358,11 @@ impl RawLua {
|
||||
triggers: HookTriggers,
|
||||
callback: F,
|
||||
) where
|
||||
F: Fn(&Lua, Debug) -> Result<()> + MaybeSend + 'static,
|
||||
F: Fn(&Lua, Debug) -> Result<crate::VmState> + MaybeSend + 'static,
|
||||
{
|
||||
use crate::types::VmState;
|
||||
use std::rc::Rc;
|
||||
|
||||
unsafe extern "C-unwind" fn hook_proc(state: *mut ffi::lua_State, ar: *mut ffi::lua_Debug) {
|
||||
let extra = ExtraData::get(state);
|
||||
if (*extra).hook_thread != state {
|
||||
@@ -360,20 +370,37 @@ impl RawLua {
|
||||
ffi::lua_sethook(state, None, 0, 0);
|
||||
return;
|
||||
}
|
||||
callback_error_ext(state, extra, move |extra, _| {
|
||||
let result = callback_error_ext(state, extra, move |extra, _| {
|
||||
let hook_cb = (*extra).hook_callback.clone();
|
||||
let hook_cb = mlua_expect!(hook_cb, "no hook callback set in hook_proc");
|
||||
if std::rc::Rc::strong_count(&hook_cb) > 2 {
|
||||
return Ok(()); // Don't allow recursion
|
||||
if Rc::strong_count(&hook_cb) > 2 {
|
||||
return Ok(VmState::Continue); // Don't allow recursion
|
||||
}
|
||||
let rawlua = (*extra).raw_lua();
|
||||
let _guard = StateGuard::new(rawlua, state);
|
||||
let debug = Debug::new(rawlua, ar);
|
||||
hook_cb((*extra).lua(), debug)
|
||||
})
|
||||
});
|
||||
match result {
|
||||
VmState::Continue => {}
|
||||
VmState::Yield => {
|
||||
// Only count and line events can yield
|
||||
if (*ar).event == ffi::LUA_HOOKCOUNT || (*ar).event == ffi::LUA_HOOKLINE {
|
||||
#[cfg(any(feature = "lua54", feature = "lua53"))]
|
||||
if ffi::lua_isyieldable(state) != 0 {
|
||||
ffi::lua_yield(state, 0);
|
||||
}
|
||||
#[cfg(any(feature = "lua52", feature = "lua51", feature = "luajit"))]
|
||||
{
|
||||
ffi::lua_pushliteral(state, "attempt to yield from a hook");
|
||||
ffi::lua_error(state);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(*self.extra.get()).hook_callback = Some(std::rc::Rc::new(callback));
|
||||
(*self.extra.get()).hook_callback = Some(Rc::new(callback));
|
||||
(*self.extra.get()).hook_thread = state; // Mark for what thread the hook is set
|
||||
ffi::lua_sethook(state, Some(hook_proc), triggers.mask(), triggers.count());
|
||||
}
|
||||
@@ -500,10 +527,9 @@ impl RawLua {
|
||||
|
||||
/// Pushes a value that implements `IntoLua` onto the Lua stack.
|
||||
///
|
||||
/// Uses 2 stack spaces, does not call checkstack.
|
||||
#[doc(hidden)]
|
||||
/// Uses up to 2 stack spaces to push a single value, does not call `checkstack`.
|
||||
#[inline(always)]
|
||||
pub unsafe fn push(&self, value: impl IntoLua) -> Result<()> {
|
||||
pub(crate) unsafe fn push(&self, value: impl IntoLua) -> Result<()> {
|
||||
value.push_into_stack(self)
|
||||
}
|
||||
|
||||
@@ -530,10 +556,13 @@ impl RawLua {
|
||||
Value::Function(f) => self.push_ref(&f.0),
|
||||
Value::Thread(t) => self.push_ref(&t.0),
|
||||
Value::UserData(ud) => self.push_ref(&ud.0),
|
||||
#[cfg(feature = "luau")]
|
||||
Value::Buffer(buf) => self.push_ref(&buf.0),
|
||||
Value::Error(err) => {
|
||||
let protect = !self.unlikely_memory_error();
|
||||
push_internal_userdata(state, WrappedFailure::Error(*err.clone()), protect)?;
|
||||
}
|
||||
Value::Other(vref) => self.push_ref(vref),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -584,9 +613,9 @@ impl RawLua {
|
||||
let v = ffi::lua_tovector(state, idx);
|
||||
mlua_debug_assert!(!v.is_null(), "vector is null");
|
||||
#[cfg(not(feature = "luau-vector4"))]
|
||||
return Value::Vector(crate::types::Vector([*v, *v.add(1), *v.add(2)]));
|
||||
return Value::Vector(crate::Vector([*v, *v.add(1), *v.add(2)]));
|
||||
#[cfg(feature = "luau-vector4")]
|
||||
return Value::Vector(crate::types::Vector([*v, *v.add(1), *v.add(2), *v.add(3)]));
|
||||
return Value::Vector(crate::Vector([*v, *v.add(1), *v.add(2), *v.add(3)]));
|
||||
}
|
||||
|
||||
ffi::LUA_TSTRING => {
|
||||
@@ -618,7 +647,7 @@ impl RawLua {
|
||||
}
|
||||
_ => {
|
||||
ffi::lua_xpush(state, self.ref_thread(), idx);
|
||||
Value::UserData(AnyUserData(self.pop_ref_thread(), SubtypeId::None))
|
||||
Value::UserData(AnyUserData(self.pop_ref_thread()))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -631,19 +660,14 @@ impl RawLua {
|
||||
|
||||
#[cfg(feature = "luau")]
|
||||
ffi::LUA_TBUFFER => {
|
||||
// Buffer is represented as a userdata type
|
||||
ffi::lua_xpush(state, self.ref_thread(), idx);
|
||||
Value::UserData(AnyUserData(self.pop_ref_thread(), SubtypeId::Buffer))
|
||||
Value::Buffer(crate::Buffer(self.pop_ref_thread()))
|
||||
}
|
||||
|
||||
#[cfg(feature = "luajit")]
|
||||
ffi::LUA_TCDATA => {
|
||||
// CData is represented as a userdata type
|
||||
_ => {
|
||||
ffi::lua_xpush(state, self.ref_thread(), idx);
|
||||
Value::UserData(AnyUserData(self.pop_ref_thread(), SubtypeId::CData))
|
||||
Value::Other(self.pop_ref_thread())
|
||||
}
|
||||
|
||||
_ => mlua_panic!("unexpected value type on stack"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -704,6 +728,11 @@ impl RawLua {
|
||||
|
||||
#[inline]
|
||||
pub(crate) unsafe fn unlikely_memory_error(&self) -> bool {
|
||||
#[cfg(debug_assertions)]
|
||||
if cfg!(force_memory_limit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// MemoryInfo is empty in module mode so we cannot predict memory limits
|
||||
match MemoryState::get(self.main_state) {
|
||||
mem_state if !mem_state.is_null() => (*mem_state).memory_limit() == 0,
|
||||
@@ -711,45 +740,45 @@ impl RawLua {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn make_userdata<T>(&self, data: UserDataVariant<T>) -> Result<AnyUserData>
|
||||
pub(crate) unsafe fn make_userdata<T>(&self, data: UserDataStorage<T>) -> Result<AnyUserData>
|
||||
where
|
||||
T: UserData + 'static,
|
||||
{
|
||||
self.make_userdata_with_metatable(data, || {
|
||||
// Check if userdata/metatable is already registered
|
||||
let type_id = TypeId::of::<T>();
|
||||
if let Some(&table_id) = (*self.extra.get()).registered_userdata.get(&type_id) {
|
||||
if let Some(&table_id) = (*self.extra.get()).registered_userdata_t.get(&type_id) {
|
||||
return Ok(table_id as Integer);
|
||||
}
|
||||
|
||||
// Create a new metatable from `UserData` definition
|
||||
let mut registry = const { UserDataRegistry::new() };
|
||||
let mut registry = UserDataRegistry::new(type_id);
|
||||
T::register(&mut registry);
|
||||
|
||||
self.register_userdata_metatable(registry)
|
||||
self.create_userdata_metatable(registry)
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn make_any_userdata<T>(&self, data: UserDataVariant<T>) -> Result<AnyUserData>
|
||||
pub(crate) unsafe fn make_any_userdata<T>(&self, data: UserDataStorage<T>) -> Result<AnyUserData>
|
||||
where
|
||||
T: 'static,
|
||||
{
|
||||
self.make_userdata_with_metatable(data, || {
|
||||
// Check if userdata/metatable is already registered
|
||||
let type_id = TypeId::of::<T>();
|
||||
if let Some(&table_id) = (*self.extra.get()).registered_userdata.get(&type_id) {
|
||||
if let Some(&table_id) = (*self.extra.get()).registered_userdata_t.get(&type_id) {
|
||||
return Ok(table_id as Integer);
|
||||
}
|
||||
|
||||
// Create an empty metatable
|
||||
let registry = const { UserDataRegistry::new() };
|
||||
self.register_userdata_metatable::<T>(registry)
|
||||
let registry = UserDataRegistry::<T>::new(type_id);
|
||||
self.create_userdata_metatable(registry)
|
||||
})
|
||||
}
|
||||
|
||||
unsafe fn make_userdata_with_metatable<T>(
|
||||
&self,
|
||||
data: UserDataVariant<T>,
|
||||
data: UserDataStorage<T>,
|
||||
get_metatable_id: impl FnOnce() -> Result<Integer>,
|
||||
) -> Result<AnyUserData> {
|
||||
let state = self.state();
|
||||
@@ -760,10 +789,7 @@ impl RawLua {
|
||||
ffi::lua_pushnil(state);
|
||||
ffi::lua_rawgeti(state, ffi::LUA_REGISTRYINDEX, get_metatable_id()?);
|
||||
let protect = !self.unlikely_memory_error();
|
||||
#[cfg(not(feature = "lua54"))]
|
||||
crate::util::push_userdata(state, data, protect)?;
|
||||
#[cfg(feature = "lua54")]
|
||||
crate::util::push_userdata_uv(state, data, crate::userdata::USER_VALUE_MAXSLOT as c_int, protect)?;
|
||||
ffi::lua_replace(state, -3);
|
||||
ffi::lua_setmetatable(state, -2);
|
||||
|
||||
@@ -779,15 +805,34 @@ impl RawLua {
|
||||
ffi::lua_setuservalue(state, -2);
|
||||
}
|
||||
|
||||
Ok(AnyUserData(self.pop_ref(), SubtypeId::None))
|
||||
Ok(AnyUserData(self.pop_ref()))
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn register_userdata_metatable<T: 'static>(
|
||||
pub(crate) unsafe fn create_userdata_metatable<T>(
|
||||
&self,
|
||||
mut registry: UserDataRegistry<T>,
|
||||
registry: UserDataRegistry<T>,
|
||||
) -> Result<Integer> {
|
||||
let state = self.state();
|
||||
let _sg = StackGuard::new(state);
|
||||
let type_id = registry.type_id();
|
||||
|
||||
self.push_userdata_metatable(registry)?;
|
||||
|
||||
let mt_ptr = ffi::lua_topointer(state, -1);
|
||||
let id = protect_lua!(state, 1, 0, |state| {
|
||||
ffi::luaL_ref(state, ffi::LUA_REGISTRYINDEX)
|
||||
})?;
|
||||
|
||||
if let Some(type_id) = type_id {
|
||||
(*self.extra.get()).registered_userdata_t.insert(type_id, id);
|
||||
}
|
||||
self.register_userdata_metatable(mt_ptr, type_id);
|
||||
|
||||
Ok(id as Integer)
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn push_userdata_metatable<T>(&self, mut registry: UserDataRegistry<T>) -> Result<()> {
|
||||
let state = self.state();
|
||||
let _sg = StackGuard::with_top(state, ffi::lua_gettop(state) + 1);
|
||||
check_stack(state, 13)?;
|
||||
|
||||
// Prepare metatable, add meta methods first and then meta fields
|
||||
@@ -922,7 +967,7 @@ impl RawLua {
|
||||
let extra_init = None;
|
||||
#[cfg(not(feature = "luau"))]
|
||||
let extra_init: Option<fn(*mut ffi::lua_State) -> Result<()>> = Some(|state| {
|
||||
ffi::lua_pushcfunction(state, crate::util::userdata_destructor::<UserDataVariant<T>>);
|
||||
ffi::lua_pushcfunction(state, crate::util::userdata_destructor::<UserDataStorage<T>>);
|
||||
rawset_field(state, -2, "__gc")
|
||||
});
|
||||
|
||||
@@ -938,44 +983,21 @@ impl RawLua {
|
||||
// Pop extra tables to get metatable on top of the stack
|
||||
ffi::lua_pop(state, extra_tables_count);
|
||||
|
||||
let mt_ptr = ffi::lua_topointer(state, -1);
|
||||
let id = protect_lua!(state, 1, 0, |state| {
|
||||
ffi::luaL_ref(state, ffi::LUA_REGISTRYINDEX)
|
||||
})?;
|
||||
|
||||
let type_id = TypeId::of::<T>();
|
||||
(*self.extra.get()).registered_userdata.insert(type_id, id);
|
||||
(*self.extra.get())
|
||||
.registered_userdata_mt
|
||||
.insert(mt_ptr, Some(type_id));
|
||||
|
||||
Ok(id as Integer)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// #[inline]
|
||||
// pub(crate) unsafe fn register_raw_userdata_metatable(
|
||||
// &self,
|
||||
// ptr: *const c_void,
|
||||
// type_id: Option<TypeId>,
|
||||
// ) {
|
||||
// (*self.extra.get())
|
||||
// .registered_userdata_mt
|
||||
// .insert(ptr, type_id);
|
||||
// }
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn register_userdata_metatable(&self, mt_ptr: *const c_void, type_id: Option<TypeId>) {
|
||||
(*self.extra.get()).registered_userdata_mt.insert(mt_ptr, type_id);
|
||||
}
|
||||
|
||||
// #[inline]
|
||||
// pub(crate) unsafe fn deregister_raw_userdata_metatable(&self, ptr: *const c_void) {
|
||||
// (*self.extra.get()).registered_userdata_mt.remove(&ptr);
|
||||
// if (*self.extra.get()).last_checked_userdata_mt.0 == ptr {
|
||||
// (*self.extra.get()).last_checked_userdata_mt = (ptr::null(), None);
|
||||
// }
|
||||
// }
|
||||
|
||||
// #[inline(always)]
|
||||
// pub(crate) unsafe fn get_userdata_ref<T: 'static>(&self, idx: c_int) -> Result<UserDataRef<T>> {
|
||||
// let guard = self.lua().lock_arc();
|
||||
// (*get_userdata::<UserDataVariant<T>>(self.state(), idx)).try_make_ref(guard)
|
||||
// }
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn deregister_userdata_metatable(&self, mt_ptr: *const c_void) {
|
||||
(*self.extra.get()).registered_userdata_mt.remove(&mt_ptr);
|
||||
if (*self.extra.get()).last_checked_userdata_mt.0 == mt_ptr {
|
||||
(*self.extra.get()).last_checked_userdata_mt = (ptr::null(), None);
|
||||
}
|
||||
}
|
||||
|
||||
// Returns `TypeId` for the userdata ref, checking that it's registered and not destructed.
|
||||
//
|
||||
@@ -985,8 +1007,18 @@ impl RawLua {
|
||||
}
|
||||
|
||||
// Same as `get_userdata_ref_type_id` but assumes the userdata is already on the stack.
|
||||
pub(crate) unsafe fn get_userdata_type_id(&self, idx: c_int) -> Result<Option<TypeId>> {
|
||||
self.get_userdata_type_id_inner(self.state(), idx)
|
||||
pub(crate) unsafe fn get_userdata_type_id<T>(&self, idx: c_int) -> Result<Option<TypeId>> {
|
||||
match self.get_userdata_type_id_inner(self.state(), idx) {
|
||||
Ok(type_id) => Ok(type_id),
|
||||
Err(Error::UserDataTypeMismatch) if ffi::lua_type(self.state(), idx) != ffi::LUA_TUSERDATA => {
|
||||
// Report `FromLuaConversionError` instead
|
||||
let idx_type_name = CStr::from_ptr(ffi::luaL_typename(self.state(), idx));
|
||||
let idx_type_name = idx_type_name.to_str().unwrap();
|
||||
let message = format!("expected userdata of type '{}'", short_type_name::<T>());
|
||||
Err(Error::from_lua_conversion(idx_type_name, "userdata", message))
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn get_userdata_type_id_inner(
|
||||
@@ -994,11 +1026,10 @@ impl RawLua {
|
||||
state: *mut ffi::lua_State,
|
||||
idx: c_int,
|
||||
) -> Result<Option<TypeId>> {
|
||||
if ffi::lua_getmetatable(state, idx) == 0 {
|
||||
let mt_ptr = get_metatable_ptr(state, idx);
|
||||
if mt_ptr.is_null() {
|
||||
return Err(Error::UserDataTypeMismatch);
|
||||
}
|
||||
let mt_ptr = ffi::lua_topointer(state, -1);
|
||||
ffi::lua_pop(state, 1);
|
||||
|
||||
// Fast path to skip looking up the metatable in the map
|
||||
let (last_mt, last_type_id) = (*self.extra.get()).last_checked_userdata_mt;
|
||||
@@ -1028,8 +1059,6 @@ impl RawLua {
|
||||
|
||||
// Creates a Function out of a Callback containing a 'static Fn.
|
||||
pub(crate) fn create_callback(&self, func: Callback) -> Result<Function> {
|
||||
// This is non-scoped version of the callback (upvalue is always valid)
|
||||
// TODO: add a scoped version
|
||||
unsafe extern "C-unwind" fn call_callback(state: *mut ffi::lua_State) -> c_int {
|
||||
let upvalue = get_userdata::<CallbackUpvalue>(state, ffi::lua_upvalueindex(1));
|
||||
callback_error_ext(state, (*upvalue).extra.get(), |extra, nargs| {
|
||||
@@ -1037,8 +1066,10 @@ impl RawLua {
|
||||
// The lock must be already held as the callback is executed
|
||||
let rawlua = (*extra).raw_lua();
|
||||
let _guard = StateGuard::new(rawlua, state);
|
||||
let func = &*(*upvalue).data;
|
||||
func(rawlua, nargs)
|
||||
match (*upvalue).data {
|
||||
Some(ref func) => func(rawlua, nargs),
|
||||
None => Err(Error::CallbackDestructed),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1047,6 +1078,7 @@ impl RawLua {
|
||||
let _sg = StackGuard::new(state);
|
||||
check_stack(state, 4)?;
|
||||
|
||||
let func = Some(func);
|
||||
let extra = XRc::clone(&self.extra);
|
||||
let protect = !self.unlikely_memory_error();
|
||||
push_internal_userdata(state, CallbackUpvalue { data: func, extra }, protect)?;
|
||||
@@ -1068,7 +1100,7 @@ impl RawLua {
|
||||
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52", feature = "luau"))]
|
||||
unsafe {
|
||||
if !(*self.extra.get()).libs.contains(StdLib::COROUTINE) {
|
||||
load_from_std_lib(self.main_state, StdLib::COROUTINE)?;
|
||||
load_std_libs(self.main_state, StdLib::COROUTINE)?;
|
||||
(*self.extra.get()).libs |= StdLib::COROUTINE;
|
||||
}
|
||||
}
|
||||
@@ -1217,7 +1249,7 @@ impl RawLua {
|
||||
}
|
||||
|
||||
// Uses 3 stack spaces
|
||||
unsafe fn load_from_std_lib(state: *mut ffi::lua_State, libs: StdLib) -> Result<()> {
|
||||
unsafe fn load_std_libs(state: *mut ffi::lua_State, libs: StdLib) -> Result<()> {
|
||||
#[inline(always)]
|
||||
pub unsafe fn requiref(
|
||||
state: *mut ffi::lua_State,
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ impl<'a> StateGuard<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for StateGuard<'a> {
|
||||
impl Drop for StateGuard<'_> {
|
||||
fn drop(&mut self) {
|
||||
self.0.state.set(self.1);
|
||||
}
|
||||
|
||||
+57
-37
@@ -1,20 +1,20 @@
|
||||
use std::borrow::Borrow;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::ops::Deref;
|
||||
use std::os::raw::c_void;
|
||||
use std::os::raw::{c_int, c_void};
|
||||
use std::string::String as StdString;
|
||||
use std::{cmp, fmt, slice, str};
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::state::Lua;
|
||||
use crate::types::{LuaType, ValueRef};
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
use {
|
||||
serde::ser::{Serialize, Serializer},
|
||||
std::result::Result as StdResult,
|
||||
};
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::state::Lua;
|
||||
use crate::types::ValueRef;
|
||||
|
||||
/// Handle to an internal Lua string.
|
||||
///
|
||||
/// Unlike Rust strings, Lua strings may not be valid UTF-8.
|
||||
@@ -45,7 +45,7 @@ impl String {
|
||||
let BorrowedBytes(bytes, guard) = self.as_bytes();
|
||||
let s = str::from_utf8(bytes).map_err(|e| Error::FromLuaConversionError {
|
||||
from: "string",
|
||||
to: "&str",
|
||||
to: "&str".to_string(),
|
||||
message: Some(e.to_string()),
|
||||
})?;
|
||||
Ok(BorrowedStr(s, guard))
|
||||
@@ -105,22 +105,22 @@ impl String {
|
||||
|
||||
unsafe fn to_slice(&self) -> (&[u8], Lua) {
|
||||
let lua = self.0.lua.upgrade();
|
||||
let rawlua = lua.lock();
|
||||
let ref_thread = rawlua.ref_thread();
|
||||
unsafe {
|
||||
let slice = unsafe {
|
||||
let rawlua = lua.lock();
|
||||
let ref_thread = rawlua.ref_thread();
|
||||
|
||||
mlua_debug_assert!(
|
||||
ffi::lua_type(ref_thread, self.0.index) == ffi::LUA_TSTRING,
|
||||
"string ref is not string type"
|
||||
);
|
||||
|
||||
let mut size = 0;
|
||||
// This will not trigger a 'm' error, because the reference is guaranteed to be of
|
||||
// string type
|
||||
let mut size = 0;
|
||||
let data = ffi::lua_tolstring(ref_thread, self.0.index, &mut size);
|
||||
|
||||
drop(rawlua);
|
||||
(slice::from_raw_parts(data as *const u8, size + 1), lua)
|
||||
}
|
||||
slice::from_raw_parts(data as *const u8, size + 1)
|
||||
};
|
||||
(slice, lua)
|
||||
}
|
||||
|
||||
/// Converts this string to a generic C pointer.
|
||||
@@ -143,27 +143,12 @@ impl fmt::Debug for String {
|
||||
}
|
||||
|
||||
// Format as bytes
|
||||
write!(f, "b\"")?;
|
||||
for &b in bytes {
|
||||
// https://doc.rust-lang.org/reference/tokens.html#byte-escapes
|
||||
match b {
|
||||
b'\n' => write!(f, "\\n")?,
|
||||
b'\r' => write!(f, "\\r")?,
|
||||
b'\t' => write!(f, "\\t")?,
|
||||
b'\\' | b'"' => write!(f, "\\{}", b as char)?,
|
||||
b'\0' => write!(f, "\\0")?,
|
||||
// ASCII printable
|
||||
0x20..=0x7e => write!(f, "{}", b as char)?,
|
||||
_ => write!(f, "\\x{b:02x}")?,
|
||||
}
|
||||
}
|
||||
write!(f, "\"")?;
|
||||
|
||||
Ok(())
|
||||
write!(f, "b")?;
|
||||
<bstr::BStr as fmt::Debug>::fmt(bstr::BStr::new(&bytes), f)
|
||||
}
|
||||
}
|
||||
|
||||
// Lua strings are basically &[u8] slices, so implement PartialEq for anything resembling that.
|
||||
// Lua strings are basically `&[u8]` slices, so implement `PartialEq` for anything resembling that.
|
||||
//
|
||||
// This makes our `String` comparable with `Vec<u8>`, `[u8]`, `&str` and `String`.
|
||||
//
|
||||
@@ -179,19 +164,34 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<String> for String {
|
||||
impl PartialEq for String {
|
||||
fn eq(&self, other: &String) -> bool {
|
||||
self.as_bytes() == other.as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<&String> for String {
|
||||
fn eq(&self, other: &&String) -> bool {
|
||||
self.as_bytes() == other.as_bytes()
|
||||
impl Eq for String {}
|
||||
|
||||
impl<T> PartialOrd<T> for String
|
||||
where
|
||||
T: AsRef<[u8]> + ?Sized,
|
||||
{
|
||||
fn partial_cmp(&self, other: &T) -> Option<cmp::Ordering> {
|
||||
self.as_bytes().partial_cmp(&other.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for String {}
|
||||
impl PartialOrd for String {
|
||||
fn partial_cmp(&self, other: &String) -> Option<cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for String {
|
||||
fn cmp(&self, other: &String) -> cmp::Ordering {
|
||||
self.as_bytes().cmp(&other.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for String {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
@@ -259,6 +259,8 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for BorrowedStr<'_> {}
|
||||
|
||||
impl<T> PartialOrd<T> for BorrowedStr<'_>
|
||||
where
|
||||
T: AsRef<str>,
|
||||
@@ -268,6 +270,12 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for BorrowedStr<'_> {
|
||||
fn cmp(&self, other: &Self) -> cmp::Ordering {
|
||||
self.0.cmp(other.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// A borrowed byte slice (`&[u8]`) that holds a strong reference to the Lua state.
|
||||
pub struct BorrowedBytes<'a>(&'a [u8], #[allow(unused)] Lua);
|
||||
|
||||
@@ -309,6 +317,8 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for BorrowedBytes<'_> {}
|
||||
|
||||
impl<T> PartialOrd<T> for BorrowedBytes<'_>
|
||||
where
|
||||
T: AsRef<[u8]>,
|
||||
@@ -318,6 +328,12 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for BorrowedBytes<'_> {
|
||||
fn cmp(&self, other: &Self) -> cmp::Ordering {
|
||||
self.0.cmp(other.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoIterator for BorrowedBytes<'a> {
|
||||
type Item = &'a u8;
|
||||
type IntoIter = slice::Iter<'a, u8>;
|
||||
@@ -327,6 +343,10 @@ impl<'a> IntoIterator for BorrowedBytes<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl LuaType for String {
|
||||
const TYPE_ID: c_int = ffi::LUA_TSTRING;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod assertions {
|
||||
use super::*;
|
||||
|
||||
+66
-78
@@ -1,9 +1,20 @@
|
||||
use std::collections::HashSet;
|
||||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
use std::os::raw::c_void;
|
||||
use std::os::raw::{c_int, c_void};
|
||||
use std::string::String as StdString;
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::function::Function;
|
||||
use crate::state::{LuaGuard, RawLua};
|
||||
use crate::traits::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, ObjectLike};
|
||||
use crate::types::{Integer, LuaType, ValueRef};
|
||||
use crate::util::{assert_stack, check_stack, get_metatable_ptr, StackGuard};
|
||||
use crate::value::{Nil, Value};
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
use futures_util::future::{self, Either, Future};
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
use {
|
||||
rustc_hash::FxHashSet,
|
||||
@@ -11,19 +22,8 @@ use {
|
||||
std::{cell::RefCell, rc::Rc, result::Result as StdResult},
|
||||
};
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::function::Function;
|
||||
use crate::state::{LuaGuard, RawLua};
|
||||
use crate::traits::ObjectLike;
|
||||
use crate::types::{Integer, ValueRef};
|
||||
use crate::util::{assert_stack, check_stack, StackGuard};
|
||||
use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, Nil, Value};
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
use futures_util::future::{self, Either, Future};
|
||||
|
||||
/// Handle to an internal Lua table.
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub struct Table(pub(crate) ValueRef);
|
||||
|
||||
impl Table {
|
||||
@@ -59,7 +59,7 @@ impl Table {
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// [`raw_set`]: #method.raw_set
|
||||
/// [`raw_set`]: Table::raw_set
|
||||
pub fn set(&self, key: impl IntoLua, value: impl IntoLua) -> Result<()> {
|
||||
// Fast track (skip protected call)
|
||||
if !self.has_metatable() {
|
||||
@@ -106,7 +106,7 @@ impl Table {
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// [`raw_get`]: #method.raw_get
|
||||
/// [`raw_get`]: Table::raw_get
|
||||
pub fn get<V: FromLua>(&self, key: impl IntoLua) -> Result<V> {
|
||||
// Fast track (skip protected call)
|
||||
if !self.has_metatable() {
|
||||
@@ -218,21 +218,20 @@ impl Table {
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn equals<T: AsRef<Self>>(&self, other: T) -> Result<bool> {
|
||||
let other = other.as_ref();
|
||||
pub fn equals(&self, other: &Self) -> Result<bool> {
|
||||
if self == other {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
// Compare using __eq metamethod if exists
|
||||
// Compare using `__eq` metamethod if exists
|
||||
// First, check the self for the metamethod.
|
||||
// If self does not define it, then check the other table.
|
||||
if let Some(mt) = self.get_metatable() {
|
||||
if let Some(mt) = self.metatable() {
|
||||
if mt.contains_key("__eq")? {
|
||||
return mt.get::<Function>("__eq")?.call((self, other));
|
||||
}
|
||||
}
|
||||
if let Some(mt) = other.get_metatable() {
|
||||
if let Some(mt) = other.metatable() {
|
||||
if mt.contains_key("__eq")? {
|
||||
return mt.get::<Function>("__eq")?.call((self, other));
|
||||
}
|
||||
@@ -243,12 +242,12 @@ impl Table {
|
||||
|
||||
/// Sets a key-value pair without invoking metamethods.
|
||||
pub fn raw_set(&self, key: impl IntoLua, value: impl IntoLua) -> Result<()> {
|
||||
#[cfg(feature = "luau")]
|
||||
self.check_readonly_write()?;
|
||||
|
||||
let lua = self.0.lua.lock();
|
||||
let state = lua.state();
|
||||
unsafe {
|
||||
#[cfg(feature = "luau")]
|
||||
self.check_readonly_write(&lua)?;
|
||||
|
||||
let _sg = StackGuard::new(state);
|
||||
check_stack(state, 5)?;
|
||||
|
||||
@@ -283,7 +282,9 @@ impl Table {
|
||||
}
|
||||
|
||||
/// Inserts element value at position `idx` to the table, shifting up the elements from
|
||||
/// `table[idx]`. The worst case complexity is O(n), where n is the table length.
|
||||
/// `table[idx]`.
|
||||
///
|
||||
/// The worst case complexity is O(n), where n is the table length.
|
||||
pub fn raw_insert(&self, idx: Integer, value: impl IntoLua) -> Result<()> {
|
||||
let size = self.raw_len() as Integer;
|
||||
if idx < 1 || idx > size + 1 {
|
||||
@@ -311,12 +312,12 @@ impl Table {
|
||||
|
||||
/// Appends a value to the back of the table without invoking metamethods.
|
||||
pub fn raw_push(&self, value: impl IntoLua) -> Result<()> {
|
||||
#[cfg(feature = "luau")]
|
||||
self.check_readonly_write()?;
|
||||
|
||||
let lua = self.0.lua.lock();
|
||||
let state = lua.state();
|
||||
unsafe {
|
||||
#[cfg(feature = "luau")]
|
||||
self.check_readonly_write(&lua)?;
|
||||
|
||||
let _sg = StackGuard::new(state);
|
||||
check_stack(state, 4)?;
|
||||
|
||||
@@ -339,12 +340,12 @@ impl Table {
|
||||
|
||||
/// Removes the last element from the table and returns it, without invoking metamethods.
|
||||
pub fn raw_pop<V: FromLua>(&self) -> Result<V> {
|
||||
#[cfg(feature = "luau")]
|
||||
self.check_readonly_write()?;
|
||||
|
||||
let lua = self.0.lua.lock();
|
||||
let state = lua.state();
|
||||
unsafe {
|
||||
#[cfg(feature = "luau")]
|
||||
self.check_readonly_write(&lua)?;
|
||||
|
||||
let _sg = StackGuard::new(state);
|
||||
check_stack(state, 3)?;
|
||||
|
||||
@@ -362,8 +363,8 @@ impl Table {
|
||||
/// Removes a key from the table.
|
||||
///
|
||||
/// If `key` is an integer, mlua shifts down the elements from `table[key+1]`,
|
||||
/// and erases element `table[key]`. The complexity is O(n) in the worst case,
|
||||
/// where n is the table length.
|
||||
/// and erases element `table[key]`. The complexity is `O(n)` in the worst case,
|
||||
/// where `n` is the table length.
|
||||
///
|
||||
/// For other key types this is equivalent to setting `table[key] = nil`.
|
||||
pub fn raw_remove(&self, key: impl IntoLua) -> Result<()> {
|
||||
@@ -400,13 +401,13 @@ impl Table {
|
||||
///
|
||||
/// This method is useful to clear the table while keeping its capacity.
|
||||
pub fn clear(&self) -> Result<()> {
|
||||
#[cfg(feature = "luau")]
|
||||
self.check_readonly_write()?;
|
||||
|
||||
let lua = self.0.lua.lock();
|
||||
unsafe {
|
||||
#[cfg(feature = "luau")]
|
||||
ffi::lua_cleartable(lua.ref_thread(), self.0.index);
|
||||
{
|
||||
self.check_readonly_write(&lua)?;
|
||||
ffi::lua_cleartable(lua.ref_thread(), self.0.index);
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "luau"))]
|
||||
{
|
||||
@@ -438,9 +439,8 @@ impl Table {
|
||||
|
||||
/// Returns the result of the Lua `#` operator.
|
||||
///
|
||||
/// This might invoke the `__len` metamethod. Use the [`raw_len`] method if that is not desired.
|
||||
///
|
||||
/// [`raw_len`]: #method.raw_len
|
||||
/// This might invoke the `__len` metamethod. Use the [`Table::raw_len`] method if that is not
|
||||
/// desired.
|
||||
pub fn len(&self) -> Result<Integer> {
|
||||
// Fast track (skip protected call)
|
||||
if !self.has_metatable() {
|
||||
@@ -492,8 +492,10 @@ impl Table {
|
||||
|
||||
/// Returns a reference to the metatable of this table, or `None` if no metatable is set.
|
||||
///
|
||||
/// Unlike the `getmetatable` Lua function, this method ignores the `__metatable` field.
|
||||
pub fn get_metatable(&self) -> Option<Table> {
|
||||
/// Unlike the [`getmetatable`] Lua function, this method ignores the `__metatable` field.
|
||||
///
|
||||
/// [`getmetatable`]: https://www.lua.org/manual/5.4/manual.html#pdf-getmetatable
|
||||
pub fn metatable(&self) -> Option<Table> {
|
||||
let lua = self.0.lua.lock();
|
||||
let state = lua.state();
|
||||
unsafe {
|
||||
@@ -509,6 +511,13 @@ impl Table {
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[deprecated(since = "0.10.0", note = "please use `metatable` instead")]
|
||||
#[cfg(not(tarpaulin_include))]
|
||||
pub fn get_metatable(&self) -> Option<Table> {
|
||||
self.metatable()
|
||||
}
|
||||
|
||||
/// Sets or removes the metatable of this table.
|
||||
///
|
||||
/// If `metatable` is `None`, the metatable is removed (if no metatable is set, this does
|
||||
@@ -541,14 +550,7 @@ impl Table {
|
||||
#[inline]
|
||||
pub fn has_metatable(&self) -> bool {
|
||||
let lua = self.0.lua.lock();
|
||||
let ref_thread = lua.ref_thread();
|
||||
unsafe {
|
||||
if ffi::lua_getmetatable(ref_thread, self.0.index) != 0 {
|
||||
ffi::lua_pop(ref_thread, 1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
unsafe { !get_metatable_ptr(lua.ref_thread(), self.0.index).is_null() }
|
||||
}
|
||||
|
||||
/// Sets `readonly` attribute on the table.
|
||||
@@ -615,7 +617,6 @@ impl Table {
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// [`Result`]: crate::Result
|
||||
/// [Lua manual]: http://www.lua.org/manual/5.4/manual.html#pdf-next
|
||||
pub fn pairs<K: FromLua, V: FromLua>(&self) -> TablePairs<K, V> {
|
||||
TablePairs {
|
||||
@@ -682,10 +683,6 @@ impl Table {
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// [`pairs`]: #method.pairs
|
||||
/// [`Result`]: crate::Result
|
||||
/// [Lua manual]: http://www.lua.org/manual/5.4/manual.html#pdf-next
|
||||
pub fn sequence_values<V: FromLua>(&self) -> TableSequence<V> {
|
||||
TableSequence {
|
||||
guard: self.0.lua.lock(),
|
||||
@@ -695,7 +692,7 @@ impl Table {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
/// Iterates over the sequence part of the table, invoking the given closure on each value.
|
||||
pub(crate) fn for_each_value<V>(&self, mut f: impl FnMut(V) -> Result<()>) -> Result<()>
|
||||
where
|
||||
V: FromLua,
|
||||
@@ -720,12 +717,12 @@ impl Table {
|
||||
/// Sets element value at position `idx` without invoking metamethods.
|
||||
#[doc(hidden)]
|
||||
pub fn raw_seti(&self, idx: usize, value: impl IntoLua) -> Result<()> {
|
||||
#[cfg(feature = "luau")]
|
||||
self.check_readonly_write()?;
|
||||
|
||||
let lua = self.0.lua.lock();
|
||||
let state = lua.state();
|
||||
unsafe {
|
||||
#[cfg(feature = "luau")]
|
||||
self.check_readonly_write(&lua)?;
|
||||
|
||||
let _sg = StackGuard::new(state);
|
||||
check_stack(state, 5)?;
|
||||
|
||||
@@ -761,8 +758,8 @@ impl Table {
|
||||
|
||||
#[cfg(feature = "luau")]
|
||||
#[inline(always)]
|
||||
pub(crate) fn check_readonly_write(&self) -> Result<()> {
|
||||
if self.is_readonly() {
|
||||
fn check_readonly_write(&self, lua: &RawLua) -> Result<()> {
|
||||
if unsafe { ffi::lua_getreadonly(lua.ref_thread(), self.0.index) != 0 } {
|
||||
return Err(Error::runtime("attempt to modify a readonly table"));
|
||||
}
|
||||
Ok(())
|
||||
@@ -779,7 +776,7 @@ impl Table {
|
||||
// Collect key/value pairs into a vector so we can sort them
|
||||
let mut pairs = self.pairs::<Value, Value>().flatten().collect::<Vec<_>>();
|
||||
// Sort keys
|
||||
pairs.sort_by(|(a, _), (b, _)| a.cmp(b));
|
||||
pairs.sort_by(|(a, _), (b, _)| a.sort_cmp(b));
|
||||
if pairs.is_empty() {
|
||||
return write!(fmt, "{{}}");
|
||||
}
|
||||
@@ -800,20 +797,7 @@ impl fmt::Debug for Table {
|
||||
if fmt.alternate() {
|
||||
return self.fmt_pretty(fmt, 0, &mut HashSet::new());
|
||||
}
|
||||
fmt.write_fmt(format_args!("Table({:?})", self.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Table {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.0 == other.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<Table> for Table {
|
||||
#[inline]
|
||||
fn as_ref(&self) -> &Self {
|
||||
self
|
||||
fmt.debug_tuple("Table").field(&self.0).finish()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -867,6 +851,10 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl LuaType for Table {
|
||||
const TYPE_ID: c_int = ffi::LUA_TTABLE;
|
||||
}
|
||||
|
||||
impl ObjectLike for Table {
|
||||
#[inline]
|
||||
fn get<V: FromLua>(&self, key: impl IntoLua) -> Result<V> {
|
||||
@@ -978,7 +966,7 @@ impl<'a> SerializableTable<'a> {
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl<'a> Serialize for SerializableTable<'a> {
|
||||
impl Serialize for SerializableTable<'_> {
|
||||
fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
@@ -1069,7 +1057,7 @@ pub struct TablePairs<'a, K, V> {
|
||||
_phantom: PhantomData<(K, V)>,
|
||||
}
|
||||
|
||||
impl<'a, K, V> Iterator for TablePairs<'a, K, V>
|
||||
impl<K, V> Iterator for TablePairs<'_, K, V>
|
||||
where
|
||||
K: FromLua,
|
||||
V: FromLua,
|
||||
@@ -1129,7 +1117,7 @@ pub struct TableSequence<'a, V> {
|
||||
_phantom: PhantomData<V>,
|
||||
}
|
||||
|
||||
impl<'a, V> Iterator for TableSequence<'a, V>
|
||||
impl<V> Iterator for TableSequence<'_, V>
|
||||
where
|
||||
V: FromLua,
|
||||
{
|
||||
|
||||
+44
-25
@@ -1,12 +1,13 @@
|
||||
use std::fmt;
|
||||
use std::os::raw::{c_int, c_void};
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
#[allow(unused)]
|
||||
use crate::state::Lua;
|
||||
use crate::state::RawLua;
|
||||
use crate::types::ValueRef;
|
||||
use crate::traits::{FromLuaMulti, IntoLuaMulti};
|
||||
use crate::types::{LuaType, ValueRef};
|
||||
use crate::util::{check_stack, error_traceback_thread, pop_error, StackGuard};
|
||||
use crate::value::{FromLuaMulti, IntoLuaMulti};
|
||||
|
||||
#[cfg(not(feature = "luau"))]
|
||||
use crate::{
|
||||
@@ -42,7 +43,7 @@ pub enum ThreadStatus {
|
||||
}
|
||||
|
||||
/// Handle to an internal Lua thread (coroutine).
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone)]
|
||||
pub struct Thread(pub(crate) ValueRef, pub(crate) *mut ffi::lua_State);
|
||||
|
||||
#[cfg(feature = "send")]
|
||||
@@ -68,23 +69,23 @@ pub struct AsyncThread<A, R> {
|
||||
|
||||
impl Thread {
|
||||
#[inline(always)]
|
||||
const fn state(&self) -> *mut ffi::lua_State {
|
||||
fn state(&self) -> *mut ffi::lua_State {
|
||||
self.1
|
||||
}
|
||||
|
||||
/// Resumes execution of this thread.
|
||||
///
|
||||
/// Equivalent to `coroutine.resume`.
|
||||
/// Equivalent to [`coroutine.resume`].
|
||||
///
|
||||
/// Passes `args` as arguments to the thread. If the coroutine has called `coroutine.yield`, it
|
||||
/// will return these arguments. Otherwise, the coroutine wasn't yet started, so the arguments
|
||||
/// are passed to its main function.
|
||||
/// Passes `args` as arguments to the thread. If the coroutine has called [`coroutine.yield`],
|
||||
/// it will return these arguments. Otherwise, the coroutine wasn't yet started, so the
|
||||
/// arguments are passed to its main function.
|
||||
///
|
||||
/// If the thread is no longer in `Active` state (meaning it has finished execution or
|
||||
/// encountered an error), this will return `Err(CoroutineInactive)`, otherwise will return `Ok`
|
||||
/// as follows:
|
||||
/// If the thread is no longer resumable (meaning it has finished execution or encountered an
|
||||
/// error), this will return [`Error::CoroutineUnresumable`], otherwise will return `Ok` as
|
||||
/// follows:
|
||||
///
|
||||
/// If the thread calls `coroutine.yield`, returns the values passed to `yield`. If the thread
|
||||
/// If the thread calls [`coroutine.yield`], returns the values passed to `yield`. If the thread
|
||||
/// `return`s values from its main function, returns those.
|
||||
///
|
||||
/// # Examples
|
||||
@@ -113,6 +114,9 @@ impl Thread {
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// [`coroutine.resume`]: https://www.lua.org/manual/5.4/manual.html#pdf-coroutine.resume
|
||||
/// [`coroutine.yield`]: https://www.lua.org/manual/5.4/manual.html#pdf-coroutine.yield
|
||||
pub fn resume<R>(&self, args: impl IntoLuaMulti) -> Result<R>
|
||||
where
|
||||
R: FromLuaMulti,
|
||||
@@ -143,7 +147,7 @@ impl Thread {
|
||||
let state = lua.state();
|
||||
let thread_state = self.state();
|
||||
|
||||
let nargs = args.push_into_stack_multi(&lua)?;
|
||||
let nargs = args.push_into_stack_multi(lua)?;
|
||||
if nargs > 0 {
|
||||
check_stack(thread_state, nargs)?;
|
||||
ffi::lua_xmove(state, thread_state, nargs);
|
||||
@@ -186,15 +190,15 @@ impl Thread {
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets a 'hook' function that will periodically be called as Lua code executes.
|
||||
/// Sets a hook function that will periodically be called as Lua code executes.
|
||||
///
|
||||
/// This function is similar or [`Lua::set_hook()`] except that it sets for the thread.
|
||||
/// To remove a hook call [`Lua::remove_hook()`].
|
||||
/// This function is similar or [`Lua::set_hook`] except that it sets for the thread.
|
||||
/// To remove a hook call [`Lua::remove_hook`].
|
||||
#[cfg(not(feature = "luau"))]
|
||||
#[cfg_attr(docsrs, doc(cfg(not(feature = "luau"))))]
|
||||
pub fn set_hook<F>(&self, triggers: HookTriggers, callback: F)
|
||||
where
|
||||
F: Fn(&Lua, Debug) -> Result<()> + MaybeSend + 'static,
|
||||
F: Fn(&Lua, Debug) -> Result<crate::VmState> + MaybeSend + 'static,
|
||||
{
|
||||
let lua = self.0.lua.lock();
|
||||
unsafe {
|
||||
@@ -251,21 +255,22 @@ impl Thread {
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts Thread to an AsyncThread which implements [`Future`] and [`Stream`] traits.
|
||||
/// Converts [`Thread`] to an [`AsyncThread`] which implements [`Future`] and [`Stream`] traits.
|
||||
///
|
||||
/// `args` are passed as arguments to the thread function for first call.
|
||||
/// The object calls [`resume()`] while polling and also allows to run rust futures
|
||||
/// The object calls [`resume`] while polling and also allow to run Rust futures
|
||||
/// to completion using an executor.
|
||||
///
|
||||
/// Using AsyncThread as a Stream allows to iterate through `coroutine.yield()`
|
||||
/// values whereas Future version discards that values and poll until the final
|
||||
/// Using [`AsyncThread`] as a [`Stream`] allow to iterate through [`coroutine.yield`]
|
||||
/// values whereas [`Future`] version discards that values and poll until the final
|
||||
/// one (returned from the thread function).
|
||||
///
|
||||
/// Requires `feature = "async"`
|
||||
///
|
||||
/// [`Future`]: std::future::Future
|
||||
/// [`Stream`]: futures_util::stream::Stream
|
||||
/// [`resume()`]: https://www.lua.org/manual/5.4/manual.html#lua_resume
|
||||
/// [`resume`]: https://www.lua.org/manual/5.4/manual.html#lua_resume
|
||||
/// [`coroutine.yield`]: https://www.lua.org/manual/5.4/manual.html#pdf-coroutine.yield
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
@@ -315,7 +320,7 @@ impl Thread {
|
||||
/// Under the hood replaces the global environment table with a new table,
|
||||
/// that performs writes locally and proxies reads to caller's global environment.
|
||||
///
|
||||
/// This mode ideally should be used together with the global sandbox mode [`Lua::sandbox()`].
|
||||
/// This mode ideally should be used together with the global sandbox mode [`Lua::sandbox`].
|
||||
///
|
||||
/// Please note that Luau links environment table with chunk when loading it into Lua state.
|
||||
/// Therefore you need to load chunks into a thread to link with the thread environment.
|
||||
@@ -324,6 +329,7 @@ impl Thread {
|
||||
///
|
||||
/// ```
|
||||
/// # use mlua::{Lua, Result};
|
||||
/// # #[cfg(feature = "luau")]
|
||||
/// # fn main() -> Result<()> {
|
||||
/// let lua = Lua::new();
|
||||
/// let thread = lua.create_thread(lua.create_function(|lua2, ()| {
|
||||
@@ -338,10 +344,13 @@ impl Thread {
|
||||
/// assert_eq!(lua.globals().get::<Option<u32>>("var")?, None);
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
///
|
||||
/// # #[cfg(not(feature = "luau"))]
|
||||
/// # fn main() { }
|
||||
/// ```
|
||||
///
|
||||
/// Requires `feature = "luau"`
|
||||
#[cfg(any(feature = "luau", docsrs))]
|
||||
#[cfg(any(feature = "luau", doc))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
|
||||
#[doc(hidden)]
|
||||
pub fn sandbox(&self) -> Result<()> {
|
||||
@@ -366,12 +375,22 @@ impl Thread {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Thread {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt.debug_tuple("Thread").field(&self.0).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Thread {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.0 == other.0
|
||||
}
|
||||
}
|
||||
|
||||
impl LuaType for Thread {
|
||||
const TYPE_ID: c_int = ffi::LUA_TTHREAD;
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
impl<A, R> AsyncThread<A, R> {
|
||||
#[inline]
|
||||
@@ -511,7 +530,7 @@ impl<'lua, 'a> WakerGuard<'lua, 'a> {
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
impl<'lua, 'a> Drop for WakerGuard<'lua, 'a> {
|
||||
impl Drop for WakerGuard<'_, '_> {
|
||||
fn drop(&mut self) {
|
||||
unsafe { self.lua.set_waker(self.prev) };
|
||||
}
|
||||
|
||||
+235
-2
@@ -1,12 +1,145 @@
|
||||
use std::os::raw::c_int;
|
||||
use std::string::String as StdString;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::error::{Error, Result};
|
||||
use crate::multi::MultiValue;
|
||||
use crate::private::Sealed;
|
||||
use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti};
|
||||
use crate::state::{Lua, RawLua};
|
||||
use crate::types::MaybeSend;
|
||||
use crate::util::{check_stack, short_type_name};
|
||||
use crate::value::Value;
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
use std::future::Future;
|
||||
|
||||
/// Trait for types convertible to [`Value`].
|
||||
pub trait IntoLua: Sized {
|
||||
/// Performs the conversion.
|
||||
fn into_lua(self, lua: &Lua) -> Result<Value>;
|
||||
|
||||
/// Pushes the value into the Lua stack.
|
||||
///
|
||||
/// # Safety
|
||||
/// This method does not check Lua stack space.
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
|
||||
lua.push_value(&self.into_lua(lua.lua())?)
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for types convertible from [`Value`].
|
||||
pub trait FromLua: Sized {
|
||||
/// Performs the conversion.
|
||||
fn from_lua(value: Value, lua: &Lua) -> Result<Self>;
|
||||
|
||||
/// Performs the conversion for an argument (eg. function argument).
|
||||
///
|
||||
/// `i` is the argument index (position),
|
||||
/// `to` is a function name that received the argument.
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
fn from_lua_arg(arg: Value, i: usize, to: Option<&str>, lua: &Lua) -> Result<Self> {
|
||||
Self::from_lua(arg, lua).map_err(|err| Error::BadArgument {
|
||||
to: to.map(|s| s.to_string()),
|
||||
pos: i,
|
||||
name: None,
|
||||
cause: Arc::new(err),
|
||||
})
|
||||
}
|
||||
|
||||
/// Performs the conversion for a value in the Lua stack at index `idx`.
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
unsafe fn from_stack(idx: c_int, lua: &RawLua) -> Result<Self> {
|
||||
Self::from_lua(lua.stack_value(idx, None), lua.lua())
|
||||
}
|
||||
|
||||
/// Same as `from_lua_arg` but for a value in the Lua stack at index `idx`.
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
unsafe fn from_stack_arg(idx: c_int, i: usize, to: Option<&str>, lua: &RawLua) -> Result<Self> {
|
||||
Self::from_stack(idx, lua).map_err(|err| Error::BadArgument {
|
||||
to: to.map(|s| s.to_string()),
|
||||
pos: i,
|
||||
name: None,
|
||||
cause: Arc::new(err),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for types convertible to any number of Lua values.
|
||||
///
|
||||
/// This is a generalization of [`IntoLua`], allowing any number of resulting Lua values instead of
|
||||
/// just one. Any type that implements [`IntoLua`] will automatically implement this trait.
|
||||
pub trait IntoLuaMulti: Sized {
|
||||
/// Performs the conversion.
|
||||
fn into_lua_multi(self, lua: &Lua) -> Result<MultiValue>;
|
||||
|
||||
/// Pushes the values into the Lua stack.
|
||||
///
|
||||
/// Returns number of pushed values.
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
unsafe fn push_into_stack_multi(self, lua: &RawLua) -> Result<c_int> {
|
||||
let values = self.into_lua_multi(lua.lua())?;
|
||||
let len: c_int = values.len().try_into().unwrap();
|
||||
unsafe {
|
||||
check_stack(lua.state(), len + 1)?;
|
||||
for val in &values {
|
||||
lua.push_value(val)?;
|
||||
}
|
||||
}
|
||||
Ok(len)
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for types that can be created from an arbitrary number of Lua values.
|
||||
///
|
||||
/// This is a generalization of [`FromLua`], allowing an arbitrary number of Lua values to
|
||||
/// participate in the conversion. Any type that implements [`FromLua`] will automatically
|
||||
/// implement this trait.
|
||||
pub trait FromLuaMulti: Sized {
|
||||
/// Performs the conversion.
|
||||
///
|
||||
/// In case `values` contains more values than needed to perform the conversion, the excess
|
||||
/// values should be ignored. This reflects the semantics of Lua when calling a function or
|
||||
/// assigning values. Similarly, if not enough values are given, conversions should assume that
|
||||
/// any missing values are nil.
|
||||
fn from_lua_multi(values: MultiValue, lua: &Lua) -> Result<Self>;
|
||||
|
||||
/// Performs the conversion for a list of arguments.
|
||||
///
|
||||
/// `i` is an index (position) of the first argument,
|
||||
/// `to` is a function name that received the arguments.
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
fn from_lua_args(args: MultiValue, i: usize, to: Option<&str>, lua: &Lua) -> Result<Self> {
|
||||
let _ = (i, to);
|
||||
Self::from_lua_multi(args, lua)
|
||||
}
|
||||
|
||||
/// Performs the conversion for a number of values in the Lua stack.
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
unsafe fn from_stack_multi(nvals: c_int, lua: &RawLua) -> Result<Self> {
|
||||
let mut values = MultiValue::with_capacity(nvals as usize);
|
||||
for idx in 0..nvals {
|
||||
values.push_back(lua.stack_value(-nvals + idx, None));
|
||||
}
|
||||
Self::from_lua_multi(values, lua.lua())
|
||||
}
|
||||
|
||||
/// Same as `from_lua_args` but for a number of values in the Lua stack.
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
unsafe fn from_stack_args(nargs: c_int, i: usize, to: Option<&str>, lua: &RawLua) -> Result<Self> {
|
||||
let _ = (i, to);
|
||||
Self::from_stack_multi(nargs, lua)
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait for types that can be used as Lua objects (usually table and userdata).
|
||||
pub trait ObjectLike: Sealed {
|
||||
/// Gets the value associated to `key` from the object, assuming it has `__index` metamethod.
|
||||
@@ -76,3 +209,103 @@ pub trait ObjectLike: Sealed {
|
||||
/// This might invoke the `__tostring` metamethod.
|
||||
fn to_string(&self) -> Result<StdString>;
|
||||
}
|
||||
|
||||
/// A trait for types that can be used as Lua functions.
|
||||
pub trait LuaNativeFn<A: FromLuaMulti> {
|
||||
type Output: IntoLuaMulti;
|
||||
|
||||
fn call(&self, args: A) -> Self::Output;
|
||||
}
|
||||
|
||||
/// A trait for types with mutable state that can be used as Lua functions.
|
||||
pub trait LuaNativeFnMut<A: FromLuaMulti> {
|
||||
type Output: IntoLuaMulti;
|
||||
|
||||
fn call(&mut self, args: A) -> Self::Output;
|
||||
}
|
||||
|
||||
/// A trait for types that returns a future and can be used as Lua functions.
|
||||
#[cfg(feature = "async")]
|
||||
pub trait LuaNativeAsyncFn<A: FromLuaMulti> {
|
||||
type Output: IntoLuaMulti;
|
||||
|
||||
fn call(&self, args: A) -> impl Future<Output = Self::Output> + MaybeSend + 'static;
|
||||
}
|
||||
|
||||
macro_rules! impl_lua_native_fn {
|
||||
($($A:ident),*) => {
|
||||
impl<FN, $($A,)* R> LuaNativeFn<($($A,)*)> for FN
|
||||
where
|
||||
FN: Fn($($A,)*) -> R + MaybeSend + 'static,
|
||||
($($A,)*): FromLuaMulti,
|
||||
R: IntoLuaMulti,
|
||||
{
|
||||
type Output = R;
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
fn call(&self, args: ($($A,)*)) -> Self::Output {
|
||||
let ($($A,)*) = args;
|
||||
self($($A,)*)
|
||||
}
|
||||
}
|
||||
|
||||
impl<FN, $($A,)* R> LuaNativeFnMut<($($A,)*)> for FN
|
||||
where
|
||||
FN: FnMut($($A,)*) -> R + MaybeSend + 'static,
|
||||
($($A,)*): FromLuaMulti,
|
||||
R: IntoLuaMulti,
|
||||
{
|
||||
type Output = R;
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
fn call(&mut self, args: ($($A,)*)) -> Self::Output {
|
||||
let ($($A,)*) = args;
|
||||
self($($A,)*)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
impl<FN, $($A,)* Fut, R> LuaNativeAsyncFn<($($A,)*)> for FN
|
||||
where
|
||||
FN: Fn($($A,)*) -> Fut + MaybeSend + 'static,
|
||||
($($A,)*): FromLuaMulti,
|
||||
Fut: Future<Output = R> + MaybeSend + 'static,
|
||||
R: IntoLuaMulti,
|
||||
{
|
||||
type Output = R;
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
fn call(&self, args: ($($A,)*)) -> impl Future<Output = Self::Output> + MaybeSend + 'static {
|
||||
let ($($A,)*) = args;
|
||||
self($($A,)*)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_lua_native_fn!();
|
||||
impl_lua_native_fn!(A);
|
||||
impl_lua_native_fn!(A, B);
|
||||
impl_lua_native_fn!(A, B, C);
|
||||
impl_lua_native_fn!(A, B, C, D);
|
||||
impl_lua_native_fn!(A, B, C, D, E);
|
||||
impl_lua_native_fn!(A, B, C, D, E, F);
|
||||
impl_lua_native_fn!(A, B, C, D, E, F, G);
|
||||
impl_lua_native_fn!(A, B, C, D, E, F, G, H);
|
||||
impl_lua_native_fn!(A, B, C, D, E, F, G, H, I);
|
||||
impl_lua_native_fn!(A, B, C, D, E, F, G, H, I, J);
|
||||
impl_lua_native_fn!(A, B, C, D, E, F, G, H, I, J, K);
|
||||
impl_lua_native_fn!(A, B, C, D, E, F, G, H, I, J, K, L);
|
||||
impl_lua_native_fn!(A, B, C, D, E, F, G, H, I, J, K, L, M);
|
||||
impl_lua_native_fn!(A, B, C, D, E, F, G, H, I, J, K, L, M, N);
|
||||
impl_lua_native_fn!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
|
||||
impl_lua_native_fn!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);
|
||||
|
||||
pub(crate) trait ShortTypeName {
|
||||
#[inline(always)]
|
||||
fn type_name() -> StdString {
|
||||
short_type_name::<Self>()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ShortTypeName for T {}
|
||||
|
||||
+21
-81
@@ -1,12 +1,11 @@
|
||||
use std::cell::UnsafeCell;
|
||||
use std::fmt;
|
||||
use std::os::raw::{c_int, c_void};
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::error::Result;
|
||||
#[cfg(not(feature = "luau"))]
|
||||
use crate::hook::Debug;
|
||||
use crate::state::{ExtraData, Lua, RawLua, WeakLua};
|
||||
use crate::state::{ExtraData, Lua, RawLua};
|
||||
|
||||
// Re-export mutex wrappers
|
||||
pub(crate) use sync::{ArcReentrantMutexGuard, ReentrantMutex, ReentrantMutexGuard, XRc, XWeak};
|
||||
@@ -18,25 +17,15 @@ pub(crate) type BoxFuture<'a, T> = futures_util::future::BoxFuture<'a, T>;
|
||||
pub(crate) type BoxFuture<'a, T> = futures_util::future::LocalBoxFuture<'a, T>;
|
||||
|
||||
pub use app_data::{AppData, AppDataRef, AppDataRefMut};
|
||||
pub use either::Either;
|
||||
pub use registry_key::RegistryKey;
|
||||
#[cfg(any(feature = "luau", doc))]
|
||||
pub use vector::Vector;
|
||||
pub(crate) use value_ref::ValueRef;
|
||||
|
||||
/// Type of Lua integer numbers.
|
||||
pub type Integer = ffi::lua_Integer;
|
||||
/// Type of Lua floating point numbers.
|
||||
pub type Number = ffi::lua_Number;
|
||||
|
||||
// Represents different subtypes wrapped to AnyUserData
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||
pub(crate) enum SubtypeId {
|
||||
None,
|
||||
#[cfg(feature = "luau")]
|
||||
Buffer,
|
||||
#[cfg(feature = "luajit")]
|
||||
CData,
|
||||
}
|
||||
|
||||
/// A "light" userdata value. Equivalent to an unmanaged raw pointer.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||
pub struct LightUserData(pub *mut c_void);
|
||||
@@ -52,12 +41,14 @@ pub(crate) type Callback = Box<dyn Fn(&RawLua, c_int) -> Result<c_int> + Send +
|
||||
#[cfg(not(feature = "send"))]
|
||||
pub(crate) type Callback = Box<dyn Fn(&RawLua, c_int) -> Result<c_int> + 'static>;
|
||||
|
||||
pub(crate) type ScopedCallback<'s> = Box<dyn Fn(&RawLua, c_int) -> Result<c_int> + 's>;
|
||||
|
||||
pub(crate) struct Upvalue<T> {
|
||||
pub(crate) data: T,
|
||||
pub(crate) extra: XRc<UnsafeCell<ExtraData>>,
|
||||
}
|
||||
|
||||
pub(crate) type CallbackUpvalue = Upvalue<Callback>;
|
||||
pub(crate) type CallbackUpvalue = Upvalue<Option<Callback>>;
|
||||
|
||||
#[cfg(all(feature = "async", feature = "send"))]
|
||||
pub(crate) type AsyncCallback =
|
||||
@@ -73,19 +64,20 @@ pub(crate) type AsyncCallbackUpvalue = Upvalue<AsyncCallback>;
|
||||
#[cfg(feature = "async")]
|
||||
pub(crate) type AsyncPollUpvalue = Upvalue<BoxFuture<'static, Result<c_int>>>;
|
||||
|
||||
/// Type to set next Luau VM action after executing interrupt function.
|
||||
#[cfg(any(feature = "luau", doc))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
|
||||
/// Type to set next Lua VM action after executing interrupt or hook function.
|
||||
pub enum VmState {
|
||||
Continue,
|
||||
/// Yield the current thread.
|
||||
///
|
||||
/// Supported by Lua 5.3+ and Luau.
|
||||
Yield,
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "send", not(feature = "luau")))]
|
||||
pub(crate) type HookCallback = Rc<dyn Fn(&Lua, Debug) -> Result<()> + Send>;
|
||||
pub(crate) type HookCallback = Rc<dyn Fn(&Lua, Debug) -> Result<VmState> + Send>;
|
||||
|
||||
#[cfg(all(not(feature = "send"), not(feature = "luau")))]
|
||||
pub(crate) type HookCallback = Rc<dyn Fn(&Lua, Debug) -> Result<()>>;
|
||||
pub(crate) type HookCallback = Rc<dyn Fn(&Lua, Debug) -> Result<VmState>>;
|
||||
|
||||
#[cfg(all(feature = "send", feature = "luau"))]
|
||||
pub(crate) type InterruptCallback = Rc<dyn Fn(&Lua) -> Result<VmState> + Send>;
|
||||
@@ -112,78 +104,26 @@ impl<T> MaybeSend for T {}
|
||||
|
||||
pub(crate) struct DestructedUserdata;
|
||||
|
||||
pub(crate) struct ValueRef {
|
||||
pub(crate) lua: WeakLua,
|
||||
pub(crate) index: c_int,
|
||||
pub(crate) drop: bool,
|
||||
pub(crate) trait LuaType {
|
||||
const TYPE_ID: c_int;
|
||||
}
|
||||
|
||||
impl ValueRef {
|
||||
#[inline]
|
||||
pub(crate) fn new(lua: &RawLua, index: c_int) -> Self {
|
||||
ValueRef {
|
||||
lua: lua.weak().clone(),
|
||||
index,
|
||||
drop: true,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn to_pointer(&self) -> *const c_void {
|
||||
let lua = self.lua.lock();
|
||||
unsafe { ffi::lua_topointer(lua.ref_thread(), self.index) }
|
||||
}
|
||||
|
||||
/// Returns a copy of the value, which is valid as long as the original value is held.
|
||||
#[inline]
|
||||
pub(crate) fn copy(&self) -> Self {
|
||||
ValueRef {
|
||||
lua: self.lua.clone(),
|
||||
index: self.index,
|
||||
drop: false,
|
||||
}
|
||||
}
|
||||
impl LuaType for bool {
|
||||
const TYPE_ID: c_int = ffi::LUA_TBOOLEAN;
|
||||
}
|
||||
|
||||
impl fmt::Debug for ValueRef {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "Ref({:p})", self.to_pointer())
|
||||
}
|
||||
impl LuaType for Number {
|
||||
const TYPE_ID: c_int = ffi::LUA_TNUMBER;
|
||||
}
|
||||
|
||||
impl Clone for ValueRef {
|
||||
fn clone(&self) -> Self {
|
||||
unsafe { self.lua.lock().clone_ref(self) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ValueRef {
|
||||
fn drop(&mut self) {
|
||||
if self.drop {
|
||||
if let Some(lua) = self.lua.try_lock() {
|
||||
unsafe { lua.drop_ref(self) };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for ValueRef {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
assert!(
|
||||
self.lua == other.lua,
|
||||
"Lua instance passed Value created from a different main Lua state"
|
||||
);
|
||||
let lua = self.lua.lock();
|
||||
unsafe { ffi::lua_rawequal(lua.ref_thread(), self.index, other.index) == 1 }
|
||||
}
|
||||
impl LuaType for LightUserData {
|
||||
const TYPE_ID: c_int = ffi::LUA_TLIGHTUSERDATA;
|
||||
}
|
||||
|
||||
mod app_data;
|
||||
mod registry_key;
|
||||
mod sync;
|
||||
|
||||
#[cfg(any(feature = "luau", doc))]
|
||||
mod vector;
|
||||
mod value_ref;
|
||||
|
||||
#[cfg(test)]
|
||||
mod assertions {
|
||||
|
||||
@@ -12,17 +12,16 @@ use parking_lot::Mutex;
|
||||
/// and instances not manually removed can be garbage collected with
|
||||
/// [`Lua::expire_registry_values`].
|
||||
///
|
||||
/// Be warned, If you place this into Lua via a [`UserData`] type or a rust callback, it is *very
|
||||
/// easy* to accidentally cause reference cycles that the Lua garbage collector cannot resolve.
|
||||
/// Instead of placing a [`RegistryKey`] into a [`UserData`] type, prefer instead to use
|
||||
/// [`AnyUserData::set_user_value`] / [`AnyUserData::user_value`].
|
||||
/// Be warned, If you place this into Lua via a [`UserData`] type or a Rust callback, it is *easy*
|
||||
/// to accidentally cause reference cycles that the Lua garbage collector cannot resolve. Instead of
|
||||
/// placing a [`RegistryKey`] into a [`UserData`] type, consider to use
|
||||
/// [`AnyUserData::set_user_value`].
|
||||
///
|
||||
/// [`UserData`]: crate::UserData
|
||||
/// [`RegistryKey`]: crate::RegistryKey
|
||||
/// [`Lua::remove_registry_value`]: crate::Lua::remove_registry_value
|
||||
/// [`Lua::expire_registry_values`]: crate::Lua::expire_registry_values
|
||||
/// [`AnyUserData::set_user_value`]: crate::AnyUserData::set_user_value
|
||||
/// [`AnyUserData::user_value`]: crate::AnyUserData::user_value
|
||||
pub struct RegistryKey {
|
||||
pub(crate) registry_id: i32,
|
||||
pub(crate) unref_list: Arc<Mutex<Option<Vec<c_int>>>>,
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
use std::fmt;
|
||||
use std::os::raw::{c_int, c_void};
|
||||
|
||||
use crate::state::{RawLua, WeakLua};
|
||||
|
||||
/// A reference to a Lua (complex) value stored in the Lua auxiliary thread.
|
||||
pub(crate) struct ValueRef {
|
||||
pub(crate) lua: WeakLua,
|
||||
pub(crate) index: c_int,
|
||||
pub(crate) drop: bool,
|
||||
}
|
||||
|
||||
impl ValueRef {
|
||||
#[inline]
|
||||
pub(crate) fn new(lua: &RawLua, index: c_int) -> Self {
|
||||
ValueRef {
|
||||
lua: lua.weak().clone(),
|
||||
index,
|
||||
drop: true,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn to_pointer(&self) -> *const c_void {
|
||||
let lua = self.lua.lock();
|
||||
unsafe { ffi::lua_topointer(lua.ref_thread(), self.index) }
|
||||
}
|
||||
|
||||
/// Returns a copy of the value, which is valid as long as the original value is held.
|
||||
#[inline]
|
||||
pub(crate) fn copy(&self) -> Self {
|
||||
ValueRef {
|
||||
lua: self.lua.clone(),
|
||||
index: self.index,
|
||||
drop: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for ValueRef {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "Ref({:p})", self.to_pointer())
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for ValueRef {
|
||||
fn clone(&self) -> Self {
|
||||
unsafe { self.lua.lock().clone_ref(self) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ValueRef {
|
||||
fn drop(&mut self) {
|
||||
if self.drop {
|
||||
if let Some(lua) = self.lua.try_lock() {
|
||||
unsafe { lua.drop_ref(self) };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for ValueRef {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
assert!(
|
||||
self.lua == other.lua,
|
||||
"Lua instance passed Value created from a different main Lua state"
|
||||
);
|
||||
let lua = self.lua.lock();
|
||||
unsafe { ffi::lua_rawequal(lua.ref_thread(), self.index, other.index) == 1 }
|
||||
}
|
||||
}
|
||||
+143
-237
@@ -2,9 +2,19 @@ use std::any::TypeId;
|
||||
use std::ffi::CStr;
|
||||
use std::fmt;
|
||||
use std::hash::Hash;
|
||||
use std::os::raw::{c_char, c_int, c_void};
|
||||
use std::os::raw::{c_char, c_void};
|
||||
use std::string::String as StdString;
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::function::Function;
|
||||
use crate::state::Lua;
|
||||
use crate::string::String;
|
||||
use crate::table::{Table, TablePairs};
|
||||
use crate::traits::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti};
|
||||
use crate::types::{MaybeSend, ValueRef};
|
||||
use crate::util::{check_stack, get_userdata, push_string, take_userdata, StackGuard};
|
||||
use crate::value::Value;
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
use std::future::Future;
|
||||
|
||||
@@ -14,30 +24,16 @@ use {
|
||||
std::result::Result as StdResult,
|
||||
};
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::function::Function;
|
||||
use crate::state::{Lua, LuaGuard};
|
||||
use crate::string::String;
|
||||
use crate::table::{Table, TablePairs};
|
||||
use crate::types::{MaybeSend, SubtypeId, ValueRef};
|
||||
use crate::util::{check_stack, get_userdata, take_userdata, StackGuard};
|
||||
use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, Value};
|
||||
|
||||
// Re-export for convenience
|
||||
pub(crate) use cell::UserDataVariant;
|
||||
pub(crate) use cell::UserDataStorage;
|
||||
pub use cell::{UserDataRef, UserDataRefMut};
|
||||
pub(crate) use registry::UserDataProxy;
|
||||
pub use registry::UserDataRegistry;
|
||||
|
||||
#[cfg(feature = "lua54")]
|
||||
pub(crate) const USER_VALUE_MAXSLOT: usize = 8;
|
||||
|
||||
/// Kinds of metamethods that can be overridden.
|
||||
///
|
||||
/// Currently, this mechanism does not allow overriding the `__gc` metamethod, since there is
|
||||
/// generally no need to do so: [`UserData`] implementors can instead just implement `Drop`.
|
||||
///
|
||||
/// [`UserData`]: crate::UserData
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
#[non_exhaustive]
|
||||
pub enum MetaMethod {
|
||||
@@ -131,7 +127,7 @@ pub enum MetaMethod {
|
||||
///
|
||||
/// Executed when a variable, that marked as to-be-closed, goes out of scope.
|
||||
///
|
||||
/// More information about to-be-closed variabled can be found in the Lua 5.4
|
||||
/// More information about to-be-closed variables can be found in the Lua 5.4
|
||||
/// [documentation][lua_doc].
|
||||
///
|
||||
/// Requires `feature = "lua54"`
|
||||
@@ -245,8 +241,6 @@ impl AsRef<str> for MetaMethod {
|
||||
}
|
||||
|
||||
/// Method registry for [`UserData`] implementors.
|
||||
///
|
||||
/// [`UserData`]: crate::UserData
|
||||
pub trait UserDataMethods<T> {
|
||||
/// Add a regular method which accepts a `&T` as the first parameter.
|
||||
///
|
||||
@@ -265,20 +259,20 @@ pub trait UserDataMethods<T> {
|
||||
///
|
||||
/// Refer to [`add_method`] for more information about the implementation.
|
||||
///
|
||||
/// [`add_method`]: #method.add_method
|
||||
/// [`add_method`]: UserDataMethods::add_method
|
||||
fn add_method_mut<M, A, R>(&mut self, name: impl ToString, method: M)
|
||||
where
|
||||
M: FnMut(&Lua, &mut T, A) -> Result<R> + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
R: IntoLuaMulti;
|
||||
|
||||
/// Add an async method which accepts a `&T` as the first parameter and returns Future.
|
||||
/// Add an async method which accepts a `&T` as the first parameter and returns [`Future`].
|
||||
///
|
||||
/// Refer to [`add_method`] for more information about the implementation.
|
||||
///
|
||||
/// Requires `feature = "async"`
|
||||
///
|
||||
/// [`add_method`]: #method.add_method
|
||||
/// [`add_method`]: UserDataMethods::add_method
|
||||
#[cfg(feature = "async")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
|
||||
fn add_async_method<M, A, MR, R>(&mut self, name: impl ToString, method: M)
|
||||
@@ -289,13 +283,13 @@ pub trait UserDataMethods<T> {
|
||||
MR: Future<Output = Result<R>> + MaybeSend + 'static,
|
||||
R: IntoLuaMulti;
|
||||
|
||||
/// Add an async method which accepts a `&mut T` as the first parameter and returns Future.
|
||||
/// Add an async method which accepts a `&mut T` as the first parameter and returns [`Future`].
|
||||
///
|
||||
/// Refer to [`add_method`] for more information about the implementation.
|
||||
///
|
||||
/// Requires `feature = "async"`
|
||||
///
|
||||
/// [`add_method`]: #method.add_method
|
||||
/// [`add_method`]: UserDataMethods::add_method
|
||||
#[cfg(feature = "async")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
|
||||
fn add_async_method_mut<M, A, MR, R>(&mut self, name: impl ToString, method: M)
|
||||
@@ -306,16 +300,11 @@ pub trait UserDataMethods<T> {
|
||||
MR: Future<Output = Result<R>> + MaybeSend + 'static,
|
||||
R: IntoLuaMulti;
|
||||
|
||||
/// Add a regular method as a function which accepts generic arguments, the first argument will
|
||||
/// be a [`AnyUserData`] of type `T` if the method is called with Lua method syntax:
|
||||
/// `my_userdata:my_method(arg1, arg2)`, or it is passed in as the first argument:
|
||||
/// `my_userdata.my_method(my_userdata, arg1, arg2)`.
|
||||
/// Add a regular method as a function which accepts generic arguments.
|
||||
///
|
||||
/// Prefer to use [`add_method`] or [`add_method_mut`] as they are easier to use.
|
||||
///
|
||||
/// [`AnyUserData`]: crate::AnyUserData
|
||||
/// [`add_method`]: #method.add_method
|
||||
/// [`add_method_mut`]: #method.add_method_mut
|
||||
/// The first argument will be a [`AnyUserData`] of type `T` if the method is called with Lua
|
||||
/// method syntax: `my_userdata:my_method(arg1, arg2)`, or it is passed in as the first
|
||||
/// argument: `my_userdata.my_method(my_userdata, arg1, arg2)`.
|
||||
fn add_function<F, A, R>(&mut self, name: impl ToString, function: F)
|
||||
where
|
||||
F: Fn(&Lua, A) -> Result<R> + MaybeSend + 'static,
|
||||
@@ -324,23 +313,23 @@ pub trait UserDataMethods<T> {
|
||||
|
||||
/// Add a regular method as a mutable function which accepts generic arguments.
|
||||
///
|
||||
/// This is a version of [`add_function`] that accepts a FnMut argument.
|
||||
/// This is a version of [`add_function`] that accepts a `FnMut` argument.
|
||||
///
|
||||
/// [`add_function`]: #method.add_function
|
||||
/// [`add_function`]: UserDataMethods::add_function
|
||||
fn add_function_mut<F, A, R>(&mut self, name: impl ToString, function: F)
|
||||
where
|
||||
F: FnMut(&Lua, A) -> Result<R> + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
R: IntoLuaMulti;
|
||||
|
||||
/// Add a regular method as an async function which accepts generic arguments
|
||||
/// and returns Future.
|
||||
/// Add a regular method as an async function which accepts generic arguments and returns
|
||||
/// [`Future`].
|
||||
///
|
||||
/// This is an async version of [`add_function`].
|
||||
///
|
||||
/// Requires `feature = "async"`
|
||||
///
|
||||
/// [`add_function`]: #method.add_function
|
||||
/// [`add_function`]: UserDataMethods::add_function
|
||||
#[cfg(feature = "async")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
|
||||
fn add_async_function<F, A, FR, R>(&mut self, name: impl ToString, function: F)
|
||||
@@ -357,7 +346,7 @@ pub trait UserDataMethods<T> {
|
||||
/// This can cause an error with certain binary metamethods that can trigger if only the right
|
||||
/// side has a metatable. To prevent this, use [`add_meta_function`].
|
||||
///
|
||||
/// [`add_meta_function`]: #method.add_meta_function
|
||||
/// [`add_meta_function`]: UserDataMethods::add_meta_function
|
||||
fn add_meta_method<M, A, R>(&mut self, name: impl ToString, method: M)
|
||||
where
|
||||
M: Fn(&Lua, &T, A) -> Result<R> + MaybeSend + 'static,
|
||||
@@ -371,20 +360,20 @@ pub trait UserDataMethods<T> {
|
||||
/// This can cause an error with certain binary metamethods that can trigger if only the right
|
||||
/// side has a metatable. To prevent this, use [`add_meta_function`].
|
||||
///
|
||||
/// [`add_meta_function`]: #method.add_meta_function
|
||||
/// [`add_meta_function`]: UserDataMethods::add_meta_function
|
||||
fn add_meta_method_mut<M, A, R>(&mut self, name: impl ToString, method: M)
|
||||
where
|
||||
M: FnMut(&Lua, &mut T, A) -> Result<R> + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
R: IntoLuaMulti;
|
||||
|
||||
/// Add an async metamethod which accepts a `&T` as the first parameter and returns Future.
|
||||
/// Add an async metamethod which accepts a `&T` as the first parameter and returns [`Future`].
|
||||
///
|
||||
/// This is an async version of [`add_meta_method`].
|
||||
///
|
||||
/// Requires `feature = "async"`
|
||||
///
|
||||
/// [`add_meta_method`]: #method.add_meta_method
|
||||
/// [`add_meta_method`]: UserDataMethods::add_meta_method
|
||||
#[cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau"))))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
|
||||
fn add_async_meta_method<M, A, MR, R>(&mut self, name: impl ToString, method: M)
|
||||
@@ -395,13 +384,14 @@ pub trait UserDataMethods<T> {
|
||||
MR: Future<Output = Result<R>> + MaybeSend + 'static,
|
||||
R: IntoLuaMulti;
|
||||
|
||||
/// Add an async metamethod which accepts a `&mut T` as the first parameter and returns Future.
|
||||
/// Add an async metamethod which accepts a `&mut T` as the first parameter and returns
|
||||
/// [`Future`].
|
||||
///
|
||||
/// This is an async version of [`add_meta_method_mut`].
|
||||
///
|
||||
/// Requires `feature = "async"`
|
||||
///
|
||||
/// [`add_meta_method_mut`]: #method.add_meta_method_mut
|
||||
/// [`add_meta_method_mut`]: UserDataMethods::add_meta_method_mut
|
||||
#[cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau"))))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
|
||||
fn add_async_meta_method_mut<M, A, MR, R>(&mut self, name: impl ToString, method: M)
|
||||
@@ -425,22 +415,22 @@ pub trait UserDataMethods<T> {
|
||||
|
||||
/// Add a metamethod as a mutable function which accepts generic arguments.
|
||||
///
|
||||
/// This is a version of [`add_meta_function`] that accepts a FnMut argument.
|
||||
/// This is a version of [`add_meta_function`] that accepts a `FnMut` argument.
|
||||
///
|
||||
/// [`add_meta_function`]: #method.add_meta_function
|
||||
/// [`add_meta_function`]: UserDataMethods::add_meta_function
|
||||
fn add_meta_function_mut<F, A, R>(&mut self, name: impl ToString, function: F)
|
||||
where
|
||||
F: FnMut(&Lua, A) -> Result<R> + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
R: IntoLuaMulti;
|
||||
|
||||
/// Add a metamethod which accepts generic arguments and returns Future.
|
||||
/// Add a metamethod which accepts generic arguments and returns [`Future`].
|
||||
///
|
||||
/// This is an async version of [`add_meta_function`].
|
||||
///
|
||||
/// Requires `feature = "async"`
|
||||
///
|
||||
/// [`add_meta_function`]: #method.add_meta_function
|
||||
/// [`add_meta_function`]: UserDataMethods::add_meta_function
|
||||
#[cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau"))))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
|
||||
fn add_async_meta_function<F, A, FR, R>(&mut self, name: impl ToString, function: F)
|
||||
@@ -452,15 +442,13 @@ pub trait UserDataMethods<T> {
|
||||
}
|
||||
|
||||
/// Field registry for [`UserData`] implementors.
|
||||
///
|
||||
/// [`UserData`]: crate::UserData
|
||||
pub trait UserDataFields<T> {
|
||||
/// Add a static field to the `UserData`.
|
||||
/// Add a static field to the [`UserData`].
|
||||
///
|
||||
/// Static fields are implemented by updating the `__index` metamethod and returning the
|
||||
/// accessed field. This allows them to be used with the expected `userdata.field` syntax.
|
||||
///
|
||||
/// Static fields are usually shared between all instances of the `UserData` of the same type.
|
||||
/// Static fields are usually shared between all instances of the [`UserData`] of the same type.
|
||||
///
|
||||
/// If `add_meta_method` is used to set the `__index` metamethod, it will
|
||||
/// be used as a fall-back if no regular field or method are found.
|
||||
@@ -495,11 +483,6 @@ pub trait UserDataFields<T> {
|
||||
|
||||
/// Add a regular field getter as a function which accepts a generic [`AnyUserData`] of type `T`
|
||||
/// argument.
|
||||
///
|
||||
/// Prefer to use [`add_field_method_get`] as it is easier to use.
|
||||
///
|
||||
/// [`AnyUserData`]: crate::AnyUserData
|
||||
/// [`add_field_method_get`]: #method.add_field_method_get
|
||||
fn add_field_function_get<F, R>(&mut self, name: impl ToString, function: F)
|
||||
where
|
||||
F: Fn(&Lua, AnyUserData) -> Result<R> + MaybeSend + 'static,
|
||||
@@ -507,11 +490,6 @@ pub trait UserDataFields<T> {
|
||||
|
||||
/// Add a regular field setter as a function which accepts a generic [`AnyUserData`] of type `T`
|
||||
/// first argument.
|
||||
///
|
||||
/// Prefer to use [`add_field_method_set`] as it is easier to use.
|
||||
///
|
||||
/// [`AnyUserData`]: crate::AnyUserData
|
||||
/// [`add_field_method_set`]: #method.add_field_method_set
|
||||
fn add_field_function_set<F, A>(&mut self, name: impl ToString, function: F)
|
||||
where
|
||||
F: FnMut(&Lua, AnyUserData, A) -> Result<()> + MaybeSend + 'static,
|
||||
@@ -519,7 +497,7 @@ pub trait UserDataFields<T> {
|
||||
|
||||
/// Add a metatable field.
|
||||
///
|
||||
/// This will initialize the metatable field with `value` on `UserData` creation.
|
||||
/// This will initialize the metatable field with `value` on [`UserData`] creation.
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
@@ -531,7 +509,7 @@ pub trait UserDataFields<T> {
|
||||
|
||||
/// Add a metatable field computed from `f`.
|
||||
///
|
||||
/// This will initialize the metatable field from `f` on `UserData` creation.
|
||||
/// This will initialize the metatable field from `f` on [`UserData`] creation.
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
@@ -546,6 +524,7 @@ pub trait UserDataFields<T> {
|
||||
/// Trait for custom userdata types.
|
||||
///
|
||||
/// By implementing this trait, a struct becomes eligible for use inside Lua code.
|
||||
///
|
||||
/// Implementation of [`IntoLua`] is automatically provided, [`FromLua`] needs to be implemented
|
||||
/// manually.
|
||||
///
|
||||
@@ -605,11 +584,6 @@ pub trait UserDataFields<T> {
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// [`IntoLua`]: crate::IntoLua
|
||||
/// [`FromLua`]: crate::FromLua
|
||||
/// [`UserDataFields`]: crate::UserDataFields
|
||||
/// [`UserDataMethods`]: crate::UserDataMethods
|
||||
pub trait UserData: Sized {
|
||||
/// Adds custom fields specific to this userdata.
|
||||
#[allow(unused_variables)]
|
||||
@@ -631,52 +605,74 @@ pub trait UserData: Sized {
|
||||
|
||||
/// Handle to an internal Lua userdata for any type that implements [`UserData`].
|
||||
///
|
||||
/// Similar to `std::any::Any`, this provides an interface for dynamic type checking via the [`is`]
|
||||
/// and [`borrow`] methods.
|
||||
///
|
||||
/// Internally, instances are stored in a `RefCell`, to best match the mutable semantics of the Lua
|
||||
/// language.
|
||||
/// Similar to [`std::any::Any`], this provides an interface for dynamic type checking via the
|
||||
/// [`is`] and [`borrow`] methods.
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// This API should only be used when necessary. Implementing [`UserData`] already allows defining
|
||||
/// methods which check the type and acquire a borrow behind the scenes.
|
||||
///
|
||||
/// [`UserData`]: crate::UserData
|
||||
/// [`is`]: crate::AnyUserData::is
|
||||
/// [`borrow`]: crate::AnyUserData::borrow
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AnyUserData(pub(crate) ValueRef, pub(crate) SubtypeId);
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct AnyUserData(pub(crate) ValueRef);
|
||||
|
||||
impl AnyUserData {
|
||||
/// Checks whether the type of this userdata is `T`.
|
||||
#[inline]
|
||||
pub fn is<T: 'static>(&self) -> bool {
|
||||
self.inspect::<T, _, _>(|_, _| Ok(())).is_ok()
|
||||
self.inspect::<T, _, _>(|_| Ok(())).is_ok()
|
||||
}
|
||||
|
||||
/// Borrow this userdata immutably if it is of type `T`.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns a `UserDataBorrowError` if the userdata is already mutably borrowed. Returns a
|
||||
/// `UserDataTypeMismatch` if the userdata is not of type `T`.
|
||||
/// Returns a [`UserDataBorrowError`] if the userdata is already mutably borrowed.
|
||||
/// Returns a [`DataTypeMismatch`] if the userdata is not of type `T` or if it's
|
||||
/// scoped.
|
||||
///
|
||||
/// [`UserDataBorrowError`]: crate::Error::UserDataBorrowError
|
||||
/// [`DataTypeMismatch`]: crate::Error::UserDataTypeMismatch
|
||||
#[inline]
|
||||
pub fn borrow<T: 'static>(&self) -> Result<UserDataRef<T>> {
|
||||
self.inspect(|variant, _| variant.try_borrow_owned())
|
||||
self.inspect(|ud| ud.try_borrow_owned())
|
||||
}
|
||||
|
||||
/// Borrow this userdata immutably if it is of type `T`, passing the borrowed value
|
||||
/// to the closure.
|
||||
///
|
||||
/// This method is the only way to borrow scoped userdata (created inside [`Lua::scope`]).
|
||||
pub fn borrow_scoped<T: 'static, R>(&self, f: impl FnOnce(&T) -> R) -> Result<R> {
|
||||
self.inspect(|ud| ud.try_borrow_scoped(|ud| f(ud)))
|
||||
}
|
||||
|
||||
/// Borrow this userdata mutably if it is of type `T`.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns a `UserDataBorrowMutError` if the userdata cannot be mutably borrowed.
|
||||
/// Returns a `UserDataTypeMismatch` if the userdata is not of type `T`.
|
||||
/// Returns a [`UserDataBorrowMutError`] if the userdata cannot be mutably borrowed.
|
||||
/// Returns a [`UserDataTypeMismatch`] if the userdata is not of type `T` or if it's
|
||||
/// scoped.
|
||||
///
|
||||
/// [`UserDataBorrowMutError`]: crate::Error::UserDataBorrowMutError
|
||||
/// [`UserDataTypeMismatch`]: crate::Error::UserDataTypeMismatch
|
||||
#[inline]
|
||||
pub fn borrow_mut<T: 'static>(&self) -> Result<UserDataRefMut<T>> {
|
||||
self.inspect(|variant, _| variant.try_borrow_owned_mut())
|
||||
self.inspect(|ud| ud.try_borrow_owned_mut())
|
||||
}
|
||||
|
||||
/// Borrow this userdata mutably if it is of type `T`, passing the borrowed value
|
||||
/// to the closure.
|
||||
///
|
||||
/// This method is the only way to borrow scoped userdata (created inside [`Lua::scope`]).
|
||||
pub fn borrow_mut_scoped<T: 'static, R>(&self, f: impl FnOnce(&mut T) -> R) -> Result<R> {
|
||||
self.inspect(|ud| ud.try_borrow_scoped_mut(|ud| f(ud)))
|
||||
}
|
||||
|
||||
/// Takes the value out of this userdata.
|
||||
///
|
||||
/// Sets the special "destructed" metatable that prevents any further operations with this
|
||||
/// userdata.
|
||||
///
|
||||
@@ -692,22 +688,22 @@ impl AnyUserData {
|
||||
match type_id {
|
||||
Some(type_id) if type_id == TypeId::of::<T>() => {
|
||||
// Try to borrow userdata exclusively
|
||||
let _ = (*get_userdata::<UserDataVariant<T>>(state, -1)).try_borrow_mut()?;
|
||||
take_userdata::<UserDataVariant<T>>(state).into_inner()
|
||||
let _ = (*get_userdata::<UserDataStorage<T>>(state, -1)).try_borrow_mut()?;
|
||||
take_userdata::<UserDataStorage<T>>(state).into_inner()
|
||||
}
|
||||
_ => Err(Error::UserDataTypeMismatch),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets an associated value to this `AnyUserData`.
|
||||
/// Sets an associated value to this [`AnyUserData`].
|
||||
///
|
||||
/// The value may be any Lua value whatsoever, and can be retrieved with [`user_value`].
|
||||
///
|
||||
/// This is the same as calling [`set_nth_user_value`] with `n` set to 1.
|
||||
///
|
||||
/// [`user_value`]: #method.user_value
|
||||
/// [`set_nth_user_value`]: #method.set_nth_user_value
|
||||
/// [`user_value`]: AnyUserData::user_value
|
||||
/// [`set_nth_user_value`]: AnyUserData::set_nth_user_value
|
||||
#[inline]
|
||||
pub fn set_user_value(&self, v: impl IntoLua) -> Result<()> {
|
||||
self.set_nth_user_value(1, v)
|
||||
@@ -717,29 +713,21 @@ impl AnyUserData {
|
||||
///
|
||||
/// This is the same as calling [`nth_user_value`] with `n` set to 1.
|
||||
///
|
||||
/// [`set_user_value`]: #method.set_user_value
|
||||
/// [`nth_user_value`]: #method.nth_user_value
|
||||
/// [`set_user_value`]: AnyUserData::set_user_value
|
||||
/// [`nth_user_value`]: AnyUserData::nth_user_value
|
||||
#[inline]
|
||||
pub fn user_value<V: FromLua>(&self) -> Result<V> {
|
||||
self.nth_user_value(1)
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[deprecated(since = "0.9.0", note = "please use `user_value` instead")]
|
||||
pub fn get_user_value<V: FromLua>(&self) -> Result<V> {
|
||||
self.nth_user_value(1)
|
||||
}
|
||||
|
||||
/// Sets an associated `n`th value to this `AnyUserData`.
|
||||
/// Sets an associated `n`th value to this [`AnyUserData`].
|
||||
///
|
||||
/// The value may be any Lua value whatsoever, and can be retrieved with [`nth_user_value`].
|
||||
/// `n` starts from 1 and can be up to 65535.
|
||||
///
|
||||
/// This is supported for all Lua versions.
|
||||
/// In Lua 5.4 first 7 elements are stored in a most efficient way.
|
||||
/// For other Lua versions this functionality is provided using a wrapping table.
|
||||
/// This is supported for all Lua versions using a wrapping table.
|
||||
///
|
||||
/// [`nth_user_value`]: #method.nth_user_value
|
||||
/// [`nth_user_value`]: AnyUserData::nth_user_value
|
||||
pub fn set_nth_user_value(&self, n: usize, v: impl IntoLua) -> Result<()> {
|
||||
if n < 1 || n > u16::MAX as usize {
|
||||
return Err(Error::runtime("user value index out of bounds"));
|
||||
@@ -754,29 +742,16 @@ impl AnyUserData {
|
||||
lua.push_userdata_ref(&self.0)?;
|
||||
lua.push(v)?;
|
||||
|
||||
#[cfg(feature = "lua54")]
|
||||
if n < USER_VALUE_MAXSLOT {
|
||||
ffi::lua_setiuservalue(state, -2, n as c_int);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Multiple (extra) user values are emulated by storing them in a table
|
||||
protect_lua!(state, 2, 0, |state| {
|
||||
if getuservalue_table(state, -2) != ffi::LUA_TTABLE {
|
||||
if ffi::lua_getuservalue(state, -2) != ffi::LUA_TTABLE {
|
||||
// Create a new table to use as uservalue
|
||||
ffi::lua_pop(state, 1);
|
||||
ffi::lua_newtable(state);
|
||||
ffi::lua_pushvalue(state, -1);
|
||||
|
||||
#[cfg(feature = "lua54")]
|
||||
ffi::lua_setiuservalue(state, -4, USER_VALUE_MAXSLOT as c_int);
|
||||
#[cfg(not(feature = "lua54"))]
|
||||
ffi::lua_setuservalue(state, -4);
|
||||
}
|
||||
ffi::lua_pushvalue(state, -2);
|
||||
#[cfg(feature = "lua54")]
|
||||
ffi::lua_rawseti(state, -2, (n - USER_VALUE_MAXSLOT + 1) as ffi::lua_Integer);
|
||||
#[cfg(not(feature = "lua54"))]
|
||||
ffi::lua_rawseti(state, -2, n as ffi::lua_Integer);
|
||||
})?;
|
||||
|
||||
@@ -788,11 +763,9 @@ impl AnyUserData {
|
||||
///
|
||||
/// `n` starts from 1 and can be up to 65535.
|
||||
///
|
||||
/// This is supported for all Lua versions.
|
||||
/// In Lua 5.4 first 7 elements are stored in a most efficient way.
|
||||
/// For other Lua versions this functionality is provided using a wrapping table.
|
||||
/// This is supported for all Lua versions using a wrapping table.
|
||||
///
|
||||
/// [`set_nth_user_value`]: #method.set_nth_user_value
|
||||
/// [`set_nth_user_value`]: AnyUserData::set_nth_user_value
|
||||
pub fn nth_user_value<V: FromLua>(&self, n: usize) -> Result<V> {
|
||||
if n < 1 || n > u16::MAX as usize {
|
||||
return Err(Error::runtime("user value index out of bounds"));
|
||||
@@ -806,39 +779,21 @@ impl AnyUserData {
|
||||
|
||||
lua.push_userdata_ref(&self.0)?;
|
||||
|
||||
#[cfg(feature = "lua54")]
|
||||
if n < USER_VALUE_MAXSLOT {
|
||||
ffi::lua_getiuservalue(state, -1, n as c_int);
|
||||
return V::from_lua(lua.pop_value(), lua.lua());
|
||||
}
|
||||
|
||||
// Multiple (extra) user values are emulated by storing them in a table
|
||||
protect_lua!(state, 1, 1, |state| {
|
||||
if getuservalue_table(state, -1) != ffi::LUA_TTABLE {
|
||||
ffi::lua_pushnil(state);
|
||||
return;
|
||||
}
|
||||
#[cfg(feature = "lua54")]
|
||||
ffi::lua_rawgeti(state, -1, (n - USER_VALUE_MAXSLOT + 1) as ffi::lua_Integer);
|
||||
#[cfg(not(feature = "lua54"))]
|
||||
ffi::lua_rawgeti(state, -1, n as ffi::lua_Integer);
|
||||
})?;
|
||||
if ffi::lua_getuservalue(state, -1) != ffi::LUA_TTABLE {
|
||||
return V::from_lua(Value::Nil, lua.lua());
|
||||
}
|
||||
ffi::lua_rawgeti(state, -1, n as ffi::lua_Integer);
|
||||
|
||||
V::from_lua(lua.pop_value(), lua.lua())
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[deprecated(since = "0.9.0", note = "please use `nth_user_value` instead")]
|
||||
pub fn get_nth_user_value<V: FromLua>(&self, n: usize) -> Result<V> {
|
||||
self.nth_user_value(n)
|
||||
}
|
||||
|
||||
/// Sets an associated value to this `AnyUserData` by name.
|
||||
/// Sets an associated value to this [`AnyUserData`] by name.
|
||||
///
|
||||
/// The value can be retrieved with [`named_user_value`].
|
||||
///
|
||||
/// [`named_user_value`]: #method.named_user_value
|
||||
/// [`named_user_value`]: AnyUserData::named_user_value
|
||||
pub fn set_named_user_value(&self, name: &str, v: impl IntoLua) -> Result<()> {
|
||||
let lua = self.0.lua.lock();
|
||||
let state = lua.state();
|
||||
@@ -851,15 +806,11 @@ impl AnyUserData {
|
||||
|
||||
// Multiple (extra) user values are emulated by storing them in a table
|
||||
protect_lua!(state, 2, 0, |state| {
|
||||
if getuservalue_table(state, -2) != ffi::LUA_TTABLE {
|
||||
if ffi::lua_getuservalue(state, -2) != ffi::LUA_TTABLE {
|
||||
// Create a new table to use as uservalue
|
||||
ffi::lua_pop(state, 1);
|
||||
ffi::lua_newtable(state);
|
||||
ffi::lua_pushvalue(state, -1);
|
||||
|
||||
#[cfg(feature = "lua54")]
|
||||
ffi::lua_setiuservalue(state, -4, USER_VALUE_MAXSLOT as c_int);
|
||||
#[cfg(not(feature = "lua54"))]
|
||||
ffi::lua_setuservalue(state, -4);
|
||||
}
|
||||
ffi::lua_pushlstring(state, name.as_ptr() as *const c_char, name.len());
|
||||
@@ -873,7 +824,7 @@ impl AnyUserData {
|
||||
|
||||
/// Returns an associated value by name set by [`set_named_user_value`].
|
||||
///
|
||||
/// [`set_named_user_value`]: #method.set_named_user_value
|
||||
/// [`set_named_user_value`]: AnyUserData::set_named_user_value
|
||||
pub fn named_user_value<V: FromLua>(&self, name: &str) -> Result<V> {
|
||||
let lua = self.0.lua.lock();
|
||||
let state = lua.state();
|
||||
@@ -884,39 +835,34 @@ impl AnyUserData {
|
||||
lua.push_userdata_ref(&self.0)?;
|
||||
|
||||
// Multiple (extra) user values are emulated by storing them in a table
|
||||
protect_lua!(state, 1, 1, |state| {
|
||||
if getuservalue_table(state, -1) != ffi::LUA_TTABLE {
|
||||
ffi::lua_pushnil(state);
|
||||
return;
|
||||
}
|
||||
ffi::lua_pushlstring(state, name.as_ptr() as *const c_char, name.len());
|
||||
ffi::lua_rawget(state, -2);
|
||||
})?;
|
||||
if ffi::lua_getuservalue(state, -1) != ffi::LUA_TTABLE {
|
||||
return V::from_lua(Value::Nil, lua.lua());
|
||||
}
|
||||
push_string(state, name.as_bytes(), !lua.unlikely_memory_error())?;
|
||||
ffi::lua_rawget(state, -2);
|
||||
|
||||
V::from_lua(lua.pop_value(), lua.lua())
|
||||
V::from_stack(-1, &lua)
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[deprecated(since = "0.9.0", note = "please use `named_user_value` instead")]
|
||||
pub fn get_named_user_value<V: FromLua>(&self, name: &str) -> Result<V> {
|
||||
self.named_user_value(name)
|
||||
}
|
||||
|
||||
/// Returns a metatable of this `UserData`.
|
||||
/// Returns a metatable of this [`AnyUserData`].
|
||||
///
|
||||
/// Returned [`UserDataMetatable`] object wraps the original metatable and
|
||||
/// provides safe access to its methods.
|
||||
///
|
||||
/// For `T: 'static` returned metatable is shared among all instances of type `T`.
|
||||
///
|
||||
/// [`UserDataMetatable`]: crate::UserDataMetatable
|
||||
#[inline]
|
||||
pub fn get_metatable(&self) -> Result<UserDataMetatable> {
|
||||
self.get_raw_metatable().map(UserDataMetatable)
|
||||
pub fn metatable(&self) -> Result<UserDataMetatable> {
|
||||
self.raw_metatable().map(UserDataMetatable)
|
||||
}
|
||||
|
||||
fn get_raw_metatable(&self) -> Result<Table> {
|
||||
#[doc(hidden)]
|
||||
#[deprecated(since = "0.10.0", note = "please use `metatable` instead")]
|
||||
pub fn get_metatable(&self) -> Result<UserDataMetatable> {
|
||||
self.metatable()
|
||||
}
|
||||
|
||||
fn raw_metatable(&self) -> Result<Table> {
|
||||
let lua = self.0.lua.lock();
|
||||
let state = lua.state();
|
||||
unsafe {
|
||||
@@ -941,14 +887,6 @@ impl AnyUserData {
|
||||
|
||||
/// Returns a type name of this `UserData` (from a metatable field).
|
||||
pub(crate) fn type_name(&self) -> Result<Option<StdString>> {
|
||||
match self.1 {
|
||||
SubtypeId::None => {}
|
||||
#[cfg(feature = "luau")]
|
||||
SubtypeId::Buffer => return Ok(Some("buffer".to_owned())),
|
||||
#[cfg(feature = "luajit")]
|
||||
SubtypeId::CData => return Ok(Some("cdata".to_owned())),
|
||||
}
|
||||
|
||||
let lua = self.0.lua.lock();
|
||||
let state = lua.state();
|
||||
unsafe {
|
||||
@@ -971,15 +909,14 @@ impl AnyUserData {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn equals<T: AsRef<Self>>(&self, other: T) -> Result<bool> {
|
||||
let other = other.as_ref();
|
||||
pub(crate) fn equals(&self, other: &Self) -> Result<bool> {
|
||||
// Uses lua_rawequal() under the hood
|
||||
if self == other {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
let mt = self.get_raw_metatable()?;
|
||||
if mt != other.get_raw_metatable()? {
|
||||
let mt = self.raw_metatable()?;
|
||||
if mt != other.raw_metatable()? {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
@@ -990,37 +927,32 @@ impl AnyUserData {
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
/// Returns `true` if this `AnyUserData` is serializable (eg. was created using
|
||||
/// `create_ser_userdata`).
|
||||
/// Returns `true` if this [`AnyUserData`] is serializable (e.g. was created using
|
||||
/// [`Lua::create_ser_userdata`]).
|
||||
#[cfg(feature = "serialize")]
|
||||
pub(crate) fn is_serializable(&self) -> bool {
|
||||
let lua = self.0.lua.lock();
|
||||
let is_serializable = || unsafe {
|
||||
// Userdata must be registered and not destructed
|
||||
let _ = lua.get_userdata_ref_type_id(&self.0)?;
|
||||
|
||||
let ud = &*get_userdata::<UserDataVariant<()>>(lua.ref_thread(), self.0.index);
|
||||
match ud {
|
||||
UserDataVariant::Serializable(..) => Result::Ok(true),
|
||||
_ => Result::Ok(false),
|
||||
}
|
||||
let ud = &*get_userdata::<UserDataStorage<()>>(lua.ref_thread(), self.0.index);
|
||||
Ok::<_, Error>((*ud).is_serializable())
|
||||
};
|
||||
is_serializable().unwrap_or(false)
|
||||
}
|
||||
|
||||
pub(crate) fn inspect<'a, T, F, R>(&'a self, func: F) -> Result<R>
|
||||
pub(crate) fn inspect<T, F, R>(&self, func: F) -> Result<R>
|
||||
where
|
||||
T: 'static,
|
||||
F: FnOnce(&'a UserDataVariant<T>, LuaGuard) -> Result<R>,
|
||||
F: FnOnce(&UserDataStorage<T>) -> Result<R>,
|
||||
{
|
||||
let lua = self.0.lua.lock();
|
||||
unsafe {
|
||||
let type_id = lua.get_userdata_ref_type_id(&self.0)?;
|
||||
match type_id {
|
||||
Some(type_id) if type_id == TypeId::of::<T>() => {
|
||||
let ref_thread = lua.ref_thread();
|
||||
let ud = get_userdata::<UserDataVariant<T>>(ref_thread, self.0.index);
|
||||
func(&*ud, lua)
|
||||
let ud = get_userdata::<UserDataStorage<T>>(lua.ref_thread(), self.0.index);
|
||||
func(&*ud)
|
||||
}
|
||||
_ => Err(Error::UserDataTypeMismatch),
|
||||
}
|
||||
@@ -1028,27 +960,7 @@ impl AnyUserData {
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for AnyUserData {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.0 == other.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<AnyUserData> for AnyUserData {
|
||||
#[inline]
|
||||
fn as_ref(&self) -> &Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn getuservalue_table(state: *mut ffi::lua_State, idx: c_int) -> c_int {
|
||||
#[cfg(feature = "lua54")]
|
||||
return ffi::lua_getiuservalue(state, idx, USER_VALUE_MAXSLOT as c_int);
|
||||
#[cfg(not(feature = "lua54"))]
|
||||
return ffi::lua_getuservalue(state, idx);
|
||||
}
|
||||
|
||||
/// Handle to a `UserData` metatable.
|
||||
/// Handle to a [`AnyUserData`] metatable.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct UserDataMetatable(pub(crate) Table);
|
||||
|
||||
@@ -1091,17 +1003,14 @@ impl UserDataMetatable {
|
||||
}
|
||||
}
|
||||
|
||||
/// An iterator over the pairs of a [`UserData`] metatable.
|
||||
/// An iterator over the pairs of a [`AnyUserData`] metatable.
|
||||
///
|
||||
/// It skips restricted metamethods, such as `__gc` or `__metatable`.
|
||||
///
|
||||
/// This struct is created by the [`UserDataMetatable::pairs`] method.
|
||||
///
|
||||
/// [`UserData`]: crate::UserData
|
||||
/// [`UserDataMetatable::pairs`]: crate::UserDataMetatable::method.pairs
|
||||
pub struct UserDataMetatablePairs<'a, V>(TablePairs<'a, StdString, V>);
|
||||
|
||||
impl<'a, V> Iterator for UserDataMetatablePairs<'a, V>
|
||||
impl<V> Iterator for UserDataMetatablePairs<'_, V>
|
||||
where
|
||||
V: FromLua,
|
||||
{
|
||||
@@ -1129,24 +1038,11 @@ impl Serialize for AnyUserData {
|
||||
S: Serializer,
|
||||
{
|
||||
let lua = self.0.lua.lock();
|
||||
|
||||
// Special case for Luau buffer type
|
||||
#[cfg(feature = "luau")]
|
||||
if self.1 == SubtypeId::Buffer {
|
||||
let buf = unsafe {
|
||||
let mut size = 0usize;
|
||||
let buf = ffi::lua_tobuffer(lua.ref_thread(), self.0.index, &mut size);
|
||||
mlua_assert!(!buf.is_null(), "invalid Luau buffer");
|
||||
std::slice::from_raw_parts(buf as *const u8, size)
|
||||
};
|
||||
return serializer.serialize_bytes(buf);
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let _ = lua
|
||||
.get_userdata_ref_type_id(&self.0)
|
||||
.map_err(ser::Error::custom)?;
|
||||
let ud = &*get_userdata::<UserDataVariant<()>>(lua.ref_thread(), self.0.index);
|
||||
let ud = &*get_userdata::<UserDataStorage<()>>(lua.ref_thread(), self.0.index);
|
||||
ud.serialize(serializer)
|
||||
}
|
||||
}
|
||||
@@ -1157,10 +1053,20 @@ pub(crate) struct WrappedUserdata<F: FnOnce(&Lua) -> Result<AnyUserData>>(F);
|
||||
impl AnyUserData {
|
||||
/// Wraps any Rust type, returning an opaque type that implements [`IntoLua`] trait.
|
||||
///
|
||||
/// This function uses [`Lua::create_any_userdata()`] under the hood.
|
||||
/// This function uses [`Lua::create_any_userdata`] under the hood.
|
||||
pub fn wrap<T: MaybeSend + 'static>(data: T) -> impl IntoLua {
|
||||
WrappedUserdata(move |lua| lua.create_any_userdata(data))
|
||||
}
|
||||
|
||||
/// Wraps any Rust type that implements [`Serialize`], returning an opaque type that implements
|
||||
/// [`IntoLua`] trait.
|
||||
///
|
||||
/// This function uses [`Lua::create_ser_any_userdata`] under the hood.
|
||||
#[cfg(feature = "serialize")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "serialize")))]
|
||||
pub fn wrap_ser<T: Serialize + MaybeSend + 'static>(data: T) -> impl IntoLua {
|
||||
WrappedUserdata(move |lua| lua.create_ser_any_userdata(data))
|
||||
}
|
||||
}
|
||||
|
||||
impl<F> IntoLua for WrappedUserdata<F>
|
||||
|
||||
+157
-38
@@ -1,5 +1,5 @@
|
||||
use std::any::{type_name, TypeId};
|
||||
use std::cell::UnsafeCell;
|
||||
use std::cell::{RefCell, UnsafeCell};
|
||||
use std::fmt;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::os::raw::c_int;
|
||||
@@ -9,10 +9,11 @@ use serde::ser::{Serialize, Serializer};
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::state::{Lua, RawLua};
|
||||
use crate::types::{MaybeSend, XRc};
|
||||
use crate::traits::FromLua;
|
||||
use crate::types::XRc;
|
||||
use crate::userdata::AnyUserData;
|
||||
use crate::util::get_userdata;
|
||||
use crate::value::{FromLua, Value};
|
||||
use crate::value::Value;
|
||||
|
||||
use super::lock::{RawLock, UserDataLock};
|
||||
|
||||
@@ -22,6 +23,11 @@ type DynSerialize = dyn erased_serde::Serialize;
|
||||
#[cfg(all(feature = "serialize", feature = "send"))]
|
||||
type DynSerialize = dyn erased_serde::Serialize + Send;
|
||||
|
||||
pub(crate) enum UserDataStorage<T> {
|
||||
Owned(UserDataVariant<T>),
|
||||
Scoped(ScopedUserDataVariant<T>),
|
||||
}
|
||||
|
||||
// A enum for storing userdata values.
|
||||
// It's stored inside a Lua VM and protected by the outer `ReentrantMutex`.
|
||||
pub(crate) enum UserDataVariant<T> {
|
||||
@@ -42,39 +48,34 @@ impl<T> Clone for UserDataVariant<T> {
|
||||
}
|
||||
|
||||
impl<T> UserDataVariant<T> {
|
||||
#[inline(always)]
|
||||
pub(crate) fn new(data: T) -> Self {
|
||||
Self::Default(XRc::new(UserDataCell::new(data)))
|
||||
}
|
||||
|
||||
// Immutably borrows the wrapped value in-place.
|
||||
#[inline(always)]
|
||||
pub(crate) fn try_borrow(&self) -> Result<UserDataBorrowRef<T>> {
|
||||
fn try_borrow(&self) -> Result<UserDataBorrowRef<T>> {
|
||||
UserDataBorrowRef::try_from(self)
|
||||
}
|
||||
|
||||
// Immutably borrows the wrapped value and returns an owned reference.
|
||||
#[inline(always)]
|
||||
pub(crate) fn try_borrow_owned(&self) -> Result<UserDataRef<T>> {
|
||||
fn try_borrow_owned(&self) -> Result<UserDataRef<T>> {
|
||||
UserDataRef::try_from(self.clone())
|
||||
}
|
||||
|
||||
// Mutably borrows the wrapped value in-place.
|
||||
#[inline(always)]
|
||||
pub(crate) fn try_borrow_mut(&self) -> Result<UserDataBorrowMut<T>> {
|
||||
fn try_borrow_mut(&self) -> Result<UserDataBorrowMut<T>> {
|
||||
UserDataBorrowMut::try_from(self)
|
||||
}
|
||||
|
||||
// Mutably borrows the wrapped value and returns an owned reference.
|
||||
#[inline(always)]
|
||||
pub(crate) fn try_borrow_owned_mut(&self) -> Result<UserDataRefMut<T>> {
|
||||
fn try_borrow_owned_mut(&self) -> Result<UserDataRefMut<T>> {
|
||||
UserDataRefMut::try_from(self.clone())
|
||||
}
|
||||
|
||||
// Returns the wrapped value.
|
||||
//
|
||||
// This method checks that we have exclusive access to the value.
|
||||
pub(crate) fn into_inner(self) -> Result<T> {
|
||||
fn into_inner(self) -> Result<T> {
|
||||
if !self.raw_lock().try_lock_exclusive() {
|
||||
return Err(Error::UserDataBorrowMutError);
|
||||
}
|
||||
@@ -108,20 +109,10 @@ impl<T> UserDataVariant<T> {
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl<T: Serialize + MaybeSend + 'static> UserDataVariant<T> {
|
||||
#[inline(always)]
|
||||
pub(crate) fn new_ser(data: T) -> Self {
|
||||
let data = Box::new(data) as Box<DynSerialize>;
|
||||
Self::Serializable(XRc::new(UserDataCell::new(data)))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl Serialize for UserDataVariant<()> {
|
||||
impl Serialize for UserDataStorage<()> {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
|
||||
match self {
|
||||
Self::Default(_) => Err(serde::ser::Error::custom("cannot serialize <userdata>")),
|
||||
Self::Serializable(inner) => unsafe {
|
||||
Self::Owned(UserDataVariant::Serializable(inner)) => unsafe {
|
||||
// We need to borrow the inner value exclusively to serialize it.
|
||||
#[cfg(feature = "send")]
|
||||
let _guard = self.try_borrow_mut().map_err(serde::ser::Error::custom)?;
|
||||
@@ -130,6 +121,7 @@ impl Serialize for UserDataVariant<()> {
|
||||
let _guard = self.try_borrow().map_err(serde::ser::Error::custom)?;
|
||||
(*inner.value.get()).serialize(serializer)
|
||||
},
|
||||
_ => Err(serde::ser::Error::custom("cannot serialize <userdata>")),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -145,7 +137,7 @@ unsafe impl<T: Send> Sync for UserDataCell<T> {}
|
||||
|
||||
impl<T> UserDataCell<T> {
|
||||
#[inline(always)]
|
||||
pub fn new(value: T) -> Self {
|
||||
fn new(value: T) -> Self {
|
||||
UserDataCell {
|
||||
raw_lock: RawLock::INIT,
|
||||
value: UnsafeCell::new(value),
|
||||
@@ -153,7 +145,7 @@ impl<T> UserDataCell<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A wrapper type for a [`UserData`] value that provides read access.
|
||||
/// A wrapper type for a userdata value that provides read access.
|
||||
///
|
||||
/// It implements [`FromLua`] and can be used to receive a typed userdata from Lua.
|
||||
pub struct UserDataRef<T>(UserDataVariant<T>);
|
||||
@@ -204,17 +196,17 @@ impl<T: 'static> FromLua for UserDataRef<T> {
|
||||
}
|
||||
|
||||
unsafe fn from_stack(idx: c_int, lua: &RawLua) -> Result<Self> {
|
||||
let type_id = lua.get_userdata_type_id(idx)?;
|
||||
let type_id = lua.get_userdata_type_id::<T>(idx)?;
|
||||
match type_id {
|
||||
Some(type_id) if type_id == TypeId::of::<T>() => {
|
||||
(*get_userdata::<UserDataVariant<T>>(lua.state(), idx)).try_borrow_owned()
|
||||
(*get_userdata::<UserDataStorage<T>>(lua.state(), idx)).try_borrow_owned()
|
||||
}
|
||||
_ => Err(Error::UserDataTypeMismatch),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A wrapper type for a mutably borrowed value from a `AnyUserData`.
|
||||
/// A wrapper type for a userdata value that provides read and write access.
|
||||
///
|
||||
/// It implements [`FromLua`] and can be used to receive a typed userdata from Lua.
|
||||
pub struct UserDataRefMut<T>(UserDataVariant<T>);
|
||||
@@ -272,10 +264,10 @@ impl<T: 'static> FromLua for UserDataRefMut<T> {
|
||||
}
|
||||
|
||||
unsafe fn from_stack(idx: c_int, lua: &RawLua) -> Result<Self> {
|
||||
let type_id = lua.get_userdata_type_id(idx)?;
|
||||
let type_id = lua.get_userdata_type_id::<T>(idx)?;
|
||||
match type_id {
|
||||
Some(type_id) if type_id == TypeId::of::<T>() => {
|
||||
(*get_userdata::<UserDataVariant<T>>(lua.state(), idx)).try_borrow_owned_mut()
|
||||
(*get_userdata::<UserDataStorage<T>>(lua.state(), idx)).try_borrow_owned_mut()
|
||||
}
|
||||
_ => Err(Error::UserDataTypeMismatch),
|
||||
}
|
||||
@@ -285,14 +277,14 @@ impl<T: 'static> FromLua for UserDataRefMut<T> {
|
||||
/// A type that provides read access to a userdata value (borrowing the value).
|
||||
pub(crate) struct UserDataBorrowRef<'a, T>(&'a UserDataVariant<T>);
|
||||
|
||||
impl<'a, T> Drop for UserDataBorrowRef<'a, T> {
|
||||
impl<T> Drop for UserDataBorrowRef<'_, T> {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
unsafe { self.0.raw_lock().unlock_shared() };
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> Deref for UserDataBorrowRef<'a, T> {
|
||||
impl<T> Deref for UserDataBorrowRef<'_, T> {
|
||||
type Target = T;
|
||||
|
||||
#[inline]
|
||||
@@ -316,14 +308,14 @@ impl<'a, T> TryFrom<&'a UserDataVariant<T>> for UserDataBorrowRef<'a, T> {
|
||||
|
||||
pub(crate) struct UserDataBorrowMut<'a, T>(&'a UserDataVariant<T>);
|
||||
|
||||
impl<'a, T> Drop for UserDataBorrowMut<'a, T> {
|
||||
impl<T> Drop for UserDataBorrowMut<'_, T> {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
unsafe { self.0.raw_lock().unlock_exclusive() };
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> Deref for UserDataBorrowMut<'a, T> {
|
||||
impl<T> Deref for UserDataBorrowMut<'_, T> {
|
||||
type Target = T;
|
||||
|
||||
#[inline]
|
||||
@@ -332,7 +324,7 @@ impl<'a, T> Deref for UserDataBorrowMut<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> DerefMut for UserDataBorrowMut<'a, T> {
|
||||
impl<T> DerefMut for UserDataBorrowMut<'_, T> {
|
||||
#[inline]
|
||||
fn deref_mut(&mut self) -> &mut T {
|
||||
unsafe { &mut *self.0.as_ptr() }
|
||||
@@ -357,12 +349,139 @@ fn try_value_to_userdata<T>(value: Value) -> Result<AnyUserData> {
|
||||
Value::UserData(ud) => Ok(ud),
|
||||
_ => Err(Error::FromLuaConversionError {
|
||||
from: value.type_name(),
|
||||
to: "userdata",
|
||||
to: "userdata".to_string(),
|
||||
message: Some(format!("expected userdata of type {}", type_name::<T>())),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) enum ScopedUserDataVariant<T> {
|
||||
Ref(*const T),
|
||||
RefMut(RefCell<*mut T>),
|
||||
Boxed(RefCell<*mut T>),
|
||||
}
|
||||
|
||||
impl<T> Drop for ScopedUserDataVariant<T> {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
if let Self::Boxed(value) = self {
|
||||
if let Ok(value) = value.try_borrow_mut() {
|
||||
unsafe { drop(Box::from_raw(*value)) };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> UserDataStorage<T> {
|
||||
#[inline(always)]
|
||||
pub(crate) fn new(data: T) -> Self {
|
||||
Self::Owned(UserDataVariant::Default(XRc::new(UserDataCell::new(data))))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn new_ref(data: &T) -> Self {
|
||||
Self::Scoped(ScopedUserDataVariant::Ref(data))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn new_ref_mut(data: &mut T) -> Self {
|
||||
Self::Scoped(ScopedUserDataVariant::RefMut(RefCell::new(data)))
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
#[inline(always)]
|
||||
pub(crate) fn new_ser(data: T) -> Self
|
||||
where
|
||||
T: Serialize + crate::types::MaybeSend,
|
||||
{
|
||||
let data = Box::new(data) as Box<DynSerialize>;
|
||||
Self::Owned(UserDataVariant::Serializable(XRc::new(UserDataCell::new(data))))
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
#[inline(always)]
|
||||
pub(crate) fn is_serializable(&self) -> bool {
|
||||
matches!(self, Self::Owned(UserDataVariant::Serializable(_)))
|
||||
}
|
||||
|
||||
// Immutably borrows the wrapped value and returns an owned reference.
|
||||
#[inline(always)]
|
||||
pub(crate) fn try_borrow_owned(&self) -> Result<UserDataRef<T>> {
|
||||
match self {
|
||||
Self::Owned(data) => data.try_borrow_owned(),
|
||||
Self::Scoped(_) => Err(Error::UserDataTypeMismatch),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
#[inline(always)]
|
||||
pub(crate) fn try_borrow(&self) -> Result<UserDataBorrowRef<T>> {
|
||||
match self {
|
||||
Self::Owned(data) => data.try_borrow(),
|
||||
Self::Scoped(_) => Err(Error::UserDataTypeMismatch),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn try_borrow_mut(&self) -> Result<UserDataBorrowMut<T>> {
|
||||
match self {
|
||||
Self::Owned(data) => data.try_borrow_mut(),
|
||||
Self::Scoped(_) => Err(Error::UserDataTypeMismatch),
|
||||
}
|
||||
}
|
||||
|
||||
// Mutably borrows the wrapped value and returns an owned reference.
|
||||
#[inline(always)]
|
||||
pub(crate) fn try_borrow_owned_mut(&self) -> Result<UserDataRefMut<T>> {
|
||||
match self {
|
||||
Self::Owned(data) => data.try_borrow_owned_mut(),
|
||||
Self::Scoped(_) => Err(Error::UserDataTypeMismatch),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn into_inner(self) -> Result<T> {
|
||||
match self {
|
||||
Self::Owned(data) => data.into_inner(),
|
||||
Self::Scoped(_) => Err(Error::UserDataTypeMismatch),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> UserDataStorage<T> {
|
||||
#[inline(always)]
|
||||
pub(crate) fn new_scoped(data: T) -> Self {
|
||||
let data = Box::into_raw(Box::new(data));
|
||||
Self::Scoped(ScopedUserDataVariant::Boxed(RefCell::new(data)))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn try_borrow_scoped<R>(&self, f: impl FnOnce(&T) -> R) -> Result<R> {
|
||||
match self {
|
||||
Self::Owned(data) => Ok(f(&*data.try_borrow()?)),
|
||||
Self::Scoped(ScopedUserDataVariant::Ref(value)) => Ok(f(unsafe { &**value })),
|
||||
Self::Scoped(ScopedUserDataVariant::RefMut(value) | ScopedUserDataVariant::Boxed(value)) => {
|
||||
let t = value.try_borrow().map_err(|_| Error::UserDataBorrowError)?;
|
||||
Ok(f(unsafe { &**t }))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn try_borrow_scoped_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> Result<R> {
|
||||
match self {
|
||||
Self::Owned(data) => Ok(f(&mut *data.try_borrow_mut()?)),
|
||||
Self::Scoped(ScopedUserDataVariant::Ref(_)) => Err(Error::UserDataBorrowMutError),
|
||||
Self::Scoped(ScopedUserDataVariant::RefMut(value) | ScopedUserDataVariant::Boxed(value)) => {
|
||||
let mut t = value
|
||||
.try_borrow_mut()
|
||||
.map_err(|_| Error::UserDataBorrowMutError)?;
|
||||
Ok(f(unsafe { &mut **t }))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod assertions {
|
||||
use super::*;
|
||||
|
||||
@@ -11,6 +11,7 @@ pub(crate) trait UserDataLock {
|
||||
pub(crate) use lock_impl::RawLock;
|
||||
|
||||
#[cfg(not(feature = "send"))]
|
||||
#[cfg(not(tarpaulin_include))]
|
||||
mod lock_impl {
|
||||
use std::cell::Cell;
|
||||
|
||||
|
||||
@@ -2,9 +2,9 @@ use std::string::String as StdString;
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::table::Table;
|
||||
use crate::traits::ObjectLike;
|
||||
use crate::traits::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, ObjectLike};
|
||||
use crate::userdata::AnyUserData;
|
||||
use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, Value};
|
||||
use crate::value::Value;
|
||||
use crate::Function;
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
@@ -88,6 +88,6 @@ impl ObjectLike for AnyUserData {
|
||||
|
||||
#[inline]
|
||||
fn to_string(&self) -> Result<StdString> {
|
||||
Value::UserData(AnyUserData(self.0.copy(), self.1)).to_string()
|
||||
Value::UserData(AnyUserData(self.0.copy())).to_string()
|
||||
}
|
||||
}
|
||||
|
||||
+124
-68
@@ -3,30 +3,34 @@
|
||||
use std::any::TypeId;
|
||||
use std::cell::RefCell;
|
||||
use std::marker::PhantomData;
|
||||
use std::os::raw::c_int;
|
||||
use std::os::raw::c_void;
|
||||
use std::string::String as StdString;
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::state::{Lua, RawLua};
|
||||
use crate::traits::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti};
|
||||
use crate::types::{Callback, MaybeSend};
|
||||
use crate::userdata::{
|
||||
AnyUserData, MetaMethod, UserData, UserDataFields, UserDataMethods, UserDataRef, UserDataRefMut,
|
||||
};
|
||||
use crate::userdata::{AnyUserData, MetaMethod, UserData, UserDataFields, UserDataMethods, UserDataStorage};
|
||||
use crate::util::{get_userdata, short_type_name};
|
||||
use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, Value};
|
||||
|
||||
use super::cell::{UserDataBorrowMut, UserDataBorrowRef, UserDataVariant};
|
||||
use crate::value::Value;
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
use {
|
||||
crate::types::AsyncCallback,
|
||||
crate::userdata::{UserDataRef, UserDataRefMut},
|
||||
std::future::{self, Future},
|
||||
};
|
||||
|
||||
type StaticFieldCallback = Box<dyn FnOnce(&RawLua) -> Result<()> + 'static>;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) enum UserDataTypeId {
|
||||
Shared(TypeId),
|
||||
Unique(usize),
|
||||
}
|
||||
|
||||
/// Handle to registry for userdata methods and metamethods.
|
||||
pub struct UserDataRegistry<T: 'static> {
|
||||
pub struct UserDataRegistry<T> {
|
||||
// Fields
|
||||
pub(crate) fields: Vec<(String, StaticFieldCallback)>,
|
||||
pub(crate) field_getters: Vec<(String, Callback)>,
|
||||
@@ -41,11 +45,13 @@ pub struct UserDataRegistry<T: 'static> {
|
||||
#[cfg(feature = "async")]
|
||||
pub(crate) async_meta_methods: Vec<(String, AsyncCallback)>,
|
||||
|
||||
pub(crate) type_id: UserDataTypeId,
|
||||
_type: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T: 'static> UserDataRegistry<T> {
|
||||
pub(crate) const fn new() -> Self {
|
||||
impl<T> UserDataRegistry<T> {
|
||||
#[inline]
|
||||
pub(crate) fn new(type_id: TypeId) -> Self {
|
||||
UserDataRegistry {
|
||||
fields: Vec::new(),
|
||||
field_getters: Vec::new(),
|
||||
@@ -57,11 +63,38 @@ impl<T: 'static> UserDataRegistry<T> {
|
||||
meta_methods: Vec::new(),
|
||||
#[cfg(feature = "async")]
|
||||
async_meta_methods: Vec::new(),
|
||||
type_id: UserDataTypeId::Shared(type_id),
|
||||
_type: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
fn box_method<M, A, R>(name: &str, method: M) -> Callback
|
||||
#[inline]
|
||||
pub(crate) fn new_unique(ud_ptr: *const c_void) -> Self {
|
||||
UserDataRegistry {
|
||||
fields: Vec::new(),
|
||||
field_getters: Vec::new(),
|
||||
field_setters: Vec::new(),
|
||||
meta_fields: Vec::new(),
|
||||
methods: Vec::new(),
|
||||
#[cfg(feature = "async")]
|
||||
async_methods: Vec::new(),
|
||||
meta_methods: Vec::new(),
|
||||
#[cfg(feature = "async")]
|
||||
async_meta_methods: Vec::new(),
|
||||
type_id: UserDataTypeId::Unique(ud_ptr as usize),
|
||||
_type: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn type_id(&self) -> Option<TypeId> {
|
||||
match self.type_id {
|
||||
UserDataTypeId::Shared(type_id) => Some(type_id),
|
||||
UserDataTypeId::Unique(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn box_method<M, A, R>(&self, name: &str, method: M) -> Callback
|
||||
where
|
||||
M: Fn(&Lua, &T, A) -> Result<R> + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
@@ -74,6 +107,7 @@ impl<T: 'static> UserDataRegistry<T> {
|
||||
};
|
||||
}
|
||||
|
||||
let target_type_id = self.type_id;
|
||||
Box::new(move |rawlua, nargs| unsafe {
|
||||
if nargs == 0 {
|
||||
let err = Error::from_lua_conversion("missing argument", "userdata", None);
|
||||
@@ -85,17 +119,34 @@ impl<T: 'static> UserDataRegistry<T> {
|
||||
// Self was at position 1, so we pass 2 here
|
||||
let args = A::from_stack_args(nargs - 1, 2, Some(&name), rawlua);
|
||||
|
||||
match try_self_arg!(rawlua.get_userdata_type_id(self_index)) {
|
||||
Some(id) if id == TypeId::of::<T>() => {
|
||||
let ud = try_self_arg!(borrow_userdata_ref::<T>(state, self_index));
|
||||
method(rawlua.lua(), &ud, args?)?.push_into_stack_multi(rawlua)
|
||||
match target_type_id {
|
||||
// This branch is for `'static` userdata that share type metatable
|
||||
UserDataTypeId::Shared(target_type_id) => {
|
||||
match try_self_arg!(rawlua.get_userdata_type_id::<T>(self_index)) {
|
||||
Some(self_type_id) if self_type_id == target_type_id => {
|
||||
let ud = get_userdata::<UserDataStorage<T>>(state, self_index);
|
||||
try_self_arg!((*ud).try_borrow_scoped(|ud| {
|
||||
method(rawlua.lua(), ud, args?)?.push_into_stack_multi(rawlua)
|
||||
}))
|
||||
}
|
||||
_ => Err(Error::bad_self_argument(&name, Error::UserDataTypeMismatch)),
|
||||
}
|
||||
}
|
||||
UserDataTypeId::Unique(target_ptr) => {
|
||||
match get_userdata::<UserDataStorage<T>>(state, self_index) {
|
||||
ud if ud as usize == target_ptr => {
|
||||
try_self_arg!((*ud).try_borrow_scoped(|ud| {
|
||||
method(rawlua.lua(), ud, args?)?.push_into_stack_multi(rawlua)
|
||||
}))
|
||||
}
|
||||
_ => Err(Error::bad_self_argument(&name, Error::UserDataTypeMismatch)),
|
||||
}
|
||||
}
|
||||
_ => Err(Error::bad_self_argument(&name, Error::UserDataTypeMismatch)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn box_method_mut<M, A, R>(name: &str, method: M) -> Callback
|
||||
fn box_method_mut<M, A, R>(&self, name: &str, method: M) -> Callback
|
||||
where
|
||||
M: FnMut(&Lua, &mut T, A) -> Result<R> + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
@@ -109,6 +160,7 @@ impl<T: 'static> UserDataRegistry<T> {
|
||||
}
|
||||
|
||||
let method = RefCell::new(method);
|
||||
let target_type_id = self.type_id;
|
||||
Box::new(move |rawlua, nargs| unsafe {
|
||||
let mut method = method.try_borrow_mut().map_err(|_| Error::RecursiveMutCallback)?;
|
||||
if nargs == 0 {
|
||||
@@ -121,19 +173,37 @@ impl<T: 'static> UserDataRegistry<T> {
|
||||
// Self was at position 1, so we pass 2 here
|
||||
let args = A::from_stack_args(nargs - 1, 2, Some(&name), rawlua);
|
||||
|
||||
match try_self_arg!(rawlua.get_userdata_type_id(self_index)) {
|
||||
Some(id) if id == TypeId::of::<T>() => {
|
||||
let mut ud = try_self_arg!(borrow_userdata_mut::<T>(state, self_index));
|
||||
method(rawlua.lua(), &mut ud, args?)?.push_into_stack_multi(rawlua)
|
||||
match target_type_id {
|
||||
// This branch is for `'static` userdata that share type metatable
|
||||
UserDataTypeId::Shared(target_type_id) => {
|
||||
match try_self_arg!(rawlua.get_userdata_type_id::<T>(self_index)) {
|
||||
Some(self_type_id) if self_type_id == target_type_id => {
|
||||
let ud = get_userdata::<UserDataStorage<T>>(state, self_index);
|
||||
try_self_arg!((*ud).try_borrow_scoped_mut(|ud| {
|
||||
method(rawlua.lua(), ud, args?)?.push_into_stack_multi(rawlua)
|
||||
}))
|
||||
}
|
||||
_ => Err(Error::bad_self_argument(&name, Error::UserDataTypeMismatch)),
|
||||
}
|
||||
}
|
||||
UserDataTypeId::Unique(target_ptr) => {
|
||||
match get_userdata::<UserDataStorage<T>>(state, self_index) {
|
||||
ud if ud as usize == target_ptr => {
|
||||
try_self_arg!((*ud).try_borrow_scoped_mut(|ud| {
|
||||
method(rawlua.lua(), ud, args?)?.push_into_stack_multi(rawlua)
|
||||
}))
|
||||
}
|
||||
_ => Err(Error::bad_self_argument(&name, Error::UserDataTypeMismatch)),
|
||||
}
|
||||
}
|
||||
_ => Err(Error::bad_self_argument(&name, Error::UserDataTypeMismatch)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
fn box_async_method<M, A, MR, R>(name: &str, method: M) -> AsyncCallback
|
||||
fn box_async_method<M, A, MR, R>(&self, name: &str, method: M) -> AsyncCallback
|
||||
where
|
||||
T: 'static,
|
||||
M: Fn(Lua, UserDataRef<T>, A) -> MR + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
MR: Future<Output = Result<R>> + MaybeSend + 'static,
|
||||
@@ -171,8 +241,9 @@ impl<T: 'static> UserDataRegistry<T> {
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
fn box_async_method_mut<M, A, MR, R>(name: &str, method: M) -> AsyncCallback
|
||||
fn box_async_method_mut<M, A, MR, R>(&self, name: &str, method: M) -> AsyncCallback
|
||||
where
|
||||
T: 'static,
|
||||
M: Fn(Lua, UserDataRefMut<T>, A) -> MR + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
MR: Future<Output = Result<R>> + MaybeSend + 'static,
|
||||
@@ -209,7 +280,7 @@ impl<T: 'static> UserDataRegistry<T> {
|
||||
})
|
||||
}
|
||||
|
||||
fn box_function<F, A, R>(name: &str, function: F) -> Callback
|
||||
fn box_function<F, A, R>(&self, name: &str, function: F) -> Callback
|
||||
where
|
||||
F: Fn(&Lua, A) -> Result<R> + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
@@ -222,7 +293,7 @@ impl<T: 'static> UserDataRegistry<T> {
|
||||
})
|
||||
}
|
||||
|
||||
fn box_function_mut<F, A, R>(name: &str, function: F) -> Callback
|
||||
fn box_function_mut<F, A, R>(&self, name: &str, function: F) -> Callback
|
||||
where
|
||||
F: FnMut(&Lua, A) -> Result<R> + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
@@ -240,7 +311,7 @@ impl<T: 'static> UserDataRegistry<T> {
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
fn box_async_function<F, A, FR, R>(name: &str, function: F) -> AsyncCallback
|
||||
fn box_async_function<F, A, FR, R>(&self, name: &str, function: F) -> AsyncCallback
|
||||
where
|
||||
F: Fn(Lua, A) -> FR + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
@@ -282,7 +353,7 @@ fn get_function_name<T>(name: &str) -> StdString {
|
||||
format!("{}.{name}", short_type_name::<T>())
|
||||
}
|
||||
|
||||
impl<T: 'static> UserDataFields<T> for UserDataRegistry<T> {
|
||||
impl<T> UserDataFields<T> for UserDataRegistry<T> {
|
||||
fn add_field<V>(&mut self, name: impl ToString, value: V)
|
||||
where
|
||||
V: IntoLua + 'static,
|
||||
@@ -300,7 +371,7 @@ impl<T: 'static> UserDataFields<T> for UserDataRegistry<T> {
|
||||
R: IntoLua,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_method(&name, move |lua, data, ()| method(lua, data));
|
||||
let callback = self.box_method(&name, move |lua, data, ()| method(lua, data));
|
||||
self.field_getters.push((name, callback));
|
||||
}
|
||||
|
||||
@@ -310,7 +381,7 @@ impl<T: 'static> UserDataFields<T> for UserDataRegistry<T> {
|
||||
A: FromLua,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_method_mut(&name, method);
|
||||
let callback = self.box_method_mut(&name, method);
|
||||
self.field_setters.push((name, callback));
|
||||
}
|
||||
|
||||
@@ -320,7 +391,7 @@ impl<T: 'static> UserDataFields<T> for UserDataRegistry<T> {
|
||||
R: IntoLua,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_function(&name, function);
|
||||
let callback = self.box_function(&name, function);
|
||||
self.field_getters.push((name, callback));
|
||||
}
|
||||
|
||||
@@ -330,7 +401,7 @@ impl<T: 'static> UserDataFields<T> for UserDataRegistry<T> {
|
||||
A: FromLua,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_function_mut(&name, move |lua, (data, val)| function(lua, data, val));
|
||||
let callback = self.box_function_mut(&name, move |lua, (data, val)| function(lua, data, val));
|
||||
self.field_setters.push((name, callback));
|
||||
}
|
||||
|
||||
@@ -363,7 +434,7 @@ impl<T: 'static> UserDataFields<T> for UserDataRegistry<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> UserDataMethods<T> for UserDataRegistry<T> {
|
||||
impl<T> UserDataMethods<T> for UserDataRegistry<T> {
|
||||
fn add_method<M, A, R>(&mut self, name: impl ToString, method: M)
|
||||
where
|
||||
M: Fn(&Lua, &T, A) -> Result<R> + MaybeSend + 'static,
|
||||
@@ -371,7 +442,7 @@ impl<T: 'static> UserDataMethods<T> for UserDataRegistry<T> {
|
||||
R: IntoLuaMulti,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_method(&name, method);
|
||||
let callback = self.box_method(&name, method);
|
||||
self.methods.push((name, callback));
|
||||
}
|
||||
|
||||
@@ -382,33 +453,35 @@ impl<T: 'static> UserDataMethods<T> for UserDataRegistry<T> {
|
||||
R: IntoLuaMulti,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_method_mut(&name, method);
|
||||
let callback = self.box_method_mut(&name, method);
|
||||
self.methods.push((name, callback));
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
fn add_async_method<M, A, MR, R>(&mut self, name: impl ToString, method: M)
|
||||
where
|
||||
T: 'static,
|
||||
M: Fn(Lua, UserDataRef<T>, A) -> MR + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
MR: Future<Output = Result<R>> + MaybeSend + 'static,
|
||||
R: IntoLuaMulti,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_async_method(&name, method);
|
||||
let callback = self.box_async_method(&name, method);
|
||||
self.async_methods.push((name, callback));
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
fn add_async_method_mut<M, A, MR, R>(&mut self, name: impl ToString, method: M)
|
||||
where
|
||||
T: 'static,
|
||||
M: Fn(Lua, UserDataRefMut<T>, A) -> MR + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
MR: Future<Output = Result<R>> + MaybeSend + 'static,
|
||||
R: IntoLuaMulti,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_async_method_mut(&name, method);
|
||||
let callback = self.box_async_method_mut(&name, method);
|
||||
self.async_methods.push((name, callback));
|
||||
}
|
||||
|
||||
@@ -419,7 +492,7 @@ impl<T: 'static> UserDataMethods<T> for UserDataRegistry<T> {
|
||||
R: IntoLuaMulti,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_function(&name, function);
|
||||
let callback = self.box_function(&name, function);
|
||||
self.methods.push((name, callback));
|
||||
}
|
||||
|
||||
@@ -430,7 +503,7 @@ impl<T: 'static> UserDataMethods<T> for UserDataRegistry<T> {
|
||||
R: IntoLuaMulti,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_function_mut(&name, function);
|
||||
let callback = self.box_function_mut(&name, function);
|
||||
self.methods.push((name, callback));
|
||||
}
|
||||
|
||||
@@ -443,7 +516,7 @@ impl<T: 'static> UserDataMethods<T> for UserDataRegistry<T> {
|
||||
R: IntoLuaMulti,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_async_function(&name, function);
|
||||
let callback = self.box_async_function(&name, function);
|
||||
self.async_methods.push((name, callback));
|
||||
}
|
||||
|
||||
@@ -454,7 +527,7 @@ impl<T: 'static> UserDataMethods<T> for UserDataRegistry<T> {
|
||||
R: IntoLuaMulti,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_method(&name, method);
|
||||
let callback = self.box_method(&name, method);
|
||||
self.meta_methods.push((name, callback));
|
||||
}
|
||||
|
||||
@@ -465,33 +538,35 @@ impl<T: 'static> UserDataMethods<T> for UserDataRegistry<T> {
|
||||
R: IntoLuaMulti,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_method_mut(&name, method);
|
||||
let callback = self.box_method_mut(&name, method);
|
||||
self.meta_methods.push((name, callback));
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau"))))]
|
||||
fn add_async_meta_method<M, A, MR, R>(&mut self, name: impl ToString, method: M)
|
||||
where
|
||||
T: 'static,
|
||||
M: Fn(Lua, UserDataRef<T>, A) -> MR + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
MR: Future<Output = Result<R>> + MaybeSend + 'static,
|
||||
R: IntoLuaMulti,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_async_method(&name, method);
|
||||
let callback = self.box_async_method(&name, method);
|
||||
self.async_meta_methods.push((name, callback));
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "async", not(any(feature = "lua51", feature = "luau"))))]
|
||||
fn add_async_meta_method_mut<M, A, MR, R>(&mut self, name: impl ToString, method: M)
|
||||
where
|
||||
T: 'static,
|
||||
M: Fn(Lua, UserDataRefMut<T>, A) -> MR + MaybeSend + 'static,
|
||||
A: FromLuaMulti,
|
||||
MR: Future<Output = Result<R>> + MaybeSend + 'static,
|
||||
R: IntoLuaMulti,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_async_method_mut(&name, method);
|
||||
let callback = self.box_async_method_mut(&name, method);
|
||||
self.async_meta_methods.push((name, callback));
|
||||
}
|
||||
|
||||
@@ -502,7 +577,7 @@ impl<T: 'static> UserDataMethods<T> for UserDataRegistry<T> {
|
||||
R: IntoLuaMulti,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_function(&name, function);
|
||||
let callback = self.box_function(&name, function);
|
||||
self.meta_methods.push((name, callback));
|
||||
}
|
||||
|
||||
@@ -513,7 +588,7 @@ impl<T: 'static> UserDataMethods<T> for UserDataRegistry<T> {
|
||||
R: IntoLuaMulti,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_function_mut(&name, function);
|
||||
let callback = self.box_function_mut(&name, function);
|
||||
self.meta_methods.push((name, callback));
|
||||
}
|
||||
|
||||
@@ -526,36 +601,17 @@ impl<T: 'static> UserDataMethods<T> for UserDataRegistry<T> {
|
||||
R: IntoLuaMulti,
|
||||
{
|
||||
let name = name.to_string();
|
||||
let callback = Self::box_async_function(&name, function);
|
||||
let callback = self.box_async_function(&name, function);
|
||||
self.async_meta_methods.push((name, callback));
|
||||
}
|
||||
}
|
||||
|
||||
// Borrow the userdata in-place from the Lua stack
|
||||
#[inline(always)]
|
||||
unsafe fn borrow_userdata_ref<'a, T>(
|
||||
state: *mut ffi::lua_State,
|
||||
index: c_int,
|
||||
) -> Result<UserDataBorrowRef<'a, T>> {
|
||||
let ud = get_userdata::<UserDataVariant<T>>(state, index);
|
||||
(*ud).try_borrow()
|
||||
}
|
||||
|
||||
// Borrow the userdata mutably in-place from the Lua stack
|
||||
#[inline(always)]
|
||||
unsafe fn borrow_userdata_mut<'a, T>(
|
||||
state: *mut ffi::lua_State,
|
||||
index: c_int,
|
||||
) -> Result<UserDataBorrowMut<'a, T>> {
|
||||
let ud = get_userdata::<UserDataVariant<T>>(state, index);
|
||||
(*ud).try_borrow_mut()
|
||||
}
|
||||
|
||||
macro_rules! lua_userdata_impl {
|
||||
($type:ty) => {
|
||||
impl<T: UserData + 'static> UserData for $type {
|
||||
fn register(registry: &mut UserDataRegistry<Self>) {
|
||||
let mut orig_registry = UserDataRegistry::new();
|
||||
let type_id = TypeId::of::<T>();
|
||||
let mut orig_registry = UserDataRegistry::new(type_id);
|
||||
T::register(&mut orig_registry);
|
||||
|
||||
// Copy all fields, methods, etc. from the original registry
|
||||
|
||||
+7
-6
@@ -204,24 +204,25 @@ pub(crate) unsafe fn protect_lua_closure<F, R>(
|
||||
f: F,
|
||||
) -> Result<R>
|
||||
where
|
||||
F: Fn(*mut ffi::lua_State) -> R,
|
||||
F: FnOnce(*mut ffi::lua_State) -> R,
|
||||
R: Copy,
|
||||
{
|
||||
struct Params<F, R: Copy> {
|
||||
function: F,
|
||||
function: Option<F>,
|
||||
result: MaybeUninit<R>,
|
||||
nresults: c_int,
|
||||
}
|
||||
|
||||
unsafe extern "C-unwind" fn do_call<F, R>(state: *mut ffi::lua_State) -> c_int
|
||||
where
|
||||
F: Fn(*mut ffi::lua_State) -> R,
|
||||
F: FnOnce(*mut ffi::lua_State) -> R,
|
||||
R: Copy,
|
||||
{
|
||||
let params = ffi::lua_touserdata(state, -1) as *mut Params<F, R>;
|
||||
ffi::lua_pop(state, 1);
|
||||
|
||||
(*params).result.write(((*params).function)(state));
|
||||
let f = (*params).function.take().unwrap();
|
||||
(*params).result.write(f(state));
|
||||
|
||||
if (*params).nresults == ffi::LUA_MULTRET {
|
||||
ffi::lua_gettop(state)
|
||||
@@ -241,7 +242,7 @@ where
|
||||
}
|
||||
|
||||
let mut params = Params {
|
||||
function: f,
|
||||
function: Some(f),
|
||||
result: MaybeUninit::uninit(),
|
||||
nresults,
|
||||
};
|
||||
@@ -365,7 +366,7 @@ pub(crate) unsafe fn init_error_registry(state: *mut ffi::lua_State) -> Result<(
|
||||
// Create destructed userdata metatable
|
||||
|
||||
unsafe extern "C-unwind" fn destructed_error(state: *mut ffi::lua_State) -> c_int {
|
||||
callback_error(state, |_| Err(Error::CallbackDestructed))
|
||||
callback_error(state, |_| Err(Error::UserDataDestructed))
|
||||
}
|
||||
|
||||
push_table(state, 0, 26, true)?;
|
||||
|
||||
+23
-7
@@ -1,6 +1,6 @@
|
||||
use std::borrow::Cow;
|
||||
use std::ffi::CStr;
|
||||
use std::os::raw::{c_char, c_int};
|
||||
use std::os::raw::{c_char, c_int, c_void};
|
||||
use std::{ptr, slice, str};
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
@@ -17,10 +17,9 @@ pub(crate) use userdata::{
|
||||
DESTRUCTED_USERDATA_METATABLE,
|
||||
};
|
||||
|
||||
#[cfg(not(feature = "lua54"))]
|
||||
#[cfg(not(feature = "luau"))]
|
||||
pub(crate) use userdata::push_uninit_userdata;
|
||||
pub(crate) use userdata::push_userdata;
|
||||
#[cfg(feature = "lua54")]
|
||||
pub(crate) use userdata::push_userdata_uv;
|
||||
|
||||
#[cfg(not(feature = "luau"))]
|
||||
pub(crate) use userdata::userdata_destructor;
|
||||
@@ -70,6 +69,7 @@ impl StackGuard {
|
||||
}
|
||||
|
||||
impl Drop for StackGuard {
|
||||
#[track_caller]
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
let top = ffi::lua_gettop(self.state);
|
||||
@@ -275,9 +275,25 @@ pub(crate) unsafe fn to_string(state: *mut ffi::lua_State, index: c_int) -> Stri
|
||||
ffi::LUA_TTHREAD => format!("<thread {:?}>", ffi::lua_topointer(state, index)),
|
||||
#[cfg(feature = "luau")]
|
||||
ffi::LUA_TBUFFER => format!("<buffer {:?}>", ffi::lua_topointer(state, index)),
|
||||
#[cfg(feature = "luajit")]
|
||||
ffi::LUA_TCDATA => format!("<cdata {:?}>", ffi::lua_topointer(state, index)),
|
||||
_ => "<unknown>".to_string(),
|
||||
type_id => {
|
||||
let type_name = CStr::from_ptr(ffi::lua_typename(state, type_id)).to_string_lossy();
|
||||
format!("<{type_name} {:?}>", ffi::lua_topointer(state, index))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn get_metatable_ptr(state: *mut ffi::lua_State, index: c_int) -> *const c_void {
|
||||
#[cfg(feature = "luau")]
|
||||
return ffi::lua_getmetatablepointer(state, index);
|
||||
|
||||
#[cfg(not(feature = "luau"))]
|
||||
if ffi::lua_getmetatable(state, index) == 0 {
|
||||
ptr::null()
|
||||
} else {
|
||||
let p = ffi::lua_topointer(state, -1);
|
||||
ffi::lua_pop(state, 1);
|
||||
p
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+27
-44
@@ -3,7 +3,7 @@ use std::os::raw::{c_int, c_void};
|
||||
use std::{ptr, str};
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::util::{check_stack, push_string, push_table, rawset_field, TypeKey};
|
||||
use crate::util::{check_stack, get_metatable_ptr, push_string, push_table, rawset_field, TypeKey};
|
||||
|
||||
// Pushes the userdata and attaches a metatable with __gc method.
|
||||
// Internally uses 3 stack spaces, does not call checkstack.
|
||||
@@ -58,71 +58,54 @@ pub(crate) unsafe fn init_internal_metatable<T: TypeKey>(
|
||||
pub(crate) unsafe fn get_internal_userdata<T: TypeKey>(
|
||||
state: *mut ffi::lua_State,
|
||||
index: c_int,
|
||||
type_mt_ptr: *const c_void,
|
||||
mut type_mt_ptr: *const c_void,
|
||||
) -> *mut T {
|
||||
let ud = ffi::lua_touserdata(state, index) as *mut T;
|
||||
if ud.is_null() || ffi::lua_getmetatable(state, index) == 0 {
|
||||
if ud.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
if !type_mt_ptr.is_null() {
|
||||
let ud_mt_ptr = ffi::lua_topointer(state, -1);
|
||||
ffi::lua_pop(state, 1);
|
||||
if ud_mt_ptr != type_mt_ptr {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
} else {
|
||||
let mt_ptr = get_metatable_ptr(state, index);
|
||||
if type_mt_ptr.is_null() {
|
||||
get_internal_metatable::<T>(state);
|
||||
let res = ffi::lua_rawequal(state, -1, -2);
|
||||
ffi::lua_pop(state, 2);
|
||||
if res == 0 {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
type_mt_ptr = ffi::lua_topointer(state, -1);
|
||||
ffi::lua_pop(state, 1);
|
||||
}
|
||||
if mt_ptr != type_mt_ptr {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
ud
|
||||
}
|
||||
|
||||
// Internally uses 3 stack spaces, does not call checkstack.
|
||||
#[inline]
|
||||
pub(crate) unsafe fn push_userdata<T>(state: *mut ffi::lua_State, t: T, protect: bool) -> Result<()> {
|
||||
#[cfg(not(feature = "luau"))]
|
||||
let ud = if protect {
|
||||
#[cfg(not(feature = "luau"))]
|
||||
pub(crate) unsafe fn push_uninit_userdata<T>(state: *mut ffi::lua_State, protect: bool) -> Result<*mut T> {
|
||||
if protect {
|
||||
protect_lua!(state, 0, 1, |state| {
|
||||
ffi::lua_newuserdata(state, std::mem::size_of::<T>()) as *mut T
|
||||
})?
|
||||
})
|
||||
} else {
|
||||
ffi::lua_newuserdata(state, std::mem::size_of::<T>()) as *mut T
|
||||
};
|
||||
Ok(ffi::lua_newuserdata(state, std::mem::size_of::<T>()) as *mut T)
|
||||
}
|
||||
}
|
||||
|
||||
// Internally uses 3 stack spaces, does not call checkstack.
|
||||
#[inline]
|
||||
pub(crate) unsafe fn push_userdata<T>(state: *mut ffi::lua_State, t: T, protect: bool) -> Result<*mut T> {
|
||||
#[cfg(not(feature = "luau"))]
|
||||
let ud_ptr = push_uninit_userdata(state, protect)?;
|
||||
#[cfg(feature = "luau")]
|
||||
let ud = if protect {
|
||||
let ud_ptr = if protect {
|
||||
protect_lua!(state, 0, 1, |state| { ffi::lua_newuserdata_t::<T>(state) })?
|
||||
} else {
|
||||
ffi::lua_newuserdata_t::<T>(state)
|
||||
};
|
||||
ptr::write(ud, t);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Internally uses 3 stack spaces, does not call checkstack.
|
||||
#[cfg(feature = "lua54")]
|
||||
#[inline]
|
||||
pub(crate) unsafe fn push_userdata_uv<T>(
|
||||
state: *mut ffi::lua_State,
|
||||
t: T,
|
||||
nuvalue: c_int,
|
||||
protect: bool,
|
||||
) -> Result<()> {
|
||||
let ud = if protect {
|
||||
protect_lua!(state, 0, 1, |state| {
|
||||
ffi::lua_newuserdatauv(state, std::mem::size_of::<T>(), nuvalue) as *mut T
|
||||
})?
|
||||
} else {
|
||||
ffi::lua_newuserdatauv(state, std::mem::size_of::<T>(), nuvalue) as *mut T
|
||||
};
|
||||
ptr::write(ud, t);
|
||||
Ok(())
|
||||
ptr::write(ud_ptr, t);
|
||||
Ok(ud_ptr)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[track_caller]
|
||||
pub(crate) unsafe fn get_userdata<T>(state: *mut ffi::lua_State, index: c_int) -> *mut T {
|
||||
let ud = ffi::lua_touserdata(state, index) as *mut T;
|
||||
mlua_debug_assert!(!ud.is_null(), "userdata pointer is null");
|
||||
|
||||
+119
-281
@@ -1,21 +1,17 @@
|
||||
use std::cell::RefCell;
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::{vec_deque, HashSet, VecDeque};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::os::raw::{c_int, c_void};
|
||||
use std::collections::HashSet;
|
||||
use std::os::raw::c_void;
|
||||
use std::string::String as StdString;
|
||||
use std::sync::Arc;
|
||||
use std::{fmt, mem, ptr, str};
|
||||
use std::{fmt, ptr, str};
|
||||
|
||||
use num_traits::FromPrimitive;
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::function::Function;
|
||||
use crate::state::{Lua, RawLua};
|
||||
use crate::string::{BorrowedStr, String};
|
||||
use crate::table::Table;
|
||||
use crate::thread::Thread;
|
||||
use crate::types::{Integer, LightUserData, Number, SubtypeId};
|
||||
use crate::types::{Integer, LightUserData, Number, ValueRef};
|
||||
use crate::userdata::AnyUserData;
|
||||
use crate::util::{check_stack, StackGuard};
|
||||
|
||||
@@ -24,12 +20,14 @@ use {
|
||||
crate::table::SerializableTable,
|
||||
rustc_hash::FxHashSet,
|
||||
serde::ser::{self, Serialize, Serializer},
|
||||
std::{rc::Rc, result::Result as StdResult},
|
||||
std::{cell::RefCell, rc::Rc, result::Result as StdResult},
|
||||
};
|
||||
|
||||
/// A dynamically typed Lua value. The `String`, `Table`, `Function`, `Thread`, and `UserData`
|
||||
/// variants contain handle types into the internal Lua state. It is a logic error to mix handle
|
||||
/// types between separate `Lua` instances, and doing so will result in a panic.
|
||||
/// A dynamically typed Lua value.
|
||||
///
|
||||
/// The non-primitive variants (eg. string/table/function/thread/userdata) contain handle types
|
||||
/// into the internal Lua state. It is a logic error to mix handle types between separate
|
||||
/// `Lua` instances, and doing so will result in a panic.
|
||||
#[derive(Clone)]
|
||||
pub enum Value {
|
||||
/// The Lua value `nil`.
|
||||
@@ -47,7 +45,7 @@ pub enum Value {
|
||||
/// A Luau vector.
|
||||
#[cfg(any(feature = "luau", doc))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
|
||||
Vector(crate::types::Vector),
|
||||
Vector(crate::Vector),
|
||||
/// An interned string, managed by Lua.
|
||||
///
|
||||
/// Unlike Rust strings, Lua strings may not be valid UTF-8.
|
||||
@@ -59,10 +57,18 @@ pub enum Value {
|
||||
/// Reference to a Lua thread (or coroutine).
|
||||
Thread(Thread),
|
||||
/// Reference to a userdata object that holds a custom type which implements `UserData`.
|
||||
///
|
||||
/// Special builtin userdata types will be represented as other `Value` variants.
|
||||
UserData(AnyUserData),
|
||||
/// A Luau buffer.
|
||||
#[cfg(any(feature = "luau", doc))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
|
||||
Buffer(crate::Buffer),
|
||||
/// `Error` is a special builtin userdata type. When received from Lua it is implicitly cloned.
|
||||
Error(Box<Error>),
|
||||
/// Any other value not known to mlua (eg. LuaJIT CData).
|
||||
#[allow(private_interfaces)]
|
||||
Other(ValueRef),
|
||||
}
|
||||
|
||||
pub use self::Value::Nil;
|
||||
@@ -74,7 +80,7 @@ impl Value {
|
||||
pub const NULL: Value = Value::LightUserData(LightUserData(ptr::null_mut()));
|
||||
|
||||
/// Returns type name of this value.
|
||||
pub const fn type_name(&self) -> &'static str {
|
||||
pub fn type_name(&self) -> &'static str {
|
||||
match *self {
|
||||
Value::Nil => "nil",
|
||||
Value::Boolean(_) => "boolean",
|
||||
@@ -87,27 +93,26 @@ impl Value {
|
||||
Value::Table(_) => "table",
|
||||
Value::Function(_) => "function",
|
||||
Value::Thread(_) => "thread",
|
||||
Value::UserData(AnyUserData(_, SubtypeId::None)) => "userdata",
|
||||
Value::UserData(_) => "userdata",
|
||||
#[cfg(feature = "luau")]
|
||||
Value::UserData(AnyUserData(_, SubtypeId::Buffer)) => "buffer",
|
||||
#[cfg(feature = "luajit")]
|
||||
Value::UserData(AnyUserData(_, SubtypeId::CData)) => "cdata",
|
||||
Value::Buffer(_) => "buffer",
|
||||
Value::Error(_) => "error",
|
||||
Value::Other(_) => "other",
|
||||
}
|
||||
}
|
||||
|
||||
/// Compares two values for equality.
|
||||
///
|
||||
/// Equality comparisons do not convert strings to numbers or vice versa.
|
||||
/// Tables, Functions, Threads, and Userdata are compared by reference:
|
||||
/// Tables, functions, threads, and userdata are compared by reference:
|
||||
/// two objects are considered equal only if they are the same object.
|
||||
///
|
||||
/// If Tables or Userdata have `__eq` metamethod then mlua will try to invoke it.
|
||||
/// If table or userdata have `__eq` metamethod then mlua will try to invoke it.
|
||||
/// The first value is checked first. If that value does not define a metamethod
|
||||
/// for `__eq`, then mlua will check the second value.
|
||||
/// Then mlua calls the metamethod with the two values as arguments, if found.
|
||||
pub fn equals<T: AsRef<Self>>(&self, other: T) -> Result<bool> {
|
||||
match (self, other.as_ref()) {
|
||||
pub fn equals(&self, other: &Self) -> Result<bool> {
|
||||
match (self, other) {
|
||||
(Value::Table(a), Value::Table(b)) => a.equals(b),
|
||||
(Value::UserData(a), Value::UserData(b)) => a.equals(b),
|
||||
(a, b) => Ok(a == b),
|
||||
@@ -124,21 +129,42 @@ impl Value {
|
||||
#[inline]
|
||||
pub fn to_pointer(&self) -> *const c_void {
|
||||
match self {
|
||||
Value::String(String(vref)) => {
|
||||
// In Lua < 5.4 (excluding Luau), string pointers are NULL
|
||||
// Use alternative approach
|
||||
let lua = vref.lua.lock();
|
||||
unsafe { ffi::lua_tostring(lua.ref_thread(), vref.index) as *const c_void }
|
||||
}
|
||||
Value::LightUserData(ud) => ud.0,
|
||||
Value::String(String(r))
|
||||
| Value::Table(Table(r))
|
||||
| Value::Function(Function(r))
|
||||
| Value::Thread(Thread(r, ..))
|
||||
| Value::UserData(AnyUserData(r, ..)) => r.to_pointer(),
|
||||
Value::Table(Table(vref))
|
||||
| Value::Function(Function(vref))
|
||||
| Value::Thread(Thread(vref, ..))
|
||||
| Value::UserData(AnyUserData(vref))
|
||||
| Value::Other(vref) => vref.to_pointer(),
|
||||
#[cfg(feature = "luau")]
|
||||
Value::Buffer(crate::Buffer(vref)) => vref.to_pointer(),
|
||||
_ => ptr::null(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts the value to a string.
|
||||
///
|
||||
/// If the value has a metatable with a `__tostring` method, then it will be called to get the
|
||||
/// result.
|
||||
/// This might invoke the `__tostring` metamethod for non-primitive types (eg. tables,
|
||||
/// functions).
|
||||
pub fn to_string(&self) -> Result<StdString> {
|
||||
unsafe fn invoke_to_string(vref: &ValueRef) -> Result<StdString> {
|
||||
let lua = vref.lua.lock();
|
||||
let state = lua.state();
|
||||
let _guard = StackGuard::new(state);
|
||||
check_stack(state, 3)?;
|
||||
|
||||
lua.push_ref(vref);
|
||||
protect_lua!(state, 1, 1, fn(state) {
|
||||
ffi::luaL_tolstring(state, -1, ptr::null_mut());
|
||||
})?;
|
||||
Ok(String(lua.pop_ref()).to_str()?.to_string())
|
||||
}
|
||||
|
||||
match self {
|
||||
Value::Nil => Ok("nil".to_string()),
|
||||
Value::Boolean(b) => Ok(b.to_string()),
|
||||
@@ -149,21 +175,13 @@ impl Value {
|
||||
#[cfg(feature = "luau")]
|
||||
Value::Vector(v) => Ok(v.to_string()),
|
||||
Value::String(s) => Ok(s.to_str()?.to_string()),
|
||||
Value::Table(Table(r))
|
||||
| Value::Function(Function(r))
|
||||
| Value::Thread(Thread(r, ..))
|
||||
| Value::UserData(AnyUserData(r, ..)) => unsafe {
|
||||
let lua = r.lua.lock();
|
||||
let state = lua.state();
|
||||
let _guard = StackGuard::new(state);
|
||||
check_stack(state, 3)?;
|
||||
|
||||
lua.push_ref(r);
|
||||
protect_lua!(state, 1, 1, fn(state) {
|
||||
ffi::luaL_tolstring(state, -1, ptr::null_mut());
|
||||
})?;
|
||||
Ok(String(lua.pop_ref()).to_str()?.to_string())
|
||||
},
|
||||
Value::Table(Table(vref))
|
||||
| Value::Function(Function(vref))
|
||||
| Value::Thread(Thread(vref, ..))
|
||||
| Value::UserData(AnyUserData(vref))
|
||||
| Value::Other(vref) => unsafe { invoke_to_string(vref) },
|
||||
#[cfg(feature = "luau")]
|
||||
Value::Buffer(crate::Buffer(vref)) => unsafe { invoke_to_string(vref) },
|
||||
Value::Error(err) => Ok(err.to_string()),
|
||||
}
|
||||
}
|
||||
@@ -175,6 +193,8 @@ impl Value {
|
||||
}
|
||||
|
||||
/// Returns `true` if the value is a [`NULL`].
|
||||
///
|
||||
/// [`NULL`]: Value::NULL
|
||||
#[inline]
|
||||
pub fn is_null(&self) -> bool {
|
||||
self == &Self::NULL
|
||||
@@ -415,26 +435,45 @@ impl Value {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if the value is a Buffer wrapped in [`AnyUserData`].
|
||||
/// Cast the value to a [`Buffer`].
|
||||
///
|
||||
/// If the value is [`Buffer`], returns it or `None` otherwise.
|
||||
///
|
||||
/// [`Buffer`]: crate::Buffer
|
||||
#[cfg(any(feature = "luau", doc))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
pub fn is_buffer(&self) -> bool {
|
||||
self.as_userdata()
|
||||
.map(|ud| ud.1 == SubtypeId::Buffer)
|
||||
.unwrap_or_default()
|
||||
pub fn as_buffer(&self) -> Option<&crate::Buffer> {
|
||||
match self {
|
||||
Value::Buffer(b) => Some(b),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if the value is a CData wrapped in [`AnyUserData`].
|
||||
#[cfg(any(feature = "luajit", doc))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "luajit")))]
|
||||
#[doc(hidden)]
|
||||
/// Returns `true` if the value is a [`Buffer`].
|
||||
///
|
||||
/// [`Buffer`]: crate::Buffer
|
||||
#[cfg(any(feature = "luau", doc))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
|
||||
#[inline]
|
||||
pub fn is_cdata(&self) -> bool {
|
||||
self.as_userdata()
|
||||
.map(|ud| ud.1 == SubtypeId::CData)
|
||||
.unwrap_or_default()
|
||||
pub fn is_buffer(&self) -> bool {
|
||||
self.as_buffer().is_some()
|
||||
}
|
||||
|
||||
/// Returns `true` if the value is an [`Error`].
|
||||
#[inline]
|
||||
pub fn is_error(&self) -> bool {
|
||||
self.as_error().is_some()
|
||||
}
|
||||
|
||||
/// Cast the value to [`Error`].
|
||||
///
|
||||
/// If the value is an [`Error`], returns it or `None` otherwise.
|
||||
pub fn as_error(&self) -> Option<&Error> {
|
||||
match self {
|
||||
Value::Error(e) => Some(e),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrap reference to this Value into [`SerializableValue`].
|
||||
@@ -449,7 +488,7 @@ impl Value {
|
||||
|
||||
// Compares two values.
|
||||
// Used to sort values for Debug printing.
|
||||
pub(crate) fn cmp(&self, other: &Self) -> Ordering {
|
||||
pub(crate) fn sort_cmp(&self, other: &Self) -> Ordering {
|
||||
fn cmp_num(a: Number, b: Number) -> Ordering {
|
||||
match (a, b) {
|
||||
_ if a < b => Ordering::Less,
|
||||
@@ -473,16 +512,19 @@ impl Value {
|
||||
(_, Value::Boolean(_)) => Ordering::Greater,
|
||||
// Integer && Number
|
||||
(Value::Integer(a), Value::Integer(b)) => a.cmp(b),
|
||||
(&Value::Integer(a), &Value::Number(b)) => cmp_num(a as Number, b),
|
||||
(&Value::Number(a), &Value::Integer(b)) => cmp_num(a, b as Number),
|
||||
(&Value::Number(a), &Value::Number(b)) => cmp_num(a, b),
|
||||
(Value::Integer(a), Value::Number(b)) => cmp_num(*a as Number, *b),
|
||||
(Value::Number(a), Value::Integer(b)) => cmp_num(*a, *b as Number),
|
||||
(Value::Number(a), Value::Number(b)) => cmp_num(*a, *b),
|
||||
(Value::Integer(_) | Value::Number(_), _) => Ordering::Less,
|
||||
(_, Value::Integer(_) | Value::Number(_)) => Ordering::Greater,
|
||||
// Vector (Luau)
|
||||
#[cfg(feature = "luau")]
|
||||
(Value::Vector(a), Value::Vector(b)) => a.partial_cmp(b).unwrap_or(Ordering::Equal),
|
||||
// String
|
||||
(Value::String(a), Value::String(b)) => a.as_bytes().cmp(&b.as_bytes()),
|
||||
(Value::String(_), _) => Ordering::Less,
|
||||
(_, Value::String(_)) => Ordering::Greater,
|
||||
// Other variants can be randomly ordered
|
||||
// Other variants can be ordered by their pointer
|
||||
(a, b) => a.to_pointer().cmp(&b.to_pointer()),
|
||||
}
|
||||
}
|
||||
@@ -519,8 +561,11 @@ impl Value {
|
||||
.unwrap_or_else(|| format!("userdata: {:?}", u.to_pointer()));
|
||||
write!(fmt, "{s}")
|
||||
}
|
||||
#[cfg(feature = "luau")]
|
||||
buf @ Value::Buffer(_) => write!(fmt, "buffer: {:?}", buf.to_pointer()),
|
||||
Value::Error(e) if recursive => write!(fmt, "{e:?}"),
|
||||
Value::Error(_) => write!(fmt, "error"),
|
||||
Value::Other(v) => write!(fmt, "other: {:?}", v.to_pointer()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -530,6 +575,7 @@ impl fmt::Debug for Value {
|
||||
if fmt.alternate() {
|
||||
return self.fmt_pretty(fmt, true, 0, &mut HashSet::new());
|
||||
}
|
||||
|
||||
match self {
|
||||
Value::Nil => write!(fmt, "Nil"),
|
||||
Value::Boolean(b) => write!(fmt, "Boolean({b})"),
|
||||
@@ -543,7 +589,10 @@ impl fmt::Debug for Value {
|
||||
Value::Function(f) => write!(fmt, "{f:?}"),
|
||||
Value::Thread(t) => write!(fmt, "{t:?}"),
|
||||
Value::UserData(ud) => write!(fmt, "{ud:?}"),
|
||||
#[cfg(feature = "luau")]
|
||||
Value::Buffer(buf) => write!(fmt, "{buf:?}"),
|
||||
Value::Error(e) => write!(fmt, "Error({e:?})"),
|
||||
Value::Other(v) => write!(fmt, "Other({v:?})"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -565,18 +614,13 @@ impl PartialEq for Value {
|
||||
(Value::Function(a), Value::Function(b)) => a == b,
|
||||
(Value::Thread(a), Value::Thread(b)) => a == b,
|
||||
(Value::UserData(a), Value::UserData(b)) => a == b,
|
||||
#[cfg(feature = "luau")]
|
||||
(Value::Buffer(a), Value::Buffer(b)) => a == b,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<Value> for Value {
|
||||
#[inline]
|
||||
fn as_ref(&self) -> &Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// A wrapped [`Value`] with customized serialization behavior.
|
||||
#[cfg(feature = "serialize")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "serialize")))]
|
||||
@@ -651,7 +695,7 @@ impl<'a> SerializableValue<'a> {
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl<'a> Serialize for SerializableValue<'a> {
|
||||
impl Serialize for SerializableValue<'_> {
|
||||
fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
@@ -673,11 +717,14 @@ impl<'a> Serialize for SerializableValue<'a> {
|
||||
Value::UserData(ud) if ud.is_serializable() || self.options.deny_unsupported_types => {
|
||||
ud.serialize(serializer)
|
||||
}
|
||||
#[cfg(feature = "luau")]
|
||||
Value::Buffer(buf) => buf.serialize(serializer),
|
||||
Value::Function(_)
|
||||
| Value::Thread(_)
|
||||
| Value::UserData(_)
|
||||
| Value::LightUserData(_)
|
||||
| Value::Error(_) => {
|
||||
| Value::Error(_)
|
||||
| Value::Other(_) => {
|
||||
if self.options.deny_unsupported_types {
|
||||
let msg = format!("cannot serialize <{}>", self.value.type_name());
|
||||
Err(ser::Error::custom(msg))
|
||||
@@ -689,221 +736,12 @@ impl<'a> Serialize for SerializableValue<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for types convertible to `Value`.
|
||||
pub trait IntoLua: Sized {
|
||||
/// Performs the conversion.
|
||||
fn into_lua(self, lua: &Lua) -> Result<Value>;
|
||||
|
||||
/// Pushes the value into the Lua stack.
|
||||
///
|
||||
/// # Safety
|
||||
/// This method does not check Lua stack space.
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
|
||||
lua.push_value(&self.into_lua(lua.lua())?)
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for types convertible from `Value`.
|
||||
pub trait FromLua: Sized {
|
||||
/// Performs the conversion.
|
||||
fn from_lua(value: Value, lua: &Lua) -> Result<Self>;
|
||||
|
||||
/// Performs the conversion for an argument (eg. function argument).
|
||||
///
|
||||
/// `i` is the argument index (position),
|
||||
/// `to` is a function name that received the argument.
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
fn from_lua_arg(arg: Value, i: usize, to: Option<&str>, lua: &Lua) -> Result<Self> {
|
||||
Self::from_lua(arg, lua).map_err(|err| Error::BadArgument {
|
||||
to: to.map(|s| s.to_string()),
|
||||
pos: i,
|
||||
name: None,
|
||||
cause: Arc::new(err),
|
||||
})
|
||||
}
|
||||
|
||||
/// Performs the conversion for a value in the Lua stack at index `idx`.
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
unsafe fn from_stack(idx: c_int, lua: &RawLua) -> Result<Self> {
|
||||
Self::from_lua(lua.stack_value(idx, None), lua.lua())
|
||||
}
|
||||
|
||||
/// Same as `from_lua_arg` but for a value in the Lua stack at index `idx`.
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
unsafe fn from_stack_arg(idx: c_int, i: usize, to: Option<&str>, lua: &RawLua) -> Result<Self> {
|
||||
Self::from_stack(idx, lua).map_err(|err| Error::BadArgument {
|
||||
to: to.map(|s| s.to_string()),
|
||||
pos: i,
|
||||
name: None,
|
||||
cause: Arc::new(err),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Multiple Lua values used for both argument passing and also for multiple return values.
|
||||
#[derive(Default, Debug, Clone)]
|
||||
pub struct MultiValue(VecDeque<Value>);
|
||||
|
||||
impl Deref for MultiValue {
|
||||
type Target = VecDeque<Value>;
|
||||
|
||||
#[inline]
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for MultiValue {
|
||||
#[inline]
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl MultiValue {
|
||||
/// Creates an empty `MultiValue` containing no values.
|
||||
#[inline]
|
||||
pub const fn new() -> MultiValue {
|
||||
MultiValue(VecDeque::new())
|
||||
}
|
||||
|
||||
/// Creates an empty `MultiValue` container with space for at least `capacity` elements.
|
||||
pub fn with_capacity(capacity: usize) -> MultiValue {
|
||||
MultiValue(VecDeque::with_capacity(capacity))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn from_lua_iter<T: IntoLua>(lua: &Lua, iter: impl IntoIterator<Item = T>) -> Result<Self> {
|
||||
let iter = iter.into_iter();
|
||||
let mut multi_value = MultiValue::with_capacity(iter.size_hint().0);
|
||||
for value in iter {
|
||||
multi_value.push_back(value.into_lua(lua)?);
|
||||
}
|
||||
Ok(multi_value)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromIterator<Value> for MultiValue {
|
||||
#[inline]
|
||||
fn from_iter<I: IntoIterator<Item = Value>>(iter: I) -> Self {
|
||||
let mut multi_value = MultiValue::new();
|
||||
multi_value.extend(iter);
|
||||
multi_value
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoIterator for MultiValue {
|
||||
type Item = Value;
|
||||
type IntoIter = vec_deque::IntoIter<Value>;
|
||||
|
||||
#[inline]
|
||||
fn into_iter(mut self) -> Self::IntoIter {
|
||||
let deque = mem::take(&mut self.0);
|
||||
mem::forget(self);
|
||||
deque.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoIterator for &'a MultiValue {
|
||||
type Item = &'a Value;
|
||||
type IntoIter = vec_deque::Iter<'a, Value>;
|
||||
|
||||
#[inline]
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.0.iter()
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for types convertible to any number of Lua values.
|
||||
///
|
||||
/// This is a generalization of `IntoLua`, allowing any number of resulting Lua values instead of
|
||||
/// just one. Any type that implements `IntoLua` will automatically implement this trait.
|
||||
pub trait IntoLuaMulti: Sized {
|
||||
/// Performs the conversion.
|
||||
fn into_lua_multi(self, lua: &Lua) -> Result<MultiValue>;
|
||||
|
||||
/// Pushes the values into the Lua stack.
|
||||
///
|
||||
/// Returns number of pushed values.
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
unsafe fn push_into_stack_multi(self, lua: &RawLua) -> Result<c_int> {
|
||||
let values = self.into_lua_multi(lua.lua())?;
|
||||
let len: c_int = values.len().try_into().unwrap();
|
||||
unsafe {
|
||||
check_stack(lua.state(), len + 1)?;
|
||||
for val in &values {
|
||||
lua.push_value(val)?;
|
||||
}
|
||||
}
|
||||
Ok(len)
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for types that can be created from an arbitrary number of Lua values.
|
||||
///
|
||||
/// This is a generalization of `FromLua`, allowing an arbitrary number of Lua values to participate
|
||||
/// in the conversion. Any type that implements `FromLua` will automatically implement this trait.
|
||||
pub trait FromLuaMulti: Sized {
|
||||
/// Performs the conversion.
|
||||
///
|
||||
/// In case `values` contains more values than needed to perform the conversion, the excess
|
||||
/// values should be ignored. This reflects the semantics of Lua when calling a function or
|
||||
/// assigning values. Similarly, if not enough values are given, conversions should assume that
|
||||
/// any missing values are nil.
|
||||
fn from_lua_multi(values: MultiValue, lua: &Lua) -> Result<Self>;
|
||||
|
||||
/// Performs the conversion for a list of arguments.
|
||||
///
|
||||
/// `i` is an index (position) of the first argument,
|
||||
/// `to` is a function name that received the arguments.
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
fn from_lua_args(args: MultiValue, i: usize, to: Option<&str>, lua: &Lua) -> Result<Self> {
|
||||
let _ = (i, to);
|
||||
Self::from_lua_multi(args, lua)
|
||||
}
|
||||
|
||||
/// Performs the conversion for a number of values in the Lua stack.
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
unsafe fn from_stack_multi(nvals: c_int, lua: &RawLua) -> Result<Self> {
|
||||
let mut values = MultiValue::with_capacity(nvals as usize);
|
||||
for idx in 0..nvals {
|
||||
values.push_back(lua.stack_value(-nvals + idx, None));
|
||||
}
|
||||
if nvals > 0 {
|
||||
// It's safe to clear the stack as all references moved to ref thread
|
||||
ffi::lua_pop(lua.state(), nvals);
|
||||
}
|
||||
Self::from_lua_multi(values, lua.lua())
|
||||
}
|
||||
|
||||
/// Same as `from_lua_args` but for a number of values in the Lua stack.
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
unsafe fn from_stack_args(nargs: c_int, i: usize, to: Option<&str>, lua: &RawLua) -> Result<Self> {
|
||||
let _ = (i, to);
|
||||
Self::from_stack_multi(nargs, lua)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod assertions {
|
||||
use super::*;
|
||||
|
||||
#[cfg(not(feature = "send"))]
|
||||
static_assertions::assert_not_impl_any!(Value: Send);
|
||||
#[cfg(not(feature = "send"))]
|
||||
static_assertions::assert_not_impl_any!(MultiValue: Send);
|
||||
|
||||
#[cfg(feature = "send")]
|
||||
static_assertions::assert_impl_all!(Value: Send, Sync);
|
||||
#[cfg(feature = "send")]
|
||||
static_assertions::assert_impl_all!(MultiValue: Send, Sync);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::fmt;
|
||||
|
||||
#[cfg(all(any(feature = "luau", doc), feature = "serialize"))]
|
||||
#[cfg(feature = "serialize")]
|
||||
use serde::ser::{Serialize, SerializeTupleStruct, Serializer};
|
||||
|
||||
/// A Luau vector type.
|
||||
@@ -8,7 +8,7 @@ use serde::ser::{Serialize, SerializeTupleStruct, Serializer};
|
||||
/// By default vectors are 3-dimensional, but can be 4-dimensional
|
||||
/// if the `luau-vector4` feature is enabled.
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq)]
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)]
|
||||
pub struct Vector(pub(crate) [f32; Self::SIZE]);
|
||||
|
||||
impl fmt::Display for Vector {
|
||||
@@ -21,6 +21,7 @@ impl fmt::Display for Vector {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(not(feature = "luau"), allow(unused))]
|
||||
impl Vector {
|
||||
pub(crate) const SIZE: usize = if cfg!(feature = "luau-vector4") { 4 } else { 3 };
|
||||
|
||||
@@ -65,7 +66,7 @@ impl Vector {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(any(feature = "luau", doc), feature = "serialize"))]
|
||||
#[cfg(feature = "serialize")]
|
||||
impl Serialize for Vector {
|
||||
fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
|
||||
let mut ts = serializer.serialize_tuple_struct("Vector", Self::SIZE)?;
|
||||
@@ -84,3 +85,8 @@ impl PartialEq<[f32; Self::SIZE]> for Vector {
|
||||
self.0 == *other
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "luau")]
|
||||
impl crate::types::LuaType for Vector {
|
||||
const TYPE_ID: std::os::raw::c_int = ffi::LUA_TVECTOR;
|
||||
}
|
||||
+15
-3
@@ -1,8 +1,20 @@
|
||||
[lua54_coverage]
|
||||
features = "lua54,vendored,async,serialize,macros"
|
||||
features = "lua54,vendored,async,send,serialize,macros,anyhow"
|
||||
|
||||
[lua54_with_memory_limit_coverage]
|
||||
features = "lua54,vendored,async,send,serialize,macros"
|
||||
rustflags = "--cfg force_memory_limit"
|
||||
|
||||
[lua51_coverage]
|
||||
features = "lua51,vendored,async,serialize,macros"
|
||||
features = "lua51,vendored,async,send,serialize,macros"
|
||||
|
||||
[lua51_with_memory_limit_coverage]
|
||||
features = "lua51,vendored,async,send,serialize,macros"
|
||||
rustflags = "--cfg force_memory_limit"
|
||||
|
||||
[luau_coverage]
|
||||
features = "luau,async,serialize,macros"
|
||||
features = "luau,async,send,serialize,macros"
|
||||
|
||||
[luau_with_memory_limit_coverage]
|
||||
features = "luau,async,send,serialize,macros"
|
||||
rustflags = "--cfg force_memory_limit"
|
||||
|
||||
+49
-2
@@ -1,5 +1,6 @@
|
||||
#![cfg(feature = "async")]
|
||||
|
||||
use std::string::String as StdString;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -39,12 +40,51 @@ async fn test_async_function() -> Result<()> {
|
||||
async fn test_async_function_wrap() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let f = Function::wrap_async(|_, s: String| async move { Ok(s) });
|
||||
let f = Function::wrap_async(|s: StdString| async move {
|
||||
tokio::task::yield_now().await;
|
||||
Ok(s)
|
||||
});
|
||||
lua.globals().set("f", f)?;
|
||||
|
||||
let res: String = lua.load(r#"f("hello")"#).eval_async().await?;
|
||||
assert_eq!(res, "hello");
|
||||
|
||||
// Return error
|
||||
let ferr = Function::wrap_async(|| async move { Err::<(), _>(Error::runtime("some async error")) });
|
||||
lua.globals().set("ferr", ferr)?;
|
||||
lua.load(
|
||||
r#"
|
||||
local ok, err = pcall(ferr)
|
||||
assert(not ok and tostring(err):find("some async error"))
|
||||
"#,
|
||||
)
|
||||
.exec_async()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_async_function_wrap_raw() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let f = Function::wrap_raw_async(|s: StdString| async move {
|
||||
tokio::task::yield_now().await;
|
||||
s
|
||||
});
|
||||
lua.globals().set("f", f)?;
|
||||
let res: String = lua.load(r#"f("hello")"#).eval_async().await?;
|
||||
assert_eq!(res, "hello");
|
||||
|
||||
// Return error
|
||||
let ferr = Function::wrap_raw_async(|| async move {
|
||||
tokio::task::yield_now().await;
|
||||
Err::<(), _>("some error")
|
||||
});
|
||||
lua.globals().set("ferr", ferr)?;
|
||||
let (_, err): (Value, String) = lua.load(r#"ferr()"#).eval_async().await?;
|
||||
assert_eq!(err, "some error");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -349,6 +389,13 @@ async fn test_async_table_object_like() -> Result<()> {
|
||||
table.set_metatable(Some(metatable));
|
||||
assert_eq!(table.call_async::<i64>(()).await.unwrap(), 15);
|
||||
|
||||
match table.call_async_method::<()>("non_existent", ()).await {
|
||||
Err(Error::RuntimeError(err)) => {
|
||||
assert!(err.contains("attempt to call a nil value (function 'non_existent')"))
|
||||
}
|
||||
r => panic!("expected RuntimeError, got {r:?}"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
#![cfg(feature = "luau")]
|
||||
|
||||
use mlua::{Lua, Result, Value};
|
||||
|
||||
#[test]
|
||||
fn test_buffer() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let buf1 = lua
|
||||
.load(
|
||||
r#"
|
||||
local buf = buffer.fromstring("hello")
|
||||
assert(buffer.len(buf) == 5)
|
||||
return buf
|
||||
"#,
|
||||
)
|
||||
.eval::<Value>()?;
|
||||
assert!(buf1.is_buffer());
|
||||
assert_eq!(buf1.type_name(), "buffer");
|
||||
|
||||
let buf2 = lua.load("buffer.fromstring('hello')").eval::<Value>()?;
|
||||
assert_ne!(buf1, buf2);
|
||||
|
||||
// Check that we can pass buffer type to Lua
|
||||
let buf1 = buf1.as_buffer().unwrap();
|
||||
let func = lua.create_function(|_, buf: Value| return buf.to_string())?;
|
||||
assert!(func.call::<String>(buf1)?.starts_with("buffer:"));
|
||||
|
||||
// Check buffer methods
|
||||
assert_eq!(buf1.len(), 5);
|
||||
assert_eq!(buf1.to_vec(), b"hello");
|
||||
assert_eq!(buf1.read_bytes::<3>(1), [b'e', b'l', b'l']);
|
||||
buf1.write_bytes(1, b"i");
|
||||
assert_eq!(buf1.to_vec(), b"hillo");
|
||||
|
||||
let buf3 = lua.create_buffer(b"")?;
|
||||
assert!(buf3.is_empty());
|
||||
assert!(!Value::Buffer(buf3).to_pointer().is_null());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "range end index 14 out of range for slice of length 13")]
|
||||
fn test_buffer_out_of_bounds_read() {
|
||||
let lua = Lua::new();
|
||||
let buf = lua.create_buffer(b"hello, world!").unwrap();
|
||||
_ = buf.read_bytes::<1>(13);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "range end index 16 out of range for slice of length 13")]
|
||||
fn test_buffer_out_of_bounds_write() {
|
||||
let lua = Lua::new();
|
||||
let buf = lua.create_buffer(b"hello, world!").unwrap();
|
||||
buf.write_bytes(14, b"!!");
|
||||
}
|
||||
+55
-2
@@ -19,7 +19,7 @@ fn test_chunk_path() -> Result<()> {
|
||||
return 321
|
||||
"#,
|
||||
)?;
|
||||
let i: i32 = lua.load(&*temp_dir.path().join("module.lua")).eval()?;
|
||||
let i: i32 = lua.load(temp_dir.path().join("module.lua")).eval()?;
|
||||
assert_eq!(i, 321);
|
||||
|
||||
match lua.load(&*temp_dir.path().join("module2.lua")).exec() {
|
||||
@@ -27,6 +27,30 @@ fn test_chunk_path() -> Result<()> {
|
||||
res => panic!("expected io::Error, got {:?}", res),
|
||||
};
|
||||
|
||||
// &Path
|
||||
assert_eq!(
|
||||
(lua.load(&*temp_dir.path().join("module.lua").as_path())).eval::<i32>()?,
|
||||
321
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_chunk_impls() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
// StdString
|
||||
assert_eq!(lua.load(String::from("1")).eval::<i32>()?, 1);
|
||||
assert_eq!(lua.load(&String::from("2")).eval::<i32>()?, 2);
|
||||
|
||||
// &[u8]
|
||||
assert_eq!(lua.load(&b"3"[..]).eval::<i32>()?, 3);
|
||||
|
||||
// Vec<u8>
|
||||
assert_eq!(lua.load(b"4".to_vec()).eval::<i32>()?, 4);
|
||||
assert_eq!(lua.load(&b"5".to_vec()).eval::<i32>()?, 5);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -42,7 +66,7 @@ fn test_chunk_macro() -> Result<()> {
|
||||
data.raw_set("num", 1)?;
|
||||
|
||||
let ud = mlua::AnyUserData::wrap("hello");
|
||||
let f = mlua::Function::wrap(|_lua, ()| Ok(()));
|
||||
let f = mlua::Function::wrap(|| Ok(()));
|
||||
|
||||
lua.globals().set("g", 123)?;
|
||||
|
||||
@@ -68,3 +92,32 @@ fn test_chunk_macro() -> Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "luau")]
|
||||
#[test]
|
||||
fn test_compiler() -> Result<()> {
|
||||
use std::vec;
|
||||
|
||||
let compiler = mlua::Compiler::new()
|
||||
.set_optimization_level(2)
|
||||
.set_debug_level(2)
|
||||
.set_type_info_level(1)
|
||||
.set_coverage_level(2)
|
||||
.set_vector_lib("vector")
|
||||
.set_vector_ctor("new")
|
||||
.set_vector_type("vector")
|
||||
.set_mutable_globals(vec!["mutable_global".into()])
|
||||
.set_userdata_types(vec!["MyUserdata".into()]);
|
||||
|
||||
assert!(compiler.compile("return vector.new(1, 2, 3)").is_ok());
|
||||
|
||||
// Error
|
||||
match compiler.compile("%") {
|
||||
Err(mlua::Error::SyntaxError { ref message, .. }) => {
|
||||
assert!(message.contains("Expected identifier when parsing expression, got '%'"),);
|
||||
}
|
||||
res => panic!("expected result: {res:?}"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -7,8 +7,6 @@ fn test_compilation() {
|
||||
t.compile_fail("tests/compile/lua_norefunwindsafe.rs");
|
||||
t.compile_fail("tests/compile/ref_nounwindsafe.rs");
|
||||
t.compile_fail("tests/compile/scope_callback_capture.rs");
|
||||
t.compile_fail("tests/compile/scope_callback_inner.rs");
|
||||
t.compile_fail("tests/compile/scope_callback_outer.rs");
|
||||
t.compile_fail("tests/compile/scope_invariance.rs");
|
||||
t.compile_fail("tests/compile/scope_mutable_aliasing.rs");
|
||||
t.compile_fail("tests/compile/scope_userdata_borrow.rs");
|
||||
@@ -17,7 +15,6 @@ fn test_compilation() {
|
||||
{
|
||||
t.compile_fail("tests/compile/async_any_userdata_method.rs");
|
||||
t.compile_fail("tests/compile/async_nonstatic_userdata.rs");
|
||||
t.compile_fail("tests/compile/async_userdata_method.rs");
|
||||
}
|
||||
|
||||
#[cfg(feature = "send")]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use mlua::{UserDataMethods, Lua};
|
||||
use mlua::{Lua, UserDataMethods};
|
||||
|
||||
fn main() {
|
||||
let lua = Lua::new();
|
||||
@@ -6,9 +6,10 @@ fn main() {
|
||||
lua.register_userdata_type::<String>(|reg| {
|
||||
let s = String::new();
|
||||
let mut s = &s;
|
||||
reg.add_async_method("t", |_, this: &String, ()| async {
|
||||
s = this;
|
||||
reg.add_async_method("t", |_, this, ()| async {
|
||||
s = &*this;
|
||||
Ok(())
|
||||
});
|
||||
}).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
@@ -1,20 +1,42 @@
|
||||
error[E0596]: cannot borrow `s` as mutable, as it is a captured variable in a `Fn` closure
|
||||
--> tests/compile/async_any_userdata_method.rs:9:58
|
||||
--> tests/compile/async_any_userdata_method.rs:9:49
|
||||
|
|
||||
9 | reg.add_async_method("t", |_, this: &String, ()| async {
|
||||
| ^^^^^ cannot borrow as mutable
|
||||
10 | s = this;
|
||||
9 | reg.add_async_method("t", |_, this, ()| async {
|
||||
| ^^^^^ cannot borrow as mutable
|
||||
10 | s = &*this;
|
||||
| - mutable borrow occurs due to use of `s` in closure
|
||||
|
||||
error: lifetime may not live long enough
|
||||
--> tests/compile/async_any_userdata_method.rs:9:58
|
||||
error[E0373]: async block may outlive the current function, but it borrows `this`, which is owned by the current function
|
||||
--> tests/compile/async_any_userdata_method.rs:9:49
|
||||
|
|
||||
9 | reg.add_async_method("t", |_, this: &String, ()| async {
|
||||
| ___________________________________----------------------_^
|
||||
| | | |
|
||||
| | | return type of closure `{async block@$DIR/tests/compile/async_any_userdata_method.rs:9:58: 9:63}` contains a lifetime `'2`
|
||||
9 | reg.add_async_method("t", |_, this, ()| async {
|
||||
| ^^^^^ may outlive borrowed value `this`
|
||||
10 | s = &*this;
|
||||
| ---- `this` is borrowed here
|
||||
|
|
||||
note: async block is returned here
|
||||
--> tests/compile/async_any_userdata_method.rs:9:49
|
||||
|
|
||||
9 | reg.add_async_method("t", |_, this, ()| async {
|
||||
| _________________________________________________^
|
||||
10 | | s = &*this;
|
||||
11 | | Ok(())
|
||||
12 | | });
|
||||
| |_________^
|
||||
help: to force the async block to take ownership of `this` (and any other referenced variables), use the `move` keyword
|
||||
|
|
||||
9 | reg.add_async_method("t", |_, this, ()| async move {
|
||||
| ++++
|
||||
|
||||
error: lifetime may not live long enough
|
||||
--> tests/compile/async_any_userdata_method.rs:9:49
|
||||
|
|
||||
9 | reg.add_async_method("t", |_, this, ()| async {
|
||||
| ___________________________________-------------_^
|
||||
| | | |
|
||||
| | | return type of closure `{async block@$DIR/tests/compile/async_any_userdata_method.rs:9:49: 9:54}` contains a lifetime `'2`
|
||||
| | lifetime `'1` represents this closure's body
|
||||
10 | | s = this;
|
||||
10 | | s = &*this;
|
||||
11 | | Ok(())
|
||||
12 | | });
|
||||
| |_________^ returning this value requires that `'1` must outlive `'2`
|
||||
@@ -28,53 +50,31 @@ error[E0597]: `s` does not live long enough
|
||||
| - binding `s` declared here
|
||||
8 | let mut s = &s;
|
||||
| ^^ borrowed value does not live long enough
|
||||
9 | / reg.add_async_method("t", |_, this: &String, ()| async {
|
||||
10 | | s = this;
|
||||
9 | / reg.add_async_method("t", |_, this, ()| async {
|
||||
10 | | s = &*this;
|
||||
11 | | Ok(())
|
||||
12 | | });
|
||||
| |__________- argument requires that `s` is borrowed for `'static`
|
||||
13 | }).unwrap();
|
||||
13 | })
|
||||
| - `s` dropped here while still borrowed
|
||||
|
||||
error[E0521]: borrowed data escapes outside of closure
|
||||
--> tests/compile/async_any_userdata_method.rs:9:9
|
||||
|
|
||||
6 | lua.register_userdata_type::<String>(|reg| {
|
||||
| ---
|
||||
| |
|
||||
| `reg` is a reference that is only valid in the closure body
|
||||
| has type `&mut LuaUserDataRegistry<'1, std::string::String>`
|
||||
...
|
||||
9 | / reg.add_async_method("t", |_, this: &String, ()| async {
|
||||
10 | | s = this;
|
||||
11 | | Ok(())
|
||||
12 | | });
|
||||
| | ^
|
||||
| | |
|
||||
| |__________`reg` escapes the closure body here
|
||||
| argument requires that `'1` must outlive `'static`
|
||||
|
|
||||
= note: requirement occurs because of a mutable reference to `LuaUserDataRegistry<'_, std::string::String>`
|
||||
= note: mutable references are invariant over their type parameter
|
||||
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
|
||||
|
||||
error[E0373]: closure may outlive the current function, but it borrows `s`, which is owned by the current function
|
||||
--> tests/compile/async_any_userdata_method.rs:9:35
|
||||
|
|
||||
9 | reg.add_async_method("t", |_, this: &String, ()| async {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ may outlive borrowed value `s`
|
||||
10 | s = this;
|
||||
9 | reg.add_async_method("t", |_, this, ()| async {
|
||||
| ^^^^^^^^^^^^^ may outlive borrowed value `s`
|
||||
10 | s = &*this;
|
||||
| - `s` is borrowed here
|
||||
|
|
||||
note: function requires argument type to outlive `'static`
|
||||
--> tests/compile/async_any_userdata_method.rs:9:9
|
||||
|
|
||||
9 | / reg.add_async_method("t", |_, this: &String, ()| async {
|
||||
10 | | s = this;
|
||||
9 | / reg.add_async_method("t", |_, this, ()| async {
|
||||
10 | | s = &*this;
|
||||
11 | | Ok(())
|
||||
12 | | });
|
||||
| |__________^
|
||||
help: to force the closure to take ownership of `s` (and any other referenced variables), use the `move` keyword
|
||||
|
|
||||
9 | reg.add_async_method("t", move |_, this: &String, ()| async {
|
||||
9 | reg.add_async_method("t", move |_, this, ()| async {
|
||||
| ++++
|
||||
|
||||
@@ -4,8 +4,8 @@ fn main() {
|
||||
#[derive(Clone)]
|
||||
struct MyUserData<'a>(&'a i64);
|
||||
|
||||
impl<'a> UserData for MyUserData<'a> {
|
||||
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||
impl UserData for MyUserData<'_> {
|
||||
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
|
||||
methods.add_async_method("print", |_, data, ()| async move {
|
||||
println!("{}", data.0);
|
||||
Ok(())
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
error: lifetime may not live long enough
|
||||
--> tests/compile/async_nonstatic_userdata.rs:9:13
|
||||
|
|
||||
7 | impl<'a> UserData for MyUserData<'a> {
|
||||
| -- lifetime `'a` defined here
|
||||
8 | fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||
7 | impl UserData for MyUserData<'_> {
|
||||
| -- lifetime `'1` appears in the `impl`'s self type
|
||||
8 | fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
|
||||
9 | / methods.add_async_method("print", |_, data, ()| async move {
|
||||
10 | | println!("{}", data.0);
|
||||
11 | | Ok(())
|
||||
12 | | });
|
||||
| |______________^ requires that `'a` must outlive `'static`
|
||||
| |______________^ requires that `'1` must outlive `'static`
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
use mlua::{UserData, UserDataMethods};
|
||||
|
||||
struct MyUserData;
|
||||
|
||||
impl UserData for MyUserData {
|
||||
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||
methods.add_async_method("method", |_, this: &'static Self, ()| async {
|
||||
Ok(())
|
||||
});
|
||||
// ^ lifetime may not live long enough
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -1,17 +0,0 @@
|
||||
warning: unused variable: `this`
|
||||
--> tests/compile/async_userdata_method.rs:7:48
|
||||
|
|
||||
7 | methods.add_async_method("method", |_, this: &'static Self, ()| async {
|
||||
| ^^^^ help: if this is intentional, prefix it with an underscore: `_this`
|
||||
|
|
||||
= note: `#[warn(unused_variables)]` on by default
|
||||
|
||||
error: lifetime may not live long enough
|
||||
--> tests/compile/async_userdata_method.rs:7:9
|
||||
|
|
||||
6 | fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||
| ---- lifetime `'lua` defined here
|
||||
7 | / methods.add_async_method("method", |_, this: &'static Self, ()| async {
|
||||
8 | | Ok(())
|
||||
9 | | });
|
||||
| |__________^ argument requires that `'lua` must outlive `'static`
|
||||
@@ -6,7 +6,5 @@ fn main() {
|
||||
let test = Test(0);
|
||||
|
||||
let lua = Lua::new();
|
||||
let _ = lua.create_function(|_, ()| -> Result<i32> {
|
||||
Ok(test.0)
|
||||
});
|
||||
let _ = lua.create_function(|_, ()| -> Result<i32> { Ok(test.0) });
|
||||
}
|
||||
|
||||
@@ -1,20 +1,17 @@
|
||||
error[E0373]: closure may outlive the current function, but it borrows `test.0`, which is owned by the current function
|
||||
--> tests/compile/function_borrow.rs:9:33
|
||||
|
|
||||
9 | let _ = lua.create_function(|_, ()| -> Result<i32> {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ may outlive borrowed value `test.0`
|
||||
10 | Ok(test.0)
|
||||
| ------ `test.0` is borrowed here
|
||||
|
|
||||
--> tests/compile/function_borrow.rs:9:33
|
||||
|
|
||||
9 | let _ = lua.create_function(|_, ()| -> Result<i32> { Ok(test.0) });
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ ------ `test.0` is borrowed here
|
||||
| |
|
||||
| may outlive borrowed value `test.0`
|
||||
|
|
||||
note: function requires argument type to outlive `'static`
|
||||
--> tests/compile/function_borrow.rs:9:13
|
||||
|
|
||||
9 | let _ = lua.create_function(|_, ()| -> Result<i32> {
|
||||
| _____________^
|
||||
10 | | Ok(test.0)
|
||||
11 | | });
|
||||
| |______^
|
||||
--> tests/compile/function_borrow.rs:9:13
|
||||
|
|
||||
9 | let _ = lua.create_function(|_, ()| -> Result<i32> { Ok(test.0) });
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
help: to force the closure to take ownership of `test.0` (and any other referenced variables), use the `move` keyword
|
||||
|
|
||||
9 | let _ = lua.create_function(move |_, ()| -> Result<i32> {
|
||||
| ++++
|
||||
|
|
||||
9 | let _ = lua.create_function(move |_, ()| -> Result<i32> { Ok(test.0) });
|
||||
| ++++
|
||||
|
||||
@@ -1,32 +1,36 @@
|
||||
error[E0277]: the type `UnsafeCell<*mut lua_State>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
|
||||
error[E0277]: the type `UnsafeCell<mlua::state::raw::RawLua>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
|
||||
--> tests/compile/lua_norefunwindsafe.rs:7:18
|
||||
|
|
||||
7 | catch_unwind(|| lua.create_table().unwrap());
|
||||
| ------------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `UnsafeCell<*mut lua_State>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
|
||||
| ------------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `UnsafeCell<mlua::state::raw::RawLua>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
|
||||
| |
|
||||
| required by a bound introduced by this call
|
||||
|
|
||||
= help: within `mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>`, the trait `RefUnwindSafe` is not implemented for `UnsafeCell<*mut lua_State>`, which is required by `{closure@$DIR/tests/compile/lua_norefunwindsafe.rs:7:18: 7:20}: UnwindSafe`
|
||||
note: required because it appears within the type `Cell<*mut lua_State>`
|
||||
--> $RUST/core/src/cell.rs
|
||||
= help: within `Lua`, the trait `RefUnwindSafe` is not implemented for `UnsafeCell<mlua::state::raw::RawLua>`, which is required by `{closure@$DIR/tests/compile/lua_norefunwindsafe.rs:7:18: 7:20}: UnwindSafe`
|
||||
note: required because it appears within the type `lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>`
|
||||
--> $CARGO/lock_api-0.4.12/src/remutex.rs
|
||||
|
|
||||
| pub struct Cell<T: ?Sized> {
|
||||
| ^^^^
|
||||
note: required because it appears within the type `mlua::state::raw::RawLua`
|
||||
--> src/state/raw.rs
|
||||
| pub struct ReentrantMutex<R, G, T: ?Sized> {
|
||||
| ^^^^^^^^^^^^^^
|
||||
note: required because it appears within the type `alloc::sync::ArcInner<lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>>`
|
||||
--> $RUST/alloc/src/sync.rs
|
||||
|
|
||||
| pub struct RawLua {
|
||||
| ^^^^^^
|
||||
note: required because it appears within the type `mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>`
|
||||
--> src/types/sync.rs
|
||||
| struct ArcInner<T: ?Sized> {
|
||||
| ^^^^^^^^
|
||||
note: required because it appears within the type `PhantomData<alloc::sync::ArcInner<lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>>>`
|
||||
--> $RUST/core/src/marker.rs
|
||||
|
|
||||
| pub(crate) struct ReentrantMutex<T>(T);
|
||||
| ^^^^^^^^^^^^^^
|
||||
= note: required for `Rc<mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>>` to implement `RefUnwindSafe`
|
||||
| pub struct PhantomData<T: ?Sized>;
|
||||
| ^^^^^^^^^^^
|
||||
note: required because it appears within the type `Arc<lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>>`
|
||||
--> $RUST/alloc/src/sync.rs
|
||||
|
|
||||
| pub struct Arc<
|
||||
| ^^^
|
||||
note: required because it appears within the type `Lua`
|
||||
--> src/state.rs
|
||||
|
|
||||
| pub struct Lua(XRc<ReentrantMutex<RawLua>>);
|
||||
| pub struct Lua {
|
||||
| ^^^
|
||||
= note: required for `&Lua` to implement `UnwindSafe`
|
||||
note: required because it's used within this closure
|
||||
@@ -40,31 +44,49 @@ note: required by a bound in `std::panic::catch_unwind`
|
||||
| pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> {
|
||||
| ^^^^^^^^^^ required by this bound in `catch_unwind`
|
||||
|
||||
error[E0277]: the type `UnsafeCell<mlua::state::extra::ExtraData>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
|
||||
error[E0277]: the type `UnsafeCell<usize>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
|
||||
--> tests/compile/lua_norefunwindsafe.rs:7:18
|
||||
|
|
||||
7 | catch_unwind(|| lua.create_table().unwrap());
|
||||
| ------------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `UnsafeCell<mlua::state::extra::ExtraData>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
|
||||
| ------------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `UnsafeCell<usize>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
|
||||
| |
|
||||
| required by a bound introduced by this call
|
||||
|
|
||||
= help: the trait `RefUnwindSafe` is not implemented for `UnsafeCell<mlua::state::extra::ExtraData>`, which is required by `{closure@$DIR/tests/compile/lua_norefunwindsafe.rs:7:18: 7:20}: UnwindSafe`
|
||||
= note: required for `Rc<UnsafeCell<mlua::state::extra::ExtraData>>` to implement `RefUnwindSafe`
|
||||
note: required because it appears within the type `mlua::state::raw::RawLua`
|
||||
--> src/state/raw.rs
|
||||
= help: within `Lua`, the trait `RefUnwindSafe` is not implemented for `UnsafeCell<usize>`, which is required by `{closure@$DIR/tests/compile/lua_norefunwindsafe.rs:7:18: 7:20}: UnwindSafe`
|
||||
note: required because it appears within the type `Cell<usize>`
|
||||
--> $RUST/core/src/cell.rs
|
||||
|
|
||||
| pub struct RawLua {
|
||||
| ^^^^^^
|
||||
note: required because it appears within the type `mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>`
|
||||
--> src/types/sync.rs
|
||||
| pub struct Cell<T: ?Sized> {
|
||||
| ^^^^
|
||||
note: required because it appears within the type `lock_api::remutex::RawReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId>`
|
||||
--> $CARGO/lock_api-0.4.12/src/remutex.rs
|
||||
|
|
||||
| pub(crate) struct ReentrantMutex<T>(T);
|
||||
| ^^^^^^^^^^^^^^
|
||||
= note: required for `Rc<mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>>` to implement `RefUnwindSafe`
|
||||
| pub struct RawReentrantMutex<R, G> {
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
note: required because it appears within the type `lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>`
|
||||
--> $CARGO/lock_api-0.4.12/src/remutex.rs
|
||||
|
|
||||
| pub struct ReentrantMutex<R, G, T: ?Sized> {
|
||||
| ^^^^^^^^^^^^^^
|
||||
note: required because it appears within the type `alloc::sync::ArcInner<lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>>`
|
||||
--> $RUST/alloc/src/sync.rs
|
||||
|
|
||||
| struct ArcInner<T: ?Sized> {
|
||||
| ^^^^^^^^
|
||||
note: required because it appears within the type `PhantomData<alloc::sync::ArcInner<lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>>>`
|
||||
--> $RUST/core/src/marker.rs
|
||||
|
|
||||
| pub struct PhantomData<T: ?Sized>;
|
||||
| ^^^^^^^^^^^
|
||||
note: required because it appears within the type `Arc<lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>>`
|
||||
--> $RUST/alloc/src/sync.rs
|
||||
|
|
||||
| pub struct Arc<
|
||||
| ^^^
|
||||
note: required because it appears within the type `Lua`
|
||||
--> src/state.rs
|
||||
|
|
||||
| pub struct Lua(XRc<ReentrantMutex<RawLua>>);
|
||||
| pub struct Lua {
|
||||
| ^^^
|
||||
= note: required for `&Lua` to implement `UnwindSafe`
|
||||
note: required because it's used within this closure
|
||||
|
||||
@@ -8,10 +8,8 @@ fn main() -> Result<()> {
|
||||
|
||||
let data = Rc::new(Cell::new(0));
|
||||
|
||||
lua.create_function(move |_, ()| {
|
||||
Ok(data.get())
|
||||
})?
|
||||
.call::<i32>(())?;
|
||||
lua.create_function(move |_, ()| Ok(data.get()))?
|
||||
.call::<i32>(())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,28 +1,25 @@
|
||||
error[E0277]: `Rc<Cell<i32>>` cannot be sent between threads safely
|
||||
--> tests/compile/non_send.rs:11:25
|
||||
|
|
||||
11 | lua.create_function(move |_, ()| {
|
||||
| --------------- ^-----------
|
||||
| | |
|
||||
| _________|_______________within this `{closure@$DIR/tests/compile/non_send.rs:11:25: 11:37}`
|
||||
| | |
|
||||
| | required by a bound introduced by this call
|
||||
12 | | Ok(data.get())
|
||||
13 | | })?
|
||||
| |_____^ `Rc<Cell<i32>>` cannot be sent between threads safely
|
||||
11 | lua.create_function(move |_, ()| Ok(data.get()))?
|
||||
| --------------- ------------^^^^^^^^^^^^^^^
|
||||
| | |
|
||||
| | `Rc<Cell<i32>>` cannot be sent between threads safely
|
||||
| | within this `{closure@$DIR/tests/compile/non_send.rs:11:25: 11:37}`
|
||||
| required by a bound introduced by this call
|
||||
|
|
||||
= help: within `{closure@$DIR/tests/compile/non_send.rs:11:25: 11:37}`, the trait `Send` is not implemented for `Rc<Cell<i32>>`
|
||||
= help: within `{closure@$DIR/tests/compile/non_send.rs:11:25: 11:37}`, the trait `Send` is not implemented for `Rc<Cell<i32>>`, which is required by `{closure@$DIR/tests/compile/non_send.rs:11:25: 11:37}: MaybeSend`
|
||||
note: required because it's used within this closure
|
||||
--> tests/compile/non_send.rs:11:25
|
||||
|
|
||||
11 | lua.create_function(move |_, ()| {
|
||||
11 | lua.create_function(move |_, ()| Ok(data.get()))?
|
||||
| ^^^^^^^^^^^^
|
||||
= note: required for `{closure@$DIR/tests/compile/non_send.rs:11:25: 11:37}` to implement `mlua::types::MaybeSend`
|
||||
= note: required for `{closure@$DIR/tests/compile/non_send.rs:11:25: 11:37}` to implement `MaybeSend`
|
||||
note: required by a bound in `Lua::create_function`
|
||||
--> src/lua.rs
|
||||
--> src/state.rs
|
||||
|
|
||||
| pub fn create_function<'lua, A, R, F>(&'lua self, func: F) -> Result<Function<'lua>>
|
||||
| pub fn create_function<F, A, R>(&self, func: F) -> Result<Function>
|
||||
| --------------- required by a bound in this associated function
|
||||
...
|
||||
| F: Fn(&'lua Lua, A) -> Result<R> + MaybeSend + 'static,
|
||||
| ^^^^^^^^^ required by this bound in `Lua::create_function`
|
||||
| where
|
||||
| F: Fn(&Lua, A) -> Result<R> + MaybeSend + 'static,
|
||||
| ^^^^^^^^^ required by this bound in `Lua::create_function`
|
||||
|
||||
@@ -1,3 +1,54 @@
|
||||
error[E0277]: the type `UnsafeCell<mlua::state::raw::RawLua>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
|
||||
--> tests/compile/ref_nounwindsafe.rs:8:18
|
||||
|
|
||||
8 | catch_unwind(move || table.set("a", "b").unwrap());
|
||||
| ------------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `UnsafeCell<mlua::state::raw::RawLua>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
|
||||
| |
|
||||
| required by a bound introduced by this call
|
||||
|
|
||||
= help: within `alloc::sync::ArcInner<lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>>`, the trait `RefUnwindSafe` is not implemented for `UnsafeCell<mlua::state::raw::RawLua>`, which is required by `{closure@$DIR/tests/compile/ref_nounwindsafe.rs:8:18: 8:25}: UnwindSafe`
|
||||
note: required because it appears within the type `lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>`
|
||||
--> $CARGO/lock_api-0.4.12/src/remutex.rs
|
||||
|
|
||||
| pub struct ReentrantMutex<R, G, T: ?Sized> {
|
||||
| ^^^^^^^^^^^^^^
|
||||
note: required because it appears within the type `alloc::sync::ArcInner<lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>>`
|
||||
--> $RUST/alloc/src/sync.rs
|
||||
|
|
||||
| struct ArcInner<T: ?Sized> {
|
||||
| ^^^^^^^^
|
||||
= note: required for `NonNull<alloc::sync::ArcInner<lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>>>` to implement `UnwindSafe`
|
||||
note: required because it appears within the type `std::sync::Weak<lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>>`
|
||||
--> $RUST/alloc/src/sync.rs
|
||||
|
|
||||
| pub struct Weak<
|
||||
| ^^^^
|
||||
note: required because it appears within the type `mlua::state::WeakLua`
|
||||
--> src/state.rs
|
||||
|
|
||||
| pub(crate) struct WeakLua(XWeak<ReentrantMutex<RawLua>>);
|
||||
| ^^^^^^^
|
||||
note: required because it appears within the type `mlua::types::ValueRef`
|
||||
--> src/types.rs
|
||||
|
|
||||
| pub(crate) struct ValueRef {
|
||||
| ^^^^^^^^
|
||||
note: required because it appears within the type `LuaTable`
|
||||
--> src/table.rs
|
||||
|
|
||||
| pub struct Table(pub(crate) ValueRef);
|
||||
| ^^^^^
|
||||
note: required because it's used within this closure
|
||||
--> tests/compile/ref_nounwindsafe.rs:8:18
|
||||
|
|
||||
8 | catch_unwind(move || table.set("a", "b").unwrap());
|
||||
| ^^^^^^^
|
||||
note: required by a bound in `std::panic::catch_unwind`
|
||||
--> $RUST/std/src/panic.rs
|
||||
|
|
||||
| pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> {
|
||||
| ^^^^^^^^^^ required by this bound in `catch_unwind`
|
||||
|
||||
error[E0277]: the type `UnsafeCell<usize>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
|
||||
--> tests/compile/ref_nounwindsafe.rs:8:18
|
||||
|
|
||||
@@ -6,138 +57,30 @@ error[E0277]: the type `UnsafeCell<usize>` may contain interior mutability and a
|
||||
| |
|
||||
| required by a bound introduced by this call
|
||||
|
|
||||
= help: within `rc::RcBox<mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>>`, the trait `RefUnwindSafe` is not implemented for `UnsafeCell<usize>`, which is required by `{closure@$DIR/tests/compile/ref_nounwindsafe.rs:8:18: 8:25}: UnwindSafe`
|
||||
= help: within `alloc::sync::ArcInner<lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>>`, the trait `RefUnwindSafe` is not implemented for `UnsafeCell<usize>`, which is required by `{closure@$DIR/tests/compile/ref_nounwindsafe.rs:8:18: 8:25}: UnwindSafe`
|
||||
note: required because it appears within the type `Cell<usize>`
|
||||
--> $RUST/core/src/cell.rs
|
||||
|
|
||||
| pub struct Cell<T: ?Sized> {
|
||||
| ^^^^
|
||||
note: required because it appears within the type `rc::RcBox<mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>>`
|
||||
--> $RUST/alloc/src/rc.rs
|
||||
note: required because it appears within the type `lock_api::remutex::RawReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId>`
|
||||
--> $CARGO/lock_api-0.4.12/src/remutex.rs
|
||||
|
|
||||
| struct RcBox<T: ?Sized> {
|
||||
| ^^^^^
|
||||
= note: required for `NonNull<rc::RcBox<mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>>>` to implement `UnwindSafe`
|
||||
note: required because it appears within the type `std::rc::Weak<mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>>`
|
||||
--> $RUST/alloc/src/rc.rs
|
||||
|
|
||||
| pub struct Weak<
|
||||
| ^^^^
|
||||
note: required because it appears within the type `mlua::state::WeakLua`
|
||||
--> src/state.rs
|
||||
|
|
||||
| pub(crate) struct WeakLua(XWeak<ReentrantMutex<RawLua>>);
|
||||
| ^^^^^^^
|
||||
note: required because it appears within the type `mlua::types::ValueRef`
|
||||
--> src/types.rs
|
||||
|
|
||||
| pub(crate) struct ValueRef {
|
||||
| ^^^^^^^^
|
||||
note: required because it appears within the type `LuaTable`
|
||||
--> src/table.rs
|
||||
|
|
||||
| pub struct Table(pub(crate) ValueRef);
|
||||
| ^^^^^
|
||||
note: required because it's used within this closure
|
||||
--> tests/compile/ref_nounwindsafe.rs:8:18
|
||||
|
|
||||
8 | catch_unwind(move || table.set("a", "b").unwrap());
|
||||
| ^^^^^^^
|
||||
note: required by a bound in `std::panic::catch_unwind`
|
||||
--> $RUST/std/src/panic.rs
|
||||
|
|
||||
| pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> {
|
||||
| ^^^^^^^^^^ required by this bound in `catch_unwind`
|
||||
|
||||
error[E0277]: the type `UnsafeCell<*mut lua_State>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
|
||||
--> tests/compile/ref_nounwindsafe.rs:8:18
|
||||
|
|
||||
8 | catch_unwind(move || table.set("a", "b").unwrap());
|
||||
| ------------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `UnsafeCell<*mut lua_State>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
|
||||
| |
|
||||
| required by a bound introduced by this call
|
||||
|
|
||||
= help: within `rc::RcBox<mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>>`, the trait `RefUnwindSafe` is not implemented for `UnsafeCell<*mut lua_State>`, which is required by `{closure@$DIR/tests/compile/ref_nounwindsafe.rs:8:18: 8:25}: UnwindSafe`
|
||||
note: required because it appears within the type `Cell<*mut lua_State>`
|
||||
--> $RUST/core/src/cell.rs
|
||||
|
|
||||
| pub struct Cell<T: ?Sized> {
|
||||
| ^^^^
|
||||
note: required because it appears within the type `mlua::state::raw::RawLua`
|
||||
--> src/state/raw.rs
|
||||
|
|
||||
| pub struct RawLua {
|
||||
| ^^^^^^
|
||||
note: required because it appears within the type `mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>`
|
||||
--> src/types/sync.rs
|
||||
|
|
||||
| pub(crate) struct ReentrantMutex<T>(T);
|
||||
| ^^^^^^^^^^^^^^
|
||||
note: required because it appears within the type `rc::RcBox<mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>>`
|
||||
--> $RUST/alloc/src/rc.rs
|
||||
|
|
||||
| struct RcBox<T: ?Sized> {
|
||||
| ^^^^^
|
||||
= note: required for `NonNull<rc::RcBox<mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>>>` to implement `UnwindSafe`
|
||||
note: required because it appears within the type `std::rc::Weak<mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>>`
|
||||
--> $RUST/alloc/src/rc.rs
|
||||
|
|
||||
| pub struct Weak<
|
||||
| ^^^^
|
||||
note: required because it appears within the type `mlua::state::WeakLua`
|
||||
--> src/state.rs
|
||||
|
|
||||
| pub(crate) struct WeakLua(XWeak<ReentrantMutex<RawLua>>);
|
||||
| ^^^^^^^
|
||||
note: required because it appears within the type `mlua::types::ValueRef`
|
||||
--> src/types.rs
|
||||
|
|
||||
| pub(crate) struct ValueRef {
|
||||
| ^^^^^^^^
|
||||
note: required because it appears within the type `LuaTable`
|
||||
--> src/table.rs
|
||||
|
|
||||
| pub struct Table(pub(crate) ValueRef);
|
||||
| ^^^^^
|
||||
note: required because it's used within this closure
|
||||
--> tests/compile/ref_nounwindsafe.rs:8:18
|
||||
|
|
||||
8 | catch_unwind(move || table.set("a", "b").unwrap());
|
||||
| ^^^^^^^
|
||||
note: required by a bound in `std::panic::catch_unwind`
|
||||
--> $RUST/std/src/panic.rs
|
||||
|
|
||||
| pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> {
|
||||
| ^^^^^^^^^^ required by this bound in `catch_unwind`
|
||||
|
||||
error[E0277]: the type `UnsafeCell<mlua::state::extra::ExtraData>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
|
||||
--> tests/compile/ref_nounwindsafe.rs:8:18
|
||||
|
|
||||
8 | catch_unwind(move || table.set("a", "b").unwrap());
|
||||
| ------------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `UnsafeCell<mlua::state::extra::ExtraData>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
|
||||
| |
|
||||
| required by a bound introduced by this call
|
||||
|
|
||||
= help: the trait `RefUnwindSafe` is not implemented for `UnsafeCell<mlua::state::extra::ExtraData>`, which is required by `{closure@$DIR/tests/compile/ref_nounwindsafe.rs:8:18: 8:25}: UnwindSafe`
|
||||
= note: required for `Rc<UnsafeCell<mlua::state::extra::ExtraData>>` to implement `RefUnwindSafe`
|
||||
note: required because it appears within the type `mlua::state::raw::RawLua`
|
||||
--> src/state/raw.rs
|
||||
|
|
||||
| pub struct RawLua {
|
||||
| ^^^^^^
|
||||
note: required because it appears within the type `mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>`
|
||||
--> src/types/sync.rs
|
||||
|
|
||||
| pub(crate) struct ReentrantMutex<T>(T);
|
||||
| ^^^^^^^^^^^^^^
|
||||
note: required because it appears within the type `rc::RcBox<mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>>`
|
||||
--> $RUST/alloc/src/rc.rs
|
||||
|
|
||||
| struct RcBox<T: ?Sized> {
|
||||
| ^^^^^
|
||||
= note: required for `NonNull<rc::RcBox<mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>>>` to implement `UnwindSafe`
|
||||
note: required because it appears within the type `std::rc::Weak<mlua::types::sync::inner::ReentrantMutex<mlua::state::raw::RawLua>>`
|
||||
--> $RUST/alloc/src/rc.rs
|
||||
| pub struct RawReentrantMutex<R, G> {
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
note: required because it appears within the type `lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>`
|
||||
--> $CARGO/lock_api-0.4.12/src/remutex.rs
|
||||
|
|
||||
| pub struct ReentrantMutex<R, G, T: ?Sized> {
|
||||
| ^^^^^^^^^^^^^^
|
||||
note: required because it appears within the type `alloc::sync::ArcInner<lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>>`
|
||||
--> $RUST/alloc/src/sync.rs
|
||||
|
|
||||
| struct ArcInner<T: ?Sized> {
|
||||
| ^^^^^^^^
|
||||
= note: required for `NonNull<alloc::sync::ArcInner<lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>>>` to implement `UnwindSafe`
|
||||
note: required because it appears within the type `std::sync::Weak<lock_api::remutex::ReentrantMutex<parking_lot::raw_mutex::RawMutex, parking_lot::remutex::RawThreadId, mlua::state::raw::RawLua>>`
|
||||
--> $RUST/alloc/src/sync.rs
|
||||
|
|
||||
| pub struct Weak<
|
||||
| ^^^^
|
||||
|
||||
@@ -4,14 +4,10 @@ fn main() {
|
||||
let lua = Lua::new();
|
||||
lua.scope(|scope| {
|
||||
let mut inner: Option<Table> = None;
|
||||
let f = scope
|
||||
.create_function_mut(move |_, t: Table| {
|
||||
if let Some(old) = inner.take() {
|
||||
// Access old callback `Lua`.
|
||||
}
|
||||
inner = Some(t);
|
||||
Ok(())
|
||||
})?;
|
||||
let f = scope.create_function_mut(|_, t: Table| {
|
||||
inner = Some(t);
|
||||
Ok(())
|
||||
})?;
|
||||
f.call::<()>(lua.create_table()?)?;
|
||||
Ok(())
|
||||
});
|
||||
|
||||
@@ -1,5 +1,24 @@
|
||||
error[E0599]: no method named `scope` found for struct `Lua` in the current scope
|
||||
--> tests/compile/scope_callback_capture.rs:5:9
|
||||
|
|
||||
5 | lua.scope(|scope| {
|
||||
| ----^^^^^ method not found in `Lua`
|
||||
error[E0373]: closure may outlive the current function, but it borrows `inner`, which is owned by the current function
|
||||
--> tests/compile/scope_callback_capture.rs:7:43
|
||||
|
|
||||
5 | lua.scope(|scope| {
|
||||
| ----- has type `&'1 mut mlua::scope::Scope<'1, '_>`
|
||||
6 | let mut inner: Option<Table> = None;
|
||||
7 | let f = scope.create_function_mut(|_, t: Table| {
|
||||
| ^^^^^^^^^^^^^ may outlive borrowed value `inner`
|
||||
8 | inner = Some(t);
|
||||
| ----- `inner` is borrowed here
|
||||
|
|
||||
note: function requires argument type to outlive `'1`
|
||||
--> tests/compile/scope_callback_capture.rs:7:17
|
||||
|
|
||||
7 | let f = scope.create_function_mut(|_, t: Table| {
|
||||
| _________________^
|
||||
8 | | inner = Some(t);
|
||||
9 | | Ok(())
|
||||
10 | | })?;
|
||||
| |__________^
|
||||
help: to force the closure to take ownership of `inner` (and any other referenced variables), use the `move` keyword
|
||||
|
|
||||
7 | let f = scope.create_function_mut(move |_, t: Table| {
|
||||
| ++++
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
use mlua::{Lua, Table};
|
||||
|
||||
fn main() {
|
||||
let lua = Lua::new();
|
||||
lua.scope(|scope| {
|
||||
let mut inner: Option<Table> = None;
|
||||
let f = scope
|
||||
.create_function_mut(|_, t: Table| {
|
||||
inner = Some(t);
|
||||
Ok(())
|
||||
})?;
|
||||
f.call::<()>(lua.create_table()?)?;
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
error[E0599]: no method named `scope` found for struct `Lua` in the current scope
|
||||
--> tests/compile/scope_callback_inner.rs:5:9
|
||||
|
|
||||
5 | lua.scope(|scope| {
|
||||
| ----^^^^^ method not found in `Lua`
|
||||
@@ -1,15 +0,0 @@
|
||||
use mlua::{Lua, Table};
|
||||
|
||||
fn main() {
|
||||
let lua = Lua::new();
|
||||
let mut outer: Option<Table> = None;
|
||||
lua.scope(|scope| {
|
||||
let f = scope
|
||||
.create_function_mut(|_, t: Table| {
|
||||
outer = Some(t);
|
||||
Ok(())
|
||||
})?;
|
||||
f.call::<()>(lua.create_table()?)?;
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
error[E0599]: no method named `scope` found for struct `Lua` in the current scope
|
||||
--> tests/compile/scope_callback_outer.rs:6:9
|
||||
|
|
||||
6 | lua.scope(|scope| {
|
||||
| ----^^^^^ method not found in `Lua`
|
||||
@@ -10,12 +10,11 @@ fn main() {
|
||||
let f = {
|
||||
let mut test = Test { field: 0 };
|
||||
|
||||
scope
|
||||
.create_function_mut(|_, ()| {
|
||||
test.field = 42;
|
||||
//~^ error: `test` does not live long enough
|
||||
Ok(())
|
||||
})?
|
||||
scope.create_function_mut(|_, ()| {
|
||||
test.field = 42;
|
||||
//~^ error: `test` does not live long enough
|
||||
Ok(())
|
||||
})?
|
||||
};
|
||||
|
||||
f.call::<()>(())
|
||||
|
||||
@@ -1,5 +1,24 @@
|
||||
error[E0599]: no method named `scope` found for struct `Lua` in the current scope
|
||||
--> tests/compile/scope_invariance.rs:9:9
|
||||
|
|
||||
9 | lua.scope(|scope| {
|
||||
| ----^^^^^ method not found in `Lua`
|
||||
error[E0373]: closure may outlive the current function, but it borrows `test.field`, which is owned by the current function
|
||||
--> tests/compile/scope_invariance.rs:13:39
|
||||
|
|
||||
9 | lua.scope(|scope| {
|
||||
| ----- has type `&'1 mut mlua::scope::Scope<'1, '_>`
|
||||
...
|
||||
13 | scope.create_function_mut(|_, ()| {
|
||||
| ^^^^^^^ may outlive borrowed value `test.field`
|
||||
14 | test.field = 42;
|
||||
| ---------- `test.field` is borrowed here
|
||||
|
|
||||
note: function requires argument type to outlive `'1`
|
||||
--> tests/compile/scope_invariance.rs:13:13
|
||||
|
|
||||
13 | / scope.create_function_mut(|_, ()| {
|
||||
14 | | test.field = 42;
|
||||
15 | | //~^ error: `test` does not live long enough
|
||||
16 | | Ok(())
|
||||
17 | | })?
|
||||
| |______________^
|
||||
help: to force the closure to take ownership of `test.field` (and any other referenced variables), use the `move` keyword
|
||||
|
|
||||
13 | scope.create_function_mut(move |_, ()| {
|
||||
| ++++
|
||||
|
||||
@@ -2,14 +2,14 @@ use mlua::{Lua, UserData};
|
||||
|
||||
fn main() {
|
||||
struct MyUserData<'a>(&'a mut i32);
|
||||
impl<'a> UserData for MyUserData<'a> {}
|
||||
impl UserData for MyUserData<'_> {}
|
||||
|
||||
let mut i = 1;
|
||||
|
||||
let lua = Lua::new();
|
||||
lua.scope(|scope| {
|
||||
let _a = scope.create_nonstatic_userdata(MyUserData(&mut i)).unwrap();
|
||||
let _b = scope.create_nonstatic_userdata(MyUserData(&mut i)).unwrap();
|
||||
let _a = scope.create_userdata(MyUserData(&mut i)).unwrap();
|
||||
let _b = scope.create_userdata(MyUserData(&mut i)).unwrap();
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
error[E0599]: no method named `scope` found for struct `Lua` in the current scope
|
||||
--> tests/compile/scope_mutable_aliasing.rs:10:9
|
||||
error[E0499]: cannot borrow `i` as mutable more than once at a time
|
||||
--> tests/compile/scope_mutable_aliasing.rs:12:51
|
||||
|
|
||||
10 | lua.scope(|scope| {
|
||||
| ----^^^^^ method not found in `Lua`
|
||||
| ----- has type `&mut mlua::scope::Scope<'_, '1>`
|
||||
11 | let _a = scope.create_userdata(MyUserData(&mut i)).unwrap();
|
||||
| -----------------------------------------
|
||||
| | |
|
||||
| | first mutable borrow occurs here
|
||||
| argument requires that `i` is borrowed for `'1`
|
||||
12 | let _b = scope.create_userdata(MyUserData(&mut i)).unwrap();
|
||||
| ^^^^^^ second mutable borrow occurs here
|
||||
|
||||
@@ -3,16 +3,16 @@ use mlua::{Lua, UserData};
|
||||
fn main() {
|
||||
// Should not allow userdata borrow to outlive lifetime of AnyUserData handle
|
||||
struct MyUserData<'a>(&'a i32);
|
||||
impl<'a> UserData for MyUserData<'a> {}
|
||||
impl UserData for MyUserData<'_> {}
|
||||
|
||||
let igood = 1;
|
||||
|
||||
let lua = Lua::new();
|
||||
lua.scope(|scope| {
|
||||
let _ugood = scope.create_nonstatic_userdata(MyUserData(&igood)).unwrap();
|
||||
let _ugood = scope.create_userdata(MyUserData(&igood)).unwrap();
|
||||
let _ubad = {
|
||||
let ibad = 42;
|
||||
scope.create_nonstatic_userdata(MyUserData(&ibad)).unwrap();
|
||||
scope.create_userdata(MyUserData(&ibad)).unwrap();
|
||||
};
|
||||
Ok(())
|
||||
});
|
||||
|
||||
@@ -1,5 +1,15 @@
|
||||
error[E0599]: no method named `scope` found for struct `Lua` in the current scope
|
||||
--> tests/compile/scope_userdata_borrow.rs:11:9
|
||||
error[E0597]: `ibad` does not live long enough
|
||||
--> tests/compile/scope_userdata_borrow.rs:15:46
|
||||
|
|
||||
11 | lua.scope(|scope| {
|
||||
| ----^^^^^ method not found in `Lua`
|
||||
| ----- has type `&mut mlua::scope::Scope<'_, '1>`
|
||||
...
|
||||
14 | let ibad = 42;
|
||||
| ---- binding `ibad` declared here
|
||||
15 | scope.create_userdata(MyUserData(&ibad)).unwrap();
|
||||
| ---------------------------------^^^^^--
|
||||
| | |
|
||||
| | borrowed value does not live long enough
|
||||
| argument requires that `ibad` is borrowed for `'1`
|
||||
16 | };
|
||||
| - `ibad` dropped here while still borrowed
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
use mlua::{AnyUserData, Lua, Table, UserData, Result};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
let globals = lua.globals();
|
||||
|
||||
// Should not allow userdata borrow to outlive lifetime of AnyUserData handle
|
||||
struct MyUserData;
|
||||
impl UserData for MyUserData {};
|
||||
let _userdata_ref;
|
||||
{
|
||||
let touter = globals.get::<Table>("touter")?;
|
||||
touter.set("userdata", lua.create_userdata(MyUserData)?)?;
|
||||
let userdata = touter.get::<AnyUserData>("userdata")?;
|
||||
_userdata_ref = userdata.borrow::<MyUserData>();
|
||||
//~^ error: `userdata` does not live long enough
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
error[E0597]: `userdata` does not live long enough
|
||||
--> $DIR/userdata_borrow.rs:15:25
|
||||
|
|
||||
15 | _userdata_ref = userdata.borrow::<MyUserData>();
|
||||
| ^^^^^^^^ borrowed value does not live long enough
|
||||
16 | //~^ error: `userdata` does not live long enough
|
||||
17 | }
|
||||
| - `userdata` dropped here while still borrowed
|
||||
18 | Ok(())
|
||||
19 | }
|
||||
| - borrow might be used here, when `_userdata_ref` is dropped and runs the destructor for type `std::result::Result<std::cell::Ref<'_, main::MyUserData>, mlua::error::Error>`
|
||||
|
|
||||
= note: values in a scope are dropped in the opposite order they are defined
|
||||
+250
-6
@@ -1,11 +1,13 @@
|
||||
use std::borrow::Cow;
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::ffi::{CStr, CString, OsString};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use bstr::BString;
|
||||
use maplit::{btreemap, btreeset, hashmap, hashset};
|
||||
use mlua::{
|
||||
AnyUserData, Error, Function, IntoLua, Lua, RegistryKey, Result, Table, Thread, UserDataRef, Value,
|
||||
AnyUserData, Either, Error, Function, IntoLua, Lua, RegistryKey, Result, Table, Thread, UserDataRef,
|
||||
Value,
|
||||
};
|
||||
|
||||
#[test]
|
||||
@@ -32,7 +34,7 @@ fn test_string_into_lua() -> Result<()> {
|
||||
// Direct conversion
|
||||
let s = lua.create_string("hello, world!")?;
|
||||
let s2 = (&s).into_lua(&lua)?;
|
||||
assert_eq!(s, s2.as_string().unwrap());
|
||||
assert_eq!(s, *s2.as_string().unwrap());
|
||||
|
||||
// Push into stack
|
||||
let table = lua.create_table()?;
|
||||
@@ -92,6 +94,21 @@ fn test_function_into_lua() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_function_from_lua() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
assert!(lua.globals().get::<Function>("print").is_ok());
|
||||
match lua.globals().get::<Function>("math") {
|
||||
Err(err @ Error::FromLuaConversionError { .. }) => {
|
||||
assert_eq!(err.to_string(), "error converting Lua table to function");
|
||||
}
|
||||
_ => panic!("expected `Error::FromLuaConversionError`"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_thread_into_lua() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
@@ -110,6 +127,20 @@ fn test_thread_into_lua() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_thread_from_lua() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
match lua.globals().get::<Thread>("print") {
|
||||
Err(err @ Error::FromLuaConversionError { .. }) => {
|
||||
assert_eq!(err.to_string(), "error converting Lua function to thread");
|
||||
}
|
||||
_ => panic!("expected `Error::FromLuaConversionError`"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_anyuserdata_into_lua() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
@@ -128,6 +159,45 @@ fn test_anyuserdata_into_lua() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_anyuserdata_from_lua() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
match lua.globals().get::<AnyUserData>("print") {
|
||||
Err(err @ Error::FromLuaConversionError { .. }) => {
|
||||
assert_eq!(err.to_string(), "error converting Lua function to userdata");
|
||||
}
|
||||
_ => panic!("expected `Error::FromLuaConversionError`"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_conversion() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
// Any Lua value can be converted to `Error`
|
||||
match lua.convert::<Error>(Error::external("external error")) {
|
||||
Ok(Error::ExternalError(msg)) => assert_eq!(msg.to_string(), "external error"),
|
||||
res => panic!("expected `Error::ExternalError`, got {res:?}"),
|
||||
}
|
||||
match lua.convert::<Error>("abc") {
|
||||
Ok(Error::RuntimeError(msg)) => assert_eq!(msg, "abc"),
|
||||
res => panic!("expected `Error::RuntimeError`, got {res:?}"),
|
||||
}
|
||||
match lua.convert::<Error>(true) {
|
||||
Ok(Error::RuntimeError(msg)) => assert_eq!(msg, "true"),
|
||||
res => panic!("expected `Error::RuntimeError`, got {res:?}"),
|
||||
}
|
||||
match lua.convert::<Error>(lua.globals()) {
|
||||
Ok(Error::RuntimeError(msg)) => assert!(msg.starts_with("table:")),
|
||||
res => panic!("expected `Error::RuntimeError`, got {res:?}"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_registry_value_into_lua() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
@@ -138,7 +208,7 @@ fn test_registry_value_into_lua() -> Result<()> {
|
||||
let value1 = lua.pack(&r)?;
|
||||
let value2 = lua.pack(r)?;
|
||||
assert_eq!(value1.as_str().as_deref(), Some("hello, world"));
|
||||
assert_eq!(value2.to_pointer(), value2.to_pointer());
|
||||
assert_eq!(value1.to_pointer(), value2.to_pointer());
|
||||
|
||||
// Push into stack
|
||||
let t = lua.create_table()?;
|
||||
@@ -173,6 +243,32 @@ fn test_registry_key_from_lua() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_into_lua() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
// Direct conversion
|
||||
assert!(true.into_lua(&lua)?.is_boolean());
|
||||
|
||||
// Push into stack
|
||||
let table = lua.create_table()?;
|
||||
table.set("b", true)?;
|
||||
assert_eq!(true, table.get::<bool>("b")?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_from_lua() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
assert!(lua.globals().get::<bool>("print")?);
|
||||
assert!(lua.convert::<bool>(123)?);
|
||||
assert!(!lua.convert::<bool>(Value::Nil)?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_integer_from_lua() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
@@ -383,8 +479,8 @@ fn test_bstring_from_lua() -> Result<()> {
|
||||
fn test_bstring_from_lua_buffer() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let b = lua.create_buffer("hello, world")?;
|
||||
let bstr = lua.unpack::<BString>(Value::UserData(b))?;
|
||||
let buf = lua.create_buffer("hello, world")?;
|
||||
let bstr = lua.convert::<BString>(buf)?;
|
||||
assert_eq!(bstr, "hello, world");
|
||||
|
||||
// Test from stack
|
||||
@@ -396,6 +492,55 @@ fn test_bstring_from_lua_buffer() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_osstring_into_from_lua() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let s = OsString::from("hello, world");
|
||||
|
||||
let v = lua.pack(s.as_os_str())?;
|
||||
assert!(v.is_string());
|
||||
assert_eq!(v.as_str().unwrap(), "hello, world");
|
||||
|
||||
let v = lua.pack(s)?;
|
||||
assert!(v.is_string());
|
||||
assert_eq!(v.as_str().unwrap(), "hello, world");
|
||||
|
||||
let s = lua.create_string("hello, world")?;
|
||||
let bstr = lua.unpack::<OsString>(Value::String(s))?;
|
||||
assert_eq!(bstr, "hello, world");
|
||||
|
||||
let bstr = lua.unpack::<OsString>(Value::Integer(123))?;
|
||||
assert_eq!(bstr, "123");
|
||||
|
||||
let bstr = lua.unpack::<OsString>(Value::Number(-123.55))?;
|
||||
assert_eq!(bstr, "-123.55");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pathbuf_into_from_lua() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let pb = PathBuf::from(env!("CARGO_TARGET_TMPDIR"));
|
||||
let pb_str = pb.to_str().unwrap();
|
||||
|
||||
let v = lua.pack(pb.as_path())?;
|
||||
assert!(v.is_string());
|
||||
assert_eq!(v.as_str().unwrap(), pb_str);
|
||||
|
||||
let v = lua.pack(pb.clone())?;
|
||||
assert!(v.is_string());
|
||||
assert_eq!(v.as_str().unwrap(), pb_str);
|
||||
|
||||
let s = lua.create_string(pb_str)?;
|
||||
let bstr = lua.unpack::<PathBuf>(Value::String(s))?;
|
||||
assert_eq!(bstr, pb);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_option_into_from_lua() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
@@ -413,3 +558,102 @@ fn test_option_into_from_lua() -> Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_either_enum() -> Result<()> {
|
||||
// Left
|
||||
let mut either = Either::<_, String>::Left(42);
|
||||
assert!(either.is_left());
|
||||
assert_eq!(*either.as_ref().left().unwrap(), 42);
|
||||
*either.as_mut().left().unwrap() = 44;
|
||||
assert_eq!(*either.as_ref().left().unwrap(), 44);
|
||||
assert_eq!(format!("{either}"), "44");
|
||||
assert_eq!(either.right(), None);
|
||||
|
||||
// Right
|
||||
either = Either::Right("hello".to_string());
|
||||
assert!(either.is_right());
|
||||
assert_eq!(*either.as_ref().right().unwrap(), "hello");
|
||||
*either.as_mut().right().unwrap() = "world".to_string();
|
||||
assert_eq!(*either.as_ref().right().unwrap(), "world");
|
||||
assert_eq!(format!("{either}"), "world");
|
||||
assert_eq!(either.left(), None);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_either_into_lua() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
// Direct conversion
|
||||
let mut either = Either::<i32, &Table>::Left(42);
|
||||
assert_eq!(either.into_lua(&lua)?, Value::Integer(42));
|
||||
let t = lua.create_table()?;
|
||||
either = Either::Right(&t);
|
||||
assert!(matches!(either.into_lua(&lua)?, Value::Table(_)));
|
||||
|
||||
// Push into stack
|
||||
let f =
|
||||
lua.create_function(|_, either: Either<i32, Table>| either.right().unwrap().set("hello", "world"))?;
|
||||
let t = lua.create_table()?;
|
||||
either = Either::Right(&t);
|
||||
f.call::<()>(either)?;
|
||||
assert_eq!(t.get::<String>("hello")?, "world");
|
||||
|
||||
let f = lua.create_function(|_, either: Either<i32, Table>| Ok(either.left().unwrap() + 1))?;
|
||||
either = Either::Left(42);
|
||||
assert_eq!(f.call::<i32>(either)?, 43);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_either_from_lua() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
// From value
|
||||
let mut either = lua.unpack::<Either<i32, Table>>(Value::Integer(42))?;
|
||||
assert!(either.is_left());
|
||||
assert_eq!(*either.as_ref().left().unwrap(), 42);
|
||||
let t = lua.create_table()?;
|
||||
either = lua.unpack::<Either<i32, Table>>(Value::Table(t.clone()))?;
|
||||
assert!(either.is_right());
|
||||
assert_eq!(either.as_ref().right().unwrap(), &t);
|
||||
match lua.unpack::<Either<i32, Table>>(Value::String(lua.create_string("abc")?)) {
|
||||
Err(Error::FromLuaConversionError { to, .. }) => assert_eq!(to, "Either<i32, Table>"),
|
||||
_ => panic!("expected `Error::FromLuaConversionError`"),
|
||||
}
|
||||
|
||||
// From stack
|
||||
let f = lua.create_function(|_, either: Either<i32, Table>| Ok(either))?;
|
||||
let either = f.call::<Either<i32, Table>>(42)?;
|
||||
assert!(either.is_left());
|
||||
assert_eq!(*either.as_ref().left().unwrap(), 42);
|
||||
|
||||
let either = f.call::<Either<i32, Table>>([5; 5])?;
|
||||
assert!(either.is_right());
|
||||
assert_eq!(either.as_ref().right().unwrap(), &[5; 5]);
|
||||
|
||||
// Check error message
|
||||
match f.call::<Value>("hello") {
|
||||
Ok(_) => panic!("expected error, got Ok"),
|
||||
Err(ref err @ Error::CallbackError { ref cause, .. }) => {
|
||||
match cause.as_ref() {
|
||||
Error::BadArgument { cause, .. } => match cause.as_ref() {
|
||||
Error::FromLuaConversionError { to, .. } => {
|
||||
assert_eq!(to, "Either<i32, Table>")
|
||||
}
|
||||
err => panic!("expected `Error::FromLuaConversionError`, got {err:?}"),
|
||||
},
|
||||
err => panic!("expected `Error::BadArgument`, got {err:?}"),
|
||||
}
|
||||
assert!(err
|
||||
.to_string()
|
||||
.starts_with("bad argument #1: error converting Lua string to Either<i32, Table>"),);
|
||||
}
|
||||
err => panic!("expected `Error::CallbackError`, got {err:?}"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+62
-9
@@ -1,4 +1,5 @@
|
||||
use std::io;
|
||||
use std::error::Error as _;
|
||||
use std::{fmt, io};
|
||||
|
||||
use mlua::{Error, ErrorContext, Lua, Result};
|
||||
|
||||
@@ -27,7 +28,6 @@ fn test_error_context() -> Result<()> {
|
||||
.load("local _, err = pcall(func2); return tostring(err)")
|
||||
.eval::<String>()?;
|
||||
assert!(msg2.contains("failed to find global"));
|
||||
println!("{msg2}");
|
||||
assert!(msg2.contains("error converting Lua nil to String"));
|
||||
|
||||
// Rewrite context message and test `downcast_ref`
|
||||
@@ -36,13 +36,66 @@ fn test_error_context() -> Result<()> {
|
||||
.context("some context")
|
||||
.context("some new context")
|
||||
})?;
|
||||
let res = func3.call::<()>(()).err().unwrap();
|
||||
let Error::CallbackError { cause, .. } = &res else {
|
||||
unreachable!()
|
||||
};
|
||||
assert!(!res.to_string().contains("some context"));
|
||||
assert!(res.to_string().contains("some new context"));
|
||||
assert!(cause.downcast_ref::<io::Error>().is_some());
|
||||
let err = func3.call::<()>(()).unwrap_err();
|
||||
let err = err.parent().unwrap();
|
||||
assert!(!err.to_string().contains("some context"));
|
||||
assert!(err.to_string().contains("some new context"));
|
||||
assert!(err.downcast_ref::<io::Error>().is_some());
|
||||
assert!(err.downcast_ref::<fmt::Error>().is_none());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_chain() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
// Check that `Error::ExternalError` creates a chain with a single element
|
||||
let io_err = io::Error::new(io::ErrorKind::Other, "other");
|
||||
assert_eq!(Error::external(io_err).chain().count(), 1);
|
||||
|
||||
let func = lua.create_function(|_, ()| {
|
||||
let err = Error::external(io::Error::new(io::ErrorKind::Other, "other")).context("io error");
|
||||
Err::<(), _>(err)
|
||||
})?;
|
||||
let err = func.call::<()>(()).unwrap_err();
|
||||
assert_eq!(err.chain().count(), 3);
|
||||
for (i, err) in err.chain().enumerate() {
|
||||
match i {
|
||||
0 => assert!(matches!(err.downcast_ref(), Some(Error::CallbackError { .. }))),
|
||||
1 => assert!(matches!(err.downcast_ref(), Some(Error::WithContext { .. }))),
|
||||
2 => assert!(matches!(err.downcast_ref(), Some(io::Error { .. }))),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
let err = err.parent().unwrap();
|
||||
assert!(err.source().is_none()); // The source is included to the `Display` output
|
||||
assert!(err.to_string().contains("io error"));
|
||||
assert!(err.to_string().contains("other"));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "anyhow")]
|
||||
#[test]
|
||||
fn test_error_anyhow() -> Result<()> {
|
||||
use mlua::IntoLua;
|
||||
|
||||
let lua = Lua::new();
|
||||
|
||||
let err = anyhow::Error::msg("anyhow error");
|
||||
let val = err.into_lua(&lua)?;
|
||||
assert!(val.is_error());
|
||||
assert_eq!(val.as_error().unwrap().to_string(), "anyhow error");
|
||||
|
||||
// Try Error -> anyhow::Error -> Error roundtrip
|
||||
let err = Error::runtime("runtime error");
|
||||
let err = anyhow::Error::new(err);
|
||||
let err = err.into_lua(&lua)?;
|
||||
assert!(err.is_error());
|
||||
let err = err.as_error().unwrap();
|
||||
assert!(matches!(err, Error::RuntimeError(msg) if msg == "runtime error"));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+211
-90
@@ -1,27 +1,34 @@
|
||||
use mlua::{Function, Lua, Result, String, Table};
|
||||
use mlua::{Error, Function, Lua, Result, String, Table, Variadic};
|
||||
|
||||
#[test]
|
||||
fn test_function() -> Result<()> {
|
||||
fn test_function_call() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let globals = lua.globals();
|
||||
lua.load(
|
||||
r#"
|
||||
function concat(arg1, arg2)
|
||||
return arg1 .. arg2
|
||||
end
|
||||
"#,
|
||||
)
|
||||
.exec()?;
|
||||
|
||||
let concat = globals.get::<Function>("concat")?;
|
||||
let concat = lua
|
||||
.load(r#"function(arg1, arg2) return arg1 .. arg2 end"#)
|
||||
.eval::<Function>()?;
|
||||
assert_eq!(concat.call::<String>(("foo", "bar"))?, "foobar");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bind() -> Result<()> {
|
||||
fn test_function_call_error() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let concat_err = lua
|
||||
.load(r#"function(arg1, arg2) error("concat error") end"#)
|
||||
.eval::<Function>()?;
|
||||
match concat_err.call::<String>(("foo", "bar")) {
|
||||
Err(Error::RuntimeError(msg)) if msg.contains("concat error") => {}
|
||||
other => panic!("unexpected result: {other:?}"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_function_bind() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let globals = lua.globals();
|
||||
@@ -54,59 +61,13 @@ fn test_bind() -> Result<()> {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rust_function() -> Result<()> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn test_function_bind_error() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let globals = lua.globals();
|
||||
lua.load(
|
||||
r#"
|
||||
function lua_function()
|
||||
return rust_function()
|
||||
end
|
||||
|
||||
-- Test to make sure chunk return is ignored
|
||||
return 1
|
||||
"#,
|
||||
)
|
||||
.exec()?;
|
||||
|
||||
let lua_function = globals.get::<Function>("lua_function")?;
|
||||
let rust_function = lua.create_function(|_, ()| Ok("hello"))?;
|
||||
|
||||
globals.set("rust_function", rust_function)?;
|
||||
assert_eq!(lua_function.call::<String>(())?, "hello");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_c_function() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
unsafe extern "C-unwind" fn c_function(state: *mut mlua::lua_State) -> std::os::raw::c_int {
|
||||
ffi::lua_pushboolean(state, 1);
|
||||
ffi::lua_setglobal(state, b"c_function\0" as *const _ as *const _);
|
||||
0
|
||||
}
|
||||
|
||||
let func = unsafe { lua.create_c_function(c_function)? };
|
||||
func.call::<()>(())?;
|
||||
assert_eq!(lua.globals().get::<bool>("c_function")?, true);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "luau"))]
|
||||
#[test]
|
||||
fn test_dump() -> Result<()> {
|
||||
let lua = unsafe { Lua::unsafe_new() };
|
||||
|
||||
let concat_lua = lua
|
||||
.load(r#"function(arg1, arg2) return arg1 .. arg2 end"#)
|
||||
.eval::<Function>()?;
|
||||
let concat = lua.load(&concat_lua.dump(false)).into_function()?;
|
||||
|
||||
assert_eq!(concat.call::<String>(("foo", "bar"))?, "foobar");
|
||||
let func = lua.load(r#"function(...) end"#).eval::<Function>()?;
|
||||
assert!(func.bind(Variadic::from_iter(1..1000000)).is_err());
|
||||
assert!(func.call::<()>(Variadic::from_iter(1..1000000)).is_err());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -114,14 +75,15 @@ fn test_dump() -> Result<()> {
|
||||
#[test]
|
||||
fn test_function_environment() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
let globals = lua.globals();
|
||||
|
||||
// We must not get or set environment for C functions
|
||||
let rust_func = lua.create_function(|_, ()| Ok("hello"))?;
|
||||
assert_eq!(rust_func.environment(), None);
|
||||
assert_eq!(rust_func.set_environment(lua.globals()).ok(), Some(false));
|
||||
assert_eq!(rust_func.set_environment(globals.clone()).ok(), Some(false));
|
||||
|
||||
// Test getting Lua function environment
|
||||
lua.globals().set("hello", "global")?;
|
||||
globals.set("hello", "global")?;
|
||||
let lua_func = lua
|
||||
.load(
|
||||
r#"
|
||||
@@ -135,7 +97,7 @@ fn test_function_environment() -> Result<()> {
|
||||
.eval::<Function>()?;
|
||||
let lua_func2 = lua.load("return hello").into_function()?;
|
||||
assert_eq!(lua_func.call::<String>(())?, "global");
|
||||
assert_eq!(lua_func.environment(), Some(lua.globals()));
|
||||
assert_eq!(lua_func.environment().as_ref(), Some(&globals));
|
||||
|
||||
// Test changing the environment
|
||||
let env = lua.create_table_from([("hello", "local")])?;
|
||||
@@ -154,9 +116,9 @@ fn test_function_environment() -> Result<()> {
|
||||
"#,
|
||||
)
|
||||
.exec()?;
|
||||
let lucky = lua.globals().get::<Function>("lucky")?;
|
||||
let lucky = globals.get::<Function>("lucky")?;
|
||||
assert_eq!(lucky.call::<String>(())?, "number is 15");
|
||||
let new_env = lua.globals().get::<Table>("new_env")?;
|
||||
let new_env = globals.get::<Table>("new_env")?;
|
||||
lucky.set_environment(new_env)?;
|
||||
assert_eq!(lucky.call::<String>(())?, "15");
|
||||
|
||||
@@ -235,6 +197,95 @@ fn test_function_info() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "luau"))]
|
||||
#[test]
|
||||
fn test_function_dump() -> Result<()> {
|
||||
let lua = unsafe { Lua::unsafe_new() };
|
||||
|
||||
let concat_lua = lua
|
||||
.load(r#"function(arg1, arg2) return arg1 .. arg2 end"#)
|
||||
.eval::<Function>()?;
|
||||
let concat = lua.load(&concat_lua.dump(false)).into_function()?;
|
||||
|
||||
assert_eq!(concat.call::<String>(("foo", "bar"))?, "foobar");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "luau")]
|
||||
#[test]
|
||||
fn test_finction_coverage() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
lua.set_compiler(mlua::Compiler::default().set_coverage_level(1));
|
||||
|
||||
let f = lua
|
||||
.load(
|
||||
r#"local s = "abc"
|
||||
assert(#s == 3)
|
||||
|
||||
function abc(i)
|
||||
if i < 5 then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
end
|
||||
end
|
||||
|
||||
(function()
|
||||
(function() abc(10) end)()
|
||||
end)()
|
||||
"#,
|
||||
)
|
||||
.into_function()?;
|
||||
|
||||
f.call::<()>(())?;
|
||||
|
||||
let mut report = Vec::new();
|
||||
f.coverage(|cov| {
|
||||
report.push(cov);
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
report[0],
|
||||
mlua::CoverageInfo {
|
||||
function: None,
|
||||
line_defined: 1,
|
||||
depth: 0,
|
||||
hits: vec![-1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1],
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
report[1],
|
||||
mlua::CoverageInfo {
|
||||
function: Some("abc".into()),
|
||||
line_defined: 4,
|
||||
depth: 1,
|
||||
hits: vec![-1, -1, -1, -1, -1, 1, 0, -1, 1, -1, -1, -1, -1, -1, -1, -1],
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
report[2],
|
||||
mlua::CoverageInfo {
|
||||
function: None,
|
||||
line_defined: 12,
|
||||
depth: 1,
|
||||
hits: vec![-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1],
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
report[3],
|
||||
mlua::CoverageInfo {
|
||||
function: None,
|
||||
line_defined: 13,
|
||||
depth: 2,
|
||||
hits: vec![-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1],
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_function_pointer() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
@@ -271,31 +322,101 @@ fn test_function_deep_clone() -> Result<()> {
|
||||
|
||||
#[test]
|
||||
fn test_function_wrap() -> Result<()> {
|
||||
use mlua::Error;
|
||||
|
||||
let lua = Lua::new();
|
||||
|
||||
lua.globals().set("f", Function::wrap(|_, s: String| Ok(s)))?;
|
||||
lua.load(r#"assert(f("hello") == "hello")"#).exec().unwrap();
|
||||
let f = Function::wrap(|s: String, n| Ok(s.to_str().unwrap().repeat(n)));
|
||||
lua.globals().set("f", f)?;
|
||||
lua.load(r#"assert(f("hello", 2) == "hellohello")"#)
|
||||
.exec()
|
||||
.unwrap();
|
||||
|
||||
let mut _i = false;
|
||||
lua.globals().set(
|
||||
"f",
|
||||
Function::wrap_mut(move |lua, ()| {
|
||||
_i = true;
|
||||
lua.globals().get::<Function>("f")?.call::<()>(())
|
||||
}),
|
||||
)?;
|
||||
match lua.globals().get::<Function>("f")?.call::<()>(()) {
|
||||
Err(Error::CallbackError { ref cause, .. }) => match *cause.as_ref() {
|
||||
Error::CallbackError { ref cause, .. } => match *cause.as_ref() {
|
||||
Error::RecursiveMutCallback { .. } => {}
|
||||
ref other => panic!("incorrect result: {other:?}"),
|
||||
},
|
||||
ref other => panic!("incorrect result: {other:?}"),
|
||||
// Return error
|
||||
let ferr = Function::wrap(|| Err::<(), _>(Error::runtime("some error")));
|
||||
lua.globals().set("ferr", ferr)?;
|
||||
lua.load(
|
||||
r#"
|
||||
local ok, err = pcall(ferr)
|
||||
assert(not ok and tostring(err):find("some error"))
|
||||
"#,
|
||||
)
|
||||
.exec()
|
||||
.unwrap();
|
||||
|
||||
// Mutable callback
|
||||
let mut i = 0;
|
||||
let fmut = Function::wrap_mut(move || {
|
||||
i += 1;
|
||||
Ok(i)
|
||||
});
|
||||
lua.globals().set("fmut", fmut)?;
|
||||
lua.load(r#"fmut(); fmut(); assert(fmut() == 3)"#).exec().unwrap();
|
||||
|
||||
// Check mutable callback with error
|
||||
let fmut_err = Function::wrap_mut(|| Err::<(), _>(Error::runtime("some error")));
|
||||
lua.globals().set("fmut_err", fmut_err)?;
|
||||
lua.load(
|
||||
r#"
|
||||
local ok, err = pcall(fmut_err)
|
||||
assert(not ok and tostring(err):find("some error"))
|
||||
"#,
|
||||
)
|
||||
.exec()
|
||||
.unwrap();
|
||||
|
||||
// Check recursive mut callback error
|
||||
let fmut = Function::wrap_mut(|f: Function| match f.call::<()>(&f) {
|
||||
Err(Error::CallbackError { cause, .. }) => match cause.as_ref() {
|
||||
Error::RecursiveMutCallback { .. } => Ok(()),
|
||||
other => panic!("incorrect result: {other:?}"),
|
||||
},
|
||||
other => panic!("incorrect result: {other:?}"),
|
||||
};
|
||||
});
|
||||
let fmut = lua.convert::<Function>(fmut)?;
|
||||
assert!(fmut.call::<()>(&fmut).is_ok());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_function_wrap_raw() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let f = Function::wrap_raw(|| "hello");
|
||||
lua.globals().set("f", f)?;
|
||||
lua.load(r#"assert(f() == "hello")"#).exec().unwrap();
|
||||
|
||||
// Return error
|
||||
let ferr = Function::wrap_raw(|| Err::<(), _>("some error"));
|
||||
lua.globals().set("ferr", ferr)?;
|
||||
lua.load(
|
||||
r#"
|
||||
local _, err = ferr()
|
||||
assert(err == "some error")
|
||||
"#,
|
||||
)
|
||||
.exec()
|
||||
.unwrap();
|
||||
|
||||
// Mutable callback
|
||||
let mut i = 0;
|
||||
let fmut = Function::wrap_raw_mut(move || {
|
||||
i += 1;
|
||||
i
|
||||
});
|
||||
lua.globals().set("fmut", fmut)?;
|
||||
lua.load(r#"fmut(); fmut(); assert(fmut() == 3)"#).exec().unwrap();
|
||||
|
||||
// Check mutable callback with error
|
||||
let fmut_err = Function::wrap_raw_mut(|| Err::<(), _>("some error"));
|
||||
lua.globals().set("fmut_err", fmut_err)?;
|
||||
lua.load(
|
||||
r#"
|
||||
local _, err = fmut_err()
|
||||
assert(err == "some error")
|
||||
"#,
|
||||
)
|
||||
.exec()
|
||||
.unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+43
-7
@@ -4,7 +4,7 @@ use std::ops::Deref;
|
||||
use std::sync::atomic::{AtomicI64, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use mlua::{DebugEvent, Error, HookTriggers, Lua, Result, Value};
|
||||
use mlua::{DebugEvent, Error, HookTriggers, Lua, Result, ThreadStatus, Value, VmState};
|
||||
|
||||
#[test]
|
||||
fn test_hook_triggers() {
|
||||
@@ -26,7 +26,7 @@ fn test_line_counts() -> Result<()> {
|
||||
lua.set_hook(HookTriggers::EVERY_LINE, move |_lua, debug| {
|
||||
assert_eq!(debug.event(), DebugEvent::Line);
|
||||
hook_output.lock().unwrap().push(debug.curr_line());
|
||||
Ok(())
|
||||
Ok(VmState::Continue)
|
||||
});
|
||||
lua.load(
|
||||
r#"
|
||||
@@ -61,7 +61,7 @@ fn test_function_calls() -> Result<()> {
|
||||
let source = debug.source();
|
||||
let name = names.name.map(|s| s.into_owned());
|
||||
hook_output.lock().unwrap().push((name, source.what));
|
||||
Ok(())
|
||||
Ok(VmState::Continue)
|
||||
});
|
||||
|
||||
lua.load(
|
||||
@@ -120,7 +120,7 @@ fn test_limit_execution_instructions() -> Result<()> {
|
||||
if max_instructions.fetch_sub(30, Ordering::Relaxed) <= 30 {
|
||||
Err(Error::runtime("time's up"))
|
||||
} else {
|
||||
Ok(())
|
||||
Ok(VmState::Continue)
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -191,10 +191,10 @@ fn test_hook_swap_within_hook() -> Result<()> {
|
||||
TL_LUA.with(|tl| {
|
||||
tl.borrow().as_ref().unwrap().remove_hook();
|
||||
});
|
||||
Ok(())
|
||||
Ok(VmState::Continue)
|
||||
})
|
||||
});
|
||||
Ok(())
|
||||
Ok(VmState::Continue)
|
||||
})
|
||||
});
|
||||
|
||||
@@ -234,7 +234,7 @@ fn test_hook_threads() -> Result<()> {
|
||||
co.set_hook(HookTriggers::EVERY_LINE, move |_lua, debug| {
|
||||
assert_eq!(debug.event(), DebugEvent::Line);
|
||||
hook_output.lock().unwrap().push(debug.curr_line());
|
||||
Ok(())
|
||||
Ok(VmState::Continue)
|
||||
});
|
||||
|
||||
co.resume::<()>(())?;
|
||||
@@ -249,3 +249,39 @@ fn test_hook_threads() -> Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hook_yield() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let func = lua
|
||||
.load(
|
||||
r#"
|
||||
local x = 2 + 3
|
||||
local y = x * 63
|
||||
local z = string.len(x..", "..y)
|
||||
"#,
|
||||
)
|
||||
.into_function()?;
|
||||
let co = lua.create_thread(func)?;
|
||||
|
||||
co.set_hook(HookTriggers::EVERY_LINE, move |_lua, _debug| Ok(VmState::Yield));
|
||||
|
||||
#[cfg(any(feature = "lua54", feature = "lua53"))]
|
||||
{
|
||||
assert!(co.resume::<()>(()).is_ok());
|
||||
assert!(co.resume::<()>(()).is_ok());
|
||||
assert!(co.resume::<()>(()).is_ok());
|
||||
assert!(co.resume::<()>(()).is_ok());
|
||||
assert!(co.status() == ThreadStatus::Finished);
|
||||
}
|
||||
#[cfg(any(feature = "lua51", feature = "lua52", feature = "luajit"))]
|
||||
{
|
||||
assert!(
|
||||
matches!(co.resume::<()>(()), Err(Error::RuntimeError(err)) if err.contains("attempt to yield from a hook"))
|
||||
);
|
||||
assert!(co.status() == ThreadStatus::Error);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+2
-104
@@ -6,10 +6,7 @@ use std::panic::{catch_unwind, AssertUnwindSafe};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
use mlua::{
|
||||
Compiler, CoverageInfo, Error, Lua, LuaOptions, Result, StdLib, Table, ThreadStatus, Value, Vector,
|
||||
VmState,
|
||||
};
|
||||
use mlua::{Compiler, Error, Lua, LuaOptions, Result, StdLib, Table, ThreadStatus, Value, Vector, VmState};
|
||||
|
||||
#[test]
|
||||
fn test_version() -> Result<()> {
|
||||
@@ -194,7 +191,7 @@ fn test_vector_metatable() -> Result<()> {
|
||||
)
|
||||
.eval::<Table>()?;
|
||||
vector_mt.set_metatable(Some(vector_mt.clone()));
|
||||
lua.set_vector_metatable(Some(vector_mt.clone()));
|
||||
lua.set_type_metatable::<Vector>(Some(vector_mt.clone()));
|
||||
lua.globals().set("Vector3", vector_mt)?;
|
||||
|
||||
let compiler = Compiler::new().set_vector_lib("Vector3").set_vector_ctor("new");
|
||||
@@ -392,105 +389,6 @@ fn test_interrupts() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_coverage() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
lua.set_compiler(Compiler::default().set_coverage_level(1));
|
||||
|
||||
let f = lua
|
||||
.load(
|
||||
r#"local s = "abc"
|
||||
assert(#s == 3)
|
||||
|
||||
function abc(i)
|
||||
if i < 5 then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
end
|
||||
end
|
||||
|
||||
(function()
|
||||
(function() abc(10) end)()
|
||||
end)()
|
||||
"#,
|
||||
)
|
||||
.into_function()?;
|
||||
|
||||
f.call::<()>(())?;
|
||||
|
||||
let mut report = Vec::new();
|
||||
f.coverage(|cov| {
|
||||
report.push(cov);
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
report[0],
|
||||
CoverageInfo {
|
||||
function: None,
|
||||
line_defined: 1,
|
||||
depth: 0,
|
||||
hits: vec![-1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1],
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
report[1],
|
||||
CoverageInfo {
|
||||
function: Some("abc".into()),
|
||||
line_defined: 4,
|
||||
depth: 1,
|
||||
hits: vec![-1, -1, -1, -1, -1, 1, 0, -1, 1, -1, -1, -1, -1, -1, -1, -1],
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
report[2],
|
||||
CoverageInfo {
|
||||
function: None,
|
||||
line_defined: 12,
|
||||
depth: 1,
|
||||
hits: vec![-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1],
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
report[3],
|
||||
CoverageInfo {
|
||||
function: None,
|
||||
line_defined: 13,
|
||||
depth: 2,
|
||||
hits: vec![-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1],
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_buffer() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let buf1 = lua
|
||||
.load(
|
||||
r#"
|
||||
local buf = buffer.fromstring("hello")
|
||||
assert(buffer.len(buf) == 5)
|
||||
return buf
|
||||
"#,
|
||||
)
|
||||
.eval::<Value>()?;
|
||||
assert!(buf1.is_userdata() && buf1.is_buffer());
|
||||
assert_eq!(buf1.type_name(), "buffer");
|
||||
|
||||
let buf2 = lua.load("buffer.fromstring('hello')").eval::<Value>()?;
|
||||
assert_ne!(buf1, buf2);
|
||||
|
||||
// Check that we can pass buffer type to Lua
|
||||
let func = lua.create_function(|_, buf: Value| return buf.to_string())?;
|
||||
assert!(func.call::<String>(buf1)?.starts_with("buffer:"));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fflags() {
|
||||
// We cannot really on any particular feature flag to be present
|
||||
|
||||
+142
-167
@@ -1,11 +1,10 @@
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
use std::string::String as StdString;
|
||||
use std::sync::Arc;
|
||||
|
||||
use mlua::{
|
||||
AnyUserData, Error, Function, Lua, MetaMethod, Result, String, UserData, UserDataFields,
|
||||
UserDataMethods,
|
||||
AnyUserData, Error, Function, Lua, MetaMethod, ObjectLike, Result, String, UserData, UserDataFields,
|
||||
UserDataMethods, UserDataRegistry,
|
||||
};
|
||||
|
||||
#[test]
|
||||
@@ -14,20 +13,20 @@ fn test_scope_func() -> Result<()> {
|
||||
|
||||
let rc = Rc::new(Cell::new(0));
|
||||
lua.scope(|scope| {
|
||||
let r = rc.clone();
|
||||
let rc2 = rc.clone();
|
||||
let f = scope.create_function(move |_, ()| {
|
||||
r.set(42);
|
||||
rc2.set(42);
|
||||
Ok(())
|
||||
})?;
|
||||
lua.globals().set("bad", f.clone())?;
|
||||
f.call::<_, ()>(())?;
|
||||
lua.globals().set("f", &f)?;
|
||||
f.call::<()>(())?;
|
||||
assert_eq!(Rc::strong_count(&rc), 2);
|
||||
Ok(())
|
||||
})?;
|
||||
assert_eq!(rc.get(), 42);
|
||||
assert_eq!(Rc::strong_count(&rc), 1);
|
||||
|
||||
match lua.globals().get::<_, Function>("bad")?.call::<_, ()>(()) {
|
||||
match lua.globals().get::<Function>("f")?.call::<()>(()) {
|
||||
Err(Error::CallbackError { ref cause, .. }) => match *cause.as_ref() {
|
||||
Error::CallbackDestructed => {}
|
||||
ref err => panic!("wrong error type {:?}", err),
|
||||
@@ -49,7 +48,7 @@ fn test_scope_capture() -> Result<()> {
|
||||
i = 42;
|
||||
Ok(())
|
||||
})?
|
||||
.call::<_, ()>(())
|
||||
.call::<()>(())
|
||||
})?;
|
||||
assert_eq!(i, 42);
|
||||
|
||||
@@ -61,12 +60,8 @@ fn test_scope_outer_lua_access() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let table = lua.create_table()?;
|
||||
lua.scope(|scope| {
|
||||
scope
|
||||
.create_function_mut(|_, ()| table.set("a", "b"))?
|
||||
.call::<_, ()>(())
|
||||
})?;
|
||||
assert_eq!(table.get::<_, String>("a")?, "b");
|
||||
lua.scope(|scope| scope.create_function(|_, ()| table.set("a", "b"))?.call::<()>(()))?;
|
||||
assert_eq!(table.get::<String>("a")?, "b");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -75,11 +70,11 @@ fn test_scope_outer_lua_access() -> Result<()> {
|
||||
fn test_scope_userdata_fields() -> Result<()> {
|
||||
struct MyUserData<'a>(&'a Cell<i64>);
|
||||
|
||||
impl<'a> UserData for MyUserData<'a> {
|
||||
fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) {
|
||||
fields.add_field("field", "hello");
|
||||
fields.add_field_method_get("val", |_, data| Ok(data.0.get()));
|
||||
fields.add_field_method_set("val", |_, data, val| {
|
||||
impl UserData for MyUserData<'_> {
|
||||
fn register(reg: &mut UserDataRegistry<Self>) {
|
||||
reg.add_field("field", "hello");
|
||||
reg.add_field_method_get("val", |_, data| Ok(data.0.get()));
|
||||
reg.add_field_method_set("val", |_, data, val| {
|
||||
data.0.set(val);
|
||||
Ok(())
|
||||
});
|
||||
@@ -101,7 +96,7 @@ fn test_scope_userdata_fields() -> Result<()> {
|
||||
)
|
||||
.eval()?;
|
||||
|
||||
lua.scope(|scope| f.call::<_, ()>(scope.create_nonstatic_userdata(MyUserData(&i))?))?;
|
||||
lua.scope(|scope| f.call::<()>(scope.create_userdata(MyUserData(&i))?))?;
|
||||
|
||||
assert_eq!(i.get(), 44);
|
||||
|
||||
@@ -112,14 +107,14 @@ fn test_scope_userdata_fields() -> Result<()> {
|
||||
fn test_scope_userdata_methods() -> Result<()> {
|
||||
struct MyUserData<'a>(&'a Cell<i64>);
|
||||
|
||||
impl<'a> UserData for MyUserData<'a> {
|
||||
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||
methods.add_method("inc", |_, data, ()| {
|
||||
impl UserData for MyUserData<'_> {
|
||||
fn register(reg: &mut UserDataRegistry<Self>) {
|
||||
reg.add_method("inc", |_, data, ()| {
|
||||
data.0.set(data.0.get() + 1);
|
||||
Ok(())
|
||||
});
|
||||
|
||||
methods.add_method("dec", |_, data, ()| {
|
||||
reg.add_method("dec", |_, data, ()| {
|
||||
data.0.set(data.0.get() - 1);
|
||||
Ok(())
|
||||
});
|
||||
@@ -142,7 +137,7 @@ fn test_scope_userdata_methods() -> Result<()> {
|
||||
)
|
||||
.eval()?;
|
||||
|
||||
lua.scope(|scope| f.call::<_, ()>(scope.create_nonstatic_userdata(MyUserData(&i))?))?;
|
||||
lua.scope(|scope| f.call::<()>(scope.create_userdata(MyUserData(&i))?))?;
|
||||
|
||||
assert_eq!(i.get(), 44);
|
||||
|
||||
@@ -150,19 +145,19 @@ fn test_scope_userdata_methods() -> Result<()> {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_scope_userdata_functions() -> Result<()> {
|
||||
fn test_scope_userdata_ops() -> Result<()> {
|
||||
struct MyUserData<'a>(&'a i64);
|
||||
|
||||
impl<'a> UserData for MyUserData<'a> {
|
||||
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||
methods.add_meta_method(MetaMethod::Add, |lua, this, ()| {
|
||||
impl UserData for MyUserData<'_> {
|
||||
fn register(reg: &mut UserDataRegistry<Self>) {
|
||||
reg.add_meta_method(MetaMethod::Add, |lua, this, ()| {
|
||||
let globals = lua.globals();
|
||||
globals.set("i", globals.get::<_, i64>("i")? + this.0)?;
|
||||
globals.set("i", globals.get::<i64>("i")? + this.0)?;
|
||||
Ok(())
|
||||
});
|
||||
methods.add_meta_method(MetaMethod::Sub, |lua, this, ()| {
|
||||
reg.add_meta_method(MetaMethod::Sub, |lua, this, ()| {
|
||||
let globals = lua.globals();
|
||||
globals.set("i", globals.get::<_, i64>("i")? + this.0)?;
|
||||
globals.set("i", globals.get::<i64>("i")? + this.0)?;
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
@@ -184,9 +179,34 @@ fn test_scope_userdata_functions() -> Result<()> {
|
||||
)
|
||||
.eval::<Function>()?;
|
||||
|
||||
lua.scope(|scope| f.call::<_, ()>(scope.create_nonstatic_userdata(MyUserData(&dummy))?))?;
|
||||
lua.scope(|scope| f.call::<()>(scope.create_userdata(MyUserData(&dummy))?))?;
|
||||
|
||||
assert_eq!(lua.globals().get::<_, i64>("i")?, 3);
|
||||
assert_eq!(lua.globals().get::<i64>("i")?, 3);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_scope_userdata_values() -> Result<()> {
|
||||
struct MyUserData<'a>(&'a i64);
|
||||
|
||||
impl UserData for MyUserData<'_> {
|
||||
fn register(registry: &mut UserDataRegistry<Self>) {
|
||||
registry.add_method("get", |_, data, ()| Ok(*data.0));
|
||||
}
|
||||
}
|
||||
|
||||
let lua = Lua::new();
|
||||
|
||||
let i = 42;
|
||||
let data = MyUserData(&i);
|
||||
lua.scope(|scope| {
|
||||
let ud = scope.create_userdata(data)?;
|
||||
assert_eq!(ud.call_method::<i64>("get", &ud)?, 42);
|
||||
ud.set_user_value("user_value")?;
|
||||
assert_eq!(ud.user_value::<String>()?, "user_value");
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -196,8 +216,8 @@ fn test_scope_userdata_mismatch() -> Result<()> {
|
||||
struct MyUserData<'a>(&'a Cell<i64>);
|
||||
|
||||
impl<'a> UserData for MyUserData<'a> {
|
||||
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||
methods.add_method("inc", |_, data, ()| {
|
||||
fn register(reg: &mut UserDataRegistry<Self>) {
|
||||
reg.add_method("inc", |_, data, ()| {
|
||||
data.0.set(data.0.get() + 1);
|
||||
Ok(())
|
||||
});
|
||||
@@ -208,13 +228,7 @@ fn test_scope_userdata_mismatch() -> Result<()> {
|
||||
|
||||
lua.load(
|
||||
r#"
|
||||
function okay(a, b)
|
||||
a.inc(a)
|
||||
b.inc(b)
|
||||
end
|
||||
function bad(a, b)
|
||||
a.inc(b)
|
||||
end
|
||||
function inc(a, b) a.inc(b) end
|
||||
"#,
|
||||
)
|
||||
.exec()?;
|
||||
@@ -222,29 +236,22 @@ fn test_scope_userdata_mismatch() -> Result<()> {
|
||||
let a = Cell::new(1);
|
||||
let b = Cell::new(1);
|
||||
|
||||
let okay: Function = lua.globals().get("okay")?;
|
||||
let bad: Function = lua.globals().get("bad")?;
|
||||
|
||||
let inc: Function = lua.globals().get("inc")?;
|
||||
lua.scope(|scope| {
|
||||
let au = scope.create_nonstatic_userdata(MyUserData(&a))?;
|
||||
let bu = scope.create_nonstatic_userdata(MyUserData(&b))?;
|
||||
assert!(okay.call::<_, ()>((au.clone(), bu.clone())).is_ok());
|
||||
match bad.call::<_, ()>((au, bu)) {
|
||||
let au = scope.create_userdata(MyUserData(&a))?;
|
||||
let bu = scope.create_userdata(MyUserData(&b))?;
|
||||
assert!(inc.call::<()>((&au, &au)).is_ok());
|
||||
match inc.call::<()>((&au, &bu)) {
|
||||
Err(Error::CallbackError { ref cause, .. }) => match cause.as_ref() {
|
||||
Error::BadArgument {
|
||||
to,
|
||||
pos,
|
||||
name,
|
||||
cause,
|
||||
} => {
|
||||
Error::BadArgument { to, pos, name, cause } => {
|
||||
assert_eq!(to.as_deref(), Some("MyUserData.inc"));
|
||||
assert_eq!(*pos, 1);
|
||||
assert_eq!(name.as_deref(), Some("self"));
|
||||
assert!(matches!(*cause.as_ref(), Error::UserDataTypeMismatch));
|
||||
}
|
||||
other => panic!("wrong error type {:?}", other),
|
||||
other => panic!("wrong error type {other:?}"),
|
||||
},
|
||||
Err(other) => panic!("wrong error type {:?}", other),
|
||||
Err(other) => panic!("wrong error type {other:?}"),
|
||||
Ok(_) => panic!("incorrectly returned Ok"),
|
||||
}
|
||||
Ok(())
|
||||
@@ -257,114 +264,46 @@ fn test_scope_userdata_mismatch() -> Result<()> {
|
||||
fn test_scope_userdata_drop() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
struct MyUserData(#[allow(unused)] Rc<()>);
|
||||
struct MyUserData<'a>(&'a Cell<i64>, #[allow(unused)] Rc<()>);
|
||||
|
||||
impl UserData for MyUserData {
|
||||
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||
methods.add_method("method", |_, _, ()| Ok(()));
|
||||
}
|
||||
}
|
||||
|
||||
struct MyUserDataArc(#[allow(unused)] Arc<()>);
|
||||
|
||||
impl UserData for MyUserDataArc {}
|
||||
|
||||
let rc = Rc::new(());
|
||||
let arc = Arc::new(());
|
||||
lua.scope(|scope| {
|
||||
let ud = scope.create_userdata(MyUserData(rc.clone()))?;
|
||||
ud.set_user_value(MyUserDataArc(arc.clone()))?;
|
||||
lua.globals().set("ud", ud)?;
|
||||
assert_eq!(Rc::strong_count(&rc), 2);
|
||||
assert_eq!(Arc::strong_count(&arc), 2);
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
lua.gc_collect()?;
|
||||
assert_eq!(Rc::strong_count(&rc), 1);
|
||||
assert_eq!(Arc::strong_count(&arc), 1);
|
||||
|
||||
match lua.load("ud:method()").exec() {
|
||||
Err(Error::CallbackError { ref cause, .. }) => match cause.as_ref() {
|
||||
Error::CallbackDestructed => {}
|
||||
err => panic!("expected CallbackDestructed, got {:?}", err),
|
||||
},
|
||||
r => panic!("improper return for destructed userdata: {:?}", r),
|
||||
};
|
||||
|
||||
let ud = lua.globals().get::<_, AnyUserData>("ud")?;
|
||||
match ud.borrow::<MyUserData>() {
|
||||
Ok(_) => panic!("succesfull borrow for destructed userdata"),
|
||||
Err(Error::UserDataDestructed) => {}
|
||||
Err(err) => panic!("improper borrow error for destructed userdata: {:?}", err),
|
||||
}
|
||||
|
||||
match ud.get_metatable() {
|
||||
Ok(_) => panic!("successful metatable retrieval of destructed userdata"),
|
||||
Err(Error::UserDataDestructed) => {}
|
||||
Err(err) => panic!(
|
||||
"improper metatable error for destructed userdata: {:?}",
|
||||
err
|
||||
),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_scope_nonstatic_userdata_drop() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
struct MyUserData<'a>(&'a Cell<i64>, #[allow(unused)] Arc<()>);
|
||||
|
||||
impl<'a> UserData for MyUserData<'a> {
|
||||
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||
methods.add_method("inc", |_, data, ()| {
|
||||
impl UserData for MyUserData<'_> {
|
||||
fn register(reg: &mut UserDataRegistry<Self>) {
|
||||
reg.add_method("inc", |_, data, ()| {
|
||||
data.0.set(data.0.get() + 1);
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
struct MyUserDataArc(#[allow(unused)] Arc<()>);
|
||||
|
||||
impl UserData for MyUserDataArc {}
|
||||
|
||||
let i = Cell::new(1);
|
||||
let arc = Arc::new(());
|
||||
let (i, rc) = (Cell::new(1), Rc::new(()));
|
||||
lua.scope(|scope| {
|
||||
let ud = scope.create_nonstatic_userdata(MyUserData(&i, arc.clone()))?;
|
||||
ud.set_user_value(MyUserDataArc(arc.clone()))?;
|
||||
let ud = scope.create_userdata(MyUserData(&i, rc.clone()))?;
|
||||
lua.globals().set("ud", ud)?;
|
||||
lua.load("ud:inc()").exec()?;
|
||||
assert_eq!(Arc::strong_count(&arc), 3);
|
||||
assert_eq!(Rc::strong_count(&rc), 2);
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
lua.gc_collect()?;
|
||||
assert_eq!(Arc::strong_count(&arc), 1);
|
||||
assert_eq!(Rc::strong_count(&rc), 1);
|
||||
assert_eq!(i.get(), 2);
|
||||
|
||||
match lua.load("ud:inc()").exec() {
|
||||
Err(Error::CallbackError { ref cause, .. }) => match cause.as_ref() {
|
||||
Error::CallbackDestructed => {}
|
||||
err => panic!("expected CallbackDestructed, got {:?}", err),
|
||||
Error::UserDataDestructed => {}
|
||||
err => panic!("expected UserDataDestructed, got {err:?}"),
|
||||
},
|
||||
r => panic!("improper return for destructed userdata: {:?}", r),
|
||||
r => panic!("improper return for destructed userdata: {r:?}"),
|
||||
};
|
||||
|
||||
let ud = lua.globals().get::<_, AnyUserData>("ud")?;
|
||||
match ud.borrow::<MyUserData>() {
|
||||
let ud = lua.globals().get::<AnyUserData>("ud")?;
|
||||
match ud.borrow_scoped::<MyUserData, _>(|_| Ok::<_, Error>(())) {
|
||||
Ok(_) => panic!("succesfull borrow for destructed userdata"),
|
||||
Err(Error::UserDataDestructed) => {}
|
||||
Err(err) => panic!("improper borrow error for destructed userdata: {:?}", err),
|
||||
Err(err) => panic!("improper borrow error for destructed userdata: {err:?}"),
|
||||
}
|
||||
match ud.get_metatable() {
|
||||
match ud.metatable() {
|
||||
Ok(_) => panic!("successful metatable retrieval of destructed userdata"),
|
||||
Err(Error::UserDataDestructed) => {}
|
||||
Err(err) => panic!(
|
||||
"improper metatable error for destructed userdata: {:?}",
|
||||
err
|
||||
),
|
||||
Err(err) => panic!("improper metatable error for destructed userdata: {err:?}"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -377,7 +316,7 @@ fn test_scope_userdata_ref() -> Result<()> {
|
||||
struct MyUserData(Cell<i64>);
|
||||
|
||||
impl UserData for MyUserData {
|
||||
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
|
||||
methods.add_method("inc", |_, data, ()| {
|
||||
data.0.set(data.0.get() + 1);
|
||||
Ok(())
|
||||
@@ -393,7 +332,15 @@ fn test_scope_userdata_ref() -> Result<()> {
|
||||
let data = MyUserData(Cell::new(1));
|
||||
lua.scope(|scope| {
|
||||
let ud = scope.create_userdata_ref(&data)?;
|
||||
modify_userdata(&lua, ud)
|
||||
modify_userdata(&lua, &ud)?;
|
||||
|
||||
// We can only borrow userdata scoped
|
||||
assert!((matches!(ud.borrow::<MyUserData>(), Err(Error::UserDataTypeMismatch))));
|
||||
ud.borrow_scoped::<MyUserData, ()>(|ud_inst| {
|
||||
assert_eq!(ud_inst.0.get(), 2);
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
assert_eq!(data.0.get(), 2);
|
||||
|
||||
@@ -407,7 +354,7 @@ fn test_scope_userdata_ref_mut() -> Result<()> {
|
||||
struct MyUserData(i64);
|
||||
|
||||
impl UserData for MyUserData {
|
||||
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
|
||||
methods.add_method_mut("inc", |_, data, ()| {
|
||||
data.0 += 1;
|
||||
Ok(())
|
||||
@@ -423,9 +370,16 @@ fn test_scope_userdata_ref_mut() -> Result<()> {
|
||||
let mut data = MyUserData(1);
|
||||
lua.scope(|scope| {
|
||||
let ud = scope.create_userdata_ref_mut(&mut data)?;
|
||||
modify_userdata(&lua, ud)
|
||||
modify_userdata(&lua, &ud)?;
|
||||
|
||||
assert!((matches!(ud.borrow_mut::<MyUserData>(), Err(Error::UserDataTypeMismatch))));
|
||||
ud.borrow_mut_scoped::<MyUserData, ()>(|ud_inst| {
|
||||
ud_inst.0 += 10;
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
assert_eq!(data.0, 2);
|
||||
assert_eq!(data.0, 12);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -438,8 +392,9 @@ fn test_scope_any_userdata() -> Result<()> {
|
||||
reg.add_meta_method("__tostring", |_, data, ()| Ok(data.clone()));
|
||||
})?;
|
||||
|
||||
let data = StdString::from("foo");
|
||||
lua.scope(|scope| {
|
||||
let ud = scope.create_any_userdata(StdString::from("foo"))?;
|
||||
let ud = scope.create_any_userdata_ref(&data)?;
|
||||
lua.globals().set("ud", ud)?;
|
||||
lua.load("assert(tostring(ud) == 'foo')").exec()
|
||||
})?;
|
||||
@@ -447,10 +402,10 @@ fn test_scope_any_userdata() -> Result<()> {
|
||||
// Check that userdata is destructed
|
||||
match lua.load("tostring(ud)").exec() {
|
||||
Err(Error::CallbackError { ref cause, .. }) => match cause.as_ref() {
|
||||
Error::CallbackDestructed => {}
|
||||
err => panic!("expected CallbackDestructed, got {:?}", err),
|
||||
Error::UserDataDestructed => {}
|
||||
err => panic!("expected CallbackDestructed, got {err:?}"),
|
||||
},
|
||||
r => panic!("improper return for destructed userdata: {:?}", r),
|
||||
r => panic!("improper return for destructed userdata: {r:?}"),
|
||||
};
|
||||
|
||||
Ok(())
|
||||
@@ -475,27 +430,47 @@ fn test_scope_any_userdata_ref() -> Result<()> {
|
||||
let data = Cell::new(1i64);
|
||||
lua.scope(|scope| {
|
||||
let ud = scope.create_any_userdata_ref(&data)?;
|
||||
modify_userdata(&lua, ud)
|
||||
modify_userdata(&lua, &ud)
|
||||
})?;
|
||||
assert_eq!(data.get(), 2);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn modify_userdata(lua: &Lua, ud: AnyUserData) -> Result<()> {
|
||||
let f: Function = lua
|
||||
.load(
|
||||
r#"
|
||||
function(u)
|
||||
u:inc()
|
||||
u:dec()
|
||||
u:inc()
|
||||
end
|
||||
"#,
|
||||
)
|
||||
.eval()?;
|
||||
#[test]
|
||||
fn test_scope_any_userdata_ref_mut() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
f.call(ud)?;
|
||||
lua.register_userdata_type::<i64>(|reg| {
|
||||
reg.add_method_mut("inc", |_, data, ()| {
|
||||
*data += 1;
|
||||
Ok(())
|
||||
});
|
||||
|
||||
reg.add_method_mut("dec", |_, data, ()| {
|
||||
*data -= 1;
|
||||
Ok(())
|
||||
});
|
||||
})?;
|
||||
|
||||
let mut data = 1i64;
|
||||
lua.scope(|scope| {
|
||||
let ud = scope.create_any_userdata_ref_mut(&mut data)?;
|
||||
modify_userdata(&lua, &ud)
|
||||
})?;
|
||||
assert_eq!(data, 2);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn modify_userdata(lua: &Lua, ud: &AnyUserData) -> Result<()> {
|
||||
lua.load(
|
||||
r#"
|
||||
local u = ...
|
||||
u:inc()
|
||||
u:dec()
|
||||
u:inc()
|
||||
"#,
|
||||
)
|
||||
.call(ud)
|
||||
}
|
||||
+28
-58
@@ -4,8 +4,8 @@ use std::collections::HashMap;
|
||||
use std::error::Error as StdError;
|
||||
|
||||
use mlua::{
|
||||
DeserializeOptions, Error, ExternalResult, Lua, LuaSerdeExt, Result as LuaResult, SerializeOptions,
|
||||
UserData, Value,
|
||||
AnyUserData, DeserializeOptions, Error, ExternalResult, IntoLua, Lua, LuaSerdeExt, Result as LuaResult,
|
||||
SerializeOptions, UserData, Value,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -71,63 +71,31 @@ fn test_serialize() -> Result<(), Box<dyn StdError>> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn test_serialize_in_scope() -> LuaResult<()> {
|
||||
// #[derive(Serialize, Clone)]
|
||||
// struct MyUserData(i64, String);
|
||||
|
||||
// impl UserData for MyUserData {}
|
||||
|
||||
// let lua = Lua::new();
|
||||
// lua.scope(|scope| {
|
||||
// let ud = scope.create_ser_userdata(MyUserData(-5, "test userdata".into()))?;
|
||||
// assert_eq!(
|
||||
// serde_json::to_value(&ud).unwrap(),
|
||||
// serde_json::json!((-5, "test userdata"))
|
||||
// );
|
||||
// Ok(())
|
||||
// })?;
|
||||
|
||||
// lua.scope(|scope| {
|
||||
// let ud = scope.create_ser_userdata(MyUserData(-5, "test userdata".into()))?;
|
||||
// lua.globals().set("ud", ud)
|
||||
// })?;
|
||||
// let val = lua.load("ud").eval::<Value>()?;
|
||||
// match serde_json::to_value(&val) {
|
||||
// Ok(v) => panic!("expected destructed error, got {}", v),
|
||||
// Err(e) if e.to_string().contains("destructed") => {}
|
||||
// Err(e) => panic!("expected destructed error, got {}", e),
|
||||
// }
|
||||
|
||||
// struct MyUserDataRef<'a>(#[allow(unused)] &'a ());
|
||||
|
||||
// impl<'a> UserData for MyUserDataRef<'a> {}
|
||||
|
||||
// lua.scope(|scope| {
|
||||
// let ud = scope.create_nonstatic_userdata(MyUserDataRef(&()))?;
|
||||
// match serde_json::to_value(&ud) {
|
||||
// Ok(v) => panic!("expected serialization error, got {}", v),
|
||||
// Err(serde_json::Error { .. }) => {}
|
||||
// };
|
||||
// Ok(())
|
||||
// })?;
|
||||
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn test_serialize_any_userdata() -> Result<(), Box<dyn StdError>> {
|
||||
fn test_serialize_any_userdata() {
|
||||
let lua = Lua::new();
|
||||
|
||||
let json_val = serde_json::json!({
|
||||
"a": 1,
|
||||
"b": "test",
|
||||
});
|
||||
let json_ud = lua.create_ser_any_userdata(json_val)?;
|
||||
let json_str = serde_json::to_string_pretty(&json_ud)?;
|
||||
let json_ud = lua.create_ser_any_userdata(json_val).unwrap();
|
||||
let json_str = serde_json::to_string_pretty(&json_ud).unwrap();
|
||||
assert_eq!(json_str, "{\n \"a\": 1,\n \"b\": \"test\"\n}");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
#[test]
|
||||
fn test_serialize_wrapped_any_userdata() {
|
||||
let lua = Lua::new();
|
||||
|
||||
let json_val = serde_json::json!({
|
||||
"a": 1,
|
||||
"b": "test",
|
||||
});
|
||||
let ud = AnyUserData::wrap_ser(json_val);
|
||||
let json_ud = ud.into_lua(&lua).unwrap();
|
||||
let json_str = serde_json::to_string(&json_ud).unwrap();
|
||||
assert_eq!(json_str, "{\"a\":1,\"b\":\"test\"}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -761,27 +729,29 @@ fn test_arbitrary_precision() {
|
||||
|
||||
#[cfg(feature = "luau")]
|
||||
#[test]
|
||||
fn test_buffer_serialize() {
|
||||
fn test_buffer_serialize() -> LuaResult<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let buf = lua.create_buffer(&[1, 2, 3, 4]).unwrap();
|
||||
let buf = lua.create_buffer(&[1, 2, 3, 4])?;
|
||||
let val = serde_value::to_value(&buf).unwrap();
|
||||
assert_eq!(val, serde_value::Value::Bytes(vec![1, 2, 3, 4]));
|
||||
|
||||
// Try empty buffer
|
||||
let buf = lua.create_buffer(&[]).unwrap();
|
||||
let buf = lua.create_buffer(&[])?;
|
||||
let val = serde_value::to_value(&buf).unwrap();
|
||||
assert_eq!(val, serde_value::Value::Bytes(vec![]));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "luau")]
|
||||
#[test]
|
||||
fn test_buffer_from_value() {
|
||||
fn test_buffer_from_value() -> LuaResult<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let buf = lua.create_buffer(&[1, 2, 3, 4]).unwrap();
|
||||
let val = lua
|
||||
.from_value::<serde_value::Value>(Value::UserData(buf))
|
||||
.unwrap();
|
||||
let buf = lua.create_buffer(&[1, 2, 3, 4])?;
|
||||
let val = lua.from_value::<serde_value::Value>(Value::Buffer(buf)).unwrap();
|
||||
assert_eq!(val, serde_value::Value::Bytes(vec![1, 2, 3, 4]));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+14
-3
@@ -17,6 +17,15 @@ fn test_string_compare() {
|
||||
with_str("teststring", |t| assert_eq!(t, t)); // mlua::String
|
||||
with_str("teststring", |t| assert_eq!(t, Cow::from(b"teststring".as_ref()))); // Cow (borrowed)
|
||||
with_str("bla", |t| assert_eq!(t, Cow::from(b"bla".to_vec()))); // Cow (owned)
|
||||
|
||||
// Test ordering
|
||||
with_str("a", |a| {
|
||||
assert!(!(a < a));
|
||||
assert!(!(a > a));
|
||||
});
|
||||
with_str("a", |a| assert!(a < "b"));
|
||||
with_str("a", |a| assert!(a < b"b"));
|
||||
with_str("a", |a| with_str("b", |b| assert!(a < b)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -52,7 +61,7 @@ fn test_string_views() -> Result<()> {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_raw_string() -> Result<()> {
|
||||
fn test_string_from_bytes() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let rs = lua.create_string(&[0, 1, 2, 3, 0, 1, 2, 3])?;
|
||||
@@ -77,16 +86,18 @@ fn test_string_hash() -> Result<()> {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_string_debug() -> Result<()> {
|
||||
fn test_string_fmt_debug() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
// Valid utf8
|
||||
let s = lua.create_string("hello")?;
|
||||
assert_eq!(format!("{s:?}"), r#""hello""#);
|
||||
assert_eq!(format!("{:?}", s.to_str()?), r#""hello""#);
|
||||
assert_eq!(format!("{:?}", s.as_bytes()), "[104, 101, 108, 108, 111]");
|
||||
|
||||
// Invalid utf8
|
||||
let s = lua.create_string(b"hello\0world\r\n\t\xF0\x90\x80")?;
|
||||
assert_eq!(format!("{s:?}"), r#"b"hello\0world\r\n\t\xf0\x90\x80""#);
|
||||
assert_eq!(format!("{s:?}"), r#"b"hello\0world\r\n\t\xF0\x90\x80""#);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+78
-60
@@ -1,4 +1,4 @@
|
||||
use mlua::{Error, Lua, Nil, ObjectLike, Result, Table, Value};
|
||||
use mlua::{Error, Lua, ObjectLike, Result, Table, Value};
|
||||
|
||||
#[test]
|
||||
fn test_globals_set_get() -> Result<()> {
|
||||
@@ -10,6 +10,8 @@ fn test_globals_set_get() -> Result<()> {
|
||||
assert_eq!(globals.get::<String>("foo")?, "bar");
|
||||
assert_eq!(globals.get::<String>("baz")?, "baf");
|
||||
|
||||
lua.load(r#"assert(foo == "bar")"#).exec().unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -19,16 +21,6 @@ fn test_table() -> Result<()> {
|
||||
|
||||
let globals = lua.globals();
|
||||
|
||||
globals.set("table", lua.create_table()?)?;
|
||||
let table1: Table = globals.get("table")?;
|
||||
let table2: Table = globals.get("table")?;
|
||||
|
||||
table1.set("foo", "bar")?;
|
||||
table2.set("baz", "baf")?;
|
||||
|
||||
assert_eq!(table2.get::<String>("foo")?, "bar");
|
||||
assert_eq!(table1.get::<String>("baz")?, "baf");
|
||||
|
||||
lua.load(
|
||||
r#"
|
||||
table1 = {1, 2, 3, 4, 5}
|
||||
@@ -39,29 +31,26 @@ fn test_table() -> Result<()> {
|
||||
.exec()?;
|
||||
|
||||
let table1 = globals.get::<Table>("table1")?;
|
||||
let table2 = globals.get::<Table>("table2")?;
|
||||
let table3 = globals.get::<Table>("table3")?;
|
||||
|
||||
assert_eq!(table1.len()?, 5);
|
||||
assert!(!table1.is_empty());
|
||||
assert_eq!(
|
||||
table1.clone().pairs().collect::<Result<Vec<(i64, i64)>>>()?,
|
||||
table1.pairs().collect::<Result<Vec<(i64, i64)>>>()?,
|
||||
vec![(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
|
||||
);
|
||||
assert_eq!(
|
||||
table1.clone().sequence_values().collect::<Result<Vec<i64>>>()?,
|
||||
table1.sequence_values().collect::<Result<Vec<i64>>>()?,
|
||||
vec![1, 2, 3, 4, 5]
|
||||
);
|
||||
assert_eq!(table1, [1, 2, 3, 4, 5]);
|
||||
assert_eq!(table1, [1, 2, 3, 4, 5].as_slice());
|
||||
|
||||
let table2 = globals.get::<Table>("table2")?;
|
||||
assert_eq!(table2.len()?, 0);
|
||||
assert!(table2.is_empty());
|
||||
assert_eq!(
|
||||
table2.clone().pairs().collect::<Result<Vec<(i64, i64)>>>()?,
|
||||
vec![]
|
||||
);
|
||||
assert_eq!(table2.pairs().collect::<Result<Vec<(i64, i64)>>>()?, vec![]);
|
||||
assert_eq!(table2, [0; 0]);
|
||||
|
||||
let table3 = globals.get::<Table>("table3")?;
|
||||
// sequence_values should only iterate until the first border
|
||||
assert_eq!(table3, [1, 2]);
|
||||
assert_eq!(
|
||||
@@ -69,26 +58,6 @@ fn test_table() -> Result<()> {
|
||||
vec![1, 2]
|
||||
);
|
||||
|
||||
globals.set("table4", lua.create_sequence_from(vec![1, 2, 3, 4, 5])?)?;
|
||||
let table4 = globals.get::<Table>("table4")?;
|
||||
assert_eq!(
|
||||
table4.clone().pairs().collect::<Result<Vec<(i64, i64)>>>()?,
|
||||
vec![(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
|
||||
);
|
||||
|
||||
table4.raw_insert(4, 35)?;
|
||||
table4.raw_insert(7, 7)?;
|
||||
assert_eq!(
|
||||
table4.clone().pairs().collect::<Result<Vec<(i64, i64)>>>()?,
|
||||
vec![(1, 1), (2, 2), (3, 3), (4, 35), (5, 4), (6, 5), (7, 7)]
|
||||
);
|
||||
|
||||
table4.raw_remove(1)?;
|
||||
assert_eq!(
|
||||
table4.clone().pairs().collect::<Result<Vec<(i64, i64)>>>()?,
|
||||
vec![(1, 2), (2, 3), (3, 35), (4, 4), (5, 5), (6, 7)]
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -97,7 +66,7 @@ fn test_table_push_pop() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
// Test raw access
|
||||
let table1 = lua.create_sequence_from(vec![123])?;
|
||||
let table1 = lua.create_sequence_from([123])?;
|
||||
table1.raw_push(321)?;
|
||||
assert_eq!(table1, [123, 321]);
|
||||
assert_eq!(table1.raw_pop::<i64>()?, 321);
|
||||
@@ -123,10 +92,7 @@ fn test_table_push_pop() -> Result<()> {
|
||||
table2.push(345)?;
|
||||
assert_eq!(table2.len()?, 2);
|
||||
assert_eq!(
|
||||
table2
|
||||
.clone()
|
||||
.sequence_values::<i64>()
|
||||
.collect::<Result<Vec<_>>>()?,
|
||||
table2.sequence_values::<i64>().collect::<Result<Vec<_>>>()?,
|
||||
vec![]
|
||||
);
|
||||
assert_eq!(table2.pop::<i64>()?, 345);
|
||||
@@ -137,22 +103,53 @@ fn test_table_push_pop() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_table_insert_remove() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let globals = lua.globals();
|
||||
|
||||
globals.set("table4", [1, 2, 3, 4, 5])?;
|
||||
let table4 = globals.get::<Table>("table4")?;
|
||||
assert_eq!(
|
||||
table4.pairs().collect::<Result<Vec<(i64, i64)>>>()?,
|
||||
vec![(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
|
||||
);
|
||||
table4.raw_insert(4, 35)?;
|
||||
table4.raw_insert(7, 7)?;
|
||||
assert_eq!(
|
||||
table4.pairs().collect::<Result<Vec<(i64, i64)>>>()?,
|
||||
vec![(1, 1), (2, 2), (3, 3), (4, 35), (5, 4), (6, 5), (7, 7)]
|
||||
);
|
||||
table4.raw_remove(1)?;
|
||||
assert_eq!(
|
||||
table4.pairs().collect::<Result<Vec<(i64, i64)>>>()?,
|
||||
vec![(1, 2), (2, 3), (3, 35), (4, 4), (5, 5), (6, 7)]
|
||||
);
|
||||
|
||||
// Wrong index, tables are 1-indexed
|
||||
assert!(table4.raw_insert(0, "123").is_err());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_table_clear() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let t = lua.create_table()?;
|
||||
|
||||
// Check readonly error
|
||||
#[cfg(feature = "luau")]
|
||||
{
|
||||
let t = lua.create_table()?;
|
||||
t.set_readonly(true);
|
||||
assert!(matches!(
|
||||
t.clear(),
|
||||
Err(Error::RuntimeError(err)) if err.contains("attempt to modify a readonly table")
|
||||
));
|
||||
t.set_readonly(false);
|
||||
}
|
||||
|
||||
let t = lua.create_table()?;
|
||||
// Set array and hash parts
|
||||
t.push("abc")?;
|
||||
t.push("bcd")?;
|
||||
@@ -181,7 +178,7 @@ fn test_table_clear() -> Result<()> {
|
||||
assert_eq!(t2.raw_len(), 0);
|
||||
assert!(t2.is_empty());
|
||||
assert_eq!(t2.raw_get::<Value>("a")?, Value::Nil);
|
||||
assert_ne!(t2.get_metatable(), None);
|
||||
assert_ne!(t2.metatable(), None);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -217,15 +214,14 @@ fn test_table_pairs() -> Result<()> {
|
||||
)
|
||||
.eval::<Table>()?;
|
||||
|
||||
let table2 = table.clone();
|
||||
for (i, kv) in table.pairs::<String, Value>().enumerate() {
|
||||
let (k, _v) = kv.unwrap();
|
||||
match i {
|
||||
// Try to add a new key
|
||||
0 => table2.set("new_key", "new_value")?,
|
||||
0 => table.set("new_key", "new_value")?,
|
||||
// Try to delete the 2nd key
|
||||
1 => {
|
||||
table2.set(k, Value::Nil)?;
|
||||
table.set(k, Value::Nil)?;
|
||||
lua.gc_collect()?;
|
||||
}
|
||||
_ => {}
|
||||
@@ -304,21 +300,15 @@ fn test_metatable() -> Result<()> {
|
||||
metatable.set("__index", lua.create_function(|_, ()| Ok("index_value"))?)?;
|
||||
table.set_metatable(Some(metatable));
|
||||
assert_eq!(table.get::<String>("any_key")?, "index_value");
|
||||
match table.raw_get::<Value>("any_key")? {
|
||||
Nil => {}
|
||||
_ => panic!(),
|
||||
}
|
||||
assert_eq!(table.raw_get::<Value>("any_key")?, Value::Nil);
|
||||
table.set_metatable(None);
|
||||
match table.get::<Value>("any_key")? {
|
||||
Nil => {}
|
||||
_ => panic!(),
|
||||
};
|
||||
assert_eq!(table.get::<Value>("any_key")?, Value::Nil);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_table_eq() -> Result<()> {
|
||||
fn test_table_equals() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
let globals = lua.globals();
|
||||
|
||||
@@ -358,6 +348,7 @@ fn test_table_pointer() -> Result<()> {
|
||||
let table1 = lua.create_table()?;
|
||||
let table2 = lua.create_table()?;
|
||||
|
||||
// Clone should not create a new table
|
||||
assert_eq!(table1.to_pointer(), table1.clone().to_pointer());
|
||||
assert_ne!(table1.to_pointer(), table2.to_pointer());
|
||||
|
||||
@@ -398,6 +389,33 @@ fn test_table_error() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_table_fmt() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let table = lua
|
||||
.load(
|
||||
r#"
|
||||
local t = {1, 2, 3, a = 5, b = { 6 }}
|
||||
t[9.2] = 9.2
|
||||
t[1.99] = 1.99
|
||||
t[true] = true
|
||||
t[false] = false
|
||||
return t
|
||||
"#,
|
||||
)
|
||||
.eval::<Table>()?;
|
||||
assert!(format!("{table:?}").starts_with("Table(Ref("));
|
||||
|
||||
// Pretty print
|
||||
assert_eq!(
|
||||
format!("{table:#?}"),
|
||||
"{\n [false] = false,\n [true] = true,\n [1] = 1,\n [1.99] = 1.99,\n [2] = 2,\n [3] = 3,\n [9.2] = 9.2,\n [\"a\"] = 5,\n [\"b\"] = {\n [1] = 6,\n },\n}"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_table_object_like() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
+111
-25
@@ -106,7 +106,6 @@ fn test_exec() -> Result<()> {
|
||||
"#,
|
||||
)
|
||||
.eval()?;
|
||||
println!("checkpoint");
|
||||
assert!(module.contains_key("func")?);
|
||||
assert_eq!(module.get::<Function>("func")?.call::<String>(())?, "hello");
|
||||
|
||||
@@ -631,8 +630,7 @@ fn test_recursive_mut_callback_error() -> Result<()> {
|
||||
// Whoops, this will recurse into the function and produce another mutable reference!
|
||||
lua.globals().get::<Function>("f")?.call::<()>(true)?;
|
||||
println!("Should not get here, mutable aliasing has occurred!");
|
||||
println!("value at {:p}", r as *mut _);
|
||||
println!("value is {}", r);
|
||||
println!("value at {:p} is {r}", r as *mut _);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -874,6 +872,49 @@ fn test_application_data() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rust_function() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let globals = lua.globals();
|
||||
lua.load(
|
||||
r#"
|
||||
function lua_function()
|
||||
return rust_function()
|
||||
end
|
||||
|
||||
-- Test to make sure chunk return is ignored
|
||||
return 1
|
||||
"#,
|
||||
)
|
||||
.exec()?;
|
||||
|
||||
let lua_function = globals.get::<Function>("lua_function")?;
|
||||
let rust_function = lua.create_function(|_, ()| Ok("hello"))?;
|
||||
|
||||
globals.set("rust_function", rust_function)?;
|
||||
assert_eq!(lua_function.call::<String>(())?, "hello");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_c_function() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
unsafe extern "C-unwind" fn c_function(state: *mut mlua::lua_State) -> std::os::raw::c_int {
|
||||
ffi::lua_pushboolean(state, 1);
|
||||
ffi::lua_setglobal(state, b"c_function\0" as *const _ as *const _);
|
||||
0
|
||||
}
|
||||
|
||||
let func = unsafe { lua.create_c_function(c_function)? };
|
||||
func.call::<()>(())?;
|
||||
assert_eq!(lua.globals().get::<bool>("c_function")?, true);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn test_recursion() -> Result<()> {
|
||||
@@ -925,26 +966,6 @@ fn test_too_many_recursions() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn test_too_many_binds() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
let globals = lua.globals();
|
||||
lua.load(
|
||||
r#"
|
||||
function f(...)
|
||||
end
|
||||
"#,
|
||||
)
|
||||
.exec()?;
|
||||
|
||||
let concat = globals.get::<Function>("f")?;
|
||||
assert!(concat.bind(Variadic::from_iter(1..1000000)).is_err());
|
||||
assert!(concat.call::<()>(Variadic::from_iter(1..1000000)).is_err());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn test_ref_stack_exhaustion() {
|
||||
@@ -1172,6 +1193,38 @@ fn test_inspect_stack() -> Result<()> {
|
||||
.set_name("chunk")
|
||||
.exec()?;
|
||||
|
||||
let stack_info = lua.create_function(|lua, ()| {
|
||||
let debug = lua.inspect_stack(1).unwrap(); // caller
|
||||
let stack_info = debug.stack();
|
||||
Ok(format!("{stack_info:?}"))
|
||||
})?;
|
||||
lua.globals().set("stack_info", stack_info)?;
|
||||
|
||||
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52", feature = "luau"))]
|
||||
lua.load(
|
||||
r#"
|
||||
local stack_info = stack_info
|
||||
local function baz(a, b, c, ...)
|
||||
return stack_info()
|
||||
end
|
||||
assert(baz() == 'DebugStack { num_ups: 1, num_params: 3, is_vararg: true }')
|
||||
"#,
|
||||
)
|
||||
.exec()?;
|
||||
|
||||
// LuaJIT does not pass this test for some reason
|
||||
#[cfg(feature = "lua51")]
|
||||
lua.load(
|
||||
r#"
|
||||
local stack_info = stack_info
|
||||
local function baz(a, b, c, ...)
|
||||
return stack_info()
|
||||
end
|
||||
assert(baz() == 'DebugStack { num_ups: 1 }')
|
||||
"#,
|
||||
)
|
||||
.exec()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1256,8 +1309,7 @@ fn test_luajit_cdata() -> Result<()> {
|
||||
"#,
|
||||
)
|
||||
.eval::<Value>()?;
|
||||
assert!(cdata.is_userdata() && cdata.is_cdata());
|
||||
assert_eq!(cdata.type_name(), "cdata");
|
||||
assert_eq!(cdata.type_name(), "other");
|
||||
assert!(cdata.to_string()?.starts_with("cdata<void *>:"));
|
||||
|
||||
Ok(())
|
||||
@@ -1289,3 +1341,37 @@ fn test_multi_thread() -> Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_exec_raw() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let sum = lua.create_function(|_, args: Variadic<i32>| {
|
||||
let mut sum = 0;
|
||||
for i in args {
|
||||
sum += i;
|
||||
}
|
||||
Ok(sum)
|
||||
})?;
|
||||
lua.globals().set("sum", sum)?;
|
||||
|
||||
let n: i32 = unsafe {
|
||||
lua.exec_raw((), |state| {
|
||||
ffi::lua_getglobal(state, b"sum\0".as_ptr() as _);
|
||||
ffi::lua_pushinteger(state, 1);
|
||||
ffi::lua_pushinteger(state, 7);
|
||||
ffi::lua_call(state, 2, 1);
|
||||
})
|
||||
}?;
|
||||
assert_eq!(n, 8);
|
||||
|
||||
// Test error handling
|
||||
let res: Result<()> = unsafe {
|
||||
lua.exec_raw("test error", |state| {
|
||||
ffi::lua_error(state);
|
||||
})
|
||||
};
|
||||
assert!(matches!(res, Err(Error::RuntimeError(err)) if err.contains("test error")));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+112
-1
@@ -1,6 +1,6 @@
|
||||
use std::os::raw::c_void;
|
||||
|
||||
use mlua::{Function, LightUserData, Lua, Result};
|
||||
use mlua::{Function, LightUserData, Lua, Number, Result, String as LuaString, Thread};
|
||||
|
||||
#[test]
|
||||
fn test_lightuserdata() -> Result<()> {
|
||||
@@ -24,3 +24,114 @@ fn test_lightuserdata() -> Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_boolean_type_metatable() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let mt = lua.create_table()?;
|
||||
mt.set("__add", Function::wrap(|a, b| Ok(a || b)))?;
|
||||
lua.set_type_metatable::<bool>(Some(mt));
|
||||
|
||||
lua.load(r#"assert(true + true == true)"#).exec().unwrap();
|
||||
lua.load(r#"assert(true + false == true)"#).exec().unwrap();
|
||||
lua.load(r#"assert(false + true == true)"#).exec().unwrap();
|
||||
lua.load(r#"assert(false + false == false)"#).exec().unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lightuserdata_type_metatable() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let mt = lua.create_table()?;
|
||||
mt.set(
|
||||
"__add",
|
||||
Function::wrap(|a: LightUserData, b: LightUserData| {
|
||||
Ok(LightUserData((a.0 as usize + b.0 as usize) as *mut c_void))
|
||||
}),
|
||||
)?;
|
||||
lua.set_type_metatable::<LightUserData>(Some(mt));
|
||||
|
||||
let res = lua
|
||||
.load(
|
||||
r#"
|
||||
local a, b = ...
|
||||
return a + b
|
||||
"#,
|
||||
)
|
||||
.call::<LightUserData>((
|
||||
LightUserData(42 as *mut c_void),
|
||||
LightUserData(100 as *mut c_void),
|
||||
))
|
||||
.unwrap();
|
||||
assert_eq!(res, LightUserData(142 as *mut c_void));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_number_type_metatable() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let mt = lua.create_table()?;
|
||||
mt.set("__call", Function::wrap(|n1: f64, n2: f64| Ok(n1 * n2)))?;
|
||||
lua.set_type_metatable::<Number>(Some(mt));
|
||||
lua.load(r#"assert((1.5)(3.0) == 4.5)"#).exec().unwrap();
|
||||
lua.load(r#"assert((5)(5) == 25)"#).exec().unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_string_type_metatable() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let mt = lua.create_table()?;
|
||||
mt.set(
|
||||
"__add",
|
||||
Function::wrap(|a: String, b: String| Ok(format!("{a}{b}"))),
|
||||
)?;
|
||||
lua.set_type_metatable::<LuaString>(Some(mt));
|
||||
|
||||
lua.load(r#"assert(("foo" + "bar") == "foobar")"#).exec().unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_function_type_metatable() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let mt = lua.create_table()?;
|
||||
mt.set(
|
||||
"__index",
|
||||
Function::wrap(|_: Function, key: String| Ok(format!("function.{key}"))),
|
||||
)?;
|
||||
lua.set_type_metatable::<Function>(Some(mt));
|
||||
|
||||
lua.load(r#"assert((function() end).foo == "function.foo")"#)
|
||||
.exec()
|
||||
.unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_thread_type_metatable() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let mt = lua.create_table()?;
|
||||
mt.set(
|
||||
"__index",
|
||||
Function::wrap(|_: Thread, key: String| Ok(format!("thread.{key}"))),
|
||||
)?;
|
||||
lua.set_type_metatable::<Thread>(Some(mt));
|
||||
|
||||
lua.load(r#"assert((coroutine.create(function() end)).foo == "thread.foo")"#)
|
||||
.exec()
|
||||
.unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+32
-23
@@ -195,19 +195,19 @@ fn test_metamethods() -> Result<()> {
|
||||
|
||||
assert!(lua.load("userdata2 == userdata3").eval::<bool>()?);
|
||||
assert!(userdata2 != userdata3); // because references are differ
|
||||
assert!(userdata2.equals(userdata3)?);
|
||||
assert!(userdata2.equals(&userdata3)?);
|
||||
|
||||
let userdata1: AnyUserData = globals.get("userdata1")?;
|
||||
assert!(userdata1.get_metatable()?.contains(MetaMethod::Add)?);
|
||||
assert!(userdata1.get_metatable()?.contains(MetaMethod::Sub)?);
|
||||
assert!(userdata1.get_metatable()?.contains(MetaMethod::Index)?);
|
||||
assert!(!userdata1.get_metatable()?.contains(MetaMethod::Pow)?);
|
||||
assert!(userdata1.metatable()?.contains(MetaMethod::Add)?);
|
||||
assert!(userdata1.metatable()?.contains(MetaMethod::Sub)?);
|
||||
assert!(userdata1.metatable()?.contains(MetaMethod::Index)?);
|
||||
assert!(!userdata1.metatable()?.contains(MetaMethod::Pow)?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "lua54")]
|
||||
#[test]
|
||||
fn test_metamethod_close() -> Result<()> {
|
||||
#[derive(Clone)]
|
||||
struct MyUserData(Arc<AtomicI64>);
|
||||
@@ -336,8 +336,8 @@ fn test_userdata_take() -> Result<()> {
|
||||
}
|
||||
match lua.load("userdata:num()").exec() {
|
||||
Err(Error::CallbackError { ref cause, .. }) => match cause.as_ref() {
|
||||
Error::CallbackDestructed => {}
|
||||
err => panic!("expected `CallbackDestructed`, got {:?}", err),
|
||||
Error::UserDataDestructed => {}
|
||||
err => panic!("expected `UserDataDestructed`, got {:?}", err),
|
||||
},
|
||||
r => panic!("improper return for destructed userdata: {:?}", r),
|
||||
}
|
||||
@@ -418,6 +418,7 @@ fn test_user_values() -> Result<()> {
|
||||
assert!(ud.nth_user_value::<Value>(65536).is_err());
|
||||
|
||||
// Named user values
|
||||
let ud = lua.create_userdata(MyUserData)?;
|
||||
ud.set_named_user_value("name", "alex")?;
|
||||
ud.set_named_user_value("age", 10)?;
|
||||
|
||||
@@ -565,7 +566,7 @@ fn test_metatable() -> Result<()> {
|
||||
impl UserData for MyUserData {
|
||||
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
|
||||
methods.add_function("my_type_name", |_, data: AnyUserData| {
|
||||
let metatable = data.get_metatable()?;
|
||||
let metatable = data.metatable()?;
|
||||
metatable.get::<String>(MetaMethod::Type)
|
||||
});
|
||||
}
|
||||
@@ -583,7 +584,7 @@ fn test_metatable() -> Result<()> {
|
||||
lua.load(r#"assert(typeof(ud) == "MyUserData")"#).exec()?;
|
||||
|
||||
let ud: AnyUserData = globals.get("ud")?;
|
||||
let metatable = ud.get_metatable()?;
|
||||
let metatable = ud.metatable()?;
|
||||
|
||||
match metatable.get::<Value>("__gc") {
|
||||
Ok(_) => panic!("expected MetaMethodRestricted, got no error"),
|
||||
@@ -629,7 +630,7 @@ fn test_metatable() -> Result<()> {
|
||||
}
|
||||
|
||||
let ud = lua.create_userdata(MyUserData3)?;
|
||||
let metatable = ud.get_metatable()?;
|
||||
let metatable = ud.metatable()?;
|
||||
assert_eq!(metatable.get::<String>(MetaMethod::Type)?.to_str()?, "CustomName");
|
||||
|
||||
Ok(())
|
||||
@@ -791,18 +792,26 @@ fn test_userdata_method_errors() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let ud = lua.create_userdata(MyUserData(123))?;
|
||||
let res = ud.call_function::<()>("get_value", ());
|
||||
let Err(Error::CallbackError { cause, .. }) = res else {
|
||||
panic!("expected CallbackError, got {res:?}");
|
||||
};
|
||||
assert!(matches!(
|
||||
&*cause,
|
||||
Error::BadArgument {
|
||||
to,
|
||||
name,
|
||||
..
|
||||
} if to.as_deref() == Some("MyUserData.get_value") && name.as_deref() == Some("self")
|
||||
));
|
||||
let res = ud.call_function::<()>("get_value", "not a userdata");
|
||||
match res {
|
||||
Err(Error::CallbackError { cause, .. }) => match cause.as_ref() {
|
||||
Error::BadArgument {
|
||||
to,
|
||||
name,
|
||||
cause: cause2,
|
||||
..
|
||||
} => {
|
||||
assert_eq!(to.as_deref(), Some("MyUserData.get_value"));
|
||||
assert_eq!(name.as_deref(), Some("self"));
|
||||
assert_eq!(
|
||||
cause2.to_string(),
|
||||
"error converting Lua string to userdata (expected userdata of type 'MyUserData')"
|
||||
);
|
||||
}
|
||||
err => panic!("expected BadArgument, got {err:?}"),
|
||||
},
|
||||
r => panic!("expected CallbackError, got {r:?}"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+76
-2
@@ -52,7 +52,7 @@ fn test_value_eq() -> Result<()> {
|
||||
assert!(string1 == string2);
|
||||
assert!(string1.equals(&string2)?);
|
||||
assert!(num1 == num2);
|
||||
assert!(num1.equals(num2)?);
|
||||
assert!(num1.equals(&num2)?);
|
||||
assert!(num1 != num3);
|
||||
assert!(func1 == func2);
|
||||
assert!(func1 != func3);
|
||||
@@ -88,30 +88,79 @@ fn test_multi_value() {
|
||||
assert!(multi_value.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_value_to_pointer() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
let globals = lua.globals();
|
||||
lua.load(
|
||||
r#"
|
||||
table = {}
|
||||
string = "hello"
|
||||
num = 1
|
||||
func = function() end
|
||||
thread = coroutine.create(function() end)
|
||||
"#,
|
||||
)
|
||||
.exec()?;
|
||||
globals.set("null", Value::NULL)?;
|
||||
|
||||
let table: Value = globals.get("table")?;
|
||||
let string: Value = globals.get("string")?;
|
||||
let num: Value = globals.get("num")?;
|
||||
let func: Value = globals.get("func")?;
|
||||
let thread: Value = globals.get("thread")?;
|
||||
let null: Value = globals.get("null")?;
|
||||
let ud: Value = Value::UserData(lua.create_any_userdata(())?);
|
||||
|
||||
assert!(!table.to_pointer().is_null());
|
||||
assert!(!string.to_pointer().is_null());
|
||||
assert!(num.to_pointer().is_null());
|
||||
assert!(!func.to_pointer().is_null());
|
||||
assert!(!thread.to_pointer().is_null());
|
||||
assert!(null.to_pointer().is_null());
|
||||
assert!(!ud.to_pointer().is_null());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_value_to_string() -> Result<()> {
|
||||
let lua = Lua::new();
|
||||
|
||||
assert_eq!(Value::Nil.to_string()?, "nil");
|
||||
assert_eq!(Value::Nil.type_name(), "nil");
|
||||
assert_eq!(Value::Boolean(true).to_string()?, "true");
|
||||
assert_eq!(Value::Boolean(true).type_name(), "boolean");
|
||||
assert_eq!(Value::NULL.to_string()?, "null");
|
||||
assert_eq!(Value::NULL.type_name(), "lightuserdata");
|
||||
assert_eq!(
|
||||
Value::LightUserData(LightUserData(0x1 as *const c_void as *mut _)).to_string()?,
|
||||
"lightuserdata: 0x1"
|
||||
);
|
||||
assert_eq!(Value::Integer(1).to_string()?, "1");
|
||||
assert_eq!(Value::Integer(1).type_name(), "integer");
|
||||
assert_eq!(Value::Number(34.59).to_string()?, "34.59");
|
||||
assert_eq!(Value::Number(34.59).type_name(), "number");
|
||||
#[cfg(all(feature = "luau", not(feature = "luau-vector4")))]
|
||||
assert_eq!(
|
||||
Value::Vector(mlua::Vector::new(10.0, 11.1, 12.2)).to_string()?,
|
||||
"vector(10, 11.1, 12.2)"
|
||||
);
|
||||
#[cfg(all(feature = "luau", not(feature = "luau-vector4")))]
|
||||
assert_eq!(
|
||||
Value::Vector(mlua::Vector::new(10.0, 11.1, 12.2)).type_name(),
|
||||
"vector"
|
||||
);
|
||||
#[cfg(feature = "luau-vector4")]
|
||||
assert_eq!(
|
||||
Value::Vector(mlua::Vector::new(10.0, 11.1, 12.2, 13.3)).to_string()?,
|
||||
"vector(10, 11.1, 12.2, 13.3)"
|
||||
);
|
||||
assert_eq!(Value::String(lua.create_string("hello")?).to_string()?, "hello");
|
||||
|
||||
let s = Value::String(lua.create_string("hello")?);
|
||||
assert_eq!(s.to_string()?, "hello");
|
||||
assert_eq!(s.type_name(), "string");
|
||||
|
||||
let table: Value = lua.load("{}").eval()?;
|
||||
assert!(table.to_string()?.starts_with("table:"));
|
||||
@@ -119,18 +168,22 @@ fn test_value_to_string() -> Result<()> {
|
||||
.load("setmetatable({}, {__tostring = function() return 'test table' end})")
|
||||
.eval()?;
|
||||
assert_eq!(table.to_string()?, "test table");
|
||||
assert_eq!(table.type_name(), "table");
|
||||
|
||||
let func: Value = lua.load("function() end").eval()?;
|
||||
assert!(func.to_string()?.starts_with("function:"));
|
||||
assert_eq!(func.type_name(), "function");
|
||||
|
||||
let thread: Value = lua.load("coroutine.create(function() end)").eval()?;
|
||||
assert!(thread.to_string()?.starts_with("thread:"));
|
||||
assert_eq!(thread.type_name(), "thread");
|
||||
|
||||
lua.register_userdata_type::<StdString>(|reg| {
|
||||
reg.add_meta_method("__tostring", |_, this, ()| Ok(this.clone()));
|
||||
})?;
|
||||
let ud: Value = Value::UserData(lua.create_any_userdata(String::from("string userdata"))?);
|
||||
assert_eq!(ud.to_string()?, "string userdata");
|
||||
assert_eq!(ud.type_name(), "userdata");
|
||||
|
||||
struct MyUserData;
|
||||
impl UserData for MyUserData {}
|
||||
@@ -139,6 +192,19 @@ fn test_value_to_string() -> Result<()> {
|
||||
|
||||
let err = Value::Error(Box::new(Error::runtime("test error")));
|
||||
assert_eq!(err.to_string()?, "runtime error: test error");
|
||||
assert_eq!(err.type_name(), "error");
|
||||
|
||||
#[cfg(feature = "luau")]
|
||||
{
|
||||
let buf = Value::Buffer(lua.create_buffer(b"hello")?);
|
||||
assert!(buf.to_string()?.starts_with("buffer:"));
|
||||
assert_eq!(buf.type_name(), "buffer");
|
||||
|
||||
// Set `__tostring` metamethod for buffer
|
||||
let mt = lua.load("{__tostring = buffer.tostring}").eval()?;
|
||||
lua.set_type_metatable::<mlua::Buffer>(mt);
|
||||
assert_eq!(buf.to_string()?, "hello");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -220,5 +286,13 @@ fn test_value_conversions() -> Result<()> {
|
||||
Some(&"hello")
|
||||
);
|
||||
|
||||
assert!(Value::Error(Box::new(Error::runtime("some error"))).is_error());
|
||||
assert_eq!(
|
||||
(Value::Error(Box::new(Error::runtime("some error"))).as_error())
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
"runtime error: some error"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user