Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the Unbox instructions, and a few tests for boxing. #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions GrEmit.Tests/OpCodesTests/Test_Boxing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Reflection.Emit;

using NUnit.Framework;

namespace GrEmit.Tests.OpCodesTests
{
[TestFixture]
public class Test_Boxing
{
[Test]
public void TestUnboxInt_Success()
{
var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(void), new[] {typeof(int)});
var il = new GroboIL(method);

var valueTypeLocal = il.DeclareLocal(typeof(int).MakeByRefType());

il.Ldc_I4(15);
il.Box(typeof(int));
il.Unbox(typeof(int));
il.Stloc(valueTypeLocal);

il.Ret();
Console.WriteLine(il.GetILCode());
}

// Fails because a string cannot be unboxed.
[Test]
public void TestUnboxString_Failure()
{
var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(void), new[] {typeof(int)});
var il = new GroboIL(method);

il.Ldstr("Test String");
Assert.Throws<ArgumentException>(() => il.Unbox(typeof(string)));
}

// Fails because the wrong type is on the stack
[Test]
public void TestUnboxWrong_Stack()
{
var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(void), new[] {typeof(int)});
var il = new GroboIL(method);

il.Ldc_I4(0);
Assert.Throws<InvalidOperationException>(() => il.Unbox(typeof(int)));
}

// Fails because a string cannot be boxed.
[Test]
public void TestBoxString_Failure()
{
var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(void), new[] {typeof(int)});
var il = new GroboIL(method);

il.Ldstr("Test String");
Assert.Throws<ArgumentException>(() => il.Box(typeof(string)));
il.Pop();

il.Ret();
Console.WriteLine(il.GetILCode());
}
}
}
21 changes: 21 additions & 0 deletions GrEmit/GroboIL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,27 @@ public void Isinst(Type type)
throw new ArgumentNullException("type");
Emit(OpCodes.Isinst, type);
}

/// <summary>
/// The unbox instruction converts the object reference (type O), the boxed representation of a value type,
/// to a value type pointer (a managed pointer, type ref), its unboxed form. The supplied value type (valType)
/// is a metadata token indicating the type of value type contained within the boxed object.
///
/// Unlike Box, which is required to make a copy of a value type for use in the object, unbox is not
/// required to copy the value type from the object. Typically it simply computes the address of the value
/// type that is already present inside of the boxed object.
/// </summary>
/// <param name="type">
/// The <see cref="Type">Type</see> of boxed object. Must be a value type.
/// </param>
public void Unbox(Type type)
{
if (type == null)
throw new ArgumentNullException("type");
if (!type.IsValueType && !type.IsGenericParameter)
throw new ArgumentException("A value type expected", "type");
Emit(OpCodes.Unbox, type);
}

/// <summary>
/// Converts the boxed representation of a type specified in the instruction to its unboxed form.
Expand Down
6 changes: 6 additions & 0 deletions GrEmit/StackMutator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ protected static void CheckIsAPointer(GroboIL il, ESType type)
if (cliType != CLIType.Pointer && cliType != CLIType.NativeInt)
ThrowError(il, $"A pointer type expected but was '{type}'");
}

protected static void CheckIsNotValueType(GroboIL il, ESType type)
{
if (type.ToType().IsValueType)
ThrowError(il, $"A reference type expected but was '{type}'");
}

protected static void CheckCanBeAssigned(GroboIL il, Type to, ESType from)
{
Expand Down
1 change: 1 addition & 0 deletions GrEmit/StackMutatorCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public static void Mutate(OpCode opCode, GroboIL il, ILInstructionParameter para
{OpCodes.Ldtoken, new LdtokenStackMutator()},
{OpCodes.Castclass, new CastclassStackMutator()},
{OpCodes.Isinst, new IsinstStackMutator()},
{OpCodes.Unbox, new Unbox_StackMutator()},
{OpCodes.Unbox_Any, new Unbox_AnyStackMutator()},
{OpCodes.Box, new BoxStackMutator()},
{OpCodes.Newobj, new NewobjStackMutator()},
Expand Down
20 changes: 20 additions & 0 deletions GrEmit/StackMutators/Unbox_StackMutator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using GrEmit.InstructionParameters;

namespace GrEmit.StackMutators
{
internal class Unbox_StackMutator : StackMutator
{
public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref EvaluationStack stack)
{
var unboxedValueType = ((TypeILInstructionParameter)parameter).Type;
if (!unboxedValueType.IsValueType)
{
ThrowError(il, $"Unbox may only be used with value types. Passed [{unboxedValueType}]");
}

CheckNotEmpty(il, stack, () => "An object reference to a value type must be put onto the evaluation stack in order to perform the 'unbox' instruction");
CheckIsNotValueType(il, stack.Pop()); // The stack should contain a managed reference type, which we will unbox.
stack.Push(unboxedValueType.MakeByRefType()); // Pushes a reference to the value type onto the stack.
}
}
}