public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters)
        {
            JsDate result = Construct(parameters);

            if (that == null)
            {
                return(visitor.Return(ToStringImpl(result, JsInstance.EMPTY)));
            }
            else
            {
                return(result);
            }
        }
Example #2
0
        public JsInstance SetUTCDateImpl(JsDictionaryObject target, JsInstance[] parameters)
        {
            if (parameters.Length == 0)
            {
                throw new ArgumentException("There was no date specified");
            }
            DateTime dateTime = JsDateConstructor.CreateDateTime(target.ToNumber());

            dateTime     = dateTime.AddDays((double)-dateTime.Day);
            dateTime     = dateTime.AddDays(parameters[0].ToNumber());
            target.Value = (object)dateTime;
            return((JsInstance)target);
        }
Example #3
0
        /// <summary>
        /// 15.9.5.36
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance SetDateImpl(JsDictionaryObject target, JsInstance[] parameters)
        {
            if (parameters.Length == 0)
            {
                throw new ArgumentException("There was no date specified");
            }
            DateTime valueOf = CreateDateTime(target.ToNumber()).ToLocalTime();

            valueOf      = valueOf.AddDays(-valueOf.Day);
            valueOf      = valueOf.AddDays(parameters[0].ToNumber());
            target.Value = valueOf;
            return(target);
        }
        /// <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()));
        }
Example #5
0
        public override JsInstance Execute(
            IJintVisitor visitor,
            JsDictionaryObject that,
            JsInstance[] parameters)
        {
            JsDate jsDate = this.Construct(parameters);

            if (that == null || that as IGlobal == visitor.Global)
            {
                return(visitor.Return(this.ToStringImpl((JsDictionaryObject)jsDate, JsInstance.EMPTY)));
            }
            return((JsInstance)jsDate);
        }
Example #6
0
        public JsInstance GetSecondsImpl(JsDictionaryObject target, JsInstance[] parameters)
        {
            if (double.IsNaN(target.ToNumber()))
            {
                return(this.Global.NaN);
            }
            JsNumberConstructor numberClass = this.Global.NumberClass;
            DateTime            dateTime    = JsDateConstructor.CreateDateTime(target.ToNumber());

            dateTime = dateTime.ToLocalTime();
            double second = (double)dateTime.Second;

            return((JsInstance)numberClass.New(second));
        }
Example #7
0
        public JsInstance GetUTCHoursImpl(JsDictionaryObject target, JsInstance[] parameters)
        {
            if (double.IsNaN(target.ToNumber()))
            {
                return(this.Global.NaN);
            }
            JsNumberConstructor numberClass = this.Global.NumberClass;
            DateTime            dateTime    = JsDateConstructor.CreateDateTime(target.ToNumber());

            dateTime = dateTime.ToUniversalTime();
            double hour = (double)dateTime.Hour;

            return((JsInstance)numberClass.New(hour));
        }
Example #8
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;
 }
Example #9
0
        /// <summary>
        /// 15.9.5.29
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance SetUTCMillisecondsImpl(JsDictionaryObject target, JsInstance[] parameters)
        {
            if (parameters.Length == 0)
            {
                throw new ArgumentException("There was no millisecond specified");
            }

            DateTime valueOf = CreateDateTime(target.ToNumber());

            valueOf      = valueOf.AddMilliseconds(-valueOf.Millisecond);
            valueOf      = valueOf.AddMilliseconds(parameters[0].ToNumber());
            target.Value = New(valueOf).ToNumber();
            return(target);
        }
Example #10
0
        public JsInstance ToTimeStringImpl(JsDictionaryObject target, JsInstance[] parameters)
        {
            if (double.IsNaN(target.ToNumber()))
            {
                return((JsInstance)this.Global.StringClass.New(double.NaN.ToString()));
            }
            JsStringConstructor stringClass = this.Global.StringClass;
            DateTime            dateTime    = JsDateConstructor.CreateDateTime(target.ToNumber());

            dateTime = dateTime.ToLocalTime();
            string str = dateTime.ToString(JsDate.TIMEFORMAT, (IFormatProvider)CultureInfo.InvariantCulture);

            return((JsInstance)stringClass.New(str));
        }
Example #11
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])));
            }
        }
Example #12
0
        public JsInstance SearchImpl(JsDictionaryObject target, JsInstance[] parameters)
        {
            if (parameters[0].Class == "String")
            {
                parameters[0] = (JsInstance)this.Global.RegExpClass.New(parameters[0].ToString(), false, false, false);
            }
            Match match = ((JsRegExp)parameters[0]).Regex.Match(target.ToString());

            if (match != null && match.Success)
            {
                return((JsInstance)this.Global.NumberClass.New((double)match.Index));
            }
            return((JsInstance)this.Global.NumberClass.New(-1.0));
        }
Example #13
0
 public JsScope(JsScope outer, JsDictionaryObject bag)
     : base((JsDictionaryObject)outer)
 {
     if (outer == null)
     {
         throw new ArgumentNullException(nameof(outer));
     }
     if (bag == null)
     {
         throw new ArgumentNullException(nameof(bag));
     }
     this.globalScope = outer.Global;
     this.bag         = bag;
 }
Example #14
0
        internal static Descriptor ToPropertyDesciptor(
            IGlobal global,
            JsDictionaryObject owner,
            string name,
            JsInstance jsInstance)
        {
            if (jsInstance.Class != "Object")
            {
                throw new JsException((JsInstance)global.TypeErrorClass.New("The target object has to be an instance of an object"));
            }
            JsObject jsObject = (JsObject)jsInstance;

            if ((jsObject.HasProperty("value") || jsObject.HasProperty("writable")) && (jsObject.HasProperty("set") || jsObject.HasProperty("get")))
            {
                throw new JsException((JsInstance)global.TypeErrorClass.New("The property cannot be both writable and have get/set accessors or cannot have both a value and an accessor defined"));
            }
            JsInstance result     = (JsInstance)null;
            Descriptor descriptor = !jsObject.HasProperty("value") ? (Descriptor) new PropertyDescriptor(global, owner, name) : (Descriptor) new ValueDescriptor(owner, name, jsObject["value"]);

            if (jsObject.TryGetProperty("enumerable", out result))
            {
                descriptor.Enumerable = result.ToBoolean();
            }
            if (jsObject.TryGetProperty("configurable", out result))
            {
                descriptor.Configurable = result.ToBoolean();
            }
            if (jsObject.TryGetProperty("writable", out result))
            {
                descriptor.Writable = result.ToBoolean();
            }
            if (jsObject.TryGetProperty("get", out result))
            {
                if (!(result is JsFunction))
                {
                    throw new JsException((JsInstance)global.TypeErrorClass.New("The getter has to be a function"));
                }
                ((PropertyDescriptor)descriptor).GetFunction = (JsFunction)result;
            }
            if (jsObject.TryGetProperty("set", out result))
            {
                if (!(result is JsFunction))
                {
                    throw new JsException((JsInstance)global.TypeErrorClass.New("The setter has to be a function"));
                }
                ((PropertyDescriptor)descriptor).SetFunction = (JsFunction)result;
            }
            return(descriptor);
        }
Example #15
0
        public JsInstance SetUTCMillisecondsImpl(
            JsDictionaryObject target,
            JsInstance[] parameters)
        {
            if (parameters.Length == 0)
            {
                throw new ArgumentException("There was no millisecond specified");
            }
            DateTime dateTime = JsDateConstructor.CreateDateTime(target.ToNumber());

            dateTime     = dateTime.AddMilliseconds((double)-dateTime.Millisecond);
            dateTime     = dateTime.AddMilliseconds(parameters[0].ToNumber());
            target.Value = (object)this.New(dateTime).ToNumber();
            return((JsInstance)target);
        }
Example #16
0
        public JsInstance SetLengthImpl(JsInstance target, JsInstance[] parameters)
        {
            int length = (int)parameters[0].ToNumber();

            if (length < 0 || double.IsNaN(length) || double.IsInfinity(length))
            {
                throw new JsException(Global.RangeErrorClass.New("invalid length"));
            }

            JsDictionaryObject obj = (JsDictionaryObject)target;

            obj.Length = length;

            return(parameters[0]);
        }
Example #17
0
 public override JsInstance Execute(
     IJintVisitor visitor,
     JsDictionaryObject that,
     JsInstance[] parameters)
 {
     if (that == null || that as IGlobal == visitor.Global)
     {
         return(visitor.Return((JsInstance)this.Construct(parameters, (Type[])null, visitor)));
     }
     for (int index = 0; index < parameters.Length; ++index)
     {
         that[index.ToString()] = parameters[index];
     }
     return(visitor.Return((JsInstance)that));
 }
 public bool IsPrototypeOf(JsDictionaryObject target)
 {
     if (target == null)
     {
         return(false);
     }
     if (target == JsUndefined.Instance || target == JsNull.Instance)
     {
         return(false);
     }
     if (target.Prototype == this)
     {
         return(true);
     }
     return(IsPrototypeOf(target.Prototype));
 }
Example #19
0
        public JsInstance ToLocaleDateStringImpl(
            JsDictionaryObject target,
            JsInstance[] parameters)
        {
            if (double.IsNaN(target.ToNumber()))
            {
                return((JsInstance)this.Global.StringClass.New(double.NaN.ToString()));
            }
            JsStringConstructor stringClass = this.Global.StringClass;
            DateTime            dateTime    = JsDateConstructor.CreateDateTime(target.ToNumber());

            dateTime = dateTime.ToLocalTime();
            string str = dateTime.ToString(JsDate.DATEFORMAT);

            return((JsInstance)stringClass.New(str));
        }
Example #20
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);
     }
 }
Example #21
0
        /// <summary>
        /// Peforms a construction of a CLR instance inside the specified
        /// </summary>
        /// <param name="visitor"></param>
        /// <param name="that"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public override JsInstance Execute(Jint.Expressions.IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters)
        {
            if (that == null || that == JsUndefined.Instance || that == JsNull.Instance || (that as IGlobal) == visitor.Global)
            {
                throw new JintException("A constructor '" + reflectedType.FullName + "' should be applied to the object");
            }

            if (that.Value != null)
            {
                throw new JintException("Can't apply the constructor '" + reflectedType.FullName + "' to already initialized '" + that.Value.ToString() + "'");
            }

            that.Value = CreateInstance(visitor, parameters);
            SetupNativeProperties(that);
            ((JsObject)that).Indexer = m_indexer;
            return(that);
        }
Example #22
0
        public override JsInstance Execute(
            IJintVisitor visitor,
            JsDictionaryObject that,
            JsInstance[] parameters)
        {
            object[] objArray = new object[this.Delegate.Method.GetParameters().Length];
            for (int index = 0; index < parameters.Length; ++index)
            {
                int num = !typeof(JsInstance).IsAssignableFrom(this.Parameters[index].ParameterType) ? 0 : (this.Parameters[index].ParameterType.IsInstanceOfType((object)parameters[index]) ? 1 : 0);
                objArray[index] = num == 0 ? (!this.Parameters[index].ParameterType.IsInstanceOfType(parameters[index].Value) ? visitor.Global.Marshaller.MarshalJsValue <object>(parameters[index]) : parameters[index].Value) : (object)parameters[index];
            }
            object o;

            try
            {
                o = this.Delegate.DynamicInvoke(objArray);
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException;
            }
            catch (Exception ex)
            {
                if (ex.InnerException is JsException)
                {
                    throw ex.InnerException;
                }
                throw;
            }
            if (o != null)
            {
                if (typeof(JsInstance).IsInstanceOfType(o))
                {
                    visitor.Return((JsInstance)o);
                }
                else
                {
                    visitor.Return((JsInstance)visitor.Global.WrapClr(o));
                }
            }
            else
            {
                visitor.Return((JsInstance)JsUndefined.Instance);
            }
            return((JsInstance)null);
        }
Example #23
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));
            }
        }
Example #24
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)
            {
                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;
        }
        public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters)
        {
            try {
                //visitor.CurrentScope["this"] = visitor.Global;
                JsInstance result = Delegate.DynamicInvoke(new object[] { parameters }) as JsInstance;
                visitor.Return(result == null ? JsUndefined.Instance : result);

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

                throw;
            }
        }
Example #26
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;
 }
Example #27
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, "]")));
            }
        }
        public virtual bool HasProperty(string key)
        {
            JsDictionaryObject obj = this;

            while (true)
            {
                if (obj.HasOwnProperty(key))
                {
                    return(true);
                }

                obj = obj.Prototype;

                if (obj == JsUndefined.Instance || obj == JsNull.Instance)
                {
                    return(false);
                }
            }
        }
Example #29
0
        /// <summary>
        /// 15.9.5.39
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance SetUTCMonthImpl(JsDictionaryObject target, JsInstance[] parameters)
        {
            if (parameters.Length == 0)
            {
                throw new ArgumentException("There was no month specified");
            }
            DateTime valueOf = CreateDateTime(target.ToNumber());

            valueOf      = valueOf.AddMonths(-valueOf.Month);
            valueOf      = valueOf.AddMonths((int)parameters[0].ToNumber() + 1);
            target.Value = valueOf;
            if (parameters.Length > 1)
            {
                JsInstance[] innerParams = new JsInstance[parameters.Length - 1];
                Array.Copy(parameters, 1, innerParams, 0, innerParams.Length);
                target = (JsDate)SetDateImpl(target, innerParams);
            }
            return(target);
        }
Example #30
0
        /// <summary>
        /// 15.9.5.35
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance SetHoursImpl(JsDictionaryObject target, JsInstance[] parameters)
        {
            if (parameters.Length == 0)
            {
                throw new ArgumentException("There was no hour specified");
            }
            DateTime valueOf = CreateDateTime(target.ToNumber()).ToLocalTime();

            valueOf      = valueOf.AddHours(-valueOf.Hour);
            valueOf      = valueOf.AddHours(parameters[0].ToNumber());
            target.Value = valueOf;
            if (parameters.Length > 1)
            {
                JsInstance[] innerParams = new JsInstance[parameters.Length - 1];
                Array.Copy(parameters, 1, innerParams, 0, innerParams.Length);
                target = (JsDate)SetMinutesImpl(target, innerParams);
            }
            return(target);
        }