Skip to content

Commit 4f30509

Browse files
committed
Apply current pending changes
1 parent 9af8999 commit 4f30509

22 files changed

Lines changed: 189 additions & 264 deletions

‎docs/api/index.md‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,32 @@ Main entry point API is `Stunt.Of<T>`, which creates an object that implements `
55
```csharp
66
ICalculator calculator = Stunt.Of<ICalculator>();
77

8-
// Extension methods to easily add behaviors without having
9-
// to cast to IStunt
10-
calculator.AddBehavior((invocation, next) => ...);
8+
Console.WriteLine(calculator.Add(2, 5));
9+
```
10+
11+
Behaviors are configured through `IStunt`, which every stunt implements. `Stunt.For<T>`
12+
creates the stunt and returns a `StuntReference<T>` that exposes both the behaviors and
13+
the stunt itself (via `ToObject()`, or an implicit conversion for non-interface types):
14+
15+
```csharp
16+
var stunt = Stunt.For<ICalculator>();
17+
ICalculator calculator = stunt.ToObject();
18+
19+
stunt.AddBehavior((invocation, next) => ...);
1120

1221
Console.WriteLine(calculator.Add(2, 5));
1322
```
1423

24+
`Stunt.Get(calculator)` returns the same `StuntReference<T>` for an already created stunt,
25+
so behaviors can be added after the fact.
26+
1527
There are overloads for implementing additional types, as well as passing constructor
1628
arguments if the base type `T` (which must be the first in the list, like in regular
1729
C# type declarations) is a class that provides a constructor with matching parameters:
18-
`Stunt.Of<T, T1...Tn>(arg1, ... argn)`
30+
`Stunt.Of<T, T1...Tn>(arg1, ... argn)` and `Stunt.For<T, T1...Tn>(arg1, ... argn)`
31+
32+
For anonymous behaviors, the delegate/lambda based overloads are typically sufficient.
33+
For more advanced or reusable behaviors, you can implement `IStuntBehavior` instead.
1934

2035
For anonymous behaviors, the delegate/lambda based overloads are typically sufficient.
2136
For more advanced or reusable behaviors, you can implement `IStuntBehavior` instead.

‎src/Acceptance/Dynamic/Tests.cs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ public class Tests
99
[Fact]
1010
public void CanConfigureDefaultValues()
1111
{
12-
var calculator = Stunt.Of<ICalculator, IDisposable>();
12+
var stunt = Stunt.For<ICalculator, IDisposable>();
13+
ICalculator calculator = stunt.ToObject();
1314

1415
Assert.IsNotType<CompiledStuntFactory>(StuntFactory.Default);
1516

1617
var recorder = new RecordingBehavior();
17-
calculator.AddBehavior(recorder);
18-
calculator.AddBehavior(new DefaultValueBehavior());
18+
stunt.AddBehavior(recorder);
19+
stunt.AddBehavior(new DefaultValueBehavior());
1920

2021
Assert.IsAssignableFrom<IDisposable>(calculator);
2122

‎src/Acceptance/Static/Tests.cs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ public class Tests
99
[Fact]
1010
public void CanConfigureDefaultValues()
1111
{
12-
var calculator = Stunt.Of<ICalculator, IDisposable>();
12+
var stunt = Stunt.For<ICalculator, IDisposable>();
13+
ICalculator calculator = stunt.ToObject();
1314

1415
var recorder = new RecordingBehavior();
1516

16-
calculator.AddBehavior(recorder);
17-
calculator.AddBehavior(new DefaultValueBehavior());
17+
stunt.AddBehavior(recorder);
18+
stunt.AddBehavior(new DefaultValueBehavior());
1819

1920
Assert.IsAssignableFrom<IDisposable>(calculator);
2021

‎src/Stunts.CompiledProxy/Stunts.CompiledProxy.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>

‎src/Stunts.CompiledProxy/Stunts.CompiledProxy.targets‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
<ItemGroup>
1919
<PackageReference Include="NuGetizer" Version="1.5.1" PrivateAssets="all" />
2020
<PackageReference Include="PolySharp" Version="1.16.0" PrivateAssets="all" />
21-
<PackageReference Include="ThisAssembly" Version="2.1.6" PrivateAssets="all" />
2221

2322
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="$(RoslynVersion)$(RoslynLabel)" Pack="false" />
2423
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(RoslynVersion)$(RoslynLabel)" Pack="false" />

‎src/Stunts.Package/Stunts.Package.msbuildproj‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ and runs everywhere, even where run-time code generation (Reflection.Emit) is fo
1010
limitted, like physical iOS devices and game consoles, through compile-time code generation.
1111
The proxy behavior is configured in code using what we call a behavior pipeline.
1212

13-
IMyService service = Stunt.Of&lt;IMyService&gt;();
14-
service.AddBehavior(...);
13+
ICalculator calculator = Stunt
14+
.For&lt;ICalculator&gt;()
15+
.AddBehavior((invocation, next) => ..., appliesTo: invocation => ...)
16+
.ToObject();
1517
</Description>
1618
<PackageTags>dotnet proxy runtime dynamic</PackageTags>
1719
</PropertyGroup>

‎src/Stunts.UnitTests/Castle/DelegateTests.cs‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public void Run()
2727
public void BehaviorHandlesACallWithNoTarget()
2828
{
2929
var seen = false;
30-
var action = Stunt.Of<Action>().AddBehavior((invocation, next) =>
30+
Action action = Stunt.For<Action>().AddBehavior((invocation, next) =>
3131
{
3232
seen = invocation.MethodBase.Name == "Invoke";
3333
return invocation.CreateReturn();
34-
});
34+
}).ToObject();
3535

3636
action();
3737

@@ -42,11 +42,11 @@ public void BehaviorHandlesACallWithNoTarget()
4242
public void NoTargetLeavesTheTailEmpty()
4343
{
4444
bool? hasImplementation = null;
45-
var action = Stunt.Of<Action>().AddBehavior((invocation, next) =>
45+
Action action = Stunt.For<Action>().AddBehavior((invocation, next) =>
4646
{
4747
hasImplementation = invocation.HasImplementation;
4848
return next(invocation, next);
49-
});
49+
}).ToObject();
5050

5151
Assert.Throws<NotImplementedException>(() => action());
5252
Assert.False(hasImplementation);
@@ -56,7 +56,7 @@ public void ImplementationIsTheTail()
5656
{
5757
var called = false;
5858
Action implementation = () => called = true;
59-
var action = Stunt.Of<Action>(implementation);
59+
Action action = Stunt.Of<Action>(implementation);
6060

6161
action();
6262

@@ -68,13 +68,13 @@ public void BehaviorWrapsTheImplementation()
6868
{
6969
var order = new List<string>();
7070
Action implementation = () => order.Add("impl");
71-
var action = Stunt.Of<Action>(implementation).AddBehavior((invocation, next) =>
71+
Action action = Stunt.For<Action>(implementation).AddBehavior((invocation, next) =>
7272
{
7373
order.Add("before");
7474
var result = next(invocation, next);
7575
order.Add("after");
7676
return result;
77-
});
77+
}).ToObject();
7878

7979
action();
8080

@@ -85,11 +85,11 @@ public void BehaviorCanReplaceAnArgument()
8585
{
8686
var seen = 0;
8787
Action<int> implementation = value => seen = value;
88-
var action = Stunt.Of<Action<int>>(implementation).AddBehavior((invocation, next) =>
88+
Action<int> action = Stunt.For<Action<int>>(implementation).AddBehavior((invocation, next) =>
8989
{
9090
invocation.Arguments.SetValue("obj", 41);
9191
return next(invocation, next);
92-
});
92+
}).ToObject();
9393

9494
action(1);
9595

@@ -104,11 +104,11 @@ public void RefAndReturnAreWrittenBack()
104104
value = result;
105105
return result;
106106
};
107-
var action = Stunt.Of<RefImpl>(implementation).AddBehavior((invocation, next) =>
107+
RefImpl action = Stunt.For<RefImpl>(implementation).AddBehavior((invocation, next) =>
108108
{
109109
invocation.Arguments.SetValue("value", 4);
110110
return next(invocation, next);
111-
});
111+
}).ToObject();
112112

113113
var value = 1;
114114
var returned = action(ref value);
@@ -130,7 +130,7 @@ public void ParameterNamedMethodForwards()
130130
{
131131
var seen = 0;
132132
Named implementation = method => seen = method;
133-
var action = Stunt.Of<Named>(implementation);
133+
Named action = Stunt.Of<Named>(implementation);
134134

135135
action(7);
136136

@@ -141,7 +141,7 @@ public void ParametersThatHideFieldsForward()
141141
{
142142
var seen = "";
143143
HidesFields target = (left, right) => seen = left + ":" + right;
144-
var action = Stunt.Of<HidesFields>(target);
144+
HidesFields action = Stunt.Of<HidesFields>(target);
145145

146146
action(2, 9);
147147

@@ -160,7 +160,7 @@ public void Run()
160160
{
161161
var called = false;
162162
Hidden implementation = () => called = true;
163-
var action = Stunt.Of<Hidden>(implementation);
163+
Hidden action = Stunt.Of<Hidden>(implementation);
164164

165165
action();
166166

‎src/Stunts.UnitTests/Castle/LanguageFeaturesTests.cs‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,51 @@ public void Run()
1818

1919
public void CovariantReturnUsesTheDerivedSignature()
2020
{
21-
var stunt = Stunt.Of<StringReturn>();
21+
var stunt = Stunt.For<StringReturn>();
22+
StringReturn value = stunt.ToObject();
2223

23-
Assert.Equal("derived", stunt.Value());
24+
Assert.Equal("derived", value.Value());
2425

2526
stunt.AddBehavior((invocation, next) => invocation.CreateValueReturn("proxy"));
2627

27-
Assert.Equal("proxy", stunt.Value());
28+
Assert.Equal("proxy", value.Value());
2829
}
2930

3031
public void PositionalRecordConstructorAndVirtualMethod()
3132
{
32-
var stunt = Stunt.Of<Person>("Ada");
33+
var stunt = Stunt.For<Person>("Ada");
34+
Person person = stunt.ToObject();
3335

34-
Assert.Equal("Ada", stunt.Name);
35-
Assert.Equal("Hello Ada", stunt.Greet());
36+
Assert.Equal("Ada", person.Name);
37+
Assert.Equal("Hello Ada", person.Greet());
3638

3739
stunt.AddBehavior((invocation, next) =>
3840
invocation.MethodBase.Name == nameof(Person.Greet)
3941
? invocation.CreateValueReturn("hi")
4042
: next(invocation, next));
4143

42-
Assert.Equal("hi", stunt.Greet());
43-
Assert.Equal("Ada", stunt.Name);
44+
Assert.Equal("hi", person.Greet());
45+
Assert.Equal("Ada", person.Name);
4446

45-
var copy = stunt with { Name = "Bea" };
47+
var copy = person with { Name = "Bea" };
4648

4749
Assert.Equal("Bea", copy.Name);
48-
Assert.IsType(stunt.GetType(), copy);
50+
Assert.IsType(person.GetType(), copy);
4951
// The synthesized record copy copies the pipeline field.
5052
Assert.Equal("hi", copy.Greet());
5153
}
5254

5355
public void EmptyRecord()
5456
{
55-
var stunt = Stunt.Of<Empty>();
57+
Empty stunt = Stunt.Of<Empty>();
5658

5759
Assert.IsAssignableFrom<Empty>(stunt);
5860
Assert.IsAssignableFrom<IStunt>(stunt);
5961
}
6062

6163
public void RecordCanTakeAnAdditionalInterface()
6264
{
63-
var stunt = Stunt.Of<Person, IDisposable>("Ada");
65+
Person stunt = Stunt.Of<Person, IDisposable>("Ada");
6466

6567
Assert.IsAssignableFrom<Person>(stunt);
6668
Assert.IsAssignableFrom<IDisposable>(stunt);

‎src/Stunts.UnitTests/Castle/Unsupported.md‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The tests next to this file are the Castle scenarios the generator can carry. Th
3131
| Covariant returns | `LanguageFeaturesTests` |
3232
| Records (positional, empty, record plus an extra interface; `with` keeps the stunt type) | `LanguageFeaturesTests` |
3333
| `ref readonly`, `Span<T>` and other `ref struct` arguments and returns, pointer parameters | `Scenarios/SignatureRefs` |
34+
| Custom attributes on generated types, members, parameters, and returns; optional parameter defaults (including reflection invocation) | `AttributeTests` |
3435

3536
Behaviors added with `AddBehavior` after `Stunt.Of` do not see virtual calls made from a base constructor. Castle passes interceptors into proxy creation, so they do. `BehaviorPipelineFactory` is the matching extension point, and `ClassProxyTests` uses it.
3637

@@ -46,24 +47,28 @@ Castle's `IProxyGenerationHook` omits members from the proxy type. Stunts overri
4647

4748
### Metadata Castle copies onto the proxy
4849

49-
The scaffold copies signatures, names, and generic constraints. It does not copy:
50+
The generator copies C#-expressible metadata, including custom attributes and optional
51+
parameter default values (covered by `AttributeTests`). Remaining metadata gaps are:
5052

51-
- custom attributes on methods, properties, parameters, or the type (`[Serializable]`, `[Guid]`, parameter attributes)
52-
- metadata-only custom modifiers (`modopt` / `modreq`) that have no C# spelling. `in`, `ref readonly`, and function-pointer calling conventions are emitted as those keywords, and the compiler writes the matching modifier
53-
- optional-parameter default values (callers compiled against the original type still get the compiler default; reflection on the proxy method does not)
53+
- metadata-only custom modifiers (`modopt` / `modreq`) with no C# spelling
5454
- extra attributes supplied at generation time (`ProxyGenerationOptions.AdditionalAttributes`)
5555

5656
### Serialization
5757

58-
Castle's proxy serialization (BinaryFormatter, `ISerializable`, restoring interceptors, mixins, selector, and generation options, including graphs with delegates back to the proxy) is a generator feature. Compile-time stunts are ordinary classes with none of that infrastructure. XML serialization of a proxy is the same kind of gap: the generated type does not replicate the attributes and constructor shape `XmlSerializer` expects.
58+
Castle-specific proxy serialization (`ISerializable`, restoring interceptors, mixins,
59+
selector, and generation options, including graphs with delegates back to the proxy) is
60+
not implemented by compile-time stunts. Copying `[Serializable]` and other attributes
61+
does not provide that proxy-serialization infrastructure.
5962

6063
### Delegates as additional type arguments
6164

6265
`Stunt.Of<SomeDelegate>()` returns a delegate bound to a generated `Invoke`, and `Stunt.Of<SomeDelegate>(implementation)` forwards `next` to that delegate. A delegate combined with a class or another interface is `ST010`. Castle can also mix a delegate `Invoke` onto some other proxy (`AsDelegate` is the missing cast).
6366

64-
### Same signature on two interfaces
67+
### Cannot distinguish identical interface slots
6568

66-
One public method implements both. Both calls are intercepted, and a behavior cannot tell which interface was used. Castle emits explicit implementations so `IInvocation.Method` differs.
69+
Identical signatures from two interfaces share one generated implementation. Both calls
70+
are intercepted, but a behavior cannot tell which interface slot was called. Castle can
71+
emit explicit implementations so `IInvocation.Method` differs.
6772

6873
### Fixed additional-interface arity
6974

‎src/Stunts.UnitTests/RefReturns.cs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ public class RefReturns
99
[Fact]
1010
public void CanReturnRef()
1111
{
12-
INumbers stunt = new NumbersStunt();
13-
12+
var stunt = new NumbersStunt();
1413

1514
Ref<int> value = 25;
1615

1716
stunt.AddBehavior((invocation, next) => invocation.CreateValueReturn(value, invocation.Arguments));
1817

18+
INumbers numbers = stunt;
19+
1920
var index = 0;
20-
ref int v = ref stunt.NumberAt(ref index, out var count);
21+
ref int v = ref numbers.NumberAt(ref index, out var count);
2122

2223
v = 42;
2324

0 commit comments

Comments
 (0)