Example #1
0
        public object CallMethod(object target, string method, object[] args)
        {
            Type t = target as Type;

            if (t == null)
            {
                t = target.GetType();
            }
            Delegate del = GetMethod(target, method, args);

            if (del == null)
            {
                //del = DelegateUtility.MethodToInstanceDelegate(target, t.GetMethod(method));
                MethodInfo info = ReflectionUtility.FindMethod(t, method, args);
                if (info.IsStatic)
                {
                    TypeDef typeDef = _assembly.GetTypeDef(t);
                    if (typeDef == null || !typeDef.allowStaticMembers)
                    {
                        throw new Exception("Static member access is not allowed on type " + t.Name);
                    }
                }
                del = DelegateUtility.MethodToInstanceDelegate(target, ReflectionUtility.FindMethod(t, method, args));
                Cache(target, method, del, args);
            }

            return(SmartCall(del, args));
        }
Example #2
0
        /// <summary>
        /// Main compilation entry
        /// </summary>
        /// <returns></returns>
        ///
        protected virtual CompiledScript _InternalCompile()
        {
            _cdata = new CompilerData(_assembly);
            List <Func <object> > functions = new List <Func <object> >();

            while (!isAtEnd)
            {
                Func <object> function = null;
                object        compiled = CompileDeclaration();
                if (compiled == null)
                {
                    continue;
                }
                Type returnType = CompilerUtility.GetReturnType(compiled);

                TypeDef typeDef = _assembly.GetTypeDef(returnType);

                if (CompilerUtility.IsFunc(compiled))
                {
                    if (compiled.GetType() != typeof(Func <object>))
                    {
                        function = typeDef.ToGenericFunction(compiled, _cdata);
                    }
                    else
                    {
                        function = (Func <object>)compiled;
                    }
                }
                else if (CompilerUtility.IsReference(compiled))
                {
                    function = ((Reference)compiled).CreateGetFunction(_cdata);
                }
                else
                {
                    function = () => { return(compiled); };
                }
                functions.Add(function);
            }
            Func <object>[] funcArray = functions.ToArray();

            CompiledScript compiledScript = new CompiledScript(() =>
            {
                object output = null;
                for (int mn = 0; mn < funcArray.Length; mn += 1)
                {
                    output = funcArray[mn]();
                    if ((_cdata._runtimeFlags & (int)CompilerData.ControlInstruction.Exit) != 0)
                    {
                        break;
                    }
                }
                _cdata._runtimeFlags = 0;
                return(_cdata._returnValue);
            },
                                                               _cdata);

            return(compiledScript);
        }