tomaka / hlua

Rust library to interface with Lua
MIT License
510 stars 48 forks source link

Allows pushing Lua code #91

Closed tomaka closed 7 years ago

tomaka commented 7 years ago

This PR allows pushing Lua code.

For example:

lua.set("foo", hlua::functions_read::LuaCode("return 5"));

You can then do:

let r = lua.execute_code::<i32>("foo()").unwrap();
assert_eq!(r, 5);

Similar to LuaCode, also adds LuaCodeFromReader.

This change required modifying the Push trait to return a Result instead, in order to handle syntax errors. More code cleanups will need to be made later. Since the code was quite dirty already, I don't think it's a problem to add even more panicking corner cases.

tomaka commented 7 years ago

While it is maybe controversial to modify Push to return a Result, let's look at the alternatives:

I'm going to change type Err = (); into type Err = Void; where Void is a non-instantiable type.

tomaka commented 7 years ago

The idea now is that set can still be used for anything except LuaCode and LuaCodeFromReader, while the new checked_set functions can be used with everything and returns a Result.