本文整理汇总了C#中Jint.Native.JsInstance.ToNumber方法的典型用法代码示例。如果您正苦于以下问题:C# JsInstance.ToNumber方法的具体用法?C# JsInstance.ToNumber怎么用?C# JsInstance.ToNumber使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Jint.Native.JsInstance
的用法示例。
在下文中一共展示了JsInstance.ToNumber方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1:
/// <summary>
/// Overriden indexer to optimize cases when we already have a number
/// </summary>
/// <param name="key">index</param>
/// <returns>value</returns>
public override JsInstance this[JsInstance key] {
get {
double keyNumber = key.ToNumber();
int i = (int)keyNumber;
if (i == keyNumber && i >= 0) {
// we have got an index
return this.get(i);
}
else {
return base[key.ToString()];
}
}
set {
double keyNumber = key.ToNumber();
int i = (int)keyNumber;
if (i == keyNumber && i >= 0) {
// we have got an index
this.put(i, value);
}
else {
base[key.ToString()] = value;
}
}
}
示例2: ConvertParameter
/// <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;
}
}
}
示例3: Delete
public override void Delete(JsInstance key)
{
double keyNumber = key.ToNumber();
int index = (int)keyNumber;
if (index == keyNumber)
m_data.Remove(index);
else
base.Delete(key.ToString());
}
示例4: ToExponentialImpl
/// <summary>
/// 15.7.4.6
/// </summary>
/// <param name="target"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public JsInstance ToExponentialImpl(JsInstance target, JsInstance[] parameters)
{
if (double.IsInfinity(target.ToNumber()) || double.IsNaN(target.ToNumber())) {
return ToStringImpl(target, new JsInstance[0]);
}
int fractions = 16;
if (parameters.Length > 0) {
fractions = Convert.ToInt32(parameters[0].ToNumber());
}
if (fractions > 20 || fractions < 0) {
throw new JsException(Global.SyntaxErrorClass.New("Fraction Digits must be greater than 0 and lesser than 20"));
}
string format = String.Concat("#.", new String('0', fractions), "e+0");
return Global.StringClass.New(target.ToNumber().ToString(format, CultureInfo.InvariantCulture));
}
示例5: ToStringImpl
public JsInstance ToStringImpl(JsInstance target, JsInstance[] parameters)
{
if (target == this["NaN"]) {
return Global.StringClass.New("NaN");
}
if (target == this["NEGATIVE_INFINITY"]) {
return Global.StringClass.New("-Infinity");
}
if (target == this["POSITIVE_INFINITY"]) {
return Global.StringClass.New("Infinity");
}
int radix = 10;
// is radix defined ?
if (parameters.Length > 0) {
if (parameters[0] != JsUndefined.Instance) {
radix = (int)parameters[0].ToNumber();
}
}
var longToBeFormatted = (long)target.ToNumber();
if (radix == 10) {
return Global.StringClass.New(target.ToNumber().ToString(CultureInfo.InvariantCulture).ToLower());
}
else {
// Extract the magnitude for conversion.
long longPositive = Math.Abs(longToBeFormatted);
int digitIndex = 0;
char[] outDigits = new char[63];
// Convert the magnitude to a digit string.
for (digitIndex = 0; digitIndex <= 64; digitIndex++)
{
if (longPositive == 0) break;
outDigits[outDigits.Length - digitIndex - 1] =
rDigits[longPositive % radix];
longPositive /= radix;
}
// Add a minus sign if the argument is negative.
if (longToBeFormatted < 0)
outDigits[outDigits.Length - digitIndex++ - 1] = '-';
return Global.StringClass.New(new string(outDigits,
outDigits.Length - digitIndex, digitIndex).ToLower());
}
}
示例6: ToPrecisionImpl
/// <summary>
/// 15.7.4.7
/// </summary>
/// <param name="target"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public JsInstance ToPrecisionImpl(JsInstance target, JsInstance[] parameters)
{
if (double.IsInfinity(target.ToNumber()) || double.IsNaN(target.ToNumber())) {
return ToStringImpl(target, new JsInstance[0]);
}
if (parameters.Length == 0) {
throw new JsException(Global.SyntaxErrorClass.New("precision missing"));
}
if (parameters[0] == JsUndefined.Instance) {
return ToStringImpl(target, new JsInstance[0]);
}
int precision = 0;
if (parameters.Length > 0) {
precision = Convert.ToInt32(parameters[0].ToNumber());
}
if (precision < 1 || precision > 21) {
throw new JsException(Global.RangeErrorClass.New("precision must be between 1 and 21"));
}
// Get the number of decimals
string str = target.ToNumber().ToString("e23", CultureInfo.InvariantCulture);
int decimals = str.IndexOfAny(new char[] { '.', 'e' });
decimals = decimals == -1 ? str.Length : decimals;
precision -= decimals;
precision = precision < 1 ? 1 : precision;
return Global.StringClass.New(target.ToNumber().ToString("f" + precision, CultureInfo.InvariantCulture));
}
示例7: ToFixedImpl
/// <summary>
/// 15.7.4.5
/// </summary>
/// <param name="target"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public JsInstance ToFixedImpl(JsInstance target, JsInstance[] parameters)
{
int fractions = 0;
if (parameters.Length > 0) {
fractions = Convert.ToInt32(parameters[0].ToNumber());
}
if (fractions > 20 || fractions < 0) {
throw new JsException(Global.SyntaxErrorClass.New("Fraction Digits must be greater than 0 and lesser than 20"));
}
if (target == Global.NaN) {
return Global.StringClass.New(target.ToString());
}
return Global.StringClass.New(target.ToNumber().ToString("f" + fractions, CultureInfo.InvariantCulture));
}
示例8: CompareTo
public bool CompareTo(JsInstance x, JsInstance y, out int result)
{
result = 0;
if (x.IsClr && y.IsClr) {
IComparable xcmp = x.Value as IComparable;
if (xcmp == null || y.Value == null || xcmp.GetType() != y.Value.GetType())
return false;
result = xcmp.CompareTo(y.Value);
} else {
Double xnum = x.ToNumber();
Double ynum = y.ToNumber();
if (Double.IsNaN(xnum) || Double.IsNaN(ynum))
return false;
if (xnum < ynum)
result = -1;
else if (xnum == ynum)
result = 0;
else
result = 1;
}
return true;
}
示例9: Compare
public JsBoolean Compare(JsInstance x, JsInstance y)
{
if (x.IsClr && y.IsClr)
{
return Global.BooleanClass.New(x.Value.Equals(y.Value));
}
if (x.IsClr)
{
return Compare(x.ToPrimitive(Global), y);
}
if (y.IsClr)
{
return Compare(x, y.ToPrimitive(Global));
}
if (x.Type == y.Type)
{ // if both are Objects but then only one is Clrs
if (x == JsUndefined.Instance)
{
return Global.BooleanClass.True;
}
else if (x == JsNull.Instance)
{
return Global.BooleanClass.True;
}
else if (x.Type == JsInstance.TYPE_NUMBER)
{
if (x.ToNumber() == double.NaN)
{
return Global.BooleanClass.False;
}
else if (y.ToNumber() == double.NaN)
{
return Global.BooleanClass.False;
}
else if (x.ToNumber() == y.ToNumber())
{
return Global.BooleanClass.True;
}
else
{
return Global.BooleanClass.False;
}
}
else if (x.Type == JsInstance.TYPE_STRING)
{
return Global.BooleanClass.New(x.ToString() == y.ToString());
}
else if (x.Type == JsInstance.TYPE_BOOLEAN)
{
return Global.BooleanClass.New(x.ToBoolean() == y.ToBoolean());
}
else if (x.Type == JsInstance.TYPE_OBJECT )
{
return Global.BooleanClass.New(x == y);
}
else
{
return Global.BooleanClass.New(x.Value.Equals(y.Value));
}
}
else if (x == JsNull.Instance && y == JsUndefined.Instance)
{
return Global.BooleanClass.True;
}
else if (x == JsUndefined.Instance && y == JsNull.Instance)
{
return Global.BooleanClass.True;
}
else if (x.Type == JsInstance.TYPE_NUMBER && y.Type == JsInstance.TYPE_STRING)
{
return Global.BooleanClass.New(x.ToNumber() == y.ToNumber());
}
else if (x.Type == JsInstance.TYPE_STRING && y.Type == JsInstance.TYPE_NUMBER)
{
return Global.BooleanClass.New(x.ToNumber() == y.ToNumber());
}
else if (x.Type == JsInstance.TYPE_BOOLEAN || y.Type == JsInstance.TYPE_BOOLEAN)
{
return Global.BooleanClass.New(x.ToNumber() == y.ToNumber());
}
else if (y.Type == JsInstance.TYPE_OBJECT && (x.Type == JsInstance.TYPE_STRING || x.Type == JsInstance.TYPE_NUMBER))
{
return Compare(x, y.ToPrimitive(Global));
}
else if (x.Type == JsInstance.TYPE_OBJECT && (y.Type == JsInstance.TYPE_STRING || y.Type == JsInstance.TYPE_NUMBER))
{
return Compare(x.ToPrimitive(Global), y);
}
else
{
return Global.BooleanClass.False;
}
}
示例10: ToStringImpl
public JsInstance ToStringImpl(JsInstance target, JsInstance[] parameters)
{
if (target == this["NaN"])
{
return Global.StringClass.New("NaN");
}
if (target == this["NEGATIVE_INFINITY"])
{
return Global.StringClass.New("-Infinity");
}
if (target == this["POSITIVE_INFINITY"])
{
return Global.StringClass.New("Infinity");
}
double radix = 10;
// is radix defined ?
if (parameters.Length > 0)
{
if (parameters[0] != JsUndefined.Instance)
{
radix = parameters[0].ToNumber();
}
}
if (radix == 10)
{
return Global.StringClass.New(target.ToNumber().ToString(CultureInfo.InvariantCulture).ToLower());
}
else
{
return Global.StringClass.New(Convert.ToString(Convert.ToUInt32(target.ToNumber(), CultureInfo.InvariantCulture), Convert.ToInt32(radix)).ToLower());
}
}