Skip to content

Commit 107426f

Browse files
committed
fix suger extern function like SetProgramVariable
1 parent 4b8bbc1 commit 107426f

5 files changed

Lines changed: 133 additions & 26 deletions

File tree

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4486,7 +4486,8 @@ impl<'a, 'ast> Generator<'a, 'ast> {
44864486
return piece;
44874487
}
44884488
let receiver_type = receiver.as_ref().map(|(_, ty)| ty);
4489-
let Some(signature) = self.external_signature(ctx, call, receiver_type, &span)
4489+
let Some((signature, passes_type_arguments)) =
4490+
self.external_signature(ctx, call, receiver_type, &span)
44904491
else {
44914492
return Piece::Error;
44924493
};
@@ -4502,19 +4503,22 @@ impl<'a, 'ast> Generator<'a, 'ast> {
45024503
}
45034504
}
45044505
pushed.extend(values.iter().copied());
4505-
// a generic extern takes its type argument as a value, after
4506-
// the ordinary parameters and before the result
4507-
for argument in &call.type_arguments {
4508-
let argument = self.substitute(argument, &ctx.key.bindings);
4509-
match self.type_constant(&argument) {
4510-
Some(slot) => pushed.push(slot),
4511-
None => {
4512-
self.error(
4513-
ctx,
4514-
Message::key("codegen.this_type_argument_has_no_system_type"),
4515-
span,
4516-
);
4517-
return Piece::Error;
4506+
// a real generic extern takes its type argument as a value,
4507+
// after the ordinary parameters and before the result; an
4508+
// erased one (C# sugar over an `object` extern) takes none
4509+
if passes_type_arguments {
4510+
for argument in &call.type_arguments {
4511+
let argument = self.substitute(argument, &ctx.key.bindings);
4512+
match self.type_constant(&argument) {
4513+
Some(slot) => pushed.push(slot),
4514+
None => {
4515+
self.error(
4516+
ctx,
4517+
Message::key("codegen.this_type_argument_has_no_system_type"),
4518+
span,
4519+
);
4520+
return Piece::Error;
4521+
}
45184522
}
45194523
}
45204524
}

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

Lines changed: 93 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Function instances: scheduling, frames, bodies, calls and virtual dispatch.
22
33
use men_sharp_parser::ast::{AccessorKind, Modifier};
4-
use men_sharp_semantics::{FunctionSignature, ParameterPassing};
4+
use men_sharp_semantics::{ExternalMember, FunctionSignature, ParameterPassing};
55

66
use super::*;
77

@@ -1877,13 +1877,20 @@ impl<'a, 'ast> Generator<'a, 'ast> {
18771877
/// static type the call was made on, when it was made on a value: the
18781878
/// whitelist may have the member there rather than on the declaring
18791879
/// type (see exposed_owner).
1880+
/// The extern for a call, and whether its type arguments are passed as
1881+
/// `System.Type` values. A real Udon generic (`GetComponent<T>`) takes the
1882+
/// type as a value and its extern ends in `__T`; but some generic methods
1883+
/// are only C# sugar over a non-generic `object` extern
1884+
/// (`UdonBehaviour.SetProgramVariable<T>(string, T)` → the whitelisted
1885+
/// `(string, object)` extern), and passing a type value — or worse, a
1886+
/// type the VM cannot name like `int[]` — is wrong for those.
18801887
pub(super) fn external_signature(
18811888
&mut self,
18821889
ctx: &Ctx,
18831890
call: &ResolvedCall,
18841891
receiver: Option<&Type>,
18851892
span: &Range<usize>,
1886-
) -> Option<String> {
1893+
) -> Option<(String, bool)> {
18871894
let MemberOrigin::External { member, .. } = &call.origin else {
18881895
return None;
18891896
};
@@ -1938,18 +1945,92 @@ impl<'a, 'ast> Generator<'a, 'ast> {
19381945
} else {
19391946
format!("__{}", parts.join("_"))
19401947
};
1941-
// Udon has no generics: a generic method is one extern named `…__T`
1942-
// that takes its type argument as an ordinary `System.Type` value.
1943-
// The caller supplies that value; see emit_call.
1944-
let suffix = if call.type_arguments.is_empty() {
1945-
format!("__{name}{middle}__{return_part}")
1948+
let receiver = receiver.map(|ty| self.substitute(ty, &ctx.key.bindings));
1949+
1950+
// no type arguments: the ordinary extern
1951+
if call.type_arguments.is_empty() {
1952+
let suffix = format!("__{name}{middle}__{return_part}");
1953+
let owner =
1954+
self.exposed_owner(&declaring, receiver.as_ref(), std::slice::from_ref(&suffix))?;
1955+
return Some((format!("{owner}.{suffix}"), false));
1956+
}
1957+
1958+
// Udon has no generics: a real generic method is one extern named
1959+
// `…__T` that takes its type argument as an ordinary `System.Type`
1960+
// value (`GetComponent<T>`). Prefer that when the VM actually exposes
1961+
// it — the caller supplies the value (see emit_call).
1962+
let generic_suffix = format!("__{name}{middle}__T");
1963+
if let Some(full) = self.real_extern(&declaring, receiver.as_ref(), &generic_suffix) {
1964+
return Some((full, true));
1965+
}
1966+
// otherwise the method may be C# sugar over a non-generic `object`
1967+
// extern: erase its type parameters to `object` and use that, without
1968+
// a type value (`SetProgramVariable<T>` → `(string, object)`).
1969+
if let Some(erased_suffix) = self.erased_generic_suffix(&name, member)
1970+
&& let Some(full) = self.real_extern(&declaring, receiver.as_ref(), &erased_suffix)
1971+
{
1972+
return Some((full, false));
1973+
}
1974+
// neither exists: keep the `__T` shape so the later "not exposed"
1975+
// diagnostic reads as it did before
1976+
let owner = self.exposed_owner(
1977+
&declaring,
1978+
receiver.as_ref(),
1979+
std::slice::from_ref(&generic_suffix),
1980+
)?;
1981+
Some((format!("{owner}.{generic_suffix}"), true))
1982+
}
1983+
1984+
/// The full extern signature for `suffix`, but only when the VM really
1985+
/// exposes it — `exposed_owner` alone falls back to a plausible owner even
1986+
/// when nothing matches, which is what the callers here must tell apart.
1987+
fn real_extern(
1988+
&self,
1989+
declaring: &Type,
1990+
receiver: Option<&Type>,
1991+
suffix: &str,
1992+
) -> Option<String> {
1993+
let owner = self.exposed_owner(
1994+
declaring,
1995+
receiver,
1996+
std::slice::from_ref(&suffix.to_string()),
1997+
)?;
1998+
let full = format!("{owner}.{suffix}");
1999+
self.nodes.has_signature(&full).then_some(full)
2000+
}
2001+
2002+
/// The extern suffix of a generic method with its type parameters erased to
2003+
/// `object`, or `None` when a non-type-parameter part cannot be named. The
2004+
/// original (uninstantiated) signature is read, so which parameters were
2005+
/// the method's own type parameters is still visible.
2006+
fn erased_generic_suffix(&self, name: &str, member: &ExternalMember) -> Option<String> {
2007+
let MemberSignature::Function(signature) = &member.signature else {
2008+
return None;
2009+
};
2010+
let mut parts: Vec<String> = Vec::new();
2011+
for parameter in &signature.parameters {
2012+
let mut part = match &parameter.parameter_type {
2013+
Type::ExternalMethodTypeParameter(_) => "SystemObject".to_string(),
2014+
other => self.extern_type_name(other)?,
2015+
};
2016+
if matches!(
2017+
parameter.passing,
2018+
ParameterPassing::Ref | ParameterPassing::Out
2019+
) {
2020+
part.push_str("Ref");
2021+
}
2022+
parts.push(part);
2023+
}
2024+
let return_part = match &signature.return_type {
2025+
Type::ExternalMethodTypeParameter(_) => "SystemObject".to_string(),
2026+
other => self.extern_type_name(other)?,
2027+
};
2028+
let middle = if parts.is_empty() {
2029+
String::new()
19462030
} else {
1947-
format!("__{name}{middle}__T")
2031+
format!("__{}", parts.join("_"))
19482032
};
1949-
let receiver = receiver.map(|ty| self.substitute(ty, &ctx.key.bindings));
1950-
let owner =
1951-
self.exposed_owner(&declaring, receiver.as_ref(), std::slice::from_ref(&suffix))?;
1952-
Some(format!("{owner}.{suffix}"))
2033+
Some(format!("__{name}{middle}__{return_part}"))
19532034
}
19542035

19552036
pub(super) fn substitute_signature(
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
using MenSharp;
2+
using VRC.Udon;
23

34
public class MenSharpRuntimeCaller : MenSharpBehaviour
45
{
56
public MenSharpRuntimeTarget target;
67
public bool done;
78
public int result;
89

10+
// SetProgramVariable<T> is C# sugar over the non-generic (string, object)
11+
// extern; passing an int[] used to fail because M# tried to name int[] as a
12+
// System.Type. Set on another behaviour, read back by the test.
13+
public UdonBehaviour receiver;
14+
915
public void RunRemote()
1016
{
1117
result = target.Add(20, 22);
1218
done = true;
1319
}
20+
21+
public void PushArray()
22+
{
23+
int[] values = { 7, 8, 9 };
24+
receiver.SetProgramVariable("received", values);
25+
}
1426
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
public class MenSharpRuntimeTarget : MenSharpBehaviour
44
{
5+
// written from another behaviour via SetProgramVariable("received", int[])
6+
public int[] received;
7+
58
public int Add(int left, int right)
69
{
710
return left + right;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ public IEnumerator GeneratedProgramsExecuteInTheSdkUdonVm()
184184
Assert.AreEqual(true, caller.GetProgramVariable("done"));
185185
Assert.AreEqual(42, caller.GetProgramVariable("result"));
186186

187+
// SetProgramVariable("received", int[]) — a generic method that erases
188+
// to the (string, object) extern; passing an array once failed to compile
189+
UdonBehaviour arrayTarget = FindUdon("MenSharpRuntimeTarget");
190+
caller.SetProgramVariable("receiver", arrayTarget);
191+
caller.RunProgram("PushArray");
192+
Assert.AreEqual(new int[] { 7, 8, 9 }, arrayTarget.GetProgramVariable("received"));
193+
187194
// a static field is one for every instance (issue: each instance
188195
// counted from 0), through the holder the scene carries
189196
GameObject holderObject = GameObject.Find(MenSharpProxy.StaticsHolderName);

0 commit comments

Comments
 (0)