Beispiel #1
0
        public object GetCustomMember(CodeContext context, string name)
        {
            object value;

            if (Scope.TryGetName(context.LanguageContext, SymbolTable.StringToId(name), out value))
            {
                if (value != Uninitialized.Instance)
                {
                    return(value);
                }
            }
            return(null);
        }
Beispiel #2
0
        public ParameterWrapper(ActionBinder binder, ParameterInfo info)
            : this(binder, info.ParameterType)
        {
            _name = SymbolTable.StringToId(info.Name ?? "<unknown>");

#if FULL
            _prohibitNull = info.IsDefined(typeof(NotNullAttribute), false);
#endif

            _isParams = info.IsDefined(typeof(ParamArrayAttribute), false);

#if FULL
            _isParamsDict = info.IsDefined(typeof(ParamDictionaryAttribute), false);
#endif
        }
Beispiel #3
0
 public bool Remove(KeyValuePair <object, object> item)
 {
     lock (this) {
         string strKey = item.Key as string;
         if (strKey != null)
         {
             object value;
             if (AsObjectKeyedDictionary().TryGetValue(strKey, out value) && value == item.Value)
             {
                 _data.Remove(SymbolTable.StringToId(strKey));
                 return(true);
             }
         }
     }
     return(false);
 }
Beispiel #4
0
        public void Add(KeyValuePair <object, object> item)
        {
            string strKey = item.Key as string;

            lock (this) {
                if (strKey != null)
                {
                    _data.Add(SymbolTable.StringToId(strKey), item.Value);
                }
                else
                {
                    Dictionary <object, object> objData = GetObjectKeysDictionary();
                    objData[item.Key] = item.Value;
                }
            }
        }
Beispiel #5
0
        void IDictionary <object, object> .Add(object key, object value)
        {
            Debug.Assert(!(key is SymbolId));

            string strKey = key as string;

            lock (this) {
                if (strKey != null)
                {
                    _data.Add(SymbolTable.StringToId(strKey), value);
                }
                else
                {
                    Dictionary <object, object> objData = GetObjectKeysDictionary();
                    objData[key] = value;
                }
            }
        }
        /// <summary>
        /// Attemps to get the value stored under the specified object key.
        ///
        /// Returns true if the key was found, false if not found.
        /// </summary>
        public virtual bool TryGetObjectValue(object name, out object value)
        {
            string strKey = name as string;

            if (strKey != null)
            {
                return(((IAttributesCollection)this).TryGetValue(SymbolTable.StringToId(strKey), out value));
            }

            lock (this) {
                Dictionary <object, object> objData = GetObjectKeysDictionaryIfExists();
                if (objData != null)
                {
                    return(objData.TryGetValue(name, out value));
                }
            }
            value = null;
            return(false);
        }
Beispiel #7
0
        void IDictionary.Remove(object key)
        {
            Debug.Assert(!(key is SymbolId));
            string strKey = key as string;

            lock (this) {
                if (strKey != null)
                {
                    _data.Remove(SymbolTable.StringToId(strKey));
                }
                else
                {
                    Dictionary <object, object> objData = GetObjectKeysDictionaryIfExists();
                    if (objData != null)
                    {
                        objData.Remove(key);
                    }
                }
            }
        }
Beispiel #8
0
 public object this[object key] {
     get {
         Debug.Assert(!(key is SymbolId));
         string strKey = key as string;
         lock (this) {
             if (strKey != null)
             {
                 object value;
                 if (_data.TryGetValue(SymbolTable.StringToId(strKey), out value))
                 {
                     return(value);
                 }
             }
             else
             {
                 Dictionary <object, object> objData = GetObjectKeysDictionaryIfExists();
                 if (objData != null)
                 {
                     return(objData[key]);
                 }
             }
         }
         throw new KeyNotFoundException(String.Format("'{0}'", key));
     }
     set {
         Debug.Assert(!(key is SymbolId));
         string strKey = key as string;
         lock (this) {
             if (strKey != null)
             {
                 _data[SymbolTable.StringToId(strKey)] = value;
             }
             else
             {
                 Dictionary <object, object> objData = GetObjectKeysDictionary();
                 objData[key] = value;
             }
         }
     }
 }
Beispiel #9
0
        bool IDictionary <object, object> .TryGetValue(object key, out object value)
        {
            Debug.Assert(!(key is SymbolId));
            string strKey = key as string;

            lock (this) {
                if (strKey != null)
                {
                    return(_data.TryGetValue(SymbolTable.StringToId(strKey), out value));
                }
                else
                {
                    value = null;
                    Dictionary <object, object> objData = GetObjectKeysDictionaryIfExists();
                    if (objData == null)
                    {
                        return(false);
                    }
                    return(objData.TryGetValue(key, out value));
                }
            }
        }
        void IDictionary.Remove(object key)
        {
            Debug.Assert(!(key is SymbolId));
            string strKey = key as string;

            if (strKey != null)
            {
                SymbolId id = SymbolTable.StringToId(strKey);
                if (TrySetExtraValue(id, Uninitialized.Instance))
                {
                    return;
                }

                lock (this) if (_data != null)
                    {
                        _data.Remove(id);
                    }
            }
            else
            {
                RemoveObjectKey(key);
            }
        }
Beispiel #11
0
        public object[] GetEnvironmentVariables()
        {
            var envs = new List <object>();

            var scope = Scope;

            while (scope != null && scope != scope.ModuleScope)
            {
                var nv = new Hashtable();

                foreach (string s in scope.Dict.Keys)
                {
                    SymbolId i = SymbolTable.StringToId(s);
                    nv.Add(i, scope.LookupName(i));
                }

                envs.Add(nv);

                scope = scope.Parent;
            }

            return(envs.ToArray());
        }
Beispiel #12
0
            public bool CheckEnumerable(object name)
            {
                if (name is SymbolId)
                {
                    return(CheckEnumerable((SymbolId)name));
                }

                ScopeMemberAttributes scopeAttrs;

                if (_objectAttrs != null)
                {
                    return(!_objectAttrs.TryGetValue(name, out scopeAttrs) ||
                           (scopeAttrs & ScopeMemberAttributes.DontEnumerate) == 0);
                }
                else
                {
                    string stringName = name as string;
                    if (stringName != null)
                    {
                        return(CheckEnumerable(SymbolTable.StringToId(stringName)));
                    }
                }
                return(true);
            }
Beispiel #13
0
        public void SetMemberAfter(string name, object value)
        {
            OnModuleChange(new ModuleChangeEventArgs(SymbolTable.StringToId(name), ModuleChangeType.Set, value));

            Scope.SetName(SymbolTable.StringToId(name), value);
        }
Beispiel #14
0
        // When leaving a context we serialize out our ID as a name
        // rather than a raw ID.  When we enter a new context we
        // consult it's FieldTable to get the ID of the symbol name in
        // the new context.

        private SymbolId(SerializationInfo info, StreamingContext context)
        {
            Contract.RequiresNotNull(info, "info");

            _id = SymbolTable.StringToId(info.GetString("symbolName"))._id;
        }
Beispiel #15
0
        private MethodCandidate MakeByRefReducedMethodTarget(MethodBase method)
        {
            List <ParameterWrapper> parameters = new List <ParameterWrapper>();
            int        argIndex = 0;
            ArgBuilder instanceBuilder;

            if (!CompilerHelpers.IsStatic(method))
            {
                parameters.Add(new ParameterWrapper(_binder, method.DeclaringType, true));
                instanceBuilder = new SimpleArgBuilder(argIndex++, parameters[0].Type);
            }
            else
            {
                instanceBuilder = new NullArgBuilder();
            }

            List <ArgBuilder> argBuilders = new List <ArgBuilder>();

            List <int> returnArgs = new List <int>();

            if (CompilerHelpers.GetReturnType(method) != typeof(void))
            {
                returnArgs.Add(-1);
            }

            int paramCount = 0;

            foreach (ParameterInfo pi in method.GetParameters())
            {
                if (pi.ParameterType == typeof(CodeContext) && paramCount == 0)
                {
                    argBuilders.Add(new ContextArgBuilder());
                    continue;
                }
                paramCount++;

                int newIndex = 0, kwIndex = -1;
                if (!CompilerHelpers.IsOutParameter(pi))
                {
                    kwIndex = GetKeywordIndex(pi);
                    if (kwIndex == -1)
                    {
                        newIndex = argIndex++;
                    }
                }

                ArgBuilder ab;
                if (CompilerHelpers.IsOutParameter(pi))
                {
                    returnArgs.Add(argBuilders.Count);
                    ab = new OutArgBuilder(pi);
                }
                else if (pi.ParameterType.IsByRef)
                {
                    returnArgs.Add(argBuilders.Count);
                    ParameterWrapper param = new ParameterWrapper(_binder, pi.ParameterType.GetElementType(), SymbolTable.StringToId(pi.Name));
                    parameters.Add(param);
                    ab = new ReturnReferenceArgBuilder(newIndex, pi.ParameterType.GetElementType());
                }
                else
                {
                    ParameterWrapper param = new ParameterWrapper(_binder, pi);
                    parameters.Add(param);
                    ab = new SimpleArgBuilder(newIndex, param.Type, pi);
                }

                if (kwIndex == -1)
                {
                    argBuilders.Add(ab);
                }
                else
                {
                    argBuilders.Add(new KeywordArgBuilder(ab, _kwArgs.Length, kwIndex));
                }
            }

            ReturnBuilder returnBuilder = MakeKeywordReturnBuilder(
                new ByRefReturnBuilder(_binder, returnArgs),
                method.GetParameters(),
                parameters);

            return(MakeMethodCandidate(method, parameters, instanceBuilder, argBuilders, returnBuilder));
        }
Beispiel #16
0
        private void AddBasicMethodTargets(MethodBase method)
        {
            List <ParameterWrapper> parameters = new List <ParameterWrapper>();
            int        argIndex = 0;
            ArgBuilder instanceBuilder;
            bool       hasDefaults = false;

            if (!CompilerHelpers.IsStatic(method))
            {
                parameters.Add(new ParameterWrapper(_binder, method.DeclaringType, true));
                instanceBuilder = new SimpleArgBuilder(argIndex++, parameters[0].Type);
            }
            else
            {
                instanceBuilder = new NullArgBuilder();
            }

            ParameterInfo[]   methodParams    = method.GetParameters();
            List <ArgBuilder> argBuilders     = new List <ArgBuilder>(methodParams.Length);
            List <ArgBuilder> defaultBuilders = new List <ArgBuilder>();
            bool hasByRefOrOut = false;

            foreach (ParameterInfo pi in methodParams)
            {
                if (pi.ParameterType == typeof(CodeContext) && argBuilders.Count == 0)
                {
                    argBuilders.Add(new ContextArgBuilder());
                    continue;
                }

                int newIndex, kwIndex = GetKeywordIndex(pi);
                if (kwIndex == -1)
                {
                    if (!CompilerHelpers.IsMandatoryParameter(pi))
                    {
                        defaultBuilders.Add(new DefaultArgBuilder(pi.ParameterType, pi.DefaultValue));
                        hasDefaults = true;
                    }
                    else if (defaultBuilders.Count > 0)
                    {
                        defaultBuilders.Add(null);
                    }
                    newIndex = argIndex++;
                }
                else
                {
                    defaultBuilders.Add(null);
                    newIndex = 0;
                }

                ArgBuilder ab;
                if (pi.ParameterType.IsByRef)
                {
                    hasByRefOrOut = true;
                    Type             refType = typeof(StrongBox <>).MakeGenericType(pi.ParameterType.GetElementType());
                    ParameterWrapper param   = new ParameterWrapper(_binder, refType, true, SymbolTable.StringToId(pi.Name));
                    parameters.Add(param);
                    ab = new ReferenceArgBuilder(newIndex, param.Type);
                }
                else
                {
                    hasByRefOrOut |= CompilerHelpers.IsOutParameter(pi);
                    ParameterWrapper param = new ParameterWrapper(_binder, pi);
                    parameters.Add(param);
                    ab = new SimpleArgBuilder(newIndex, param.Type, pi);
                }

                if (kwIndex == -1)
                {
                    argBuilders.Add(ab);
                }
                else
                {
                    argBuilders.Add(new KeywordArgBuilder(ab, _kwArgs.Length, kwIndex));
                }
            }

            ReturnBuilder returnBuilder = MakeKeywordReturnBuilder(
                new ReturnBuilder(CompilerHelpers.GetReturnType(method)),
                methodParams,
                parameters);

            if (hasDefaults)
            {
                for (int defaultsUsed = 1; defaultsUsed < defaultBuilders.Count + 1; defaultsUsed++)
                {
                    // if the left most default we'll use is not present then don't add a default.  This happens in cases such as:
                    // a(a=1, b=2, c=3) and then call with a(a=5, c=3).  We'll come through once for c (no default, skip),
                    // once for b (default present, emit) and then a (no default, skip again).  W/o skipping we'd generate the same
                    // method multiple times.  This also happens w/ non-contigious default values, e.g. foo(a, b=3, c) where we don't want
                    // to generate a default candidate for just c which matches the normal method.
                    if (defaultBuilders[defaultBuilders.Count - defaultsUsed] != null)
                    {
                        AddTarget(MakeDefaultCandidate(
                                      method,
                                      parameters,
                                      instanceBuilder,
                                      argBuilders,
                                      defaultBuilders,
                                      returnBuilder,
                                      defaultsUsed));
                    }
                }
            }

            if (hasByRefOrOut)
            {
                AddSimpleTarget(MakeByRefReducedMethodTarget(method));
            }
            AddSimpleTarget(MakeMethodCandidate(method, parameters, instanceBuilder, argBuilders, returnBuilder));
        }
Beispiel #17
0
 /// <summary>
 /// Attemps to remove the provided name from this scope
 /// </summary>
 public bool RemoveVariable(string name)
 {
     return(_scope.TryRemoveName(InvariantContext.Instance, SymbolTable.StringToId(name)));
 }
Beispiel #18
0
 /// <summary>
 /// Determines if this context or any outer scope contains the defined name.
 /// </summary>
 public bool VariableExists(string name)
 {
     return(_scope.ContainsName(InvariantContext.Instance, SymbolTable.StringToId(name)));
 }
Beispiel #19
0
 /// <summary>
 /// Sets the name to the specified value for the current context.
 /// </summary>
 public void SetVariable(string name, object value)
 {
     _scope.SetName(SymbolTable.StringToId(name), value);
 }
Beispiel #20
0
 /// <summary>
 /// Attempts to lookup the provided name in this scope or any outer scope.   If the
 /// name is not defined MissingMemberException is thrown.
 /// </summary>
 public object LookupVariable(string name)
 {
     return(_scope.LookupName(InvariantContext.Instance, SymbolTable.StringToId(name)));
 }
Beispiel #21
0
 /// <summary>
 /// Attempts to lookup the provided name in this scope or any outer scope.
 /// </summary>
 public bool TryLookupVariable(string name, out object value)
 {
     return(_scope.TryLookupName(InvariantContext.Instance, SymbolTable.StringToId(name), out value));
 }