-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract LocalBuilder, emit LocalBuilder in ILGenerator
- Loading branch information
Showing
21 changed files
with
371 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 0 additions & 37 deletions
37
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/LocalBuilder.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/LocalBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Reflection.Emit | ||
{ | ||
public abstract class LocalBuilder : LocalVariableInfo | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="LocalBuilder"/> class. | ||
/// </summary> | ||
/// <remarks> | ||
/// This constructor is invoked by derived classes. | ||
/// </remarks> | ||
protected LocalBuilder() { } | ||
|
||
/// <summary> | ||
/// Returns the method where the local variable declared. | ||
/// </summary> | ||
/// <remarks>Can be used for validating the containing method when referencing the local.</remarks> | ||
public abstract MethodInfo Method { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/libraries/System.Reflection.Emit.ILGeneration/tests/LocalBuilder/LocalBuilderMethod.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
|
||
namespace System.Reflection.Emit.Tests | ||
{ | ||
// Mono doesn't support LocalBuilder.Method | ||
[ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMonoRuntime))] | ||
public class LocalBuilderMethod | ||
{ | ||
[Fact] | ||
public void LocalBuilder_MethodReturnsCorrectMethod() | ||
{ | ||
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); | ||
MethodBuilder method1 = type.DefineMethod("Method1", MethodAttributes.Public); | ||
MethodBuilder method2 = type.DefineMethod("Method2", MethodAttributes.Public); | ||
ILGenerator ilGenerator1 = method1.GetILGenerator(); | ||
ILGenerator ilGenerator2 = method2.GetILGenerator(); | ||
LocalBuilder local1 = ilGenerator1.DeclareLocal(typeof(int)); | ||
LocalBuilder local2 = ilGenerator2.DeclareLocal(typeof(string), true); | ||
|
||
Assert.Equal(method1, local1.Method); | ||
Assert.Equal(method2, local2.Method); | ||
} | ||
|
||
[Fact] | ||
public void LocalBuilder_UseLocalsThatBelongsToTheMethod() | ||
{ | ||
TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic); | ||
MethodBuilder method1 = type.DefineMethod("Method1", MethodAttributes.Public); | ||
MethodBuilder method2 = type.DefineMethod("Method2", MethodAttributes.Public); | ||
ILGenerator ilGenerator1 = method1.GetILGenerator(); | ||
ILGenerator ilGenerator2 = method2.GetILGenerator(); | ||
LocalBuilder local1 = ilGenerator1.DeclareLocal(typeof(short)); | ||
LocalBuilder local2 = ilGenerator2.DeclareLocal(typeof(long), true); | ||
|
||
ilGenerator1.Emit(OpCodes.Ldloc, local1); | ||
ilGenerator2.Emit(OpCodes.Ldloc, local2); | ||
|
||
Assert.Throws<ArgumentException>(() => ilGenerator1.Emit(OpCodes.Ldloc, local2)); | ||
Assert.Throws<ArgumentException>(() => ilGenerator2.Emit(OpCodes.Ldloc, local1)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/LocalBuilderImpl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Reflection.Emit | ||
{ | ||
internal sealed class LocalBuilderImpl : LocalBuilder | ||
{ | ||
#region Private Data Members | ||
private readonly int _localIndex; | ||
private readonly Type _localType; | ||
private readonly MethodInfo _method; | ||
private readonly bool _isPinned; | ||
#endregion | ||
|
||
#region Constructor | ||
internal LocalBuilderImpl(int index, Type type, MethodInfo method, bool isPinned) | ||
{ | ||
_isPinned = isPinned; | ||
_localIndex = index; | ||
_localType = type; | ||
_method = method; | ||
} | ||
#endregion | ||
|
||
#region LocalVariableInfo Override | ||
public override bool IsPinned => _isPinned; | ||
public override Type LocalType => _localType; | ||
public override int LocalIndex => _localIndex; | ||
public override MethodInfo Method => _method; | ||
#endregion | ||
} | ||
} |
Oops, something went wrong.