本文整理汇总了C#中Jint.Native.JsValue.AsObject方法的典型用法代码示例。如果您正苦于以下问题:C# JsValue.AsObject方法的具体用法?C# JsValue.AsObject怎么用?C# JsValue.AsObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Jint.Native.JsValue
的用法示例。
在下文中一共展示了JsValue.AsObject方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToRavenJObject
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;
}
示例2: ToObjBag
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;
}
示例3: GetErrorMessage
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;
}
示例4: ToPrimitive
/// <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);
}
示例5: ToRavenJObject
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;
}
示例6: Call
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;
}
示例7: ToRavenJObject
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;
}
示例8: ToObject
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);
}
示例9: ToString
/// <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));
}
示例10: ToNumber
/// <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));
}
示例11: ShouldBeAnObject
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());
}
示例12: ConvertBack
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();
}
}
示例13: UncastJsValue
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;
}