Struct State

Source
pub struct State { /* private fields */ }
Expand description

An idiomatic, Rust wrapper around lua_State.

Function names adhere to Rust naming conventions. Most of the time, this means breaking up long C function names using underscores; however, there are some cases where different names are used. Typically, these are cases where the name itself is a reserved Rust keyword (such as ref in luaL_ref or where in luaL_where) or where the name is used in both the base Lua library and the auxiliary Lua library (such as lua_getmetatable and luaL_getmetatable). More descriptive names have been chosen for these functions. Finally, any reference to C functions has been replaced by the term native functions. lua_iscfunction is is_native_fn and lua_tocfunction is to_native_fn.

Implementations§

Source§

impl State

Source

pub fn new() -> State

Initializes a new Lua state. This function does not open any libraries by default. Calls lua_newstate internally.

Examples found in repository?
examples/userdata/main.rs (line 81)
80fn main() {
81  let mut state = lua::State::new();
82
83  state.open_libs();
84
85  // make a Point2D table globally available to the lua state and register
86  // our functions there:
87  state.new_table();
88  state.set_fns(&POINT2D_LIB, 0);
89  // copy reference to Point2D table so we can keep the original reference on
90  // the stack for later
91  state.push_value(-1);
92  state.set_global("Point2D");
93
94  // create a metatable for Point2D in the lua registry that refers to the
95  // global Point2D table:
96  state.new_metatable("Point2D");
97  // copy reference to Point2D table
98  state.push_value(-2);
99  // Point2Dmetatable.__index = Point2D
100  state.set_field(-2, "__index");
101
102  // pop metatable and Point2D table from the stack
103  state.pop(2);
104
105  // try it out:
106  state.do_string("local p = Point2D.new(12, 34)
107                   print(p:x(), p:y())");
108}
More examples
Hide additional examples
examples/userdata-with-drop/main.rs (line 106)
105fn main() {
106    let mut state = lua::State::new();
107
108    state.open_libs();
109
110    // make a VecWrapper table globally available to the lua state and register
111    // our functions there:
112    state.new_table();
113    state.set_fns(&VECWRAPPER_LIB, 0);
114    // copy reference to VecWrapper table so we can keep the original reference
115    // on the stack for later
116    state.push_value(-1);
117    state.set_global("VecWrapper");
118
119    // create a metatable for VecWrapper in the lua registry that refers to the
120    // global VecWrapper table:
121    state.new_metatable("VecWrapper");
122    // copy reference to VecWrapper table
123    state.push_value(-2);
124    // VecWrappermetatable.__index = VecWrapper
125    state.set_field(-2, "__index");
126    // VecWrappermetatable.__gc = lua_gc
127    state.push_fn(Some(VecWrapper::lua_gc));
128    state.set_field(-2, "__gc");
129
130    // pop metatable and VecWrapper table from the stack
131    state.pop(2);
132
133    // try it out:
134    state.do_string("local v = VecWrapper.new()
135                     v:push(12)
136                     v:push(34)
137                     -- should print 2
138                     print('length of vec is', v:len())
139                     -- should print 12 34 nil
140                     print(v:get(0), v:get(1), v:get(2))");
141}
Source

pub unsafe fn from_ptr(L: *mut lua_State) -> State

Constructs a wrapper State from a raw pointer. This is suitable for use inside of native functions that accept a lua_State to obtain a wrapper.

Examples found in repository?
examples/userdata-with-drop/main.rs (line 44)
43  unsafe extern "C" fn lua_new(L: *mut lua_State) -> c_int {
44    let mut state = State::from_ptr(L);
45    // construct new userdata in lua space and initialize it
46    let v: *mut VecWrapper = state.new_userdata_typed();
47    std::ptr::write(v, VecWrapper::new());
48    // set the userdata's metatable so we can call methods on it
49    state.set_metatable_from_registry("VecWrapper");
50    // return the userdata on top of the stack
51    1
52  }
53
54  /// Returns the value in the underlying Vec at index `i`. If the index is out
55  /// of bounds, this function returns nil instead.
56  #[allow(non_snake_case)]
57  unsafe extern "C" fn lua_get(L: *mut lua_State) -> c_int {
58    let mut state = State::from_ptr(L);
59    let v = state.check_userdata(1, "VecWrapper") as *mut VecWrapper;
60    let i = state.check_integer(2) as usize;
61    // push integer if index is not out of bounds, otherwise nil
62    match (*v).data.get(i) {
63      Some(value) => state.push_integer(*value),
64      None        => state.push_nil()
65    };
66    1
67  }
68
69  /// Pushes a value into the underlying Vec.
70  #[allow(non_snake_case)]
71  unsafe extern "C" fn lua_push(L: *mut lua_State) -> c_int {
72    let mut state = State::from_ptr(L);
73    let v = state.check_userdata(1, "VecWrapper") as *mut VecWrapper;
74    let i = state.check_integer(2);
75    (*v).data.push(i);
76    1
77  }
78
79  /// Returns the length of the underlying Vec.
80  #[allow(non_snake_case)]
81  unsafe extern "C" fn lua_len(L: *mut lua_State) -> c_int {
82    let mut state = State::from_ptr(L);
83    let v = state.check_userdata(1, "VecWrapper") as *mut VecWrapper;
84    state.push_integer((*v).data.len() as i64);
85    1
86  }
87
88  /// Garbage collects a VecWrapper.
89  #[allow(non_snake_case)]
90  unsafe extern "C" fn lua_gc(L: *mut lua_State) -> c_int {
91    let mut state = State::from_ptr(L);
92    let v = state.check_userdata(1, "VecWrapper") as *mut VecWrapper;
93    std::ptr::drop_in_place(v);
94    0
95  }
More examples
Hide additional examples
examples/userdata/main.rs (line 45)
44  unsafe extern "C" fn lua_new(L: *mut lua_State) -> c_int {
45    let mut state = State::from_ptr(L);
46    // takes two optional integer parameters
47    let x = state.opt_integer(1, 0);
48    let y = state.opt_integer(2, 0);
49    // construct new userdata in lua space and initialize it
50    *state.new_userdata_typed::<Point2D>() = Point2D::new(x, y);
51    // set the userdata's metatable so we can call methods on it
52    state.set_metatable_from_registry("Point2D");
53    // return the userdata on top of the stack
54    1
55  }
56
57  #[allow(non_snake_case)]
58  unsafe extern "C" fn lua_x(L: *mut lua_State) -> c_int {
59    let mut state = State::from_ptr(L);
60    let point = state.check_userdata(1, "Point2D") as *mut Point2D;
61    state.push_integer((*point).x);
62    1
63  }
64
65  #[allow(non_snake_case)]
66  unsafe extern "C" fn lua_y(L: *mut lua_State) -> c_int {
67    let mut state = State::from_ptr(L);
68    let point = state.check_userdata(1, "Point2D") as *mut Point2D;
69    state.push_integer((*point).y);
70    1
71  }
Source

pub fn as_ptr(&self) -> *mut lua_State

Returns an unsafe pointer to the wrapped lua_State.

Source

pub fn open_libs(&mut self)

Maps to luaL_openlibs.

Examples found in repository?
examples/userdata/main.rs (line 83)
80fn main() {
81  let mut state = lua::State::new();
82
83  state.open_libs();
84
85  // make a Point2D table globally available to the lua state and register
86  // our functions there:
87  state.new_table();
88  state.set_fns(&POINT2D_LIB, 0);
89  // copy reference to Point2D table so we can keep the original reference on
90  // the stack for later
91  state.push_value(-1);
92  state.set_global("Point2D");
93
94  // create a metatable for Point2D in the lua registry that refers to the
95  // global Point2D table:
96  state.new_metatable("Point2D");
97  // copy reference to Point2D table
98  state.push_value(-2);
99  // Point2Dmetatable.__index = Point2D
100  state.set_field(-2, "__index");
101
102  // pop metatable and Point2D table from the stack
103  state.pop(2);
104
105  // try it out:
106  state.do_string("local p = Point2D.new(12, 34)
107                   print(p:x(), p:y())");
108}
More examples
Hide additional examples
examples/userdata-with-drop/main.rs (line 108)
105fn main() {
106    let mut state = lua::State::new();
107
108    state.open_libs();
109
110    // make a VecWrapper table globally available to the lua state and register
111    // our functions there:
112    state.new_table();
113    state.set_fns(&VECWRAPPER_LIB, 0);
114    // copy reference to VecWrapper table so we can keep the original reference
115    // on the stack for later
116    state.push_value(-1);
117    state.set_global("VecWrapper");
118
119    // create a metatable for VecWrapper in the lua registry that refers to the
120    // global VecWrapper table:
121    state.new_metatable("VecWrapper");
122    // copy reference to VecWrapper table
123    state.push_value(-2);
124    // VecWrappermetatable.__index = VecWrapper
125    state.set_field(-2, "__index");
126    // VecWrappermetatable.__gc = lua_gc
127    state.push_fn(Some(VecWrapper::lua_gc));
128    state.set_field(-2, "__gc");
129
130    // pop metatable and VecWrapper table from the stack
131    state.pop(2);
132
133    // try it out:
134    state.do_string("local v = VecWrapper.new()
135                     v:push(12)
136                     v:push(34)
137                     -- should print 2
138                     print('length of vec is', v:len())
139                     -- should print 12 34 nil
140                     print(v:get(0), v:get(1), v:get(2))");
141}
Source

pub fn preload_library(&mut self, lib: Library)

Preloads library, i.e. it’s not exposed, but can be required

Source

pub fn load_library(&mut self, lib: Library)

Loads a built-in library and exposes it into lua code

Source

pub fn open_base(&mut self) -> c_int

Maps to luaopen_base.

Source

pub fn open_coroutine(&mut self) -> c_int

Maps to luaopen_coroutine.

Source

pub fn open_table(&mut self) -> c_int

Maps to luaopen_table.

Source

pub fn open_io(&mut self) -> c_int

Maps to luaopen_io.

Source

pub fn open_os(&mut self) -> c_int

Maps to luaopen_os.

Source

pub fn open_string(&mut self) -> c_int

Maps to luaopen_string.

Source

pub fn open_utf8(&mut self) -> c_int

Maps to luaopen_utf8.

Source

pub fn open_bit32(&mut self) -> c_int

Maps to luaopen_bit32.

Source

pub fn open_math(&mut self) -> c_int

Maps to luaopen_math.

Source

pub fn open_debug(&mut self) -> c_int

Maps to luaopen_debug.

Source

pub fn open_package(&mut self) -> c_int

Maps to luaopen_package.

Source

pub fn do_file(&mut self, filename: &str) -> ThreadStatus

Maps to luaL_dofile.

Source

pub fn do_string(&mut self, s: &str) -> ThreadStatus

Maps to luaL_dostring.

Examples found in repository?
examples/userdata/main.rs (lines 106-107)
80fn main() {
81  let mut state = lua::State::new();
82
83  state.open_libs();
84
85  // make a Point2D table globally available to the lua state and register
86  // our functions there:
87  state.new_table();
88  state.set_fns(&POINT2D_LIB, 0);
89  // copy reference to Point2D table so we can keep the original reference on
90  // the stack for later
91  state.push_value(-1);
92  state.set_global("Point2D");
93
94  // create a metatable for Point2D in the lua registry that refers to the
95  // global Point2D table:
96  state.new_metatable("Point2D");
97  // copy reference to Point2D table
98  state.push_value(-2);
99  // Point2Dmetatable.__index = Point2D
100  state.set_field(-2, "__index");
101
102  // pop metatable and Point2D table from the stack
103  state.pop(2);
104
105  // try it out:
106  state.do_string("local p = Point2D.new(12, 34)
107                   print(p:x(), p:y())");
108}
More examples
Hide additional examples
examples/userdata-with-drop/main.rs (lines 134-140)
105fn main() {
106    let mut state = lua::State::new();
107
108    state.open_libs();
109
110    // make a VecWrapper table globally available to the lua state and register
111    // our functions there:
112    state.new_table();
113    state.set_fns(&VECWRAPPER_LIB, 0);
114    // copy reference to VecWrapper table so we can keep the original reference
115    // on the stack for later
116    state.push_value(-1);
117    state.set_global("VecWrapper");
118
119    // create a metatable for VecWrapper in the lua registry that refers to the
120    // global VecWrapper table:
121    state.new_metatable("VecWrapper");
122    // copy reference to VecWrapper table
123    state.push_value(-2);
124    // VecWrappermetatable.__index = VecWrapper
125    state.set_field(-2, "__index");
126    // VecWrappermetatable.__gc = lua_gc
127    state.push_fn(Some(VecWrapper::lua_gc));
128    state.set_field(-2, "__gc");
129
130    // pop metatable and VecWrapper table from the stack
131    state.pop(2);
132
133    // try it out:
134    state.do_string("local v = VecWrapper.new()
135                     v:push(12)
136                     v:push(34)
137                     -- should print 2
138                     print('length of vec is', v:len())
139                     -- should print 12 34 nil
140                     print(v:get(0), v:get(1), v:get(2))");
141}
Source

pub fn push<T: ToLua>(&mut self, value: T)

Pushes the given value onto the stack.

Source

pub fn to_type<T: FromLua>(&mut self, index: Index) -> Option<T>

Converts the value on top of the stack to a value of type T and returns it.

Source

pub fn close(self)

Maps to lua_close.

Source

pub fn new_thread(&mut self) -> State

Maps to lua_newthread.

Source

pub fn at_panic(&mut self, panicf: Function) -> Function

Maps to lua_atpanic.

Source

pub fn version(state: Option<&mut State>) -> Number

Maps to lua_version.

Source

pub fn abs_index(&mut self, idx: Index) -> Index

Maps to lua_absindex.

Source

pub fn get_top(&mut self) -> Index

Maps to lua_gettop.

Source

pub fn set_top(&mut self, index: Index)

Maps to lua_settop.

Source

pub fn push_value(&mut self, index: Index)

Maps to lua_pushvalue.

Examples found in repository?
examples/userdata/main.rs (line 91)
80fn main() {
81  let mut state = lua::State::new();
82
83  state.open_libs();
84
85  // make a Point2D table globally available to the lua state and register
86  // our functions there:
87  state.new_table();
88  state.set_fns(&POINT2D_LIB, 0);
89  // copy reference to Point2D table so we can keep the original reference on
90  // the stack for later
91  state.push_value(-1);
92  state.set_global("Point2D");
93
94  // create a metatable for Point2D in the lua registry that refers to the
95  // global Point2D table:
96  state.new_metatable("Point2D");
97  // copy reference to Point2D table
98  state.push_value(-2);
99  // Point2Dmetatable.__index = Point2D
100  state.set_field(-2, "__index");
101
102  // pop metatable and Point2D table from the stack
103  state.pop(2);
104
105  // try it out:
106  state.do_string("local p = Point2D.new(12, 34)
107                   print(p:x(), p:y())");
108}
More examples
Hide additional examples
examples/userdata-with-drop/main.rs (line 116)
105fn main() {
106    let mut state = lua::State::new();
107
108    state.open_libs();
109
110    // make a VecWrapper table globally available to the lua state and register
111    // our functions there:
112    state.new_table();
113    state.set_fns(&VECWRAPPER_LIB, 0);
114    // copy reference to VecWrapper table so we can keep the original reference
115    // on the stack for later
116    state.push_value(-1);
117    state.set_global("VecWrapper");
118
119    // create a metatable for VecWrapper in the lua registry that refers to the
120    // global VecWrapper table:
121    state.new_metatable("VecWrapper");
122    // copy reference to VecWrapper table
123    state.push_value(-2);
124    // VecWrappermetatable.__index = VecWrapper
125    state.set_field(-2, "__index");
126    // VecWrappermetatable.__gc = lua_gc
127    state.push_fn(Some(VecWrapper::lua_gc));
128    state.set_field(-2, "__gc");
129
130    // pop metatable and VecWrapper table from the stack
131    state.pop(2);
132
133    // try it out:
134    state.do_string("local v = VecWrapper.new()
135                     v:push(12)
136                     v:push(34)
137                     -- should print 2
138                     print('length of vec is', v:len())
139                     -- should print 12 34 nil
140                     print(v:get(0), v:get(1), v:get(2))");
141}
Source

pub fn rotate(&mut self, idx: Index, n: c_int)

Maps to lua_rotate.

Source

pub fn copy(&mut self, from_idx: Index, to_idx: Index)

Maps to lua_copy.

Source

pub fn check_stack(&mut self, extra: c_int) -> bool

Maps to lua_checkstack.

Source

pub fn xmove(&mut self, to: &mut State, n: c_int)

Maps to lua_xmove.

Source

pub fn is_number(&mut self, index: Index) -> bool

Maps to lua_isnumber.

Source

pub fn is_string(&mut self, index: Index) -> bool

Maps to lua_isstring.

Source

pub fn is_native_fn(&mut self, index: Index) -> bool

Maps to lua_iscfunction.

Source

pub fn is_integer(&mut self, index: Index) -> bool

Maps to lua_isinteger.

Source

pub fn is_userdata(&mut self, index: Index) -> bool

Maps to lua_isuserdata.

Source

pub fn type_of(&mut self, index: Index) -> Option<Type>

Maps to lua_type.

Source

pub fn typename_of(&mut self, tp: Type) -> &'static str

Maps to lua_typename.

Source

pub fn to_numberx(&mut self, index: Index) -> Option<Number>

Maps to lua_tonumberx.

Source

pub fn to_integerx(&mut self, index: Index) -> Option<Integer>

Maps to lua_tointegerx.

Source

pub fn to_bool(&mut self, index: Index) -> bool

Maps to lua_toboolean.

Source

pub fn raw_len(&mut self, index: Index) -> size_t

Maps to lua_rawlen.

Source

pub fn to_native_fn(&mut self, index: Index) -> Function

Maps to lua_tocfunction.

Source

pub fn to_userdata(&mut self, index: Index) -> *mut c_void

Maps to lua_touserdata.

Source

pub unsafe fn to_userdata_typed<'a, T>( &'a mut self, index: Index, ) -> Option<&'a mut T>

Convenience function that calls to_userdata and performs a cast.

Source

pub fn to_thread(&mut self, index: Index) -> Option<State>

Maps to lua_tothread.

Source

pub fn to_pointer(&mut self, index: Index) -> *const c_void

Maps to lua_topointer.

Source

pub fn arith(&mut self, op: Arithmetic)

Maps to lua_arith.

Source

pub fn raw_equal(&mut self, idx1: Index, idx2: Index) -> bool

Maps to lua_rawequal.

Source

pub fn compare(&mut self, idx1: Index, idx2: Index, op: Comparison) -> bool

Maps to lua_compare.

Source

pub fn push_nil(&mut self)

Maps to lua_pushnil.

Examples found in repository?
examples/userdata-with-drop/main.rs (line 64)
57  unsafe extern "C" fn lua_get(L: *mut lua_State) -> c_int {
58    let mut state = State::from_ptr(L);
59    let v = state.check_userdata(1, "VecWrapper") as *mut VecWrapper;
60    let i = state.check_integer(2) as usize;
61    // push integer if index is not out of bounds, otherwise nil
62    match (*v).data.get(i) {
63      Some(value) => state.push_integer(*value),
64      None        => state.push_nil()
65    };
66    1
67  }
Source

pub fn push_number(&mut self, n: Number)

Maps to lua_pushnumber.

Source

pub fn push_integer(&mut self, i: Integer)

Maps to lua_pushinteger.

Examples found in repository?
examples/userdata/main.rs (line 61)
58  unsafe extern "C" fn lua_x(L: *mut lua_State) -> c_int {
59    let mut state = State::from_ptr(L);
60    let point = state.check_userdata(1, "Point2D") as *mut Point2D;
61    state.push_integer((*point).x);
62    1
63  }
64
65  #[allow(non_snake_case)]
66  unsafe extern "C" fn lua_y(L: *mut lua_State) -> c_int {
67    let mut state = State::from_ptr(L);
68    let point = state.check_userdata(1, "Point2D") as *mut Point2D;
69    state.push_integer((*point).y);
70    1
71  }
More examples
Hide additional examples
examples/userdata-with-drop/main.rs (line 63)
57  unsafe extern "C" fn lua_get(L: *mut lua_State) -> c_int {
58    let mut state = State::from_ptr(L);
59    let v = state.check_userdata(1, "VecWrapper") as *mut VecWrapper;
60    let i = state.check_integer(2) as usize;
61    // push integer if index is not out of bounds, otherwise nil
62    match (*v).data.get(i) {
63      Some(value) => state.push_integer(*value),
64      None        => state.push_nil()
65    };
66    1
67  }
68
69  /// Pushes a value into the underlying Vec.
70  #[allow(non_snake_case)]
71  unsafe extern "C" fn lua_push(L: *mut lua_State) -> c_int {
72    let mut state = State::from_ptr(L);
73    let v = state.check_userdata(1, "VecWrapper") as *mut VecWrapper;
74    let i = state.check_integer(2);
75    (*v).data.push(i);
76    1
77  }
78
79  /// Returns the length of the underlying Vec.
80  #[allow(non_snake_case)]
81  unsafe extern "C" fn lua_len(L: *mut lua_State) -> c_int {
82    let mut state = State::from_ptr(L);
83    let v = state.check_userdata(1, "VecWrapper") as *mut VecWrapper;
84    state.push_integer((*v).data.len() as i64);
85    1
86  }
Source

pub fn push_string(&mut self, s: &str)

Maps to lua_pushlstring.

Source

pub fn push_closure(&mut self, f: Function, n: c_int)

Maps to lua_pushcclosure.

Source

pub fn push_bool(&mut self, b: bool)

Maps to lua_pushboolean.

Source

pub unsafe fn push_light_userdata<T>(&mut self, ud: *mut T)

Maps to lua_pushlightuserdata. The Lua state will receive a pointer to the given value. The caller is responsible for cleaning up the data. Any code that manipulates the userdata is free to modify its contents, so memory safety is not guaranteed.

Source

pub fn push_thread(&mut self) -> bool

Maps to lua_pushthread.

Source

pub fn get_global(&mut self, name: &str) -> Type

Maps to lua_getglobal.

Source

pub fn get_table(&mut self, index: Index) -> Type

Maps to lua_gettable.

Source

pub fn get_field(&mut self, index: Index, k: &str) -> Type

Maps to lua_getfield.

Source

pub fn geti(&mut self, index: Index, i: Integer) -> Type

Maps to lua_geti.

Source

pub fn raw_get(&mut self, index: Index) -> Type

Maps to lua_rawget.

Source

pub fn raw_geti(&mut self, index: Index, n: Integer) -> Type

Maps to lua_rawgeti.

Source

pub fn raw_getp<T>(&mut self, index: Index, p: *const T) -> Type

Maps to lua_rawgetp.

Source

pub fn create_table(&mut self, narr: c_int, nrec: c_int)

Maps to lua_createtable.

Source

pub fn new_userdata(&mut self, sz: size_t) -> *mut c_void

Maps to lua_newuserdata. The pointer returned is owned by the Lua state and it will be garbage collected when it is no longer in use or the state is closed. To specify custom cleanup behavior, use a __gc metamethod.

Source

pub fn new_userdata_typed<T>(&mut self) -> *mut T

Convenience function that uses type information to call new_userdata and perform a cast.

§Example
unsafe { *state.new_userdata_typed() = MyStruct::new(...); }
state.set_metatable_from_registry("MyStruct");
Examples found in repository?
examples/userdata-with-drop/main.rs (line 46)
43  unsafe extern "C" fn lua_new(L: *mut lua_State) -> c_int {
44    let mut state = State::from_ptr(L);
45    // construct new userdata in lua space and initialize it
46    let v: *mut VecWrapper = state.new_userdata_typed();
47    std::ptr::write(v, VecWrapper::new());
48    // set the userdata's metatable so we can call methods on it
49    state.set_metatable_from_registry("VecWrapper");
50    // return the userdata on top of the stack
51    1
52  }
More examples
Hide additional examples
examples/userdata/main.rs (line 50)
44  unsafe extern "C" fn lua_new(L: *mut lua_State) -> c_int {
45    let mut state = State::from_ptr(L);
46    // takes two optional integer parameters
47    let x = state.opt_integer(1, 0);
48    let y = state.opt_integer(2, 0);
49    // construct new userdata in lua space and initialize it
50    *state.new_userdata_typed::<Point2D>() = Point2D::new(x, y);
51    // set the userdata's metatable so we can call methods on it
52    state.set_metatable_from_registry("Point2D");
53    // return the userdata on top of the stack
54    1
55  }
Source

pub fn get_metatable(&mut self, objindex: Index) -> bool

Maps to lua_getmetatable.

Source

pub fn get_uservalue(&mut self, idx: Index) -> Type

Maps to lua_getuservalue.

Source

pub fn set_global(&mut self, var: &str)

Maps to lua_setglobal.

Examples found in repository?
examples/userdata/main.rs (line 92)
80fn main() {
81  let mut state = lua::State::new();
82
83  state.open_libs();
84
85  // make a Point2D table globally available to the lua state and register
86  // our functions there:
87  state.new_table();
88  state.set_fns(&POINT2D_LIB, 0);
89  // copy reference to Point2D table so we can keep the original reference on
90  // the stack for later
91  state.push_value(-1);
92  state.set_global("Point2D");
93
94  // create a metatable for Point2D in the lua registry that refers to the
95  // global Point2D table:
96  state.new_metatable("Point2D");
97  // copy reference to Point2D table
98  state.push_value(-2);
99  // Point2Dmetatable.__index = Point2D
100  state.set_field(-2, "__index");
101
102  // pop metatable and Point2D table from the stack
103  state.pop(2);
104
105  // try it out:
106  state.do_string("local p = Point2D.new(12, 34)
107                   print(p:x(), p:y())");
108}
More examples
Hide additional examples
examples/userdata-with-drop/main.rs (line 117)
105fn main() {
106    let mut state = lua::State::new();
107
108    state.open_libs();
109
110    // make a VecWrapper table globally available to the lua state and register
111    // our functions there:
112    state.new_table();
113    state.set_fns(&VECWRAPPER_LIB, 0);
114    // copy reference to VecWrapper table so we can keep the original reference
115    // on the stack for later
116    state.push_value(-1);
117    state.set_global("VecWrapper");
118
119    // create a metatable for VecWrapper in the lua registry that refers to the
120    // global VecWrapper table:
121    state.new_metatable("VecWrapper");
122    // copy reference to VecWrapper table
123    state.push_value(-2);
124    // VecWrappermetatable.__index = VecWrapper
125    state.set_field(-2, "__index");
126    // VecWrappermetatable.__gc = lua_gc
127    state.push_fn(Some(VecWrapper::lua_gc));
128    state.set_field(-2, "__gc");
129
130    // pop metatable and VecWrapper table from the stack
131    state.pop(2);
132
133    // try it out:
134    state.do_string("local v = VecWrapper.new()
135                     v:push(12)
136                     v:push(34)
137                     -- should print 2
138                     print('length of vec is', v:len())
139                     -- should print 12 34 nil
140                     print(v:get(0), v:get(1), v:get(2))");
141}
Source

pub fn set_table(&mut self, idx: Index)

Maps to lua_settable.

Source

pub fn set_field(&mut self, idx: Index, k: &str)

Maps to lua_setfield.

Examples found in repository?
examples/userdata/main.rs (line 100)
80fn main() {
81  let mut state = lua::State::new();
82
83  state.open_libs();
84
85  // make a Point2D table globally available to the lua state and register
86  // our functions there:
87  state.new_table();
88  state.set_fns(&POINT2D_LIB, 0);
89  // copy reference to Point2D table so we can keep the original reference on
90  // the stack for later
91  state.push_value(-1);
92  state.set_global("Point2D");
93
94  // create a metatable for Point2D in the lua registry that refers to the
95  // global Point2D table:
96  state.new_metatable("Point2D");
97  // copy reference to Point2D table
98  state.push_value(-2);
99  // Point2Dmetatable.__index = Point2D
100  state.set_field(-2, "__index");
101
102  // pop metatable and Point2D table from the stack
103  state.pop(2);
104
105  // try it out:
106  state.do_string("local p = Point2D.new(12, 34)
107                   print(p:x(), p:y())");
108}
More examples
Hide additional examples
examples/userdata-with-drop/main.rs (line 125)
105fn main() {
106    let mut state = lua::State::new();
107
108    state.open_libs();
109
110    // make a VecWrapper table globally available to the lua state and register
111    // our functions there:
112    state.new_table();
113    state.set_fns(&VECWRAPPER_LIB, 0);
114    // copy reference to VecWrapper table so we can keep the original reference
115    // on the stack for later
116    state.push_value(-1);
117    state.set_global("VecWrapper");
118
119    // create a metatable for VecWrapper in the lua registry that refers to the
120    // global VecWrapper table:
121    state.new_metatable("VecWrapper");
122    // copy reference to VecWrapper table
123    state.push_value(-2);
124    // VecWrappermetatable.__index = VecWrapper
125    state.set_field(-2, "__index");
126    // VecWrappermetatable.__gc = lua_gc
127    state.push_fn(Some(VecWrapper::lua_gc));
128    state.set_field(-2, "__gc");
129
130    // pop metatable and VecWrapper table from the stack
131    state.pop(2);
132
133    // try it out:
134    state.do_string("local v = VecWrapper.new()
135                     v:push(12)
136                     v:push(34)
137                     -- should print 2
138                     print('length of vec is', v:len())
139                     -- should print 12 34 nil
140                     print(v:get(0), v:get(1), v:get(2))");
141}
Source

pub fn seti(&mut self, idx: Index, n: Integer)

Maps to lua_seti.

Source

pub fn raw_set(&mut self, idx: Index)

Maps to lua_rawset.

Source

pub fn raw_seti(&mut self, idx: Index, n: Integer)

Maps to lua_rawseti.

Source

pub fn raw_setp<T>(&mut self, idx: Index, p: *const T)

Maps to lua_rawsetp.

Source

pub fn set_metatable(&mut self, objindex: Index)

Maps to lua_setmetatable.

Source

pub fn set_uservalue(&mut self, idx: Index)

Maps to lua_setuservalue.

Source

pub fn callk<F>(&mut self, nargs: c_int, nresults: c_int, continuation: F)
where F: FnOnce(&mut State, ThreadStatus) -> c_int,

Maps to lua_callk.

Source

pub fn call(&mut self, nargs: c_int, nresults: c_int)

Maps to lua_call.

Source

pub fn pcallk<F>( &mut self, nargs: c_int, nresults: c_int, msgh: c_int, continuation: F, ) -> c_int
where F: FnOnce(&mut State, ThreadStatus) -> c_int,

Maps to lua_pcallk.

Source

pub fn pcall( &mut self, nargs: c_int, nresults: c_int, msgh: c_int, ) -> ThreadStatus

Maps to lua_pcall.

Source

pub fn load<F>(&mut self, reader: F, source: &str, mode: &str) -> ThreadStatus
where F: FnMut(&mut State) -> &[u8],

Maps to lua_load.

Source

pub fn dump<F>(&mut self, writer: F, strip: bool) -> c_int
where F: FnMut(&mut State, &[u8]) -> c_int,

Maps to lua_dump.

Source

pub fn co_yieldk<F>(&mut self, nresults: c_int, continuation: F) -> !
where F: FnOnce(&mut State, ThreadStatus) -> c_int,

Maps to lua_yieldk.

Source

pub fn co_yield(&mut self, nresults: c_int) -> !

Maps to lua_yield. This function is not called yield because it is a reserved keyword.

Source

pub fn resume(&mut self, from: Option<&mut State>, nargs: c_int) -> ThreadStatus

Maps to lua_resume.

Source

pub fn status(&mut self) -> ThreadStatus

Maps to lua_status.

Source

pub fn is_yieldable(&mut self) -> bool

Maps to lua_isyieldable.

Source

pub fn gc(&mut self, what: GcOption, data: c_int) -> c_int

Maps to lua_gc.

Source

pub fn error(&mut self) -> !

Maps to lua_error.

Source

pub fn next(&mut self, idx: Index) -> bool

Maps to lua_next.

Source

pub fn concat(&mut self, n: c_int)

Maps to lua_concat.

Source

pub fn len(&mut self, idx: Index)

Maps to lua_len.

Source

pub fn string_to_number(&mut self, s: &str) -> size_t

Maps to lua_stringtonumber.

Source

pub fn get_alloc_fn(&mut self) -> (Allocator, *mut c_void)

Maps to lua_getallocf.

Source

pub fn set_alloc_fn(&mut self, f: Allocator, ud: *mut c_void)

Maps to lua_setallocf.

Source

pub fn set_extra(&mut self, extra: Option<Extra>) -> Option<Extra>

Set extra data. Return previous value if it was set.

Source

pub fn with_extra<F, R>(&mut self, closure: F) -> R
where F: FnOnce(&mut Option<Extra>) -> R,

Do some actions with mutable extra.

Source

pub fn with_extra_typed<T, F, R>(&mut self, closure: F) -> R
where T: Any, F: FnOnce(&mut T) -> R,

Unwrap and downcast extra to typed.

§Panics

Panics if state has no attached Extra or it’s impossible to downcast to T.

Source

pub fn to_number(&mut self, index: Index) -> Number

Maps to lua_tonumber.

Source

pub fn to_integer(&mut self, index: Index) -> Integer

Maps to lua_tointeger.

Source

pub fn pop(&mut self, n: c_int)

Maps to lua_pop.

Examples found in repository?
examples/userdata/main.rs (line 103)
80fn main() {
81  let mut state = lua::State::new();
82
83  state.open_libs();
84
85  // make a Point2D table globally available to the lua state and register
86  // our functions there:
87  state.new_table();
88  state.set_fns(&POINT2D_LIB, 0);
89  // copy reference to Point2D table so we can keep the original reference on
90  // the stack for later
91  state.push_value(-1);
92  state.set_global("Point2D");
93
94  // create a metatable for Point2D in the lua registry that refers to the
95  // global Point2D table:
96  state.new_metatable("Point2D");
97  // copy reference to Point2D table
98  state.push_value(-2);
99  // Point2Dmetatable.__index = Point2D
100  state.set_field(-2, "__index");
101
102  // pop metatable and Point2D table from the stack
103  state.pop(2);
104
105  // try it out:
106  state.do_string("local p = Point2D.new(12, 34)
107                   print(p:x(), p:y())");
108}
More examples
Hide additional examples
examples/userdata-with-drop/main.rs (line 131)
105fn main() {
106    let mut state = lua::State::new();
107
108    state.open_libs();
109
110    // make a VecWrapper table globally available to the lua state and register
111    // our functions there:
112    state.new_table();
113    state.set_fns(&VECWRAPPER_LIB, 0);
114    // copy reference to VecWrapper table so we can keep the original reference
115    // on the stack for later
116    state.push_value(-1);
117    state.set_global("VecWrapper");
118
119    // create a metatable for VecWrapper in the lua registry that refers to the
120    // global VecWrapper table:
121    state.new_metatable("VecWrapper");
122    // copy reference to VecWrapper table
123    state.push_value(-2);
124    // VecWrappermetatable.__index = VecWrapper
125    state.set_field(-2, "__index");
126    // VecWrappermetatable.__gc = lua_gc
127    state.push_fn(Some(VecWrapper::lua_gc));
128    state.set_field(-2, "__gc");
129
130    // pop metatable and VecWrapper table from the stack
131    state.pop(2);
132
133    // try it out:
134    state.do_string("local v = VecWrapper.new()
135                     v:push(12)
136                     v:push(34)
137                     -- should print 2
138                     print('length of vec is', v:len())
139                     -- should print 12 34 nil
140                     print(v:get(0), v:get(1), v:get(2))");
141}
Source

pub fn new_table(&mut self)

Maps to lua_newtable.

Examples found in repository?
examples/userdata/main.rs (line 87)
80fn main() {
81  let mut state = lua::State::new();
82
83  state.open_libs();
84
85  // make a Point2D table globally available to the lua state and register
86  // our functions there:
87  state.new_table();
88  state.set_fns(&POINT2D_LIB, 0);
89  // copy reference to Point2D table so we can keep the original reference on
90  // the stack for later
91  state.push_value(-1);
92  state.set_global("Point2D");
93
94  // create a metatable for Point2D in the lua registry that refers to the
95  // global Point2D table:
96  state.new_metatable("Point2D");
97  // copy reference to Point2D table
98  state.push_value(-2);
99  // Point2Dmetatable.__index = Point2D
100  state.set_field(-2, "__index");
101
102  // pop metatable and Point2D table from the stack
103  state.pop(2);
104
105  // try it out:
106  state.do_string("local p = Point2D.new(12, 34)
107                   print(p:x(), p:y())");
108}
More examples
Hide additional examples
examples/userdata-with-drop/main.rs (line 112)
105fn main() {
106    let mut state = lua::State::new();
107
108    state.open_libs();
109
110    // make a VecWrapper table globally available to the lua state and register
111    // our functions there:
112    state.new_table();
113    state.set_fns(&VECWRAPPER_LIB, 0);
114    // copy reference to VecWrapper table so we can keep the original reference
115    // on the stack for later
116    state.push_value(-1);
117    state.set_global("VecWrapper");
118
119    // create a metatable for VecWrapper in the lua registry that refers to the
120    // global VecWrapper table:
121    state.new_metatable("VecWrapper");
122    // copy reference to VecWrapper table
123    state.push_value(-2);
124    // VecWrappermetatable.__index = VecWrapper
125    state.set_field(-2, "__index");
126    // VecWrappermetatable.__gc = lua_gc
127    state.push_fn(Some(VecWrapper::lua_gc));
128    state.set_field(-2, "__gc");
129
130    // pop metatable and VecWrapper table from the stack
131    state.pop(2);
132
133    // try it out:
134    state.do_string("local v = VecWrapper.new()
135                     v:push(12)
136                     v:push(34)
137                     -- should print 2
138                     print('length of vec is', v:len())
139                     -- should print 12 34 nil
140                     print(v:get(0), v:get(1), v:get(2))");
141}
Source

pub fn register(&mut self, n: &str, f: Function)

Maps to lua_register.

Source

pub fn push_fn(&mut self, f: Function)

Maps to lua_pushcfunction.

Examples found in repository?
examples/userdata-with-drop/main.rs (line 127)
105fn main() {
106    let mut state = lua::State::new();
107
108    state.open_libs();
109
110    // make a VecWrapper table globally available to the lua state and register
111    // our functions there:
112    state.new_table();
113    state.set_fns(&VECWRAPPER_LIB, 0);
114    // copy reference to VecWrapper table so we can keep the original reference
115    // on the stack for later
116    state.push_value(-1);
117    state.set_global("VecWrapper");
118
119    // create a metatable for VecWrapper in the lua registry that refers to the
120    // global VecWrapper table:
121    state.new_metatable("VecWrapper");
122    // copy reference to VecWrapper table
123    state.push_value(-2);
124    // VecWrappermetatable.__index = VecWrapper
125    state.set_field(-2, "__index");
126    // VecWrappermetatable.__gc = lua_gc
127    state.push_fn(Some(VecWrapper::lua_gc));
128    state.set_field(-2, "__gc");
129
130    // pop metatable and VecWrapper table from the stack
131    state.pop(2);
132
133    // try it out:
134    state.do_string("local v = VecWrapper.new()
135                     v:push(12)
136                     v:push(34)
137                     -- should print 2
138                     print('length of vec is', v:len())
139                     -- should print 12 34 nil
140                     print(v:get(0), v:get(1), v:get(2))");
141}
Source

pub fn is_fn(&mut self, index: Index) -> bool

Maps to lua_isfunction.

Source

pub fn is_table(&mut self, index: Index) -> bool

Maps to lua_istable.

Source

pub fn is_light_userdata(&mut self, index: Index) -> bool

Maps to lua_islightuserdata.

Source

pub fn is_nil(&mut self, index: Index) -> bool

Maps to lua_isnil.

Source

pub fn is_bool(&mut self, index: Index) -> bool

Maps to lua_isboolean.

Source

pub fn is_thread(&mut self, index: Index) -> bool

Maps to lua_isthread.

Source

pub fn is_none(&mut self, index: Index) -> bool

Maps to lua_isnone.

Source

pub fn is_none_or_nil(&mut self, index: Index) -> bool

Maps to lua_isnoneornil.

Source

pub fn push_global_table(&mut self)

Maps to lua_pushglobaltable.

Source

pub fn insert(&mut self, idx: Index)

Maps to lua_insert.

Source

pub fn remove(&mut self, idx: Index)

Maps to lua_remove.

Source

pub fn replace(&mut self, idx: Index)

Maps to lua_replace.

Source

pub fn get_stack(&mut self, level: c_int) -> Option<lua_Debug>

Maps to lua_getstack.

Source

pub fn get_info(&mut self, what: &str) -> Option<lua_Debug>

Maps to lua_getinfo.

Source

pub fn get_local(&mut self, ar: &lua_Debug, n: c_int) -> Option<&str>

Maps to lua_getlocal.

Source

pub fn set_local(&mut self, ar: &lua_Debug, n: c_int) -> Option<&str>

Maps to lua_setlocal.

Source

pub fn get_upvalue(&mut self, funcindex: Index, n: c_int) -> Option<&str>

Maps to lua_getupvalue.

Source

pub fn set_upvalue(&mut self, funcindex: Index, n: c_int) -> Option<&str>

Maps to lua_setupvalue.

Source

pub fn upvalue_id(&mut self, funcindex: Index, n: c_int) -> *mut c_void

Maps to lua_upvalueid.

Source

pub fn upvalue_join(&mut self, fidx1: Index, n1: c_int, fidx2: Index, n2: c_int)

Maps to lua_upvaluejoin.

Source

pub fn set_hook(&mut self, func: Hook, mask: HookMask, count: c_int)

Maps to lua_sethook.

Source

pub fn get_hook(&mut self) -> Hook

Maps to lua_gethook.

Source

pub fn get_hook_mask(&mut self) -> HookMask

Maps to lua_gethookmask.

Source

pub fn get_hook_count(&mut self) -> c_int

Maps to lua_gethookcount.

Source

pub fn check_version(&mut self)

Maps to luaL_checkversion.

Source

pub fn get_metafield(&mut self, obj: Index, e: &str) -> bool

Maps to luaL_getmetafield.

Source

pub fn call_meta(&mut self, obj: Index, e: &str) -> bool

Maps to luaL_callmeta.

Source

pub fn to_str(&mut self, index: Index) -> Option<&str>

Maps to luaL_tolstring. This function is not called to_string because that method name is used for the ToString trait. This function returns a reference to the string at the given index, on which to_owned may be called.

Source

pub fn to_str_in_place(&mut self, index: Index) -> Option<&str>

Maps to lua_tolstring. This function is not called to_string because that method name is used for the ToString trait. This function returns a reference to the string at the given index, on which to_owned may be called.

Source

pub fn arg_error(&mut self, arg: Index, extramsg: &str) -> !

Maps to luaL_argerror.

Source

pub fn check_number(&mut self, arg: Index) -> Number

Maps to luaL_checknumber.

Source

pub fn opt_number(&mut self, arg: Index, def: Number) -> Number

Maps to luaL_optnumber.

Source

pub fn check_integer(&mut self, arg: Index) -> Integer

Maps to luaL_checkinteger.

Examples found in repository?
examples/userdata-with-drop/main.rs (line 60)
57  unsafe extern "C" fn lua_get(L: *mut lua_State) -> c_int {
58    let mut state = State::from_ptr(L);
59    let v = state.check_userdata(1, "VecWrapper") as *mut VecWrapper;
60    let i = state.check_integer(2) as usize;
61    // push integer if index is not out of bounds, otherwise nil
62    match (*v).data.get(i) {
63      Some(value) => state.push_integer(*value),
64      None        => state.push_nil()
65    };
66    1
67  }
68
69  /// Pushes a value into the underlying Vec.
70  #[allow(non_snake_case)]
71  unsafe extern "C" fn lua_push(L: *mut lua_State) -> c_int {
72    let mut state = State::from_ptr(L);
73    let v = state.check_userdata(1, "VecWrapper") as *mut VecWrapper;
74    let i = state.check_integer(2);
75    (*v).data.push(i);
76    1
77  }
Source

pub fn opt_integer(&mut self, arg: Index, def: Integer) -> Integer

Maps to luaL_optinteger.

Examples found in repository?
examples/userdata/main.rs (line 47)
44  unsafe extern "C" fn lua_new(L: *mut lua_State) -> c_int {
45    let mut state = State::from_ptr(L);
46    // takes two optional integer parameters
47    let x = state.opt_integer(1, 0);
48    let y = state.opt_integer(2, 0);
49    // construct new userdata in lua space and initialize it
50    *state.new_userdata_typed::<Point2D>() = Point2D::new(x, y);
51    // set the userdata's metatable so we can call methods on it
52    state.set_metatable_from_registry("Point2D");
53    // return the userdata on top of the stack
54    1
55  }
Source

pub fn check_stack_msg(&mut self, sz: c_int, msg: &str)

Maps to luaL_checkstack.

Source

pub fn check_type(&mut self, arg: Index, t: Type)

Maps to luaL_checktype.

Source

pub fn check_any(&mut self, arg: Index)

Maps to luaL_checkany.

Source

pub fn new_metatable(&mut self, tname: &str) -> bool

Maps to luaL_newmetatable.

Examples found in repository?
examples/userdata/main.rs (line 96)
80fn main() {
81  let mut state = lua::State::new();
82
83  state.open_libs();
84
85  // make a Point2D table globally available to the lua state and register
86  // our functions there:
87  state.new_table();
88  state.set_fns(&POINT2D_LIB, 0);
89  // copy reference to Point2D table so we can keep the original reference on
90  // the stack for later
91  state.push_value(-1);
92  state.set_global("Point2D");
93
94  // create a metatable for Point2D in the lua registry that refers to the
95  // global Point2D table:
96  state.new_metatable("Point2D");
97  // copy reference to Point2D table
98  state.push_value(-2);
99  // Point2Dmetatable.__index = Point2D
100  state.set_field(-2, "__index");
101
102  // pop metatable and Point2D table from the stack
103  state.pop(2);
104
105  // try it out:
106  state.do_string("local p = Point2D.new(12, 34)
107                   print(p:x(), p:y())");
108}
More examples
Hide additional examples
examples/userdata-with-drop/main.rs (line 121)
105fn main() {
106    let mut state = lua::State::new();
107
108    state.open_libs();
109
110    // make a VecWrapper table globally available to the lua state and register
111    // our functions there:
112    state.new_table();
113    state.set_fns(&VECWRAPPER_LIB, 0);
114    // copy reference to VecWrapper table so we can keep the original reference
115    // on the stack for later
116    state.push_value(-1);
117    state.set_global("VecWrapper");
118
119    // create a metatable for VecWrapper in the lua registry that refers to the
120    // global VecWrapper table:
121    state.new_metatable("VecWrapper");
122    // copy reference to VecWrapper table
123    state.push_value(-2);
124    // VecWrappermetatable.__index = VecWrapper
125    state.set_field(-2, "__index");
126    // VecWrappermetatable.__gc = lua_gc
127    state.push_fn(Some(VecWrapper::lua_gc));
128    state.set_field(-2, "__gc");
129
130    // pop metatable and VecWrapper table from the stack
131    state.pop(2);
132
133    // try it out:
134    state.do_string("local v = VecWrapper.new()
135                     v:push(12)
136                     v:push(34)
137                     -- should print 2
138                     print('length of vec is', v:len())
139                     -- should print 12 34 nil
140                     print(v:get(0), v:get(1), v:get(2))");
141}
Source

pub fn set_metatable_from_registry(&mut self, tname: &str)

Maps to luaL_setmetatable.

Examples found in repository?
examples/userdata-with-drop/main.rs (line 49)
43  unsafe extern "C" fn lua_new(L: *mut lua_State) -> c_int {
44    let mut state = State::from_ptr(L);
45    // construct new userdata in lua space and initialize it
46    let v: *mut VecWrapper = state.new_userdata_typed();
47    std::ptr::write(v, VecWrapper::new());
48    // set the userdata's metatable so we can call methods on it
49    state.set_metatable_from_registry("VecWrapper");
50    // return the userdata on top of the stack
51    1
52  }
More examples
Hide additional examples
examples/userdata/main.rs (line 52)
44  unsafe extern "C" fn lua_new(L: *mut lua_State) -> c_int {
45    let mut state = State::from_ptr(L);
46    // takes two optional integer parameters
47    let x = state.opt_integer(1, 0);
48    let y = state.opt_integer(2, 0);
49    // construct new userdata in lua space and initialize it
50    *state.new_userdata_typed::<Point2D>() = Point2D::new(x, y);
51    // set the userdata's metatable so we can call methods on it
52    state.set_metatable_from_registry("Point2D");
53    // return the userdata on top of the stack
54    1
55  }
Source

pub fn test_userdata(&mut self, arg: Index, tname: &str) -> *mut c_void

Maps to luaL_testudata.

Source

pub unsafe fn test_userdata_typed<'a, T>( &'a mut self, arg: Index, tname: &str, ) -> Option<&'a mut T>

Convenience function that calls test_userdata and performs a cast.

Source

pub fn check_userdata(&mut self, arg: Index, tname: &str) -> *mut c_void

Maps to luaL_checkudata.

Examples found in repository?
examples/userdata/main.rs (line 60)
58  unsafe extern "C" fn lua_x(L: *mut lua_State) -> c_int {
59    let mut state = State::from_ptr(L);
60    let point = state.check_userdata(1, "Point2D") as *mut Point2D;
61    state.push_integer((*point).x);
62    1
63  }
64
65  #[allow(non_snake_case)]
66  unsafe extern "C" fn lua_y(L: *mut lua_State) -> c_int {
67    let mut state = State::from_ptr(L);
68    let point = state.check_userdata(1, "Point2D") as *mut Point2D;
69    state.push_integer((*point).y);
70    1
71  }
More examples
Hide additional examples
examples/userdata-with-drop/main.rs (line 59)
57  unsafe extern "C" fn lua_get(L: *mut lua_State) -> c_int {
58    let mut state = State::from_ptr(L);
59    let v = state.check_userdata(1, "VecWrapper") as *mut VecWrapper;
60    let i = state.check_integer(2) as usize;
61    // push integer if index is not out of bounds, otherwise nil
62    match (*v).data.get(i) {
63      Some(value) => state.push_integer(*value),
64      None        => state.push_nil()
65    };
66    1
67  }
68
69  /// Pushes a value into the underlying Vec.
70  #[allow(non_snake_case)]
71  unsafe extern "C" fn lua_push(L: *mut lua_State) -> c_int {
72    let mut state = State::from_ptr(L);
73    let v = state.check_userdata(1, "VecWrapper") as *mut VecWrapper;
74    let i = state.check_integer(2);
75    (*v).data.push(i);
76    1
77  }
78
79  /// Returns the length of the underlying Vec.
80  #[allow(non_snake_case)]
81  unsafe extern "C" fn lua_len(L: *mut lua_State) -> c_int {
82    let mut state = State::from_ptr(L);
83    let v = state.check_userdata(1, "VecWrapper") as *mut VecWrapper;
84    state.push_integer((*v).data.len() as i64);
85    1
86  }
87
88  /// Garbage collects a VecWrapper.
89  #[allow(non_snake_case)]
90  unsafe extern "C" fn lua_gc(L: *mut lua_State) -> c_int {
91    let mut state = State::from_ptr(L);
92    let v = state.check_userdata(1, "VecWrapper") as *mut VecWrapper;
93    std::ptr::drop_in_place(v);
94    0
95  }
Source

pub unsafe fn check_userdata_typed<'a, T>( &'a mut self, arg: Index, tname: &str, ) -> &'a mut T

Convenience function that calls check_userdata and performs a cast.

Source

pub fn location(&mut self, lvl: c_int)

Maps to luaL_where. where is a reserved keyword.

Source

pub fn check_option( &mut self, arg: Index, def: Option<&str>, lst: &[&str], ) -> usize

Maps to luaL_checkoption.

Source

pub fn file_result(&mut self, stat: c_int, fname: &str) -> c_int

Maps to luaL_fileresult.

Source

pub fn exec_result(&mut self, stat: c_int) -> c_int

Maps to luaL_execresult.

Source

pub fn reference(&mut self, t: Index) -> Reference

Maps to luaL_ref.

Source

pub fn unreference(&mut self, t: Index, reference: Reference)

Maps to luaL_unref.

Source

pub fn load_filex(&mut self, filename: &str, mode: &str) -> ThreadStatus

Maps to luaL_loadfilex.

Source

pub fn load_file(&mut self, filename: &str) -> ThreadStatus

Maps to luaL_loadfile.

Source

pub fn load_bufferx( &mut self, buff: &[u8], name: &str, mode: &str, ) -> ThreadStatus

Maps to luaL_loadbufferx.

Source

pub fn load_string(&mut self, source: &str) -> ThreadStatus

Maps to luaL_loadstring.

Source

pub fn len_direct(&mut self, index: Index) -> Integer

Maps to luaL_len.

Source

pub fn gsub(&mut self, s: &str, p: &str, r: &str) -> &str

Maps to luaL_gsub.

Source

pub fn set_fns(&mut self, l: &[(&str, Function)], nup: c_int)

Maps to luaL_setfuncs.

Examples found in repository?
examples/userdata/main.rs (line 88)
80fn main() {
81  let mut state = lua::State::new();
82
83  state.open_libs();
84
85  // make a Point2D table globally available to the lua state and register
86  // our functions there:
87  state.new_table();
88  state.set_fns(&POINT2D_LIB, 0);
89  // copy reference to Point2D table so we can keep the original reference on
90  // the stack for later
91  state.push_value(-1);
92  state.set_global("Point2D");
93
94  // create a metatable for Point2D in the lua registry that refers to the
95  // global Point2D table:
96  state.new_metatable("Point2D");
97  // copy reference to Point2D table
98  state.push_value(-2);
99  // Point2Dmetatable.__index = Point2D
100  state.set_field(-2, "__index");
101
102  // pop metatable and Point2D table from the stack
103  state.pop(2);
104
105  // try it out:
106  state.do_string("local p = Point2D.new(12, 34)
107                   print(p:x(), p:y())");
108}
More examples
Hide additional examples
examples/userdata-with-drop/main.rs (line 113)
105fn main() {
106    let mut state = lua::State::new();
107
108    state.open_libs();
109
110    // make a VecWrapper table globally available to the lua state and register
111    // our functions there:
112    state.new_table();
113    state.set_fns(&VECWRAPPER_LIB, 0);
114    // copy reference to VecWrapper table so we can keep the original reference
115    // on the stack for later
116    state.push_value(-1);
117    state.set_global("VecWrapper");
118
119    // create a metatable for VecWrapper in the lua registry that refers to the
120    // global VecWrapper table:
121    state.new_metatable("VecWrapper");
122    // copy reference to VecWrapper table
123    state.push_value(-2);
124    // VecWrappermetatable.__index = VecWrapper
125    state.set_field(-2, "__index");
126    // VecWrappermetatable.__gc = lua_gc
127    state.push_fn(Some(VecWrapper::lua_gc));
128    state.set_field(-2, "__gc");
129
130    // pop metatable and VecWrapper table from the stack
131    state.pop(2);
132
133    // try it out:
134    state.do_string("local v = VecWrapper.new()
135                     v:push(12)
136                     v:push(34)
137                     -- should print 2
138                     print('length of vec is', v:len())
139                     -- should print 12 34 nil
140                     print(v:get(0), v:get(1), v:get(2))");
141}
Source

pub fn get_subtable(&mut self, idx: Index, fname: &str) -> bool

Maps to luaL_getsubtable.

Source

pub fn traceback(&mut self, state: &mut State, msg: &str, level: c_int)

Maps to luaL_traceback.

Source

pub fn requiref(&mut self, modname: &str, openf: Function, glb: bool)

Maps to luaL_requiref.

Source

pub fn new_lib_table(&mut self, l: &[(&str, Function)])

Maps to luaL_newlibtable.

Source

pub fn new_lib(&mut self, l: &[(&str, Function)])

Maps to luaL_newlib.

Source

pub fn arg_check(&mut self, cond: bool, arg: Index, extramsg: &str)

Maps to luaL_argcheck.

Source

pub fn check_string(&mut self, n: Index) -> &str

Maps to luaL_checklstring.

Source

pub fn opt_string<'a>(&'a mut self, n: Index, default: &'a str) -> &'a str

Maps to luaL_optlstring.

Source

pub fn typename_at(&mut self, n: Index) -> &'static str

Maps to luaL_typename.

Source

pub fn get_metatable_from_registry(&mut self, tname: &str)

Maps to luaL_getmetatable.

Source

pub fn load_buffer(&mut self, buff: &[u8], name: &str) -> ThreadStatus

Maps to luaL_loadbuffer.

Trait Implementations§

Source§

impl Drop for State

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for State

Auto Trait Implementations§

§

impl Freeze for State

§

impl RefUnwindSafe for State

§

impl !Sync for State

§

impl Unpin for State

§

impl UnwindSafe for State

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.