/// <summary> /// https://tc39.es/ecma262/#sec-touint16 /// </summary> public static ushort ToUint16(JsValue o) { if (o._type == InternalTypes.Integer) { var integer = o.AsInteger(); if (integer is >= 0 and <= ushort.MaxValue) { return((ushort)integer); } } var number = ToNumber(o); if (double.IsNaN(number) || number == 0 || double.IsInfinity(number)) { return(0); } var intValue = Math.Floor(Math.Abs(number)); if (number < 0) { intValue *= -1; } var int16Bit = intValue % 65_536; // 2^16 return((ushort)int16Bit); }
private static JsValue CompareInteger(JsValue x, JsValue y, bool leftFirst) { int nx, ny; if (leftFirst) { nx = x.AsInteger(); ny = y.AsInteger(); } else { ny = y.AsInteger(); nx = x.AsInteger(); } return(nx < ny); }
/// <summary> /// https://tc39.es/ecma262/#sec-touint8clamp /// </summary> internal static byte ToUint8Clamp(JsValue o) { if (o._type == InternalTypes.Integer) { var intValue = o.AsInteger(); if (intValue is > -1 and < 256) { return((byte)intValue); } } return(ToUint8ClampUnlikely(o)); }
/// <summary> /// https://tc39.es/ecma262/#sec-touint32 /// </summary> public static uint ToUint32(JsValue o) { if (o._type == InternalTypes.Integer) { return((uint)o.AsInteger()); } var doubleVal = ToNumber(o); if (doubleVal is >= 0.0 and <= uint.MaxValue) { // Double-to-uint cast is correct in this range return((uint)doubleVal); } return((uint)DoubleToInt32Slow(doubleVal)); }
/// <summary> /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.5 /// </summary> public static int ToInt32(JsValue o) { if (o._type == InternalTypes.Integer) { return(o.AsInteger()); } var doubleVal = ToNumber(o); if (doubleVal >= -(double)int.MinValue && doubleVal <= int.MaxValue) { // Double-to-int cast is correct in this range return((int)doubleVal); } return(DoubleToInt32Slow(doubleVal)); }
private static JsValue DivideInteger(JsValue lval, JsValue rval) { var lN = lval.AsInteger(); var rN = rval.AsInteger(); if (lN == 0 && rN == 0) { return(JsNumber.DoubleNaN); } if (rN == 0) { return(lN > 0 ? double.PositiveInfinity : double.NegativeInfinity); } if (lN % rN == 0) { return(lN / rN); } return((double)lN / rN); }
private static int ToIntegerSupportInfinity(JsValue numberVal) { return(numberVal._type == InternalTypes.Integer ? numberVal.AsInteger() : ToIntegerSupportInfinityUnlikely(numberVal)); }
/// <summary> /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.7 /// </summary> public static ushort ToUint16(JsValue o) { return(o._type == InternalTypes.Integer ? (ushort)(uint)o.AsInteger() : (ushort)(uint)ToNumber(o)); }
/// <summary> /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.5 /// </summary> public static int ToInt32(JsValue o) { return(o._type == InternalTypes.Integer ? o.AsInteger() : (int)(uint)ToNumber(o)); }
/// <summary> /// https://tc39.es/ecma262/#sec-touint8 /// </summary> internal static double ToUint8(JsValue o) { return(o._type == InternalTypes.Integer ? (byte)o.AsInteger() : (byte)(long)ToNumber(o)); }
/// <summary> /// https://tc39.es/ecma262/#sec-toint16 /// </summary> internal static double ToInt16(JsValue o) { return(o._type == InternalTypes.Integer ? (short)o.AsInteger() : (short)(long)ToNumber(o)); }
public static bool StrictlyEqual(JsValue x, JsValue y) { var typeX = x._type & ~InternalTypes.InternalFlags; var typeY = y._type & ~InternalTypes.InternalFlags; if (typeX != typeY) { if (typeX == InternalTypes.Integer) { typeX = InternalTypes.Number; } if (typeY == InternalTypes.Integer) { typeY = InternalTypes.Number; } if (typeX != typeY) { return(false); } } if (typeX == InternalTypes.Undefined || typeX == InternalTypes.Null) { return(true); } if (typeX == InternalTypes.Integer) { return(x.AsInteger() == y.AsInteger()); } if (typeX == InternalTypes.Number) { var nx = ((JsNumber)x)._value; var ny = ((JsNumber)y)._value; return(!double.IsNaN(nx) && !double.IsNaN(ny) && nx == ny); } if ((typeX & InternalTypes.String) != 0) { return(x.ToString() == y.ToString()); } if (typeX == InternalTypes.Boolean) { return(((JsBoolean)x)._value == ((JsBoolean)y)._value); } if ((typeX & InternalTypes.Object) != 0 && x.AsObject() is IObjectWrapper xw) { var yw = y.AsObject() as IObjectWrapper; if (yw == null) { return(false); } return(Equals(xw.Target, yw.Target)); } return(x == y); }