Inheritance: JsObject
Esempio n. 1
0
        public JsFunction New(Delegate d)
        {
            JsFunction constructor = (JsFunction) new ClrFunction(d, this.PrototypeProperty);

            constructor.PrototypeProperty = this.Global.ObjectClass.New(constructor);
            return(constructor);
        }
Esempio n. 2
0
        public JsArguments(IGlobal global, JsFunction callee, JsInstance[] arguments)
            : base(global.ObjectClass.New())
        {
            this.global = global;
            // 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())
                {
                    Attributes = PropertyAttributes.DontDelete
                };

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

                this.DefineOwnProperty(i.ToString(), d);
            }

            length = arguments.Length;

            calleeDescriptor = new ValueDescriptor(this, CALLEE)
            {
                Attributes = PropertyAttributes.DontEnum
            };
            DefineOwnProperty(CALLEE, calleeDescriptor);
            calleeDescriptor.Set(this, callee);

            DefineOwnProperty("length", new PropertyDescriptor <JsArguments>(global, this, "length", GetLength)
            {
                Attributes = PropertyAttributes.DontEnum
            });
        }
Esempio n. 3
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));
        }
Esempio n. 4
0
        public JsFunction New()
        {
            JsFunction constructor = new JsFunction(this.PrototypeProperty);

            constructor.PrototypeProperty = this.Global.ObjectClass.New(constructor);
            return(constructor);
        }
Esempio n. 5
0
        public JsFunction New <T>(Func <T, JsInstance[], JsInstance> impl, int length) where T : JsInstance
        {
            JsFunction constructor = (JsFunction) new ClrImplDefinition <T>(impl, length, this.PrototypeProperty);

            constructor.PrototypeProperty = this.Global.ObjectClass.New(constructor);
            return(constructor);
        }
Esempio n. 6
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));
        }
Esempio n. 7
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);
        }
Esempio n. 8
0
        public override JsObject Construct(JsInstance[] parameters, Type[] genericArgs, IJintVisitor visitor)
        {
            JsFunction instance = New();

            instance.Arguments = new List <string>();

            for (int i = 0; i < parameters.Length - 1; i++)
            {
                string arg = parameters[i].ToString();

                foreach (string a in arg.Split(','))
                {
                    instance.Arguments.Add(a.Trim());
                }
            }

            if (parameters.Length >= 1)
            {
                Program p = JintEngine.Compile(parameters[parameters.Length - 1].Value.ToString(), visitor.DebugMode);
                instance.Statement = new BlockStatement()
                {
                    Statements = p.Statements
                };
            }

            return(instance);
        }
Esempio n. 9
0
        public JsFunction New()
        {
            JsFunction function = new JsFunction(PrototypeProperty);

            function.PrototypeProperty = Global.ObjectClass.New(function);
            return(function);
        }
Esempio n. 10
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);
        }
Esempio n. 11
0
        public JsArguments(IGlobal global, JsFunction callee, JsInstance[] arguments)
            : base(global.ObjectClass.New())
        {
            this.global = global;

            // Add the named parameters
            for (int i = 0; i < arguments.Length; i++)
            {
                this.DefineOwnProperty(
                    new ValueDescriptor(this, i.ToString(), arguments[i])
                {
                    Enumerable = false
                }
                    );
            }

            length = arguments.Length;

            calleeDescriptor = new ValueDescriptor(this, CALLEE, callee)
            {
                Enumerable = false
            };
            DefineOwnProperty(calleeDescriptor);

            DefineOwnProperty(new PropertyDescriptor <JsArguments>(global, this, "length", GetLength)
            {
                Enumerable = false
            });
        }
Esempio n. 12
0
        public JsInstance ToStringImpl(JsArray target, JsInstance[] parameters)
        {
            JsArray jsArray = this.Global.ArrayClass.New();

            for (int index = 0; index < target.Length; ++index)
            {
                JsDictionaryObject _this = (JsDictionaryObject)target[index.ToString()];
                if (ExecutionVisitor.IsNullOrUndefined((JsInstance)_this))
                {
                    jsArray[index.ToString()] = (JsInstance)this.Global.StringClass.New();
                }
                else
                {
                    JsFunction function = _this["toString"] as JsFunction;
                    if (function != null)
                    {
                        this.Global.Visitor.ExecuteFunction(function, _this, parameters);
                        jsArray[index.ToString()] = this.Global.Visitor.Returned;
                    }
                    else
                    {
                        jsArray[index.ToString()] = (JsInstance)this.Global.StringClass.New();
                    }
                }
            }
            return((JsInstance)this.Global.StringClass.New(jsArray.ToString()));
        }
Esempio n. 13
0
        public override JsObject Construct(
            JsInstance[] parameters,
            Type[] genericArgs,
            IJintVisitor visitor)
        {
            JsFunction jsFunction = this.New();

            jsFunction.Arguments = new List <string>();
            for (int index = 0; index < parameters.Length - 1; ++index)
            {
                string str1    = parameters[index].ToString();
                char[] chArray = new char[1] {
                    ','
                };
                foreach (string str2 in str1.Split(chArray))
                {
                    jsFunction.Arguments.Add(str2.Trim());
                }
            }
            if (parameters.Length >= 1)
            {
                Program program = JintEngine.Compile(parameters[parameters.Length - 1].Value.ToString(), visitor.DebugMode);
                jsFunction.Statement = (Statement) new BlockStatement()
                {
                    Statements = program.Statements
                };
            }
            return((JsObject)jsFunction);
        }
        public JsFunction New()
        {
            JsFunction function = new JsFunction();

            function.Prototype       = Global.ObjectClass.New(function);
            function.Scope.Prototype = Prototype;
            return(function);
        }
Esempio n. 15
0
 public void DefineProperty(int index, JsFunction @delegate, int argumentCount, PropertyAttributes attributes)
 {
     EnsurePropertyStore();
     PropertyStore.DefineProperty(
         index,
         Global.CreateFunction(Global.GetIdentifier(index), @delegate, argumentCount),
         attributes
     );
 }
Esempio n. 16
0
        public JsDelegate(string name, JsFunction @delegate, int argumentCount)
        {
            if (@delegate == null)
                throw new ArgumentNullException("delegate");

            Name = name;
            Delegate = @delegate;
            ArgumentCount = argumentCount;
        }
Esempio n. 17
0
        /// <summary>
        /// Creates new JsObject, sets a [[Prototype]] to the Prototype parameter and a 'constructor' property to the specified function.
        /// </summary>
        /// <param name="constructor">JsFunction which is used as a constructor</param>
        /// <param name="Prototype">JsObjetc which is used as a prototype</param>
        /// <returns>new object</returns>
        public JsObject New(JsFunction constructor, JsObject Prototype)
        {
            JsObject obj = new JsObject(Prototype);

            obj.DefineOwnProperty(new ValueDescriptor(obj, CONSTRUCTOR, constructor)
            {
                Enumerable = false
            });
            return(obj);
        }
Esempio n. 18
0
        public JsObject New(JsFunction constructor, JsObject Prototype)
        {
            JsObject        jsObject1       = new JsObject(Prototype);
            JsObject        jsObject2       = jsObject1;
            ValueDescriptor valueDescriptor = new ValueDescriptor((JsDictionaryObject)jsObject1, JsFunction.CONSTRUCTOR, (JsInstance)constructor);

            valueDescriptor.Enumerable = false;
            jsObject2.DefineOwnProperty((Descriptor)valueDescriptor);
            return(jsObject1);
        }
        /// <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.Class != JsFunction.TYPEOF && parameter.Class != JsArray.TYPEOF)
            {
                return(parameter.Value);
            }
            else if (parameter == JsNull.Instance)
            {
                return(null);
            }
            else
            {
                if (parameter.IsClr)
                {
                    return(parameter.Value);
                }

                JsFunction 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);
                    }
                    object[] 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);
                }
            }
        }
Esempio n. 20
0
        public void DefineAccessor(int index, JsFunction getter, JsFunction setter, PropertyAttributes attributes)
        {
            var getterObject =
                getter != null
                    ? Global.CreateFunction(null, getter, 0)
                    : null;
            var setterObject =
                setter != null
                    ? Global.CreateFunction(null, setter, 1)
                    : null;

            DefineAccessor(index, getterObject, setterObject, attributes);
        }
Esempio n. 21
0
        public JsObject CreateNakedFunction(string name, JsFunction @delegate, int argumentCount, JsObject prototype, bool isClr)
        {
            // Prototype is set to the created object from the CreateFunction
            // above; prototype here is "Result(9)"
            //   11. Set the prototype property of F to Result(9). This property is given attributes as specified in section 15.3.5.2.

            var result = CreateObject(null, prototype, new JsDelegate(name, @delegate, argumentCount));

            result.SetClass(JsNames.ClassFunction);
            result.IsClr = isClr;

            return result;
        }
        /// <summary>
        /// 15.4.4.11
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance Sort(JsObject target, JsInstance[] parameters)
        {
            if (target.Length <= 1)
            {
                return(target);
            }

            JsFunction compare = null;

            // Compare function defined
            if (parameters.Length > 0)
            {
                compare = parameters[0] as JsFunction;
            }

            var values = new List <JsInstance>();
            var length = (int)target["length"].ToNumber();

            for (int i = 0; i < length; i++)
            {
                values.Add(target[i.ToString()]);
            }

            if (compare != null)
            {
                try
                {
                    values.Sort(new JsComparer(Global.Visitor, compare));
                }
                catch (Exception e)
                {
                    if (e.InnerException is JsException)
                    {
                        throw e.InnerException;
                    }

                    throw;
                }
            }
            else
            {
                values.Sort();
            }

            for (int i = 0; i < length; i++)
            {
                target[i.ToString()] = values[i];
            }

            return(target);
        }
Esempio n. 23
0
        public JsGlobal(ExecutionVisitor visitor, Options options)
            : base((JsObject)JsNull.Instance)
        {
            this.Options = options;
            this.Visitor = (IJintVisitor)visitor;
            this["null"] = (JsInstance)JsNull.Instance;
            JsObject   jsObject   = new JsObject((JsObject)JsNull.Instance);
            JsFunction jsFunction = (JsFunction) new JsFunctionWrapper((Func <JsInstance[], JsInstance>)(arguments => (JsInstance)JsUndefined.Instance), jsObject);

            this["Function"] = (JsInstance)(this.FunctionClass = new JsFunctionConstructor((IGlobal)this, (JsObject)jsFunction));
            this["Object"]   = (JsInstance)(this.ObjectClass = new JsObjectConstructor((IGlobal)this, (JsObject)jsFunction, jsObject));
            this.ObjectClass.InitPrototype((IGlobal)this);
            this["Array"]          = (JsInstance)(this.ArrayClass = new JsArrayConstructor((IGlobal)this));
            this["Boolean"]        = (JsInstance)(this.BooleanClass = new JsBooleanConstructor((IGlobal)this));
            this["Date"]           = (JsInstance)(this.DateClass = new JsDateConstructor((IGlobal)this));
            this["Error"]          = (JsInstance)(this.ErrorClass = new JsErrorConstructor((IGlobal)this, "Error"));
            this["EvalError"]      = (JsInstance)(this.EvalErrorClass = new JsErrorConstructor((IGlobal)this, "EvalError"));
            this["RangeError"]     = (JsInstance)(this.RangeErrorClass = new JsErrorConstructor((IGlobal)this, "RangeError"));
            this["ReferenceError"] = (JsInstance)(this.ReferenceErrorClass = new JsErrorConstructor((IGlobal)this, "ReferenceError"));
            this["SyntaxError"]    = (JsInstance)(this.SyntaxErrorClass = new JsErrorConstructor((IGlobal)this, "SyntaxError"));
            this["TypeError"]      = (JsInstance)(this.TypeErrorClass = new JsErrorConstructor((IGlobal)this, "TypeError"));
            this["URIError"]       = (JsInstance)(this.URIErrorClass = new JsErrorConstructor((IGlobal)this, "URIError"));
            this["Number"]         = (JsInstance)(this.NumberClass = new JsNumberConstructor((IGlobal)this));
            this["RegExp"]         = (JsInstance)(this.RegExpClass = new JsRegExpConstructor((IGlobal)this));
            this["String"]         = (JsInstance)(this.StringClass = new JsStringConstructor((IGlobal)this));
            this["Math"]           = (JsInstance)(this.MathClass = new JsMathConstructor((IGlobal)this));
            foreach (JsInstance jsInstance in this.GetValues())
            {
                if (jsInstance is JsConstructor)
                {
                    ((JsConstructor)jsInstance).InitPrototype((IGlobal)this);
                }
            }
            this[nameof(NaN)]          = this.NumberClass[nameof(NaN)];
            this["Infinity"]           = this.NumberClass["POSITIVE_INFINITY"];
            this["undefined"]          = (JsInstance)JsUndefined.Instance;
            this[JsScope.THIS]         = (JsInstance)this;
            this["eval"]               = (JsInstance) new JsFunctionWrapper(new Func <JsInstance[], JsInstance>(this.Eval), this.FunctionClass.PrototypeProperty);
            this["parseInt"]           = (JsInstance) new JsFunctionWrapper(new Func <JsInstance[], JsInstance>(this.ParseInt), this.FunctionClass.PrototypeProperty);
            this["parseFloat"]         = (JsInstance) new JsFunctionWrapper(new Func <JsInstance[], JsInstance>(this.ParseFloat), this.FunctionClass.PrototypeProperty);
            this["isNaN"]              = (JsInstance) new JsFunctionWrapper(new Func <JsInstance[], JsInstance>(this.IsNaN), this.FunctionClass.PrototypeProperty);
            this["isFinite"]           = (JsInstance) new JsFunctionWrapper(new Func <JsInstance[], JsInstance>(this.isFinite), this.FunctionClass.PrototypeProperty);
            this["decodeURI"]          = (JsInstance) new JsFunctionWrapper(new Func <JsInstance[], JsInstance>(this.DecodeURI), this.FunctionClass.PrototypeProperty);
            this["encodeURI"]          = (JsInstance) new JsFunctionWrapper(new Func <JsInstance[], JsInstance>(this.EncodeURI), this.FunctionClass.PrototypeProperty);
            this["decodeURIComponent"] = (JsInstance) new JsFunctionWrapper(new Func <JsInstance[], JsInstance>(this.DecodeURIComponent), this.FunctionClass.PrototypeProperty);
            this["encodeURIComponent"] = (JsInstance) new JsFunctionWrapper(new Func <JsInstance[], JsInstance>(this.EncodeURIComponent), this.FunctionClass.PrototypeProperty);
            this.Marshaller            = new Marshaller((IGlobal)this);
            this.Marshaller.InitTypes();
        }
Esempio n. 24
0
        public JsObject CreateFunction(string name, JsFunction @delegate, int argumentCount)
        {
            // The CreateObject here is because of 13.2; this is "Result(9)":
            //   9. Create a new object as would be constructed by the expression new Object().

            var prototype = CreateObject(FunctionClass.Prototype);

            var result = CreateNakedFunction(name, @delegate, argumentCount, prototype, false);

            // Constructor on the prototype links back to the result of CreateFunction:
            //   10. Set the constructor property of Result(9) to F. This property is given attributes { DontEnum }.
            prototype.DefineProperty(Id.constructor, result, PropertyAttributes.DontEnum);

            return result;
        }
Esempio n. 25
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;
 }
Esempio n. 26
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;
        }
Esempio n. 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, "]")));
            }
        }
Esempio n. 28
0
        public JsArguments(IGlobal global, JsFunction callee, JsInstance[] arguments)
            : base(global.ObjectClass.New())
        {            
            this.global = global;

            // Add the named parameters            
            for (int i = 0; i < arguments.Length ; i++)
                this.DefineOwnProperty(
                    new ValueDescriptor(this, i.ToString(), arguments[i]) { Enumerable = false }
                );

            length = arguments.Length;

            calleeDescriptor = new ValueDescriptor(this, CALLEE, callee) { Enumerable = false };
            DefineOwnProperty(calleeDescriptor);

            DefineOwnProperty(new PropertyDescriptor<JsArguments>(global, this, "length", GetLength) { Enumerable = false });
        }
Esempio n. 29
0
        public JsFunction New(Delegate d)
        {
            if (d == null)
            {
                throw new ArgumentNullException();
            }
            //JsFunction function = new ClrFunction(d, PrototypeProperty);

            JsMethodImpl impl    = Global.Marshaller.WrapMethod(d.GetType().GetMethod("Invoke"), false);
            JsObject     wrapper = new JsObject(d, JsNull.Instance);

            JsFunction function = New <JsInstance>((that, args) => impl(Global, wrapper, args));

            function.PrototypeProperty = Global.ObjectClass.New(function);

            //function.Scope = new JsScope(PrototypeProperty);
            return(function);
        }
Esempio n. 30
0
        public JsInstance Sort(JsObject target, JsInstance[] parameters)
        {
            if (target.Length <= 1)
            {
                return((JsInstance)target);
            }
            JsFunction function = (JsFunction)null;

            if ((uint)parameters.Length > 0U)
            {
                function = parameters[0] as JsFunction;
            }
            List <JsInstance> jsInstanceList = new List <JsInstance>();
            int number = (int)target["length"].ToNumber();

            for (int index = 0; index < number; ++index)
            {
                jsInstanceList.Add(target[index.ToString()]);
            }
            if (function != null)
            {
                try
                {
                    jsInstanceList.Sort((IComparer <JsInstance>) new JsComparer(this.Global.Visitor, function));
                }
                catch (Exception ex)
                {
                    if (ex.InnerException is JsException)
                    {
                        throw ex.InnerException;
                    }
                    throw;
                }
            }
            else
            {
                jsInstanceList.Sort();
            }
            for (int index = 0; index < number; ++index)
            {
                target[index.ToString()] = jsInstanceList[index];
            }
            return((JsInstance)target);
        }
Esempio n. 31
0
        public JsArguments(IGlobal global, JsFunction callee, JsInstance[] arguments)
            : base(global.ObjectClass.New())
        {
            this.global = global;
            // 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()) { Attributes = PropertyAttributes.DontDelete };

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

                this.DefineOwnProperty(i.ToString(), d);
            }

            length = arguments.Length;

            calleeDescriptor = new ValueDescriptor(this, CALLEE) { Attributes = PropertyAttributes.DontEnum };
            DefineOwnProperty(CALLEE, calleeDescriptor);
            calleeDescriptor.Set(this, callee);

            DefineOwnProperty("length", new PropertyDescriptor<JsArguments>(global, this, "length", GetLength) { Attributes = PropertyAttributes.DontEnum });
        }
Esempio n. 32
0
        public JsArguments(IGlobal global, JsFunction callee, JsInstance[] arguments)
            : base(global.ObjectClass.New())
        {
            this.global = global;
            for (int index = 0; index < arguments.Length; ++index)
            {
                ValueDescriptor valueDescriptor = new ValueDescriptor((JsDictionaryObject)this, index.ToString(), arguments[index]);
                valueDescriptor.Enumerable = false;
                this.DefineOwnProperty((Descriptor)valueDescriptor);
            }
            this.length = arguments.Length;
            ValueDescriptor valueDescriptor1 = new ValueDescriptor((JsDictionaryObject)this, nameof(callee), (JsInstance)callee);

            valueDescriptor1.Enumerable = false;
            this.calleeDescriptor       = valueDescriptor1;
            this.DefineOwnProperty((Descriptor)this.calleeDescriptor);
            PropertyDescriptor <JsArguments> propertyDescriptor = new PropertyDescriptor <JsArguments>(global, (JsDictionaryObject)this, nameof(length), new Func <JsArguments, JsInstance>(this.GetLength));

            propertyDescriptor.Enumerable = false;
            this.DefineOwnProperty((Descriptor)propertyDescriptor);
        }
        /// <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()];
                JsFunction function = obj["toString"] as JsFunction;
                if (function != null)
                {
                    Global.Visitor.ExecuteFunction(function, obj, parameters);
                    result[i.ToString()] = Global.Visitor.Returned;
                }
                else
                {
                    result[i.ToString()] = null;
                }
            }

            return(Global.StringClass.New(result.ToString()));
        }
        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;
        }
Esempio n. 35
0
 public override JsInstance Execute(
     IJintVisitor visitor,
     JsDictionaryObject that,
     JsInstance[] parameters)
 {
     try
     {
         JsInstance result;
         if (this.hasParameters)
         {
             result = this.impl.DynamicInvoke((object)that, (object)parameters) as JsInstance;
         }
         else
         {
             result = this.impl.DynamicInvoke((object)that) as JsInstance;
         }
         visitor.Return(result);
         return(result);
     }
     catch (TargetInvocationException ex)
     {
         throw ex.InnerException;
     }
     catch (ArgumentException ex)
     {
         JsFunction jsFunction = that["constructor"] as JsFunction;
         throw new JsException((JsInstance)visitor.Global.TypeErrorClass.New("incompatible type: " + (object)jsFunction == null ? "<unknown>" : jsFunction.Name));
     }
     catch (Exception ex)
     {
         if (ex.InnerException is JsException)
         {
             throw ex.InnerException;
         }
         throw;
     }
 }
Esempio n. 36
0
        public JsInstance ReplaceImpl(JsDictionaryObject target, JsInstance[] parameters)
        {
            if (parameters.Length == 0)
            {
                return((JsInstance)this.Global.StringClass.New(target.ToString()));
            }
            JsInstance parameter  = parameters[0];
            JsInstance jsInstance = (JsInstance)JsUndefined.Instance;

            if (parameters.Length > 1)
            {
                jsInstance = parameters[1];
            }
            string     source   = target.ToString();
            JsFunction function = jsInstance as JsFunction;

            if (parameter.Class == "RegExp")
            {
                int      count   = ((JsRegExp)parameters[0]).IsGlobal ? int.MaxValue : 1;
                JsRegExp regexp  = (JsRegExp)parameters[0];
                int      startat = regexp.IsGlobal ? 0 : Math.Max(0, (int)regexp["lastIndex"].ToNumber() - 1);
                if (regexp.IsGlobal)
                {
                    regexp["lastIndex"] = (JsInstance)this.Global.NumberClass.New(0.0);
                }
                if (jsInstance is JsFunction)
                {
                    return((JsInstance)this.Global.StringClass.New(((JsRegExp)parameters[0]).Regex.Replace(source, (MatchEvaluator)(m =>
                    {
                        List <JsInstance> jsInstanceList = new List <JsInstance>();
                        if (!regexp.IsGlobal)
                        {
                            regexp["lastIndex"] = (JsInstance)this.Global.NumberClass.New((double)(m.Index + 1));
                        }
                        jsInstanceList.Add((JsInstance)this.Global.StringClass.New(m.Value));
                        for (int index = 1; index < m.Groups.Count; ++index)
                        {
                            if (m.Groups[index].Success)
                            {
                                jsInstanceList.Add((JsInstance)this.Global.StringClass.New(m.Groups[index].Value));
                            }
                            else
                            {
                                jsInstanceList.Add((JsInstance)JsUndefined.Instance);
                            }
                        }
                        jsInstanceList.Add((JsInstance)this.Global.NumberClass.New((double)m.Index));
                        jsInstanceList.Add((JsInstance)this.Global.StringClass.New(source));
                        this.Global.Visitor.ExecuteFunction(function, (JsDictionaryObject)null, jsInstanceList.ToArray());
                        return this.Global.Visitor.Returned.ToString();
                    }), count, startat)));
                }
                string str = parameters[1].ToString();
                return((JsInstance)this.Global.StringClass.New(((JsRegExp)parameters[0]).Regex.Replace(target.ToString(), (MatchEvaluator)(m =>
                {
                    if (!regexp.IsGlobal)
                    {
                        regexp["lastIndex"] = (JsInstance)this.Global.NumberClass.New((double)(m.Index + 1));
                    }
                    string after = source.Substring(Math.Min(source.Length - 1, m.Index + m.Length));
                    return JsStringConstructor.EvaluateReplacePattern(m.Value, source.Substring(0, m.Index), after, str, m.Groups);
                }), count, startat)));
            }
            string matched = parameter.ToString();
            int    length  = source.IndexOf(matched);

            if (length == -1)
            {
                return((JsInstance)this.Global.StringClass.New(source));
            }
            if (jsInstance is JsFunction)
            {
                this.Global.Visitor.ExecuteFunction(function, (JsDictionaryObject)null, new List <JsInstance>()
                {
                    (JsInstance)this.Global.StringClass.New(matched),
                    (JsInstance)this.Global.NumberClass.New((double)length),
                    (JsInstance)this.Global.StringClass.New(source)
                }.ToArray());
                JsInstance result = this.Global.Visitor.Result;
                return((JsInstance)this.Global.StringClass.New(source.Substring(0, length) + result.ToString() + source.Substring(length + matched.Length)));
            }
            string before         = source.Substring(0, length);
            string after1         = source.Substring(length + matched.Length);
            string replacePattern = JsStringConstructor.EvaluateReplacePattern(matched, before, after1, jsInstance.ToString(), (GroupCollection)null);

            return((JsInstance)this.Global.StringClass.New(before + replacePattern + after1));
        }
Esempio n. 37
0
 public JsComparer(IJintVisitor visitor, JsFunction function)
 {
     this.Visitor  = visitor;
     this.Function = function;
 }
Esempio n. 38
0
        public void ExecuteFunction(JsFunction function, JsDictionaryObject that, JsInstance[] parameters)
        {
            if (function == null)
            {
                return;
            }

            JsScope functionScope = new JsScope();
            JsArguments args = new JsArguments(Global, function, parameters);
            functionScope.Prototype = args;
            if (HasOption(Options.Strict))
                functionScope.DefineOwnProperty(JsInstance.ARGUMENTS, args);
            else
                functionScope.Prototype.DefineOwnProperty(JsInstance.ARGUMENTS, args);

            if (that != null)
                functionScope.DefineOwnProperty(JsInstance.THIS, that);
            functionScope.Extensible = false;

            //for (int i = function.DeclaringScopes.Count - 1; i >= 0; i--)
            //{
            //    EnterScope(function.DeclaringScopes[i]);
            //}

            EnterScope(function);
            EnterScope(functionScope);

            try
            {
                PermissionSet.PermitOnly();

                Result = function.Execute(this, that, parameters);

                // Resets the return flag
                if (exit)
                {
                    exit = false;
                }
            }
            finally
            {
                ExitScope();
                ExitScope();
                CodeAccessPermission.RevertPermitOnly();
            }
        }
Esempio n. 39
0
 public object CallFunction(JsFunction function, params object[] args) {
     Visitor.ExecuteFunction(function, null, 
         args.Select(x => Visitor.Global.Marshaller.MarshalClrValue<object>(x)).ToArray()
         //Array.ConvertAll<object,JsInstance>( args, x => Visitor.Global.Marshaller.MarshalClrValue<object>(x) )
         );
     return Visitor.Global.Marshaller.MarshalJsValue<object>(Visitor.Returned);
 }
Esempio n. 40
0
 public JintEngine SetFunction(string name, JsFunction function) {
     Visitor.GlobalScope[name] = function;
     return this;
 }
Esempio n. 41
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="function">Function to exectue</param>
 /// <param name="that">Object to call the function on</param>
 /// <param name="parameters">Parameters of the execution</param>
 public void CallFunction(JsFunction function, JsDictionaryObject that, JsInstance[] parameters)
 {
     function.Statement.Accept(this);
 }
 public JsObject(JsFunction constructor)
 {
     Prototype = new JsObject(constructor.Prototype);
 }
Esempio n. 43
0
 public JsObject CreateNakedFunction(string name, JsFunction @delegate, int argumentCount, JsObject prototype)
 {
     return CreateNakedFunction(name, @delegate, argumentCount, prototype, false);
 }
Esempio n. 44
0
 public void ExecuteFunction(JsFunction function, JsDictionaryObject that, JsInstance[] parameters)
 {
     ExecuteFunction(function, that, parameters, null);
 }
Esempio n. 45
0
 public JsObject CreateFunction(string name, JsFunction function, int parameterCount)
 {
     return Global.CreateFunction(
         name,
         function,
         parameterCount
     );
 }
Esempio n. 46
0
        public void ExecuteFunction(JsFunction function, JsDictionaryObject that, JsInstance[] parameters, Type[] genericParameters)
        {
            if (function == null) {
                return;
            }

            if (recursionLevel++ > MaxRecursions) {
                throw new JsException(Global.ErrorClass.New("Too many recursions in the script."));
            }

            // ecma chapter 10.
            // TODO: move creation of the activation object to the JsFunction
            // create new argument object and instantinate arguments into it
            JsArguments args = new JsArguments(Global, function, parameters);

            // create new activation object and copy instantinated arguments to it
            // Activation should be before the function.Scope hierarchy
            JsScope functionScope = new JsScope(function.Scope ?? GlobalScope);

            for (int i = 0; i < function.Arguments.Count; i++)
                if (i < parameters.Length)
                    functionScope.DefineOwnProperty(
                        new LinkedDescriptor(
                            functionScope,
                            function.Arguments[i],
                            args.GetDescriptor(i.ToString()),
                            args
                        )
                    );
                else
                    functionScope.DefineOwnProperty(
                        new ValueDescriptor(
                            functionScope,
                            function.Arguments[i],
                            JsUndefined.Instance
                        )
                    );

            // define arguments variable
            if (HasOption(Options.Strict))
                functionScope.DefineOwnProperty(JsScope.ARGUMENTS, args);
            else
                args.DefineOwnProperty(JsScope.ARGUMENTS, args);

            // set this variable
            if (that != null)
                functionScope.DefineOwnProperty(JsScope.THIS, that);
            else
                functionScope.DefineOwnProperty(JsScope.THIS, that = Global as JsObject);

            // enter activation object
            EnterScope(functionScope);

            try {
                PermissionSet.PermitOnly();

                if (genericParameters != null && genericParameters.Length > 0)
                    Result = function.Execute(this, that, parameters, genericParameters);
                else
                    Result = function.Execute(this, that, parameters);

                // Resets the return flag
                if (exit) {
                    exit = false;
                }
            }
            finally {
                // return to previous execution state
                ExitScope();

                CodeAccessPermission.RevertPermitOnly();
                recursionLevel--;
            }
        }
Esempio n. 47
0
 public object CallFunction(JsFunction function, params object[] args)
 {
     visitor.ExecuteFunction(function, null, JsClr.ConvertParametersBack(visitor, args));
     return JsClr.ConvertParameter(visitor.Returned);
 }
Esempio n. 48
0
        /// <summary>
        /// 15.5.4.11
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance ReplaceImpl(JsDictionaryObject target, JsInstance[] parameters)
        {
            if (parameters.Length == 0)
            {
                return(Global.StringClass.New(target.ToString()));
            }

            JsInstance searchValue  = parameters[0];
            JsInstance replaceValue = JsUndefined.Instance;

            if (parameters.Length > 1)
            {
                replaceValue = parameters[1];
            }

            string source = target.ToString();

            JsFunction function = replaceValue as JsFunction;

            if (searchValue.Class == JsInstance.CLASS_REGEXP)
            {
                int count     = ((JsRegExp)parameters[0]).IsGlobal ? int.MaxValue : 1;
                var regexp    = ((JsRegExp)parameters[0]);
                int lastIndex = regexp.IsGlobal ? 0 : Math.Max(0, (int)regexp["lastIndex"].ToNumber() - 1);

                if (regexp.IsGlobal)
                {
                    regexp["lastIndex"] = Global.NumberClass.New(0);
                }

                if (replaceValue is JsFunction)
                {
                    string ret = ((JsRegExp)parameters[0]).Regex.Replace(source, delegate(Match m) {
                        List <JsInstance> replaceParameters = new List <JsInstance>();
                        if (!regexp.IsGlobal)
                        {
                            regexp["lastIndex"] = Global.NumberClass.New(m.Index + 1);
                        }

                        replaceParameters.Add(Global.StringClass.New(m.Value));
                        for (int i = 1; i < m.Groups.Count; i++)
                        {
                            if (m.Groups[i].Success)
                            {
                                replaceParameters.Add(Global.StringClass.New(m.Groups[i].Value));
                            }
                            else
                            {
                                replaceParameters.Add(JsUndefined.Instance);
                            }
                        }
                        replaceParameters.Add(Global.NumberClass.New(m.Index));
                        replaceParameters.Add(Global.StringClass.New(source));

                        Global.Visitor.ExecuteFunction(function, null, replaceParameters.ToArray());

                        return(Global.Visitor.Returned.ToString());
                    }, count, lastIndex);


                    return(Global.StringClass.New(ret));
                }
                else
                {
                    string str = parameters[1].ToString();
                    string ret = ((JsRegExp)parameters[0]).Regex.Replace(target.ToString(), delegate(Match m) {
                        if (!regexp.IsGlobal)
                        {
                            regexp["lastIndex"] = Global.NumberClass.New(m.Index + 1);
                        }

                        string after = source.Substring(Math.Min(source.Length - 1, m.Index + m.Length));
                        return(EvaluateReplacePattern(m.Value, source.Substring(0, m.Index), after, str, m.Groups));
                    }, count, lastIndex);

                    return(Global.StringClass.New(ret));
                }
            }
            else
            {
                string search = searchValue.ToString();
                int    index  = source.IndexOf(search);
                if (index != -1)
                {
                    if (replaceValue is JsFunction)
                    {
                        List <JsInstance> replaceParameters = new List <JsInstance>();
                        replaceParameters.Add(Global.StringClass.New(search));
                        replaceParameters.Add(Global.NumberClass.New(index));
                        replaceParameters.Add(Global.StringClass.New(source));

                        Global.Visitor.ExecuteFunction(function, null, replaceParameters.ToArray());
                        replaceValue = Global.Visitor.Result;

                        return(Global.StringClass.New(source.Substring(0, index) + replaceValue.ToString() + source.Substring(index + search.Length)));
                    }
                    else
                    {
                        string before    = source.Substring(0, index);
                        string after     = source.Substring(index + search.Length);
                        string newString = EvaluateReplacePattern(search, before, after, replaceValue.ToString(), null);
                        return(Global.StringClass.New(before + newString + after));
                    }
                }
                else
                {
                    return(Global.StringClass.New(source));
                }
            }
        }
Esempio n. 49
0
 public CallProxyFunction(JsFunction callee)
 {
     Callee = callee;
 }
Esempio n. 50
0
 public JsObject(JsFunction constructor)
 {
     Prototype = new JsObject(constructor.Prototype);
 }
Esempio n. 51
0
 /// <summary>
 /// Creates new JsObject, sets a [[Prototype]] to the Prototype parameter and a 'constructor' property to the specified function.
 /// </summary>
 /// <param name="constructor">JsFunction which is used as a constructor</param>
 /// <param name="Prototype">JsObjetc which is used as a prototype</param>
 /// <returns>new object</returns>
 public JsObject New(JsFunction constructor, JsObject Prototype)
 {
     JsObject obj = new JsObject(Prototype);
     obj.DefineOwnProperty(new ValueDescriptor(obj, CONSTRUCTOR, constructor) { Enumerable = false });
     return obj;
 }
Esempio n. 52
0
 public object CallFunction(JsFunction function, params object[] args)
 {
     try
     {
         visitor.ExecuteFunction(function, null, JsClr.ConvertParametersBack(visitor, args));
         return JsClr.ConvertParameter(visitor.Returned);
     }
     catch (TargetInvocationException e)
     {
         string message = String.Format("{0}\n{1}: {2} in {3}"
             , e.InnerException.Message, moduleName, CurrentString, CurrentLine);
         throw new BitMobile.Utilities.Exceptions.JSException(message, e);
     }
     catch (BitMobile.Utilities.Exceptions.JsGlobalException e)
     {
         string message = String.Format("Error occured in script function:\n{0}\n{1}: {2} in {3}"
             , e.Message, moduleName, CurrentString, CurrentLine);
         throw new BitMobile.Utilities.Exceptions.JSException(message, e);
     }
     catch (Exception e)
     {
         string message = String.Format("{0}\n{1}: {2} in {3}"
             , e.Message, moduleName, CurrentString, CurrentLine);
         throw new BitMobile.Utilities.Exceptions.JSException(message, e);
     }
 }