@@ -88,49 +88,102 @@ fn test_multi_value() {
8888 assert ! ( multi_value. is_empty( ) ) ;
8989}
9090
91+ #[ test]
92+ fn test_value_to_pointer ( ) -> Result < ( ) > {
93+ let lua = Lua :: new ( ) ;
94+
95+ let globals = lua. globals ( ) ;
96+ lua. load (
97+ r#"
98+ table = {}
99+ string = "hello"
100+ num = 1
101+ func = function() end
102+ thread = coroutine.create(function() end)
103+ "# ,
104+ )
105+ . exec ( ) ?;
106+ globals. set ( "null" , Value :: NULL ) ?;
107+
108+ let table: Value = globals. get ( "table" ) ?;
109+ let string: Value = globals. get ( "string" ) ?;
110+ let num: Value = globals. get ( "num" ) ?;
111+ let func: Value = globals. get ( "func" ) ?;
112+ let thread: Value = globals. get ( "thread" ) ?;
113+ let null: Value = globals. get ( "null" ) ?;
114+ let ud: Value = Value :: UserData ( lua. create_any_userdata ( ( ) ) ?) ;
115+
116+ assert ! ( !table. to_pointer( ) . is_null( ) ) ;
117+ assert ! ( !string. to_pointer( ) . is_null( ) ) ;
118+ assert ! ( num. to_pointer( ) . is_null( ) ) ;
119+ assert ! ( !func. to_pointer( ) . is_null( ) ) ;
120+ assert ! ( !thread. to_pointer( ) . is_null( ) ) ;
121+ assert ! ( null. to_pointer( ) . is_null( ) ) ;
122+ assert ! ( !ud. to_pointer( ) . is_null( ) ) ;
123+
124+ Ok ( ( ) )
125+ }
126+
91127#[ test]
92128fn test_value_to_string ( ) -> Result < ( ) > {
93129 let lua = Lua :: new ( ) ;
94130
95131 assert_eq ! ( Value :: Nil . to_string( ) ?, "nil" ) ;
132+ assert_eq ! ( Value :: Nil . type_name( ) , "nil" ) ;
96133 assert_eq ! ( Value :: Boolean ( true ) . to_string( ) ?, "true" ) ;
134+ assert_eq ! ( Value :: Boolean ( true ) . type_name( ) , "boolean" ) ;
97135 assert_eq ! ( Value :: NULL . to_string( ) ?, "null" ) ;
136+ assert_eq ! ( Value :: NULL . type_name( ) , "lightuserdata" ) ;
98137 assert_eq ! (
99138 Value :: LightUserData ( LightUserData ( 0x1 as * const c_void as * mut _) ) . to_string( ) ?,
100139 "lightuserdata: 0x1"
101140 ) ;
102141 assert_eq ! ( Value :: Integer ( 1 ) . to_string( ) ?, "1" ) ;
142+ assert_eq ! ( Value :: Integer ( 1 ) . type_name( ) , "integer" ) ;
103143 assert_eq ! ( Value :: Number ( 34.59 ) . to_string( ) ?, "34.59" ) ;
144+ assert_eq ! ( Value :: Number ( 34.59 ) . type_name( ) , "number" ) ;
104145 #[ cfg( all( feature = "luau" , not( feature = "luau-vector4" ) ) ) ]
105146 assert_eq ! (
106147 Value :: Vector ( mlua:: Vector :: new( 10.0 , 11.1 , 12.2 ) ) . to_string( ) ?,
107148 "vector(10, 11.1, 12.2)"
108149 ) ;
150+ #[ cfg( all( feature = "luau" , not( feature = "luau-vector4" ) ) ) ]
151+ assert_eq ! (
152+ Value :: Vector ( mlua:: Vector :: new( 10.0 , 11.1 , 12.2 ) ) . type_name( ) ,
153+ "vector"
154+ ) ;
109155 #[ cfg( feature = "luau-vector4" ) ]
110156 assert_eq ! (
111157 Value :: Vector ( mlua:: Vector :: new( 10.0 , 11.1 , 12.2 , 13.3 ) ) . to_string( ) ?,
112158 "vector(10, 11.1, 12.2, 13.3)"
113159 ) ;
114- assert_eq ! ( Value :: String ( lua. create_string( "hello" ) ?) . to_string( ) ?, "hello" ) ;
160+
161+ let s = Value :: String ( lua. create_string ( "hello" ) ?) ;
162+ assert_eq ! ( s. to_string( ) ?, "hello" ) ;
163+ assert_eq ! ( s. type_name( ) , "string" ) ;
115164
116165 let table: Value = lua. load ( "{}" ) . eval ( ) ?;
117166 assert ! ( table. to_string( ) ?. starts_with( "table:" ) ) ;
118167 let table: Value = lua
119168 . load ( "setmetatable({}, {__tostring = function() return 'test table' end})" )
120169 . eval ( ) ?;
121170 assert_eq ! ( table. to_string( ) ?, "test table" ) ;
171+ assert_eq ! ( table. type_name( ) , "table" ) ;
122172
123173 let func: Value = lua. load ( "function() end" ) . eval ( ) ?;
124174 assert ! ( func. to_string( ) ?. starts_with( "function:" ) ) ;
175+ assert_eq ! ( func. type_name( ) , "function" ) ;
125176
126177 let thread: Value = lua. load ( "coroutine.create(function() end)" ) . eval ( ) ?;
127178 assert ! ( thread. to_string( ) ?. starts_with( "thread:" ) ) ;
179+ assert_eq ! ( thread. type_name( ) , "thread" ) ;
128180
129181 lua. register_userdata_type :: < StdString > ( |reg| {
130182 reg. add_meta_method ( "__tostring" , |_, this, ( ) | Ok ( this. clone ( ) ) ) ;
131183 } ) ?;
132184 let ud: Value = Value :: UserData ( lua. create_any_userdata ( String :: from ( "string userdata" ) ) ?) ;
133185 assert_eq ! ( ud. to_string( ) ?, "string userdata" ) ;
186+ assert_eq ! ( ud. type_name( ) , "userdata" ) ;
134187
135188 struct MyUserData ;
136189 impl UserData for MyUserData { }
@@ -139,11 +192,13 @@ fn test_value_to_string() -> Result<()> {
139192
140193 let err = Value :: Error ( Box :: new ( Error :: runtime ( "test error" ) ) ) ;
141194 assert_eq ! ( err. to_string( ) ?, "runtime error: test error" ) ;
195+ assert_eq ! ( err. type_name( ) , "error" ) ;
142196
143197 #[ cfg( feature = "luau" ) ]
144198 {
145199 let buf = Value :: Buffer ( lua. create_buffer ( b"hello" ) ?) ;
146200 assert ! ( buf. to_string( ) ?. starts_with( "buffer:" ) ) ;
201+ assert_eq ! ( buf. type_name( ) , "buffer" ) ;
147202
148203 // Set `__tostring` metamethod for buffer
149204 let mt = lua. load ( "{__tostring = buffer.tostring}" ) . eval ( ) ?;
0 commit comments