Commit Graph

66 Commits

Author SHA1 Message Date
Alex Orlenko 9fdba541e9
Update `UserDataMethods::add_async_method()` functions to take `&T` as second argument instead of cloning `T`.
New functions: `UserDataMethods::add_async_method_mut()`, `UserDataMethods::add_async_meta_method_mut()`.
2023-06-15 00:34:41 +01:00
Alex Orlenko 5a135a331a
Add `UserDataFields::add_field()` method to add static fields to UserData.
Plus `UserDataFields::add_meta_field()` for static meta fields.
Fix propagating fields to wrapped UserData types.
2023-06-07 12:18:24 +01:00
Alex Orlenko e0224ab159
Use futures-core and futures-tasks via futures-util.
Just to reduce number of dependencies.
2023-05-29 00:30:29 +01:00
Alex Orlenko aaf0a5e44a
Remove usage of crate::ffi (clippy) 2023-04-14 00:36:31 +01:00
Alex Orlenko b66bff9155
Drop `Lua::async_scope` as it's unsound 2023-02-12 22:25:10 +00:00
Alex Orlenko f52abf919e
Add create_userdata_ref/create_userdata_ref_mut for scope (#206)
New methods would allow creating userdata objects from (mutable) reference
to a UserData of registered type.
2023-02-12 16:37:02 +00:00
Alex Orlenko 47c8300ccf
Allow registering and creating custom userdata types that don't necessary implement the `UserData` trait.
This is useful to register 3rd party types that cannot implement `UserData` due to Rust orphan rules.
See #206
2023-02-03 23:46:04 +00:00
Alex Orlenko d67332a3ac
Refactor `userdata::MetaMethod`.
Take `impl AsRef<str>` as name of metamethod (instead of `Into<MetaMethod>`) in UserDataMethods/UserDataFields.
Change position of generic args in userdata.
2022-12-24 19:16:58 +00:00
Alex Orlenko a13c188de3
Rename ToLua/ToLuaMulti -> IntoLua/IntoLuaMulti 2022-12-19 23:00:47 +00:00
Alex Orlenko 9b4e3a1598
Refactor LuaInner state
Add static_assertions to check for auto traits impl on compilation stage
Bump MSRV to 1.63 (required for `Ref::filter_map`)
2022-12-19 16:09:05 +00:00
Alex Orlenko c44463f030
Replace `impl AsRef<str>` with simple `&str` 2022-12-18 00:35:41 +00:00
Alex Orlenko de38e299fc
Use `impl AsRef<str>` for userdata methods/fields instead of generic param.
Use `impl AsRef<str>` for module names and named registry values.
2022-12-18 00:35:41 +00:00
Alex Orlenko 9af1aaf889
Make clippy happy 2022-06-28 21:28:48 +01:00
Alex Orlenko 93d36b9068
More performance optimization (userdata part) 2022-06-06 21:42:55 +01:00
Alex Orlenko 55b778c68b
Fix clippy warnings 2022-03-22 00:47:31 +00:00
Alex Orlenko 3e5f8e7bb8
Enable `Lua::inspect_stack` for Luau 2022-03-21 01:08:47 +00:00
Alex Orlenko c322e028e2
Initial Luau support 2022-03-20 20:30:20 +00:00
Alex Orlenko 153502ec73
Add `set_nth_user_value` and `get_nth_user_value` to `AnyUserData`
with `n` up to 65535 for all Lua versions.
2021-11-08 21:16:31 +00:00
Alex Orlenko d7d987fa14
Add async meta methods for all Lua except 51 2021-11-04 00:57:49 +00:00
Alex Orlenko ed48b11e7f
Update documentation references
Using rustdoc links (see RFC https://github.com/rust-lang/rfcs/pull/1946)
2021-10-12 00:49:45 +01:00
Alex Orlenko a544e41b33
Add (hidden) method `UserData::take()` to take out value from userdata 2021-10-05 15:46:50 +01:00
Alex Orlenko a74b637ed4
Update check_stack requirements 2021-09-28 18:41:25 +01:00
Alex Orlenko d586eef0f5
Refactor UserData metatables handling 2021-09-28 16:33:36 +01:00
Alex Orlenko e42d67c70d
Make `protect_lua` as a smart macro to choose from C/closure 2021-09-28 16:26:30 +01:00
Alex Orlenko 1731f5d61b
Revert "Make `protect_lua` as a smart macro to choose from C/closure"
This reverts commit 84fe5f7f76.
2021-07-08 18:41:10 +01:00
Alex Orlenko 84fe5f7f76
Make `protect_lua` as a smart macro to choose from C/closure 2021-07-07 12:54:19 +01:00
Alex Orlenko 41aae83304
Optimize callback creation
Attach only one upvalue to callbacks rather than two.
This leads to less lookup to Lua registry.
2021-06-30 16:50:50 +01:00
Alex Orlenko 8d474bbf8d
Fix clippy warning 2021-06-21 03:25:21 +01:00
Alex Orlenko 4e92ea341b Fix userdata memleak in edge case.
This can happen if we unable to push metatable with `__gc` metamethod after pushing userdata.
In this case Lua will never execute drop.
Instead, we will push metatable first and then userdata.
2021-06-18 17:45:20 +01:00
Alex Orlenko d3f44354e0 Revert commit ced808d5ab
I think this experiment is unsuccessful and does not work well in a module mode
with dynamic symbols resolution and mixing between different mlua instances.
Overall the Rust bug has been fixed and we can wait for the "C-unwind" feature become stable.
2021-06-16 22:13:01 +01:00
Alex Orlenko abb95c3c56 Remove `T: Clone` requirement from `add_async_function` 2021-06-03 23:21:00 +01:00
Alex Orlenko a944f4ad6f Implement `UserData` for Rc<RefCell>/Arc<Mutex>/Arc<RwLock> wrappers 2021-06-03 16:42:55 +01:00
Alex Orlenko 3f55958bdd Stack assertions review
Other minor code and documentation updates
2021-05-02 11:42:03 +01:00
Alex Orlenko 463fc646bc Refactor UserDataCell 2021-04-27 21:55:31 +01:00
Alex Orlenko b5f1325f2f Store nonstatic UserData pointer in self userdata (instead of metatable) 2021-04-27 17:06:37 +01:00
Alex Orlenko 269ef9c55d Fix lua51 scope compilation 2021-04-27 10:11:37 +01:00
Alex Orlenko c10169a380 cargo fmt and other minor fixes 2021-04-27 00:29:38 +01:00
Alex Orlenko ced808d5ab Don't trigger longjmp in rust.
Motivation behind this change is upcoming breaking change in Rust
compiler v1.52.0 to prevent unwinding across FFI boundaries.
https://github.com/rust-lang/rust/pull/76570
The new functionality requires nightly compiler to declare FFI
functions as "C-unwind".
The fundamental solution is to use C shim to wrap "e" and "m"
Lua functions in pcall.
Additionally define Rust calling convention to trigger lua_error
on Rust behalf.
2021-04-27 00:29:38 +01:00
Alex Orlenko 5a7ad9f7cd Fix some clippy warnings & minor changes 2021-04-27 00:29:37 +01:00
Alex Orlenko 1635903d3f Improve/fix scoped UserData drop 2021-04-27 00:29:37 +01:00
Alex Orlenko 2b2df708f9 Add `UserDataFields` API.
Provide safe access to UserData metatable and allow to define custom metamethods..
2021-04-27 00:29:37 +01:00
Alex Orlenko 0c7db4916c Serialize only known (registered) userdata.
This reverts commit 7332c6a.
Non-static userdata is a special case and can cause segfault if try to serialize it.
Now it should be safe, plus I added non-static userdata destructor to generate better error messages
in case of accessing destructed userdata.
2021-04-16 22:01:55 +01:00
Alex Orlenko 2aed548747 Fix scoped async destruction of partially polled futures 2021-02-21 23:52:07 +00:00
Alex Orlenko 9f82cbe0c5 Update documentation 2020-12-31 13:39:38 +00:00
Alex Orlenko 6e2b687cb7 Serde support (serialize feature flag) 2020-12-14 00:51:26 +00:00
Alex Orlenko 687ecc9247 Update documentation 2020-05-15 01:48:57 +01:00
Alex Orlenko 9f2d598517 Add clippy check and fix clippy warnings 2020-05-15 01:48:56 +01:00
Alex Orlenko 539b569ff4 Add Lua 5.4 (rc2) support 2020-05-11 02:43:34 +01:00
Alex Orlenko d366ce0dd4 Scope support (including async) 2020-05-11 02:43:34 +01:00
Alex Orlenko 47e8a80c1c v0.3.0-alpha.1 with async support
Squashed commit of the async branch.
2020-04-17 22:39:50 +01:00
Alex Orlenko fd17a01456 Add Lua 5.2 support 2019-11-30 00:58:41 +00:00
Alex Orlenko ae677b0918 Move lua 5.1 support under new "lua51" feature 2019-11-04 22:23:15 +00:00
Alex Orlenko c4fd7a9faf Lua 5.1 support 2019-10-17 17:05:42 +01:00
Alex Orlenko b23ee6a162 cargo fmt 2019-09-29 12:56:03 +01:00
Alex Orlenko affa85feb0 Backport changes from rlua 0.16 (master branch) 2019-09-29 12:53:13 +01:00
kyren 7eb71fb1df Rename Scope::create_userdata to Scope::create_nonstatic_userdata
avoids clashing with the previous method name to avoid confusion
2018-09-16 19:54:12 -04:00
kyren 703601e348 code re-org have slightly less pub(crate) items 2018-09-04 19:05:21 -04:00
kyren 30a94c4dec Comment updates that I really hope are correct
Tried to explain the rationale for safety around callbacks in Lua and Scope a
bit better, because every time I don't look at this for a while I forget my
reasoning.  I'm not always so great at using the right terminology, so to
whoever reads this, if I got this wrong please tell me.
2018-09-04 17:36:06 -04:00
kyren bd00af2bac Initial design for non-'static scoped userdata
Uses the same UserData trait, and should at least in theory support everything
that 'static UserData does, except that any functions added that rely on
AnyUserData are pretty much useless.

Probably pretty slow and I'm not sure how to make it dramatically faster, which
is a shame because generally when you need non'-static userdata you might be
creating it kind of a lot (if it was long-lived, it would probably be 'static).

Haven't added tests yet, will do that next.
2018-09-04 03:40:13 -04:00
kyren 1a9c50f228 Solve (maybe) *another* soundness issue with `Lua::scope`
Callbacks should not be able to capture their arguments and hold onto them,
because the `&Lua` used in previous calls will not remain valid across calls.
One could imagine an API where the specific `&Lua` is simply stored inside the
`Scope` itself, but this is harder to do, and would (badly) encourage storing
references inside Lua userdata.

Ideally, the only way it should be possible to store Lua handles inside Lua
itself is through usafety or the `rental` crate or other self-borrowing
techniques to make references into 'static types.  If at all possible this
roadblock should stay, because reference types inside userdata are almost always
going to lead to a a memory leak, and if you accept the risks you should just
use `RegistryKey` with its manual removal.
2018-08-05 20:03:47 -04:00
kyren b35ff5fa12 Remove out of date documentation, simpler scope lifetimes
The documentation describing it being a logic bug to access "outer" callback
handles when inside an "inner" callback is inaccurate, that was only true when
using an older design for handle values.

Also, there is no reason to have a separate 'callback lifetime, because 'scope
is already invariant and just using 'scope seems equivalent.
2018-08-05 19:02:19 -04:00
kyren a2615a8cbb Fix for a soundness bug around scope, don't allow callback parameters to escape
Also includes other fixes for compiletest_rs failures, and a small reorg of tests
2018-08-05 11:54:33 -04:00
kyren 2e1bdb64c0 format with up-to-date rustfmt 2018-08-05 09:51:39 -04:00
kyren 8b9ab3d031 Small renames and comments to better communicate the intention of stack checking functions 2018-03-19 17:42:10 -04:00
kyren 985636267c Fix some bad potential unsafety on inner callback calls.
Since we now optionally use stack spaces for handle values, we have to be
mindful of whether our stack handle points to the stack in an outer level of
Lua "stack protection".  We now keep track of the "recursion level" of Lua
instances, and do not allow ref manipulation on "outer" Lua instances until the
inner callback has returned.  Also, update the documentation to reflect the
additional panic behavior.
2018-03-12 22:36:52 -04:00
kyren f79d771f1a Documentation improvements, split scope into its own module, improved tests
Also makes `Lua` and associated types !UnwindSafe and !RefUnwindSafe, which they
should be because they are intensely internally mutable.  Lua IS still panic
safe, but that doesn't mean it should be marked as UnwindSafe (as I understand
it).
2018-03-12 16:00:11 -04:00