示例#1
0
 public override void Set(JsDictionaryObject that, JsInstance value)
 {
     if (SetFunction == null)
         throw new JsException(global.TypeErrorClass.New());
     //JsDictionaryObject that = global.Visitor.CallTarget;
     global.Visitor.ExecuteFunction(SetFunction, that, new JsInstance[] { value });
 }
示例#2
0
        public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters)
        {
            JsFunction function = that as JsFunction;

            if (function == null) {
                throw new ArgumentException("the target of call() must be a function");
            }
            JsDictionaryObject _this;
            JsInstance[] _parameters;
            if (parameters.Length >= 1)
                _this = parameters[0] as JsDictionaryObject;
            else
                _this = visitor.Global as JsDictionaryObject;

            if (parameters.Length >= 2 && parameters[1] != JsNull.Instance) {
                JsObject arguments = parameters[1] as JsObject;
                if (arguments == null)
                    throw new JsException(visitor.Global.TypeErrorClass.New("second argument must be an array"));
                _parameters = new JsInstance[arguments.Length];
                for (int i = 0; i < arguments.Length; i++) {
                    _parameters[i] = arguments[i.ToString()];
                }
            }
            else {
                _parameters = JsInstance.EMPTY;
            }

            // Executes the statements in 'that' and use _this as the target of the call
            visitor.ExecuteFunction(function, _this, _parameters);
            return visitor.Result;
            //visitor.CallFunction(function, _this, _parameters);

            //return visitor.Result;
        }
示例#3
0
        public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters)
        {
            if (that == null)
            {
                JsArray array = Global.ArrayClass.New();

                for (int i = 0; i < parameters.Length; i++)
                {
                    array[i.ToString()] = parameters[i];
                }

                visitor.Return(array);
            }
            else
            {
                // When called as part of a new expression, it is a constructor: it initialises the newly created object.
                for (int i = 0; i < parameters.Length; i++)
                {
                    that[i.ToString()] = parameters[i];
                }

                visitor.Return(that);
            }

            return that;
        }
示例#4
0
        public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters)
        {
            if (that == null)
            {
                // 15.5.1 - When String is called as a function rather than as a constructor, it performs a type conversion.
                if (parameters.Length > 0)
                {
                    return visitor.Return(Global.StringClass.New(parameters[0].ToString()));
                }
                else
                {
                    return visitor.Return(Global.StringClass.New(String.Empty));
                }
            }
            else
            {
                // 15.5.2 - When String is called as part of a new expression, it is a constructor: it initialises the newly created object.
                if (parameters.Length > 0)
                {
                    that.Value = parameters[0].ToString();
                }
                else
                {
                    that.Value = String.Empty;
                }

                return visitor.Return(that);
            }
        }
示例#5
0
        public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters)
        {
            JsFunction function = that as JsFunction;

            if (function == null)
            {
                throw new ArgumentException("the target of call() must be a function");
            }

            JsDictionaryObject _this;
            JsInstance[] _parameters;
            if (parameters.Length >= 1 && parameters[0] != JsUndefined.Instance && parameters[0] != JsNull.Instance)
                _this = parameters[0] as JsDictionaryObject;
            else
                _this = visitor.Global as JsDictionaryObject;

            if (parameters.Length >= 2 && parameters[1] != JsNull.Instance)
            {
                _parameters = new JsInstance[parameters.Length - 1];
                for (int i = 1; i < parameters.Length; i++)
                {
                    _parameters[i - 1] = parameters[i];
                }
            }
            else
            {
                _parameters = JsInstance.EMPTY;
            }
            // Executes the statements in 'that' and use _this as the target of the call
            visitor.ExecuteFunction(function, _this, _parameters);
            return visitor.Result;
            //visitor.CallFunction(function, _this, _parameters);

            //return visitor.Result;
        }
示例#6
0
 public ValueDescriptor(JsDictionaryObject owner, string name)
     : base(owner, name)
 {
     Enumerable = true;
     Writable = true;
     Configurable = true;
 }
示例#7
0
        public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters)
        {
            if (that == null)
            {
                // 15.7.1 - When Number is called as a function rather than as a constructor, it performs a type conversion.
                if (parameters.Length > 0)
                {
                    return visitor.Return(new JsNumber(parameters[0].ToNumber()));
                }
                else
                {
                    return visitor.Return(new JsNumber(0));
                }
            }
            else
            {
                // 15.7.2 - When Number is called as part of a new expression, it is a constructor: it initialises the newly created object.
                if (parameters.Length > 0)
                {
                    that.Value = parameters[0].ToNumber();
                }
                else
                {
                    that.Value = 0;
                }

                visitor.Return(that);
            }

            return that;
        }
        public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters) {
            if (parameters.Length == 0) {
                return visitor.Return(New());
                //throw new ArgumentNullException("pattern");
            }

            return visitor.Return(New(parameters[0].ToString(), false, false, false));
        }
示例#9
0
 public JsScope(JsScope outer, JsDictionaryObject bag)
     : base(outer) {
     if (outer == null)
         throw new ArgumentNullException("outer");
     if (bag == null)
         throw new ArgumentNullException("bag");
     globalScope = outer.Global;
     this.bag = bag;
 }
示例#10
0
 public NativeDescriptor(JsDictionaryObject owner, NativeDescriptor src)
     : base(owner, src.Name)
 {
     getter = src.getter;
     setter = src.setter;
     Writable = src.Writable;
     Configurable = src.Configurable;
     Enumerable = src.Enumerable;
 }
示例#11
0
 /// <summary>
 /// Constructs new descriptor
 /// </summary>
 /// <param name="owner">An owner of the new descriptor</param>
 /// <param name="name">A name of the new descriptor</param>
 /// <param name="source">A property descriptor of the target object to which we should link to</param>
 /// <param name="that">A target object to whose property we are linking. This parameter will be
 /// used in the calls to a 'Get' and 'Set' properties of the source descriptor.</param>
 public LinkedDescriptor(JsDictionaryObject owner, string name, Descriptor source, JsDictionaryObject that)
     : base(owner, name)
 {
     d = source;
     Enumerable = true;
     Writable = true;
     Configurable = true;
     m_that = that;
 }
示例#12
0
        /// <summary>
        /// 15.5.4.6
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public JsInstance ConcatImpl(JsDictionaryObject target, JsInstance[] parameters)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(target.ToString());

            for (int i = 0; i < parameters.Length; i++) {
                sb.Append(parameters[i].ToString());
            }

            return Global.StringClass.New(sb.ToString());
        }
示例#13
0
        /// <summary>
        /// 15.5.4.5
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance CharCodeAtImpl(JsDictionaryObject target, JsInstance[] parameters)
        {
            var r = target.ToString();
            var at = (int)parameters[0].ToNumber();

            if (r == String.Empty || at > r.Length - 1) {
                return Global.NaN;
            }
            else {
                return Global.NumberClass.New(Convert.ToInt32(r[at]));
            }
        }
示例#14
0
 public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters, Type[] genericArguments)
 {
     if (m_generics.Count == 0 && (genericArguments != null && genericArguments.Length > 0))
         return base.Execute(visitor, that, parameters, genericArguments);
     else
     {
         JsMethodImpl impl = m_overloads.ResolveOverload(parameters, genericArguments);
         if (impl == null)
             throw new JintException(String.Format("No matching overload found {0}<{1}>", Name, genericArguments));
         visitor.Return(impl(visitor.Global, that, parameters));
         return that;
     }
 }
示例#15
0
 /// <summary>
 /// Constructs new descriptor
 /// </summary>
 /// <param name="owner">An owner of the new descriptor</param>
 /// <param name="name">A name of the new descriptor</param>
 /// <param name="source">A property descriptor of the target object to which we should link to</param>
 /// <param name="that">A target object to whose property we are linking. This parameter will be
 /// used in the calls to a 'Get' and 'Set' properties of the source descriptor.</param>
 public LinkedDescriptor(JsDictionaryObject owner, string name, Descriptor source, JsDictionaryObject that)
     : base(owner, name) {
     if (source.isReference) {
         LinkedDescriptor sourceLink = source as LinkedDescriptor;
         d = sourceLink.d;
         m_that = sourceLink.m_that;
     } else
         d = source;
     Enumerable = true;
     Writable = true;
     Configurable = true;
     m_that = that;
 }
示例#16
0
        public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters) {
            if (that == null || (that as IGlobal) == visitor.Global ) {
                return visitor.Return(Construct(parameters,null,visitor));
            }
            else {
                // When called as part of a new expression, it is a constructor: it initialises the newly created object.
                for (int i = 0; i < parameters.Length; i++) {
                    that[i.ToString()] = parameters[i];
                }

                return visitor.Return(that);
            }
        }
示例#17
0
        /// <summary>
        /// 15.2.2.1
        /// </summary>
        public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters)
        {
            if (parameters.Length > 0) {
                switch (parameters[0].Class) {
                    case JsInstance.CLASS_STRING: return Global.StringClass.New(parameters[0].ToString());
                    case JsInstance.CLASS_NUMBER: return Global.NumberClass.New(parameters[0].ToNumber());
                    case JsInstance.CLASS_BOOLEAN: return Global.BooleanClass.New(parameters[0].ToBoolean());
                    default:
                        return parameters[0];
                }
            }

            return New(this);
        }
示例#18
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] = visitor.Global.Marshaller.MarshalJsValue<object>(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;
        }
示例#19
0
        /// <summary>
        /// 8.10.5
        /// </summary>
        /// <param name="global"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        internal static Descriptor ToPropertyDesciptor(IGlobal global, JsDictionaryObject owner, string name, JsInstance jsInstance) {
            if (jsInstance.Class != JsInstance.CLASS_OBJECT) {
                throw new JsException(global.TypeErrorClass.New("The target object has to be an instance of an object"));
            }

            JsObject obj = (JsObject)jsInstance;
            if ((obj.HasProperty("value") || obj.HasProperty("writable")) && (obj.HasProperty("set") || obj.HasProperty("get"))) {
                throw new JsException(global.TypeErrorClass.New("The property cannot be both writable and have get/set accessors or cannot have both a value and an accessor defined"));
            }

            Descriptor desc;
            JsInstance result = null;

            if (obj.HasProperty("value")) {
                desc = new ValueDescriptor(owner, name, obj["value"]);
            }
            else {
                desc = new PropertyDescriptor(global, owner, name);
            }

            if (obj.TryGetProperty("enumerable", out result)) {
                desc.Enumerable = result.ToBoolean();
            }

            if (obj.TryGetProperty("configurable", out result)) {
                desc.Configurable = result.ToBoolean();
            }

            if (obj.TryGetProperty("writable", out result)) {
                desc.Writable = result.ToBoolean();
            }

            if (obj.TryGetProperty("get", out result)) {
                if (!(result is JsFunction))
                    throw new JsException(global.TypeErrorClass.New("The getter has to be a function"));

                ((PropertyDescriptor)desc).GetFunction = (JsFunction)result;
            }

            if (obj.TryGetProperty("set", out result)) {
                if (!(result is JsFunction))
                    throw new JsException(global.TypeErrorClass.New("The setter has to be a function"));

                ((PropertyDescriptor)desc).SetFunction = (JsFunction)result;
            }

            return desc;
        }
示例#20
0
        public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters) {
            try {
                //visitor.CurrentScope["this"] = visitor.Global;
                JsInstance result = Delegate( parameters );
                visitor.Return(result == null ? JsUndefined.Instance : result);

                return that;
            }
            catch (Exception e) {
                if (e.InnerException is JsException) {
                    throw e.InnerException;
                }

                throw;
            }
        }
示例#21
0
        /// <summary>
        /// 15.2.2.1
        /// </summary>
        public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters)
        {
            if (parameters.Length > 0)
            {
                switch (parameters[0].Class)
                {
                    case JsString.TYPEOF: return Global.StringClass.New(parameters[0].ToString());
                    case JsNumber.TYPEOF: return new JsNumber(parameters[0].ToNumber());
                    case JsBoolean.TYPEOF: return new JsBoolean(parameters[0].ToBoolean());
                    default:
                        return parameters[0];
                }
            }

            return New(this);
        }
示例#22
0
 public JsFunctionDelegate(IJintVisitor visitor, JsFunction function, JsDictionaryObject that,Type delegateType)
 {
     if (visitor == null)
         throw new ArgumentNullException("visitor");
     if (function == null)
         throw new ArgumentNullException("function");
     if (delegateType == null)
         throw new ArgumentNullException("delegateType");
     if (!typeof(Delegate).IsAssignableFrom(delegateType))
         throw new ArgumentException("A delegate type is required", "delegateType");
     m_visitor = visitor;
     m_function = function;
     m_delegateType = delegateType;
     m_that = that;
     m_marshaller = visitor.Global.Marshaller;
 }
示例#23
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);
        }
示例#24
0
        public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters) {
            if (that == null || (that as IGlobal) == visitor.Global)
            {
                visitor.Return(parameters.Length > 0 ? New(parameters[0].ToString()) : New());
            }
            else {
                if (parameters.Length > 0) {
                    that.Value = parameters[0].ToString();
                }
                else {
                    that.Value = String.Empty;
                }

                visitor.Return(that);
            }

            return that;
        }
示例#25
0
        // 15.2.4.3 and 15.2.4.4
        public JsInstance ToStringImpl(JsDictionaryObject target, JsInstance[] parameters)
        {
            JsFunction constructor = target.Prototype["constructor"] as JsFunction;

            if (target.Class == JsFunction.TYPEOF)
                return Global.StringClass.New(String.Concat("[object Function]"));

            if (constructor == null)
            {
                return Global.StringClass.New(String.Concat("[object Object]"));
            }
            else
            {
                return Global.StringClass.New(String.Concat("[object ", constructor.Name, "]"));
            }


        }
示例#26
0
        public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters) {
            if (parameters.Length == 0) {
                return visitor.Return(New());
                //throw new ArgumentNullException("pattern");
            }

            bool g = false, m = false, ic = false;

            if (parameters.Length == 2) {
                string strParam = parameters[1].ToString();
                if (strParam != null) {
                    m = strParam.Contains("m");
                    ic = strParam.Contains("i");
                    g = strParam.Contains("g");
                }
            }

            return visitor.Return(New(parameters[0].ToString(), g, ic, m));
        }
        public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters) {
            // e.g., var foo = Boolean(true);
            if (that == null || (that as IGlobal) == visitor.Global)
            {
                visitor.Return(parameters.Length > 0 ? new JsBoolean(parameters[0].ToBoolean(), PrototypeProperty) : new JsBoolean(PrototypeProperty));
            }
            else // e.g., var foo = new Boolean(true);
            {
                if (parameters.Length > 0) {
                    that.Value = parameters[0].ToBoolean();
                }
                else {
                    that.Value = false;
                }

                visitor.Return(that);
            }

            return that;
        }
示例#28
0
 public LinkedDescriptor(
     JsDictionaryObject owner,
     string name,
     Descriptor source,
     JsDictionaryObject that)
     : base(owner, name)
 {
     if (source.isReference)
     {
         LinkedDescriptor linkedDescriptor = source as LinkedDescriptor;
         this.d      = linkedDescriptor.d;
         this.m_that = linkedDescriptor.m_that;
     }
     else
     {
         this.d = source;
     }
     this.Enumerable   = true;
     this.Writable     = true;
     this.Configurable = true;
     this.m_that       = that;
 }
示例#29
0
		public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters)
		{
			try
			{
				JsInstance result;

				switch (argLen)
				{
					case 0:
						result = impl.DynamicInvoke() as JsInstance;
						break;
					case 1:
						result = impl.DynamicInvoke(parameters[0].Value) as JsInstance;
						break;
					case 2:
						result = impl.DynamicInvoke(parameters[0].Value, parameters[1].Value) as JsInstance;
						break;
					default:
						throw new ArgumentOutOfRangeException("argLen");
				}
				
				visitor.Return(result);
				return result;
			}
			catch (ArgumentException)
			{
				var constructor = that["constructor"] as JsFunction;
				throw new JsException(visitor.Global.TypeErrorClass.New("incompatible type: " + constructor == null ? "<unknown>" : constructor.Name));
			}
			catch (Exception e)
			{
				if (e.InnerException is JsException)
				{
					throw e.InnerException;
				}

				throw;
			}
		}
示例#30
0
 public JsFunctionDelegate(IJintVisitor visitor, JsFunction function, JsDictionaryObject that, Type delegateType)
 {
     if (visitor == null)
     {
         throw new ArgumentNullException("visitor");
     }
     if (function == null)
     {
         throw new ArgumentNullException("function");
     }
     if (delegateType == null)
     {
         throw new ArgumentNullException("delegateType");
     }
     if (!typeof(Delegate).IsAssignableFrom(delegateType))
     {
         throw new ArgumentException("A delegate type is required", "delegateType");
     }
     m_visitor      = visitor;
     m_function     = function;
     m_delegateType = delegateType;
     m_that         = that;
     m_marshaller   = visitor.Global.Marshaller;
 }
示例#31
0
 public ClrEntityDescriptor(IEntityAccessor entityAccessor, IGlobal global, JsDictionaryObject owner, string name)
     : base(owner, name)
 {
     _entityAccessor = entityAccessor;
     _global         = global;
 }
示例#32
0
 public override void Set(JsDictionaryObject that, JsInstance value)
 {
     _entityAccessor.SetValue(that.Value, Name, value.Value);
 }
示例#33
0
        public override JsInstance Get(JsDictionaryObject that)
        {
            object value = _entityAccessor.GetValue(that.Value, Name);

            return(_global.Visitor.Return(_global.WrapClr(value)));
        }
示例#34
0
 public override JsInstance Get(JsDictionaryObject that)
 {
     return(this.d.Get(that));
 }
示例#35
0
 public void ExecuteFunction(JsFunction function, JsDictionaryObject _this, JsInstance[] _parameters)
 {
     throw new NotImplementedException();
 }
示例#36
0
        // 15.2.4.6
        public JsInstance IsPrototypeOfImpl(JsDictionaryObject target, JsInstance[] parameters) {
            if (target.Class != JsInstance.CLASS_OBJECT) {
                return Global.BooleanClass.False;
            }

            while (true) {
                IsPrototypeOf(target);
                if (target == null) {
                    return Global.BooleanClass.True;
                }

                if (target == this) {
                    return Global.BooleanClass.True;
                }
            }
        }
示例#37
0
 public override void Set(JsDictionaryObject that, JsInstance value)
 {
     this.d.Set(that, value);
 }
示例#38
0
        // 15.2.4.7
        public JsInstance PropertyIsEnumerableImpl(JsDictionaryObject target, JsInstance[] parameters) {
            if (!HasOwnProperty(parameters[0])) {
                return Global.BooleanClass.False;
            }

            var v = target[parameters[0]];

            return Global.BooleanClass.New((v.Attributes & PropertyAttributes.DontEnum) == PropertyAttributes.None);
        }
示例#39
0
 public void CallFunction(JsFunction jsFunction, JsDictionaryObject that, JsInstance[] parameters)
 {
     throw new NotImplementedException();
 }