Exemple #1
0
 public virtual string[] GetAuxPropertyNames(IHostInvokeContext context, BindingFlags bindFlags)
 {
     return MiscHelpers.GetEmptyArray<string>();
 }
 public static object InvokeDelegate(IHostInvokeContext context, Delegate del, object[] args)
 {
     return InvokeMethod(context, del, del.GetType().GetMethod("Invoke"), args);
 }
Exemple #3
0
        public override HostTargetFlags GetFlags(IHostInvokeContext context)
        {
            var type = GetSpecificTypeNoThrow();

            return((type != null) ? HostTargetFlags.AllowStaticMembers : HostTargetFlags.None);
        }
 public override bool TryInvoke(IHostInvokeContext context, BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
 {
     result = target.InvokeMember(name, invokeFlags.HasFlag(BindingFlags.SetField) ? BindingFlags.SetProperty : BindingFlags.GetProperty, args, bindArgs, null, true);
     return(true);
 }
Exemple #5
0
 public override bool TryInvoke(IHostInvokeContext context, BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
 {
     result = target.InvokeMethod(name, args);
     return(true);
 }
 public virtual string[] GetAuxPropertyNames(IHostInvokeContext context, BindingFlags bindFlags)
 {
     return(ArrayHelpers.GetEmptyArray <string>());
 }
Exemple #7
0
 public override string[] GetAuxPropertyNames(IHostInvokeContext context, BindingFlags bindFlags)
 {
     return(target.GetAuxPropertyNames(context, bindFlags));
 }
Exemple #8
0
        public static object InvokeMethod(IHostInvokeContext context, object target, MethodInfo method, object[] args)
        {
            var    argList      = new List <object>();
            var    byRefArgInfo = new List <ByRefArgItem>();
            object tailArgsArg  = null;

            var parameters = method.GetParameters();

            for (var index = 0; index < parameters.Length; index++)
            {
                var param = parameters[index];
                if (Attribute.IsDefined(param, typeof(ParamArrayAttribute)))
                {
                    if ((index != (args.Length - 1)) || !param.ParameterType.IsInstanceOfType(args[index]))
                    {
                        var tailArgType = param.ParameterType.GetElementType();
                        var tailArgs    = Array.CreateInstance(tailArgType, args.Length - index);
                        for (var innerIndex = index; innerIndex < args.Length; innerIndex++)
                        {
                            var byRefArg = args[innerIndex] as IByRefArg;
                            if (byRefArg == null)
                            {
                                tailArgs.SetValue(GetCompatibleArg(param.Name, tailArgType, args[innerIndex]), innerIndex - index);
                            }
                            else
                            {
                                tailArgs.SetValue(GetCompatibleArg(param.Name, tailArgType, byRefArg.Value), innerIndex - index);
                                byRefArgInfo.Add(new ByRefArgItem(byRefArg, tailArgs, innerIndex - index));
                            }
                        }

                        argList.Add(tailArgs);
                        tailArgsArg = tailArgs;
                        break;
                    }
                }

                if (index < args.Length)
                {
                    var byRefArg = args[index] as IByRefArg;
                    if (byRefArg == null)
                    {
                        argList.Add(GetCompatibleArg(param, args[index]));
                    }
                    else
                    {
                        argList.Add(GetCompatibleArg(param, byRefArg.Value));
                        byRefArgInfo.Add(new ByRefArgItem(byRefArg, null, index));
                    }
                }
                else if (param.IsOptional)
                {
                    if (param.Attributes.HasFlag(ParameterAttributes.HasDefault))
                    {
                        try
                        {
                            argList.Add(param.DefaultValue);
                        }
                        catch (FormatException)
                        {
                            // undocumented but observed when calling HostFunctions.newVar()
                            argList.Add(null);
                        }
                    }
                    else
                    {
                        argList.Add(Missing.Value);
                    }
                }
                else
                {
                    break;
                }
            }

            var finalArgs = argList.ToArray();
            var result    = method.Invoke(target, finalArgs);

            foreach (var item in byRefArgInfo)
            {
                var array = item.Array ?? finalArgs;
                item.ByRefArg.Value = array.GetValue(item.Index);
            }

            for (var index = 0; index < finalArgs.Length; index++)
            {
                if (index >= args.Length)
                {
                    break;
                }

                var finalArg = finalArgs[index];
                if (ReferenceEquals(finalArg, tailArgsArg))
                {
                    break;
                }

                args[index] = finalArg;
            }

            var type = method.ReturnType;

            if (type == typeof(void))
            {
                return(VoidResult.Value);
            }

            return(context.Engine.PrepareResult(result, type, method.GetScriptMemberFlags(), false));
        }
Exemple #9
0
        public override bool TryInvokeAuxMember(IHostInvokeContext context, string name, BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
        {
            var type = GetSpecificTypeNoThrow();
            if (type != null)
            {
                var nestedTypes = type.GetScriptableNestedTypes(invokeFlags, context.DefaultAccess).Where(testType => testType.GetRootName() == name).ToIList();
                if (nestedTypes.Count > 0)
                {
                    var tempResult = Wrap(nestedTypes.Select(testType => testType.ApplyTypeArguments(type.GetGenericArguments())).ToArray());
                    if (invokeFlags.HasFlag(BindingFlags.InvokeMethod))
                    {
                        return tempResult.TryInvoke(context, invokeFlags, args, bindArgs, out result);
                    }

                    result = tempResult;
                    return true;
                }
            }

            result = null;
            return false;
        }
Exemple #10
0
 public virtual string[] GetAuxMethodNames(IHostInvokeContext context, BindingFlags bindFlags)
 {
     return(MiscHelpers.GetEmptyArray <string>());
 }
Exemple #11
0
        public override bool TryInvoke(IHostInvokeContext context, BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
        {
            if (!invokeFlags.HasFlag(BindingFlags.InvokeMethod) || (args.Length < 1))
            {
                result = null;
                return false;
            }

            if (!args.All(arg => arg is HostType))
            {
                throw new ArgumentException("Invalid generic type argument");
            }

            var templates = types.Where(type => !type.IsSpecific()).ToArray();
            var typeArgs = args.Cast<HostType>().Select(hostType => hostType.GetTypeArg()).ToArray();

            var template = templates.FirstOrDefault(testTemplate => testTemplate.GetGenericParamCount() == typeArgs.Length);
            if (template == null)
            {
                throw new TypeLoadException(MiscHelpers.FormatInvariant("Could not find a matching generic type definition for '{0}'", templates[0].GetRootName()));
            }

            result = Wrap(template.MakeSpecificType(typeArgs));
            return true;
        }
Exemple #12
0
        public override string[] GetAuxPropertyNames(IHostInvokeContext context, BindingFlags bindFlags)
        {
            var type = GetSpecificTypeNoThrow();
            if (type != null)
            {
                return type.GetScriptableNestedTypes(bindFlags, context.DefaultAccess).Select(testType => testType.GetRootName()).Distinct().ToArray();
            }

            return MiscHelpers.GetEmptyArray<string>();
        }
        public static object InvokeMethod(IHostInvokeContext context, object target, MethodInfo method, object[] args)
        {
            var argList = new List<object>();
            var byRefArgInfo = new List<ByRefArgItem>();
            object tailArgsArg = null;

            var parameters = method.GetParameters();
            for (var index = 0; index < parameters.Length; index++)
            {
                var param = parameters[index];
                if (Attribute.IsDefined(param, typeof(ParamArrayAttribute)))
                {
                    if ((index != (args.Length - 1)) || !param.ParameterType.IsInstanceOfType(args[index]))
                    {
                        var tailArgs = Array.CreateInstance(param.ParameterType.GetElementType(), args.Length - index);
                        for (var innerIndex = index; innerIndex < args.Length; innerIndex++)
                        {
                            var byRefArg = args[innerIndex] as IByRefArg;
                            if (byRefArg == null)
                            {
                                tailArgs.SetValue(args[innerIndex], innerIndex - index);
                            }
                            else
                            {
                                tailArgs.SetValue(byRefArg.Value, innerIndex - index);
                                byRefArgInfo.Add(new ByRefArgItem(byRefArg, tailArgs, innerIndex - index));
                            }
                        }

                        argList.Add(tailArgs);
                        tailArgsArg = tailArgs;
                        break;
                    }
                }

                if (index < args.Length)
                {
                    var byRefArg = args[index] as IByRefArg;
                    if (byRefArg == null)
                    {
                        argList.Add(args[index]);
                    }
                    else
                    {
                        argList.Add(byRefArg.Value);
                        byRefArgInfo.Add(new ByRefArgItem(byRefArg, null, index));
                    }
                }
                else if (param.IsOptional)
                {
                    if (param.Attributes.HasFlag(ParameterAttributes.HasDefault))
                    {
                        try
                        {
                            argList.Add(param.DefaultValue);
                        }
                        catch (FormatException)
                        {
                            // undocumented but observed when calling HostFunctions.newVar()
                            argList.Add(null);
                        }
                    }
                    else
                    {
                        argList.Add(Missing.Value);
                    }
                }
                else
                {
                    break;
                }
            }

            var finalArgs = argList.ToArray();
            var result = method.Invoke(target, finalArgs);

            foreach (var item in byRefArgInfo)
            {
                var array = item.Array ?? finalArgs;
                item.ByRefArg.Value = array.GetValue(item.Index);
            }

            for (var index = 0; index < finalArgs.Length; index++)
            {
                if (index >= args.Length)
                {
                    break;
                }

                var finalArg = finalArgs[index];
                if (ReferenceEquals(finalArg, tailArgsArg))
                {
                    break;
                }

                args[index] = finalArg;
            }

            var type = method.ReturnType;
            if (type == typeof(void))
            {
                return VoidResult.Value;
            }

            return context.Engine.PrepareResult(result, type, method.GetScriptMemberFlags());
        }
 public DynamicInvokeMemberBinder(IHostInvokeContext context, string name, BindingFlags invokeFlags, string[] paramNames)
     : base(name, false, new CallInfo(paramNames.Length, paramNames))
 {
     this.context     = context;
     this.invokeFlags = invokeFlags;
 }
Exemple #15
0
 public override bool TryInvokeAuxMember(IHostInvokeContext context, string name, BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
 {
     return(target.TryInvokeAuxMember(context, name, invokeFlags, args, bindArgs, out result));
 }
Exemple #16
0
 public static object InvokeDelegate(IHostInvokeContext context, Delegate del, object[] args)
 {
     return(InvokeMethod(context, del, del.GetType().GetMethod("Invoke"), args));
 }
Exemple #17
0
 public override bool TryInvoke(IHostInvokeContext context, BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
 {
     return(target.TryInvoke(context, invokeFlags, args, bindArgs, out result));
 }
Exemple #18
0
 public override HostTargetFlags GetFlags(IHostInvokeContext context)
 {
     return(HostTargetFlags.None);
 }
 public override string[] GetAuxMethodNames(IHostInvokeContext context, BindingFlags bindFlags)
 {
     return auxMethodNames;
 }
Exemple #20
0
        public override bool TryInvokeAuxMember(IHostInvokeContext context, string name, BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
        {
            const BindingFlags getPropertyFlags =
                BindingFlags.GetField |
                BindingFlags.GetProperty;

            const BindingFlags setPropertyFlags =
                BindingFlags.SetProperty |
                BindingFlags.PutDispProperty |
                BindingFlags.PutRefDispProperty;

            if (name == "out")
            {
                if ((invokeFlags & getPropertyFlags) != 0)
                {
                    result = new OutArg <T>(this);
                    return(true);
                }
            }
            else if (name == "ref")
            {
                if ((invokeFlags & getPropertyFlags) != 0)
                {
                    result = new RefArg <T>(this);
                    return(true);
                }
            }
            else if (name == "value")
            {
                if (invokeFlags.HasFlag(BindingFlags.InvokeMethod))
                {
                    if (InvokeHelpers.TryInvokeObject(context, value, invokeFlags, args, bindArgs, typeof(IDynamicMetaObjectProvider).IsAssignableFrom(typeof(T)), out result))
                    {
                        return(true);
                    }

                    if (invokeFlags.HasFlag(BindingFlags.GetField) && (args.Length < 1))
                    {
                        result = context.Engine.PrepareResult(value, ScriptMemberFlags.None, false);
                        return(true);
                    }

                    result = null;
                    return(false);
                }

                if ((invokeFlags & getPropertyFlags) != 0)
                {
                    result = context.Engine.PrepareResult(value, ScriptMemberFlags.None, false);
                    return(true);
                }

                if ((invokeFlags & setPropertyFlags) != 0)
                {
                    if (args.Length == 1)
                    {
                        result = context.Engine.PrepareResult(((IHostVariable)this).Value = args[0], typeof(T), ScriptMemberFlags.None, false);
                        return(true);
                    }
                }
            }

            result = null;
            return(false);
        }
 public override bool TryInvoke(IHostInvokeContext context, BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
 {
     result = target.InvokeMember(name, invokeFlags.HasFlag(BindingFlags.SetField) ? BindingFlags.SetProperty : BindingFlags.GetProperty, args, bindArgs, null, true);
     return true;
 }
 public virtual bool TryInvoke(IHostInvokeContext context, BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
 {
     result = null;
     return(false);
 }
        public override bool TryInvokeAuxMember(IHostInvokeContext context, string memberName, BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
        {
            if (invokeFlags.HasFlag(BindingFlags.InvokeMethod))
            {
                if (memberName == "get")
                {
                    result = target.InvokeMember(name, BindingFlags.GetProperty, args, bindArgs, null, true);
                    return true;
                }

                if (memberName == "set")
                {
                    result = target.InvokeMember(name, BindingFlags.SetProperty, args, bindArgs, null, true);
                    return true;
                }
            }

            result = null;
            return false;
        }
 public override string[] GetAuxMethodNames(IHostInvokeContext context, BindingFlags bindFlags)
 {
     return(auxMethodNames);
 }
 public static bool TryInvoke(this DynamicMetaObject target, IHostInvokeContext context, object[] args, out object result)
 {
     return(TryDynamicOperation(() => target.Invoke(args), out result));
 }
Exemple #26
0
 public override bool TryInvoke(IHostInvokeContext context, BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
 {
     result = target.InvokeMember(name, invokeFlags, args, bindArgs, null, true);
     return true;
 }
 public static bool TryInvokeMember(this DynamicMetaObject target, IHostInvokeContext context, string name, BindingFlags invokeFlags, object[] args, out object result)
 {
     return(TryDynamicOperation(() => target.InvokeMember(context, name, invokeFlags, args), out result));
 }
Exemple #28
0
 public virtual bool TryInvokeAuxMember(IHostInvokeContext context, string name, BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
 {
     result = null;
     return false;
 }
        public static bool TryInvokeObject(IHostInvokeContext context, object target, BindingFlags invokeFlags, object[] args, object[] bindArgs, bool tryDynamic, out object result)
        {
            var hostTarget = target as HostTarget;
            if (hostTarget != null)
            {
                if (hostTarget.TryInvoke(context, invokeFlags, args, bindArgs, out result))
                {
                    return true;
                }

                if (hostTarget is HostType)
                {
                    return false;
                }

                target = hostTarget.InvokeTarget;
                tryDynamic = tryDynamic && typeof(IDynamicMetaObjectProvider).IsAssignableFrom(hostTarget.Type);
            }

            if ((target != null) && invokeFlags.HasFlag(BindingFlags.InvokeMethod))
            {
                var scriptItem = target as ScriptItem;
                if (scriptItem != null)
                {
                    target = DelegateFactory.CreateFunc<object>(scriptItem.Engine, target, args.Length);
                }

                var del = target as Delegate;
                if (del != null)
                {
                    result = InvokeDelegate(context, del, args);
                    return true;
                }

                if (tryDynamic)
                {
                    var dynamicMetaObjectProvider = target as IDynamicMetaObjectProvider;
                    if (dynamicMetaObjectProvider != null)
                    {
                        if (dynamicMetaObjectProvider.GetMetaObject(Expression.Constant(target)).TryInvoke(context, args, out result))
                        {
                            return true;
                        }
                    }
                }
            }

            result = null;
            return false;
        }