| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
package com.google.javascript.rhino; |
| 48 |
|
| 49 |
import java.text.MessageFormat; |
| 50 |
import java.util.Locale; |
| 51 |
import java.util.ResourceBundle; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public class ScriptRuntime { |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
protected ScriptRuntime() { |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
public static boolean isJSLineTerminator(int c) { |
| 68 |
|
| 69 |
|
| 70 |
if ((c & 0xDFD0) != 0) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
return c == '\n' || c == '\r' || c == 0x2028 || c == 0x2029; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public static final double |
| 81 |
NaN = Double.longBitsToDouble(0x7ff8000000000000L); |
| 82 |
|
| 83 |
|
| 84 |
public static final double |
| 85 |
negativeZero = Double.longBitsToDouble(0x8000000000000000L); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
@SuppressWarnings("fallthrough") |
| 91 |
static double stringToNumber(String s, int start, int radix) { |
| 92 |
char digitMax = '9'; |
| 93 |
char lowerCaseBound = 'a'; |
| 94 |
char upperCaseBound = 'A'; |
| 95 |
int len = s.length(); |
| 96 |
if (radix < 10) { |
| 97 |
digitMax = (char) ('0' + radix - 1); |
| 98 |
} |
| 99 |
if (radix > 10) { |
| 100 |
lowerCaseBound = (char) ('a' + radix - 10); |
| 101 |
upperCaseBound = (char) ('A' + radix - 10); |
| 102 |
} |
| 103 |
int end; |
| 104 |
double sum = 0.0; |
| 105 |
for (end=start; end < len; end++) { |
| 106 |
char c = s.charAt(end); |
| 107 |
int newDigit; |
| 108 |
if ('0' <= c && c <= digitMax) |
| 109 |
newDigit = c - '0'; |
| 110 |
else if ('a' <= c && c < lowerCaseBound) |
| 111 |
newDigit = c - 'a' + 10; |
| 112 |
else if ('A' <= c && c < upperCaseBound) |
| 113 |
newDigit = c - 'A' + 10; |
| 114 |
else |
| 115 |
break; |
| 116 |
sum = sum*radix + newDigit; |
| 117 |
} |
| 118 |
if (start == end) { |
| 119 |
return NaN; |
| 120 |
} |
| 121 |
if (sum >= 9007199254740992.0) { |
| 122 |
if (radix == 10) { |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
try { |
| 129 |
return Double.valueOf(s.substring(start, end)).doubleValue(); |
| 130 |
} catch (NumberFormatException nfe) { |
| 131 |
return NaN; |
| 132 |
} |
| 133 |
} else if (radix == 2 || radix == 4 || radix == 8 || |
| 134 |
radix == 16 || radix == 32) { |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
int bitShiftInChar = 1; |
| 146 |
int digit = 0; |
| 147 |
|
| 148 |
final int SKIP_LEADING_ZEROS = 0; |
| 149 |
final int FIRST_EXACT_53_BITS = 1; |
| 150 |
final int AFTER_BIT_53 = 2; |
| 151 |
final int ZEROS_AFTER_54 = 3; |
| 152 |
final int MIXED_AFTER_54 = 4; |
| 153 |
|
| 154 |
int state = SKIP_LEADING_ZEROS; |
| 155 |
int exactBitsLimit = 53; |
| 156 |
double factor = 0.0; |
| 157 |
boolean bit53 = false; |
| 158 |
|
| 159 |
boolean bit54 = false; |
| 160 |
|
| 161 |
for (;;) { |
| 162 |
if (bitShiftInChar == 1) { |
| 163 |
if (start == end) |
| 164 |
break; |
| 165 |
digit = s.charAt(start++); |
| 166 |
if ('0' <= digit && digit <= '9') |
| 167 |
digit -= '0'; |
| 168 |
else if ('a' <= digit && digit <= 'z') |
| 169 |
digit -= 'a' - 10; |
| 170 |
else |
| 171 |
digit -= 'A' - 10; |
| 172 |
bitShiftInChar = radix; |
| 173 |
} |
| 174 |
bitShiftInChar >>= 1; |
| 175 |
boolean bit = (digit & bitShiftInChar) != 0; |
| 176 |
|
| 177 |
switch (state) { |
| 178 |
case SKIP_LEADING_ZEROS: |
| 179 |
if (bit) { |
| 180 |
--exactBitsLimit; |
| 181 |
sum = 1.0; |
| 182 |
state = FIRST_EXACT_53_BITS; |
| 183 |
} |
| 184 |
break; |
| 185 |
case FIRST_EXACT_53_BITS: |
| 186 |
sum *= 2.0; |
| 187 |
if (bit) |
| 188 |
sum += 1.0; |
| 189 |
--exactBitsLimit; |
| 190 |
if (exactBitsLimit == 0) { |
| 191 |
bit53 = bit; |
| 192 |
state = AFTER_BIT_53; |
| 193 |
} |
| 194 |
break; |
| 195 |
case AFTER_BIT_53: |
| 196 |
bit54 = bit; |
| 197 |
factor = 2.0; |
| 198 |
state = ZEROS_AFTER_54; |
| 199 |
break; |
| 200 |
case ZEROS_AFTER_54: |
| 201 |
if (bit) { |
| 202 |
state = MIXED_AFTER_54; |
| 203 |
} |
| 204 |
|
| 205 |
case MIXED_AFTER_54: |
| 206 |
factor *= 2; |
| 207 |
break; |
| 208 |
} |
| 209 |
} |
| 210 |
switch (state) { |
| 211 |
case SKIP_LEADING_ZEROS: |
| 212 |
sum = 0.0; |
| 213 |
break; |
| 214 |
case FIRST_EXACT_53_BITS: |
| 215 |
case AFTER_BIT_53: |
| 216 |
|
| 217 |
break; |
| 218 |
case ZEROS_AFTER_54: |
| 219 |
|
| 220 |
|
| 221 |
if (bit54 & bit53) |
| 222 |
sum += 1.0; |
| 223 |
sum *= factor; |
| 224 |
break; |
| 225 |
case MIXED_AFTER_54: |
| 226 |
|
| 227 |
|
| 228 |
if (bit54) |
| 229 |
sum += 1.0; |
| 230 |
sum *= factor; |
| 231 |
break; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
} |
| 236 |
return sum; |
| 237 |
} |
| 238 |
|
| 239 |
public static String escapeString(String s) { |
| 240 |
return escapeString(s, '"'); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public static String escapeString(String s, char escapeQuote) { |
| 248 |
if (!(escapeQuote == '"' || escapeQuote == '\'')) { |
| 249 |
throw new IllegalStateException("unexpected quote char:" + escapeQuote); |
| 250 |
} |
| 251 |
StringBuffer sb = null; |
| 252 |
|
| 253 |
for(int i = 0, L = s.length(); i != L; ++i) { |
| 254 |
int c = s.charAt(i); |
| 255 |
|
| 256 |
if (' ' <= c && c <= '~' && c != escapeQuote && c != '\\') { |
| 257 |
|
| 258 |
|
| 259 |
if (sb != null) { |
| 260 |
sb.append((char)c); |
| 261 |
} |
| 262 |
continue; |
| 263 |
} |
| 264 |
if (sb == null) { |
| 265 |
sb = new StringBuffer(L + 3); |
| 266 |
sb.append(s); |
| 267 |
sb.setLength(i); |
| 268 |
} |
| 269 |
|
| 270 |
int escape = -1; |
| 271 |
switch (c) { |
| 272 |
case '\b': escape = 'b'; break; |
| 273 |
case '\f': escape = 'f'; break; |
| 274 |
case '\n': escape = 'n'; break; |
| 275 |
case '\r': escape = 'r'; break; |
| 276 |
case '\t': escape = 't'; break; |
| 277 |
case 0xb: escape = 'v'; break; |
| 278 |
case ' ': escape = ' '; break; |
| 279 |
case '\\': escape = '\\'; break; |
| 280 |
} |
| 281 |
if (escape >= 0) { |
| 282 |
|
| 283 |
sb.append('\\'); |
| 284 |
sb.append((char)escape); |
| 285 |
} else if (c == escapeQuote) { |
| 286 |
sb.append('\\'); |
| 287 |
sb.append(escapeQuote); |
| 288 |
} else { |
| 289 |
int hexSize; |
| 290 |
if (c < 256) { |
| 291 |
|
| 292 |
sb.append("\\x"); |
| 293 |
hexSize = 2; |
| 294 |
} else { |
| 295 |
|
| 296 |
sb.append("\\u"); |
| 297 |
hexSize = 4; |
| 298 |
} |
| 299 |
|
| 300 |
for (int shift = (hexSize - 1) * 4; shift >= 0; shift -= 4) { |
| 301 |
int digit = 0xf & (c >> shift); |
| 302 |
int hc = (digit < 10) ? '0' + digit : 'a' - 10 + digit; |
| 303 |
sb.append((char)hc); |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
return (sb == null) ? s : sb.toString(); |
| 308 |
} |
| 309 |
|
| 310 |
static boolean isValidIdentifierName(String s) { |
| 311 |
int L = s.length(); |
| 312 |
if (L == 0) |
| 313 |
return false; |
| 314 |
if (!Character.isJavaIdentifierStart(s.charAt(0))) |
| 315 |
return false; |
| 316 |
for (int i = 1; i != L; ++i) { |
| 317 |
if (!Character.isJavaIdentifierPart(s.charAt(i))) |
| 318 |
return false; |
| 319 |
} |
| 320 |
return !TokenStream.isKeyword(s); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
public static long testUint32String(String str) { |
| 328 |
|
| 329 |
|
| 330 |
final int MAX_VALUE_LENGTH = 10; |
| 331 |
|
| 332 |
int len = str.length(); |
| 333 |
if (1 <= len && len <= MAX_VALUE_LENGTH) { |
| 334 |
int c = str.charAt(0); |
| 335 |
c -= '0'; |
| 336 |
if (c == 0) { |
| 337 |
|
| 338 |
return (len == 1) ? 0L : -1L; |
| 339 |
} |
| 340 |
if (1 <= c && c <= 9) { |
| 341 |
long v = c; |
| 342 |
for (int i = 1; i != len; ++i) { |
| 343 |
c = str.charAt(i) - '0'; |
| 344 |
if (!(0 <= c && c <= 9)) { |
| 345 |
return -1; |
| 346 |
} |
| 347 |
v = 10 * v + c; |
| 348 |
} |
| 349 |
|
| 350 |
if ((v >>> 32) == 0) { |
| 351 |
return v; |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
return -1; |
| 356 |
} |
| 357 |
|
| 358 |
static boolean isSpecialProperty(String s) { |
| 359 |
return s.equals("__proto__") || s.equals("__parent__"); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
public static String getMessage0(String messageId) { |
| 367 |
return getMessage(messageId, null); |
| 368 |
} |
| 369 |
|
| 370 |
public static String getMessage1(String messageId, Object arg1) { |
| 371 |
Object[] arguments = {arg1}; |
| 372 |
return getMessage(messageId, arguments); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
public static String getMessage(String messageId, Object[] arguments) { |
| 380 |
final String defaultResource |
| 381 |
= "rhino_ast.java.com.google.javascript.rhino.Messages"; |
| 382 |
|
| 383 |
Locale locale = Locale.getDefault(); |
| 384 |
|
| 385 |
|
| 386 |
ResourceBundle rb = ResourceBundle.getBundle(defaultResource, locale); |
| 387 |
|
| 388 |
String formatString; |
| 389 |
try { |
| 390 |
formatString = rb.getString(messageId); |
| 391 |
} catch (java.util.MissingResourceException mre) { |
| 392 |
throw new RuntimeException |
| 393 |
("no message resource found for message property "+ messageId); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
MessageFormat formatter = new MessageFormat(formatString); |
| 403 |
return formatter.format(arguments); |
| 404 |
} |
| 405 |
} |