Commit Graph

937 Commits

Author SHA1 Message Date
Jonas Schievink 58c80c05be Replace unneeded eval by exec 2017-07-25 02:05:57 +02:00
Jonas Schievink 34fd4a00ce Document UserDataMethods 2017-07-25 02:00:49 +02:00
Jonas Schievink e2aeca58ae Table docs and examples
This looks almost like the standard lib now :)
2017-07-25 01:40:53 +02:00
Jonas Schievink 09dc89895a Add an example 2017-07-25 00:42:59 +02:00
Jonas Schievink 1f2fc6455f Beef up the String API
Adds `as_bytes` to view the string as a `[u8]`. Unlike the conversion to
a `&str` slice, this cannot fail.

Adds tests for both functions, which made me notice that `to_str` is
broken when the string contains null bytes, so I made it use the
`as_bytes` method.
2017-07-25 00:37:37 +02:00
Jonas Schievink 0b7b7e9d79 Add Function::call example 2017-07-25 00:16:43 +02:00
Jonas Schievink 59ab95f6ff Beef up Function::bind docs + example 2017-07-25 00:05:36 +02:00
Jonas Schievink 63e0587d26 Remove "equivalent" Lua code from Function::bind
Not only was this code not equivalent, it didn't even run since varargs
cannot be used as an upvalue (it's multiple values, after all).

Since Lua does not allow passing 2 sets of variadic arguments to a
function, the resulting code would be *very* complex and would involve
packing both sets of varargs into tables, concatenating them, then
`table.unpack`ing them to finally pass them.

This complex code would only make the docs more difficult to understand,
which is the opposite effect I originally intended with this. Let's just
get rid of this bad equivalence.
2017-07-24 23:52:15 +02:00
Jonas Schievink 9b809a8197 Make examples adhere to API guidelines
> Examples use ?, not try!, not unwrap (C-QUESTION-MARK)

> Like it or not, example code is often copied verbatim by users.
> Unwrapping an error should be a conscious decision that the user
> needs to make.
2017-07-24 23:45:24 +02:00
kyren 69fa01df45 auto formatting 2017-07-24 10:40:00 -04:00
kyren 1eaa201441 Merge pull request #17 from jonas-schievink/remove-lua-prefix
Remove the `Lua*` prefix from most types
2017-07-24 07:31:59 -04:00
kyren b3b0f17f59 Slight changes for consistency
I am not actually sure what the best pattern is to import conflicting
standard types, but this is at least consistent.
2017-07-24 07:30:29 -04:00
kyren 698785df64 Merge remote-tracking branch 'base/master' into remove-lua-prefix 2017-07-24 07:21:54 -04:00
kyren c7d2311c6a Do several more Lua prefix renames, add prelude module
Rename the following:

LuaNil => Nil
LuaExternalError => ExternalError
LuaExternalResult => ExternalResult
LuaCallback => Callback (internal only)

Use qualified re-exports at the top of the module.

Add a new public 'prelude' module which re-exports everything with a
non-conflicting name (Adds back the Lua prefix), and is meant to be imported
unqualified.
2017-07-24 07:12:52 -04:00
kyren 44c99ea1b9 Remove error_guard
Replace with custom protected versions of lua ffi functions.
2017-07-23 13:41:46 -04:00
Jonas Schievink 8f044755dc Fixes after rebase 2017-07-23 18:37:48 +02:00
Jonas Schievink b47f9e0d76 Continue renames in comments/strings 2017-07-23 18:36:50 +02:00
Jonas Schievink 8bd0c2c812 Rename `LuaString` to `String`
This required a lot of little adjustments where we used std's `String`
before. In downstream code, this shouldn't be necessary, as you can just
do `use rlua::String as LuaString` to disambiguate.
2017-07-23 18:36:50 +02:00
Jonas Schievink 9df7727eaa Remove the `Lua*` prefix from most types
cc #15

Doesn't touch `LuaString` mainly because that's a *lot* of renaming work
and the code looks weird. Also I want feedback before I proceed.
2017-07-23 18:36:50 +02:00
kyren 91dbbfe759 More aggressively remove code from error_guard 2017-07-23 10:41:51 -04:00
kyren d5ec09614c Merge pull request #22 from jonas-schievink/droptest
Add a test ensuring that userdata is dropped
2017-07-23 10:09:16 -04:00
Jonas Schievink 984ade66cc Make test_expired_userdata less noisy
The printing was cluttering the test runner output
2017-07-23 12:51:14 +02:00
Jonas Schievink 1c53ba3d4c Add a test ensuring that userdata is dropped 2017-07-23 12:50:42 +02:00
kyren 2bd7a2ee8c Reduce error_guard code to as little as possible
Also ensure that on error in error_guard the stack is in a predictable place.
2017-07-23 02:08:32 -04:00
kyren 36134e6373 Userdata can have __gc metamethods called multiple times
Lua 5.3 has the ability for scripts to define __gc metamethods on
tables, which gives them the ability to "resurrect" userdata after __gc
has been called.  This means, __gc can be called multiple times on
userdata.  This commit protects against this by simply panicking on
access after resurrection.  This is possibly not the best approach?
2017-07-23 01:00:33 -04:00
kyren 396a4b0916 Possibly this fixes rust stable? 2017-07-19 21:32:54 -04:00
kyren 5b723d58be Allow callback functions to have 'lua lifetime rather than 'static
Fixes #14
2017-07-19 21:28:16 -04:00
kyren 6df96d8eb1 Rename LightUserData to LuaLightUserData for consistency
I know, this is the opposite of PR #17 wishes to do, please don't take this as
an indication that I would wish to do the opposite.  I actually want to discuss
PR #17 with you, but I'm not sure about it yet, and my pedantry will not allow
me to let this remain inconsistent in the meantime.  This way, either way it's
consistent haha.
2017-07-19 20:15:46 -04:00
kyren 160c5405e1 Add back explanatory comment about trying "return <expr>" before statement
Also run through rustfmt
2017-07-19 20:10:11 -04:00
kyren 4f2d894a52 Merge pull request #16 from jonas-schievink/eval-name
Give `Lua::eval` a source name param and simplify
2017-07-19 20:07:37 -04:00
Jonas Schievink d9098900d1 Give `Lua::eval` a source name param and simplify 2017-07-16 22:53:32 +02:00
kyren eafe4c7c30 cargo version bump for linking fix 2017-07-09 17:20:51 -04:00
kyren cc216721b8 slightly cleaner way of linking to an external liblua5.3, if the option is enabled 2017-07-09 17:05:54 -04:00
kyren b8fab1b3ed Make the builtin version of lua 5.3 optional
In case you would need to build a crazy custom version of lua for a weird
platform.  This could possibly be done in a cleaner way.
2017-07-05 19:35:53 -04:00
kyren 6b8a4240e2 format fix, fixes rustfmt warning 2017-06-30 15:17:53 -04:00
kyren d9478d6cd7 Comments should be wrapped at 100 lines, according to standard 2017-06-27 14:05:49 -04:00
kyren 6f0caa4a6d Update README, small example changes. 2017-06-25 22:25:28 -04:00
kyren 8156b7e529 Small README typo fix 2017-06-25 18:33:02 -04:00
kyren cd4343c83e Bump cargo version (again! I'm sorry!), and update README 2017-06-25 18:27:52 -04:00
kyren d3b311fe49 Another major API change, out of stack space is not an Err
It, ahem "should not" be possible to exhaust lua stack space in normal usage,
and causing stack errors to be Err is slightly obnoxious.  I have been wanting
to make this change for a while, and removing the callback API from tables makes
this sensible *I think*.

I can think of a couple of ways that this is not technically true, but I think
that they are acceptable, or should be handled differently.

One, you can make arbitrarily sized LuaVariadic values.  I think this is maybe a
bug already, because there is an argument limit in Lua which is lower than the
stack limit.  I'm not sure what happens there, but if it is a stack based panic,
(or any panic?) it is a bug.

Two, I believe that if you recurse over and over between lua -> rust -> lua ->
rust etc, and call rlua API functions, you might get a stack panic.  I think for
trusted lua code, this is morally equivalent to a regular stack overflow in
plain rust, which is already.. well it's not a panic but it's some kind of safe
crash I'm not sure, so I think this is acceptable.  For *untrusted* lua code,
this could theoretically be a problem if the API provided a callback that would
call back into lua, then some lua script could force a stack based panic.  There
are so many concerns with untrusted lua code, and this library is NOT safe
enough yet for untrusted code (it doesn't even provide an option to limit lua to
the safe API subset yet!), so this is not currently an issue.  When the library
provides support for "safe lua", it should come with big warnings anyway, and
being able to force a stack panic is pretty minor in comparison.

I think if there are other ways to cause unbounded stack usage, that it is a
bug, or there can be an error just for that situation, like argument count
limits.

This commit also fixes several stupid bugs with tests, stack checking, and
panics.
2017-06-25 17:15:11 -04:00
kyren bf9bf849c2 Simplification of error types
The multi-level error types were a mistake.  Probably should have waited on the
cargo version bump, oh well.
2017-06-25 04:25:48 -04:00
kyren 8548fbd263 Bump cargo version to 0.7.0
I'm not 100% sold on the LuaError design, I think there are a lot of questions
still, but there have been enough bugfixes that it's better to do a cargo bump.
2017-06-25 02:41:02 -04:00
kyren 7dba280a4b Tests for LuaError conversion, Important pcall / xpcall bugfixes. 2017-06-25 02:40:09 -04:00
kyren cbae1a805a This is a SLIGHTLY better implementation I think. 2017-06-25 02:04:14 -04:00
kyren 2c439f8097 Not sure I like everything about this approach yet 2017-06-25 01:47:55 -04:00
kyren a609f709ee A lot of corner case bugfixes 2017-06-25 01:10:26 -04:00
kyren b59b8cc23b Different strategy.. 2017-06-24 22:26:35 -04:00
kyren 1fda34225e Small improvements to WrappedError
Currently trying to figure out how to make LuaError a proper LuaUserDataType
2017-06-24 21:44:27 -04:00
kyren 3deb6df525 Lots of LuaError changes
It is possible that I have gone too far here into error discrimination and
should scale it back, not sure yet.
2017-06-24 20:57:04 -04:00
kyren 47db72cac4 Big API incompatible error change, remove dependency on error_chain
The current situation with error_chain is less than ideal, and there are lots of
conflicting interests that are impossible to meet at once.  Here is an
unorganized brain dump of the current situation, stay awhile and listen!

This change was triggered ultimately by the desire to make LuaError implement
Clone, and this is currently impossible with error_chain.  LuaError must
implement Clone to be a proper lua citizen that can live as userdata within a
lua runtime, because there is no way to limit what the lua runtime can do with a
received error.  Currently, this is solved by there being a rule that the error
will "expire" if the error is passed back into rust, and this is very
sub-optimal.  In fact, one could easily imagine a scenario where lua is for
example memoizing some function, and if the function has ever errored in the
past the function should continue returning the same error, and this situation
immediately fails with this restriciton in place.

Additionally, there are other more minor problems with error_chain which make
the API less good than it could be, or limit how we can use error_chain.  This
change has already solved a small bug in a Chucklefish project, where the
conversion from an external error type (Borrow[Mut]Error) was allowed but not
intended for user code, and was accidentally used.  Additionally, pattern
matching on error_chain errors, which should be common when dealing with Lua, is
less convenient than a hand rolled error type.

So, if we decide not to use error_chain, we now have a new set of problems if we
decide interoperability with error_chain is important.  The first problem we run
into is that there are two natural bounds for wrapped errors that we would
pick, (Error + Send + Sync), or just Error, and neither of them will
interoperate well with error_chain.  (Error + Send + Sync) means we can't wrap
error chain errors into LuaError::ExternalError (they're missing the Sync
bound), and having the bounds be just Error means the opposite, that we can't
hold a LuaError inside an error_chain error.

We could just decide that interoperability with error_chain is the most
important qualification, and pick (Error + Send), but this causes a DIFFERENT
set of problems.  The rust ecosystem has the two primary error bounds as Error
or (Error + Send + Sync), and there are Into impls from &str / String to
Box<Error + Send + Sync> for example, but NOT (Error + Send).  This means that
we are forced to manually recreate the conversions from &str / String to
LuaError rather than relying on a single Into<Box<Error + Send + Sync>> bound,
but this means that string conversions have a different set of methods than
other error types for external error conversion.  I have not been able to figure
out an API that I am happy with that uses the (Error + Send) bound.  Box<Error>
is obnoxious because not having errors implement Send causes needless problems
in a multithreaded context, so that leaves (Error + Send + Sync).  This is
actually a completely reasonable bound for external errors, and has the nice
String Into impls that we would want, the ONLY problem is that it is a pain to
interoperate with the current version of error_chain.

It would be nice to be able to specify the traits that an error generated by the
error_chain macro would implement, and this is apparently in progress in the
error_chain library.  This would solve both the problem with not being able to
implement Clone and the problems with (Error + Send) bounds.  I am not convinced
that this library should go back to using error_chain when that functionality is
in stable error_chain though, because of the other minor usability problems with
using error_chain.

In that theoretical situation, the downside of NOT using error_chain is simply
that there would not be automatic stacktraces of LuaError.  This is not a huge
problem, because stack traces of lua errors are not extremely useful, and for
external errors it is not too hard to create a different version of the
LuaExternalResult / LuaExternalError traits and do conversion from an
error_chain type into a type that will print the stacktrace on display, or
use downcasting in the error causes.

So in summary, this library is no longer using error_chain, and probably will
not use it again in the future.  Currently this means that to interoperate with
error_chain, you should use error_chain 0.8.1, which derives Sync on errors, or
wait for a version that supports user defined trait derives.  In the future
when error_chain supports user defined trait derives, users may have to take an
extra step to make wrapped external errors print the stacktrace that they
capture.

This change works, but is not entirely complete.  There is no error
documentation yet, and the change brought to a head an ugly module organization
problem.  There will be more commits for documentation and reorganization, then
a new stable version of rlua.
2017-06-24 18:11:56 -04:00
kyren 802d5467c1 Change LuaError ToLua approach
Lua should be consistent independent of the way they are produced.  pcall in lua
should produce the same sort of error that would be returned by a LuaError
conversion.

The situation is not currently great, because LuaError is not Clone, so passing
a LuaError back into rust will result in the error being "consumed".
2017-06-23 19:41:08 -04:00
kyren 8e3a9f0e84 Add ToString metamethod, make LuaError implement LuaUserData
LuaError implementing LuaUserData makes it easy to return LuaResult from a
callback to implement callback functions that can error.
2017-06-23 15:24:03 -04:00
kyren 7b7f7f36fc Bump cargo version, bugfix for numeric conversions.
This could arguably be a semver update, but the previous behavior was definitely
not intentional, it was a bug.
2017-06-23 03:28:43 -04:00
kyren 3ab3970c01 ACTUALLY correctly limit numeric conversions
Don't mistakenly do rust-side conversions from integer <-> number
2017-06-23 00:51:16 -04:00
kyren 918edad1b2 Merge pull request #13 from jonas-schievink/example
Add a few examples
2017-06-22 14:09:36 -05:00
Jonas Schievink 979e1d1675 Add example to `LuaString::to_str` 2017-06-22 10:49:18 +02:00
kyren 9dc0a3e098 Simplify load / exec / eval
load is the fundamental operation here
2017-06-21 19:40:18 -05:00
kyren efd77fb862 Merge pull request #12 from jonas-schievink/load
Add `Lua::load` for precompiling chunks
2017-06-21 19:23:22 -05:00
Jonas Schievink f75385305c Add an example to `LuaFunction::bind` 2017-06-22 00:38:08 +02:00
Jonas Schievink 2e87556a1b Add example to `LuaThread::resume`
Adapted from #8
2017-06-22 00:28:44 +02:00
Jonas Schievink 14c168f118 Add `Lua::load` for precompiling chunks
This allows loading scripts into memory, performing a syntax check and
precompiling them, without immediately running them like `exec` and
`eval` do.
2017-06-22 00:16:30 +02:00
kyren 06cd2c8e68 Add numeric conversion test 2017-06-21 16:53:52 -05:00
kyren f92e21d5cb Bump cargo version to 0.6.0 2017-06-20 18:13:40 -05:00
kyren f0c2f9a870 Method renames, remove ToLua / FromLua impls for Sets
There is no single obvious form for a set in lua, and it is not very difficult
to accept a table and convert the sequence values into a set.

Also rename some methods as per discussion.
2017-06-20 18:04:25 -05:00
kyren 7924a910e1 typo fixes 2017-06-19 02:08:50 -05:00
kyren a0e83b3d1b Big API change for tables
Removes the callback versions of loops over LuaTable, instead now there are
'pairs' and 'ipairs' iterators over them.
2017-06-19 02:04:00 -05:00
kyren f1dee39416 update reference to raw_length method 2017-06-18 17:11:55 -05:00
kyren 5b148536d6 Merge pull request #10 from jonas-schievink/api-revamp
Rename a few methods to better match their intention or conventions
2017-06-18 17:02:18 -05:00
Jonas Schievink 16ae4720d6 Rename `LuaString::get` to `to_str`
This is what similar types in libstd do (`CStr::to_str`,
`OsStr::to_str`, `Path::to_str`).
2017-06-18 14:48:45 +02:00
Jonas Schievink a47a6e32ef Rename `LuaTable::has` to `contains_key`
This is what all Rust maps use
2017-06-18 14:39:18 +02:00
Jonas Schievink ed210e88b5 Rename `LuaTable::(raw_)length` to `(raw_)len`
All Rust containers use `len` for this. Even in the Lua API, this is
called `lua_len` and `lua_rawlen`.
2017-06-18 14:36:30 +02:00
Jonas Schievink de0f3dc3c0 Rename `Lua::load` to `Lua::exec`
When talking about "loading" Lua code, it usually means compiling a
chunk of code into a runnable Lua function, but without actually
running it. This makes that clear.
2017-06-18 14:31:38 +02:00
kyren ba9f7a2ee7 Add mostly untested missing metamethods, improve documentation about them 2017-06-18 00:28:55 -04:00
kyren 56a30fba2e Slightly more palatable coroutine API 2017-06-17 23:50:40 -04:00
kyren 4d34ccdba1 Merge pull request #9 from jonas-schievink/docs
Documentation
2017-06-17 23:28:13 -04:00
kyren 64d1eae55c New 0.5.x cargo update with important coroutine bugfix, build changes 2017-06-17 23:23:17 -04:00
kyren edc5fc8ce3 ACTUALLY check the status of lua threads before calling resume 2017-06-17 22:40:09 -04:00
Jonas Schievink 0e14d787b7 Documentation 2017-06-18 03:31:20 +02:00
kyren 7b00ff02f2 enable more lua platform defines in build.rs 2017-06-17 16:56:36 -04:00
kyren 75b7024a93 Merge pull request #7 from jonas-schievink/popen
Enable `io.popen` on Linux
2017-06-17 16:31:09 -04:00
Jonas Schievink 4a980de0ed Allow multiline input in the REPL example 2017-06-17 14:43:05 +02:00
Jonas Schievink 2cddfecfb2 Enable `io.popen` on Linux 2017-06-17 01:53:51 +02:00
Jonas Schievink 4f05516783 Add a simple repl example 2017-06-17 01:19:21 +02:00
kyren b02dda3431 that example hardly shows ALL the features 2017-06-15 16:30:16 -04:00
kyren 44370dd64e bump cargo version, nice addition of Result conversion 2017-06-15 16:28:12 -04:00
kyren 32c83d77b7 small warning / clippy fixes, doc comments 2017-06-15 16:27:39 -04:00
kyren 484479477b Merge pull request #4 from jonas-schievink/result-to-lua-dejavu
Allow passing `Result<T, E>` to Lua
2017-06-15 15:59:20 -04:00
kyren 0d652a3c94 Merge pull request #5 from jonas-schievink/who-needs-macros
Remove src/macros.rs
2017-06-15 15:55:54 -04:00
kyren fc9164ab9f Merge pull request #6 from jonas-schievink/cfg_attr
Utilize `cfg_attr` instead of duplicating code
2017-06-15 15:55:06 -04:00
kyren 0022057058 rustfmt changes 2017-06-15 10:26:39 -04:00
Jonas Schievink 6b651820b6 Utilize `cfg_attr` instead of duplicating code 2017-06-14 01:30:33 +02:00
Jonas Schievink 5019396bd7 Remove src/macros.rs
It's not used by anything. It's not even included via `mod macros;`.
2017-06-14 01:24:38 +02:00
Jonas Schievink 80be9e8091 Allow passing `Result<T, E>` to Lua
For this to work, both `T` and `E` need to implement `ToLua`. An `Ok(t)`
passes the contained `T`, while an `Err(e)` passes `nil` followed by the
contained `E`. This matches the common Lua idiom used by functions like
`io.open`.

Closes #3
2017-06-14 01:17:41 +02:00
kyren 8440298e71 Go ahead and release 0.5.0
partially for the selfish reason that my submodule setup does not deal well with
ureleased versions of cargo libs where the version number ends with -pre
2017-06-11 01:33:08 -04:00
kyren 5c8aa19b8d Two major API changes:
Allow load to return values, allows reimplementing require() like functions
properly.

Make globals table explicit in Lua, remove Lua::get / Lua::set in favor of
Lua::globals.  Allows obeying globals metatable, using other Table functions on
the globals table.

Also added "has" method as shorthand for checking whether a table entry is not
nil.
2017-06-11 01:12:25 -04:00
kyren 8203414b76 Use LUA_RIDX_MAINTHREAD instead of weird bespoke method 2017-06-05 05:03:18 -04:00
kyren 7436e84e93 make sure other table methods do not trigger metamethods and errors 2017-06-05 04:10:54 -04:00
kyren cb7c55f742 Don't need pub on internal function 2017-06-05 01:51:48 -04:00
kyren 1c0b1014ee Improve performance of create_table / create_array_table 2017-06-05 01:46:45 -04:00
kyren 9aed99e3e3 atpanic function was not a good idea, it's not sound
It crosses the C api boundary, the only proper way to panic would be to actually
error guard everywhere, aborting is fine.
2017-06-05 01:05:41 -04:00