@@ -211,7 +211,7 @@ impl Table {
211211 ///
212212 /// let always_equals_mt = lua.create_table()?;
213213 /// 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))? ;
215215 ///
216216 /// assert!(table1.equals(&table1.clone())?);
217217 /// assert!(table1.equals(&table2)?);
@@ -501,27 +501,23 @@ impl Table {
501501 ///
502502 /// If `metatable` is `None`, the metatable is removed (if no metatable is set, this does
503503 /// 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 < ( ) > {
506505 #[ cfg( feature = "luau" ) ]
507506 if self . is_readonly ( ) {
508- panic ! ( "attempt to modify a readonly table" ) ;
507+ return Err ( Error :: runtime ( "attempt to modify a readonly table" ) ) ;
509508 }
510509
511510 let lua = self . 0 . lua . lock ( ) ;
512- let state = lua. state ( ) ;
511+ let ref_thread = lua. ref_thread ( ) ;
513512 unsafe {
514- let _sg = StackGuard :: new ( state) ;
515- assert_stack ( state, 2 ) ;
516-
517- lua. push_ref ( & self . 0 ) ;
518513 if let Some ( metatable) = metatable {
519- lua . push_ref ( & metatable. 0 ) ;
514+ ffi :: lua_pushvalue ( ref_thread , metatable. 0 . index ) ;
520515 } else {
521- ffi:: lua_pushnil ( state ) ;
516+ ffi:: lua_pushnil ( ref_thread ) ;
522517 }
523- ffi:: lua_setmetatable ( state , - 2 ) ;
518+ ffi:: lua_setmetatable ( ref_thread , self . 0 . index ) ;
524519 }
520+ Ok ( ( ) )
525521 }
526522
527523 /// Returns true if the table has metatable attached.
0 commit comments