本文整理汇总了C#中JsValue.IsNumber方法的典型用法代码示例。如果您正苦于以下问题:C# JsValue.IsNumber方法的具体用法?C# JsValue.IsNumber怎么用?C# JsValue.IsNumber使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JsValue
的用法示例。
在下文中一共展示了JsValue.IsNumber方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToBoolean
/// <summary>
/// http://www.ecma-international.org/ecma-262/5.1/#sec-9.2
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static bool ToBoolean(JsValue o)
{
if (o.IsObject())
{
return true;
}
if (o == Undefined.Instance || o == Null.Instance)
{
return false;
}
if (o.IsBoolean())
{
return o.AsBoolean();
}
if (o.IsNumber())
{
var n = o.AsNumber();
if (n.Equals(0) || double.IsNaN(n))
{
return false;
}
else
{
return true;
}
}
if (o.IsString())
{
var s = o.AsString();
if (String.IsNullOrEmpty(s))
{
return false;
}
else
{
return true;
}
}
return true;
}
示例2: ToLocaleString
private JsValue ToLocaleString(JsValue thisObject, JsValue[] arguments)
{
if (!thisObject.IsNumber() && (thisObject.TryCast<NumberInstance>() == null))
{
throw new JavaScriptException(Engine.TypeError);
}
var m = TypeConverter.ToNumber(thisObject);
if (double.IsNaN(m))
{
return "NaN";
}
if (m.Equals(0))
{
return "0";
}
if (m < 0)
{
return "-" + ToLocaleString(-m, arguments);
}
if (double.IsPositiveInfinity(m) || m >= double.MaxValue)
{
return "Infinity";
}
if (double.IsNegativeInfinity(m) || m <= -double.MaxValue)
{
return "-Infinity";
}
return m.ToString("n", Engine.Options.GetCulture());
}
示例3: Serialize
public JsValue Serialize(JsValue value, JsValue replacer, JsValue space)
{
_stack = new Stack<object>();
// for JSON.stringify(), any function passed as the first argument will return undefined
// if the replacer is not defined. The function is not called either.
if (value.Is<ICallable>() && replacer == Undefined.Instance)
{
return Undefined.Instance;
}
if (replacer.IsObject())
{
if (replacer.Is<ICallable>())
{
_replacerFunction = replacer;
}
else
{
var replacerObj = replacer.AsObject();
if (replacerObj.Class == "Array")
{
_propertyList = new List<string>();
}
foreach (var property in replacerObj.GetOwnProperties().Select(x => x.Value))
{
JsValue v = _engine.GetValue(property);
string item = null;
if (v.IsString())
{
item = v.AsString();
}
else if (v.IsNumber())
{
item = TypeConverter.ToString(v);
}
else if (v.IsObject())
{
var propertyObj = v.AsObject();
if (propertyObj.Class == "String" || propertyObj.Class == "Number")
{
item = TypeConverter.ToString(v);
}
}
if (item != null && !_propertyList.Contains(item))
{
_propertyList.Add(item);
}
}
}
}
if (space.IsObject())
{
var spaceObj = space.AsObject();
if (spaceObj.Class == "Number")
{
space = TypeConverter.ToNumber(spaceObj);
}
else if (spaceObj.Class == "String")
{
space = TypeConverter.ToString(spaceObj);
}
}
// defining the gap
if (space.IsNumber())
{
if (space.AsNumber() > 0) {
_gap = new System.String(' ', (int)System.Math.Min(10, space.AsNumber()));
}
else
{
_gap = string.Empty;
}
}
else if (space.IsString())
{
var stringSpace = space.AsString();
_gap = stringSpace.Length <= 10 ? stringSpace : stringSpace.Substring(0, 10);
}
else
{
_gap = string.Empty;
}
var wrapper = _engine.Object.Construct(Arguments.Empty);
wrapper.DefineOwnProperty("", new PropertyDescriptor(value, true, true, true), false);
return Str("", wrapper);
}
示例4: ToNumberString
private JsValue ToNumberString(JsValue thisObject, JsValue[] arguments)
{
if (!thisObject.IsNumber() && (thisObject.TryCast<NumberInstance>() == null))
{
throw new JavaScriptException(Engine.TypeError);
}
var radix = arguments.At(0) == JsValue.Undefined ? 10 : (int) TypeConverter.ToInteger(arguments.At(0));
if (radix < 2 || radix > 36)
{
throw new JavaScriptException(Engine.RangeError, "radix must be between 2 and 36");
}
var x = TypeConverter.ToNumber(thisObject);
if (double.IsNaN(x))
{
return "NaN";
}
if (x.Equals(0))
{
return "0";
}
if (double.IsPositiveInfinity(x) || x >= double.MaxValue)
{
return "Infinity";
}
if (x < 0)
{
return "-" + ToNumberString(-x, arguments);
}
if (radix == 10)
{
return ToNumberString(x);
}
var integer = (long) x;
var fraction = x - integer;
string result = ToBase(integer, radix);
if (!fraction.Equals(0))
{
result += "." + ToFractionBase(fraction, radix);
}
return result;
}
示例5: 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);
}
示例6: 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));
}
示例7: 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 = o.AsString().Trim();
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));
}