3 abstract class JSNumber extends JSPrimitive {
4 boolean jsequals(JS o) {
5 if(o == this) return true;
6 if(o instanceof JSNumber) {
7 JSNumber n = (JSNumber) o;
8 if(this instanceof D || n instanceof D) return n.toDouble() == toDouble();
9 return n.toLong() == toLong();
10 } else if(o instanceof JSString) {
11 String s = ((JSString)o).s.trim();
13 if(this instanceof D || s.indexOf('.') != -1) return Double.parseDouble(s) == toDouble();
14 return Long.parseLong(s) == toLong();
15 } catch(NumberFormatException e) {
22 // FEATURE: Better hash function? (if d != (int) d then do something double specific)
23 public int hashCode() { return toInt(); }
26 long toLong() { return toInt(); }
27 boolean toBoolean() { return toInt() != 0; }
28 double toDouble() { return toLong(); }
29 float toFloat() { return (float) toDouble(); }
31 final static class I extends JSNumber {
33 I(int i) { this.i = i; }
34 int toInt() { return i; }
35 public String coerceToString() { return Integer.toString(i); }
38 final static class L extends JSNumber {
40 L(long l) { this.l = l; }
41 int toInt() { return (int) l; }
42 long toLong() { return l; }
43 public String coerceToString() { return Long.toString(l); }
46 final static class D extends JSNumber {
47 private final double d;
48 D(double d) { this.d = d; }
49 int toInt() { return (int) d; }
50 long toLong() { return (long) d; }
51 double toDouble() { return d; }
52 boolean toBoolean() { return d == d && d != 0.0; }
53 public String coerceToString() { return d == (long) d ? Long.toString((long)d) : Double.toString(d); }
56 final static class B extends JSNumber {
57 private final boolean b;
58 B(boolean b) { this.b = b; }
59 int toInt() { return b ? 1 : 0; }
60 public String coerceToString() { return b ? "true" : "false"; }