Commit Graph

1134 Commits

Author SHA1 Message Date
kyren 7231e95195 It's far too easy to write 'stack_guard' as opposed to 'stack_err_guard'!
And it will work until something fails!  Maybe there should be a test that calls
every possible function that invokes to_lua / from_lua with a type where both
directions fail?
2018-02-11 16:47:39 -05:00
kyren ce7e8e61fd shave this yak some more, make `Callback` type alias have two lifetimes 2018-02-11 08:51:17 -05:00
kyren da1e1625b3 Specify the types exactly in the scary transmute
If I happen to change the definition of the Callback type alias, instead of
creating a potentially arbitrary transmute, it will now instead fail to compile.
2018-02-11 08:30:03 -05:00
kyren a91e3ed411 clarify the scary transmute 2018-02-11 08:22:15 -05:00
kyren fd0e31c6ea Add some extra warnings about the reference cycle dangers of `RegistryKey` 2018-02-11 06:37:51 -05:00
kyren b123eb087d Clarify some things in the README 2018-02-11 06:23:52 -05:00
kyren ca631e470a changelog spelling fix 2018-02-11 06:14:52 -05:00
kyren 9483b5054e Change changelog to be less confusing
There will not be a changelog entry for the changelog entry
2018-02-11 06:13:34 -05:00
kyren f94037856a Small README clarification 2018-02-10 19:13:56 -05:00
kyren 61236e685f Change changelog, readme, examples, Cargo.toml for 0.12 release 2018-02-10 19:04:18 -05:00
kyren 1b92d3319c consistent capitalization 2018-02-10 18:00:53 -05:00
kyren 0b7f07aa5d clarify that the Lua *C* API is the one that longjmps 2018-02-10 17:59:20 -05:00
kyren c3242add40 Update README for readability 2018-02-10 17:55:20 -05:00
kyren 20480ec88d fix unused process import warnings 2018-02-10 17:55:08 -05:00
kyren c5a5c51e9f A lot of README updates for the next version 2018-02-10 17:50:04 -05:00
kyren 9e3374ff9e lua_abort / lua_internal_abort macros 2018-02-10 17:49:54 -05:00
kyren 1426bdbc16 fix duplicated matrix entry for .travis.yml 2018-02-10 00:35:12 -05:00
kyren d4c80d44c8 Attempt to enable compiletest_rs on nightly on travis 2018-02-10 00:27:15 -05:00
kyren f785a3abe4 Don't bother with a feature, just disable LUA_USE_APICHECK in release 2018-02-09 23:59:11 -05:00
kyren 60743d45cd Move all tests into a tests/ subfolder 2018-02-09 23:52:05 -05:00
kyren d331e4b97c Error correctly on too many arguments / returns / binds / recursions
There are also some other drive-by changes to fix panicking in extern "C"
functions and other edge case stack errors
2018-02-09 23:40:23 -05:00
kyren fe6e4bdf35 Explicit error type for destructed callbacks
Also removes some cleverness if debug_assertions was disabled, as it really
doesn't make much of a performance difference.
2018-02-09 21:23:59 -05:00
kyren 514abd5b82 Actually unref scope created types from the registry AS WELL AS destructing them 2018-02-09 07:27:55 -05:00
kyren 84b009da03 A few small performance improvements
When 'debug_assertions' is not enabled, don't bother doing asserts in
stack_guard / stack_err_guard.  Also, add an optional feature not enabled by
default to disable LUA_USE_APICHECK in release mode.  Once the bugs in rlua that
allow you to trigger LUA_USE_APICHECK are fixed, this feature will be the
default behavior.
2018-02-09 01:22:34 -05:00
kyren de4d21f8ea Fix some strange lifetime usage on `Lua::create_function`
I don't think that the lifetime of the &Lua in the callback and the lifetime of
the &Lua from creating the callback need to be related at all.  I'm not sure if
this has any actual effect, but it makes more sense (I think?).
2018-02-08 18:52:29 -05:00
kyren b6bc8d0bed Make the `Scope` lifetimes more sensible
Avoids messy lifetime issues when interacting with other handle types with scope
produced values.

The whole lifetime situation with 'lua on most methods could actually probably
use some looking at, I'm sure it probably has lots of less than optimal
decisions in it.

This also adds a proper comment to the 'scope lifetime to explain that the key
is that 'scope needs to be invariant to make things safe.  Disregard my previous
commit message, the real problem is that I had a poor understanding of lifetime
variance / invaraince.
2018-02-08 18:45:07 -05:00
kyren 7701aeef85 TERRIBLE HACK FIX I DO NOT UNDERSTAND
Okay, so this is the fix for the previously mentioned lifetime problem.  I
mimicked the API for `crossbeam::scope` extremely closely for `Lua::scope`, and
for some reason things that would not compile with `crossbeam::scope` WOULD
compile with `Lua::scope`, and I could not figure it out.

So I took the crossbeam source and made tiny edits until I determined the
crossover point where invalid borrows would compile, and it was.. not what I
expected it to be.  Simply replacing a RefCell<Option<DtorChain<'a>>> with a
PhantomData<&'a ()> would suddenly cause this to compile with crossbeam:

```
struct Test {
    field: i32,
}
crossbeam::scope(|scope| {
    let mut t = Test {
        field: 0,
    };

    scope.spawn(|| t.field = 42);

    drop(t);

    // ...anything
})

```

which is precisely the same problem as `rlua`.

To say I am unsatisfied by this fix is a drastic understatement.  SURELY this
must be a compiler bug?
2018-02-08 05:12:27 -05:00
kyren f05716deb8 This SHOULD fix the lifetime problem with scope... but it doesn't!
The following code should not compile:

```
struct Test {
    field: i32,
}

let lua = Lua::new();
lua.scope(|scope| {
    let mut test = Test { field: 0 };

    let f = scope
        .create_function(|_, ()| {
            test.field = 42;
            Ok(())
        })
        .unwrap();
    lua.globals().set("bad!", f).unwrap();
});
```

yet it does with this commit.  However, I have a fix for this, which I do not in
any way understand.
2018-02-08 05:12:11 -05:00
kyren 7a0c066593 export accidentally hidden `Scope` type 2018-02-08 01:54:30 -05:00
kyren 728e8ea714
Merge pull request #68 from chucklefish/scope
Lots of changes, not sure if actually safe yet.
2018-02-07 17:07:13 -05:00
kyren 164250b352 Don't panic with "rlua internal error" message on panics that are not internal
It is part of the contract that only LuaRef types constructed from the same
parent Lua state are passed into Lua, so generating a panic there is not an
internal error.
2018-02-07 17:05:00 -05:00
kyren b9d9bea28a slightly faster, less obnoxious scope drop 2018-02-07 16:51:24 -05:00
kyren 98ee4e9492 More correct scope drop behavior
now no longer aborts if a Drop impl panics
2018-02-07 16:42:03 -05:00
kyren ab9841a02f Don't keep the unref list around forever after Lua is dropped 2018-02-07 11:16:22 -05:00
kyren cb25a99f70 Lots of changes, not sure if actually safe yet.
* Make Lua Send
* Add Send bounds to (nearly) all instances where userdata and functions are
  passed to Lua
* Add a "scope" method which takes a callback that accepts a `Scope`, and give
  `Scope` the ability to create functions and userdata that are !Send, *and also
  functions that are not even 'static!*.
2018-02-06 20:53:25 -05:00
kyren 7780a91e19 fix missing unwrap in tests 2018-02-06 20:29:48 -05:00
kyren b056ed2c4e Don't panic on mismatched `RegistryKey` use, instead return error 2018-02-06 10:51:39 -05:00
kyren 823c2deaca Slightly different strategy with RegistryKey values
Provide a method for automatic cleanup of expired RegistryKey values, so that
manually cleaning up registry values is optional.
2018-02-06 03:33:19 -05:00
kyren 8820e7705c test `owns_registry_value` 2018-02-06 00:54:04 -05:00
kyren 79635f29be Add method to check whether a RegistryKey is owned by a given `Lua` instance 2018-02-06 00:41:51 -05:00
kyren d43f8129f3 experimentally make `RegistryKey` Send 2018-02-06 00:05:35 -05:00
kyren fe35742026 Set the metatable of __gc'ed userdata to something more informative 2018-02-05 14:40:20 -05:00
kyren 6382baa991 Use ptr::write to initialize uninitalized memory, NOT mem::replace 2018-01-27 18:38:00 -05:00
kyren 77eb73a50c Simplify handling of userdata __gc and resurrected userdata.
Now, simply remove the userdata table immediately before dropping the userdata.
This does two things, it prevents __gc from double dropping the userdata, and
after the first call to __gc, it prevents the userdata from being identified as
any particular userdata type, so it cannot be misused after being finalized.

This change thus removes the userdata invalidation error, and simplifies a lot
of userdata handling code.

It also fixes a panic bug.  Because there is no predictable order for
finalizers, it is possible to run a userdata finalizer that does not resurrect
itself before a lua table finalizer that accesses that userdata, and this means
that there were several asserts that were possible to trigger in normal Lua code
in util.rs related to `WrappedError`.

Now, finalized userdata is simply a userdata with no methods, so any use of
finalized userdata becomes a normal script runtime error (though, with a
potentially confusing error message).  As a future improvement, we could set
a metatable on finalized userdata that provides a better error message.
2018-01-27 18:27:01 -05:00
kyren cbc882bad0 Bump cargo version to 0.11.0 2018-01-26 22:03:13 -05:00
kyren 1ffc6ee36f update changelog for 0.11.0 2018-01-26 21:58:23 -05:00
kyren 3db880af04 Update README for clarity, typofixes 2018-01-26 21:37:17 -05:00
kyren 8527266d3b Add an API that exposes the functionality of `lua_getuservalue` and `lua_setuservalue` 2018-01-26 20:06:18 -05:00
kyren 0801104762 ACTUALLY expose `RegistryKey` API
Also fixes a safety issue with RegistryKey, where you could use RegistryKeys
with mismatching Lua instances.
2018-01-26 19:43:53 -05:00
kyren b44a6c95df
Merge pull request #66 from jonas-schievink/docs
Clarify a few docs
2018-01-26 17:56:15 -05:00
Jonas Schievink ff847ea438 __gc would be safe now, reword MetaMethod docs accordingly 2018-01-26 21:31:01 +01:00
Jonas Schievink 0a4ae8d859 Additional `MetaMethod` docs 2018-01-26 19:44:35 +01:00
Jonas Schievink 79b028419f create_function docs: mention what returning `Err` does 2018-01-26 19:24:01 +01:00
Jonas Schievink 67f8e1d49c Fix rustdoc rendering warning 2018-01-26 18:35:21 +01:00
Jonas Schievink 2d89eb39da Don't use a `StdResult` alias for better docs.
This is a pretty opinionated change, but I find documentation to be
clearer when using plain old names everybody understands immediately.
2018-01-26 18:32:58 +01:00
kyren 50ecf39beb
Merge pull request #63 from chucklefish/failure
Experimentally use the `failure` crate for errors
2018-01-24 15:02:31 -05:00
kyren 79ba909db0 Experimentally use the `failure` crate for errors 2018-01-21 20:08:51 -05:00
kyren 685fc12aad Missed unprotected call to luaL_ref 2017-12-17 16:55:37 -05:00
kyren 443054f753 Update README, CHANGELOG, and Cargo.toml for 0.10.2 2017-12-17 01:14:11 -05:00
kyren 42007260ca Add automatic Lua "user accessible registry" keys
Also, during the implementation of this, I noticed a problem with the 0.10
memory safety, which is that luaL_ref is also memory unsafe.  I attempted to
change the API to support luaL_ref potentially returning Result, but this change
will cause an enormous amount of API chaos, (just as an example, it becomes
impossible to implement Clone for LuaRef as is).  Instead, luaL_ref now is
guarded by gc_guard.
2017-12-17 00:46:22 -05:00
kyren e6d84a4bb3 Change API names, add unset function 2017-12-16 18:05:53 -05:00
kyren ad23fe83e0 auto formatting 2017-12-16 17:46:32 -05:00
kyren bfb6111e0a API for registry access via string keys only (for now)
Also includes some fixes for stack usage and changes an assert_eq to lua_assert
2017-12-16 17:44:13 -05:00
kyren 56c9493f23 spelling fix for my spelling fix 2017-12-05 07:30:34 -05:00
kyren c67ae24c97 bump version for doc spelling fix 2017-12-05 00:16:35 -05:00
kyren d609d38675 spelling fixes 2017-12-05 00:12:00 -05:00
kyren 447810364a extra space 2017-12-04 23:57:08 -05:00
kyren 66a4e9a8e7 Add `ExpiredUserData` error and avoid what was previously a panic
Also make sure that panic messages clearly state that they are internal errors,
so people report them as a bug.  Since the only panics left are all internal
errors, just move the internal error message into the panic / assert macros.
2017-12-04 02:50:27 -05:00
kyren 80a98ef29c Couple of changes:
- Update readme, changelog, cargo version number in preparation for release
- Remove panicking behavior on recursive callback calls, add additional error
  variant for recursive callback errors.
2017-12-04 01:47:04 -05:00
kyren 0c644e7136 more reorganization in an attempt to shrink the size of lua.rs 2017-12-04 01:04:12 -05:00
kyren a44b6b5170 Move function and thread into their own modules, auto-formatting 2017-12-04 00:57:39 -05:00
kyren 51838f3509 Include garbage collector error type, remove unnecessary setmetatable wrapper 2017-12-04 00:35:13 -05:00
kyren d76935e683 I *THINK* this might actually be it, is rlua 'm' safe now? 2017-12-04 00:15:20 -05:00
kyren d0ff10b528 I believe this is all the external API changes necessary for 'm' safety 2017-12-03 23:45:00 -05:00
kyren e80e7d4540 missing push_string calls in util 2017-12-03 23:25:03 -05:00
kyren 67e8907f19 Couple of changes in preparation for 'm' safety:
- auto formatting
- add gc control to ffi
- add gc_guard to util functions
- use gc_guard to make util error handling functions never trigger __gc
  metamethod Lua errors even without __gc metatable wrapper
- sort of a technicality, don't call luaL_requiref outside of the Lua
  constructor, as it could trigger the garbage collector when user code has had
  a chance to set __gc metamethods.  Changes the API to load the debug table.
2017-12-03 23:01:03 -05:00
kyren 0bd676aa81 more refactoring 2017-12-03 21:19:32 -05:00
kyren c95f591935 remove specific protected functions in favor of generic protect_lua_call 2017-12-03 20:29:41 -05:00
kyren 37a3145ced missed stack size fix 2017-12-03 20:15:57 -05:00
kyren a490229f31 More refactoring towards mem error safety 2017-12-03 20:10:45 -05:00
kyren 0909ca34fc auto formatting 2017-12-03 18:25:53 -05:00
kyren e41c72d642 more refactoring trying to find a workable path to 'm' error safety 2017-12-03 18:15:31 -05:00
kyren 5742e3f20a still making small structural changes
slowly trying to refactor things until using all the protected calls in
protected_ffi is workable
2017-12-02 19:16:57 -05:00
kyren f51a822738 auto-formatting 2017-12-02 18:56:14 -05:00
kyren 8a6161b16f new strategy for protected ffi calls 2017-12-02 18:37:17 -05:00
kyren b7c80ec066 make some things private 2017-12-02 17:47:00 -05:00
kyren 2b7e89e7c6 move error / panic metatable creation 2017-12-02 17:13:46 -05:00
kyren fa1703d3d1 split macros into their own file 2017-12-02 17:04:33 -05:00
kyren 8a7e03978b Experimental protected versions of all used 'm' erroring functions 2017-12-02 15:41:53 -05:00
kyren cfa2b34a7b Bump version to 0.9.7, update changelog 2017-11-08 14:07:48 -05:00
kyren cf013a6a43
Merge pull request #55 from Timidger/bugfix/setting_metatable_nil
Fixes error when setting metatable to nil
2017-11-08 00:32:38 -05:00
Timidger f9c451bd26 Add test ensuring behaviour is correct 2017-11-07 20:13:52 -08:00
Timidger 8ac27877e4 Only get gc on setmetatable if metatable isn't nil 2017-11-07 20:13:30 -08:00
kyren 33eff0e132
Merge pull request #54 from Timidger/feature/debug-unsafe-lib
Added ability to load debug lib, Fixes #52
2017-11-04 02:57:03 -04:00
Timidger a137086064 Check that we have enough room on stack for module 2017-10-29 15:26:40 -07:00
Timidger 71fdca5477 Fixed test to use Lua assert 2017-10-29 15:24:54 -07:00
Timidger 316c6976e4 Added pop after loading lua library 2017-10-29 14:54:55 -07:00
Timidger 26b7901ab3 Added test for loading unsafe debug library in Lua 2017-10-29 14:53:30 -07:00
Timidger 2658433bd1 Added ability to load debug lib, Fixes #52 2017-10-28 20:46:24 -07:00
kyren 00a3153ae0 Bump version / changelog for 0.9.6 2017-10-26 16:53:14 -04:00