1 28 29 package com.caucho.es; 30 31 34 class NativeBoolean 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 NativeBoolean(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 nativeBool = new NativeBoolean("Boolean", NEW, 1); 55 ESWrapper boolProto = new ESWrapper("Boolean", resin.objProto, 56 ESBoolean.FALSE); 57 NativeWrapper bool = new NativeWrapper(resin, nativeBool, 58 boolProto, ESThunk.BOOL_THUNK); 59 resin.boolProto = boolProto; 60 61 put(boolProto, "toString", TO_STRING, 0, DONT_ENUM); 62 put(boolProto, "valueOf", VALUE_OF, 0, DONT_ENUM); 63 64 bool.setClean(); 65 boolProto.setClean(); 66 67 return bool; 68 } 69 70 private static void put(ESObject obj, String name, int n, int len, 71 int flags) 72 { 73 obj.put(name, new NativeBoolean(name, n, len), flags); 74 } 75 76 public ESBase call(Call eval, int length) throws Throwable 77 { 78 switch (n) { 79 case NEW: 80 if (length == 0) 81 return ESBoolean.FALSE; 82 else 83 return ESBoolean.create(eval.getArg(0).toBoolean()); 84 85 case TO_STRING: 86 try { 87 return ((ESBase) ((ESWrapper) eval.getArg(-1)).value).toStr(); 88 } catch (ClassCastException e) { 89 if (eval.getArg(-1) instanceof ESBoolean) 90 return eval.getArg(-1); 91 if (eval.getArg(-1) instanceof ESThunk) 92 return ((ESBase) ((ESWrapper) ((ESThunk) eval.getArg(-1)).getObject()).value).toStr(); 93 94 throw new ESException("toString expected boolean object"); 95 } 96 97 case VALUE_OF: 98 try { 99 return (ESBase) ((ESWrapper) eval.getArg(-1)).value; 100 } catch (ClassCastException e) { 101 if (eval.getArg(-1) instanceof ESBoolean) 102 return eval.getArg(-1); 103 if (eval.getArg(-1) instanceof ESThunk) 104 return (ESBase) ((ESWrapper) ((ESThunk) eval.getArg(-1)).getObject()).value; 105 106 throw new ESException("valueOf expected boolean object"); 107 } 108 109 default: 110 throw new RuntimeException ("Unknown object function"); 111 } 112 } 113 114 public ESBase construct(Call eval, int length) throws Throwable 115 { 116 if (n != NEW) 117 return super.construct(eval, length); 118 119 ESBase value; 120 121 if (length == 0) 122 value = ESBoolean.FALSE; 123 else 124 value = ESBoolean.create(eval.getArg(0).toBoolean()); 125 126 return value.toObject(); 127 } 128 } 129 | Popular Tags |