| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
import java.io.Serializable; |
| 10 |
|
| 11 |
import org.mozilla.javascript.xml.XMLLib; |
| 12 |
import static org.mozilla.javascript.ScriptableObject.DONTENUM; |
| 13 |
import static org.mozilla.javascript.ScriptableObject.READONLY; |
| 14 |
import static org.mozilla.javascript.ScriptableObject.PERMANENT; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class NativeGlobal implements Serializable, IdFunctionCall |
| 26 |
{ |
| 27 |
static final long serialVersionUID = 6080442165748707530L; |
| 28 |
|
| 29 |
public static void init(Context cx, Scriptable scope, boolean sealed) { |
| 30 |
NativeGlobal obj = new NativeGlobal(); |
| 31 |
|
| 32 |
for (int id = 1; id <= LAST_SCOPE_FUNCTION_ID; ++id) { |
| 33 |
String name; |
| 34 |
int arity = 1; |
| 35 |
switch (id) { |
| 36 |
case Id_decodeURI: |
| 37 |
name = "decodeURI"; |
| 38 |
break; |
| 39 |
case Id_decodeURIComponent: |
| 40 |
name = "decodeURIComponent"; |
| 41 |
break; |
| 42 |
case Id_encodeURI: |
| 43 |
name = "encodeURI"; |
| 44 |
break; |
| 45 |
case Id_encodeURIComponent: |
| 46 |
name = "encodeURIComponent"; |
| 47 |
break; |
| 48 |
case Id_escape: |
| 49 |
name = "escape"; |
| 50 |
break; |
| 51 |
case Id_eval: |
| 52 |
name = "eval"; |
| 53 |
break; |
| 54 |
case Id_isFinite: |
| 55 |
name = "isFinite"; |
| 56 |
break; |
| 57 |
case Id_isNaN: |
| 58 |
name = "isNaN"; |
| 59 |
break; |
| 60 |
case Id_isXMLName: |
| 61 |
name = "isXMLName"; |
| 62 |
break; |
| 63 |
case Id_parseFloat: |
| 64 |
name = "parseFloat"; |
| 65 |
break; |
| 66 |
case Id_parseInt: |
| 67 |
name = "parseInt"; |
| 68 |
arity = 2; |
| 69 |
break; |
| 70 |
case Id_unescape: |
| 71 |
name = "unescape"; |
| 72 |
break; |
| 73 |
case Id_uneval: |
| 74 |
name = "uneval"; |
| 75 |
break; |
| 76 |
default: |
| 77 |
throw Kit.codeBug(); |
| 78 |
} |
| 79 |
IdFunctionObject f = new IdFunctionObject(obj, FTAG, id, name, |
| 80 |
arity, scope); |
| 81 |
if (sealed) { |
| 82 |
f.sealObject(); |
| 83 |
} |
| 84 |
f.exportAsScopeProperty(); |
| 85 |
} |
| 86 |
|
| 87 |
ScriptableObject.defineProperty( |
| 88 |
scope, "NaN", ScriptRuntime.NaNobj, |
| 89 |
READONLY|DONTENUM|PERMANENT); |
| 90 |
ScriptableObject.defineProperty( |
| 91 |
scope, "Infinity", |
| 92 |
ScriptRuntime.wrapNumber(Double.POSITIVE_INFINITY), |
| 93 |
READONLY|DONTENUM|PERMANENT); |
| 94 |
ScriptableObject.defineProperty( |
| 95 |
scope, "undefined", Undefined.instance, |
| 96 |
READONLY|DONTENUM|PERMANENT); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
for (TopLevel.NativeErrors error : TopLevel.NativeErrors.values()) { |
| 103 |
if (error == TopLevel.NativeErrors.Error) { |
| 104 |
|
| 105 |
continue; |
| 106 |
} |
| 107 |
String name = error.name(); |
| 108 |
ScriptableObject errorProto = |
| 109 |
(ScriptableObject) ScriptRuntime.newBuiltinObject(cx, scope, |
| 110 |
TopLevel.Builtins.Error, |
| 111 |
ScriptRuntime.emptyArgs); |
| 112 |
errorProto.put("name", errorProto, name); |
| 113 |
errorProto.put("message", errorProto, ""); |
| 114 |
IdFunctionObject ctor = new IdFunctionObject(obj, FTAG, |
| 115 |
Id_new_CommonError, |
| 116 |
name, 1, scope); |
| 117 |
ctor.markAsConstructor(errorProto); |
| 118 |
errorProto.put("constructor", errorProto, ctor); |
| 119 |
errorProto.setAttributes("constructor", ScriptableObject.DONTENUM); |
| 120 |
if (sealed) { |
| 121 |
errorProto.sealObject(); |
| 122 |
ctor.sealObject(); |
| 123 |
} |
| 124 |
ctor.exportAsScopeProperty(); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, |
| 129 |
Scriptable thisObj, Object[] args) |
| 130 |
{ |
| 131 |
if (f.hasTag(FTAG)) { |
| 132 |
int methodId = f.methodId(); |
| 133 |
switch (methodId) { |
| 134 |
case Id_decodeURI: |
| 135 |
case Id_decodeURIComponent: { |
| 136 |
String str = ScriptRuntime.toString(args, 0); |
| 137 |
return decode(str, methodId == Id_decodeURI); |
| 138 |
} |
| 139 |
|
| 140 |
case Id_encodeURI: |
| 141 |
case Id_encodeURIComponent: { |
| 142 |
String str = ScriptRuntime.toString(args, 0); |
| 143 |
return encode(str, methodId == Id_encodeURI); |
| 144 |
} |
| 145 |
|
| 146 |
case Id_escape: |
| 147 |
return js_escape(args); |
| 148 |
|
| 149 |
case Id_eval: |
| 150 |
return js_eval(cx, scope, args); |
| 151 |
|
| 152 |
case Id_isFinite: { |
| 153 |
if (args.length < 1) { |
| 154 |
return Boolean.FALSE; |
| 155 |
} |
| 156 |
return NativeNumber.isFinite(args[0]); |
| 157 |
} |
| 158 |
|
| 159 |
case Id_isNaN: { |
| 160 |
|
| 161 |
boolean result; |
| 162 |
if (args.length < 1) { |
| 163 |
result = true; |
| 164 |
} else { |
| 165 |
double d = ScriptRuntime.toNumber(args[0]); |
| 166 |
result = (d != d); |
| 167 |
} |
| 168 |
return ScriptRuntime.wrapBoolean(result); |
| 169 |
} |
| 170 |
|
| 171 |
case Id_isXMLName: { |
| 172 |
Object name = (args.length == 0) |
| 173 |
? Undefined.instance : args[0]; |
| 174 |
XMLLib xmlLib = XMLLib.extractFromScope(scope); |
| 175 |
return ScriptRuntime.wrapBoolean( |
| 176 |
xmlLib.isXMLName(cx, name)); |
| 177 |
} |
| 178 |
|
| 179 |
case Id_parseFloat: |
| 180 |
return js_parseFloat(args); |
| 181 |
|
| 182 |
case Id_parseInt: |
| 183 |
return js_parseInt(args); |
| 184 |
|
| 185 |
case Id_unescape: |
| 186 |
return js_unescape(args); |
| 187 |
|
| 188 |
case Id_uneval: { |
| 189 |
Object value = (args.length != 0) |
| 190 |
? args[0] : Undefined.instance; |
| 191 |
return ScriptRuntime.uneval(cx, scope, value); |
| 192 |
} |
| 193 |
|
| 194 |
case Id_new_CommonError: |
| 195 |
|
| 196 |
|
| 197 |
return NativeError.make(cx, scope, f, args); |
| 198 |
} |
| 199 |
} |
| 200 |
throw f.unknown(); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
static Object js_parseInt(Object[] args) { |
| 207 |
String s = ScriptRuntime.toString(args, 0); |
| 208 |
int radix = ScriptRuntime.toInt32(args, 1); |
| 209 |
|
| 210 |
int len = s.length(); |
| 211 |
if (len == 0) |
| 212 |
return ScriptRuntime.NaNobj; |
| 213 |
|
| 214 |
boolean negative = false; |
| 215 |
int start = 0; |
| 216 |
char c; |
| 217 |
do { |
| 218 |
c = s.charAt(start); |
| 219 |
if (!ScriptRuntime.isStrWhiteSpaceChar(c)) |
| 220 |
break; |
| 221 |
start++; |
| 222 |
} while (start < len); |
| 223 |
|
| 224 |
if (c == '+' || (negative = (c == '-'))) |
| 225 |
start++; |
| 226 |
|
| 227 |
final int NO_RADIX = -1; |
| 228 |
if (radix == 0) { |
| 229 |
radix = NO_RADIX; |
| 230 |
} else if (radix < 2 || radix > 36) { |
| 231 |
return ScriptRuntime.NaNobj; |
| 232 |
} else if (radix == 16 && len - start > 1 && s.charAt(start) == '0') { |
| 233 |
c = s.charAt(start+1); |
| 234 |
if (c == 'x' || c == 'X') |
| 235 |
start += 2; |
| 236 |
} |
| 237 |
|
| 238 |
if (radix == NO_RADIX) { |
| 239 |
radix = 10; |
| 240 |
if (len - start > 1 && s.charAt(start) == '0') { |
| 241 |
c = s.charAt(start+1); |
| 242 |
if (c == 'x' || c == 'X') { |
| 243 |
radix = 16; |
| 244 |
start += 2; |
| 245 |
} else if ('0' <= c && c <= '9') { |
| 246 |
radix = 8; |
| 247 |
start++; |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
double d = ScriptRuntime.stringToNumber(s, start, radix); |
| 253 |
return ScriptRuntime.wrapNumber(negative ? -d : d); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
static Object js_parseFloat(Object[] args) |
| 262 |
{ |
| 263 |
if (args.length < 1) |
| 264 |
return ScriptRuntime.NaNobj; |
| 265 |
|
| 266 |
String s = ScriptRuntime.toString(args[0]); |
| 267 |
int len = s.length(); |
| 268 |
int start = 0; |
| 269 |
|
| 270 |
char c; |
| 271 |
for (;;) { |
| 272 |
if (start == len) { |
| 273 |
return ScriptRuntime.NaNobj; |
| 274 |
} |
| 275 |
c = s.charAt(start); |
| 276 |
if (!ScriptRuntime.isStrWhiteSpaceChar(c)) { |
| 277 |
break; |
| 278 |
} |
| 279 |
++start; |
| 280 |
} |
| 281 |
|
| 282 |
int i = start; |
| 283 |
if (c == '+' || c == '-') { |
| 284 |
++i; |
| 285 |
if (i == len) { |
| 286 |
return ScriptRuntime.NaNobj; |
| 287 |
} |
| 288 |
c = s.charAt(i); |
| 289 |
} |
| 290 |
|
| 291 |
if (c == 'I') { |
| 292 |
|
| 293 |
if (i+8 <= len && s.regionMatches(i, "Infinity", 0, 8)) { |
| 294 |
double d; |
| 295 |
if (s.charAt(start) == '-') { |
| 296 |
d = Double.NEGATIVE_INFINITY; |
| 297 |
} else { |
| 298 |
d = Double.POSITIVE_INFINITY; |
| 299 |
} |
| 300 |
return ScriptRuntime.wrapNumber(d); |
| 301 |
} |
| 302 |
return ScriptRuntime.NaNobj; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
int decimal = -1; |
| 307 |
int exponent = -1; |
| 308 |
boolean exponentValid = false; |
| 309 |
for (; i < len; i++) { |
| 310 |
switch (s.charAt(i)) { |
| 311 |
case '.': |
| 312 |
if (decimal != -1) |
| 313 |
break; |
| 314 |
decimal = i; |
| 315 |
continue; |
| 316 |
|
| 317 |
case 'e': |
| 318 |
case 'E': |
| 319 |
if (exponent != -1) { |
| 320 |
break; |
| 321 |
} else if (i == len - 1) { |
| 322 |
break; |
| 323 |
} |
| 324 |
exponent = i; |
| 325 |
continue; |
| 326 |
|
| 327 |
case '+': |
| 328 |
case '-': |
| 329 |
|
| 330 |
if (exponent != i-1) { |
| 331 |
break; |
| 332 |
} else if (i == len - 1) { |
| 333 |
--i; |
| 334 |
break; |
| 335 |
} |
| 336 |
continue; |
| 337 |
|
| 338 |
case '0': case '1': case '2': case '3': case '4': |
| 339 |
case '5': case '6': case '7': case '8': case '9': |
| 340 |
if (exponent != -1) { |
| 341 |
exponentValid = true; |
| 342 |
} |
| 343 |
continue; |
| 344 |
|
| 345 |
default: |
| 346 |
break; |
| 347 |
} |
| 348 |
break; |
| 349 |
} |
| 350 |
if (exponent != -1 && !exponentValid) { |
| 351 |
i = exponent; |
| 352 |
} |
| 353 |
s = s.substring(start, i); |
| 354 |
try { |
| 355 |
return Double.valueOf(s); |
| 356 |
} |
| 357 |
catch (NumberFormatException ex) { |
| 358 |
return ScriptRuntime.NaNobj; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
private Object js_escape(Object[] args) { |
| 371 |
final int |
| 372 |
URL_XALPHAS = 1, |
| 373 |
URL_XPALPHAS = 2, |
| 374 |
URL_PATH = 4; |
| 375 |
|
| 376 |
String s = ScriptRuntime.toString(args, 0); |
| 377 |
|
| 378 |
int mask = URL_XALPHAS | URL_XPALPHAS | URL_PATH; |
| 379 |
if (args.length > 1) { |
| 380 |
double d = ScriptRuntime.toNumber(args[1]); |
| 381 |
if (d != d || ((mask = (int) d) != d) || |
| 382 |
0 != (mask & ~(URL_XALPHAS | URL_XPALPHAS | URL_PATH))) |
| 383 |
{ |
| 384 |
throw Context.reportRuntimeError0("msg.bad.esc.mask"); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
StringBuilder sb = null; |
| 389 |
for (int k = 0, L = s.length(); k != L; ++k) { |
| 390 |
int c = s.charAt(k); |
| 391 |
if (mask != 0 |
| 392 |
&& ((c >= '0' && c <= '9') |
| 393 |
|| (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
| 394 |
|| c == '@' || c == '*' || c == '_' || c == '-' || c == '.' |
| 395 |
|| (0 != (mask & URL_PATH) && (c == '/' || c == '+')))) |
| 396 |
{ |
| 397 |
if (sb != null) { |
| 398 |
sb.append((char)c); |
| 399 |
} |
| 400 |
} else { |
| 401 |
if (sb == null) { |
| 402 |
sb = new StringBuilder(L + 3); |
| 403 |
sb.append(s); |
| 404 |
sb.setLength(k); |
| 405 |
} |
| 406 |
|
| 407 |
int hexSize; |
| 408 |
if (c < 256) { |
| 409 |
if (c == ' ' && mask == URL_XPALPHAS) { |
| 410 |
sb.append('+'); |
| 411 |
continue; |
| 412 |
} |
| 413 |
sb.append('%'); |
| 414 |
hexSize = 2; |
| 415 |
} else { |
| 416 |
sb.append('%'); |
| 417 |
sb.append('u'); |
| 418 |
hexSize = 4; |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
for (int shift = (hexSize - 1) * 4; shift >= 0; shift -= 4) { |
| 423 |
int digit = 0xf & (c >> shift); |
| 424 |
int hc = (digit < 10) ? '0' + digit : 'A' - 10 + digit; |
| 425 |
sb.append((char)hc); |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
return (sb == null) ? s : sb.toString(); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
private Object js_unescape(Object[] args) |
| 438 |
{ |
| 439 |
String s = ScriptRuntime.toString(args, 0); |
| 440 |
int firstEscapePos = s.indexOf('%'); |
| 441 |
if (firstEscapePos >= 0) { |
| 442 |
int L = s.length(); |
| 443 |
char[] buf = s.toCharArray(); |
| 444 |
int destination = firstEscapePos; |
| 445 |
for (int k = firstEscapePos; k != L;) { |
| 446 |
char c = buf[k]; |
| 447 |
++k; |
| 448 |
if (c == '%' && k != L) { |
| 449 |
int end, start; |
| 450 |
if (buf[k] == 'u') { |
| 451 |
start = k + 1; |
| 452 |
end = k + 5; |
| 453 |
} else { |
| 454 |
start = k; |
| 455 |
end = k + 2; |
| 456 |
} |
| 457 |
if (end <= L) { |
| 458 |
int x = 0; |
| 459 |
for (int i = start; i != end; ++i) { |
| 460 |
x = Kit.xDigitToInt(buf[i], x); |
| 461 |
} |
| 462 |
if (x >= 0) { |
| 463 |
c = (char)x; |
| 464 |
k = end; |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
buf[destination] = c; |
| 469 |
++destination; |
| 470 |
} |
| 471 |
s = new String(buf, 0, destination); |
| 472 |
} |
| 473 |
return s; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
private Object js_eval(Context cx, Scriptable scope, Object[] args) |
| 481 |
{ |
| 482 |
Scriptable global = ScriptableObject.getTopLevelScope(scope); |
| 483 |
return ScriptRuntime.evalSpecial(cx, global, global, args, "eval code", 1); |
| 484 |
} |
| 485 |
|
| 486 |
static boolean isEvalFunction(Object functionObj) |
| 487 |
{ |
| 488 |
if (functionObj instanceof IdFunctionObject) { |
| 489 |
IdFunctionObject function = (IdFunctionObject)functionObj; |
| 490 |
if (function.hasTag(FTAG) && function.methodId() == Id_eval) { |
| 491 |
return true; |
| 492 |
} |
| 493 |
} |
| 494 |
return false; |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
@Deprecated |
| 502 |
public static EcmaError constructError(Context cx, |
| 503 |
String error, |
| 504 |
String message, |
| 505 |
Scriptable scope) |
| 506 |
{ |
| 507 |
return ScriptRuntime.constructError(error, message); |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
@Deprecated |
| 516 |
public static EcmaError constructError(Context cx, |
| 517 |
String error, |
| 518 |
String message, |
| 519 |
Scriptable scope, |
| 520 |
String sourceName, |
| 521 |
int lineNumber, |
| 522 |
int columnNumber, |
| 523 |
String lineSource) |
| 524 |
{ |
| 525 |
return ScriptRuntime.constructError(error, message, |
| 526 |
sourceName, lineNumber, |
| 527 |
lineSource, columnNumber); |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
private static String encode(String str, boolean fullUri) { |
| 538 |
byte[] utf8buf = null; |
| 539 |
StringBuilder sb = null; |
| 540 |
|
| 541 |
for (int k = 0, length = str.length(); k != length; ++k) { |
| 542 |
char C = str.charAt(k); |
| 543 |
if (encodeUnescaped(C, fullUri)) { |
| 544 |
if (sb != null) { |
| 545 |
sb.append(C); |
| 546 |
} |
| 547 |
} else { |
| 548 |
if (sb == null) { |
| 549 |
sb = new StringBuilder(length + 3); |
| 550 |
sb.append(str); |
| 551 |
sb.setLength(k); |
| 552 |
utf8buf = new byte[6]; |
| 553 |
} |
| 554 |
if (0xDC00 <= C && C <= 0xDFFF) { |
| 555 |
throw uriError(); |
| 556 |
} |
| 557 |
int V; |
| 558 |
if (C < 0xD800 || 0xDBFF < C) { |
| 559 |
V = C; |
| 560 |
} else { |
| 561 |
k++; |
| 562 |
if (k == length) { |
| 563 |
throw uriError(); |
| 564 |
} |
| 565 |
char C2 = str.charAt(k); |
| 566 |
if (!(0xDC00 <= C2 && C2 <= 0xDFFF)) { |
| 567 |
throw uriError(); |
| 568 |
} |
| 569 |
V = ((C - 0xD800) << 10) + (C2 - 0xDC00) + 0x10000; |
| 570 |
} |
| 571 |
int L = oneUcs4ToUtf8Char(utf8buf, V); |
| 572 |
for (int j = 0; j < L; j++) { |
| 573 |
int d = 0xff & utf8buf[j]; |
| 574 |
sb.append('%'); |
| 575 |
sb.append(toHexChar(d >>> 4)); |
| 576 |
sb.append(toHexChar(d & 0xf)); |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
return (sb == null) ? str : sb.toString(); |
| 581 |
} |
| 582 |
|
| 583 |
private static char toHexChar(int i) { |
| 584 |
if (i >> 4 != 0) Kit.codeBug(); |
| 585 |
return (char)((i < 10) ? i + '0' : i - 10 + 'A'); |
| 586 |
} |
| 587 |
|
| 588 |
private static int unHex(char c) { |
| 589 |
if ('A' <= c && c <= 'F') { |
| 590 |
return c - 'A' + 10; |
| 591 |
} else if ('a' <= c && c <= 'f') { |
| 592 |
return c - 'a' + 10; |
| 593 |
} else if ('0' <= c && c <= '9') { |
| 594 |
return c - '0'; |
| 595 |
} else { |
| 596 |
return -1; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
private static int unHex(char c1, char c2) { |
| 601 |
int i1 = unHex(c1); |
| 602 |
int i2 = unHex(c2); |
| 603 |
if (i1 >= 0 && i2 >= 0) { |
| 604 |
return (i1 << 4) | i2; |
| 605 |
} |
| 606 |
return -1; |
| 607 |
} |
| 608 |
|
| 609 |
private static String decode(String str, boolean fullUri) { |
| 610 |
char[] buf = null; |
| 611 |
int bufTop = 0; |
| 612 |
|
| 613 |
for (int k = 0, length = str.length(); k != length;) { |
| 614 |
char C = str.charAt(k); |
| 615 |
if (C != '%') { |
| 616 |
if (buf != null) { |
| 617 |
buf[bufTop++] = C; |
| 618 |
} |
| 619 |
++k; |
| 620 |
} else { |
| 621 |
if (buf == null) { |
| 622 |
|
| 623 |
|
| 624 |
buf = new char[length]; |
| 625 |
str.getChars(0, k, buf, 0); |
| 626 |
bufTop = k; |
| 627 |
} |
| 628 |
int start = k; |
| 629 |
if (k + 3 > length) |
| 630 |
throw uriError(); |
| 631 |
int B = unHex(str.charAt(k + 1), str.charAt(k + 2)); |
| 632 |
if (B < 0) throw uriError(); |
| 633 |
k += 3; |
| 634 |
if ((B & 0x80) == 0) { |
| 635 |
C = (char)B; |
| 636 |
} else { |
| 637 |
|
| 638 |
|
| 639 |
int utf8Tail, ucs4Char, minUcs4Char; |
| 640 |
if ((B & 0xC0) == 0x80) { |
| 641 |
|
| 642 |
throw uriError(); |
| 643 |
} else if ((B & 0x20) == 0) { |
| 644 |
utf8Tail = 1; ucs4Char = B & 0x1F; |
| 645 |
minUcs4Char = 0x80; |
| 646 |
} else if ((B & 0x10) == 0) { |
| 647 |
utf8Tail = 2; ucs4Char = B & 0x0F; |
| 648 |
minUcs4Char = 0x800; |
| 649 |
} else if ((B & 0x08) == 0) { |
| 650 |
utf8Tail = 3; ucs4Char = B & 0x07; |
| 651 |
minUcs4Char = 0x10000; |
| 652 |
} else if ((B & 0x04) == 0) { |
| 653 |
utf8Tail = 4; ucs4Char = B & 0x03; |
| 654 |
minUcs4Char = 0x200000; |
| 655 |
} else if ((B & 0x02) == 0) { |
| 656 |
utf8Tail = 5; ucs4Char = B & 0x01; |
| 657 |
minUcs4Char = 0x4000000; |
| 658 |
} else { |
| 659 |
|
| 660 |
throw uriError(); |
| 661 |
} |
| 662 |
if (k + 3 * utf8Tail > length) |
| 663 |
throw uriError(); |
| 664 |
for (int j = 0; j != utf8Tail; j++) { |
| 665 |
if (str.charAt(k) != '%') |
| 666 |
throw uriError(); |
| 667 |
B = unHex(str.charAt(k + 1), str.charAt(k + 2)); |
| 668 |
if (B < 0 || (B & 0xC0) != 0x80) |
| 669 |
throw uriError(); |
| 670 |
ucs4Char = (ucs4Char << 6) | (B & 0x3F); |
| 671 |
k += 3; |
| 672 |
} |
| 673 |
|
| 674 |
if (ucs4Char < minUcs4Char |
| 675 |
|| (ucs4Char >= 0xD800 && ucs4Char <= 0xDFFF)) { |
| 676 |
ucs4Char = INVALID_UTF8; |
| 677 |
} else if (ucs4Char == 0xFFFE || ucs4Char == 0xFFFF) { |
| 678 |
ucs4Char = 0xFFFD; |
| 679 |
} |
| 680 |
if (ucs4Char >= 0x10000) { |
| 681 |
ucs4Char -= 0x10000; |
| 682 |
if (ucs4Char > 0xFFFFF) { |
| 683 |
throw uriError(); |
| 684 |
} |
| 685 |
char H = (char)((ucs4Char >>> 10) + 0xD800); |
| 686 |
C = (char)((ucs4Char & 0x3FF) + 0xDC00); |
| 687 |
buf[bufTop++] = H; |
| 688 |
} else { |
| 689 |
C = (char)ucs4Char; |
| 690 |
} |
| 691 |
} |
| 692 |
if (fullUri && URI_DECODE_RESERVED.indexOf(C) >= 0) { |
| 693 |
for (int x = start; x != k; x++) { |
| 694 |
buf[bufTop++] = str.charAt(x); |
| 695 |
} |
| 696 |
} else { |
| 697 |
buf[bufTop++] = C; |
| 698 |
} |
| 699 |
} |
| 700 |
} |
| 701 |
return (buf == null) ? str : new String(buf, 0, bufTop); |
| 702 |
} |
| 703 |
|
| 704 |
private static boolean encodeUnescaped(char c, boolean fullUri) { |
| 705 |
if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') |
| 706 |
|| ('0' <= c && c <= '9')) { |
| 707 |
return true; |
| 708 |
} |
| 709 |
if ("-_.!~*'()".indexOf(c) >= 0) { |
| 710 |
return true; |
| 711 |
} |
| 712 |
if (fullUri) { |
| 713 |
return URI_DECODE_RESERVED.indexOf(c) >= 0; |
| 714 |
} |
| 715 |
return false; |
| 716 |
} |
| 717 |
|
| 718 |
private static EcmaError uriError() { |
| 719 |
return ScriptRuntime.constructError("URIError", |
| 720 |
ScriptRuntime.getMessage0("msg.bad.uri")); |
| 721 |
} |
| 722 |
|
| 723 |
private static final String URI_DECODE_RESERVED = ";/?:@&=+$,#"; |
| 724 |
private static final int INVALID_UTF8 = Integer.MAX_VALUE; |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
private static int oneUcs4ToUtf8Char(byte[] utf8Buffer, int ucs4Char) { |
| 730 |
int utf8Length = 1; |
| 731 |
|
| 732 |
|
| 733 |
if ((ucs4Char & ~0x7F) == 0) |
| 734 |
utf8Buffer[0] = (byte)ucs4Char; |
| 735 |
else { |
| 736 |
int i; |
| 737 |
int a = ucs4Char >>> 11; |
| 738 |
utf8Length = 2; |
| 739 |
while (a != 0) { |
| 740 |
a >>>= 5; |
| 741 |
utf8Length++; |
| 742 |
} |
| 743 |
i = utf8Length; |
| 744 |
while (--i > 0) { |
| 745 |
utf8Buffer[i] = (byte)((ucs4Char & 0x3F) | 0x80); |
| 746 |
ucs4Char >>>= 6; |
| 747 |
} |
| 748 |
utf8Buffer[0] = (byte)(0x100 - (1 << (8-utf8Length)) + ucs4Char); |
| 749 |
} |
| 750 |
return utf8Length; |
| 751 |
} |
| 752 |
|
| 753 |
private static final Object FTAG = "Global"; |
| 754 |
|
| 755 |
private static final int |
| 756 |
Id_decodeURI = 1, |
| 757 |
Id_decodeURIComponent = 2, |
| 758 |
Id_encodeURI = 3, |
| 759 |
Id_encodeURIComponent = 4, |
| 760 |
Id_escape = 5, |
| 761 |
Id_eval = 6, |
| 762 |
Id_isFinite = 7, |
| 763 |
Id_isNaN = 8, |
| 764 |
Id_isXMLName = 9, |
| 765 |
Id_parseFloat = 10, |
| 766 |
Id_parseInt = 11, |
| 767 |
Id_unescape = 12, |
| 768 |
Id_uneval = 13, |
| 769 |
|
| 770 |
LAST_SCOPE_FUNCTION_ID = 13, |
| 771 |
|
| 772 |
Id_new_CommonError = 14; |
| 773 |
} |