本文整理汇总了C#中ILGen.EmitCall方法的典型用法代码示例。如果您正苦于以下问题:C# ILGen.EmitCall方法的具体用法?C# ILGen.EmitCall怎么用?C# ILGen.EmitCall使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ILGen
的用法示例。
在下文中一共展示了ILGen.EmitCall方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compile
public override void Compile(Executive engine, object form, ILGen il, LocalAccess locals, Stack<Type> st, object[] args)
{
if (args.Length != 2)
throw new ArgumentException("Unproperly formated cast expression");
engine.CompileExpr(il, locals, st, args[0]);
Type type = args[1] as Type;
if (type == null)
throw new ArgumentException("Expecting type value");
Type curr_type = st.Pop();
if (curr_type.IsValueType)
{
if (type == typeof(System.Object))
il.EmitBoxing(curr_type);
else if (curr_type != type)
throw new InvalidCastException();
}
else
{
il.Emit(OpCodes.Isinst, type);
il.Emit(OpCodes.Dup);
Label ok = il.DefineLabel();
il.Emit(OpCodes.Brtrue_S, ok);
il.EmitCall(_raiseInvalidCast);
il.MarkLabel(ok);
if (type.IsValueType)
il.EmitUnbox(type);
}
st.Push(type);
}
示例2: Create
internal static Type Create(GenContext context, Type baseClass)
{
//ModuleBuilder mb = context.ModuleBldr;
string name = baseClass.Name + "_impl";
//TypeBuilder baseTB = context.ModuleBldr.DefineType(name, TypeAttributes.Class | TypeAttributes.Public, baseClass);
TypeBuilder baseTB = context.AssemblyGen.DefinePublicType(name, baseClass, true);
ObjExpr.MarkAsSerializable(baseTB);
baseTB.DefineDefaultConstructor(MethodAttributes.Public);
FieldBuilder metaField = baseTB.DefineField("_meta", typeof(IPersistentMap), FieldAttributes.Public);
MethodBuilder metaMB = baseTB.DefineMethod("meta", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.ReuseSlot, typeof(IPersistentMap), Type.EmptyTypes);
ILGen gen = new ILGen(metaMB.GetILGenerator());
gen.EmitLoadArg(0);
gen.EmitFieldGet(metaField);
gen.Emit(OpCodes.Ret);
MethodBuilder withMB = baseTB.DefineMethod("withMeta", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.ReuseSlot, typeof(IObj), new Type[] { typeof(IPersistentMap)});
gen = new ILGen(withMB.GetILGenerator());
gen.EmitLoadArg(0);
gen.EmitCall(Compiler.Method_Object_MemberwiseClone);
gen.Emit(OpCodes.Castclass, baseTB);
gen.Emit(OpCodes.Dup);
gen.EmitLoadArg(1);
gen.EmitFieldSet(metaField);
gen.Emit(OpCodes.Ret);
for (int i = 0; i < 20; i++ )
DefineDelegateFieldAndOverride(baseTB, i);
return baseTB.CreateType();
}
示例3: BuildConstructors
private void BuildConstructors(IList<ConstructorBuilderInfo>/*!*/ ctors) {
foreach (var ctor in ctors) {
// ctor(... RubyClass! class ..., <visible params>) : base(<hidden params>, <visible params>) { _class = class; }
// ctor(... RubyClass! class ..., <visible params>) : base(... RubyOps.GetContextFromClass(class) ..., <visible params>) { _class = class; }
// ctor(RubyClass! class) : base(RubyOps.GetDefaultExceptionMessage(class)) { _class = class; }
ConstructorBuilder cb = _tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, ctor.ParameterTypes);
ILGen il = new ILGen(cb.GetILGenerator());
int paramIndex = 0;
int argIndex = 0;
// We need to initialize before calling base ctor since the ctor can call virtual methods.
// _immediateClass = immediateClass:
if (!IsDerivedRubyType) {
il.EmitLoadArg(0);
il.EmitLoadArg(1 + ctor.ClassParamIndex);
il.EmitFieldSet(ImmediateClassField);
}
// base ctor call:
il.EmitLoadArg(0);
ConstructorInfo msgCtor;
if (ctor.ParameterTypes.Length == 1 && ctor.Adjustment == SignatureAdjustment.InsertClass &&
_tb.IsSubclassOf(typeof(Exception)) && IsAvailable(msgCtor = _tb.BaseType.GetConstructor(_exceptionMessageSignature))) {
// a parameterless exception constructor should use Ruby default message:
il.EmitLoadArg(1);
il.EmitCall(Methods.GetDefaultExceptionMessage);
il.Emit(OpCodes.Call, msgCtor);
} else {
if (ctor.Adjustment == SignatureAdjustment.InsertClass) {
paramIndex++;
}
while (paramIndex < ctor.ParameterTypes.Length) {
if (ctor.Adjustment == SignatureAdjustment.ConvertClassToContext && argIndex == ctor.ContextArgIndex) {
il.EmitLoadArg(1 + ctor.ClassParamIndex);
il.EmitCall(Methods.GetContextFromModule);
} else {
ClsTypeEmitter.DefineParameterCopy(cb, paramIndex, ctor.BaseParameters[argIndex]);
il.EmitLoadArg(1 + paramIndex);
}
argIndex++;
paramIndex++;
}
il.Emit(OpCodes.Call, ctor.BaseCtor);
}
il.Emit(OpCodes.Ret);
}
}
示例4: EmitMain
static void EmitMain(GenContext context, TypeBuilder proxyTB, string mainName, FieldBuilder mainFB)
{
MethodBuilder cb = proxyTB.DefineMethod("Main",MethodAttributes.Public| MethodAttributes.Static,CallingConventions.Standard,typeof(void),new Type[] { typeof(String[]) });
ILGen gen = new ILGen(cb.GetILGenerator()); ;
Label noMainLabel = gen.DefineLabel();
Label endLabel = gen.DefineLabel();
EmitGetVar(gen, mainFB);
gen.Emit(OpCodes.Dup);
gen.Emit(OpCodes.Brfalse_S, noMainLabel);
gen.Emit(OpCodes.Castclass, typeof(IFn));
gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0);
gen.EmitCall(Method_RT_seq); // gen.Emit(OpCodes.Call, Method_RT_seq);
gen.EmitCall(Method_IFn_applyTo_Object_ISeq); // gen.Emit(OpCodes.Call, Method_IFn_applyTo_Object_ISeq);
gen.Emit(OpCodes.Pop);
gen.Emit(OpCodes.Br_S, endLabel);
// no main found
gen.MarkLabel(noMainLabel);
EmitUnsupported(gen, mainName);
gen.MarkLabel(endLabel);
gen.Emit(OpCodes.Ret);
//context.AssyBldr.SetEntryPoint(cb);
context.AssemblyBuilder.SetEntryPoint(cb);
}
示例5: EmitForwardingMethod
private static void EmitForwardingMethod(TypeBuilder proxyTB,
bool isStatic,
FieldBuilder regularFB,
FieldBuilder overloadFB,
MethodSignature sig,
ElseGenDelegate elseGen)
{
MethodAttributes attributes;
CallingConventions conventions;
if (isStatic)
{
attributes = MethodAttributes.Public | MethodAttributes.Static;
conventions = CallingConventions.Standard;
}
else
{
attributes = MethodAttributes.Public | MethodAttributes.Virtual;
conventions = CallingConventions.HasThis;
}
MethodBuilder mb = proxyTB.DefineMethod(sig.Name, attributes, conventions, sig.ReturnType, sig.ParamTypes);
ILGen gen = new ILGen(mb.GetILGenerator());
Label foundLabel = gen.DefineLabel();
Label elseLabel = gen.DefineLabel();
Label endLabel = gen.DefineLabel();
if (sig.ParamTypes.Length > 18)
elseGen(gen);
else
{
if (overloadFB != null)
{
EmitGetVar(gen, overloadFB);
gen.Emit(OpCodes.Dup);
gen.Emit(OpCodes.Brtrue_S, foundLabel);
gen.Emit(OpCodes.Pop);
}
EmitGetVar(gen, regularFB);
gen.Emit(OpCodes.Dup);
gen.Emit(OpCodes.Brfalse_S, elseLabel);
if (overloadFB != null)
gen.MarkLabel(foundLabel);
gen.Emit(OpCodes.Castclass, typeof(IFn));
if (!isStatic)
gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0);
for (int i = 0; i < sig.ParamTypes.Length; i++)
{
gen.EmitLoadArg(isStatic ? i : i + 1); // gen.Emit(OpCodes.Ldarg, i + 1);
if (sig.ParamTypes[i].IsValueType)
gen.Emit(OpCodes.Box, sig.ParamTypes[i]);
}
gen.EmitCall(Compiler.Methods_IFn_invoke[sig.ParamTypes.Length + (isStatic ? 0 : 1)]);
//gen.Emit(OpCodes.Call, Compiler.Methods_IFn_invoke[sig.ParamTypes.Length + (isStatic ? 0 : 1)]);
if (sig.ReturnType == typeof(void))
gen.Emit(OpCodes.Pop);
else if (sig.ReturnType.IsValueType)
gen.Emit(OpCodes.Unbox_Any,sig.ReturnType);
gen.Emit(OpCodes.Br_S, endLabel);
gen.MarkLabel(elseLabel);
gen.Emit(OpCodes.Pop);
elseGen(gen);
gen.MarkLabel(endLabel);
gen.Emit(OpCodes.Ret);
}
}
示例6: DefineCtors
static void DefineCtors(TypeBuilder proxyTB,
Type superClass,
string initName,
string postInitName,
ISeq ctorsTypes,
FieldBuilder initFB,
FieldBuilder postInitFB,
FieldBuilder stateFB,
string factoryName)
{
for (ISeq s = ctorsTypes; s != null; s = s.next())
{
IMapEntry me = (IMapEntry)s.first();
ISeq thisParamTypesV = (ISeq)me.key();
ISeq baseParamTypesV = (ISeq)me.val();
Type[] thisParamTypes = CreateTypeArray(thisParamTypesV);
Type[] baseParamTypes = CreateTypeArray(baseParamTypesV);
BindingFlags flags = BindingFlags.CreateInstance| BindingFlags.NonPublic| BindingFlags.Public| BindingFlags.Instance;
ConstructorInfo superCtor = superClass.GetConstructor(flags,null,baseParamTypes,null);
if (superCtor == null || superCtor.IsPrivate)
throw new InvalidOperationException("Base class constructor missing or private");
ConstructorBuilder cb = proxyTB.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, thisParamTypes);
ILGen gen = new ILGen(cb.GetILGenerator());
Label noInitLabel = gen.DefineLabel();
Label noPostInitLabel = gen.DefineLabel();
Label endPostInitLabel = gen.DefineLabel();
Label endLabel = gen.DefineLabel();
LocalBuilder locSuperArgs = gen.DeclareLocal(typeof(object));
LocalBuilder locInitVal = gen.DeclareLocal(typeof(object));
if (initFB != null)
{
// init supplied
EmitGetVar(gen, initFB);
gen.Emit(OpCodes.Dup);
gen.Emit(OpCodes.Brfalse_S, noInitLabel);
gen.Emit(OpCodes.Castclass, typeof(IFn));
// box init args
for (int i = 0; i < thisParamTypes.Length; i++)
{
gen.EmitLoadArg(i + 1); // gen.Emit(OpCodes.Ldarg, i + 1);
if (thisParamTypes[i].IsValueType)
gen.Emit(OpCodes.Box,thisParamTypes[i]);
}
gen.EmitCall(Compiler.Methods_IFn_invoke[thisParamTypes.Length]); // gen.Emit(OpCodes.Call, Compiler.Methods_IFn_invoke[thisParamTypes.Length]);
// Expecting: [[super-ctor-args...] state]
// store the init return in a local
gen.Emit(OpCodes.Dup);
gen.Emit(OpCodes.Stloc,locInitVal);
// store the first element in a local
gen.EmitInt(0); // gen.Emit(OpCodes.Ldc_I4_0);
gen.EmitCall(Method_RT_nth); // gen.Emit(OpCodes.Call, Method_RT_nth);
gen.Emit(OpCodes.Stloc, locSuperArgs);
// Stack this + super-ctor-args + call base-class ctor.
gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0);
for (int i = 0; i < baseParamTypes.Length; i++)
{
gen.Emit(OpCodes.Ldloc, locSuperArgs);
gen.EmitInt(i); // gen.Emit(OpCodes.Ldc_I4, i);
gen.EmitCall(Method_RT_nth); // gen.Emit(OpCodes.Call, Method_RT_nth);
if (baseParamTypes[i].IsValueType)
gen.Emit(OpCodes.Unbox_Any, baseParamTypes[i]);
else
gen.Emit(OpCodes.Castclass, baseParamTypes[i]);
}
gen.Emit(OpCodes.Call, superCtor);
if (stateFB != null)
{
gen.EmitLoadArg(0); // gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Ldloc, locInitVal);
gen.EmitInt(1); // gen.Emit(OpCodes.Ldc_I4_1);
gen.EmitCall(Method_RT_nth); // gen.Emit(OpCodes.Call, Method_RT_nth);
gen.Emit(OpCodes.Castclass, typeof(object));
gen.EmitFieldSet(stateFB); // gen.Emit(OpCodes.Stfld, stateFB);
}
gen.Emit(OpCodes.Br_S, endLabel);
// No init found
gen.MarkLabel(noInitLabel);
gen.Emit(OpCodes.Pop);
EmitUnsupported(gen, initName);
gen.MarkLabel(endLabel);
}
//.........这里部分代码省略.........
示例7: OverrideConstructor
private void OverrideConstructor(ConstructorInfo parentConstructor) {
ParameterInfo[] pis = parentConstructor.GetParameters();
if (pis.Length == 0 && typeof(IPythonObject).IsAssignableFrom(_baseType)) {
// default ctor on a base type, don't override this one, it assumes
// the PythonType is some default value and we'll always be unique.
return;
}
ParameterInfo[] overrideParams = GetOverrideCtorSignature(pis);
Type[] argTypes = new Type[overrideParams.Length];
string[] paramNames = new string[overrideParams.Length];
for (int i = 0; i < overrideParams.Length; i++) {
argTypes[i] = overrideParams[i].ParameterType;
paramNames[i] = overrideParams[i].Name;
}
ConstructorBuilder cb = _tg.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, argTypes);
for (int i = 0; i < overrideParams.Length; i++) {
ParameterBuilder pb = cb.DefineParameter(i + 1,
overrideParams[i].Attributes,
overrideParams[i].Name);
int origIndex = GetOriginalIndex(pis, overrideParams, i);
if (origIndex >= 0) {
if (pis[origIndex].IsDefined(typeof(ParamArrayAttribute), false)) {
pb.SetCustomAttribute(new CustomAttributeBuilder(
typeof(ParamArrayAttribute).GetConstructor(Type.EmptyTypes), ArrayUtils.EmptyObjects));
} else if (pis[origIndex].IsDefined(typeof(ParamDictionaryAttribute), false)) {
pb.SetCustomAttribute(new CustomAttributeBuilder(
typeof(ParamDictionaryAttribute).GetConstructor(Type.EmptyTypes), ArrayUtils.EmptyObjects));
}
if ((pis[origIndex].Attributes & ParameterAttributes.HasDefault) != 0) {
pb.SetConstant(pis[origIndex].DefaultValue);
}
}
}
ILGen il = new ILGen(cb.GetILGenerator());
int typeArg;
if (pis.Length == 0 || pis[0].ParameterType != typeof(CodeContext)) {
typeArg = 1;
} else {
typeArg = 2;
}
// this.__class__ = <arg?>
// can occur 2 ways:
// 1. If we have our own _typeField then we set it
// 2. If we're a subclass of IPythonObject (e.g. one of our exception classes) then we'll flow it to the
// base type constructor which will set it.
if (!typeof(IPythonObject).IsAssignableFrom(_baseType)) {
il.EmitLoadArg(0);
// base class could have CodeContext parameter in which case our type is the 2nd parameter.
il.EmitLoadArg(typeArg);
il.EmitFieldSet(_typeField);
}
if (_explicitMO != null) {
il.Emit(OpCodes.Ldarg_0);
il.EmitNew(_explicitMO.FieldType.GetConstructor(Type.EmptyTypes));
il.Emit(OpCodes.Stfld, _explicitMO);
}
// initialize all slots to Uninitialized.instance
MethodInfo init = typeof(PythonOps).GetMethod("InitializeUserTypeSlots");
il.EmitLoadArg(0);
il.EmitLoadArg(typeArg);
il.EmitCall(init);
Debug.Assert(_slotsField != null);
il.EmitFieldSet(_slotsField);
CallBaseConstructor(parentConstructor, pis, overrideParams, il);
}
示例8: EmitNonInheritedMethodLookup
/// <summary>
/// Emits the call to lookup a member defined in the user's type. Returns
/// the local which stores the resulting value and leaves a value on the
/// stack indicating the success of the lookup.
/// </summary>
private LocalBuilder EmitNonInheritedMethodLookup(string name, ILGen il) {
LocalBuilder callTarget = il.DeclareLocal(typeof(object));
// emit call to helper to do lookup
il.EmitLoadArg(0);
if (typeof(IPythonObject).IsAssignableFrom(_baseType)) {
Debug.Assert(_typeField == null);
il.EmitPropertyGet(TypeInfo._IPythonObject.PythonType);
} else {
il.EmitFieldGet(_typeField);
}
il.EmitLoadArg(0);
EmitSymbolId(il, name);
il.Emit(OpCodes.Ldloca, callTarget);
il.EmitCall(typeof(UserTypeOps), "TryGetNonInheritedMethodHelper");
return callTarget;
}
示例9: GenerateStaticConstructor
private void GenerateStaticConstructor(TypeBuilder fnTB, Type baseType, bool isDebuggable)
{
if (_constants.count() > 0)
{
ConstructorBuilder cb = fnTB.DefineConstructor(MethodAttributes.Static, CallingConventions.Standard, Type.EmptyTypes);
MethodBuilder method1 = GenerateConstants(fnTB, baseType, isDebuggable);
MethodBuilder method2 = GenerateVarCallsiteInits(fnTB, baseType, isDebuggable);
MethodBuilder method3 = GenerateKeywordCallsiteInit(fnTB, baseType, isDebuggable);
ILGen gen = new ILGen(cb.GetILGenerator());
gen.EmitCall(method1); // gen.Emit(OpCodes.Call, method1);
if (method2 != null)
gen.EmitCall(method2);
if (method3 != null)
gen.EmitCall(method3);
gen.Emit(OpCodes.Ret);
}
}
示例10: WriteEpilog
private void WriteEpilog(ILGen il, Parameter[] parameters, LocalAccess locals)
{
if (locals.HasLocals || parameters.Length > 0)
{
Label label = il.DefineLabel();
if (parameters.Length > 0)
{
for (int k = 0; k < parameters.Length; k++)
{
il.Emit(OpCodes.Ldarg_2);
il.EmitInt(k);
il.Emit(OpCodes.Ldelem_Ref);
if (parameters[k].Type.IsValueType)
{
il.Emit(OpCodes.Isinst, parameters[k].Type);
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Brfalse, label);
if (locals.IsParameterBinded(parameters[k].ID))
{
LocalBuilder local = locals.GetLocal(parameters[k].ID);
il.EmitUnbox(local.LocalType);
il.Emit(OpCodes.Stloc, local);
}
else
il.Emit(OpCodes.Pop);
}
else
{
Label label2 = il.DefineLabel();
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Brfalse_S, label2);
il.Emit(OpCodes.Isinst, parameters[k].Type);
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Brfalse, label);
il.MarkLabel(label2);
if (locals.IsParameterBinded(parameters[k].ID))
{
LocalBuilder local = locals.GetLocal(parameters[k].ID);
il.Emit(OpCodes.Stloc, local);
}
else
il.Emit(OpCodes.Pop);
}
}
}
if (locals.HasLocals)
{
il.Emit(OpCodes.Ldarg_0);
il.EmitPropertyGet(typeof(CompiledLambda), "Values");
LocalBuilder values = il.DeclareLocal(typeof(SymbolLink[]));
il.Emit(OpCodes.Stloc, values);
int k = 0;
foreach (LocalAccess.LocalBinding b in locals)
{
il.Emit(OpCodes.Ldarg_3);
il.Emit(OpCodes.Ldloc, values);
il.EmitInt(k++);
il.Emit(OpCodes.Ldelem_Ref);
il.EmitCall(m_memoryPoolGetData);
if (b.Local.LocalType.IsValueType)
{
il.Emit(OpCodes.Isinst, b.Local.LocalType);
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Brfalse, label);
il.EmitUnbox(b.Local.LocalType);
il.Emit(OpCodes.Stloc, b.Local);
}
else
{
Label label2 = il.DefineLabel();
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Brfalse_S, label2);
il.Emit(OpCodes.Isinst, b.Local.LocalType);
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Brfalse, label);
il.MarkLabel(label2);
il.Emit(OpCodes.Stloc, b.Local);
}
}
il.FreeLocal(values);
}
Label end = il.DefineLabel();
il.Emit(OpCodes.Br_S, end);
il.MarkLabel(label);
il.Emit(OpCodes.Pop); // Isinst
il.EmitPropertyGet(typeof(Undefined), "Value");
il.Emit(OpCodes.Ret);
il.MarkLabel(end);
}
}
示例11: EmitClassObjectFromInstance
protected override void EmitClassObjectFromInstance(ILGen il) {
if (typeof(IRubyObject).IsAssignableFrom(BaseType)) {
il.EmitCall(Methods.IRubyObject_get_ImmediateClass);
} else {
il.EmitFieldGet(_immediateClassField);
}
}
示例12: EmitConvertFromObject
/// <summary>
/// Emit code to convert object to a given type. This code is semantically equivalent
/// to PythonBinder.EmitConvertFromObject, except this version accepts ILGen whereas
/// PythonBinder accepts Compiler. The Binder will chagne soon and the two will merge.
/// </summary>
public void EmitConvertFromObject(ILGen il, Type toType) {
if (toType == typeof(object)) {
return;
} else if (toType == typeof(void)) {
il.Emit(OpCodes.Pop);
return;
}
var callTarget = il.DeclareLocal(typeof(object));
il.Emit(OpCodes.Stloc, callTarget);
var site = GetConversionSite(toType);
// Emit the site invoke
il.EmitFieldGet(site);
FieldInfo target = site.FieldType.GetField("Target");
il.EmitFieldGet(target);
il.EmitFieldGet(site);
// Emit the context
EmitContext(il, false);
il.Emit(OpCodes.Ldloc, callTarget);
il.EmitCall(target.FieldType, "Invoke");
}
示例13: CreateStaticCtor
void CreateStaticCtor()
{
ConstructorBuilder ctorB = _typeBuilder.DefineConstructor(MethodAttributes.Static | MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes);
ILGen gen = new ILGen(ctorB.GetILGenerator());
for (int i = 0; i < _fieldBuilders.Count; i++)
{
FieldBuilder fb = _fieldBuilders[i];
Expression fbInit = _fieldInits[i];
string setterName = String.Format("{0}_setter", fb.Name);
MethodBuilder mbSetter = _typeBuilder.DefineMethod(
setterName,
MethodAttributes.Public | MethodAttributes.Static,
CallingConventions.Standard,
fbInit.Type,
Type.EmptyTypes);
LambdaExpression initL = Expression.Lambda(Expression.Assign(Expression.Field(null, fb), fbInit));
initL.CompileToMethod(mbSetter);
gen.EmitCall(mbSetter);
gen.Emit(OpCodes.Pop);
}
gen.Emit(OpCodes.Ret);
}
示例14: EmitConvertFromObject
/// <summary>
/// Emit code to convert object to a given type. This code is semantically equivalent
/// to PythonBinder.EmitConvertFromObject, except this version accepts ILGen whereas
/// PythonBinder accepts Compiler. The Binder will chagne soon and the two will merge.
/// </summary>
private static void EmitConvertFromObject(ILGen il, Type toType) {
if (toType == typeof(object)) return;
if (toType.IsGenericParameter) {
il.EmitCall(typeof(PythonOps).GetMethod("ConvertFromObject").MakeGenericMethod(toType));
return;
}
MethodInfo fastConvertMethod = PythonBinder.GetFastConvertMethod(toType);
if (fastConvertMethod != null) {
il.EmitCall(fastConvertMethod);
} else if (toType == typeof(void)) {
il.Emit(OpCodes.Pop);
} else if (typeof(Delegate).IsAssignableFrom(toType)) {
il.EmitType(toType);
il.EmitCall(typeof(Converter), "ConvertToDelegate");
il.Emit(OpCodes.Castclass, toType);
} else {
Label end = il.DefineLabel();
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Isinst, toType);
il.Emit(OpCodes.Brtrue_S, end);
il.Emit(OpCodes.Ldtoken, toType);
il.EmitCall(PythonBinder.GetGenericConvertMethod(toType));
il.MarkLabel(end);
il.Emit(OpCodes.Unbox_Any, toType); //??? this check may be redundant
}
}
示例15: EmitBaseClassCallCheckForEvents
/// <summary>
/// Emits code to check if the class has overridden this specific
/// function. For example:
///
/// MyDerivedType.SomeVirtualFunction = ...
/// or
///
/// class MyDerivedType(MyBaseType):
/// def SomeVirtualFunction(self, ...):
///
/// </summary>
private LocalBuilder EmitBaseClassCallCheckForEvents(ILGen il, MethodInfo baseMethod, string name) {
Label instanceCall = il.DefineLabel();
LocalBuilder callTarget = il.DeclareLocal(typeof(object));
il.EmitLoadArg(0);
EmitSymbolId(il, name);
il.Emit(OpCodes.Ldloca, callTarget);
il.EmitCall(typeof(UserTypeOps), "TryGetNonInheritedValueHelper");
il.Emit(OpCodes.Brtrue, instanceCall);
EmitBaseMethodDispatch(baseMethod, il);
il.MarkLabel(instanceCall);
return callTarget;
}