@@ -88,49 +88,102 @@ fn test_multi_value() {
88
88
assert ! ( multi_value. is_empty( ) ) ;
89
89
}
90
90
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
+
91
127
#[ test]
92
128
fn test_value_to_string ( ) -> Result < ( ) > {
93
129
let lua = Lua :: new ( ) ;
94
130
95
131
assert_eq ! ( Value :: Nil . to_string( ) ?, "nil" ) ;
132
+ assert_eq ! ( Value :: Nil . type_name( ) , "nil" ) ;
96
133
assert_eq ! ( Value :: Boolean ( true ) . to_string( ) ?, "true" ) ;
134
+ assert_eq ! ( Value :: Boolean ( true ) . type_name( ) , "boolean" ) ;
97
135
assert_eq ! ( Value :: NULL . to_string( ) ?, "null" ) ;
136
+ assert_eq ! ( Value :: NULL . type_name( ) , "lightuserdata" ) ;
98
137
assert_eq ! (
99
138
Value :: LightUserData ( LightUserData ( 0x1 as * const c_void as * mut _) ) . to_string( ) ?,
100
139
"lightuserdata: 0x1"
101
140
) ;
102
141
assert_eq ! ( Value :: Integer ( 1 ) . to_string( ) ?, "1" ) ;
142
+ assert_eq ! ( Value :: Integer ( 1 ) . type_name( ) , "integer" ) ;
103
143
assert_eq ! ( Value :: Number ( 34.59 ) . to_string( ) ?, "34.59" ) ;
144
+ assert_eq ! ( Value :: Number ( 34.59 ) . type_name( ) , "number" ) ;
104
145
#[ cfg( all( feature = "luau" , not( feature = "luau-vector4" ) ) ) ]
105
146
assert_eq ! (
106
147
Value :: Vector ( mlua:: Vector :: new( 10.0 , 11.1 , 12.2 ) ) . to_string( ) ?,
107
148
"vector(10, 11.1, 12.2)"
108
149
) ;
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
+ ) ;
109
155
#[ cfg( feature = "luau-vector4" ) ]
110
156
assert_eq ! (
111
157
Value :: Vector ( mlua:: Vector :: new( 10.0 , 11.1 , 12.2 , 13.3 ) ) . to_string( ) ?,
112
158
"vector(10, 11.1, 12.2, 13.3)"
113
159
) ;
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" ) ;
115
164
116
165
let table: Value = lua. load ( "{}" ) . eval ( ) ?;
117
166
assert ! ( table. to_string( ) ?. starts_with( "table:" ) ) ;
118
167
let table: Value = lua
119
168
. load ( "setmetatable({}, {__tostring = function() return 'test table' end})" )
120
169
. eval ( ) ?;
121
170
assert_eq ! ( table. to_string( ) ?, "test table" ) ;
171
+ assert_eq ! ( table. type_name( ) , "table" ) ;
122
172
123
173
let func: Value = lua. load ( "function() end" ) . eval ( ) ?;
124
174
assert ! ( func. to_string( ) ?. starts_with( "function:" ) ) ;
175
+ assert_eq ! ( func. type_name( ) , "function" ) ;
125
176
126
177
let thread: Value = lua. load ( "coroutine.create(function() end)" ) . eval ( ) ?;
127
178
assert ! ( thread. to_string( ) ?. starts_with( "thread:" ) ) ;
179
+ assert_eq ! ( thread. type_name( ) , "thread" ) ;
128
180
129
181
lua. register_userdata_type :: < StdString > ( |reg| {
130
182
reg. add_meta_method ( "__tostring" , |_, this, ( ) | Ok ( this. clone ( ) ) ) ;
131
183
} ) ?;
132
184
let ud: Value = Value :: UserData ( lua. create_any_userdata ( String :: from ( "string userdata" ) ) ?) ;
133
185
assert_eq ! ( ud. to_string( ) ?, "string userdata" ) ;
186
+ assert_eq ! ( ud. type_name( ) , "userdata" ) ;
134
187
135
188
struct MyUserData ;
136
189
impl UserData for MyUserData { }
@@ -139,11 +192,13 @@ fn test_value_to_string() -> Result<()> {
139
192
140
193
let err = Value :: Error ( Box :: new ( Error :: runtime ( "test error" ) ) ) ;
141
194
assert_eq ! ( err. to_string( ) ?, "runtime error: test error" ) ;
195
+ assert_eq ! ( err. type_name( ) , "error" ) ;
142
196
143
197
#[ cfg( feature = "luau" ) ]
144
198
{
145
199
let buf = Value :: Buffer ( lua. create_buffer ( b"hello" ) ?) ;
146
200
assert ! ( buf. to_string( ) ?. starts_with( "buffer:" ) ) ;
201
+ assert_eq ! ( buf. type_name( ) , "buffer" ) ;
147
202
148
203
// Set `__tostring` metamethod for buffer
149
204
let mt = lua. load ( "{__tostring = buffer.tostring}" ) . eval ( ) ?;
0 commit comments