@@ -9,7 +9,7 @@ use crate::function::Function;
9
9
use crate :: state:: { LuaGuard , RawLua } ;
10
10
use crate :: traits:: { FromLua , FromLuaMulti , IntoLua , IntoLuaMulti , ObjectLike } ;
11
11
use crate :: types:: { Integer , LuaType , ValueRef } ;
12
- use crate :: util:: { assert_stack, check_stack, StackGuard } ;
12
+ use crate :: util:: { assert_stack, check_stack, get_metatable_ptr , StackGuard } ;
13
13
use crate :: value:: { Nil , Value } ;
14
14
15
15
#[ cfg( feature = "async" ) ]
@@ -242,12 +242,12 @@ impl Table {
242
242
243
243
/// Sets a key-value pair without invoking metamethods.
244
244
pub fn raw_set ( & self , key : impl IntoLua , value : impl IntoLua ) -> Result < ( ) > {
245
- #[ cfg( feature = "luau" ) ]
246
- self . check_readonly_write ( ) ?;
247
-
248
245
let lua = self . 0 . lua . lock ( ) ;
249
246
let state = lua. state ( ) ;
250
247
unsafe {
248
+ #[ cfg( feature = "luau" ) ]
249
+ self . check_readonly_write ( & lua) ?;
250
+
251
251
let _sg = StackGuard :: new ( state) ;
252
252
check_stack ( state, 5 ) ?;
253
253
@@ -312,12 +312,12 @@ impl Table {
312
312
313
313
/// Appends a value to the back of the table without invoking metamethods.
314
314
pub fn raw_push ( & self , value : impl IntoLua ) -> Result < ( ) > {
315
- #[ cfg( feature = "luau" ) ]
316
- self . check_readonly_write ( ) ?;
317
-
318
315
let lua = self . 0 . lua . lock ( ) ;
319
316
let state = lua. state ( ) ;
320
317
unsafe {
318
+ #[ cfg( feature = "luau" ) ]
319
+ self . check_readonly_write ( & lua) ?;
320
+
321
321
let _sg = StackGuard :: new ( state) ;
322
322
check_stack ( state, 4 ) ?;
323
323
@@ -340,12 +340,12 @@ impl Table {
340
340
341
341
/// Removes the last element from the table and returns it, without invoking metamethods.
342
342
pub fn raw_pop < V : FromLua > ( & self ) -> Result < V > {
343
- #[ cfg( feature = "luau" ) ]
344
- self . check_readonly_write ( ) ?;
345
-
346
343
let lua = self . 0 . lua . lock ( ) ;
347
344
let state = lua. state ( ) ;
348
345
unsafe {
346
+ #[ cfg( feature = "luau" ) ]
347
+ self . check_readonly_write ( & lua) ?;
348
+
349
349
let _sg = StackGuard :: new ( state) ;
350
350
check_stack ( state, 3 ) ?;
351
351
@@ -401,13 +401,13 @@ impl Table {
401
401
///
402
402
/// This method is useful to clear the table while keeping its capacity.
403
403
pub fn clear ( & self ) -> Result < ( ) > {
404
- #[ cfg( feature = "luau" ) ]
405
- self . check_readonly_write ( ) ?;
406
-
407
404
let lua = self . 0 . lua . lock ( ) ;
408
405
unsafe {
409
406
#[ cfg( feature = "luau" ) ]
410
- ffi:: lua_cleartable ( lua. ref_thread ( ) , self . 0 . index ) ;
407
+ {
408
+ self . check_readonly_write ( & lua) ?;
409
+ ffi:: lua_cleartable ( lua. ref_thread ( ) , self . 0 . index ) ;
410
+ }
411
411
412
412
#[ cfg( not( feature = "luau" ) ) ]
413
413
{
@@ -550,14 +550,7 @@ impl Table {
550
550
#[ inline]
551
551
pub fn has_metatable ( & self ) -> bool {
552
552
let lua = self . 0 . lua . lock ( ) ;
553
- let ref_thread = lua. ref_thread ( ) ;
554
- unsafe {
555
- if ffi:: lua_getmetatable ( ref_thread, self . 0 . index ) != 0 {
556
- ffi:: lua_pop ( ref_thread, 1 ) ;
557
- return true ;
558
- }
559
- }
560
- false
553
+ unsafe { !get_metatable_ptr ( lua. ref_thread ( ) , self . 0 . index ) . is_null ( ) }
561
554
}
562
555
563
556
/// Sets `readonly` attribute on the table.
@@ -724,12 +717,12 @@ impl Table {
724
717
/// Sets element value at position `idx` without invoking metamethods.
725
718
#[ doc( hidden) ]
726
719
pub fn raw_seti ( & self , idx : usize , value : impl IntoLua ) -> Result < ( ) > {
727
- #[ cfg( feature = "luau" ) ]
728
- self . check_readonly_write ( ) ?;
729
-
730
720
let lua = self . 0 . lua . lock ( ) ;
731
721
let state = lua. state ( ) ;
732
722
unsafe {
723
+ #[ cfg( feature = "luau" ) ]
724
+ self . check_readonly_write ( & lua) ?;
725
+
733
726
let _sg = StackGuard :: new ( state) ;
734
727
check_stack ( state, 5 ) ?;
735
728
@@ -765,8 +758,8 @@ impl Table {
765
758
766
759
#[ cfg( feature = "luau" ) ]
767
760
#[ inline( always) ]
768
- pub ( crate ) fn check_readonly_write ( & self ) -> Result < ( ) > {
769
- if self . is_readonly ( ) {
761
+ fn check_readonly_write ( & self , lua : & RawLua ) -> Result < ( ) > {
762
+ if unsafe { ffi :: lua_getreadonly ( lua . ref_thread ( ) , self . 0 . index ) != 0 } {
770
763
return Err ( Error :: runtime ( "attempt to modify a readonly table" ) ) ;
771
764
}
772
765
Ok ( ( ) )
0 commit comments