| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript.regexp; |
| 8 |
|
| 9 |
import org.mozilla.javascript.*; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class NativeRegExpCtor extends BaseFunction |
| 25 |
{ |
| 26 |
static final long serialVersionUID = -5733330028285400526L; |
| 27 |
|
| 28 |
NativeRegExpCtor() |
| 29 |
{ |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
public String getFunctionName() |
| 34 |
{ |
| 35 |
return "RegExp"; |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public int getLength() { |
| 40 |
return 2; |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public int getArity() { |
| 45 |
return 2; |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public Object call(Context cx, Scriptable scope, Scriptable thisObj, |
| 50 |
Object[] args) |
| 51 |
{ |
| 52 |
if (args.length > 0 && args[0] instanceof NativeRegExp && |
| 53 |
(args.length == 1 || args[1] == Undefined.instance)) |
| 54 |
{ |
| 55 |
return args[0]; |
| 56 |
} |
| 57 |
return construct(cx, scope, args); |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public Scriptable construct(Context cx, Scriptable scope, Object[] args) |
| 62 |
{ |
| 63 |
NativeRegExp re = new NativeRegExp(); |
| 64 |
re.compile(cx, scope, args); |
| 65 |
ScriptRuntime.setBuiltinProtoAndParent(re, scope, TopLevel.Builtins.RegExp); |
| 66 |
return re; |
| 67 |
} |
| 68 |
|
| 69 |
private static RegExpImpl getImpl() |
| 70 |
{ |
| 71 |
Context cx = Context.getCurrentContext(); |
| 72 |
return (RegExpImpl) ScriptRuntime.getRegExpProxy(cx); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
private static final int |
| 78 |
Id_multiline = 1, |
| 79 |
Id_STAR = 2, |
| 80 |
|
| 81 |
Id_input = 3, |
| 82 |
Id_UNDERSCORE = 4, |
| 83 |
|
| 84 |
Id_lastMatch = 5, |
| 85 |
Id_AMPERSAND = 6, |
| 86 |
|
| 87 |
Id_lastParen = 7, |
| 88 |
Id_PLUS = 8, |
| 89 |
|
| 90 |
Id_leftContext = 9, |
| 91 |
Id_BACK_QUOTE = 10, |
| 92 |
|
| 93 |
Id_rightContext = 11, |
| 94 |
Id_QUOTE = 12, |
| 95 |
|
| 96 |
DOLLAR_ID_BASE = 12; |
| 97 |
|
| 98 |
private static final int |
| 99 |
Id_DOLLAR_1 = DOLLAR_ID_BASE + 1, |
| 100 |
Id_DOLLAR_2 = DOLLAR_ID_BASE + 2, |
| 101 |
Id_DOLLAR_3 = DOLLAR_ID_BASE + 3, |
| 102 |
Id_DOLLAR_4 = DOLLAR_ID_BASE + 4, |
| 103 |
Id_DOLLAR_5 = DOLLAR_ID_BASE + 5, |
| 104 |
Id_DOLLAR_6 = DOLLAR_ID_BASE + 6, |
| 105 |
Id_DOLLAR_7 = DOLLAR_ID_BASE + 7, |
| 106 |
Id_DOLLAR_8 = DOLLAR_ID_BASE + 8, |
| 107 |
Id_DOLLAR_9 = DOLLAR_ID_BASE + 9, |
| 108 |
|
| 109 |
MAX_INSTANCE_ID = DOLLAR_ID_BASE + 9; |
| 110 |
|
| 111 |
@Override |
| 112 |
protected int getMaxInstanceId() |
| 113 |
{ |
| 114 |
return super.getMaxInstanceId() + MAX_INSTANCE_ID; |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
protected int findInstanceIdInfo(String s) { |
| 119 |
int id; |
| 120 |
|
| 121 |
L0: { id = 0; String X = null; int c; |
| 122 |
L: switch (s.length()) { |
| 123 |
case 2: switch (s.charAt(1)) { |
| 124 |
case '&': if (s.charAt(0)=='$') {id=Id_AMPERSAND; break L0;} break L; |
| 125 |
case '\'': if (s.charAt(0)=='$') {id=Id_QUOTE; break L0;} break L; |
| 126 |
case '*': if (s.charAt(0)=='$') {id=Id_STAR; break L0;} break L; |
| 127 |
case '+': if (s.charAt(0)=='$') {id=Id_PLUS; break L0;} break L; |
| 128 |
case '1': if (s.charAt(0)=='$') {id=Id_DOLLAR_1; break L0;} break L; |
| 129 |
case '2': if (s.charAt(0)=='$') {id=Id_DOLLAR_2; break L0;} break L; |
| 130 |
case '3': if (s.charAt(0)=='$') {id=Id_DOLLAR_3; break L0;} break L; |
| 131 |
case '4': if (s.charAt(0)=='$') {id=Id_DOLLAR_4; break L0;} break L; |
| 132 |
case '5': if (s.charAt(0)=='$') {id=Id_DOLLAR_5; break L0;} break L; |
| 133 |
case '6': if (s.charAt(0)=='$') {id=Id_DOLLAR_6; break L0;} break L; |
| 134 |
case '7': if (s.charAt(0)=='$') {id=Id_DOLLAR_7; break L0;} break L; |
| 135 |
case '8': if (s.charAt(0)=='$') {id=Id_DOLLAR_8; break L0;} break L; |
| 136 |
case '9': if (s.charAt(0)=='$') {id=Id_DOLLAR_9; break L0;} break L; |
| 137 |
case '_': if (s.charAt(0)=='$') {id=Id_UNDERSCORE; break L0;} break L; |
| 138 |
case '`': if (s.charAt(0)=='$') {id=Id_BACK_QUOTE; break L0;} break L; |
| 139 |
} break L; |
| 140 |
case 5: X="input";id=Id_input; break L; |
| 141 |
case 9: c=s.charAt(4); |
| 142 |
if (c=='M') { X="lastMatch";id=Id_lastMatch; } |
| 143 |
else if (c=='P') { X="lastParen";id=Id_lastParen; } |
| 144 |
else if (c=='i') { X="multiline";id=Id_multiline; } |
| 145 |
break L; |
| 146 |
case 11: X="leftContext";id=Id_leftContext; break L; |
| 147 |
case 12: X="rightContext";id=Id_rightContext; break L; |
| 148 |
} |
| 149 |
if (X!=null && X!=s && !X.equals(s)) id = 0; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
if (id == 0) return super.findInstanceIdInfo(s); |
| 154 |
|
| 155 |
int attr; |
| 156 |
switch (id) { |
| 157 |
case Id_multiline: |
| 158 |
attr = multilineAttr; |
| 159 |
break; |
| 160 |
case Id_STAR: |
| 161 |
attr = starAttr; |
| 162 |
break; |
| 163 |
case Id_input: |
| 164 |
attr = inputAttr; |
| 165 |
break; |
| 166 |
case Id_UNDERSCORE: |
| 167 |
attr = underscoreAttr; |
| 168 |
break; |
| 169 |
default: |
| 170 |
attr = PERMANENT | READONLY; |
| 171 |
break; |
| 172 |
} |
| 173 |
|
| 174 |
return instanceIdInfo(attr, super.getMaxInstanceId() + id); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
@Override |
| 180 |
protected String getInstanceIdName(int id) |
| 181 |
{ |
| 182 |
int shifted = id - super.getMaxInstanceId(); |
| 183 |
if (1 <= shifted && shifted <= MAX_INSTANCE_ID) { |
| 184 |
switch (shifted) { |
| 185 |
case Id_multiline: return "multiline"; |
| 186 |
case Id_STAR: return "$*"; |
| 187 |
|
| 188 |
case Id_input: return "input"; |
| 189 |
case Id_UNDERSCORE: return "$_"; |
| 190 |
|
| 191 |
case Id_lastMatch: return "lastMatch"; |
| 192 |
case Id_AMPERSAND: return "$&"; |
| 193 |
|
| 194 |
case Id_lastParen: return "lastParen"; |
| 195 |
case Id_PLUS: return "$+"; |
| 196 |
|
| 197 |
case Id_leftContext: return "leftContext"; |
| 198 |
case Id_BACK_QUOTE: return "$`"; |
| 199 |
|
| 200 |
case Id_rightContext: return "rightContext"; |
| 201 |
case Id_QUOTE: return "$'"; |
| 202 |
} |
| 203 |
|
| 204 |
int substring_number = shifted - DOLLAR_ID_BASE - 1; |
| 205 |
char[] buf = { '$', (char)('1' + substring_number) }; |
| 206 |
return new String(buf); |
| 207 |
} |
| 208 |
return super.getInstanceIdName(id); |
| 209 |
} |
| 210 |
|
| 211 |
@Override |
| 212 |
protected Object getInstanceIdValue(int id) |
| 213 |
{ |
| 214 |
int shifted = id - super.getMaxInstanceId(); |
| 215 |
if (1 <= shifted && shifted <= MAX_INSTANCE_ID) { |
| 216 |
RegExpImpl impl = getImpl(); |
| 217 |
Object stringResult; |
| 218 |
switch (shifted) { |
| 219 |
case Id_multiline: |
| 220 |
case Id_STAR: |
| 221 |
return ScriptRuntime.wrapBoolean(impl.multiline); |
| 222 |
|
| 223 |
case Id_input: |
| 224 |
case Id_UNDERSCORE: |
| 225 |
stringResult = impl.input; |
| 226 |
break; |
| 227 |
|
| 228 |
case Id_lastMatch: |
| 229 |
case Id_AMPERSAND: |
| 230 |
stringResult = impl.lastMatch; |
| 231 |
break; |
| 232 |
|
| 233 |
case Id_lastParen: |
| 234 |
case Id_PLUS: |
| 235 |
stringResult = impl.lastParen; |
| 236 |
break; |
| 237 |
|
| 238 |
case Id_leftContext: |
| 239 |
case Id_BACK_QUOTE: |
| 240 |
stringResult = impl.leftContext; |
| 241 |
break; |
| 242 |
|
| 243 |
case Id_rightContext: |
| 244 |
case Id_QUOTE: |
| 245 |
stringResult = impl.rightContext; |
| 246 |
break; |
| 247 |
|
| 248 |
default: |
| 249 |
{ |
| 250 |
|
| 251 |
int substring_number = shifted - DOLLAR_ID_BASE - 1; |
| 252 |
stringResult = impl.getParenSubString(substring_number); |
| 253 |
break; |
| 254 |
} |
| 255 |
} |
| 256 |
return (stringResult == null) ? "" : stringResult.toString(); |
| 257 |
} |
| 258 |
return super.getInstanceIdValue(id); |
| 259 |
} |
| 260 |
|
| 261 |
@Override |
| 262 |
protected void setInstanceIdValue(int id, Object value) |
| 263 |
{ |
| 264 |
int shifted = id - super.getMaxInstanceId(); |
| 265 |
switch (shifted) { |
| 266 |
case Id_multiline: |
| 267 |
case Id_STAR: |
| 268 |
getImpl().multiline = ScriptRuntime.toBoolean(value); |
| 269 |
return; |
| 270 |
|
| 271 |
case Id_input: |
| 272 |
case Id_UNDERSCORE: |
| 273 |
getImpl().input = ScriptRuntime.toString(value); |
| 274 |
return; |
| 275 |
|
| 276 |
case Id_lastMatch: |
| 277 |
case Id_AMPERSAND: |
| 278 |
case Id_lastParen: |
| 279 |
case Id_PLUS: |
| 280 |
case Id_leftContext: |
| 281 |
case Id_BACK_QUOTE: |
| 282 |
case Id_rightContext: |
| 283 |
case Id_QUOTE: |
| 284 |
return; |
| 285 |
default: |
| 286 |
int substring_number = shifted - DOLLAR_ID_BASE - 1; |
| 287 |
if (0 <= substring_number && substring_number <= 8) { |
| 288 |
return; |
| 289 |
} |
| 290 |
} |
| 291 |
super.setInstanceIdValue(id, value); |
| 292 |
} |
| 293 |
|
| 294 |
@Override |
| 295 |
protected void setInstanceIdAttributes(int id, int attr) { |
| 296 |
int shifted = id - super.getMaxInstanceId(); |
| 297 |
switch (shifted) { |
| 298 |
case Id_multiline: |
| 299 |
multilineAttr = attr; |
| 300 |
return; |
| 301 |
case Id_STAR: |
| 302 |
starAttr = attr; |
| 303 |
return; |
| 304 |
case Id_input: |
| 305 |
inputAttr = attr; |
| 306 |
return; |
| 307 |
case Id_UNDERSCORE: |
| 308 |
underscoreAttr = attr; |
| 309 |
return; |
| 310 |
|
| 311 |
case Id_lastMatch: |
| 312 |
case Id_AMPERSAND: |
| 313 |
case Id_lastParen: |
| 314 |
case Id_PLUS: |
| 315 |
case Id_leftContext: |
| 316 |
case Id_BACK_QUOTE: |
| 317 |
case Id_rightContext: |
| 318 |
case Id_QUOTE: |
| 319 |
|
| 320 |
return; |
| 321 |
default: |
| 322 |
int substring_number = shifted - DOLLAR_ID_BASE - 1; |
| 323 |
if (0 <= substring_number && substring_number <= 8) { |
| 324 |
|
| 325 |
return; |
| 326 |
} |
| 327 |
} |
| 328 |
super.setInstanceIdAttributes(id, attr); |
| 329 |
} |
| 330 |
|
| 331 |
private int multilineAttr = PERMANENT; |
| 332 |
private int starAttr = PERMANENT; |
| 333 |
private int inputAttr = PERMANENT; |
| 334 |
private int underscoreAttr = PERMANENT; |
| 335 |
} |