Correct convenience wrapper implementation

This commit is contained in:
Stefanos A 2013-12-04 22:26:30 +01:00
parent e2801c8c6c
commit 7487aab4d9

View file

@ -225,7 +225,6 @@ namespace OpenTK.Rewrite
// Create body for method // Create body for method
static void ProcessMethod(MethodDefinition wrapper, MethodDefinition native, int slot, FieldDefinition entry_points) static void ProcessMethod(MethodDefinition wrapper, MethodDefinition native, int slot, FieldDefinition entry_points)
{ {
var nint = wrapper.DeclaringType.Module.Import(mscorlib.MainModule.GetType("System.IntPtr"));
var body = wrapper.Body; var body = wrapper.Body;
var il = body.GetILProcessor(); var il = body.GetILProcessor();
var instructions = body.Instructions; var instructions = body.Instructions;
@ -233,13 +232,15 @@ namespace OpenTK.Rewrite
// Declare pinned variables for every reference and array parameter // Declare pinned variables for every reference and array parameter
// and push each parameter on the stack // and push each parameter on the stack
EmitParameters(wrapper, nint, body, il);
// Patch convenience wrappers // Patch convenience wrappers
if (wrapper.Parameters.Count < native.Parameters.Count) if (wrapper.Parameters.Count == native.Parameters.Count)
{ {
int parameter_count = EmitParameters(wrapper, nint, body, il); EmitParameters(wrapper, body, il);
int difference = native.Parameters.Count - parameter_count; }
else
{
int difference = native.Parameters.Count - wrapper.Parameters.Count;
EmitConvenienceWrapper(wrapper, native, difference, body, il); EmitConvenienceWrapper(wrapper, native, difference, body, il);
} }
@ -253,11 +254,15 @@ namespace OpenTK.Rewrite
{ {
EmitReturnTypeWrapper(wrapper, native, body, il); EmitReturnTypeWrapper(wrapper, native, body, il);
} }
if (wrapper.Parameters.Any(p => p.ParameterType.Name == "StringBuilder")) if (wrapper.Parameters.Any(p => p.ParameterType.Name == "StringBuilder"))
{ {
EmitStringBuilderEpilogue(wrapper, native, body, il); EmitStringBuilderEpilogue(wrapper, native, body, il);
} }
if (wrapper.Parameters.Any(p => p.ParameterType.Name == "StringBuilder" && p.ParameterType.IsArray))
{
EmitStringArrayEpilogue(wrapper, native, body, il);
}
// return // return
il.Emit(OpCodes.Ret); il.Emit(OpCodes.Ret);
@ -408,6 +413,13 @@ namespace OpenTK.Rewrite
private static void EmitConvenienceWrapper(MethodDefinition wrapper, private static void EmitConvenienceWrapper(MethodDefinition wrapper,
MethodDefinition native, int difference, MethodBody body, ILProcessor il) MethodDefinition native, int difference, MethodBody body, ILProcessor il)
{
if (wrapper.Parameters.Count > 1)
{
EmitParameters(wrapper, body, il);
}
if (wrapper.ReturnType.Name != "Void")
{ {
if (difference == 2) if (difference == 2)
{ {
@ -419,11 +431,11 @@ namespace OpenTK.Rewrite
// calli GenTextures(n, &textures); // calli GenTextures(n, &textures);
// return result; // return result;
// } // }
body.Variables.Add(new VariableDefinition(native.Parameters.Last().ParameterType)); body.Variables.Add(new VariableDefinition(wrapper.ReturnType));
il.Emit(OpCodes.Ldc_I4, 1); // const int n = 1 il.Emit(OpCodes.Ldc_I4, 1); // const int n = 1
il.Emit(OpCodes.Ldloca, body.Variables.Count - 1); // &buffers il.Emit(OpCodes.Ldloca, body.Variables.Count - 1); // &buffers
} }
else if (difference == 1 && wrapper.ReturnType.Name != "Void") else if (difference == 1)
{ {
// Convert unsized out-array/reference to return value, for example: // Convert unsized out-array/reference to return value, for example:
// void GetBoolean(GetPName pname, out bool data) -> bool GetBoolean(GetPName pname) // void GetBoolean(GetPName pname, out bool data) -> bool GetBoolean(GetPName pname)
@ -435,7 +447,14 @@ namespace OpenTK.Rewrite
body.Variables.Add(new VariableDefinition(wrapper.ReturnType)); body.Variables.Add(new VariableDefinition(wrapper.ReturnType));
il.Emit(OpCodes.Ldloca, body.Variables.Count - 1); il.Emit(OpCodes.Ldloca, body.Variables.Count - 1);
} }
else if (difference == 1 && wrapper.ReturnType.Name == "Void") else
{
Console.Error.WriteLine("Unknown wrapper type for ({0})", native.Name);
}
}
else
{
if (difference == 1)
{ {
// Convert in-array/reference to single element, for example: // Convert in-array/reference to single element, for example:
// void DeleteTextures(int n, ref int textures) -> void DeleteTexture(int texture) // void DeleteTextures(int n, ref int textures) -> void DeleteTexture(int texture)
@ -451,8 +470,9 @@ namespace OpenTK.Rewrite
Console.Error.WriteLine("Unknown wrapper type for ({0})", native.Name); Console.Error.WriteLine("Unknown wrapper type for ({0})", native.Name);
} }
} }
}
static int EmitParameters(MethodDefinition method, TypeReference nint, MethodBody body, ILProcessor il) static int EmitParameters(MethodDefinition method, MethodBody body, ILProcessor il)
{ {
int i; int i;
for (i = 0; i < method.Parameters.Count; i++) for (i = 0; i < method.Parameters.Count; i++)
@ -479,7 +499,7 @@ namespace OpenTK.Rewrite
// IntPtr ptr; // IntPtr ptr;
var variable_name = p.Name + " _sb_ptr"; var variable_name = p.Name + " _sb_ptr";
body.Variables.Add(new VariableDefinition(variable_name, nint)); body.Variables.Add(new VariableDefinition(variable_name, TypeIntPtr));
int index = body.Variables.Count - 1; int index = body.Variables.Count - 1;
// ptr = Marshal.AllocHGlobal(sb.Capacity + 1); // ptr = Marshal.AllocHGlobal(sb.Capacity + 1);