1 28 29 package com.caucho.es; 30 31 34 class NativeNumber extends Native { 35 static final int NEW = 1; 36 static final int TO_STRING = NEW + 1; 37 static final int VALUE_OF = TO_STRING + 1; 38 39 42 private NativeNumber(String name, int n, int len) 43 { 44 super(name, len); 45 46 this.n = n; 47 } 48 49 52 static ESObject create(Global resin) 53 { 54 Native nativeNum = new NativeNumber("Number", NEW, 1); 55 ESWrapper numProto = new ESWrapper("Number", resin.objProto, 56 ESNumber.create(0)); 57 NativeWrapper num = new NativeWrapper(resin, nativeNum, numProto, 58 ESThunk.NUM_THUNK); 59 resin.numProto = numProto; 60 61 int flags = DONT_ENUM; 62 int allflags = (DONT_ENUM|DONT_DELETE|READ_ONLY); 63 64 numProto.put(ESId.intern("toString"), 65 new NativeNumber("toString", TO_STRING, 0), 66 flags); 67 numProto.put(ESId.intern("valueOf"), 68 new NativeNumber("valueOf", VALUE_OF, 0), 69 flags); 70 71 num.put("length", ESNumber.create(1), allflags); 72 num.put("MAX_VALUE", ESNumber.create(Double.MAX_VALUE), allflags); 73 num.put("MIN_VALUE", ESNumber.create(Double.MIN_VALUE), allflags); 74 num.put("NaN", ESNumber.create(0.0/0.0), allflags); 75 num.put("NEGATIVE_INFINITY", ESNumber.create(-1.0/0.0), allflags); 76 num.put("POSITIVE_INFINITY", ESNumber.create(1.0/0.0), allflags); 77 78 numProto.setClean(); 79 num.setClean(); 80 81 return num; 82 } 83 84 public ESBase call(Call eval, int length) throws Throwable 85 { 86 ESBase argThis; 87 88 switch (n) { 89 case NEW: 90 if (length == 0) 91 return ESNumber.create(0); 92 else 93 return ESNumber.create(eval.getArg(0).toNum()); 94 95 case TO_STRING: 96 try { 97 return ((ESBase) ((ESWrapper) eval.getArg(-1)).value).toStr(); 98 } catch (ClassCastException e) { 99 if (eval.getArg(-1) instanceof ESNumber) 100 return eval.getArg(-1); 101 if (eval.getArg(-1) instanceof ESThunk) 102 return ((ESBase) ((ESWrapper) ((ESThunk) eval.getArg(-1)).getObject()).value).toStr(); 103 104 throw new ESException("toString expected number object"); 105 } 106 107 case VALUE_OF: 108 try { 109 return (ESBase) ((ESWrapper) eval.getArg(-1)).value; 110 } catch (ClassCastException e) { 111 if (eval.getArg(-1) instanceof ESNumber) 112 return eval.getArg(-1); 113 if (eval.getArg(-1) instanceof ESThunk) 114 return (ESBase) ((ESWrapper) ((ESThunk) eval.getArg(-1)).getObject()).value; 115 116 throw new ESException("valueOf expected number object"); 117 } 118 119 default: 120 throw new RuntimeException ("Unknown object function"); 121 } 122 } 123 124 public ESBase construct(Call eval, int length) throws Throwable 125 { 126 if (n != NEW) 127 return super.construct(eval, length); 128 129 ESBase value; 130 131 if (length == 0) 132 value = ESNumber.create(0); 133 else 134 value = ESNumber.create(eval.getArg(0).toNum()); 135 136 return value.toObject(); 137 } 138 } 139 | Popular Tags |