本文整理汇总了C#中CodeGen.DefineMethod方法的典型用法代码示例。如果您正苦于以下问题:C# CodeGen.DefineMethod方法的具体用法?C# CodeGen.DefineMethod怎么用?C# CodeGen.DefineMethod使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeGen
的用法示例。
在下文中一共展示了CodeGen.DefineMethod方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Emit
internal override void Emit(CodeGen cg)
{
cg.EmitPosition(Start, header);
SignatureInfo sigInfo = GetSignature(cg);
FlowChecker.Check(this);
string mname = name.GetString() + "$f" + counter++;
// create the new method & setup it's locals
CodeGen impl = cg.DefineMethod(mname, typeof(object), sigInfo.ParamTypes, sigInfo.ParamNames);
impl.Names = CodeGen.CreateLocalNamespace(impl);
impl.Context = cg.Context;
for (int arg = sigInfo.HasContext ? 1 : 0; arg < sigInfo.ParamNames.Length; arg++) {
impl.Names.SetSlot(sigInfo.ParamNames[arg], impl.GetArgumentSlot(arg));
}
if (sigInfo.HasContext) {
if (IsClosure) {
impl.StaticLinkSlot = impl.GetArgumentSlot(0);
}
impl.ContextSlot = impl.GetArgumentSlot(0);
impl.ModuleSlot = new PropertySlot(impl.ContextSlot, typeof(ICallerContext).GetProperty("Module"));
}
// then generate the actual method
EmitFunctionImplementation(impl, cg);
impl.Finish();
if (NeedsWrapperMethod()) impl = MakeWrapperMethodN(cg, impl.MethodInfo, sigInfo.HasContext);
// Create instance of the Function? object
Type funcType, targetType;
using (impl) {
GetFunctionType(out funcType, out targetType);
cg.EmitModuleInstance();
cg.EmitString(name.GetString());
cg.EmitDelegate(impl, targetType, sigInfo.ContextSlot);
}
int first = sigInfo.HasContext ? 1 : 0;
// Emit string array (minus the first environment argument)
cg.EmitInt(sigInfo.ParamNames.Length - first);
cg.Emit(OpCodes.Newarr, typeof(string));
for (int i = first; i < sigInfo.ParamNames.Length; i++) {
cg.Emit(OpCodes.Dup);
cg.EmitInt(i - first);
cg.EmitStringOrNull(sigInfo.ParamNames[i].GetString());
cg.Emit(OpCodes.Stelem_Ref);
}
cg.EmitObjectArray(defaults);
if (flags == FunctionAttributes.None) {
cg.Emit(OpCodes.Newobj, funcType.GetConstructor(
new Type[] { typeof(PythonModule), typeof(string), targetType, typeof(string[]), typeof(object[]) }));
} else {
cg.EmitInt((int)flags);
cg.Emit(OpCodes.Newobj, funcType.GetConstructor(
new Type[] { typeof(PythonModule), typeof(string), targetType, typeof(string[]), typeof(object[]), typeof(FunctionAttributes) }));
}
string doc = Body.Documentation;
if (doc != null) {
cg.Emit(OpCodes.Dup);
cg.EmitString(doc);
cg.EmitCall(typeof(PythonFunction).GetProperty("Documentation").GetSetMethod());
}
// update func_code w/ appropriate state.
cg.Emit(OpCodes.Dup);
Slot functionCode = cg.GetLocalTmp(typeof(FunctionCode));
cg.EmitCall(typeof(PythonFunction).GetProperty("FunctionCode").GetGetMethod());
cg.Emit(OpCodes.Castclass, typeof(FunctionCode));
cg.Emit(OpCodes.Dup);
functionCode.EmitSet(cg);
if (IsExternal) {
cg.EmitInt(this.ExternalStart.Line);
} else {
cg.EmitInt(this.Start.Line);
}
cg.EmitCall(typeof(FunctionCode), "SetLineNumber");
functionCode.EmitGet(cg);
if (IsExternal) {
cg.EmitString(ExternalInfo.OriginalFileName);
} else {
cg.EmitString(this.filename);
}
cg.EmitCall(typeof(FunctionCode), "SetFilename");
// Only codegen the call into SetFlags if there are flags to set.
FunctionCode.FuncCodeFlags codeFlags = 0;
if (cg.Context.TrueDivision) codeFlags |= FunctionCode.FuncCodeFlags.FutureDivision;
if (this.yieldCount > 0) codeFlags |= FunctionCode.FuncCodeFlags.Generator;
if (codeFlags != 0) {
functionCode.EmitGet(cg);
//.........这里部分代码省略.........
示例2: EmitGeneratorBody
private void EmitGeneratorBody(CodeGen cg, CodeGen ocg)
{
// Create the GenerateNext function
CodeGen ncg = cg.DefineMethod(name.GetString() + "$g" + counter++, typeof(bool),
new Type[] { typeof(Generator), typeof(object).MakeByRefType() },
new String[] { "$gen", "$ret" });
ncg.Context = cg.Context;
PromoteLocalsToEnvironment();
ncg.FuncOrClassName = name.ToString();
ncg.EmitSetTraceBackUpdateStatus(false);
// Namespace without er factory - all locals must exist ahead of time
ncg.Names = new Namespace(null);
Slot generator = ncg.GetArgumentSlot(0);
ncg.StaticLinkSlot = new FieldSlot(generator, typeof(Generator).GetField("staticLink"));
if (HasEnvironment) {
cg.EnvironmentSlot = CreateEnvironment(cg);
EnvironmentFactory ef = this.environmentFactory;
Slot envSlotCast = new CastSlot(
new FieldSlot(generator, typeof(Generator).GetField("environment")),
ef.EnvironmentType
);
Slot envSlot = ncg.GetLocalTmp(ef.EnvironmentType);
// setup the environment and static link slots
ncg.EnvironmentSlot = envSlot;
ncg.ContextSlot = envSlot;
// pull the environment into typed local variable
envSlot.EmitSet(ncg, envSlotCast);
InheritEnvironment(ncg);
CreateGeneratorTemps(ef, ncg);
} else {
ncg.ContextSlot = ncg.StaticLinkSlot;
}
ncg.ModuleSlot = new PropertySlot(ncg.ContextSlot, typeof(ICallerContext).GetProperty("Module"));
CreateClosureSlots(ncg);
CreateGlobalSlots(ncg, ocg);
// Emit the generator body using the typed er
EmitGenerator(ncg);
// Initialize the generator
EmitTupleParams(cg);
// Create instance of the generator
cg.EmitStaticLinkOrNull();
cg.EmitEnvironmentOrNull();
cg.EmitDelegate(ncg, typeof(Generator.NextTarget), null);
cg.EmitNew(typeof(Generator), new Type[] { typeof(FunctionEnvironmentDictionary), typeof(FunctionEnvironmentDictionary), typeof(Generator.NextTarget) });
cg.EmitReturn();
}
示例3: CreateMethod
/// <summary>
/// Defines the method with the correct signature and sets up the context slot appropriately.
/// </summary>
/// <returns></returns>
private CodeGen CreateMethod(CodeGen _impl)
{
// Create the GenerateNext function
CodeGen ncg = _impl.DefineMethod(
GetGeneratorMethodName(), // Method Name
typeof(bool), // Return Type
new Type[] { // signature
_generator,
typeof(object).MakeByRefType()
},
_GeneratorSigNames, // param names
GetStaticDataForBody(_impl));
Slot generator = ncg.GetArgumentSlot(0);
ncg.ContextSlot = new PropertySlot(generator, typeof(Generator).GetProperty("Context"));
// Namespace without er factory - all locals must exist ahead of time
ncg.Allocator = new ScopeAllocator(_impl.Allocator, null);
ncg.Allocator.Block = null; // No scope is active at this point
// We are emitting generator, mark the CodeGen
ncg.IsGenerator = true;
return ncg;
}