Skip to content

Commit 136ac5d

Browse files
committed
fix extern whitelist check order
1 parent b83ca21 commit 136ac5d

6 files changed

Lines changed: 119 additions & 10 deletions

File tree

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,30 @@ impl<'a, 'ast> Generator<'a, 'ast> {
22172217
names
22182218
}
22192219

2220+
/// The type an external member's extern is named after: the declaring
2221+
/// type itself when the whitelist has the member there, else the nearest
2222+
/// base class that does, else the declaring type (so the error names the
2223+
/// obvious signature). `suffixes` are the member's spellings after the
2224+
/// `Type.` part (`__LoadURL__VRCSDKBaseVRCUrl__SystemVoid`; a getter and
2225+
/// a setter for a property).
2226+
///
2227+
/// The SDK registers a virtual member once, on the class that introduces
2228+
/// it: `LoadURL` is `BaseVRCVideoPlayer`'s extern, and the
2229+
/// `VRCAVProVideoPlayer` override a call resolves to has none of its
2230+
/// own. Udon takes the derived instance for the base's extern.
2231+
pub(super) fn exposed_owner(&self, declaring: &Type, suffixes: &[String]) -> Option<String> {
2232+
let chain = self.external_chain(declaring);
2233+
chain
2234+
.iter()
2235+
.find(|owner| {
2236+
suffixes
2237+
.iter()
2238+
.any(|suffix| self.nodes.has_signature(&format!("{owner}.{suffix}")))
2239+
})
2240+
.cloned()
2241+
.or_else(|| chain.into_iter().next())
2242+
}
2243+
22202244
/// Does a value of this type live behind a reference? Structs and enums do
22212245
/// not, and comparing two boxed copies of one by identity would be wrong.
22222246
pub(super) fn is_reference_type(&self, ty: &Type) -> bool {

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,7 +2402,18 @@ impl<'a, 'ast> Generator<'a, 'ast> {
24022402
{
24032403
return place;
24042404
}
2405-
let Some(owner) = self.extern_type_name(&declaring) else {
2405+
// the owner is whichever class up the chain the whitelist
2406+
// has the accessor on (see exposed_owner); a getter, a
2407+
// property setter or a struct field setter all count
2408+
let accessors: Vec<String> = match self.extern_type_name(&member_type) {
2409+
Some(value) => vec![
2410+
format!("__get_{}__{value}", external.name),
2411+
format!("__set_{}__{value}__SystemVoid", external.name),
2412+
format!("__set_{}__{value}", external.name),
2413+
],
2414+
None => Vec::new(),
2415+
};
2416+
let Some(owner) = self.exposed_owner(&declaring, &accessors) else {
24062417
self.error(
24072418
ctx,
24082419
Message::key("codegen.this_type_is_not_available_on_udon"),
@@ -4745,14 +4756,14 @@ impl<'a, 'ast> Generator<'a, 'ast> {
47454756
};
47464757
let name = member.name.replace('.', "");
47474758
let declaring = self.substitute(&call.declaring_type, &ctx.key.bindings);
4748-
let Some(owner) = self.extern_type_name(&declaring) else {
4759+
if self.extern_type_name(&declaring).is_none() {
47494760
self.error(
47504761
ctx,
47514762
Message::key("codegen.this_indexer_s_declaring_type_cannot_be"),
47524763
span,
47534764
);
47544765
return Place::Error;
4755-
};
4766+
}
47564767
let signature = self.substitute_signature(&call.signature, &ctx.key.bindings);
47574768
let mut indices: Vec<(DataId, Type)> = Vec::new();
47584769
for (position, argument) in arguments.iter().enumerate() {
@@ -4778,12 +4789,34 @@ impl<'a, 'ast> Generator<'a, 'ast> {
47784789
};
47794790
indices.push((value, target));
47804791
}
4792+
let ty = self.substitute(&signature.return_type, &ctx.key.bindings);
4793+
// the owner is whichever class up the chain the whitelist has the
4794+
// accessor on (see exposed_owner)
4795+
// (an index type without a spelling is reported when the place is
4796+
// read or written, not here)
4797+
let index_names: Option<Vec<String>> = indices
4798+
.iter()
4799+
.map(|(_, index_type)| self.extern_type_name(index_type))
4800+
.collect();
4801+
let accessors: Vec<String> = match (self.extern_type_name(&ty), index_names) {
4802+
(Some(value), Some(parts)) => {
4803+
let parts = parts.join("_");
4804+
vec![
4805+
format!("__get_{name}__{parts}__{value}"),
4806+
format!("__set_{name}__{parts}_{value}__SystemVoid"),
4807+
]
4808+
}
4809+
_ => Vec::new(),
4810+
};
4811+
let Some(owner) = self.exposed_owner(&declaring, &accessors) else {
4812+
return Place::Error;
4813+
};
47814814
Place::ExternalIndexer {
47824815
receiver,
47834816
owner,
47844817
name,
47854818
indices,
4786-
ty: self.substitute(&signature.return_type, &ctx.key.bindings),
4819+
ty,
47874820
}
47884821
}
47894822

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,14 +1866,14 @@ impl<'a, 'ast> Generator<'a, 'ast> {
18661866
return None;
18671867
};
18681868
let declaring = self.substitute(&call.declaring_type, &ctx.key.bindings);
1869-
let Some(owner) = self.extern_type_name(&declaring) else {
1869+
if self.extern_type_name(&declaring).is_none() {
18701870
self.error(
18711871
ctx,
18721872
Message::key("codegen.this_call_s_declaring_type_cannot_be"),
18731873
span.clone(),
18741874
);
18751875
return None;
1876-
};
1876+
}
18771877
let name = member.name.replace('.', "");
18781878
let signature = self.substitute_signature(&call.signature, &ctx.key.bindings);
18791879
let mut parts: Vec<String> = Vec::new();
@@ -1919,10 +1919,13 @@ impl<'a, 'ast> Generator<'a, 'ast> {
19191919
// Udon has no generics: a generic method is one extern named `…__T`
19201920
// that takes its type argument as an ordinary `System.Type` value.
19211921
// The caller supplies that value; see emit_call.
1922-
if !call.type_arguments.is_empty() {
1923-
return Some(format!("{owner}.__{name}{middle}__T"));
1924-
}
1925-
Some(format!("{owner}.__{name}{middle}__{return_part}"))
1922+
let suffix = if call.type_arguments.is_empty() {
1923+
format!("__{name}{middle}__{return_part}")
1924+
} else {
1925+
format!("__{name}{middle}__T")
1926+
};
1927+
let owner = self.exposed_owner(&declaring, std::slice::from_ref(&suffix))?;
1928+
Some(format!("{owner}.{suffix}"))
19261929
}
19271930

19281931
pub(super) fn substitute_signature(
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// MenSharp verification: members a VRC video player inherits from
2+
// BaseVRCVideoPlayer.
3+
//
4+
// The SDK registers `LoadURL`, `Loop` and the rest once, on the abstract base
5+
// class; the AVPro and Unity players only override them. A call through the
6+
// derived type has to compile against the base's extern (as UdonSharp does),
7+
// not against a derived extern that does not exist.
8+
//
9+
// Compile-only: the integration suite asserts this fixture yields a program
10+
// asset; the runtime fixture list leaves it out, as a player has nothing to
11+
// load in batchmode.
12+
13+
using MenSharp;
14+
using UnityEngine;
15+
using VRC.SDK3.Video.Components;
16+
using VRC.SDK3.Video.Components.AVPro;
17+
using VRC.SDKBase;
18+
19+
public class VerifyVideo : MenSharpBehaviour
20+
{
21+
[SerializeField]
22+
private VRCAVProVideoPlayer avpvp;
23+
24+
[SerializeField]
25+
private VRCUnityVideoPlayer uvp;
26+
27+
[SerializeField]
28+
private VRCUrl url;
29+
30+
public void Interact()
31+
{
32+
avpvp.LoadURL(url);
33+
uvp.LoadURL(url);
34+
uvp.Loop = !avpvp.Loop;
35+
Debug.Log($"[verify-video] playing: {avpvp.IsPlaying} {uvp.IsPlaying}");
36+
}
37+
}

‎tests/unity-project/Assets/MenSharp/VerifyVideo.cs.meta‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public class MenSharpIntegrationTests
6060
"VerifyTuple",
6161
"VerifyUdonSharp",
6262
"VerifyUsharpCompat",
63+
"VerifyVideo",
6364
"VerifyWake",
6465
};
6566

0 commit comments

Comments
 (0)