Skip to content

Commit 321e35d

Browse files
committed
ConsumeRead now uses HasLua
1 parent 3466c3d commit 321e35d

File tree

6 files changed

+90
-82
lines changed

6 files changed

+90
-82
lines changed

rust-hl-lua/src/any.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ impl<'lua, L: HasLua<'lua>> CopyRead<L> for AnyLuaValue {
3737
}
3838
}
3939

40-
impl<'a,'lua> ConsumeRead<'a,'lua> for AnyLuaValue {
41-
fn read_from_variable(var: LoadedVariable<'a, 'lua>) -> Result<AnyLuaValue, LoadedVariable<'a, 'lua>> {
42-
match CopyRead::read_from_lua(var.lua, -1) {
40+
impl<'a, 'lua, L: HasLua<'lua>> ConsumeRead<'a, L> for AnyLuaValue {
41+
fn read_from_variable(mut var: LoadedVariable<'a, L>) -> Result<AnyLuaValue, LoadedVariable<'a, L>> {
42+
match CopyRead::read_from_lua(&mut var, -1) {
4343
None => Err(var),
4444
Some(a) => Ok(a)
4545
}

rust-hl-lua/src/functions_read.rs

+24-20
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use ffi;
2-
use { Lua, ConsumeRead, CopyRead, LoadedVariable, LuaError, ExecutionError, WrongType, SyntaxError };
2+
use {HasLua, Lua, ConsumeRead, CopyRead, LoadedVariable, LuaError, ExecutionError, WrongType, SyntaxError};
33

44
#[unstable]
5-
pub struct LuaFunction<'a,'lua> {
6-
variable: LoadedVariable<'a,'lua>
5+
///
6+
/// Lifetime `'a` represents the lifetime of the function on the stack.
7+
/// Param `L` represents the stack the function has been loaded on and must be a `HasLua`.
8+
pub struct LuaFunction<'a, L> {
9+
variable: LoadedVariable<'a, L>,
710
}
811

912
struct ReadData {
@@ -22,14 +25,14 @@ extern fn reader(_: *mut ffi::lua_State, dataRaw: *mut ::libc::c_void, size: *mu
2225
data.buffer.as_ptr() as *const ::libc::c_char
2326
}
2427

25-
impl<'a,'lua> LuaFunction<'a,'lua> {
26-
pub fn call<V: CopyRead<Lua<'lua>>>(&mut self) -> Result<V, LuaError> {
28+
impl<'a, 'lua, L: HasLua<'lua>> LuaFunction<'a, L> {
29+
pub fn call<V: CopyRead<LoadedVariable<'a, L>>>(&mut self) -> Result<V, LuaError> {
2730
// calling pcall pops the parameters and pushes output
28-
let pcallReturnValue = unsafe { ffi::lua_pcall(self.variable.lua.lua, 0, 1, 0) }; // TODO:
31+
let pcallReturnValue = unsafe { ffi::lua_pcall(self.variable.use_lua(), 0, 1, 0) }; // TODO:
2932

3033
// if pcall succeeded, returning
3134
if pcallReturnValue == 0 {
32-
return match CopyRead::read_from_lua(self.variable.lua, -1) {
35+
return match CopyRead::read_from_lua(&mut self.variable, -1) {
3336
None => Err(WrongType),
3437
Some(x) => Ok(x)
3538
};
@@ -41,21 +44,22 @@ impl<'a,'lua> LuaFunction<'a,'lua> {
4144
}
4245

4346
if pcallReturnValue == ffi::LUA_ERRRUN {
44-
let errorMsg: String = CopyRead::read_from_lua(self.variable.lua, -1).expect("can't find error message at the top of the Lua stack");
45-
unsafe { ffi::lua_pop(self.variable.lua.lua, 1) };
46-
return Err(ExecutionError(errorMsg));
47+
unimplemented!()
48+
/*let errorMsg: String = CopyRead::read_from_lua(self.variable.lua, -1).expect("can't find error message at the top of the Lua stack");
49+
unsafe { ffi::lua_pop(self.variable.use_lua(), 1) };
50+
return Err(ExecutionError(errorMsg));*/
4751
}
4852

4953
fail!("Unknown error code returned by lua_pcall: {}", pcallReturnValue)
5054
}
5155

52-
pub fn load_from_reader<R: ::std::io::Reader + 'static>(lua: &'a mut Lua<'lua>, code: R)
53-
-> Result<LuaFunction<'a,'lua>, LuaError>
56+
pub fn load_from_reader<R: ::std::io::Reader + 'static>(lua: &'a mut L, code: R)
57+
-> Result<LuaFunction<'a, L>, LuaError>
5458
{
5559
let readdata = ReadData { reader: box code, buffer: unsafe { ::std::mem::uninitialized() } };
5660

5761
let loadReturnValue = "chunk".with_c_str(|chunk|
58-
unsafe { ffi::lua_load(lua.lua, reader, ::std::mem::transmute(&readdata), chunk, ::std::ptr::null()) }
62+
unsafe { ffi::lua_load(lua.use_lua(), reader, ::std::mem::transmute(&readdata), chunk, ::std::ptr::null()) }
5963
);
6064

6165
if loadReturnValue == 0 {
@@ -68,7 +72,7 @@ impl<'a,'lua> LuaFunction<'a,'lua> {
6872
}
6973

7074
let errorMsg: String = CopyRead::read_from_lua(lua, -1).expect("can't find error message at the top of the Lua stack");
71-
unsafe { ffi::lua_pop(lua.lua, 1) };
75+
unsafe { ffi::lua_pop(lua.use_lua(), 1) };
7276

7377
if loadReturnValue == ffi::LUA_ERRMEM {
7478
fail!("LUA_ERRMEM");
@@ -80,8 +84,8 @@ impl<'a,'lua> LuaFunction<'a,'lua> {
8084
fail!("Unknown error while calling lua_load");
8185
}
8286

83-
pub fn load(lua: &'a mut Lua<'lua>, code: &str)
84-
-> Result<LuaFunction<'a,'lua>, LuaError>
87+
pub fn load(lua: &'a mut L, code: &str)
88+
-> Result<LuaFunction<'a, L>, LuaError>
8589
{
8690
let reader = ::std::io::MemReader::new(code.to_c_str().as_bytes().init().to_owned());
8791
LuaFunction::load_from_reader(lua, reader)
@@ -95,11 +99,11 @@ impl<'a,'lua> LuaFunction<'a,'lua> {
9599
}
96100
}*/
97101

98-
impl<'a,'lua> ConsumeRead<'a,'lua> for LuaFunction<'a,'lua> {
99-
fn read_from_variable(var: LoadedVariable<'a, 'lua>)
100-
-> Result<LuaFunction<'a, 'lua>, LoadedVariable<'a, 'lua>>
102+
impl<'a, 'lua, L: HasLua<'lua>> ConsumeRead<'a, L> for LuaFunction<'a, L> {
103+
fn read_from_variable(mut var: LoadedVariable<'a, L>)
104+
-> Result<LuaFunction<'a, L>, LoadedVariable<'a, L>>
101105
{
102-
if unsafe { ffi::lua_isfunction(var.lua.lua, -1) } {
106+
if unsafe { ffi::lua_isfunction(var.use_lua(), -1) } {
103107
Ok(LuaFunction{ variable: var })
104108
} else {
105109
Err(var)

rust-hl-lua/src/lib.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ impl<'lua> HasLua<'lua> for Lua<'lua> {
4949
}
5050

5151
/// Object which allows access to a Lua variable.
52-
struct LoadedVariable<'var, 'lua> {
53-
lua: &'var mut Lua<'lua>,
54-
size: uint // number of elements at the top of the stack
52+
struct LoadedVariable<'var, L> {
53+
lua: &'var mut L,
54+
size: uint, // number of elements over "lua"
5555
}
5656

57-
impl<'var, 'lua> HasLua<'lua> for LoadedVariable<'var, 'lua> {
57+
impl<'var, 'lua, L: HasLua<'lua>> HasLua<'lua> for LoadedVariable<'var, L> {
5858
fn use_lua(&mut self) -> *mut ffi::lua_State {
59-
self.lua.lua
59+
self.lua.use_lua()
6060
}
6161
}
6262

@@ -73,9 +73,9 @@ pub trait Push<L> {
7373

7474
/// Should be implemented by types that can be read by consomming a LoadedVariable.
7575
#[unstable]
76-
pub trait ConsumeRead<'a, 'lua> {
76+
pub trait ConsumeRead<'a, L> {
7777
/// Returns the LoadedVariable in case of failure.
78-
fn read_from_variable(var: LoadedVariable<'a, 'lua>) -> Result<Self, LoadedVariable<'a, 'lua>>;
78+
fn read_from_variable(var: LoadedVariable<'a, L>) -> Result<Self, LoadedVariable<'a, L>>;
7979
}
8080

8181
/// Should be implemented by whatever type can be read by copy from the Lua stack.
@@ -176,21 +176,21 @@ impl<'lua> Lua<'lua> {
176176

177177
/// Executes some Lua code on the context.
178178
#[unstable]
179-
pub fn execute<T: CopyRead<Lua<'lua>>>(&mut self, code: &str) -> Result<T, LuaError> {
179+
pub fn execute<'a, T: CopyRead<LoadedVariable<'a, Lua<'lua>>>>(&'a mut self, code: &str) -> Result<T, LuaError> {
180180
let mut f = try!(functions_read::LuaFunction::load(self, code));
181181
f.call()
182182
}
183183

184184
/// Executes some Lua code on the context.
185185
#[unstable]
186-
pub fn execute_from_reader<T: CopyRead<Lua<'lua>>, R: std::io::Reader + 'static>(&mut self, code: R) -> Result<T, LuaError> {
186+
pub fn execute_from_reader<'a, T: CopyRead<LoadedVariable<'a, Lua<'lua>>>, R: std::io::Reader + 'static>(&'a mut self, code: R) -> Result<T, LuaError> {
187187
let mut f = try!(functions_read::LuaFunction::load_from_reader(self, code));
188188
f.call()
189189
}
190190

191191
/// Loads the value of a global variable.
192192
#[unstable]
193-
pub fn load<'a, I: Str, V: ConsumeRead<'a, 'lua>>(&'a mut self, index: I) -> Option<V> {
193+
pub fn load<'a, I: Str, V: ConsumeRead<'a, Lua<'lua>>>(&'a mut self, index: I) -> Option<V> {
194194
unsafe { ffi::lua_getglobal(self.lua, index.as_slice().to_c_str().unwrap()); }
195195
ConsumeRead::read_from_variable(LoadedVariable { lua: self, size: 1 }).ok()
196196
}
@@ -210,7 +210,7 @@ impl<'lua> Lua<'lua> {
210210
}
211211

212212
#[unstable]
213-
pub fn load_new_table<'var>(&'var mut self) -> LuaTable<'var, 'lua> {
213+
pub fn load_new_table<'var>(&'var mut self) -> LuaTable<'var, Lua<'lua>> {
214214
unsafe { ffi::lua_newtable(self.lua) };
215215
ConsumeRead::read_from_variable(LoadedVariable { lua: self, size: 1 }).ok().unwrap()
216216
}
@@ -225,9 +225,10 @@ impl<'lua> Drop for Lua<'lua> {
225225
}
226226
}
227227

228-
#[unsafe_destructor]
229-
impl<'a, 'lua> Drop for LoadedVariable<'a, 'lua> {
228+
// TODO: crashes the compiler
229+
/*#[unsafe_destructor]
230+
impl<'a, 'lua, L: HasLua<'lua>> Drop for LoadedVariable<'a, L> {
230231
fn drop(&mut self) {
231-
unsafe { ffi::lua_pop(self.lua.lua, self.size as libc::c_int) }
232+
unsafe { ffi::lua_pop(self.use_lua(), self.size as libc::c_int) }
232233
}
233-
}
234+
}*/

rust-hl-lua/src/lua_tables.rs

+30-30
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,71 @@ use {HasLua, Lua, CopyRead, ConsumeRead, LoadedVariable, Push, Index};
22
use ffi;
33

44
#[unstable]
5-
pub struct LuaTable<'var, 'lua> {
6-
variable: LoadedVariable<'var, 'lua>
5+
pub struct LuaTable<'var, L> {
6+
variable: LoadedVariable<'var, L>
77
}
88

9-
impl<'var, 'lua> HasLua<'lua> for LuaTable<'var, 'lua> {
9+
impl<'var, 'lua, L: HasLua<'lua>> HasLua<'lua> for LuaTable<'var, L> {
1010
fn use_lua(&mut self) -> *mut ffi::lua_State {
1111
self.variable.use_lua()
1212
}
1313
}
1414

1515
// while the LuaTableIterator is active, the current key is constantly pushed over the table
1616
#[unstable]
17-
pub struct LuaTableIterator<'var, 'lua, 'table> {
18-
table: &'table mut LuaTable<'var, 'lua>
17+
pub struct LuaTableIterator<'var, 'table, L> {
18+
table: &'table mut LuaTable<'var, L>
1919
}
2020

21-
impl<'var, 'lua, 'table> HasLua<'lua> for LuaTableIterator<'var, 'lua, 'table> {
21+
impl<'var, 'lua, 'table, L: HasLua<'lua>> HasLua<'lua> for LuaTableIterator<'var, 'table, L> {
2222
fn use_lua(&mut self) -> *mut ffi::lua_State {
2323
self.table.use_lua()
2424
}
2525
}
2626

27-
impl<'var, 'lua> ConsumeRead<'var, 'lua> for LuaTable<'var, 'lua> {
28-
fn read_from_variable(var: LoadedVariable<'var, 'lua>)
29-
-> Result<LuaTable<'var, 'lua>, LoadedVariable<'var, 'lua>>
27+
impl<'var, 'lua, L: HasLua<'lua>> ConsumeRead<'var, L> for LuaTable<'var, L> {
28+
fn read_from_variable(mut var: LoadedVariable<'var, L>)
29+
-> Result<LuaTable<'var, L>, LoadedVariable<'var, L>>
3030
{
31-
if unsafe { ffi::lua_istable(var.lua.lua, -1) } {
31+
if unsafe { ffi::lua_istable(var.use_lua(), -1) } {
3232
Ok(LuaTable{ variable: var })
3333
} else {
3434
Err(var)
3535
}
3636
}
3737
}
3838

39-
impl<'var, 'lua> LuaTable<'var, 'lua> {
39+
impl<'var, 'lua, L: HasLua<'lua>> LuaTable<'var, L> {
4040
pub fn iter<'me>(&'me mut self)
41-
-> LuaTableIterator<'var, 'lua, 'me>
41+
-> LuaTableIterator<'var, 'me, L>
4242
{
43-
unsafe { ffi::lua_pushnil(self.variable.lua.lua) };
43+
unsafe { ffi::lua_pushnil(self.variable.use_lua()) };
4444
LuaTableIterator { table: self }
4545
}
4646

47-
pub fn get<R: CopyRead<LuaTable<'var, 'lua>>, I: Index<Lua<'lua>>>(&mut self, index: I) -> Option<R> {
48-
index.push_to_lua(self.variable.lua);
49-
unsafe { ffi::lua_gettable(self.variable.lua.lua, -2); }
47+
pub fn get<R: CopyRead<LuaTable<'var, L>>, I: Index<LuaTable<'var, L>>>(&mut self, index: I) -> Option<R> {
48+
index.push_to_lua(self);
49+
unsafe { ffi::lua_gettable(self.use_lua(), -2); }
5050
let value = CopyRead::read_from_lua(self, -1);
51-
unsafe { ffi::lua_pop(self.variable.lua.lua, 1); }
51+
unsafe { ffi::lua_pop(self.use_lua(), 1); }
5252
value
5353
}
5454

55-
pub fn set<I: Index<Lua<'lua>>, V: Push<Lua<'lua>>>(&mut self, index: I, value: V) {
56-
index.push_to_lua(self.variable.lua);
57-
value.push_to_lua(self.variable.lua);
58-
unsafe { ffi::lua_settable(self.variable.lua.lua, -3); }
55+
pub fn set<I: Index<LuaTable<'var, L>>, V: Push<LuaTable<'var, L>>>(&mut self, index: I, value: V) {
56+
index.push_to_lua(self);
57+
value.push_to_lua(self);
58+
unsafe { ffi::lua_settable(self.use_lua(), -3); }
5959
}
6060

6161
// Obtains or create the metatable of the table
62-
pub fn get_or_create_metatable(mut self) -> LuaTable<'var, 'lua> {
63-
let result = unsafe { ffi::lua_getmetatable(self.variable.lua.lua, -1) };
62+
pub fn get_or_create_metatable(mut self) -> LuaTable<'var, L> {
63+
let result = unsafe { ffi::lua_getmetatable(self.variable.use_lua(), -1) };
6464

6565
if result == 0 {
6666
unsafe {
67-
ffi::lua_newtable(self.variable.lua.lua);
68-
ffi::lua_setmetatable(self.variable.lua.lua, -2);
69-
let r = ffi::lua_getmetatable(self.variable.lua.lua, -1);
67+
ffi::lua_newtable(self.variable.use_lua());
68+
ffi::lua_setmetatable(self.variable.use_lua(), -2);
69+
let r = ffi::lua_getmetatable(self.variable.use_lua(), -1);
7070
assert!(r != 0);
7171
}
7272
}
@@ -77,22 +77,22 @@ impl<'var, 'lua> LuaTable<'var, 'lua> {
7777
}
7878
}
7979

80-
impl<'a, 'b, 'c, K: CopyRead<LuaTableIterator<'a, 'b, 'c>>, V: CopyRead<LuaTableIterator<'a, 'b, 'c>>>
81-
Iterator<Option<(K, V)>> for LuaTableIterator<'a, 'b, 'c>
80+
impl<'a, 'b, 'lua, L: HasLua<'lua>, K: CopyRead<LuaTableIterator<'a, 'b, L>>, V: CopyRead<LuaTableIterator<'a, 'b, L>>>
81+
Iterator<Option<(K, V)>> for LuaTableIterator<'a, 'b, L>
8282
{
8383
fn next(&mut self)
8484
-> Option<Option<(K,V)>>
8585
{
8686
// this call pushes the next key and value on the stack
87-
if unsafe { ffi::lua_next(self.table.variable.lua.lua, -2) } == 0 {
87+
if unsafe { ffi::lua_next(self.table.use_lua(), -2) } == 0 {
8888
return None
8989
}
9090

9191
let key = CopyRead::read_from_lua(self, -2);
9292
let value = CopyRead::read_from_lua(self, -1);
9393

9494
// removing the value, leaving only the key on the top of the stack
95-
unsafe { ffi::lua_pop(self.table.variable.lua.lua, 1) };
95+
unsafe { ffi::lua_pop(self.table.use_lua(), 1) };
9696

9797
//
9898
if key.is_none() || value.is_none() {

rust-hl-lua/src/userdata.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use ffi;
22
use Lua;
3+
use HasLua;
34
use CopyRead;
45
use ConsumeRead;
56
use Push;
@@ -16,13 +17,15 @@ use std::any::Any;
1617
/// * metatable: Function that fills the metatable of the object.
1718
// TODO: the type must be Send because the Lua context is Send, but this conflicts with &str
1819
#[experimental]
19-
pub fn push_userdata<T: ::std::any::Any>(data: T, lua: &mut Lua, metatable: |&mut LuaTable|) -> uint {
20+
pub fn push_userdata<'a, 'lua, T: ::std::any::Any>(data: T, lua: &'a mut Lua<'lua>, metatable: |&mut LuaTable<'a, Lua<'lua>>|) -> uint {
2021
let typeid = format!("{}", data.get_type_id());
2122

2223
let luaDataRaw = unsafe { ffi::lua_newuserdata(lua.lua, ::std::mem::size_of_val(&data) as ::libc::size_t) };
2324
let luaData: *mut T = unsafe { ::std::mem::transmute(luaDataRaw) };
2425
unsafe { ::std::ptr::write(luaData, data) };
2526

27+
let lua_raw = lua.use_lua();
28+
2629
// creating a metatable
2730
unsafe {
2831
ffi::lua_newtable(lua.lua);
@@ -44,7 +47,7 @@ pub fn push_userdata<T: ::std::any::Any>(data: T, lua: &mut Lua, metatable: |&mu
4447
::std::mem::forget(table);
4548
}
4649

47-
ffi::lua_setmetatable(lua.lua, -2);
50+
ffi::lua_setmetatable(lua_raw, -2);
4851
}
4952

5053
1

0 commit comments

Comments
 (0)