public IJsValue SetValue(IJsValue self, IJsValue value)
        {
            if (!Writable)
            {
                throw new JsReferenceException("Property {0} is not writable", Name);
            }
            if (!ReferenceEquals(self, Owner))
            {
                // We are setting an inherited property in a subclassed instance
                var @this = self.Cast<IJsObject>("Cannot set property {0} on non-object", Name);

                // Is the property present in subclassed instance?
                var pd = @this.GetOwnProperty(Name);
                if (pd != null)
                {
                    return pd.SetValue(self, value);
                }

                // If not, create it and return
                @this.DefineOwnProperty(new OwnedValuePropertyDescriptor(@this, Name, value, Flags));
                return value;
            }

            return Value = value;
        }
 public OwnedValuePropertyDescriptor(IJsObject owner, string name, IJsValue value, PropertyDescriptorFlags flags)
 {
     Owner = owner;
     Name = name;
     Value = value;
     Flags = flags;
 }
Example #3
0
 public static IJsValue Add(IEnvironment environment, IJsValue lhs, IJsValue rhs)
 {
     if ((lhs.ToPrimitive() is string) || (rhs.ToPrimitive() is string))
     {
         return environment.CreateString(lhs.ToString() + rhs.ToString());
     }
     return ArithEval(environment, lhs, rhs, (l, r) => l + r);
 }
Example #4
0
 public FunctionEnvironment(IEnvironment parent, IJsFunction function, IJsValue @this, IList<IJsValue> arguments)
 {
     if (parent == null) throw new ArgumentNullException("parent");
     Parent = parent;
     Function = function;
     This = @this;
     Arguments = arguments;
 }
Example #5
0
 public override IReference GetReference(IJsValue name)
 {
     var index = name.ToArrayIndex();
     if (index.HasValue && (index >= 0))
     {
         return GetElementReference(index.Value);
     }
     return base.GetReference(name.ToString());
 }
Example #6
0
        public IReference CreateReference(string name, IJsValue value)
        {
            if (_references == null)
            {
                _references = new Dictionary<string, IReference>();
            }
            IReference reference;
            if (_references.TryGetValue(name, out reference))
            {
                //throw new JsReferenceError(); // TODO: throw if strict only

                // NOTE: node.js allows redeclaration
                reference.SetValue(value);
                return reference;
            }
            return _references[name] = new ValueReference(value);
        }
Example #7
0
 public static IJsValue ArithEval(IEnvironment environment, IJsValue lhs, IJsValue rhs,
                                  Func<double, double, double> mathOperator)
 {
     /*
     var l = lhs.ToNumber();
     if (double.IsNaN(l) || double.IsInfinity(l))
     {
         return environment.CreateNumber(double.NaN);
     }
     var r = rhs.ToNumber();
     if (double.IsNaN(r) || double.IsInfinity(r))
     {
         return environment.CreateNumber(double.NaN);
     }
     return environment.CreateNumber(mathOperator(l, r));
      */
     var v = mathOperator(lhs.ToNumber(), rhs.ToNumber());
     return environment.CreateNumber(double.IsInfinity(v) ? double.NaN : v);
 }
Example #8
0
        public override IJsValue Apply(IJsValue @this, IList<IJsValue> arguments)
        {
            if (Body == null)
            {
                return JsUndefined.Value;
            }
            var applyEnvironment =
                new Environment(
                    new BoundArgumentsEnvironment(
                        new FunctionEnvironment(Environment,
                                                this,
                                                @this,
                                                arguments),
                        Arguments,
                        arguments
                        )
                    );

            return Body.Evaluate(applyEnvironment);
        }
Example #9
0
 public abstract IJsValue Apply(IJsValue @this, IList<IJsValue> arguments);
Example #10
0
 public static IJsValue RelationalEval(IEnvironment environment, IJsValue lhs, IJsValue rhs,
                                       Func<double, double, bool> relationalOperator,
                                       Func<string, string, bool> stringRelationalOperator)
 {
     if ((lhs.ToPrimitive() is string) || (rhs.ToPrimitive() is string))
     {
         return environment.CreateBool(stringRelationalOperator(lhs.ToString(), rhs.ToString()));
     }
     var ln = lhs.ToNumber();
     if (double.IsNaN(ln))
     {
         return JsUndefined.Value;
     }
     var rn = rhs.ToNumber();
     if (double.IsNaN(rn))
     {
         return JsUndefined.Value;
     }
     return environment.CreateBool(relationalOperator(ln, rn));
 }
Example #11
0
 public static IJsValue Neg(IEnvironment environment, IJsValue value)
 {
     return environment.CreateNumber(-value.ToNumber());
 }
Example #12
0
 public IJsValue SetValue(IJsValue self, IJsValue value)
 {
     throw new JsReferenceException("{0} is not defined", Name);
 }
Example #13
0
 public ReadonlyValueReference(IJsValue value)
 {
     Value = value;
 }
Example #14
0
 public IJsValue GetValue(IJsValue self = null)
 {
     return Value;
 }
Example #15
0
 public IJsValue SetValue(IJsValue self, IJsValue value)
 {
     return _setter(_name, value);
 }
Example #16
0
 private void Put(string name, IJsValue value)
 {
     GetReference(name).SetValue(this, value);
 }
Example #17
0
 public virtual IReference GetReference(IJsValue name)
 {
     return GetReference(name.ToString());
 }
Example #18
0
 public static IJsValue Apply(this IJsFunction function, IJsValue @this, IEnumerable<IJsValue> arguments)
 {
     return function.Apply(@this, arguments.ToList());
 }
 public IJsValue GetValue(IJsValue self)
 {
     return Value;
 }
Example #20
0
 public static IJsValue Apply(this IJsFunction function, IJsValue @this, params IJsValue[] arguments)
 {
     return function.Apply(@this, (IList<IJsValue>)arguments);
 }
Example #21
0
 public override IJsValue Apply(IJsValue @this, IList<IJsValue> arguments)
 {
     return Func(Environment, @this, arguments) ?? JsUndefined.Value;
 }
Example #22
0
 public ValueReference(IJsValue value)
 {
     Value = value;
 }
Example #23
0
 public IReference GetReference(IJsValue name)
 {
     throw new JsReferenceException("Cannot read property {0} of {1}", name, this);
 }
Example #24
0
 public IJsValue Evaluate(IEnvironment environment, IJsValue value)
 {
     return Eval(environment, value);
 }
Example #25
0
 public IJsValue SetValue(IJsValue self, IJsValue value)
 {
     throw new JsReferenceException("Illegal assignment");
 }
Example #26
0
 public IJsValue SetValue(IJsValue self, IJsValue value)
 {
     return Value = value;
 }
Example #27
0
 public IJsValue GetValue(IJsValue self)
 {
     return _getter(_name);
 }
Example #28
0
 public IJsValue GetValue(IJsValue self)
 {
     return JsUndefined.Value;
 }
Example #29
0
 public static IJsValue SetValue(this IReference reference, IJsValue value)
 {
     return reference.SetValue(null, value);
 }
Example #30
0
 public IJsValue Evaluate(IEnvironment environment, IJsValue lhs, IJsValue rhs)
 {
     return Eval(environment, lhs, rhs);
 }