コード例 #1
0
ファイル: BuiltinMethod.cs プロジェクト: lsgxeva/IronScheme
        public override object Call(object[] args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args cannot be null");
            }

            int nargs = args.Length;

            Callable c;

            if (cache.TryGetValue(-1, out c) ||
                cache.TryGetValue(nargs, out c))
            {
                return(c.Call(args));
            }

            if (!baked)
            {
                try
                {
                    if (methods.Length == 1)
                    {
                        // make c fast
                        MethodBase mb = methods[0];

                        bool needContext = false;

#warning Remove when Mono fixed: https:                         //bugzilla.novell.com/show_bug.cgi?id=655439
                        Type dt = (nargs > 8 || IsParams(mb)) ? // for mono
                                  typeof(CallTargetN) : CallTargets.GetTargetType(needContext, nargs, false);
                        Delegate d = Delegate.CreateDelegate(dt, mb as MethodInfo, false);
                        if (d == null)
                        {
                            d = Delegate.CreateDelegate(typeof(CallTargetN), needContext ? context : null, mb as MethodInfo, false);
                        }

                        if (d != null)
                        {
                            if (dt == typeof(CallTargetN))
                            {
                                cache[-1] = c = Closure.Create(d);
                            }
                            else
                            {
                                cache[nargs] = c = Closure.Create(d);
                            }
                        }
                    }
                    else
                    {
                        Type[] targs = Array.ConvertAll <object, Type>(args, delegate(object input)
                        {
                            if (input == null)
                            {
                                return(typeof(object));
                            }
                            else
                            {
                                return(input.GetType());
                            }
                        });

                        MethodCandidate mc = meth.MakeBindingTarget(CallType.None, targs);
                        if (mc != null)
                        {
                            MethodBase mb = mc.Target.Method;

                            bool needContext = NeedContext(mb);     // TODO: check if can remove

#warning Remove when Mono fixed: https:                             //bugzilla.novell.com/show_bug.cgi?id=655439
                            Type dt = (nargs > 8 || IsParams(mb)) ? // for mono
                                      typeof(CallTargetN) : CallTargets.GetTargetType(needContext, nargs, false);
                            Delegate d = Delegate.CreateDelegate(dt, mb as MethodInfo, false);
                            if (d == null)
                            {
                                d = Delegate.CreateDelegate(typeof(CallTargetN), needContext ? context : null, mb as MethodInfo, false);
                            }

                            if (d != null)
                            {
                                cache[nargs] = c = Closure.Create(d);
                            }
                        }
                    }
                }
                catch
                {
                    ;
                }

                if (c != null)
                {
                    return(c.Call(args));
                }
            }
            // fallback
            baked = true;

            try
            {
                // DO NOT WANT!!!!
                return(meth.CallReflected(context, CallType.None, args));
            }
            catch (ArgumentTypeException ex)
            {
                return(Closure.AssertionViolation(meth.ToString(), ex.Message, args));
            }
        }