public MethodInfo CompileDynamicMethod(string name, CodeTreeNode codeTreeNode) { Block generator = new Block(); codeTreeNode.EmitRun(generator); DynamicMethod method = new DynamicMethod( name, typeof(object), new Type[]{typeof(Runtime), typeof(IScope)}, moduleBuilder); generator.Build(new DynamicMethodProxy(method)); return method; }
public void Emit(Block block) { lines.AddRange(block.lines); }
private void DefineParserConstructor() { OrderedSet<string> fields = pattern.Fields; Type[] parameterTypes = new Type[1 + fields.Count]; parameterTypes[0] = typeof(Source); for (int n = 1; n < parameterTypes.Length; n++) parameterTypes[n] = typeof(object); ConstructorBuilder builder = typeBuilder.DefineConstructor( MethodAttributes.Public, CallingConventions.Standard, parameterTypes); Block generator = new Block(); // Call the base constructor ConstructorInfo baseConstructor = typeBuilder.BaseType.GetConstructor(new Type[]{typeof(Source)}); if (baseConstructor == null) throw new Exception(); generator.LoadArg(0); generator.LoadArg(1); generator.Call(baseConstructor); SetMethodsToInitialValues(generator); // Save the fields for (int n = 0; n < fields.Count; n++) { generator.LoadArg(0); generator.LoadArg(n + 2); generator.StoreField(typeBuilder.GetField(fields[n])); } generator.Return(); generator.Build(new ConstructorBuilderProxy(builder)); }
private void SetMethodsToInitialValues(Block generator) { foreach (Method method in methods) { generator.LoadArg(0); generator.Load(method); generator.StoreField(typeBuilder.GetField(method.Name)); } }
private void DefineConstructor(Method constructor) { Type[] parameters = new Type[1 + constructor.Parameters.Count]; parameters[0] = typeof(RuntimeState); for (int n = 1; n < parameters.Length; n++) parameters[n] = typeof(object); ConstructorBuilder builder = typeBuilder.DefineConstructor( MethodAttributes.Public, CallingConventions.Standard, parameters); Block generator = new Block(); SetMethodsToInitialValues(generator); if (!constructor.IsInstance) throw new Exception(); MethodInfo callMethod = typeof(Method).GetMethod("Call", new Type[]{typeof(RuntimeState), typeof(object), typeof(object[])}); if (callMethod == null) throw new Exception(); generator.Load(constructor); generator.LoadArg(1); generator.LoadArg(0); generator.Load(constructor.Parameters.Count); generator.NewArray(typeof(object)); for (int n = 0; n < constructor.Parameters.Count; n++) { generator.Dup(); generator.Load(n); generator.LoadArg(n + 1); generator.StoreElementRef(); } generator.Call(callMethod); generator.Pop(); generator.Return(); generator.Build(new ConstructorBuilderProxy(builder)); }
private void DefineDefaultConstructor() { ConstructorBuilder builder = typeBuilder.DefineConstructor( MethodAttributes.Public, CallingConventions.Standard, new Type[0]); Block generator = new Block(); // Call the base constructor ConstructorInfo baseConstructor = typeBuilder.BaseType.GetConstructor(new Type[0]); if (baseConstructor != null) { generator.LoadArg(0); generator.Call(baseConstructor); } SetMethodsToInitialValues(generator); generator.Return(); generator.Build(new ConstructorBuilderProxy(builder)); }
public virtual void EmitSet(Block generator) { throw new NotImplementedException(TypeNames.GetName(GetType())); }
public override void EmitGet(Block generator) { generator.Load(o); }