@@ -321,6 +321,7 @@ impl World {
321
321
name = "reflect_reference_functions"
322
322
) ]
323
323
impl ReflectReference {
324
+ /// If this type is an enum, will return the name of the variant it represents on the type.
324
325
fn variant_name (
325
326
ctxt : FunctionCallContext ,
326
327
s : ReflectReference ,
@@ -330,12 +331,14 @@ impl ReflectReference {
330
331
s. variant_name ( world)
331
332
}
332
333
334
+ /// Displays this reference without printing the exact contents.
333
335
fn display_ref ( ctxt : FunctionCallContext , s : ReflectReference ) -> Result < String , InteropError > {
334
336
profiling:: function_scope!( "display_ref" ) ;
335
337
let world = ctxt. world ( ) ?;
336
338
Ok ( s. display_with_world ( world) )
337
339
}
338
340
341
+ /// Displays the "value" of this reference
339
342
fn display_value (
340
343
ctxt : FunctionCallContext ,
341
344
s : ReflectReference ,
@@ -345,6 +348,43 @@ impl ReflectReference {
345
348
Ok ( s. display_value_with_world ( world) )
346
349
}
347
350
351
+ /// Gets and clones the value under the specified key if the underlying type is a map type.
352
+ fn map_get (
353
+ ctxt : FunctionCallContext ,
354
+ self_ : ReflectReference ,
355
+ key : ScriptValue ,
356
+ ) -> Result < Option < ScriptValue > , InteropError > {
357
+ profiling:: function_scope!( "map_get" ) ;
358
+ let world = ctxt. world ( ) ?;
359
+ let key = <Box < dyn PartialReflect > >:: from_script_ref (
360
+ self_. key_type_id ( world. clone ( ) ) ?. ok_or_else ( || {
361
+ InteropError :: unsupported_operation (
362
+ self_. tail_type_id ( world. clone ( ) ) . unwrap_or_default ( ) ,
363
+ Some ( Box :: new ( key. clone ( ) ) ) ,
364
+ "Could not get key type id. Are you trying to index into a type that's not a map?" . to_owned ( ) ,
365
+ )
366
+ } ) ?,
367
+ key,
368
+ world. clone ( ) ,
369
+ ) ?;
370
+ self_. with_reflect_mut ( world. clone ( ) , |s| match s. try_map_get ( key. as_ref ( ) ) ? {
371
+ Some ( value) => {
372
+ let reference = {
373
+ let allocator = world. allocator ( ) ;
374
+ let mut allocator = allocator. write ( ) ;
375
+ let owned_value = <dyn PartialReflect >:: from_reflect ( value, world. clone ( ) ) ?;
376
+ ReflectReference :: new_allocated_boxed ( owned_value, & mut allocator)
377
+ } ;
378
+ Ok ( Some ( ReflectReference :: into_script_ref ( reference, world) ?) )
379
+ }
380
+ None => Ok ( None ) ,
381
+ } ) ?
382
+ }
383
+
384
+ /// Indexes into the given reference and if the nested type is a reference type, returns a deeper reference, otherwise
385
+ /// returns the concrete value.
386
+ ///
387
+ /// Does not support map types at the moment, for maps see `map_get`
348
388
fn get (
349
389
ctxt : FunctionCallContext ,
350
390
mut self_ : ReflectReference ,
@@ -360,6 +400,7 @@ impl ReflectReference {
360
400
ReflectReference :: into_script_ref ( self_, world)
361
401
}
362
402
403
+ /// Sets the value under the specified path on the underlying value.
363
404
fn set (
364
405
ctxt : FunctionCallContext ,
365
406
self_ : ScriptValue ,
@@ -395,6 +436,7 @@ impl ReflectReference {
395
436
Ok ( ScriptValue :: Unit )
396
437
}
397
438
439
+ /// Pushes the value into the reference, if the reference is an appropriate container type.
398
440
fn push (
399
441
ctxt : FunctionCallContext ,
400
442
s : ReflectReference ,
@@ -413,6 +455,7 @@ impl ReflectReference {
413
455
s. with_reflect_mut ( world, |s| s. try_push_boxed ( other) ) ?
414
456
}
415
457
458
+ /// Pops the value from the reference, if the reference is an appropriate container type.
416
459
fn pop ( ctxt : FunctionCallContext , s : ReflectReference ) -> Result < ScriptValue , InteropError > {
417
460
profiling:: function_scope!( "pop" ) ;
418
461
let world = ctxt. world ( ) ?;
@@ -426,6 +469,7 @@ impl ReflectReference {
426
469
ReflectReference :: into_script_ref ( reference, world)
427
470
}
428
471
472
+ /// Inserts the value into the reference at the specified index, if the reference is an appropriate container type.
429
473
fn insert (
430
474
ctxt : FunctionCallContext ,
431
475
s : ReflectReference ,
@@ -461,18 +505,21 @@ impl ReflectReference {
461
505
s. with_reflect_mut ( world, |s| s. try_insert_boxed ( key, value) ) ?
462
506
}
463
507
508
+ /// Clears the container, if the reference is an appropriate container type.
464
509
fn clear ( ctxt : FunctionCallContext , s : ReflectReference ) -> Result < ( ) , InteropError > {
465
510
profiling:: function_scope!( "clear" ) ;
466
511
let world = ctxt. world ( ) ?;
467
512
s. with_reflect_mut ( world, |s| s. try_clear ( ) ) ?
468
513
}
469
514
515
+ /// Retrieves the length of the reference, if the reference is an appropriate container type.
470
516
fn len ( ctxt : FunctionCallContext , s : ReflectReference ) -> Result < Option < usize > , InteropError > {
471
517
profiling:: function_scope!( "len" ) ;
472
518
let world = ctxt. world ( ) ?;
473
519
s. len ( world)
474
520
}
475
521
522
+ /// Removes the value at the specified key from the reference, if the reference is an appropriate container type.
476
523
fn remove (
477
524
ctxt : FunctionCallContext ,
478
525
s : ReflectReference ,
@@ -508,6 +555,9 @@ impl ReflectReference {
508
555
}
509
556
}
510
557
558
+ /// Iterates over the reference, if the reference is an appropriate container type.
559
+ ///
560
+ /// Returns an "next" iterator function.
511
561
fn iter (
512
562
ctxt : FunctionCallContext ,
513
563
s : ReflectReference ,
@@ -536,6 +586,7 @@ impl ReflectReference {
536
586
Ok ( iter_function. into_dynamic_script_function_mut ( ) )
537
587
}
538
588
589
+ /// Lists the functions available on the reference.
539
590
fn functions (
540
591
ctxt : FunctionCallContext ,
541
592
s : ReflectReference ,
0 commit comments