Ejemplo n.º 1
0
        public void ExecuteCallback(object obj, object state, object args)
        {
            var visitor = (IJintVisitor)obj;

            JsInstance[] jsPrameters = JsClr.ConvertParametersBack(visitor, new[] { state, args });
            visitor.ExecuteFunction(this, null, jsPrameters);
        }
Ejemplo n.º 2
0
        public void ExecuteStandalone(object obj, params object[] parameters)
        {
            var visitor = (IJintVisitor)obj;

            JsInstance[] jsPrameters = JsClr.ConvertParametersBack(visitor, parameters);
            visitor.ExecuteFunction(this, null, jsPrameters);
        }
        public JsClr WrapClr(object value)
        {
            if (value == null)
            {
                return(new JsClr(Visitor, null));
            }

            JsClr clr = new JsClr(Visitor, value);

            switch (Convert.GetTypeCode(value))
            {
            case TypeCode.Boolean:
                clr.Prototype = BooleanClass.Prototype;
                break;

            case TypeCode.Char:
            case TypeCode.String:
                clr.Prototype = StringClass.Prototype;
                break;

            case TypeCode.DateTime:
                clr.Prototype = DateClass.Prototype;
                break;

            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.SByte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
            case TypeCode.Decimal:
            case TypeCode.Double:
            case TypeCode.Single:
                clr.Prototype = NumberClass.Prototype;
                break;

            case TypeCode.Object:
            case TypeCode.DBNull:
            case TypeCode.Empty:
            default:
                if (value is System.Collections.IEnumerable)
                {
                    clr.Prototype = ArrayClass.Prototype;
                }
                else
                {
                    clr.Prototype = ObjectClass.Prototype;
                }
                break;
            }

            return(clr);
        }
Ejemplo n.º 4
0
        public override void Set(JsDictionaryObject that, JsInstance value)
        {
            object[] nativeValue = JsClr.ConvertParameters(value);
            System.Reflection.PropertyInfo pi = getter.GetValue(that.Value, Name, nativeValue);

            object val = nativeValue [0];

            if (!pi.PropertyType.Equals(val.GetType()))
            {
                Type t = pi.PropertyType;
                if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    t = Nullable.GetUnderlyingType(t);
                }
                val = Convert.ChangeType(val, t);
            }

            pi.SetValue(that.Value, val, null);
        }
Ejemplo n.º 5
0
        public JsClr WrapClr(object value)
        {
            if (value == null)
            {
                return new JsClr(Visitor, null);
            }

            JsClr clr = new JsClr(Visitor, value);

            switch (Convert.GetTypeCode(value))
            {
                case TypeCode.Boolean:
                    clr.Prototype = BooleanClass.Prototype;
                    break;
                case TypeCode.Char:
                case TypeCode.String:
                    clr.Prototype = StringClass.Prototype;
                    break;
                case TypeCode.DateTime:
                    clr.Prototype = DateClass.Prototype;
                    break;
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.SByte:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:
                case TypeCode.Decimal:
                case TypeCode.Double:
                case TypeCode.Single:
                    clr.Prototype = NumberClass.Prototype;
                    break;
                case TypeCode.Object:
                case TypeCode.DBNull:
                case TypeCode.Empty:
                default:
                    if (value is System.Collections.IEnumerable)
                        clr.Prototype = ArrayClass.Prototype;
                    else
                        clr.Prototype = ObjectClass.Prototype;
                    break;
            }

            return clr;
        }
Ejemplo n.º 6
0
 public override void Set(JsDictionaryObject that, JsInstance value)
 {
     object[] nativeValue = JsClr.ConvertParameters(value);
     getter.GetValue(that.Value, Name, nativeValue).SetValue(that.Value, nativeValue[0], null);
 }
Ejemplo n.º 7
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;
        }
Ejemplo n.º 8
0
        public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters)
        {
            int clrParameterCount = Delegate.Method.GetParameters().Length;

            object[] clrParameters = new object[clrParameterCount];

            for (int i = 0; i < parameters.Length; i++)
            {
                // First see if either the JsInstance or it's value can be directly accepted without converstion
                if (typeof(JsInstance).IsAssignableFrom(Parameters[i].ParameterType) && Parameters[i].ParameterType.IsInstanceOfType(parameters[i]))
                {
                    clrParameters[i] = parameters[i];
                }
                else if (Parameters[i].ParameterType.IsInstanceOfType(parameters[i].Value))
                {
                    clrParameters[i] = parameters[i].Value;
                }
                else
                {
                    clrParameters[i] = JsClr.ConvertParameter(parameters[i]);
                }
            }

            object result;

            try
            {
                result = Delegate.DynamicInvoke(clrParameters);
            }
            catch (TargetInvocationException e)
            {
                throw e.InnerException;
            }
            catch (Exception e)
            {
                if (e.InnerException is JsException)
                {
                    throw e.InnerException;
                }

                throw;
            }

            if (result != null)
            {
                // Don't wrap if the result should be a JsInstance
                if (typeof(JsInstance).IsInstanceOfType(result))
                {
                    visitor.Return((JsInstance)result);
                }
                else
                {
                    visitor.Return(visitor.Global.WrapClr(result));
                }
            }
            else
            {
                visitor.Return(JsUndefined.Instance);
            }

            return(null);
        }
Ejemplo n.º 9
0
 public Exception CreateException(object innerObject)
 {
     var jsClr = new JsClr(visitor, innerObject);
     return new JsException(jsClr);
 }