Skip to content

Commit 76604d5

Browse files
committed
fix init overloaded
1 parent d6f8041 commit 76604d5

4 files changed

Lines changed: 90 additions & 2 deletions

File tree

‎src/men-sharp-codegen/src/generator.rs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3958,14 +3958,20 @@ impl<'a, 'ast> Generator<'a, 'ast> {
39583958
// public-variable values are applied *after* the heap loads, so a
39593959
// baked default lets them win — runtime initializer code would
39603960
// overwrite them on the first event
3961-
// `= { 1, 2 }` builds an array, so it always runs at startup
39623961
let baked = match initializer {
39633962
Some(InitializerValue::Expression(expression)) => {
39643963
literal_heap_init(expression, &udon_type)
39653964
}
39663965
_ => None,
39673966
};
3968-
let runs_at_startup = initializer.is_some() && baked.is_none();
3967+
// an exported field is a public variable: its value is whatever the
3968+
// Unity proxy transfers — the inspector value, or the C# field
3969+
// initializer when untouched — applied to the heap after it loads.
3970+
// Startup initializer code would overwrite that on the first event
3971+
// (`int[] a = { }` wiped the inspector's array back to empty), so an
3972+
// exported field never runs one. A non-exported field (a private
3973+
// field, a static) has no proxy value, so it must.
3974+
let runs_at_startup = initializer.is_some() && baked.is_none() && !export;
39693975

39703976
let sync = self.sync_mode_of(field);
39713977
let slot = self.program.add_data(DataSymbol {

‎src/men-sharp-compiler/tests/codegen.rs‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

‎tests/unity-project/Assets/MenSharp/MenSharpRuntimeSmoke.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ public void Scale()
6060
scaled = scale * 0.92f + 1f;
6161
}
6262

63+
// a public array field with an initializer, set in the inspector: the
64+
// transferred value must survive, not be wiped by `= { }` at startup
65+
// (issue: intArray.Length came out 0). And `private float = 1` on a
66+
// field the proxy does not transfer must reach the VM as a Single.
67+
public int[] items = { };
68+
public int itemsLength;
69+
private float one = 1;
70+
public float oneProduct;
71+
public void CountItems() { itemsLength = items.Length; }
72+
public void ScaleOne() { oneProduct = one * 0.92f; }
73+
6374
const string greeting = "Hello";
6475
public string greeted;
6576
public void Greet()

‎tests/unity-project/Assets/Tests/Editor/MenSharpIntegrationTests.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ public IEnumerator GeneratedProgramsExecuteInTheSdkUdonVm()
143143
smoke.RunProgram("Scale");
144144
Assert.AreEqual(1f, smoke.GetProgramVariable("scaled"));
145145

146+
// a public array initializer must not overwrite the inspector value
147+
smoke.RunProgram("CountItems");
148+
Assert.AreEqual(1, smoke.GetProgramVariable("itemsLength"));
149+
150+
// `private float = 1` reaches the VM as a Single
151+
smoke.RunProgram("ScaleOne");
152+
Assert.AreEqual(0.92f, smoke.GetProgramVariable("oneProduct"));
153+
146154
smoke.RunProgram("EnumOps");
147155
Assert.AreEqual(2, smoke.GetProgramVariable("splitCount"));
148156
Assert.AreEqual(1, smoke.GetProgramVariable("flagsValue"));
@@ -207,6 +215,10 @@ private static void BuildRuntimeScene()
207215
var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
208216

209217
GameObject smokeObject = MenSharpTestScene.AddProxy("MenSharpRuntimeSmoke", "MenSharpRuntimeSmoke", Vector3.zero);
218+
MenSharpTestScene.Assign(
219+
MenSharpTestScene.Proxy(smokeObject, "MenSharpRuntimeSmoke"),
220+
"items",
221+
new int[] { 42 });
210222
GameObject secondSmoke = MenSharpTestScene.AddProxy("MenSharpRuntimeSmoke2", "MenSharpRuntimeSmoke", Vector3.up * 4);
211223
GameObject targetObject = MenSharpTestScene.AddProxy("MenSharpRuntimeTarget", "MenSharpRuntimeTarget", Vector3.right * 4);
212224
GameObject callerObject = MenSharpTestScene.AddProxy("MenSharpRuntimeCaller", "MenSharpRuntimeCaller", Vector3.right * 8);

0 commit comments

Comments
 (0)