@@ -223,12 +223,13 @@ impl<'lua> LuaTable<'lua> {
223
223
let key = key. to_lua ( lua) ?;
224
224
let value = value. to_lua ( lua) ?;
225
225
unsafe {
226
- check_stack ( lua. state , 3 ) ;
226
+ check_stack ( lua. state , 5 ) ;
227
227
lua. push_ref ( lua. state , & self . 0 ) ;
228
228
lua. push_value ( lua. state , key) ;
229
229
lua. push_value ( lua. state , value) ;
230
- error_guard ( lua. state , 3 , 0 , |state| {
230
+ error_guard ( lua. state , 3 , |state| {
231
231
ffi:: lua_settable ( state, -3 ) ;
232
+ ffi:: lua_pop ( state, 1 ) ;
232
233
Ok ( ( ) )
233
234
} )
234
235
}
@@ -243,15 +244,15 @@ impl<'lua> LuaTable<'lua> {
243
244
let lua = self . 0 . lua ;
244
245
let key = key. to_lua ( lua) ?;
245
246
unsafe {
246
- check_stack ( lua. state , 2 ) ;
247
+ check_stack ( lua. state , 4 ) ;
247
248
lua. push_ref ( lua. state , & self . 0 ) ;
248
249
lua. push_value ( lua. state , key. to_lua ( lua) ?) ;
249
- let res = error_guard ( lua. state , 2 , 0 , |state| {
250
+ error_guard ( lua. state , 2 , |state| {
250
251
ffi:: lua_gettable ( state, -2 ) ;
251
- let res = lua. pop_value ( state) ;
252
- ffi:: lua_pop ( state, 1 ) ;
253
- Ok ( res)
252
+ Ok ( ( ) )
254
253
} ) ?;
254
+ let res = lua. pop_value ( lua. state ) ;
255
+ ffi:: lua_pop ( lua. state , 1 ) ;
255
256
V :: from_lua ( res, lua)
256
257
}
257
258
}
@@ -261,15 +262,16 @@ impl<'lua> LuaTable<'lua> {
261
262
let lua = self . 0 . lua ;
262
263
let key = key. to_lua ( lua) ?;
263
264
unsafe {
264
- check_stack ( lua. state , 2 ) ;
265
+ check_stack ( lua. state , 4 ) ;
265
266
lua. push_ref ( lua. state , & self . 0 ) ;
266
267
lua. push_value ( lua. state , key) ;
267
- error_guard ( lua. state , 2 , 0 , |state| {
268
+ error_guard ( lua. state , 2 , |state| {
268
269
ffi:: lua_gettable ( state, -2 ) ;
269
- let has = ffi:: lua_isnil ( state, -1 ) == 0 ;
270
- ffi:: lua_pop ( state, 2 ) ;
271
- Ok ( has)
272
- } )
270
+ Ok ( ( ) )
271
+ } ) ?;
272
+ let has = ffi:: lua_isnil ( lua. state , -1 ) == 0 ;
273
+ ffi:: lua_pop ( lua. state , 2 ) ;
274
+ Ok ( has)
273
275
}
274
276
}
275
277
@@ -311,10 +313,12 @@ impl<'lua> LuaTable<'lua> {
311
313
pub fn len ( & self ) -> LuaResult < LuaInteger > {
312
314
let lua = self . 0 . lua ;
313
315
unsafe {
314
- error_guard ( lua. state , 0 , 0 , |state| {
315
- check_stack ( state, 1 ) ;
316
- lua. push_ref ( state, & self . 0 ) ;
317
- Ok ( ffi:: luaL_len ( state, -1 ) )
316
+ check_stack ( lua. state , 3 ) ;
317
+ lua. push_ref ( lua. state , & self . 0 ) ;
318
+ error_guard ( lua. state , 1 , |state| {
319
+ let len = ffi:: luaL_len ( state, -1 ) ;
320
+ ffi:: lua_pop ( state, 1 ) ;
321
+ Ok ( len)
318
322
} )
319
323
}
320
324
}
@@ -381,31 +385,29 @@ where
381
385
let lua = self . table . lua ;
382
386
383
387
unsafe {
384
- check_stack ( lua. state , 4 ) ;
388
+ check_stack ( lua. state , 6 ) ;
385
389
386
390
lua. push_ref ( lua. state , & self . table ) ;
387
391
lua. push_ref ( lua. state , & next_key) ;
388
392
389
- match error_guard ( lua. state , 2 , 0 , |state| if ffi:: lua_next ( state, -2 ) != 0 {
390
- ffi:: lua_pushvalue ( state, -2 ) ;
391
- let key = lua. pop_value ( state) ;
392
- let value = lua. pop_value ( state) ;
393
- let next_key = lua. pop_ref ( lua. state ) ;
394
- ffi:: lua_pop ( lua. state , 1 ) ;
395
- Ok ( Some ( ( key, value, next_key) ) )
396
- } else {
397
- ffi:: lua_pop ( lua. state , 1 ) ;
398
- Ok ( None )
399
- } ) {
400
- Ok ( Some ( ( key, value, next_key) ) ) => {
401
- self . next_key = Some ( next_key) ;
393
+ match error_guard ( lua. state , 2 , |state| Ok ( ffi:: lua_next ( state, -2 ) != 0 ) ) {
394
+ Ok ( true ) => {
395
+ ffi:: lua_pushvalue ( lua. state , -2 ) ;
396
+ let key = lua. pop_value ( lua. state ) ;
397
+ let value = lua. pop_value ( lua. state ) ;
398
+ self . next_key = Some ( lua. pop_ref ( lua. state ) ) ;
399
+ ffi:: lua_pop ( lua. state , 1 ) ;
400
+
402
401
Some ( ( || {
403
- let key = K :: from_lua ( key, lua) ?;
404
- let value = V :: from_lua ( value, lua) ?;
405
- Ok ( ( key, value) )
406
- } ) ( ) )
402
+ let key = K :: from_lua ( key, lua) ?;
403
+ let value = V :: from_lua ( value, lua) ?;
404
+ Ok ( ( key, value) )
405
+ } ) ( ) )
406
+ }
407
+ Ok ( false ) => {
408
+ ffi:: lua_pop ( lua. state , 1 ) ;
409
+ None
407
410
}
408
- Ok ( None ) => None ,
409
411
Err ( e) => Some ( Err ( e) ) ,
410
412
}
411
413
}
@@ -436,27 +438,20 @@ where
436
438
let lua = self . table . lua ;
437
439
438
440
unsafe {
439
- check_stack ( lua. state , 2 ) ;
441
+ check_stack ( lua. state , 4 ) ;
440
442
441
443
lua. push_ref ( lua. state , & self . table ) ;
442
- match error_guard (
443
- lua. state ,
444
- 1 ,
445
- 0 ,
446
- |state| if ffi:: lua_geti ( state, -1 , index) != ffi:: LUA_TNIL {
447
- let value = lua. pop_value ( state) ;
448
- ffi:: lua_pop ( state, 1 ) ;
449
- Ok ( Some ( value) )
450
- } else {
451
- ffi:: lua_pop ( state, 2 ) ;
452
- Ok ( None )
453
- } ,
454
- ) {
455
- Ok ( Some ( r) ) => {
444
+ match error_guard ( lua. state , 1 , |state| Ok ( ffi:: lua_geti ( state, -1 , index) != ffi:: LUA_TNIL ) ) {
445
+ Ok ( true ) => {
446
+ let value = lua. pop_value ( lua. state ) ;
447
+ ffi:: lua_pop ( lua. state , 1 ) ;
456
448
self . index = Some ( index + 1 ) ;
457
- Some ( V :: from_lua ( r , lua) )
449
+ Some ( V :: from_lua ( value , lua) )
458
450
}
459
- Ok ( None ) => None ,
451
+ Ok ( false ) => {
452
+ ffi:: lua_pop ( lua. state , 2 ) ;
453
+ None
454
+ } ,
460
455
Err ( e) => Some ( Err ( e) ) ,
461
456
}
462
457
}
0 commit comments