1
+ use std:: error:: Error ;
1
2
use std:: ops:: Add ;
2
3
3
4
use gdnative:: export:: { StaticArgs , StaticArgsMethod , StaticallyNamed } ;
@@ -8,6 +9,7 @@ pub(crate) fn run_tests() -> bool {
8
9
9
10
status &= test_register_property ( ) ;
10
11
status &= test_advanced_methods ( ) ;
12
+ status &= test_varargs_gets ( ) ;
11
13
12
14
status
13
15
}
@@ -16,6 +18,7 @@ pub(crate) fn register(handle: InitHandle) {
16
18
handle. add_class :: < RegisterSignal > ( ) ;
17
19
handle. add_class :: < RegisterProperty > ( ) ;
18
20
handle. add_class :: < AdvancedMethods > ( ) ;
21
+ handle. add_class :: < VarargsGets > ( ) ;
19
22
}
20
23
21
24
#[ derive( Copy , Clone , Debug , Default ) ]
@@ -232,3 +235,61 @@ fn test_advanced_methods() -> bool {
232
235
233
236
ok
234
237
}
238
+
239
+ #[ derive( NativeClass ) ]
240
+ #[ inherit( Reference ) ]
241
+ #[ register_with( VarargsGets :: register) ]
242
+ struct VarargsGets { }
243
+
244
+ #[ methods]
245
+ impl VarargsGets {
246
+ fn new ( _owner : TRef < Reference > ) -> Self {
247
+ Self { }
248
+ }
249
+
250
+ fn register ( builder : & ClassBuilder < VarargsGets > ) {
251
+ builder. method ( "calc" , CalcMethod ) . done ( ) ;
252
+ }
253
+ }
254
+
255
+ struct CalcMethod ;
256
+
257
+ impl Method < VarargsGets > for CalcMethod {
258
+ fn call (
259
+ & self ,
260
+ _this : TInstance < ' _ , VarargsGets > ,
261
+ args : gdnative:: export:: Varargs < ' _ > ,
262
+ ) -> Variant {
263
+ ( || {
264
+ args. check_length ( 1 ..=3 ) ?;
265
+ let a: i64 = args. get ( 0 ) ?;
266
+ let b: i64 = args. get ( 1 ) ?;
267
+ let c: i64 = args. get_opt ( 2 ) ?. unwrap_or ( 11 ) ;
268
+
269
+ let ret = a * b - c;
270
+ Ok :: < Variant , Box < dyn Error > > ( ret. to_variant ( ) )
271
+ } ) ( )
272
+ . unwrap_or_default ( )
273
+ }
274
+ }
275
+
276
+ fn test_varargs_gets ( ) -> bool {
277
+ println ! ( " -- test_varargs_gets" ) ;
278
+
279
+ let ok = std:: panic:: catch_unwind ( || {
280
+ let thing = Instance :: < VarargsGets , _ > :: new ( ) ;
281
+ let base = thing. base ( ) ;
282
+
283
+ let args = [ 3_i64 . to_variant ( ) , 4_i64 . to_variant ( ) , 5_i64 . to_variant ( ) ] ;
284
+ assert_eq ! ( unsafe { base. call( "calc" , & args) . to( ) } , Some ( 7 ) ) ;
285
+
286
+ let args = [ 3_i64 . to_variant ( ) , 4_i64 . to_variant ( ) ] ;
287
+ assert_eq ! ( unsafe { base. call( "calc" , & args) . to( ) } , Some ( 1 ) ) ;
288
+ } )
289
+ . is_ok ( ) ;
290
+
291
+ if !ok {
292
+ godot_error ! ( " !! Test test_varargs_gets failed" ) ;
293
+ }
294
+ ok
295
+ }
0 commit comments