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
impl State
Sourcepub fn new() -> State
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?
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
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}
Sourcepub unsafe fn from_ptr(L: *mut lua_State) -> State
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?
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
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 }
Sourcepub fn open_libs(&mut self)
pub fn open_libs(&mut self)
Maps to luaL_openlibs
.
Examples found in repository?
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
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}
Sourcepub fn preload_library(&mut self, lib: Library)
pub fn preload_library(&mut self, lib: Library)
Preloads library, i.e. it’s not exposed, but can be required
Sourcepub fn load_library(&mut self, lib: Library)
pub fn load_library(&mut self, lib: Library)
Loads a built-in library and exposes it into lua code
Sourcepub fn open_coroutine(&mut self) -> c_int
pub fn open_coroutine(&mut self) -> c_int
Maps to luaopen_coroutine
.
Sourcepub fn open_table(&mut self) -> c_int
pub fn open_table(&mut self) -> c_int
Maps to luaopen_table
.
Sourcepub fn open_string(&mut self) -> c_int
pub fn open_string(&mut self) -> c_int
Maps to luaopen_string
.
Sourcepub fn open_bit32(&mut self) -> c_int
pub fn open_bit32(&mut self) -> c_int
Maps to luaopen_bit32
.
Sourcepub fn open_debug(&mut self) -> c_int
pub fn open_debug(&mut self) -> c_int
Maps to luaopen_debug
.
Sourcepub fn open_package(&mut self) -> c_int
pub fn open_package(&mut self) -> c_int
Maps to luaopen_package
.
Sourcepub fn do_file(&mut self, filename: &str) -> ThreadStatus
pub fn do_file(&mut self, filename: &str) -> ThreadStatus
Maps to luaL_dofile
.
Sourcepub fn do_string(&mut self, s: &str) -> ThreadStatus
pub fn do_string(&mut self, s: &str) -> ThreadStatus
Maps to luaL_dostring
.
Examples found in repository?
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
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}
Sourcepub fn to_type<T: FromLua>(&mut self, index: Index) -> Option<T>
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.
Sourcepub fn new_thread(&mut self) -> State
pub fn new_thread(&mut self) -> State
Maps to lua_newthread
.
Sourcepub fn push_value(&mut self, index: Index)
pub fn push_value(&mut self, index: Index)
Maps to lua_pushvalue
.
Examples found in repository?
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
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}
Sourcepub fn check_stack(&mut self, extra: c_int) -> bool
pub fn check_stack(&mut self, extra: c_int) -> bool
Maps to lua_checkstack
.
Sourcepub fn is_native_fn(&mut self, index: Index) -> bool
pub fn is_native_fn(&mut self, index: Index) -> bool
Maps to lua_iscfunction
.
Sourcepub fn is_integer(&mut self, index: Index) -> bool
pub fn is_integer(&mut self, index: Index) -> bool
Maps to lua_isinteger
.
Sourcepub fn is_userdata(&mut self, index: Index) -> bool
pub fn is_userdata(&mut self, index: Index) -> bool
Maps to lua_isuserdata
.
Sourcepub fn typename_of(&mut self, tp: Type) -> &'static str
pub fn typename_of(&mut self, tp: Type) -> &'static str
Maps to lua_typename
.
Sourcepub fn to_numberx(&mut self, index: Index) -> Option<Number>
pub fn to_numberx(&mut self, index: Index) -> Option<Number>
Maps to lua_tonumberx
.
Sourcepub fn to_integerx(&mut self, index: Index) -> Option<Integer>
pub fn to_integerx(&mut self, index: Index) -> Option<Integer>
Maps to lua_tointegerx
.
Sourcepub fn to_native_fn(&mut self, index: Index) -> Function
pub fn to_native_fn(&mut self, index: Index) -> Function
Maps to lua_tocfunction
.
Sourcepub fn to_userdata(&mut self, index: Index) -> *mut c_void
pub fn to_userdata(&mut self, index: Index) -> *mut c_void
Maps to lua_touserdata
.
Sourcepub unsafe fn to_userdata_typed<'a, T>(
&'a mut self,
index: Index,
) -> Option<&'a mut T>
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.
Sourcepub fn to_pointer(&mut self, index: Index) -> *const c_void
pub fn to_pointer(&mut self, index: Index) -> *const c_void
Maps to lua_topointer
.
Sourcepub fn arith(&mut self, op: Arithmetic)
pub fn arith(&mut self, op: Arithmetic)
Maps to lua_arith
.
Sourcepub fn compare(&mut self, idx1: Index, idx2: Index, op: Comparison) -> bool
pub fn compare(&mut self, idx1: Index, idx2: Index, op: Comparison) -> bool
Maps to lua_compare
.
Sourcepub fn push_number(&mut self, n: Number)
pub fn push_number(&mut self, n: Number)
Maps to lua_pushnumber
.
Sourcepub fn push_integer(&mut self, i: Integer)
pub fn push_integer(&mut self, i: Integer)
Maps to lua_pushinteger
.
Examples found in repository?
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
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 }
Sourcepub fn push_string(&mut self, s: &str)
pub fn push_string(&mut self, s: &str)
Maps to lua_pushlstring
.
Sourcepub fn push_closure(&mut self, f: Function, n: c_int)
pub fn push_closure(&mut self, f: Function, n: c_int)
Maps to lua_pushcclosure
.
Sourcepub unsafe fn push_light_userdata<T>(&mut self, ud: *mut T)
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.
Sourcepub fn push_thread(&mut self) -> bool
pub fn push_thread(&mut self) -> bool
Maps to lua_pushthread
.
Sourcepub fn get_global(&mut self, name: &str) -> Type
pub fn get_global(&mut self, name: &str) -> Type
Maps to lua_getglobal
.
Sourcepub fn create_table(&mut self, narr: c_int, nrec: c_int)
pub fn create_table(&mut self, narr: c_int, nrec: c_int)
Maps to lua_createtable
.
Sourcepub fn new_userdata(&mut self, sz: size_t) -> *mut c_void
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.
Sourcepub fn new_userdata_typed<T>(&mut self) -> *mut T
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?
More examples
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 }
Sourcepub fn get_metatable(&mut self, objindex: Index) -> bool
pub fn get_metatable(&mut self, objindex: Index) -> bool
Maps to lua_getmetatable
.
Sourcepub fn get_uservalue(&mut self, idx: Index) -> Type
pub fn get_uservalue(&mut self, idx: Index) -> Type
Maps to lua_getuservalue
.
Sourcepub fn set_global(&mut self, var: &str)
pub fn set_global(&mut self, var: &str)
Maps to lua_setglobal
.
Examples found in repository?
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
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}
Sourcepub fn set_field(&mut self, idx: Index, k: &str)
pub fn set_field(&mut self, idx: Index, k: &str)
Maps to lua_setfield
.
Examples found in repository?
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
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}
Sourcepub fn set_metatable(&mut self, objindex: Index)
pub fn set_metatable(&mut self, objindex: Index)
Maps to lua_setmetatable
.
Sourcepub fn set_uservalue(&mut self, idx: Index)
pub fn set_uservalue(&mut self, idx: Index)
Maps to lua_setuservalue
.
Sourcepub fn pcallk<F>(
&mut self,
nargs: c_int,
nresults: c_int,
msgh: c_int,
continuation: F,
) -> c_int
pub fn pcallk<F>( &mut self, nargs: c_int, nresults: c_int, msgh: c_int, continuation: F, ) -> c_int
Maps to lua_pcallk
.
Sourcepub fn pcall(
&mut self,
nargs: c_int,
nresults: c_int,
msgh: c_int,
) -> ThreadStatus
pub fn pcall( &mut self, nargs: c_int, nresults: c_int, msgh: c_int, ) -> ThreadStatus
Maps to lua_pcall
.
Sourcepub fn load<F>(&mut self, reader: F, source: &str, mode: &str) -> ThreadStatus
pub fn load<F>(&mut self, reader: F, source: &str, mode: &str) -> ThreadStatus
Maps to lua_load
.
Sourcepub fn co_yield(&mut self, nresults: c_int) -> !
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.
Sourcepub fn resume(&mut self, from: Option<&mut State>, nargs: c_int) -> ThreadStatus
pub fn resume(&mut self, from: Option<&mut State>, nargs: c_int) -> ThreadStatus
Maps to lua_resume
.
Sourcepub fn status(&mut self) -> ThreadStatus
pub fn status(&mut self) -> ThreadStatus
Maps to lua_status
.
Sourcepub fn is_yieldable(&mut self) -> bool
pub fn is_yieldable(&mut self) -> bool
Maps to lua_isyieldable
.
Sourcepub fn string_to_number(&mut self, s: &str) -> size_t
pub fn string_to_number(&mut self, s: &str) -> size_t
Maps to lua_stringtonumber
.
Sourcepub fn get_alloc_fn(&mut self) -> (Allocator, *mut c_void)
pub fn get_alloc_fn(&mut self) -> (Allocator, *mut c_void)
Maps to lua_getallocf
.
Sourcepub fn set_alloc_fn(&mut self, f: Allocator, ud: *mut c_void)
pub fn set_alloc_fn(&mut self, f: Allocator, ud: *mut c_void)
Maps to lua_setallocf
.
Sourcepub fn set_extra(&mut self, extra: Option<Extra>) -> Option<Extra>
pub fn set_extra(&mut self, extra: Option<Extra>) -> Option<Extra>
Set extra data. Return previous value if it was set.
Sourcepub fn with_extra<F, R>(&mut self, closure: F) -> R
pub fn with_extra<F, R>(&mut self, closure: F) -> R
Do some actions with mutable extra.
Sourcepub fn with_extra_typed<T, F, R>(&mut self, closure: F) -> R
pub fn with_extra_typed<T, F, R>(&mut self, closure: F) -> R
Unwrap and downcast extra to typed.
§Panics
Panics if state has no attached Extra
or it’s impossible to downcast to T
.
Sourcepub fn to_integer(&mut self, index: Index) -> Integer
pub fn to_integer(&mut self, index: Index) -> Integer
Maps to lua_tointeger
.
Sourcepub fn pop(&mut self, n: c_int)
pub fn pop(&mut self, n: c_int)
Maps to lua_pop
.
Examples found in repository?
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
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}
Sourcepub fn new_table(&mut self)
pub fn new_table(&mut self)
Maps to lua_newtable
.
Examples found in repository?
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
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}
Sourcepub fn push_fn(&mut self, f: Function)
pub fn push_fn(&mut self, f: Function)
Maps to lua_pushcfunction
.
Examples found in repository?
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}
Sourcepub fn is_light_userdata(&mut self, index: Index) -> bool
pub fn is_light_userdata(&mut self, index: Index) -> bool
Maps to lua_islightuserdata
.
Sourcepub fn is_none_or_nil(&mut self, index: Index) -> bool
pub fn is_none_or_nil(&mut self, index: Index) -> bool
Maps to lua_isnoneornil
.
Sourcepub fn push_global_table(&mut self)
pub fn push_global_table(&mut self)
Maps to lua_pushglobaltable
.
Sourcepub fn get_upvalue(&mut self, funcindex: Index, n: c_int) -> Option<&str>
pub fn get_upvalue(&mut self, funcindex: Index, n: c_int) -> Option<&str>
Maps to lua_getupvalue
.
Sourcepub fn set_upvalue(&mut self, funcindex: Index, n: c_int) -> Option<&str>
pub fn set_upvalue(&mut self, funcindex: Index, n: c_int) -> Option<&str>
Maps to lua_setupvalue
.
Sourcepub fn upvalue_join(&mut self, fidx1: Index, n1: c_int, fidx2: Index, n2: c_int)
pub fn upvalue_join(&mut self, fidx1: Index, n1: c_int, fidx2: Index, n2: c_int)
Maps to lua_upvaluejoin
.
Sourcepub fn get_hook_mask(&mut self) -> HookMask
pub fn get_hook_mask(&mut self) -> HookMask
Maps to lua_gethookmask
.
Sourcepub fn get_hook_count(&mut self) -> c_int
pub fn get_hook_count(&mut self) -> c_int
Maps to lua_gethookcount
.
Sourcepub fn check_version(&mut self)
pub fn check_version(&mut self)
Maps to luaL_checkversion
.
Sourcepub fn get_metafield(&mut self, obj: Index, e: &str) -> bool
pub fn get_metafield(&mut self, obj: Index, e: &str) -> bool
Maps to luaL_getmetafield
.
Sourcepub fn to_str(&mut self, index: Index) -> Option<&str>
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.
Sourcepub fn to_str_in_place(&mut self, index: Index) -> Option<&str>
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.
Sourcepub fn check_number(&mut self, arg: Index) -> Number
pub fn check_number(&mut self, arg: Index) -> Number
Maps to luaL_checknumber
.
Sourcepub fn opt_number(&mut self, arg: Index, def: Number) -> Number
pub fn opt_number(&mut self, arg: Index, def: Number) -> Number
Maps to luaL_optnumber
.
Sourcepub fn check_integer(&mut self, arg: Index) -> Integer
pub fn check_integer(&mut self, arg: Index) -> Integer
Maps to luaL_checkinteger
.
Examples found in repository?
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 }
Sourcepub fn opt_integer(&mut self, arg: Index, def: Integer) -> Integer
pub fn opt_integer(&mut self, arg: Index, def: Integer) -> Integer
Maps to luaL_optinteger
.
Examples found in repository?
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 }
Sourcepub fn check_stack_msg(&mut self, sz: c_int, msg: &str)
pub fn check_stack_msg(&mut self, sz: c_int, msg: &str)
Maps to luaL_checkstack
.
Sourcepub fn check_type(&mut self, arg: Index, t: Type)
pub fn check_type(&mut self, arg: Index, t: Type)
Maps to luaL_checktype
.
Sourcepub fn new_metatable(&mut self, tname: &str) -> bool
pub fn new_metatable(&mut self, tname: &str) -> bool
Maps to luaL_newmetatable
.
Examples found in repository?
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
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}
Sourcepub fn set_metatable_from_registry(&mut self, tname: &str)
pub fn set_metatable_from_registry(&mut self, tname: &str)
Maps to luaL_setmetatable
.
Examples found in repository?
More examples
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 }
Sourcepub fn test_userdata(&mut self, arg: Index, tname: &str) -> *mut c_void
pub fn test_userdata(&mut self, arg: Index, tname: &str) -> *mut c_void
Maps to luaL_testudata
.
Sourcepub unsafe fn test_userdata_typed<'a, T>(
&'a mut self,
arg: Index,
tname: &str,
) -> Option<&'a mut T>
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.
Sourcepub fn check_userdata(&mut self, arg: Index, tname: &str) -> *mut c_void
pub fn check_userdata(&mut self, arg: Index, tname: &str) -> *mut c_void
Maps to luaL_checkudata
.
Examples found in repository?
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
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 }
Sourcepub unsafe fn check_userdata_typed<'a, T>(
&'a mut self,
arg: Index,
tname: &str,
) -> &'a mut T
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.
Sourcepub fn check_option(
&mut self,
arg: Index,
def: Option<&str>,
lst: &[&str],
) -> usize
pub fn check_option( &mut self, arg: Index, def: Option<&str>, lst: &[&str], ) -> usize
Maps to luaL_checkoption
.
Sourcepub fn file_result(&mut self, stat: c_int, fname: &str) -> c_int
pub fn file_result(&mut self, stat: c_int, fname: &str) -> c_int
Maps to luaL_fileresult
.
Sourcepub fn exec_result(&mut self, stat: c_int) -> c_int
pub fn exec_result(&mut self, stat: c_int) -> c_int
Maps to luaL_execresult
.
Sourcepub fn unreference(&mut self, t: Index, reference: Reference)
pub fn unreference(&mut self, t: Index, reference: Reference)
Maps to luaL_unref
.
Sourcepub fn load_filex(&mut self, filename: &str, mode: &str) -> ThreadStatus
pub fn load_filex(&mut self, filename: &str, mode: &str) -> ThreadStatus
Maps to luaL_loadfilex
.
Sourcepub fn load_file(&mut self, filename: &str) -> ThreadStatus
pub fn load_file(&mut self, filename: &str) -> ThreadStatus
Maps to luaL_loadfile
.
Sourcepub fn load_bufferx(
&mut self,
buff: &[u8],
name: &str,
mode: &str,
) -> ThreadStatus
pub fn load_bufferx( &mut self, buff: &[u8], name: &str, mode: &str, ) -> ThreadStatus
Maps to luaL_loadbufferx
.
Sourcepub fn load_string(&mut self, source: &str) -> ThreadStatus
pub fn load_string(&mut self, source: &str) -> ThreadStatus
Maps to luaL_loadstring
.
Sourcepub fn len_direct(&mut self, index: Index) -> Integer
pub fn len_direct(&mut self, index: Index) -> Integer
Maps to luaL_len
.
Sourcepub fn set_fns(&mut self, l: &[(&str, Function)], nup: c_int)
pub fn set_fns(&mut self, l: &[(&str, Function)], nup: c_int)
Maps to luaL_setfuncs
.
Examples found in repository?
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
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}
Sourcepub fn get_subtable(&mut self, idx: Index, fname: &str) -> bool
pub fn get_subtable(&mut self, idx: Index, fname: &str) -> bool
Maps to luaL_getsubtable
.
Sourcepub fn traceback(&mut self, state: &mut State, msg: &str, level: c_int)
pub fn traceback(&mut self, state: &mut State, msg: &str, level: c_int)
Maps to luaL_traceback
.
Sourcepub fn new_lib_table(&mut self, l: &[(&str, Function)])
pub fn new_lib_table(&mut self, l: &[(&str, Function)])
Maps to luaL_newlibtable
.
Sourcepub fn check_string(&mut self, n: Index) -> &str
pub fn check_string(&mut self, n: Index) -> &str
Maps to luaL_checklstring
.
Sourcepub fn opt_string<'a>(&'a mut self, n: Index, default: &'a str) -> &'a str
pub fn opt_string<'a>(&'a mut self, n: Index, default: &'a str) -> &'a str
Maps to luaL_optlstring
.
Sourcepub fn typename_at(&mut self, n: Index) -> &'static str
pub fn typename_at(&mut self, n: Index) -> &'static str
Maps to luaL_typename
.
Sourcepub fn get_metatable_from_registry(&mut self, tname: &str)
pub fn get_metatable_from_registry(&mut self, tname: &str)
Maps to luaL_getmetatable
.
Sourcepub fn load_buffer(&mut self, buff: &[u8], name: &str) -> ThreadStatus
pub fn load_buffer(&mut self, buff: &[u8], name: &str) -> ThreadStatus
Maps to luaL_loadbuffer
.