|
1 | 1 | //! Function instances: scheduling, frames, bodies, calls and virtual dispatch. |
2 | 2 |
|
3 | 3 | use men_sharp_parser::ast::{AccessorKind, Modifier}; |
4 | | -use men_sharp_semantics::{FunctionSignature, ParameterPassing}; |
| 4 | +use men_sharp_semantics::{ExternalMember, FunctionSignature, ParameterPassing}; |
5 | 5 |
|
6 | 6 | use super::*; |
7 | 7 |
|
@@ -1877,13 +1877,20 @@ impl<'a, 'ast> Generator<'a, 'ast> { |
1877 | 1877 | /// static type the call was made on, when it was made on a value: the |
1878 | 1878 | /// whitelist may have the member there rather than on the declaring |
1879 | 1879 | /// 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. |
1880 | 1887 | pub(super) fn external_signature( |
1881 | 1888 | &mut self, |
1882 | 1889 | ctx: &Ctx, |
1883 | 1890 | call: &ResolvedCall, |
1884 | 1891 | receiver: Option<&Type>, |
1885 | 1892 | span: &Range<usize>, |
1886 | | - ) -> Option<String> { |
| 1893 | + ) -> Option<(String, bool)> { |
1887 | 1894 | let MemberOrigin::External { member, .. } = &call.origin else { |
1888 | 1895 | return None; |
1889 | 1896 | }; |
@@ -1938,18 +1945,92 @@ impl<'a, 'ast> Generator<'a, 'ast> { |
1938 | 1945 | } else { |
1939 | 1946 | format!("__{}", parts.join("_")) |
1940 | 1947 | }; |
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 ¶meter.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() |
1946 | 2030 | } else { |
1947 | | - format!("__{name}{middle}__T") |
| 2031 | + format!("__{}", parts.join("_")) |
1948 | 2032 | }; |
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}")) |
1953 | 2034 | } |
1954 | 2035 |
|
1955 | 2036 | pub(super) fn substitute_signature( |
|
0 commit comments