File tree Expand file tree Collapse file tree 3 files changed +21
-6
lines changed
Expand file tree Collapse file tree 3 files changed +21
-6
lines changed Original file line number Diff line number Diff line change @@ -349,7 +349,11 @@ pub(crate) unsafe fn init_error_registry(state: *mut ffi::lua_State) -> Result<(
349349 state,
350350 Some ( |state| {
351351 ffi:: lua_pushcfunction ( state, error_tostring) ;
352- rawset_field ( state, -2 , "__tostring" )
352+ ffi:: lua_setfield ( state, -2 , cstr ! ( "__tostring" ) ) ;
353+
354+ // This is mostly for Luau typeof() function
355+ ffi:: lua_pushstring ( state, cstr ! ( "error" ) ) ;
356+ ffi:: lua_setfield ( state, -2 , cstr ! ( "__type" ) ) ;
353357 } ) ,
354358 ) ?;
355359
Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ pub(crate) unsafe fn get_internal_metatable<T: TypeKey>(state: *mut ffi::lua_Sta
4747// Uses 6 stack spaces and calls checkstack.
4848pub ( crate ) unsafe fn init_internal_metatable < T : TypeKey > (
4949 state : * mut ffi:: lua_State ,
50- customize_fn : Option < fn ( * mut ffi:: lua_State ) -> Result < ( ) > > ,
50+ customize_fn : Option < fn ( * mut ffi:: lua_State ) > ,
5151) -> Result < ( ) > {
5252 check_stack ( state, 6 ) ?;
5353
@@ -62,11 +62,11 @@ pub(crate) unsafe fn init_internal_metatable<T: TypeKey>(
6262 ffi:: lua_pushboolean ( state, 0 ) ;
6363 rawset_field ( state, -2 , "__metatable" ) ?;
6464
65- if let Some ( f) = customize_fn {
66- f ( state) ?;
67- }
68-
6965 protect_lua ! ( state, 1 , 0 , |state| {
66+ if let Some ( f) = customize_fn {
67+ f( state) ;
68+ }
69+
7070 ffi:: lua_rawsetp( state, ffi:: LUA_REGISTRYINDEX , T :: type_key( ) ) ;
7171 } ) ?;
7272
Original file line number Diff line number Diff line change @@ -436,5 +436,16 @@ fn test_loadstring() -> Result<()> {
436436 Ok ( ( ) )
437437}
438438
439+ #[ test]
440+ fn test_typeof_error ( ) -> Result < ( ) > {
441+ let lua = Lua :: new ( ) ;
442+
443+ let err = Error :: runtime ( "just a test error" ) ;
444+ let res = lua. load ( "return typeof(...)" ) . call :: < String > ( err) ?;
445+ assert_eq ! ( res, "error" ) ;
446+
447+ Ok ( ( ) )
448+ }
449+
439450#[ path = "luau/require.rs" ]
440451mod require;
You can’t perform that action at this time.
0 commit comments