public RavenJObject ToRavenJObject(JsValue jsObject, string propertyKey = null, bool recursiveCall = false) { var objectInstance = jsObject.AsObject(); if (objectInstance.Class == "Function") { // getting a Function instance here, // means that we couldn't evaulate it using Jint return null; } var rjo = new RavenJObject(); foreach (var property in objectInstance.Properties) { if (property.Key == Constants.ReduceKeyFieldName || property.Key == Constants.DocumentIdFieldName) continue; var value = property.Value.Value; if (value.HasValue == false) continue; if (value.Value.IsRegExp()) continue; var recursive = jsObject == value; if (recursiveCall && recursive) rjo[property.Key] = null; else rjo[property.Key] = ToRavenJToken(value.Value, CreatePropertyKey(property.Key, propertyKey), recursive); } return rjo; }
public static IDictionary<String, Object> ToObjBag(JsValue arg) { var obj = arg.AsObject(); var dict = new Dictionary<String, Object>(); var properties = obj.GetOwnProperties(); foreach (var property in properties) dict.Add(property.Key, property.Value.Value.Clr()); return dict; }
private static string GetErrorMessage(JsValue error) { if (error.IsObject()) { var oi = error.AsObject(); var message = oi.Get("message").AsString(); return message; } else return string.Empty; }
/// <summary> /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.1 /// </summary> /// <param name="input"></param> /// <param name="preferredType"></param> /// <returns></returns> public static JsValue ToPrimitive(JsValue input, Types preferredType = Types.None) { if (input == Null.Instance || input == Undefined.Instance) { return input; } if (input.IsPrimitive()) { return input; } return input.AsObject().DefaultValue(preferredType); }
public RavenJObject ToRavenJObject(JsValue jsObject, string propertyKey = null) { var rjo = new RavenJObject(); foreach (var property in jsObject.AsObject().Properties) { if (property.Key == Constants.ReduceKeyFieldName || property.Key == Constants.DocumentIdFieldName) continue; var value = property.Value.Value; if (!value.HasValue) continue; if (value.Value.IsRegExp()) continue; rjo[property.Key] = ToRavenJToken(value.Value, CreatePropertyKey(property.Key, propertyKey)); } return rjo; }
public override JsValue Call(JsValue thisObject, JsValue[] arguments) { if (_method != null && thisObject.Type == Types.Object) { var node = thisObject.AsObject() as DomNodeInstance; if (node != null) { try { var parameters = _engine.BuildArgs(_method, arguments); return _method.Invoke(node.Value, parameters).ToJsValue(_engine); } catch { throw new JavaScriptException(Engine.Error); } } } return JsValue.Undefined; }
public RavenJObject ToRavenJObject(JsValue jsObject, string propertyKey = null, bool recursiveCall = false) { var rjo = new RavenJObject(); foreach (var property in jsObject.AsObject().Properties) { if (property.Key == Constants.ReduceKeyFieldName || property.Key == Constants.DocumentIdFieldName) continue; var value = property.Value.Value; if (value.HasValue == false) continue; if (value.Value.IsRegExp()) continue; var recursive = jsObject == value; if (recursiveCall && recursive) rjo[property.Key] = null; else rjo[property.Key] = ToRavenJToken(value.Value, CreatePropertyKey(property.Key, propertyKey), recursive); } return rjo; }
public static ObjectInstance ToObject(Engine engine, JsValue value) { if (value.IsObject()) { return value.AsObject(); } if (value == Undefined.Instance) { throw new JavaScriptException(engine.TypeError); } if (value == Null.Instance) { throw new JavaScriptException(engine.TypeError); } if (value.IsBoolean()) { return engine.Boolean.Construct(value.AsBoolean()); } if (value.IsNumber()) { return engine.Number.Construct(value.AsNumber()); } if (value.IsString()) { return engine.String.Construct(value.AsString()); } throw new JavaScriptException(engine.TypeError); }
/// <summary> /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.8 /// </summary> /// <param name="o"></param> /// <returns></returns> public static string ToString(JsValue o) { if (o.IsObject()) { var p = o.AsObject() as IPrimitiveInstance; if (p != null) { o = p.PrimitiveValue; } } if (o.IsString()) { return o.AsString(); } if (o == Undefined.Instance) { return Undefined.Text; } if (o == Null.Instance) { return Null.Text; } if (o.IsBoolean()) { return o.AsBoolean() ? "true" : "false"; } if (o.IsNumber()) { return NumberPrototype.ToNumberString(o.AsNumber()); } return ToString(ToPrimitive(o, Types.String)); }
/// <summary> /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.3 /// </summary> /// <param name="o"></param> /// <returns></returns> public static double ToNumber(JsValue o) { // check number first as this is what is usually expected if (o.IsNumber()) { return o.AsNumber(); } if (o.IsObject()) { var p = o.AsObject() as IPrimitiveInstance; if (p != null) { o = p.PrimitiveValue; } } if (o == Undefined.Instance) { return double.NaN; } if (o == Null.Instance) { return 0; } if (o.IsBoolean()) { return o.AsBoolean() ? 1 : 0; } if (o.IsString()) { var s = StringPrototype.TrimEx(o.AsString()); if (String.IsNullOrEmpty(s)) { return 0; } if ("+Infinity".Equals(s) || "Infinity".Equals(s)) { return double.PositiveInfinity; } if ("-Infinity".Equals(s)) { return double.NegativeInfinity; } // todo: use a common implementation with JavascriptParser try { if (!s.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) { var start = s[0]; if (start != '+' && start != '-' && start != '.' && !char.IsDigit(start)) { return double.NaN; } double n = Double.Parse(s, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowExponent, CultureInfo.InvariantCulture); if (s.StartsWith("-") && n.Equals(0)) { return -0.0; } return n; } int i = int.Parse(s.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture); return i; } catch (OverflowException) { return s.StartsWith("-") ? double.NegativeInfinity : double.PositiveInfinity; } catch { return double.NaN; } } return ToNumber(ToPrimitive(o, Types.Number)); }
public void ShouldBeAnObject() { var value = new JsValue(new ObjectInstance(null)); Assert.Equal(false, value.IsBoolean()); Assert.Equal(false, value.IsArray()); Assert.Equal(false, value.IsDate()); Assert.Equal(false, value.IsNull()); Assert.Equal(false, value.IsNumber()); Assert.Equal(true, value.IsObject()); Assert.Equal(true, value.AsObject() != null); Assert.Equal(false, value.IsPrimitive()); Assert.Equal(false, value.IsRegExp()); Assert.Equal(false, value.IsString()); Assert.Equal(false, value.IsUndefined()); }
internal JToken ConvertBack(JsValue value) { switch (value.Type) { case Jint.Runtime.Types.None: throw new NotSupportedException(); case Jint.Runtime.Types.Undefined: return JValue.CreateUndefined(); case Jint.Runtime.Types.Null: return JValue.CreateNull(); case Jint.Runtime.Types.Boolean: return new JValue(value.AsBoolean()); case Jint.Runtime.Types.String: return JValue.CreateString(value.AsString()); case Jint.Runtime.Types.Number: return new JValue(value.AsNumber()); case Jint.Runtime.Types.Object: var ntsObjectInstance = value.AsObject() as NTSObjectInstance; if (ntsObjectInstance != null) return ntsObjectInstance.root; return new JObject(value.AsObject().Properties.Where(kvp => !kvp.Value.Enumerable.HasValue || kvp.Value.Enumerable.Value).Select(kvp => new JProperty(kvp.Key, ConvertBack(kvp.Value.Value ?? JsValue.Undefined)))); default: throw new NotSupportedException(); } }
public object UncastJsValue(JsValue value) { if (value.IsBoolean()) { return value.AsBoolean(); } if (value.IsNumber()) { return value.AsNumber(); } if (value.IsObject()) { return value.AsObject(); } if (value.IsString()) { return value.AsString(); } return null; }