@@ -211,7 +211,7 @@ impl Table {
211
211
///
212
212
/// let always_equals_mt = lua.create_table()?;
213
213
/// always_equals_mt.set("__eq", lua.create_function(|_, (_t1, _t2): (Table, Table)| Ok(true))?)?;
214
- /// table2.set_metatable(Some(always_equals_mt));
214
+ /// table2.set_metatable(Some(always_equals_mt))? ;
215
215
///
216
216
/// assert!(table1.equals(&table1.clone())?);
217
217
/// assert!(table1.equals(&table2)?);
@@ -501,27 +501,23 @@ impl Table {
501
501
///
502
502
/// If `metatable` is `None`, the metatable is removed (if no metatable is set, this does
503
503
/// nothing).
504
- pub fn set_metatable ( & self , metatable : Option < Table > ) {
505
- // Workaround to throw readonly error without returning Result
504
+ pub fn set_metatable ( & self , metatable : Option < Table > ) -> Result < ( ) > {
506
505
#[ cfg( feature = "luau" ) ]
507
506
if self . is_readonly ( ) {
508
- panic ! ( "attempt to modify a readonly table" ) ;
507
+ return Err ( Error :: runtime ( "attempt to modify a readonly table" ) ) ;
509
508
}
510
509
511
510
let lua = self . 0 . lua . lock ( ) ;
512
- let state = lua. state ( ) ;
511
+ let ref_thread = lua. ref_thread ( ) ;
513
512
unsafe {
514
- let _sg = StackGuard :: new ( state) ;
515
- assert_stack ( state, 2 ) ;
516
-
517
- lua. push_ref ( & self . 0 ) ;
518
513
if let Some ( metatable) = metatable {
519
- lua . push_ref ( & metatable. 0 ) ;
514
+ ffi :: lua_pushvalue ( ref_thread , metatable. 0 . index ) ;
520
515
} else {
521
- ffi:: lua_pushnil ( state ) ;
516
+ ffi:: lua_pushnil ( ref_thread ) ;
522
517
}
523
- ffi:: lua_setmetatable ( state , - 2 ) ;
518
+ ffi:: lua_setmetatable ( ref_thread , self . 0 . index ) ;
524
519
}
520
+ Ok ( ( ) )
525
521
}
526
522
527
523
/// Returns true if the table has metatable attached.
0 commit comments