Beispiel #1
0
        private static object GetMemberValue(object obj, string memberInfo)
        {
            // Check whether we are getting member value from a dynamic object
            var dynamicObject = obj as DynamicObject;

            if (dynamicObject != null)
            {
                // Get or create a new call site for property getter
                var getter = CallSiteCache.GetOrCreatePropertyGetter(memberInfo);
                if (getter == null)
                {
                    throw new ScriptIdNotFoundException(memberInfo);
                }
                // get property value from dynamic object
                return(getter.Target(getter, obj));
            }

            var bind = RuntimeHost.Binder.BindToMember(obj, memberInfo, true);

            if (bind == null)
            {
                throw new ScriptIdNotFoundException(memberInfo);
            }

            return(bind.GetValue());
        }
Beispiel #2
0
        private void SetMember(IScriptContext context, object obj, object value)
        {
            // Check whether we are setting member value for a dynamic object
            var dynamicObject = obj as DynamicObject;

            if (dynamicObject != null)
            {
                // Get or create a new call site for a setter
                var setter = CallSiteCache.GetOrCreatePropertySetter(_identifier);
                if (setter == null)
                {
                    throw new ScriptIdNotFoundException(_identifier);
                }
                // set property value for the dynamic object
                setter.Target(setter, obj, value);
            }
            else
            {
                IMemberBinding bind = RuntimeHost.Binder.BindToMember(obj, _identifier, true);
                if (bind == null)
                {
                    throw new ScriptIdNotFoundException(_identifier);
                }
                bind.SetValue(value);
            }

            context.Result = value;
        }
Beispiel #3
0
 public CallSite(Symbol methodName,
                 Visibility visibility = Visibility.Public,
                 IEnumerable <ArgumentKind> argumentKinds = null)
 {
     MethodName    = methodName;
     Visibility    = visibility;
     ArgumentKinds = System.Array.AsReadOnly(argumentKinds?.ToArray() ?? System.Array.Empty <ArgumentKind>());
     CallCache     = new EmptyCallSiteCache(this);
 }
Beispiel #4
0
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        // Logic adapted from JsonSerializerInternalReader.CreateDynamic()
        // https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs#L1751
        // By James Newton-King https://github.com/JamesNK

        var contract = (JsonDynamicContract)serializer.ContractResolver.ResolveContract(objectType);

        if (reader.TokenType == JsonToken.Null)
        {
            return(null);
        }
        var jObj = JObject.Load(reader);
        var used = new HashSet <string>();
        var obj  = CreateObject(jObj, objectType, serializer, used);

        foreach (var jProperty in jObj.Properties())
        {
            var memberName = jProperty.Name;
            if (used.Contains(memberName))
            {
                continue;
            }
            // first attempt to find a settable property, otherwise fall back to a dynamic set without type
            JsonProperty property = contract.Properties.GetClosestMatchProperty(memberName);
            if (property != null && property.Writable && !property.Ignored)
            {
                var propertyValue = jProperty.Value.ToObject(property.PropertyType, serializer);
                property.ValueProvider.SetValue(obj, propertyValue);
            }
            else
            {
                object propertyValue;
                if (jProperty.Value.Type == JTokenType.Null)
                {
                    propertyValue = null;
                }
                else if (jProperty.Value is JValue)
                {
                    // Primitive
                    propertyValue = ((JValue)jProperty.Value).Value;
                }
                else
                {
                    propertyValue = jProperty.Value.ToObject <IDynamicMetaObjectProvider>(serializer);
                }
                // Unfortunately the following is not public!
                // contract.TrySetMember(obj, memberName, propertyValue);
                // So we have to duplicate the logic of what Json.NET has already done.
                CallSiteCache.SetValue(memberName, obj, propertyValue);
            }
        }
        return(obj);
    }
        public override IMessage Invoke(IMessage msg)
        {
            var call = msg as IMethodCallMessage;

            if (call == null)
            {
                throw new ArgumentException("QueryLanguageProxy only supports call messages");
            }

            if (!Session.Current.IsCapturing)
            {
                return(ForwardCall(call));
            }

            var method = (System.Reflection.MethodInfo)call.MethodBase;

            int skipFrames;

            // Walk the stack until we find the first invocation to IQueryLanguage, this is expensive
            // but fortunately unique per method so we can cache it.
            if (!_skipFrameCount.TryGetValue(method, out skipFrames))
            {
                skipFrames = -1;

                var trace  = new StackTrace(0, false);
                var frames = trace.GetFrames();

                for (int i = 0; i < frames.Length; i++)
                {
                    var dt = frames[i].GetMethod();

                    if (IsRemotingWrapper(dt))
                    {
                        for (; i < frames.Length; i++)
                        {
                            if (!IsRemotingWrapper(frames[i].GetMethod()))
                            {
                                skipFrames = _skipFrameCount.GetOrAdd(method, i + 1);
                                break;
                            }
                        }

                        break;
                    }
                }
            }

            var callSite = skipFrames == -1 ? null : CallSiteCache.Get(skipFrames + 1);
            var handler  = GetHandler(call, method, callSite);

            return(handler(call));
        }
Beispiel #6
0
 public static CallSite Create(MethodInfo info, ushort id, CallSiteCache cache)
 {
     Type[] args = new Type[7];
     var parameters = info.GetParameters();
     int i = 0;
     if(!info.IsStatic)
     {
         args[i] = info.DeclaringType;
         i++;
     }
     for (int x = 0; x < parameters.Length; x++, i++)
     {
         args[i] = parameters[x].ParameterType;
     }
     for (; i < args.Length; i++)
     {
         args[i] = typeof(DBNull);
     }
     var type = typeof(CallSite<,,,,,,>).MakeGenericType(args);
     var result = (CallSite)Activator.CreateInstance(type);
     result.Arguments = args;
     result.Build(info, id, cache);
     return result;
 }
 public override object this[object target, string name]
 {
     get { return(CallSiteCache.GetValue(name, target)); }
     set { CallSiteCache.SetValue(name, target, value); }
 }
Beispiel #8
0
 public abstract void Build(MethodInfo info, ushort id, CallSiteCache cache);