示例#1
0
 public ConstantPool CopyData()
 {
     ConstantPool ret = new ConstantPool();
     ret._data = this._data;
     ret._types = this._types;
     return ret;
 }
示例#2
0
 public CodeGen DefineMethod(string methodName, Type returnType, IList<Type> paramTypes, ConstantPool constantPool)
 {
     CodeGen cg;
     if (GenerateStaticMethods) {
         int index = Interlocked.Increment(ref _index);
         TypeGen tg = DefinePublicType("Type$" + methodName + "$" + index, typeof(object));
         cg = tg.DefineMethod("Handle" + index, returnType, paramTypes, null, constantPool);
         cg.DynamicMethod = true;
     } else {
         Type[] parameterTypes = CompilerHelpers.MakeParamTypeArray(paramTypes, constantPool);
         string dynamicMethodName = methodName + "##" + Interlocked.Increment(ref _index);
         DynamicMethod target;
     #if SILVERLIGHT // Module-hosted DynamicMethod is not available in SILVERLIGHT
         target = new DynamicMethod(dynamicMethodName, returnType, parameterTypes);
     #else
         target = new DynamicMethod(dynamicMethodName, returnType, parameterTypes, _myModule);
     #endif
         cg = new CodeGen(null, this, target, target.GetILGenerator(), parameterTypes, constantPool);
     }
     return cg;
 }
示例#3
0
        internal static CodeGen CreateDebuggableDynamicCodeGenerator(CompilerContext context, string name, Type retType, IList <Type> paramTypes, IList <string> paramNames, ConstantPool constantPool)
        {
            TypeGen tg = ScriptDomainManager.CurrentManager.Snippets.DefineDebuggableType(name, context.SourceUnit);
            CodeGen cg = tg.DefineMethod("Initialize", retType, paramTypes, paramNames, constantPool);

            tg.AddCodeContextField();
            cg.DynamicMethod = true;

            return(cg);
        }
示例#4
0
 /// <summary>
 ///
 /// </summary>
 internal static CodeGen CreateDynamicCodeGenerator(string name, Type retType, IList <Type> paramTypes, ConstantPool constantPool)
 {
     return(ScriptDomainManager.CurrentManager.Snippets.Assembly.DefineMethod(name, retType, paramTypes, constantPool));
 }
示例#5
0
        public CodeGen DefineMethod(string methodName, Type returnType, IList <Type> paramTypes, ConstantPool constantPool)
        {
            CodeGen cg;

            if (GenerateStaticMethods)
            {
                int     index = Interlocked.Increment(ref _index);
                TypeGen tg    = DefinePublicType("Type$" + methodName + "$" + index, typeof(object));
                cg = tg.DefineMethod("Handle" + index, returnType, paramTypes, null, constantPool);
                cg.DynamicMethod = true;
            }
            else
            {
                Type[]        parameterTypes    = CompilerHelpers.MakeParamTypeArray(paramTypes, constantPool);
                string        dynamicMethodName = methodName + "##" + Interlocked.Increment(ref _index);
                DynamicMethod target;
#if SILVERLIGHT // Module-hosted DynamicMethod is not available in SILVERLIGHT
                target = new DynamicMethod(dynamicMethodName, returnType, parameterTypes);
#else
                target = new DynamicMethod(dynamicMethodName, returnType, parameterTypes, _myModule);
#endif
                cg = new CodeGen(null, this, target, target.GetILGenerator(), parameterTypes, constantPool);
            }
            return(cg);
        }
示例#6
0
 public CodeGen CreateCodeGen(MethodBase mi, ILGenerator ilg, IList<Type> paramTypes, ConstantPool constantPool) {
     CodeGen ret = new CodeGen(this, _myAssembly, mi, ilg, paramTypes, constantPool);
     if (_binder != null) ret.Binder = _binder;
     if (_contextSlot != null) ret.ContextSlot = _contextSlot;
     return ret;
 }
示例#7
0
        public CodeGen DefineMethod(MethodAttributes attrs, string name, Type retType, IList<Type> paramTypes, IList<string> paramNames, 
            object[] defaultVals, CustomAttributeBuilder[] cabs, ConstantPool constantPool) {
            Contract.RequiresNotNull(paramTypes, "paramTypes");
            if (paramNames == null) {
                if (defaultVals != null) throw new ArgumentException("must provide paramNames when providing defaultVals");
                if (cabs != null) throw new ArgumentException("must provide paramNames when providing cabs");
            } else {
                if (paramTypes.Count != paramNames.Count) {
                    throw new ArgumentException("Must provide same number of paramNames as paramTypes");
                }
                if (defaultVals != null && defaultVals.Length > paramNames.Count) {
                    throw new ArgumentException("Provided more defaultValues than parameters");
                }
                if (cabs != null && cabs.Length > paramNames.Count) {
                    throw new ArgumentException("Provided more custom attributes than parameters");
                }
            }

            Type[] parameterTypes = CompilerHelpers.MakeParamTypeArray(paramTypes, constantPool);

            if (parameterTypes.Length > 0 && parameterTypes[0] == typeof(CodeContext) && name != "Initialize")
            {
              attrs = MethodAttributes.Static | MethodAttributes.Private;
            }

            MethodBuilder mb = _myType.DefineMethod(name, attrs, retType, parameterTypes);
            CodeGen res = CreateCodeGen(mb, mb.GetILGenerator(), parameterTypes, constantPool);

            if (paramNames == null) return res;
            // parameters are index from 1, with constant pool we need to skip the first arg
            int offset = constantPool != null ? 2 : 1;
            for (int i = 0; i < paramNames.Count; i++) {
                ParameterBuilder pb = res.DefineParameter(i + offset, ParameterAttributes.None, paramNames[i]);
                if (defaultVals != null && i < defaultVals.Length && defaultVals[i] != DBNull.Value) {
                    pb.SetConstant(defaultVals[i]);
                }

                if (cabs != null && i < cabs.Length && cabs[i] != null) {
                    pb.SetCustomAttribute(cabs[i]);
                }
            }
            return res;
        }
示例#8
0
 public CodeGen DefineMethod(string name, Type retType, IList<Type> paramTypes, IList<string> paramNames, ConstantPool constantPool) {
     return DefineMethod(CompilerHelpers.PublicStatic, name, retType, paramTypes, paramNames, null, null, constantPool);
 }
        public DelegateInfo GenerateDelegateStub() {
            PerfTrack.NoteEvent(PerfTrack.Categories.DelegateCreate, ToString());

            Type[] delegateParams = new Type[_parameters.Length];
            for (int i = 0; i < _parameters.Length; i++) {
                delegateParams[i] = _parameters[i].ParameterType;
            }

            AssemblyGen snippets = ScriptDomainManager.CurrentManager.Snippets.Assembly;

            // Create new constant pool
            ConstantPool constants = new ConstantPool();

            // Create the method
            CodeGen cg = snippets.DefineMethod(ToString(), _returnType, delegateParams, constants);
            cg.Binder = _binder;

            // Add the space for the delegate target and save the index at which it was placed,
            // most likely zero.
            int targetIndex = constants.Count;
#if DEBUG
            Slot target = constants.AddData(TargetPlaceHolder);
#else
            Slot target = constants.AddData(null);
#endif

            // Add the CodeContext into the constant pool
            Slot context = cg.ConstantPool.AddData(_binder.Context);
            Debug.Assert(typeof(CodeContext).IsAssignableFrom(context.Type));
            cg.ContextSlot = context;

            // Emit the stub
            StubGenerator.EmitClrCallStub(cg, target, 0, StubGenerator.CallType.None);

            // Finish the method
            MethodInfo method = cg.CreateDelegateMethodInfo();

            // Save the constants in the delegate info class
            return new DelegateInfo(method, constants.Data, targetIndex);
        }