예제 #1
0
        public void Visit(Identifier expression)
        {
            Result = null;

            string propertyName = lastIdentifier = expression.Text;


            // Search for .NET property or method
            if (CurrentScope.IsClr && CurrentScope.Value != null)
            {
                EnsureClrAllowed();

                var propertyInfo = propertyGetter.GetValue(CurrentScope.Value, propertyName);
                if (propertyInfo != null)
                {
                    Result = Global.WrapClr(propertyInfo.GetValue(CurrentScope.Value, null));
                    return;
                }

                var fieldInfo = fieldGetter.GetValue(CurrentScope.Value, propertyName);
                if (fieldInfo != null)
                {
                    Result = Global.WrapClr(fieldInfo.GetValue(CurrentScope.Value));
                    return;
                }

                // Not a property, then must be a method
                Result = new JsClrMethodInfo(propertyName);
                return;

                throw new JintException("Invalid property name: " + propertyName);
            }

            // escalade scopes
            JsDictionaryObject oldCallTarget = callTarget;
            try
            {
                foreach (JsDictionaryObject scope in Scopes.ToArray())
                {
                    callTarget = scope;
                    JsInstance result = null;
                    if (scope.TryGetProperty(propertyName, out result))
                    {
                        Result = result;
                        if (Result != null)
                            return;
                    }
                }
            }
            finally
            {
                callTarget = oldCallTarget;
            }

            if (propertyName == "null")
            {
                Result = JsNull.Instance;
                return;
            }

            if (propertyName == "undefined")
            {
                Result = JsUndefined.Instance;
                return;
            }

            // Try to record full path in case it's a type
            if (Result == null)
            {
                typeFullname.Append(propertyName);
            }

            Result = JsUndefined.Instance;
        }
예제 #2
0
        public void Visit(PropertyExpression expression)
        {

            Result = null;

            string propertyName = lastIdentifier = expression.Text;

            if (propertyName == JsDictionaryObject.PROTOTYPE)
            {
                Result = CurrentScope.Prototype;
                return;
            }

            JsInstance result = null;

            JsDictionaryObject oldCallTarget = callTarget;
            callTarget = CurrentScope;
            try
            {// Closure ?
                callTarget = CurrentScope;
                if (CurrentScope.Class == JsFunction.TYPEOF)
                {
                    JsScope scope = ((JsFunction)CurrentScope).Scope;
                    if (scope.TryGetProperty(propertyName, out result))
                    {
                        Result = result;
                        return;
                    }
                }

                callTarget = CurrentScope;
                if (CurrentScope.TryGetProperty(propertyName, out result))
                {
                    Result = result;
                    return;
                }
            }
            finally
            {
                callTarget = oldCallTarget;
            }


            // Search for .NET property or method
            if (CurrentScope.IsClr && CurrentScope.Value != null)
            {
                EnsureClrAllowed();

                // enum ?
                var type = CurrentScope.Value as Type;
                if (type != null && type.IsEnum)
                {
                    Result = new JsClr(this, Enum.Parse(type, propertyName));
                    return;
                }

                var propertyInfo = propertyGetter.GetValue(CurrentScope.Value, propertyName);
                if (propertyInfo != null)
                {
                    Result = Global.WrapClr(propertyInfo.GetValue(CurrentScope.Value, null));
                    return;
                }


                var fieldInfo = fieldGetter.GetValue(CurrentScope.Value, propertyName);
                if (fieldInfo != null)
                {
                    Result = Global.WrapClr(fieldInfo.GetValue(CurrentScope.Value));
                    return;
                }

                // Not a property, then must be a method
                Result = new JsClrMethodInfo(propertyName);
                return;

                throw new JintException("Invalid property name: " + propertyName);
            }


            // Search for a static CLR call
            if (Result == null && typeFullname.Length > 0)
            {
                Type type = typeResolver.ResolveType(typeFullname.ToString());

                if (type != null)
                {
                    EnsureClrAllowed();

                    var propertyInfo = propertyGetter.GetValue(type, propertyName);
                    if (propertyInfo != null)
                    {
                        Result = Global.WrapClr(propertyInfo.GetValue(type, null));
                        return;
                    }

                    var fieldInfo = fieldGetter.GetValue(type, propertyName);
                    if (fieldInfo != null)
                    {
                        Result = Global.WrapClr(fieldInfo.GetValue(type));
                        return;
                    }

                    // Not a property, then must be a method
                    Result = new JsClrMethodInfo(propertyName);
                    return;

                    throw new JintException("Invalid property name: " + propertyName);
                }
            }

            if (Result == null && typeFullname.Length > 0)
            {
                typeFullname.Append('.').Append(propertyName);
            }

            Result = JsUndefined.Instance;
        }