Commit Graph

1134 Commits

Author SHA1 Message Date
kyren 167184ae76 Allow arbitrary [u8] Lua strings 2018-09-30 15:42:04 -04:00
kyren 8810c36979 Add test for i128 Lua round-trip 2018-09-26 21:13:25 -04:00
kyren 6d17a383df Avoid mem::uninitialized in generic context as it is unsound with e.g. bool 2018-09-26 21:07:11 -04:00
kyren 58ce05ff9a Improve the situation with numerical conversion
This is a somewhat involved change with two breaking API changes:

1) Lua::coerce_xxx methods now return Option (this is easier and faster than
dealing with Result)
2) rlua numeric conversions now allow more loss of precision
conversions (e.g. 1.5f32 to 1i32)

The logic for the first breaking change is that mostly the coerce methods are
probably used internally, and they make sense as low-level fallible casts and
are now used as such, and there's no reason to confuse things with a Result with
a large error type and force the user to match on the error which will hopefully
only be FromLuaConversionError anyway.

The logic for the second change is that it matches the behavior of
num_traits::cast, and is more consistent in that *some* loss of precision
conversions were previously allowed (e.g. f64 to f32).

The problem is that now, Lua::coerce_integer and Lua::unpack::<i64> have
different behavior when given, for example, the number 1.5.  I still think this
is the best option, though, because the Lua::coerce_xxx methods represent how
Lua works internally and the standard C API cast functions that Lua provides,
and the ToLua / FromLua code represents the most common form of fallible Rust
numeric conversion.

I could revert this change and turn `Lua::eval::<i64>("1.5", None)` back into an
error, but it seems inconsistent to allow f64 -> f32 loss of precision but not
f64 -> i64 loss of precision.
2018-09-26 21:01:54 -04:00
kyren c3d0110722 Return rlua::Error on out of range numeric conversions using num_traits::cast 2018-09-24 22:14:50 -04:00
kyren 6a5ec6b387 cargo fmt 2018-09-24 22:13:42 -04:00
kyren 70b67052c9 Upgrade rustyline to 2.0 to avoid confusion 2018-09-24 21:27:29 -04:00
kyren 14090821d3 Make the changes I proposed to PR #79
Allows people (maybe only I care about this?) to build rlua in weird
environments.
2018-09-16 21:18:24 -04:00
kyren c07abdc03e
Merge pull request #79 from acrisci/system-lua-pkg-config
find system lua with pkg-config
2018-09-16 20:57:45 -04:00
kyren 654fff7065 Next version will be 0.15 2018-09-16 20:19:46 -04:00
kyren b8da08187d Move integration tests into top-level tests directory
other minor refactors
2018-09-16 20:15:51 -04:00
kyren 4a587ca1c5 Add compilefail test for Scope::create_nonstatic_userdata 2018-09-16 19:54:58 -04: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 58b991b83b
Merge pull request #86 from kyren/unstatic
Allow non-'static UserData created from Scope
2018-09-16 19:42:14 -04:00
kyren 6153ec4b95 basic tests for nonstatic userdata 2018-09-04 19:41:00 -04:00
kyren add547b356 comment terminology... fix? 2018-09-04 19:09:09 -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 37165a8201 Don't leak userdata if the metatable creation errors or panics 2018-09-04 03:38:22 -04:00
kyren 89b9968920 small macro style change 2018-09-02 20:34:39 -04:00
kyren 42054a9bfe better cargo test flags for travis 2018-09-02 02:53:30 -04:00
kyren 9c4d451d4c Implement tuple MultiValue tuple conversion up to 16 2018-09-02 02:48:54 -04:00
kyren ac21874d11 (cargo-release) start next development iteration 0.14.3-alpha.0 2018-08-06 19:09:27 -04:00
kyren ca8326475f (cargo-release) version 0.14.2 2018-08-06 19:09:09 -04:00
kyren 9fbcbdac64 Update changelog for 0.14.2 additional soundness fix 2018-08-06 19:08:50 -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 e4de847395 (cargo-release) start next development iteration 0.14.2-alpha.0 2018-08-05 12:35:05 -04:00
kyren e6ca00b65c (cargo-release) version 0.14.1 2018-08-05 12:33:26 -04:00
kyren 294f3e64ca Update changelog in preparation for 0.14.1 release
I'm a bit unclear on whether bumping a dependency's minor version is considered
a semver breaking change, but without doing this everyone probably gets
deprecation warnings?
2018-08-05 12:31:33 -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 8366960368 Update to failure 0.1.2, rename deprecated methods 2018-08-05 11:48:25 -04:00
kyren 2e1bdb64c0 format with up-to-date rustfmt 2018-08-05 09:51:39 -04:00
kyren e6688e1db2 very small doc fixes 2018-08-05 09:51:32 -04:00
kyren 73306d2286 Upgrade embedded Lua to 5.3.5 2018-07-18 05:29:27 -04:00
kyren 2d4f4b95c8 (cargo-release) start next development iteration 0.14.1-alpha.0 2018-06-29 01:28:28 -04:00
kyren 0cab2e25de (cargo-release) version 0.14.0 2018-06-29 01:25:15 -04:00
kyren 02bc8da203 Prepare for 0.14.0 release 2018-06-29 01:24:28 -04:00
Tony Crisci 66e49518d3 find system lua with pkg-config 2018-05-14 08:51:51 -04:00
kyren 31fa9173ae Fix #78 2018-05-02 20:05:43 -04:00
kyren 71f3dd50a1 New approach for ref types, use an auxillary thread stack
Vastly simpler and less magical than using a fixed size magical section of the
active stack, and seems to be no slower.  The only real downside is that
it *seems* extremely extremely hacky (and to be fair, it is).
2018-03-28 01:09:51 -04:00
kyren 60bfe1fb07 Nope, require failure 1.0 because otherwise it depends on backtrace automatically 2018-03-20 16:23:23 -04:00
kyren a22c23ae26 rely on failure 0.1.2 from git to get backtrace disabled by default
without relying on an unreleased 1.0 API.  Trying to get rid of the special
chucklefish hack branches in the meantime before 1.0.
2018-03-20 15:17:27 -04:00
kyren 5aa22de68e Use git dependency on failure 1.0 for right now 2018-03-20 14:07:32 -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 a05f0d5cd0 Where possible, don't call to_lua / from_lua / to_lua_multi / from_lua_multi callbacks during Lua stack manipulation
This should protect against being able to trigger a stack assert in Lua.  Lua
and associated types shoul be able to assume that LUA_MINSTACK stack slots are
available on any user entry point.  In the future, we could turn check_stack
into something that only checked the Lua stack when debug_assertions is true.
2018-03-19 15:16:40 -04:00
kyren 0d5e45e800 Always ensure LUA_MINSTACK available stack spaces on callback
Otherwise, cleanly error with an appropriate stack error.  Part of an effort to
ensure that it should not be possible to trigger a stack space assert.
2018-03-19 14:36:01 -04:00
kyren 4b6809c766 Clean up some lifetime specification 2018-03-19 14:35:46 -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 c1e1ac432c changelog fixes 2018-03-12 21:06:31 -04:00
kyren 1019ab8a3f Use rlua_ asserts instead of unreachable!, changelog updates for 0.14
0.14 will be released alongside `failure` 1.0 with a dependency update.
2018-03-12 20:36:39 -04:00
kyren c252668ba6 Fix README mention of "registry handles" 2018-03-12 18:13:22 -04:00
kyren c6c90f201c Documentation updates for new handle behavior, and some minor cleanup 2018-03-12 17:50:48 -04:00
kyren 4358034bbf Do not crash in release when accessing an AnyUserData
Also, don't bother asserting if the userdata has no metatable, just behave as
though the userdata has no type.  This should be impossible to trigger currently
without the debug library, but it is not really that useful of an assert anyway.
2018-03-12 17:48:05 -04:00
kyren f0775f4a1a Move several asserts to only be active with debug, bump alpha version number 2018-03-12 16:14: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
kyren ee23f199f0 Remove `stack_guard` function and instead just use StackGuard directly 2018-03-12 13:13:44 -04:00
kyren 7b2f7a2932 Add a simple userdata benchmark 2018-03-12 12:48:20 -04:00
kyren 95633ce915 Merge branch 'bench' 2018-03-12 12:29:27 -04:00
kyren 601e9f4cac A lot of performance changes.
Okay, so this is kind of a mega-commit of a lot of performance related changes
to rlua, some of which are pretty complicated.

There are some small improvements here and there, but most of the benefits of
this change are from a few big changes.  The simplest big change is that there
is now `protect_lua` as well as `protect_lua_call`, which allows skipping a
lightuserdata parameter and some stack manipulation in some cases.  Second
simplest is the change to use Vec instead of VecDeque for MultiValue, and to
have MultiValue be used as a sort of "backwards-only" Vec so that ToLuaMulti /
FromLuaMulti still work correctly.

The most complex change, though, is a change to the way LuaRef works, so that
LuaRef can optionally point into the Lua stack instead of only registry values.
At state creation a set number of stack slots is reserved for the first N LuaRef
types (currently 16), and space for these are also allocated separately
allocated at callback time.  There is a huge breaking change here, which is that
now any LuaRef types MUST only be used with the Lua on which they were created,
and CANNOT be used with any other Lua callback instance.  This mostly will
affect people using LuaRef types from inside a scope callback, but hopefully in
those cases `Function::bind` will be a suitable replacement.  On the plus side,
the rules for LuaRef types are easier to state now.

There is probably more easy-ish perf on the table here, but here's the
preliminary results, based on my very limited benchmarks:

create table            time:   [314.13 ns 315.71 ns 317.44 ns]
                        change: [-36.154% -35.670% -35.205%] (p = 0.00 < 0.05)
create array 10         time:   [2.9731 us 2.9816 us 2.9901 us]
                        change: [-16.996% -16.600% -16.196%] (p = 0.00 < 0.05)
                        Performance has improved.
create string table 10  time:   [5.6904 us 5.7164 us 5.7411 us]
                        change: [-53.536% -53.309% -53.079%] (p = 0.00 < 0.05)
                        Performance has improved.
call add function 3 10  time:   [5.1134 us 5.1222 us 5.1320 us]
                        change: [-4.1095% -3.6910% -3.1781%] (p = 0.00 < 0.05)
                        Performance has improved.
call callback add 2 10  time:   [5.4408 us 5.4480 us 5.4560 us]
                        change: [-6.4203% -5.7780% -5.0013%] (p = 0.00 < 0.05)
                        Performance has improved.
call callback append 10 time:   [9.8243 us 9.8410 us 9.8586 us]
                        change: [-26.937% -26.702% -26.469%] (p = 0.00 < 0.05)
                        Performance has improved.
create registry 10      time:   [3.7005 us 3.7089 us 3.7174 us]
                        change: [-8.4965% -8.1042% -7.6926%] (p = 0.00 < 0.05)
                        Performance has improved.

I think that a lot of these benchmarks are too "easy", and most API usage is
going to be more like the 'create string table 10' benchmark, where there are a
lot of handles and tables and strings, so I think that 25%-50% improvement is a
good guess for most use cases.
2018-03-11 23:20:10 -04:00
kyren 84ee394b1d Additional benchmarks 2018-03-11 17:50:17 -04:00
kyren a5377b959f Add some more benchmarks 2018-03-11 14:26:26 -04:00
kyren 964666e11b Use criterion for benchmarking, add some simple benchmarks 2018-03-10 10:31:57 -05:00
kyren 6470b6eefc Improve documentation about __index vs regular methods 2018-03-10 10:30:17 -05:00
kyren 431f84012a Enable stack leak panic universally
This will potentially panic on Drop of a `Lua` instance, which may be an abort
if this is a double panic, but that is more desirable than such a bug being
hidden.
2018-03-08 12:36:03 -05:00
kyren d06890afc6 Simplify stack_guard / stack_err_guard
The expected change is always zero, because stack_guard / stack_err_guard are
always used at `rlua` entry / exit points.
2018-03-08 11:40:24 -05:00
kyren 10802bf70f Whoops, fix an assert that was improperly changed to an internal error 2018-03-08 11:14:02 -05:00
kyren adfeaeab49 Change strategies for handling the Lua stack during panics
Previously, on an internal panic, the Lua stack would be reset before panicking
in an attempt to make sure that such panics would not cause stack leaks or leave
the stack in an unknown state.  Now, such panic handling is done in stack_guard
and stack_err_guard instead, and this is for a few reasons:

1) The previous approach did NOT handle user triggered panics that were outside
   of `rlua`, such as a panic in a ToLua / FromLua implementation.  This is
   especially bad since most other panics would be indicative of an internal bug
   anyway, so the utility of keeping `rlua` types usable after such panics was
   questionable.  It is much more sensible to ensure that `rlua` types are
   usable after *user generated* panics.
2) Every entry point into `rlua` should be guarded by a stack_guard or
   stack_err_guard anyway, so this should restore the Lua stack on exiting back
   to user code in all cases.
3) The method of stack restoration no longer *clears* the stack, only resets it
   to what it previously was.  This allows us, potentially, to keep values at
   the beginning of the Lua stack long term and know that panics will not
   clobber them.  There may be a way of dramatically speeding up ref types by
   using a small static area at the beginning of the stack instead of only the
   registry, so this may be important.
2018-03-08 10:59:50 -05:00
kyren 6a0264169a README updates 2018-03-06 07:04:50 -05:00
kyren 6ab7f99315 Revert "Temporary fix for #71. Remove when rust #48251 is fixed in stable."
This reverts commit 5d96ddc52a.
2018-03-06 07:03:58 -05:00
kyren 1e76de1d08 Update docs to include warning about RegistryKey in callbacks 2018-03-06 06:23:04 -05:00
kyren eb154e4a9e Further safety updates of `protect_lua_call`
Only allow Copy result types and Fn parameter functions, do not risk dropping
anything inside function passed to lua_pcall.
2018-03-06 06:22:05 -05:00
kyren 37feaebdce Also describe how protect_lua_call functions should not hold types that Drop 2018-03-01 17:56:19 -05:00
kyren 8ac78c4585 Make some changes whose necessity became recently apparent while reading rustc 1.24.1 change notes.
So, despite staring intently at the params structure magic in protect_lua_call,
there is still a nasty bug.  In the event of an error, the return value of the
parameters structure could be dropped despite being mem::unintialized.  Of
course, the actual return values are incidentally always Copy I think, so this
wasn't an actual bug, but I've proven to myself the danger of such dark majyyks.
Just use Option and be done with it, it doesn't have to be so complicated!

Also document why there are a slew of random functions in the ffi module.
2018-03-01 17:17:18 -05:00
kyren 0e9a70e688 (cargo-release) start next development iteration 0.13.1-alpha.0 2018-02-28 14:51:50 -05:00
kyren e98d4d1827 Update changelog / cargo.toml in prep for 0.13 2018-02-28 14:44:16 -05:00
kyren 5d96ddc52a Temporary fix for #71. Remove when rust #48251 is fixed in stable. 2018-02-28 14:43:15 -05:00
kyren d7995137d7 Add debug API to ffi (not used yet, was using experimentally)
Also fix for cstr! macro
2018-02-28 14:42:05 -05:00
kyren 8824a236b2 Remove slightly triggering, and now misleading, language from README 2018-02-19 18:16:43 -05:00
kyren a49ea51b79 Remove terrible awful no-good evil hack
The breakage is being addressed in rust itself.
2018-02-19 18:09:04 -05:00
kyren e19a5b6481 Cleanup max upvalues constant a bit, add some luaconf.h assumptions 2018-02-19 18:03:18 -05:00
kyren d78420b51c Communicate a little bit better about the checkstack constant 2018-02-19 17:57:39 -05:00
kyren ace5cb44f0 Letting scope handles escape the scope was unsafe
This simplifies the Scope lifetimes, and should make it a compile error for
scope created handles to exit the scope.  This should be strictly better, as you
would never WANT to do this, but I hope that I have not caused a subtle lifetime
problem that would prevent passing those created handles back into Lua.  I've
tested every situation I can think of, and it doesn't appear to be an issue, but
I admit that I don't fully understand everything involved and I could be missing
something.

The reason that I needed to do this is that if you can let a scope handle escape
the scope, you have a LuaRef with an unused registry id, and that can lead to
UB.  Since not letting the scope references escape is a strict improvement
ANYWAY (if I haven't caused a lifetime issue), this is the easiest fix.

This is technically a breaking change but I think in most cases if you notice it
you would be invoking UB, or you had a function that accepted a Scope or
something.  I don't know if it's worth a version bump?
2018-02-19 17:40:48 -05:00
kyren 0450c9b597 Make error_traceback never trigger a Lua error
It is called from both Lua and Rust, and any error would hide the error it's
trying to generate a traceback for.
2018-02-18 21:13:35 -05:00
kyren b07557c1c7 more hard to trigger bugs that I noticed doing conversion 2018-02-18 06:09:15 -05:00
kyren bb2a9c5b5d Fix several bugs found while doing C conversion
Fixing these in master in case I need to back out the change I'm making
2018-02-18 05:26:14 -05:00
kyren 52eedfd378 I'm not going to release this as is, but I need to weigh my options 2018-02-16 23:43:30 -05:00
kyren dec360f78f Can.. can I do this? Is this a thing that actually works?
Drastic times and all that.
2018-02-16 22:01:41 -05:00
kyren 73de52dcce Remove debugging println!s 2018-02-16 21:09:49 -05:00
kyren f0186d1799 Provisional "fix" for #71. Requires nightly :( 2018-02-15 21:39:35 -05:00
kyren 6b46e8abe9 prepare for 0.12.2 2018-02-14 00:56:20 -05:00
kyren fc058eba60 comment fixes 2018-02-12 13:56:23 -05:00
kyren c22aae461b Some changes for panic correctness, stack usage correctness, and speed 2018-02-12 13:54:31 -05:00
kyren c4b3170e2b More documentation fixes 2018-02-11 18:17:15 -05:00
kyren 9a45ef45e4 clarify the situation with the debug library a bit better 2018-02-11 18:12:20 -05:00
kyren 9388a72d61 Update Cargo.toml for 0.12.1 2018-02-11 17:55:40 -05:00
kyren 5f596d9117 Update changelog for 0.12.1, and add missing entry for 0.12.0 2018-02-11 17:54:27 -05:00
kyren bfe44089ef Documentation fixes / additions 2018-02-11 17:54:17 -05:00
kyren ea834635c1 Add `UserDataMethods::` `add_function_mut` and `add_meta_function_mut` 2018-02-11 17:53:25 -05:00