John Lam 1997 — 2026

Automatic boxing in RubyCLR

I spent some time this morning thinking about how to fix the boxing showstopper bug in RubyCLR. Here’s a simple repro:

a = ArrayList.new
p = Point.new(3, 4)
a.add(p)

This problem is so much harder than it looks. ArrayList.Add expects a reference type, but p contains a value type. RubyCLR respects value type semantics, so the Ruby VALUE object contains the opaque blob of data that is a Point struct.

The problem is: how do I marshal this thing to the CLR? The way you marshal a value type to the CLR is to explicitly box it. The CIL instruction sequence looks like:

ldarg_s   value
ldc_i4_s  16
add
ldobj      'System.Point'
box        'System.Point'

A Ruby VALUE object is an opaque blob of memory. Ruby and RubyCLR store some other stuff in the first 16 bytes, so I have to add that offset first before loading the value type object onto the stack.

But here’s the problem: both the ldobj and the box instructions require a type parameter. This code sequence must execute within my marshaling shim that is generated at runtime. I don’t know what the type of value is at shim generation (aka compile) time.

It looks like I have to generate specific boxing functions for each value type and cache them at runtime.

Unless someone has a better suggestion?

4 archived reader comments

Jeroen Frijters (2006-05-24)

I can’t find anything either. It looks like a missed opportunity for TypedReference, it should clearly have an overload of MakeTypedReference that takes a Type and a void*.

Not recommend for production (duh!), but for fun I did hack something together:

using System;

unsafe class EvilCode { static void Main(string[] args) { int val = 42; Console.WriteLine(GenericBox(typeof(int), &val)); }

static object GenericBox(Type type, void* pValue)
{
    TypedReference r;
    EvilHack(&r, type, pValue);
    return TypedReference.ToObject(r);
}

struct HackStruct
{
    internal IntPtr Value;
    internal IntPtr Type;
}

static void EvilHack(void* p, Type type, void* pValue)
{
    HackStruct* pStruct = (HackStruct*)p;
    pStruct->Value = (IntPtr)pValue;
    pStruct->Type = type.TypeHandle.Value;
}

}

Craig (2006-05-24)

Managed C++ punted by writing a __box instruction, which you needed to call explicitly. So could you give all your value types a box method that would return a boxed reference? It’s not automatic, but it’s simple, and it would work. Wouldn’t it?

gertjan (2006-05-24)

About EvilCode: add a check to verify that you’re indeed dealing with a value type, and the thing becomes a lot less evil, doesn’t it? That is, unless the layout of TypeReference changes in the future, but I seriously doubt that..

John Lam (2006-05-24)

Hi Jeroen, nice to see you around here again!

Wow. That is a scary hack! Let me ping some guys on the CLR dynamic languages team with your hack to see if it stands a chance of working under a reasonable set of conditions. I wonder how TypedReference.ToObject() is actuallly implemented. I guess I’d have to take a peek at the sources to find out.

Craig - I basically implemented this already yesterday, but as a Ruby extension method for value types. So you could write something to explicitly box that would look like this:

a = ArrayList.new p = Point.new(3, 4) a.add(p.box)

The problem there was that the box() method would have to marshal the resulting boxed value type back to Ruby as a VALUE, before marshaling it back to the CLR as an Object reference during the actual method call. Since this is on the rate-determining step of the marshaler, I think I’d have better luck just generating and caching the boxing shims in the marshaler itself.