1 28 29 package com.caucho.es; 30 31 34 class NativeRegexp extends Native { 35 static ESId INDEX = ESId.intern("index"); 36 static ESId INPUT = ESId.intern("input"); 37 38 static final int NEW = 1; 39 static final int COMPILE = NEW + 1; 40 static final int EXEC = COMPILE + 1; 41 static final int TEST = EXEC + 1; 42 static final int TO_STRING = TEST + 1; 43 44 47 private NativeRegexp(String name, int n, int len) 48 { 49 super(name, len); 50 51 this.n = n; 52 } 53 54 57 static ESRegexpWrapper create(Global resin) 58 { 59 NativeRegexp nativeRegexp = new NativeRegexp("Regexp", NEW, 1); 60 ESRegexp proto; 61 62 try { 63 proto = new ESRegexp("", ""); 64 } catch (Exception e) { 65 throw new RuntimeException (); 66 } 67 proto.prototype = resin.objProto; 68 resin.regexpProto = proto; 69 ESRegexpWrapper regexp = new ESRegexpWrapper(resin, nativeRegexp, proto); 70 71 put(proto, "exec", EXEC, 1); 72 put(proto, "compile", COMPILE, 2); 73 put(proto, "test", TEST, 1); 74 put(proto, "toString", TO_STRING, 0); 75 76 proto.setClean(); 77 regexp.setClean(); 78 79 return regexp; 80 } 81 82 static private void put(ESObject proto, String name, int n, int len) 83 { 84 ESId id = ESId.intern(name); 85 NativeRegexp fun = new NativeRegexp(name, n, len); 86 87 proto.put(id, fun, DONT_ENUM); 88 } 89 90 public ESBase call(Call eval, int length) throws Throwable 91 { 92 switch (n) { 93 case NEW: 94 return create(eval, length); 95 96 case TO_STRING: 97 try { 98 ESRegexp regexp = (ESRegexp) eval.getThis(); 99 String s = regexp.pattern.toString(); 100 String f = regexp.flags.toString(); 101 102 return ESString.create("/" + s + "/" + f); 103 } catch (ClassCastException e) { 104 throw new ESException("toString expected regexp object"); 105 } 106 107 case EXEC: 108 return exec(eval, length); 109 110 case COMPILE: 111 return compile(eval, length); 112 113 case TEST: 114 return test(eval, length); 115 116 default: 117 throw new ESException("Unknown object function"); 118 } 119 } 120 121 private ESBase create(Call eval, int length) throws Throwable 122 { 123 ESString pattern; 124 ESString flags = null; 125 126 if (length == 0) 127 pattern = ESString.NULL; 128 else 129 pattern = eval.getArg(0).toStr(); 130 131 if (length > 1) 132 flags = eval.getArg(1).toStr(); 133 else 134 flags = ESString.NULL; 135 136 ESObject obj; 137 obj = new ESRegexp(pattern, flags); 138 139 return obj; 140 } 141 142 private ESBase compile(Call eval, int length) throws Throwable 143 { 144 ESString pattern; 145 ESString flags = null; 146 ESBase arg = eval.getArg(-1); 147 148 if (arg instanceof ESThunk) 149 arg = ((ESThunk) arg).toObject(); 150 151 if (! (arg instanceof ESRegexp)) 152 throw new ESException("compile must be bound to regexp"); 153 ESRegexp regexp = (ESRegexp) arg; 154 155 if (length == 0) 156 return esUndefined; 157 else 158 pattern = eval.getArg(0).toStr(); 159 160 if (length > 1) 161 flags = eval.getArg(1).toStr(); 162 else 163 flags = ESString.NULL; 164 165 regexp.compile(pattern, flags); 166 167 return regexp; 168 } 169 170 static ESBase exec(Call eval, int length) throws Throwable 171 { 172 ESBase reg = eval.getArg(-1); 173 ESRegexp regexp; 174 175 if (reg instanceof ESThunk) 176 reg = ((ESThunk) reg).toObject(); 177 178 if (reg instanceof ESRegexp) 179 regexp = (ESRegexp) reg; 180 else 181 regexp = new ESRegexp(reg.toStr(), ESString.NULL); 182 if (regexp.prototype == null) 183 throw new RuntimeException (); 184 185 ESString string; 186 187 Global global = Global.getGlobalProto(); 188 if (length == 0) 189 string = global.getRegexp().getProperty(global.getRegexp().INPUT).toStr(); 190 else 191 string = eval.getArg(0).toStr(); 192 193 global.getRegexp().setRegexp(regexp); 194 if (! regexp.exec(string)) 195 return esNull; 196 197 return esNull; 198 216 } 217 218 private ESBase test(Call eval, int length) throws Throwable 219 { 220 ESBase reg = eval.getArg(-1); 221 ESRegexp regexp; 222 223 if (reg instanceof ESThunk) 224 reg = ((ESThunk) reg).toObject(); 225 226 if (reg instanceof ESRegexp) 227 regexp = (ESRegexp) reg; 228 else 229 regexp = new ESRegexp(reg.toStr(), ESString.NULL); 230 if (regexp.prototype == null) 231 throw new RuntimeException (); 232 233 ESString string; 234 ESRegexpWrapper globalRegexp = Global.getGlobalProto().getRegexp(); 235 if (length == 0) 236 string = globalRegexp.getProperty(globalRegexp.INPUT).toStr(); 237 else 238 string = eval.getArg(0).toStr(); 239 240 globalRegexp.setRegexp(regexp); 241 return ESBoolean.create(regexp.exec(string)); 242 } 243 } 244 | Popular Tags |