@@ -12331,3 +12331,62 @@ fn an_integer_initializer_is_stored_as_the_field_type() {
1233112331 assert ! ( matches!( emulator. value_of( "value" ) , Some ( Value :: Single ( v) ) if * v == 0.0 ) ) ;
1233212332 assert ! ( matches!( emulator. value_of( "wide" ) , Some ( Value :: Double ( v) ) if * v == 2.0 ) ) ;
1233312333}
12334+
12335+ #[ test]
12336+ fn an_exported_field_initializer_does_not_overwrite_the_transferred_value ( ) {
12337+ // `public int[] a = { }` is the inspector's array once set, applied by
12338+ // the Unity proxy after the heap loads. A startup initializer would wipe
12339+ // it back to empty on the first event (issue: Length came out 0 for an
12340+ // inspector-set array), so an exported field runs no initializer — its
12341+ // value is whatever was transferred.
12342+ let mut sources = vec ! [ SourceCode :: new(
12343+ "test.cs" ,
12344+ r#"
12345+ using MenSharp;
12346+ public class Thing : MenSharpBehaviour
12347+ {
12348+ public int[] items = { };
12349+ public int length;
12350+ public void Start() { length = items.Length; }
12351+ }
12352+ "# ,
12353+ ) ] ;
12354+ sources. extend ( Compiler :: corlib_sources ( ) ) ;
12355+ let program = compile_behaviour ( sources, "Thing" ) . unwrap ( ) ;
12356+ assert ! (
12357+ program. output. errors. is_empty( ) ,
12358+ "codegen errors: {:#?}" ,
12359+ program. output. errors
12360+ ) ;
12361+ let assembled = program. output . program . assemble ( ) . unwrap ( ) ;
12362+ let mut emulator = Emulator :: new ( & program. output . program , & assembled) ;
12363+ // what the proxy transfers for an inspector array of length 1
12364+ let transferred = std:: rc:: Rc :: new ( std:: cell:: RefCell :: new ( vec ! [ Value :: Int32 ( 42 ) ] ) ) ;
12365+ assert ! ( emulator. set_value( "items" , Value :: Array ( transferred) ) ) ;
12366+ emulator. run ( & assembled, "_start" ) . unwrap ( ) ;
12367+ assert_eq ! ( int_of( & emulator, "length" ) , 1 ) ;
12368+ }
12369+
12370+ #[ test]
12371+ fn a_private_field_initializer_still_runs ( ) {
12372+ // the flip side: a non-exported field has no proxy value, so its
12373+ // initializer must run — including one that is not a bakeable literal
12374+ let Some ( emulator) = run_behaviour (
12375+ r#"
12376+ using MenSharp;
12377+ public class Thing : MenSharpBehaviour
12378+ {
12379+ private int[] items = { 4, 5, 6 };
12380+ public int length;
12381+ public int first;
12382+ public void Start() { length = items.Length; first = items[0]; }
12383+ }
12384+ "# ,
12385+ "Thing" ,
12386+ "_start" ,
12387+ ) else {
12388+ return ;
12389+ } ;
12390+ assert_eq ! ( int_of( & emulator, "length" ) , 3 ) ;
12391+ assert_eq ! ( int_of( & emulator, "first" ) , 4 ) ;
12392+ }
0 commit comments