1
1
use std:: { mem, process, ptr, str} ;
2
2
use std:: sync:: { Arc , Mutex } ;
3
- use std:: ops:: DerefMut ;
4
3
use std:: cell:: RefCell ;
5
4
use std:: ffi:: CString ;
6
5
use std:: any:: { Any , TypeId } ;
@@ -32,7 +31,7 @@ pub struct Lua {
32
31
/// !Send, and callbacks that are !Send and not 'static.
33
32
pub struct Scope < ' lua , ' scope > {
34
33
lua : & ' lua Lua ,
35
- destructors : RefCell < Vec < Box < FnMut ( * mut ffi:: lua_State ) -> Box < Any > > > > ,
34
+ destructors : RefCell < Vec < Box < Fn ( * mut ffi:: lua_State ) -> Box < Any > > > > ,
36
35
// 'scope lifetime must be invariant
37
36
_scope : PhantomData < & ' scope mut & ' scope ( ) > ,
38
37
}
@@ -265,20 +264,33 @@ impl Lua {
265
264
///
266
265
/// [`ToLua`]: trait.ToLua.html
267
266
/// [`ToLuaMulti`]: trait.ToLuaMulti.html
268
- pub fn create_function < ' lua , ' callback , A , R , F > (
269
- & ' lua self ,
270
- mut func : F ,
271
- ) -> Result < Function < ' lua > >
267
+ pub fn create_function < ' lua , ' callback , A , R , F > ( & ' lua self , func : F ) -> Result < Function < ' lua > >
272
268
where
273
269
A : FromLuaMulti < ' callback > ,
274
270
R : ToLuaMulti < ' callback > ,
275
- F : ' static + Send + FnMut ( & ' callback Lua , A ) -> Result < R > ,
271
+ F : ' static + Send + Fn ( & ' callback Lua , A ) -> Result < R > ,
276
272
{
277
273
self . create_callback_function ( Box :: new ( move |lua, args| {
278
274
func ( lua, A :: from_lua_multi ( args, lua) ?) ?. to_lua_multi ( lua)
279
275
} ) )
280
276
}
281
277
278
+ pub fn create_function_mut < ' lua , ' callback , A , R , F > (
279
+ & ' lua self ,
280
+ func : F ,
281
+ ) -> Result < Function < ' lua > >
282
+ where
283
+ A : FromLuaMulti < ' callback > ,
284
+ R : ToLuaMulti < ' callback > ,
285
+ F : ' static + Send + FnMut ( & ' callback Lua , A ) -> Result < R > ,
286
+ {
287
+ let func = RefCell :: new ( func) ;
288
+ self . create_function ( move |lua, args| {
289
+ ( & mut * func. try_borrow_mut ( )
290
+ . map_err ( |_| Error :: RecursiveMutCallback ) ?) ( lua, args)
291
+ } )
292
+ }
293
+
282
294
/// Wraps a Lua function into a new thread (or coroutine).
283
295
///
284
296
/// Equivalent to `coroutine.create`.
@@ -354,7 +366,7 @@ impl Lua {
354
366
Value :: String ( s) => Ok ( s) ,
355
367
v => unsafe {
356
368
stack_err_guard ( self . state , 0 , || {
357
- check_stack ( self . state , 2 ) ;
369
+ check_stack ( self . state , 4 ) ;
358
370
let ty = v. type_name ( ) ;
359
371
self . push_value ( self . state , v) ;
360
372
let s =
@@ -383,7 +395,7 @@ impl Lua {
383
395
Value :: Integer ( i) => Ok ( i) ,
384
396
v => unsafe {
385
397
stack_guard ( self . state , 0 , || {
386
- check_stack ( self . state , 1 ) ;
398
+ check_stack ( self . state , 2 ) ;
387
399
let ty = v. type_name ( ) ;
388
400
self . push_value ( self . state , v) ;
389
401
let mut isint = 0 ;
@@ -412,7 +424,7 @@ impl Lua {
412
424
Value :: Number ( n) => Ok ( n) ,
413
425
v => unsafe {
414
426
stack_guard ( self . state , 0 , || {
415
- check_stack ( self . state , 1 ) ;
427
+ check_stack ( self . state , 2 ) ;
416
428
let ty = v. type_name ( ) ;
417
429
self . push_value ( self . state , v) ;
418
430
let mut isnum = 0 ;
@@ -511,7 +523,7 @@ impl Lua {
511
523
pub fn create_registry_value < ' lua , T : ToLua < ' lua > > ( & ' lua self , t : T ) -> Result < RegistryKey > {
512
524
unsafe {
513
525
stack_guard ( self . state , 0 , || {
514
- check_stack ( self . state , 1 ) ;
526
+ check_stack ( self . state , 2 ) ;
515
527
516
528
self . push_value ( self . state , t. to_lua ( self ) ?) ;
517
529
let registry_id = gc_guard ( self . state , || {
@@ -592,7 +604,7 @@ impl Lua {
592
604
}
593
605
}
594
606
595
- // Uses 1 stack space , does not call checkstack
607
+ // Uses 2 stack spaces , does not call checkstack
596
608
pub ( crate ) unsafe fn push_value ( & self , state : * mut ffi:: lua_State , value : Value ) {
597
609
match value {
598
610
Value :: Nil => {
@@ -730,7 +742,7 @@ impl Lua {
730
742
// Used if both an __index metamethod is set and regular methods, checks methods table
731
743
// first, then __index metamethod.
732
744
unsafe extern "C" fn meta_index_impl ( state : * mut ffi:: lua_State ) -> c_int {
733
- check_stack ( state, 2 ) ;
745
+ ffi :: luaL_checkstack ( state, 2 , ptr :: null ( ) ) ;
734
746
735
747
ffi:: lua_pushvalue ( state, -1 ) ;
736
748
ffi:: lua_gettable ( state, ffi:: lua_upvalueindex ( 1 ) ) ;
@@ -922,7 +934,7 @@ impl Lua {
922
934
ffi:: lua_newtable ( state) ;
923
935
924
936
push_string ( state, "__gc" ) . unwrap ( ) ;
925
- ffi:: lua_pushcfunction ( state, userdata_destructor :: < RefCell < Callback > > ) ;
937
+ ffi:: lua_pushcfunction ( state, userdata_destructor :: < Callback > ) ;
926
938
ffi:: lua_rawset ( state, -3 ) ;
927
939
928
940
push_string ( state, "__metatable" ) . unwrap ( ) ;
@@ -977,10 +989,7 @@ impl Lua {
977
989
return Err ( Error :: CallbackDestructed ) ;
978
990
}
979
991
980
- let func = get_userdata :: < RefCell < Callback > > ( state, ffi:: lua_upvalueindex ( 1 ) ) ;
981
- let mut func = ( * func)
982
- . try_borrow_mut ( )
983
- . map_err ( |_| Error :: RecursiveCallback ) ?;
992
+ let func = get_userdata :: < Callback > ( state, ffi:: lua_upvalueindex ( 1 ) ) ;
984
993
985
994
let nargs = ffi:: lua_gettop ( state) ;
986
995
let mut args = MultiValue :: new ( ) ;
@@ -989,10 +998,10 @@ impl Lua {
989
998
args. push_front ( lua. pop_value ( state) ) ;
990
999
}
991
1000
992
- let results = func . deref_mut ( ) ( & lua, args) ?;
1001
+ let results = ( * func ) ( & lua, args) ?;
993
1002
let nresults = results. len ( ) as c_int ;
994
1003
995
- check_stack ( state, nresults) ;
1004
+ check_stack_err ( state, nresults) ? ;
996
1005
997
1006
for r in results {
998
1007
lua. push_value ( state, r) ;
@@ -1006,7 +1015,7 @@ impl Lua {
1006
1015
stack_err_guard ( self . state , 0 , move || {
1007
1016
check_stack ( self . state , 2 ) ;
1008
1017
1009
- push_userdata :: < RefCell < Callback > > ( self . state , RefCell :: new ( func) ) ?;
1018
+ push_userdata :: < Callback > ( self . state , func) ?;
1010
1019
1011
1020
ffi:: lua_pushlightuserdata (
1012
1021
self . state ,
@@ -1053,15 +1062,15 @@ impl Lua {
1053
1062
}
1054
1063
1055
1064
impl < ' lua , ' scope > Scope < ' lua , ' scope > {
1056
- pub fn create_function < ' callback , A , R , F > ( & self , mut func : F ) -> Result < Function < ' lua > >
1065
+ pub fn create_function < ' callback , A , R , F > ( & self , func : F ) -> Result < Function < ' lua > >
1057
1066
where
1058
1067
A : FromLuaMulti < ' callback > ,
1059
1068
R : ToLuaMulti < ' callback > ,
1060
- F : ' scope + FnMut ( & ' callback Lua , A ) -> Result < R > ,
1069
+ F : ' scope + Fn ( & ' callback Lua , A ) -> Result < R > ,
1061
1070
{
1062
1071
unsafe {
1063
1072
let f: Box <
1064
- FnMut ( & ' callback Lua , MultiValue < ' callback > ) -> Result < MultiValue < ' callback > > ,
1073
+ Fn ( & ' callback Lua , MultiValue < ' callback > ) -> Result < MultiValue < ' callback > > ,
1065
1074
> = Box :: new ( move |lua, args| {
1066
1075
func ( lua, A :: from_lua_multi ( args, lua) ?) ?. to_lua_multi ( lua)
1067
1076
} ) ;
@@ -1082,7 +1091,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
1082
1091
ffi:: luaL_unref ( state, ffi:: LUA_REGISTRYINDEX , registry_id) ;
1083
1092
1084
1093
ffi:: lua_getupvalue ( state, -1 , 1 ) ;
1085
- let ud = take_userdata :: < RefCell < Callback > > ( state) ;
1094
+ let ud = take_userdata :: < Callback > ( state) ;
1086
1095
1087
1096
ffi:: lua_pushnil ( state) ;
1088
1097
ffi:: lua_setupvalue ( state, -2 , 1 ) ;
@@ -1094,6 +1103,19 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
1094
1103
}
1095
1104
}
1096
1105
1106
+ pub fn create_function_mut < ' callback , A , R , F > ( & self , func : F ) -> Result < Function < ' lua > >
1107
+ where
1108
+ A : FromLuaMulti < ' callback > ,
1109
+ R : ToLuaMulti < ' callback > ,
1110
+ F : ' scope + FnMut ( & ' callback Lua , A ) -> Result < R > ,
1111
+ {
1112
+ let func = RefCell :: new ( func) ;
1113
+ self . create_function ( move |lua, args| {
1114
+ ( & mut * func. try_borrow_mut ( )
1115
+ . map_err ( |_| Error :: RecursiveMutCallback ) ?) ( lua, args)
1116
+ } )
1117
+ }
1118
+
1097
1119
pub fn create_userdata < T > ( & self , data : T ) -> Result < AnyUserData < ' lua > >
1098
1120
where
1099
1121
T : UserData ,
@@ -1128,7 +1150,7 @@ impl<'lua, 'scope> Drop for Scope<'lua, 'scope> {
1128
1150
let to_drop = self . destructors
1129
1151
. get_mut ( )
1130
1152
. drain ( ..)
1131
- . map ( |mut destructor| destructor ( state) )
1153
+ . map ( |destructor| destructor ( state) )
1132
1154
. collect :: < Vec < _ > > ( ) ;
1133
1155
drop ( to_drop) ;
1134
1156
}
0 commit comments