예제 #1
0
        /// <summary>
        /// 15.5.4.14
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance SplitImpl(JsDictionaryObject target, JsInstance[] parameters)
        {
            JsObject A = Global.ArrayClass.New();
            string   S = target.ToString();

            if (parameters.Length == 0 || parameters[0] == JsUndefined.Instance)
            {
                A["0"] = Global.StringClass.New(S);
            }

            JsInstance separator = parameters[0];
            int        limit     = parameters.Length > 1 ? Convert.ToInt32(parameters[1].ToNumber()) : Int32.MaxValue;
            int        s         = S.Length;

            string[] result;

            if (separator.Class == JsInstance.CLASS_REGEXP)
            {
                result = ((JsRegExp)parameters[0]).Regex.Split(S, limit);
            }
            else
            {
                result = S.Split(new string[] { separator.ToString() }, limit, StringSplitOptions.None);
            }

            for (int i = 0; i < result.Length; i++)
            {
                A[i.ToString()] = Global.StringClass.New(result[i]);
            }

            return(A);
        }
예제 #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 = parameters.Length < 1 || parameters[0] == JsUndefined.Instance || parameters[0] == JsNull.Instance ? visitor.Global as JsDictionaryObject : parameters[0] as JsDictionaryObject;

            JsInstance[] _parameters;
            if (parameters.Length >= 2 && parameters[1] != JsNull.Instance)
            {
                _parameters = new JsInstance[parameters.Length - 1];
                for (int index = 1; index < parameters.Length; ++index)
                {
                    _parameters[index - 1] = parameters[index];
                }
            }
            else
            {
                _parameters = JsInstance.EMPTY;
            }
            visitor.ExecuteFunction(function, _this, _parameters);
            return(visitor.Result);
        }
예제 #3
0
 public JsInstance ExecImpl(JsRegExp regexp, JsInstance[] parameters)
 {
     string S = parameters[0].ToString();
     int length = S.Length;
     int lastIndex = (int)regexp["lastIndex"].ToNumber();
     int i = lastIndex;
     if (regexp["global"].ToBoolean())
         i = 0;
     if (i < 0 || i > length)
     {
         lastIndex = 0;
         return JsNull.Instance;
     }
     Match r = ((Regex)regexp.Value).Match(S, i);
     if (!r.Success)
     {
         lastIndex = 0;
         return JsNull.Instance;
     }
     int e = r.Index + r.Length;
     if (regexp["global"].ToBoolean())
         lastIndex = e;
     int n = r.Groups.Count;
     JsArray result = Global.ArrayClass.New();
     result["index"] = Global.NumberClass.New(r.Index);
     result["input"] = Global.StringClass.New(S);
     result["length"] = Global.NumberClass.New(n + 1);
     result[Global.NumberClass.New(0)] = Global.StringClass.New(r.Value);
     for (i = 1; i > 0 && i < n; i++)
     {
         result[Global.NumberClass.New(i)] = Global.StringClass.New(r.Groups[i].Value);
     }
     return result;
 }
예제 #4
0
 public override void Set(JsDictionaryObject that, JsInstance value)
 {
     if (setter != null)
     {
         setter(that, value);
     }
 }
예제 #5
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;
        }
예제 #6
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 = parameters.Length < 1 || parameters[0] == JsUndefined.Instance || parameters[0] == JsNull.Instance ? visitor.Global as JsDictionaryObject : parameters[0] as JsDictionaryObject;

            JsInstance[] _parameters;
            if (parameters.Length >= 2 && parameters[1] != JsNull.Instance)
            {
                JsObject parameter = parameters[1] as JsObject;
                if (parameter == null)
                {
                    throw new JsException((JsInstance)visitor.Global.TypeErrorClass.New("second argument must be an array"));
                }
                _parameters = new JsInstance[parameter.Length];
                for (int index = 0; index < parameter.Length; ++index)
                {
                    _parameters[index] = parameter[index.ToString()];
                }
            }
            else
            {
                _parameters = JsInstance.EMPTY;
            }
            visitor.ExecuteFunction(function, _this, _parameters);
            return(visitor.Result);
        }
예제 #7
0
        public JsInstance ToPrecisionImpl(JsInstance target, JsInstance[] parameters)
        {
            if (double.IsInfinity(target.ToNumber()) || double.IsNaN(target.ToNumber()))
            {
                return(this.ToStringImpl(target, new JsInstance[0]));
            }
            if (parameters.Length == 0)
            {
                throw new JsException((JsInstance)this.Global.SyntaxErrorClass.New("precision missing"));
            }
            if (parameters[0] == JsUndefined.Instance)
            {
                return(this.ToStringImpl(target, new JsInstance[0]));
            }
            int num1 = 0;

            if ((uint)parameters.Length > 0U)
            {
                num1 = Convert.ToInt32(parameters[0].ToNumber());
            }
            if (num1 < 1 || num1 > 21)
            {
                throw new JsException((JsInstance)this.Global.RangeErrorClass.New("precision must be between 1 and 21"));
            }
            string str  = target.ToNumber().ToString("e23", (IFormatProvider)CultureInfo.InvariantCulture);
            int    num2 = str.IndexOfAny(new char[2] {
                '.', 'e'
            });
            int num3 = num2 == -1 ? str.Length : num2;
            int num4 = num1 - num3;
            int num5 = num4 < 1 ? 1 : num4;

            return((JsInstance)this.Global.StringClass.New(target.ToNumber().ToString("f" + (object)num5, (IFormatProvider)CultureInfo.InvariantCulture)));
        }
예제 #8
0
        public JsInstance LastIndexOfImpl(JsObject target, JsInstance[] parameters)
        {
            if (parameters.Length == 0)
            {
                return((JsInstance)this.Global.NumberClass.New(-1.0));
            }
            int length = target.Length;

            if (length == 0)
            {
                return((JsInstance)this.Global.NumberClass.New(-1.0));
            }
            int val1 = length;

            if (parameters.Length > 1)
            {
                val1 = Convert.ToInt32(parameters[1].ToNumber());
            }
            JsInstance parameter = parameters[0];

            for (int index = val1 < 0 ? length - Math.Abs(val1 - 1) : Math.Min(val1, length - 1); index >= 0; --index)
            {
                JsInstance result = (JsInstance)null;
                if (target.TryGetProperty(index.ToString(), out result) && result == parameter)
                {
                    return((JsInstance)this.Global.NumberClass.New((double)index));
                }
            }
            return((JsInstance)this.Global.NumberClass.New(-1.0));
        }
예제 #9
0
        public JsInstance IndexOfImpl(JsObject target, JsInstance[] parameters)
        {
            if (parameters.Length == 0)
            {
                return((JsInstance)this.Global.NumberClass.New(-1.0));
            }
            int number = (int)target["length"].ToNumber();

            if (number == 0)
            {
                return((JsInstance)this.Global.NumberClass.New(-1.0));
            }
            int num = 0;

            if (parameters.Length > 1)
            {
                num = Convert.ToInt32(parameters[1].ToNumber());
            }
            if (num >= number)
            {
                return((JsInstance)this.Global.NumberClass.New(-1.0));
            }
            JsInstance parameter = parameters[0];

            for (int index = num < 0 ? number - Math.Abs(num) : num; index < number; ++index)
            {
                JsInstance result = (JsInstance)null;
                if (target.TryGetProperty(index.ToString(), out result) && result == parameter)
                {
                    return((JsInstance)this.Global.NumberClass.New((double)index));
                }
            }
            return((JsInstance)this.Global.NumberClass.New(-1.0));
        }
예제 #10
0
        public JsInstance Shift(JsDictionaryObject target, JsInstance[] parameters)
        {
            if (target.Length == 0)
            {
                return((JsInstance)JsUndefined.Instance);
            }
            JsInstance jsInstance = target[0.ToString()];
            int        num;

            for (int index1 = 1; index1 < target.Length; ++index1)
            {
                JsInstance result = (JsInstance)null;
                string     index2 = index1.ToString();
                num = index1 - 1;
                string index3 = num.ToString();
                if (target.TryGetProperty(index2, out result))
                {
                    target[index3] = result;
                }
                else
                {
                    target.Delete(index3);
                }
            }
            JsDictionaryObject dictionaryObject = target;

            num = target.Length - 1;
            string index = num.ToString();

            dictionaryObject.Delete(index);
            num = target.Length--;
            return(jsInstance);
        }
예제 #11
0
        public JsInstance UnShift(JsObject target, JsInstance[] parameters)
        {
            for (int length = target.Length; length > 0; --length)
            {
                JsInstance result = (JsInstance)null;
                int        num    = length - 1;
                string     index1 = num.ToString();
                num = length + parameters.Length - 1;
                string index2 = num.ToString();
                if (target.TryGetProperty(index1, out result))
                {
                    target[index2] = result;
                }
                else
                {
                    target.Delete(index2);
                }
            }
            List <JsInstance> jsInstanceList = new List <JsInstance>((IEnumerable <JsInstance>)parameters);
            int num1 = 0;

            while (jsInstanceList.Count > 0)
            {
                JsInstance jsInstance = jsInstanceList[0];
                jsInstanceList.RemoveAt(0);
                target[num1.ToString()] = jsInstance;
                ++num1;
            }
            return((JsInstance)this.Global.NumberClass.New((double)target.Length));
        }
예제 #12
0
        public JsInstance Reverse(JsObject target, JsInstance[] parameters)
        {
            int length = target.Length;
            int num    = length / 2;

            for (int index1 = 0; index1 != num; ++index1)
            {
                string     index2    = (length - index1 - 1).ToString();
                string     index3    = index1.ToString();
                JsInstance result1   = (JsInstance)null;
                JsInstance result2   = (JsInstance)null;
                bool       property1 = target.TryGetProperty(index3, out result1);
                bool       property2 = target.TryGetProperty(index2, out result2);
                if (property1)
                {
                    target[index2] = result1;
                }
                else
                {
                    target.Delete(index2);
                }
                if (property2)
                {
                    target[index3] = result2;
                }
                else
                {
                    target.Delete(index3);
                }
            }
            return((JsInstance)target);
        }
예제 #13
0
        public JsInstance Join(JsObject target, JsInstance[] parameters)
        {
            if (target is JsArray)
            {
                return((JsInstance)((JsArray)target).join(this.Global, parameters.Length != 0 ? parameters[0] : (JsInstance)JsUndefined.Instance));
            }
            string str = parameters.Length == 0 || parameters[0] == JsUndefined.Instance ? "," : parameters[0].ToString();

            if (target.Length == 0)
            {
                return((JsInstance)this.Global.StringClass.New());
            }
            JsInstance    jsInstance1   = target[0.ToString()];
            StringBuilder stringBuilder = jsInstance1 != JsUndefined.Instance && jsInstance1 != JsNull.Instance ? new StringBuilder(jsInstance1.ToString()) : new StringBuilder(string.Empty);
            double        number        = target["length"].ToNumber();

            for (int index = 1; (double)index < number; ++index)
            {
                stringBuilder.Append(str);
                JsInstance jsInstance2 = target[index.ToString()];
                if (jsInstance2 != JsUndefined.Instance && jsInstance2 != JsNull.Instance)
                {
                    stringBuilder.Append(jsInstance2.ToString());
                }
            }
            return((JsInstance)this.Global.StringClass.New(stringBuilder.ToString()));
        }
예제 #14
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;
        }
예제 #15
0
        /// <summary>
        /// 15.4.4.2
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance ToStringImpl(JsArray target, JsInstance[] parameters)
        {
            JsArray result = Global.ArrayClass.New();

            for (int i = 0; i < target.Length; i++)
            {
                var obj = (JsDictionaryObject)target[i.ToString()];
                if (ExecutionVisitor.IsNullOrUndefined(obj)) {
                    result[i.ToString()] = Global.StringClass.New();
                }
                else
                {
                    var function = obj["toString"] as JsFunction;
                    if (function != null) {
                        Global.Visitor.ExecuteFunction(function, obj, parameters);
                        result[i.ToString()] = Global.Visitor.Returned;
                    }
                    else
                    {
                        result[i.ToString()] = Global.StringClass.New();
                    }
                }
            }

            return Global.StringClass.New(result.ToString());

        }
예제 #16
0
        public JsInstance ExecImpl(JsRegExp regexp, JsInstance[] parameters)
        {
            JsArray A = Global.ArrayClass.New();
            string input = parameters[0].ToString();
            A["input"] = Global.StringClass.New(input);

            int i = 0;
            var lastIndex = regexp.IsGlobal ? regexp["lastIndex"].ToNumber() : 0;
            MatchCollection matches = Regex.Matches(input.Substring((int)lastIndex), regexp.Pattern, regexp.Options);
            if (matches.Count > 0) {
                // A[Global.NumberClass.New(i++)] = Global.StringClass.New(matches[0].Value);
                A["index"] = Global.NumberClass.New(matches[0].Index);

                if(regexp.IsGlobal)
                {
                    regexp["lastIndex"] = Global.NumberClass.New(lastIndex + matches[0].Index + matches[0].Value.Length);
                }

                foreach (Group g in matches[0].Groups) {
                    A[Global.NumberClass.New(i++)] = Global.StringClass.New(g.Value);
                }

                return A;
            }
            else
            {
                return JsNull.Instance;
            }
        }
예제 #17
0
        /// <summary>
        /// 15.2.3.6
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="p"></param>
        /// <param name="currentDescriptor"></param>
        public JsInstance DefineProperty(JsInstance[] parameters)
        {
            JsInstance instance = parameters[0];

            if (!(instance is JsDictionaryObject))
            {
                throw new JsException(Global.TypeErrorClass.New());
            }

            string     name = parameters[1].ToString();
            Descriptor desc = Descriptor.ToPropertyDesciptor(Global, (JsDictionaryObject)instance, name, parameters[2]);

            if (desc.DescriptorType == DescriptorType.Accessor)
            {
                if (((PropertyDescriptor)desc).GetFunction != null)
                {
                    ((PropertyDescriptor)desc).GetFunction.Scope[JsInstance.THIS] = instance;
                }
                if (((PropertyDescriptor)desc).SetFunction != null)
                {
                    ((PropertyDescriptor)desc).SetFunction.Scope[JsInstance.THIS] = instance;
                }
            }
            ((JsDictionaryObject)instance).DefineOwnProperty(name, desc);

            return(instance);
        }
예제 #18
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;
        }
예제 #19
0
파일: NativeIndexer.cs 프로젝트: cosh/Jint
 public JsInstance get(JsInstance that, JsInstance index)
 {
     JsIndexerGetter getter = m_getOverload.ResolveOverload(new JsInstance[] { index }, null);
     if (getter == null)
         throw new JintException("No matching overload found");
     return getter(that, index);
 }
        /// <summary>
        /// 15.4.4.4
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance Concat(JsObject target, JsInstance[] parameters)
        {
            JsArray           array = Global.ArrayClass.New();
            List <JsInstance> items = new List <JsInstance>();

            items.Add(target);
            items.AddRange(parameters);
            int n = 0;

            while (items.Count > 0)
            {
                JsInstance e = items[0];
                items.RemoveAt(0);
                if (e.Class == JsArray.TYPEOF)
                {
                    for (int k = 0; k < ((JsArray)e).Length; k++)
                    {
                        string     p      = k.ToString();
                        JsInstance result = null;
                        if (((JsDictionaryObject)e).TryGetProperty(p, out result))
                        {
                            array.DefineOwnProperty(n.ToString(), new ValueDescriptor(this, n.ToString(), result));
                            n++;
                        }
                    }
                }
                else
                {
                    array.DefineOwnProperty(n.ToString(), new ValueDescriptor(this, n.ToString(), e));
                    n++;
                }
            }
            return(array);
        }
예제 #21
0
        public JsArguments(IGlobal global, JsFunction callee, JsInstance[] arguments)
            : base()
        {
			this.args = arguments;
            this.global = global;
            Prototype = global.ObjectClass.New();
            // Add the named parameters
            for (int i = 0; i < Math.Max(arguments.Length, callee.Arguments.Count); i++)
            {
                ValueDescriptor d = new ValueDescriptor(this, i < callee.Arguments.Count ? callee.Arguments[i] : i.ToString());

                d.Set(this, i < arguments.Length ? arguments[i] : JsUndefined.Instance);

                if (i < callee.Arguments.Count)
                    this.DefineOwnProperty(callee.Arguments[i], d);
                this.DefineOwnProperty(i.ToString(), d);
            }

            length = arguments.Length;
            calleeDescriptor = new ValueDescriptor(this, "callee");
            DefineOwnProperty("callee", calleeDescriptor);
            calleeDescriptor.Set(this, callee);
            DefineOwnProperty("length", new PropertyDescriptor<JsArguments>(global, this, "length", GetLength));
			DefineOwnProperty("array", new PropertyDescriptor<JsArguments>(global, this, "array", GetArray));
        }
예제 #22
0
 public JsException(JsInstance value)
     : base(value.ToString())
 {
     Value = value;
     //if (value is JsDictionaryObject)
     //    ((JsDictionaryObject)value)["jintException"] = new JsClr(this);
 }
예제 #23
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);
            }
        }
예제 #24
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;
        }
예제 #25
0
 /// <summary>
 /// 15.4.4.4
 /// </summary>
 /// <param name="target"></param>
 /// <param name="parameters"></param>
 /// <returns></returns>
 public JsInstance Concat(JsObject target, JsInstance[] parameters)
 {
     if (target is JsArray)
         return ((JsArray)target).concat(Global, parameters);
     JsArray array = Global.ArrayClass.New();
     List<JsInstance> items = new List<JsInstance>();
     items.Add(target);
     items.AddRange(parameters);
     int n = 0;
     while (items.Count > 0) {
         JsInstance e = items[0];
         items.RemoveAt(0);
         if (Global.ArrayClass.HasInstance(e as JsObject)) {
             for (int k = 0; k < ((JsObject)e).Length; k++) {
                 string p = k.ToString();
                 JsInstance result = null;
                 if (((JsObject)e).TryGetProperty(p, out result))
                     array.put(n, result);
                 n++;
             }
         }
         else {
             array.put(n, e);
             n++;
         }
     }
     return array;
 }
        /// <summary>
        /// 15.4.4.9
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance Shift(JsDictionaryObject target, JsInstance[] parameters)
        {
            if (target.Length == 0)
            {
                return(JsUndefined.Instance);
            }

            JsInstance first = target[0.ToString()];

            for (int k = 1; k < target.Length; k++)
            {
                JsInstance result = null;

                string from = k.ToString();
                string to   = (k - 1).ToString();
                if (target.TryGetProperty(from, out result))
                {
                    target[to] = result;
                }
                else
                {
                    target.Delete(to);
                }
            }
            target.Delete((target.Length - 1).ToString());
            if (length != int.MinValue)
            {
                length -= parameters.Length;
            }
            return(first);
        }
예제 #27
0
 public JsException(JsInstance value)
     : base(value.ToString())
 {
     Value = value;
     //if (value is JsDictionaryObject)
     //    ((JsDictionaryObject)value)["jintException"] = new JsClr(this);
 }
        /// <summary>
        /// 15.4.4.13
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance UnShift(JsObject target, JsInstance[] parameters)
        {
            for (int k = target.Length; k > 0; k--)
            {
                JsInstance result = null;
                string     from   = (k - 1).ToString();
                string     to     = (k + parameters.Length - 1).ToString();
                if (target.TryGetProperty(from, out result))
                {
                    target[to] = result;
                }
                else
                {
                    target.Delete(to);
                }
            }
            List <JsInstance> items = new List <JsInstance>(parameters);

            for (int j = 0; items.Count > 0; j++)
            {
                JsInstance e = items[0];
                items.RemoveAt(0);
                target[j.ToString()] = e;
            }
            return(Global.NumberClass.New(target.Length));
        }
예제 #29
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 });
 }
        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 array length"));
            }

            JsDictionaryObject array = (JsDictionaryObject)target;

            if (length < array.Length)
            {
                int oldLength = array.Length;
                for (int i = length; i < oldLength; i++)
                {
                    array.Delete(i.ToString());
                }
            }
            else
            {
                for (int i = array.Length; i < length; i++)
                {
                    array[i.ToString()] = JsUndefined.Instance;
                }
            }

            return(parameters[0]);
        }
예제 #31
0
 /// <summary>
 /// Overriden indexer to optimize cases when we already have a number
 /// </summary>
 /// <param name="key">index</param>
 /// <returns>value</returns>
 public override JsInstance this[JsInstance key] {
     get {
         double keyNumber = key.ToNumber();
         int    i         = (int)keyNumber;
         if (i == keyNumber && i >= 0)
         {
             // we have got an index
             return(this.get(i));
         }
         else
         {
             return(base[key.ToString()]);
         }
     }
     set {
         double keyNumber = key.ToNumber();
         int    i         = (int)keyNumber;
         if (i == keyNumber && i >= 0)
         {
             // we have got an index
             this.put(i, value);
         }
         else
         {
             base[key.ToString()] = value;
         }
     }
 }
예제 #32
0
        public JsInstance ExecImpl(JsRegExp regexp, JsInstance[] parameters)
        {
            JsArray A = Global.ArrayClass.New();
            string input = parameters[0].ToString();
            A["input"] = Global.StringClass.New(input);

            int i = 0;
            MatchCollection matches = Regex.Matches(input, regexp.Pattern, regexp.Options);
            if (matches.Count > 0) {
                if (regexp.IsGlobal) {
                    foreach (Match m in matches) {
                        A[Global.NumberClass.New(i++)] = Global.StringClass.New(m.Value);
                    }
                }
                else {
                    foreach (Group g in matches[0].Groups) {
                        A[Global.NumberClass.New(i++)] = Global.StringClass.New(g.Value);
                    }
                }

                A["index"] = Global.NumberClass.New(matches[0].Index);
            }

            return A;
        }
예제 #33
0
 public override JsInstance this[JsInstance key]
 {
     get
     {
         double number = key.ToNumber();
         int    i      = (int)number;
         if ((double)i == number && i >= 0)
         {
             return(this.get(i));
         }
         return(base[key.ToString()]);
     }
     set
     {
         double number = key.ToNumber();
         int    i      = (int)number;
         if ((double)i == number && i >= 0)
         {
             this.put(i, value);
         }
         else
         {
             base[key.ToString()] = value;
         }
     }
 }
예제 #34
0
        public JsInstance SplitImpl(JsDictionaryObject target, JsInstance[] parameters)
        {
            JsObject jsObject = (JsObject)this.Global.ArrayClass.New();
            string   input    = target.ToString();

            if (parameters.Length == 0 || parameters[0] == JsUndefined.Instance)
            {
                jsObject["0"] = (JsInstance)this.Global.StringClass.New(input);
            }
            JsInstance parameter = parameters[0];
            int        count     = parameters.Length > 1 ? Convert.ToInt32(parameters[1].ToNumber()) : int.MaxValue;
            int        length    = input.Length;

            string[] strArray;
            if (parameter.Class == "RegExp")
            {
                strArray = ((JsRegExp)parameters[0]).Regex.Split(input, count);
            }
            else
            {
                strArray = input.Split(new string[1]
                {
                    parameter.ToString()
                }, count, StringSplitOptions.None);
            }
            for (int index = 0; index < strArray.Length; ++index)
            {
                jsObject[index.ToString()] = (JsInstance)this.Global.StringClass.New(strArray[index]);
            }
            return((JsInstance)jsObject);
        }
예제 #35
0
        /// <summary>
        /// Converts a JsInstance object to its CLR equivalence
        /// </summary>
        /// <param name="parameter">The object to convert</param>
        /// <returns>A CLR object</returns>
        public static object ConvertParameter(JsInstance parameter)
        {
            if (parameter == null)
            {
                return(null);
            }

            if (parameter.Class != JsFunction.TYPEOF && parameter.Class != JsArray.TYPEOF)
            {
                return(parameter.Value);
            }

            if (parameter == JsNull.Instance)
            {
                return(null);
            }

            if (parameter.IsClr)
            {
                return(parameter.Value);
            }

            var constructor = ((JsDictionaryObject)parameter)["constructor"] as JsFunction;

            if (constructor == null)
            {
                return(parameter);
            }
            switch (constructor.Name)
            {
            case "Date":
                return(JsDateConstructor.CreateDateTime(parameter.ToNumber()));

            case "String":
            case "RegExp":
            case "Number":
                return(parameter.Value);

            case "Array":
            case "Object":
                if (parameter.Class == JsFunction.TYPEOF)
                {
                    return(parameter);
                }
                var array = new object[((JsObject)parameter).Length];
                foreach (KeyValuePair <string, JsInstance> key in (JsObject)parameter)
                {
                    int index;
                    if (int.TryParse(key.Key, out index))
                    {
                        array[index] = ConvertParameters(key.Value)[0];
                    }
                }
                return(new System.Collections.ArrayList(array));

            default:
                return(parameter);
            }
        }
예제 #36
0
 public JsInstance put(int i, JsInstance value)
 {
     if (i >= this.length)
     {
         this.length = i + 1;
     }
     return(this.m_data[i] = value);
 }
예제 #37
0
 public JsInstance put(int i, JsInstance value)
 {
     if (i >= length)
     {
         length = i + 1;
     }
     return(m_data[i] = value);
 }
예제 #38
0
 public override void Set(JsDictionaryObject that, JsInstance value)
 {
     if (this.setter == null)
     {
         return;
     }
     this.setter(that, value);
 }
예제 #39
0
        public override JsObject Construct(JsInstance[] parameters, Type[] genericArgs, IJintVisitor visitor) {
            JsArray array = New();

            for (int i = 0; i < parameters.Length; i++)
                array.put(i, parameters[i]); // fast versin since it avoids a type conversion

            return array;
        }
예제 #40
0
        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));
        }
예제 #41
0
파일: NativeIndexer.cs 프로젝트: cosh/Jint
        public void set(JsInstance that, JsInstance index, JsInstance value)
        {
            JsIndexerSetter setter = m_setOverload.ResolveOverload(new JsInstance[] { index, value }, null);
            if (setter == null)
                throw new JintException("No matching overload found");

            setter(that, index, value);
        }
예제 #42
0
 public override void Set(JsDictionaryObject that, JsInstance value)
 {
     if (!Writable)
     {
         throw new JintException("This property is not writable");
     }
     this.value = value;
 }
예제 #43
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 });
 }
예제 #44
0
 public override bool TryGetProperty(string index, out JsInstance result)
 {
     result = JsUndefined.Instance;
     try {
         return(m_data.TryGetValue(Convert.ToInt32(index), out result));
     }
     catch (FormatException) {
         return(base.TryGetProperty(index, out result));
     }
 }
예제 #45
0
 public static JsInstance[] ConvertParametersBack(IJintVisitor visitor, object[] args)
 {
     JsInstance[] jsParameters = new JsInstance[args.Length];
     for (int j = 0; j < jsParameters.Length; j++)
     {
         // don't convert JsFunction as they will be translated to Delegates later
         jsParameters[j] = ConvertParameterBack(visitor, args[j]);
     }
     return(jsParameters);
 }
예제 #46
0
        protected JsInstance isFinite(JsInstance[] arguments)
        {
            if (arguments.Length < 1 || arguments[0] == JsUndefined.Instance)
            {
                return((JsInstance)this.BooleanClass.False);
            }
            JsInstance jsInstance = arguments[0];

            return((JsInstance)this.BooleanClass.New(jsInstance != this.NumberClass["NaN"] && jsInstance != this.NumberClass["POSITIVE_INFINITY"] && jsInstance != this.NumberClass["NEGATIVE_INFINITY"]));
        }
예제 #47
0
        public JsInstance get(JsInstance that, JsInstance index)
        {
            JsIndexerGetter getter = m_getOverload.ResolveOverload(new JsInstance[] { index }, null);

            if (getter == null)
            {
                throw new JintException("No matching overload found");
            }
            return(getter(that, index));
        }
예제 #48
0
파일: JsScope.cs 프로젝트: pusp/o2platform
 public override void DefineOwnProperty(string key, JsInstance value)
 {
     if (Extensible)
     {
         base.DefineOwnProperty(key, value);
     }
     else
     {
         Prototype.DefineOwnProperty(key, value);
     }
 }
예제 #49
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]));
            }
        }
예제 #50
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());
        }
예제 #51
0
        public object Invoke(object[] parameters)
        {
            JsInstance[] arguments = new JsInstance[parameters.Length];
            for (int i = 0; i < parameters.Length; i++)
            {
                arguments[i] = Visitor.Global.WrapClr(parameters[i]);
            }

            Visitor.ExecuteFunction(Function, That, arguments);

            return Visitor.Returned == null ? null : Visitor.Returned.Value;
        }
예제 #52
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;
     }
 }
예제 #53
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);
            }
        }
예제 #54
0
        public JsInstance ToStringImpl(JsInstance target, JsInstance[] parameters) {
            if (target == this["NaN"]) {
                return Global.StringClass.New("NaN");
            }

            if (target == this["NEGATIVE_INFINITY"]) {
                return Global.StringClass.New("-Infinity");
            }

            if (target == this["POSITIVE_INFINITY"]) {
                return Global.StringClass.New("Infinity");
            }

            int radix = 10;

            // is radix defined ?
            if (parameters.Length > 0) {
                if (parameters[0] != JsUndefined.Instance) {
                    radix = (int)parameters[0].ToNumber();
                }
            }

            var longToBeFormatted = (long)target.ToNumber();

            if (radix == 10) {
                return Global.StringClass.New(target.ToNumber().ToString(CultureInfo.InvariantCulture).ToLower());
            }
            else {
                // Extract the magnitude for conversion.
                long longPositive = Math.Abs(longToBeFormatted);
                int digitIndex = 0;

                char[] outDigits = new char[63];
                // Convert the magnitude to a digit string.
                for (digitIndex = 0; digitIndex <= 64; digitIndex++)
                {
                    if (longPositive == 0) break;

                    outDigits[outDigits.Length - digitIndex - 1] =
                        rDigits[longPositive % radix];
                    longPositive /= radix;
                }

                // Add a minus sign if the argument is negative.
                if (longToBeFormatted < 0)
                    outDigits[outDigits.Length - digitIndex++ - 1] = '-';

                return Global.StringClass.New(new string(outDigits,
                    outDigits.Length - digitIndex, digitIndex).ToLower());
            }
        }
예제 #55
0
        /// <summary>
        /// 15.2.3.6
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="p"></param>
        /// <param name="currentDescriptor"></param>
        public JsInstance DefineProperty(JsInstance[] parameters)
        {
            JsInstance instance = parameters[0];

            if (!(instance is JsDictionaryObject))
                throw new JsException(Global.TypeErrorClass.New());

            string name = parameters[1].ToString();
            Descriptor desc = Descriptor.ToPropertyDesciptor(Global, (JsDictionaryObject)instance, name, parameters[2]);

            ((JsDictionaryObject)instance).DefineOwnProperty(name, desc);

            return instance;
        }
예제 #56
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);
        }
예제 #57
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;
        }
예제 #58
0
        public JsInstance MaxImpl(JsObject target, JsInstance[] parameters)
        {
            if (parameters.Length == 0) {
                return Global.NumberClass["NEGATIVE_INFINITY"];
            }

            var result = parameters[0].ToNumber();

            foreach (var p in parameters)
            {
                result = Math.Max(p.ToNumber(), result);
            }

            return Global.NumberClass.New(result);
        }
예제 #59
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;
        }
예제 #60
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);
        }