我正在使用 编译器System.Reflection.Emit
,并且遇到了我无法弄清楚的 JIT 限制错误。问题出现在我的函数句柄实现中。即生成代码
function foo() { }
f = foo;
f();
f
由于规范超出了我的控制范围,语言是动态类型的,所以我不知道在编译时需要多少参数。为了解决这个问题,我没有发出Ldftn
for foo
,而是生成了一个新方法 ,λfoo
它接受调用表达式中给出的参数数组并将它们推送到 for 的 eval 堆栈中foo
。这在 CLR 中是否允许?
我现在得到的是“JIT 遇到内部限制”异常(或者“CLR 检测到无效程序”,如果我保存程序集并运行它而不是从内存中调用它),堆栈跟踪显示它发生在λfoo
. 这是我正在生成的 IL。
.method private instance class [MylibInterop]MylibInterop.MylibValue
'λfoo'(class [MylibInterop]MylibInterop.MylibValue[] A_1) cil managed
{
// Code size 90 (0x5a)
.maxstack 10
.locals init (int32 V_0,
int32 V_1)
IL_0000: ldarg.1
IL_0001: call instance int32 [mscorlib]System.Array::get_Length()
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: ldc.i4 0x0
IL_000d: ble IL_001d
IL_0012: ldstr "Too many arguments to lambda call"
IL_0017: newobj instance void [mscorlib]System.Exception::.ctor(string)
IL_001c: throw
IL_001d: ldarg.0
IL_001e: ldc.i4.0
IL_001f: stloc.1
IL_0020: ldloc.0
IL_0021: newobj instance void [MylibInterop]MylibInterop.MylibValue::.ctor(int32)
IL_0026: ldloc.1
IL_0027: ldloc.0
IL_0028: bge IL_003d
IL_002d: ldarg.1
IL_002e: ldloc.1
IL_002f: ldelem [MylibInterop]MylibInterop.MylibValue
IL_0034: ldloc.1
IL_0035: ldc.i4.1
IL_0036: add
IL_0037: stloc.1
IL_0038: br IL_0026
IL_003d: ldloc.0
IL_003e: stloc.1
IL_003f: ldloc.1
IL_0040: ldc.i4 0x0
IL_0045: bge IL_0054
IL_004a: ldnull
IL_004b: ldloc.1
IL_004c: ldc.i4.1
IL_004d: add
IL_004e: stloc.1
IL_004f: br IL_003f
IL_0054: call instance class [MylibInterop]MylibInterop.MylibValue debug.Program::foo(class [MylibInterop]MylibInterop.MylibValue)
IL_0059: ret
} // end of method Program::'λfoo'