| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
import java.io.Serializable; |
| 10 |
import java.lang.reflect.Constructor; |
| 11 |
import java.text.MessageFormat; |
| 12 |
import java.util.Locale; |
| 13 |
import java.util.ResourceBundle; |
| 14 |
|
| 15 |
import org.mozilla.javascript.ast.FunctionNode; |
| 16 |
import org.mozilla.javascript.v8dtoa.DoubleConversion; |
| 17 |
import org.mozilla.javascript.v8dtoa.FastDtoa; |
| 18 |
import org.mozilla.javascript.xml.XMLObject; |
| 19 |
import org.mozilla.javascript.xml.XMLLib; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class ScriptRuntime { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
protected ScriptRuntime() { |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
@Deprecated |
| 43 |
public static BaseFunction typeErrorThrower() { |
| 44 |
return typeErrorThrower(Context.getCurrentContext()); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public static BaseFunction typeErrorThrower(Context cx) { |
| 52 |
if (cx.typeErrorThrower == null) { |
| 53 |
BaseFunction thrower = new BaseFunction() { |
| 54 |
static final long serialVersionUID = -5891740962154902286L; |
| 55 |
|
| 56 |
@Override |
| 57 |
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { |
| 58 |
throw typeError0("msg.op.not.allowed"); |
| 59 |
} |
| 60 |
@Override |
| 61 |
public int getLength() { |
| 62 |
return 0; |
| 63 |
} |
| 64 |
}; |
| 65 |
ScriptRuntime.setFunctionProtoAndParent(thrower, cx.topCallScope); |
| 66 |
thrower.preventExtensions(); |
| 67 |
cx.typeErrorThrower = thrower; |
| 68 |
} |
| 69 |
return cx.typeErrorThrower; |
| 70 |
} |
| 71 |
|
| 72 |
static class NoSuchMethodShim implements Callable { |
| 73 |
String methodName; |
| 74 |
Callable noSuchMethodMethod; |
| 75 |
|
| 76 |
NoSuchMethodShim(Callable noSuchMethodMethod, String methodName) |
| 77 |
{ |
| 78 |
this.noSuchMethodMethod = noSuchMethodMethod; |
| 79 |
this.methodName = methodName; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public Object call(Context cx, Scriptable scope, Scriptable thisObj, |
| 91 |
Object[] args) |
| 92 |
{ |
| 93 |
Object[] nestedArgs = new Object[2]; |
| 94 |
|
| 95 |
nestedArgs[0] = methodName; |
| 96 |
nestedArgs[1] = newArrayLiteral(args, null, cx, scope); |
| 97 |
return noSuchMethodMethod.call(cx, scope, thisObj, nestedArgs); |
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public final static Class<?> |
| 112 |
BooleanClass = Kit.classOrNull("java.lang.Boolean"), |
| 113 |
ByteClass = Kit.classOrNull("java.lang.Byte"), |
| 114 |
CharacterClass = Kit.classOrNull("java.lang.Character"), |
| 115 |
ClassClass = Kit.classOrNull("java.lang.Class"), |
| 116 |
DoubleClass = Kit.classOrNull("java.lang.Double"), |
| 117 |
FloatClass = Kit.classOrNull("java.lang.Float"), |
| 118 |
IntegerClass = Kit.classOrNull("java.lang.Integer"), |
| 119 |
LongClass = Kit.classOrNull("java.lang.Long"), |
| 120 |
NumberClass = Kit.classOrNull("java.lang.Number"), |
| 121 |
ObjectClass = Kit.classOrNull("java.lang.Object"), |
| 122 |
ShortClass = Kit.classOrNull("java.lang.Short"), |
| 123 |
StringClass = Kit.classOrNull("java.lang.String"), |
| 124 |
DateClass = Kit.classOrNull("java.util.Date"); |
| 125 |
|
| 126 |
public final static Class<?> |
| 127 |
ContextClass |
| 128 |
= Kit.classOrNull("org.mozilla.javascript.Context"), |
| 129 |
ContextFactoryClass |
| 130 |
= Kit.classOrNull("org.mozilla.javascript.ContextFactory"), |
| 131 |
FunctionClass |
| 132 |
= Kit.classOrNull("org.mozilla.javascript.Function"), |
| 133 |
ScriptableObjectClass |
| 134 |
= Kit.classOrNull("org.mozilla.javascript.ScriptableObject"); |
| 135 |
public static final Class<Scriptable> ScriptableClass = |
| 136 |
Scriptable.class; |
| 137 |
|
| 138 |
|
| 139 |
public static Locale ROOT_LOCALE = new Locale(""); |
| 140 |
|
| 141 |
private static final Object LIBRARY_SCOPE_KEY = "LIBRARY_SCOPE"; |
| 142 |
|
| 143 |
public static boolean isRhinoRuntimeType(Class<?> cl) |
| 144 |
{ |
| 145 |
if (cl.isPrimitive()) { |
| 146 |
return (cl != Character.TYPE); |
| 147 |
} else { |
| 148 |
return (cl == StringClass || cl == BooleanClass |
| 149 |
|| NumberClass.isAssignableFrom(cl) |
| 150 |
|| ScriptableClass.isAssignableFrom(cl)); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
public static ScriptableObject initSafeStandardObjects(Context cx, |
| 155 |
ScriptableObject scope, |
| 156 |
boolean sealed) |
| 157 |
{ |
| 158 |
if (scope == null) { |
| 159 |
scope = new NativeObject(); |
| 160 |
} |
| 161 |
scope.associateValue(LIBRARY_SCOPE_KEY, scope); |
| 162 |
(new ClassCache()).associate(scope); |
| 163 |
|
| 164 |
BaseFunction.init(scope, sealed); |
| 165 |
NativeObject.init(scope, sealed); |
| 166 |
|
| 167 |
Scriptable objectProto = ScriptableObject.getObjectPrototype(scope); |
| 168 |
|
| 169 |
|
| 170 |
Scriptable functionProto = ScriptableObject.getClassPrototype(scope, "Function"); |
| 171 |
functionProto.setPrototype(objectProto); |
| 172 |
|
| 173 |
|
| 174 |
if (scope.getPrototype() == null) |
| 175 |
scope.setPrototype(objectProto); |
| 176 |
|
| 177 |
|
| 178 |
NativeError.init(scope, sealed); |
| 179 |
NativeGlobal.init(cx, scope, sealed); |
| 180 |
|
| 181 |
NativeArray.init(scope, sealed); |
| 182 |
if (cx.getOptimizationLevel() > 0) { |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
NativeArray.setMaximumInitialCapacity(200000); |
| 187 |
} |
| 188 |
NativeString.init(scope, sealed); |
| 189 |
NativeBoolean.init(scope, sealed); |
| 190 |
NativeNumber.init(scope, sealed); |
| 191 |
NativeDate.init(scope, sealed); |
| 192 |
NativeMath.init(scope, sealed); |
| 193 |
NativeJSON.init(scope, sealed); |
| 194 |
|
| 195 |
NativeWith.init(scope, sealed); |
| 196 |
NativeCall.init(scope, sealed); |
| 197 |
NativeScript.init(scope, sealed); |
| 198 |
|
| 199 |
NativeIterator.init(scope, sealed); |
| 200 |
|
| 201 |
NativeArrayIterator.init(scope, sealed); |
| 202 |
NativeStringIterator.init(scope, sealed); |
| 203 |
|
| 204 |
boolean withXml = cx.hasFeature(Context.FEATURE_E4X) && |
| 205 |
cx.getE4xImplementationFactory() != null; |
| 206 |
|
| 207 |
|
| 208 |
new LazilyLoadedCtor(scope, "RegExp", |
| 209 |
"org.mozilla.javascript.regexp.NativeRegExp", sealed, true); |
| 210 |
new LazilyLoadedCtor(scope, "Continuation", |
| 211 |
"org.mozilla.javascript.NativeContinuation", sealed, true); |
| 212 |
|
| 213 |
if (withXml) { |
| 214 |
String xmlImpl = cx.getE4xImplementationFactory().getImplementationClassName(); |
| 215 |
new LazilyLoadedCtor(scope, "XML", xmlImpl, sealed, true); |
| 216 |
new LazilyLoadedCtor(scope, "XMLList", xmlImpl, sealed, true); |
| 217 |
new LazilyLoadedCtor(scope, "Namespace", xmlImpl, sealed, true); |
| 218 |
new LazilyLoadedCtor(scope, "QName", xmlImpl, sealed, true); |
| 219 |
} |
| 220 |
|
| 221 |
if (((cx.getLanguageVersion() >= Context.VERSION_1_8) && |
| 222 |
cx.hasFeature(Context.FEATURE_V8_EXTENSIONS)) || |
| 223 |
(cx.getLanguageVersion() >= Context.VERSION_ES6)) |
| 224 |
{ |
| 225 |
new LazilyLoadedCtor(scope, "ArrayBuffer", |
| 226 |
"org.mozilla.javascript.typedarrays.NativeArrayBuffer", |
| 227 |
sealed, true); |
| 228 |
new LazilyLoadedCtor(scope, "Int8Array", |
| 229 |
"org.mozilla.javascript.typedarrays.NativeInt8Array", |
| 230 |
sealed, true); |
| 231 |
new LazilyLoadedCtor(scope, "Uint8Array", |
| 232 |
"org.mozilla.javascript.typedarrays.NativeUint8Array", |
| 233 |
sealed, true); |
| 234 |
new LazilyLoadedCtor(scope, "Uint8ClampedArray", |
| 235 |
"org.mozilla.javascript.typedarrays.NativeUint8ClampedArray", |
| 236 |
sealed, true); |
| 237 |
new LazilyLoadedCtor(scope, "Int16Array", |
| 238 |
"org.mozilla.javascript.typedarrays.NativeInt16Array", |
| 239 |
sealed, true); |
| 240 |
new LazilyLoadedCtor(scope, "Uint16Array", |
| 241 |
"org.mozilla.javascript.typedarrays.NativeUint16Array", |
| 242 |
sealed, true); |
| 243 |
new LazilyLoadedCtor(scope, "Int32Array", |
| 244 |
"org.mozilla.javascript.typedarrays.NativeInt32Array", |
| 245 |
sealed, true); |
| 246 |
new LazilyLoadedCtor(scope, "Uint32Array", |
| 247 |
"org.mozilla.javascript.typedarrays.NativeUint32Array", |
| 248 |
sealed, true); |
| 249 |
new LazilyLoadedCtor(scope, "Float32Array", |
| 250 |
"org.mozilla.javascript.typedarrays.NativeFloat32Array", |
| 251 |
sealed, true); |
| 252 |
new LazilyLoadedCtor(scope, "Float64Array", |
| 253 |
"org.mozilla.javascript.typedarrays.NativeFloat64Array", |
| 254 |
sealed, true); |
| 255 |
new LazilyLoadedCtor(scope, "DataView", |
| 256 |
"org.mozilla.javascript.typedarrays.NativeDataView", |
| 257 |
sealed, true); |
| 258 |
} |
| 259 |
|
| 260 |
if (cx.getLanguageVersion() >= Context.VERSION_ES6) { |
| 261 |
NativeSymbol.init(cx, scope, sealed); |
| 262 |
} |
| 263 |
|
| 264 |
if (scope instanceof TopLevel) { |
| 265 |
((TopLevel)scope).cacheBuiltins(); |
| 266 |
} |
| 267 |
|
| 268 |
return scope; |
| 269 |
} |
| 270 |
|
| 271 |
public static ScriptableObject initStandardObjects(Context cx, |
| 272 |
ScriptableObject scope, |
| 273 |
boolean sealed) |
| 274 |
{ |
| 275 |
ScriptableObject s = initSafeStandardObjects(cx, scope, sealed); |
| 276 |
|
| 277 |
new LazilyLoadedCtor(s, "Packages", |
| 278 |
"org.mozilla.javascript.NativeJavaTopPackage", sealed, true); |
| 279 |
new LazilyLoadedCtor(s, "getClass", |
| 280 |
"org.mozilla.javascript.NativeJavaTopPackage", sealed, true); |
| 281 |
new LazilyLoadedCtor(s, "JavaAdapter", |
| 282 |
"org.mozilla.javascript.JavaAdapter", sealed, true); |
| 283 |
new LazilyLoadedCtor(s, "JavaImporter", |
| 284 |
"org.mozilla.javascript.ImporterTopLevel", sealed, true); |
| 285 |
|
| 286 |
for (String packageName : getTopPackageNames()) { |
| 287 |
new LazilyLoadedCtor(s, packageName, |
| 288 |
"org.mozilla.javascript.NativeJavaTopPackage", sealed, true); |
| 289 |
} |
| 290 |
|
| 291 |
return s; |
| 292 |
} |
| 293 |
|
| 294 |
static String[] getTopPackageNames() { |
| 295 |
|
| 296 |
return "Dalvik".equals(System.getProperty("java.vm.name")) ? |
| 297 |
new String[] { "java", "javax", "org", "com", "edu", "net", "android" } : |
| 298 |
new String[] { "java", "javax", "org", "com", "edu", "net" }; |
| 299 |
} |
| 300 |
|
| 301 |
public static ScriptableObject getLibraryScopeOrNull(Scriptable scope) |
| 302 |
{ |
| 303 |
ScriptableObject libScope; |
| 304 |
libScope = (ScriptableObject)ScriptableObject. |
| 305 |
getTopScopeValue(scope, LIBRARY_SCOPE_KEY); |
| 306 |
return libScope; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
public static boolean isJSLineTerminator(int c) |
| 311 |
{ |
| 312 |
|
| 313 |
|
| 314 |
if ((c & 0xDFD0) != 0) { |
| 315 |
return false; |
| 316 |
} |
| 317 |
return c == '\n' || c == '\r' || c == 0x2028 || c == 0x2029; |
| 318 |
} |
| 319 |
|
| 320 |
public static boolean isJSWhitespaceOrLineTerminator(int c) { |
| 321 |
return (isStrWhiteSpaceChar(c) || isJSLineTerminator(c)); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
static boolean isStrWhiteSpaceChar(int c) |
| 340 |
{ |
| 341 |
switch (c) { |
| 342 |
case ' ': |
| 343 |
case '\n': |
| 344 |
case '\r': |
| 345 |
case '\t': |
| 346 |
case '\u00A0': |
| 347 |
case '\u000C': |
| 348 |
case '\u000B': |
| 349 |
case '\u2028': |
| 350 |
case '\u2029': |
| 351 |
case '\uFEFF': |
| 352 |
return true; |
| 353 |
default: |
| 354 |
return Character.getType(c) == Character.SPACE_SEPARATOR; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
public static Boolean wrapBoolean(boolean b) |
| 359 |
{ |
| 360 |
return b ? Boolean.TRUE : Boolean.FALSE; |
| 361 |
} |
| 362 |
|
| 363 |
public static Integer wrapInt(int i) |
| 364 |
{ |
| 365 |
return Integer.valueOf(i); |
| 366 |
} |
| 367 |
|
| 368 |
public static Number wrapNumber(double x) |
| 369 |
{ |
| 370 |
if (x != x) { |
| 371 |
return ScriptRuntime.NaNobj; |
| 372 |
} |
| 373 |
return new Double(x); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
public static boolean toBoolean(Object val) |
| 382 |
{ |
| 383 |
for (;;) { |
| 384 |
if (val instanceof Boolean) |
| 385 |
return ((Boolean) val).booleanValue(); |
| 386 |
if (val == null || val == Undefined.instance) |
| 387 |
return false; |
| 388 |
if (val instanceof CharSequence) |
| 389 |
return ((CharSequence) val).length() != 0; |
| 390 |
if (val instanceof Number) { |
| 391 |
double d = ((Number) val).doubleValue(); |
| 392 |
return (d == d && d != 0.0); |
| 393 |
} |
| 394 |
if (val instanceof Scriptable) { |
| 395 |
if (val instanceof ScriptableObject && |
| 396 |
((ScriptableObject) val).avoidObjectDetection()) |
| 397 |
{ |
| 398 |
return false; |
| 399 |
} |
| 400 |
if (Context.getContext().isVersionECMA1()) { |
| 401 |
|
| 402 |
return true; |
| 403 |
} |
| 404 |
|
| 405 |
val = ((Scriptable) val).getDefaultValue(BooleanClass); |
| 406 |
if ((val instanceof Scriptable) && !isSymbol(val)) |
| 407 |
throw errorWithClassName("msg.primitive.expected", val); |
| 408 |
continue; |
| 409 |
} |
| 410 |
warnAboutNonJSObject(val); |
| 411 |
return true; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
public static double toNumber(Object val) |
| 421 |
{ |
| 422 |
for (;;) { |
| 423 |
if (val instanceof Number) |
| 424 |
return ((Number) val).doubleValue(); |
| 425 |
if (val == null) |
| 426 |
return +0.0; |
| 427 |
if (val == Undefined.instance) |
| 428 |
return NaN; |
| 429 |
if (val instanceof String) |
| 430 |
return toNumber((String) val); |
| 431 |
if (val instanceof CharSequence) |
| 432 |
return toNumber(val.toString()); |
| 433 |
if (val instanceof Boolean) |
| 434 |
return ((Boolean) val).booleanValue() ? 1 : +0.0; |
| 435 |
if (val instanceof Symbol) |
| 436 |
throw typeError0("msg.not.a.number"); |
| 437 |
if (val instanceof Scriptable) { |
| 438 |
val = ((Scriptable) val).getDefaultValue(NumberClass); |
| 439 |
if ((val instanceof Scriptable) && !isSymbol(val)) |
| 440 |
throw errorWithClassName("msg.primitive.expected", val); |
| 441 |
continue; |
| 442 |
} |
| 443 |
warnAboutNonJSObject(val); |
| 444 |
return NaN; |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
public static double toNumber(Object[] args, int index) { |
| 449 |
return (index < args.length) ? toNumber(args[index]) : NaN; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
public static final double |
| 457 |
NaN = Double.longBitsToDouble(0x7ff8000000000000L); |
| 458 |
|
| 459 |
|
| 460 |
public static final double |
| 461 |
negativeZero = Double.longBitsToDouble(0x8000000000000000L); |
| 462 |
|
| 463 |
public static final Double NaNobj = new Double(NaN); |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
static double stringToNumber(String s, int start, int radix) { |
| 469 |
char digitMax = '9'; |
| 470 |
char lowerCaseBound = 'a'; |
| 471 |
char upperCaseBound = 'A'; |
| 472 |
int len = s.length(); |
| 473 |
if (radix < 10) { |
| 474 |
digitMax = (char) ('0' + radix - 1); |
| 475 |
} |
| 476 |
if (radix > 10) { |
| 477 |
lowerCaseBound = (char) ('a' + radix - 10); |
| 478 |
upperCaseBound = (char) ('A' + radix - 10); |
| 479 |
} |
| 480 |
int end; |
| 481 |
double sum = 0.0; |
| 482 |
for (end=start; end < len; end++) { |
| 483 |
char c = s.charAt(end); |
| 484 |
int newDigit; |
| 485 |
if ('0' <= c && c <= digitMax) |
| 486 |
newDigit = c - '0'; |
| 487 |
else if ('a' <= c && c < lowerCaseBound) |
| 488 |
newDigit = c - 'a' + 10; |
| 489 |
else if ('A' <= c && c < upperCaseBound) |
| 490 |
newDigit = c - 'A' + 10; |
| 491 |
else |
| 492 |
break; |
| 493 |
sum = sum*radix + newDigit; |
| 494 |
} |
| 495 |
if (start == end) { |
| 496 |
return NaN; |
| 497 |
} |
| 498 |
if (sum >= 9007199254740992.0) { |
| 499 |
if (radix == 10) { |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
try { |
| 506 |
return Double.parseDouble(s.substring(start, end)); |
| 507 |
} catch (NumberFormatException nfe) { |
| 508 |
return NaN; |
| 509 |
} |
| 510 |
} else if (radix == 2 || radix == 4 || radix == 8 || |
| 511 |
radix == 16 || radix == 32) |
| 512 |
{ |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
int bitShiftInChar = 1; |
| 524 |
int digit = 0; |
| 525 |
|
| 526 |
final int SKIP_LEADING_ZEROS = 0; |
| 527 |
final int FIRST_EXACT_53_BITS = 1; |
| 528 |
final int AFTER_BIT_53 = 2; |
| 529 |
final int ZEROS_AFTER_54 = 3; |
| 530 |
final int MIXED_AFTER_54 = 4; |
| 531 |
|
| 532 |
int state = SKIP_LEADING_ZEROS; |
| 533 |
int exactBitsLimit = 53; |
| 534 |
double factor = 0.0; |
| 535 |
boolean bit53 = false; |
| 536 |
|
| 537 |
boolean bit54 = false; |
| 538 |
|
| 539 |
for (;;) { |
| 540 |
if (bitShiftInChar == 1) { |
| 541 |
if (start == end) |
| 542 |
break; |
| 543 |
digit = s.charAt(start++); |
| 544 |
if ('0' <= digit && digit <= '9') |
| 545 |
digit -= '0'; |
| 546 |
else if ('a' <= digit && digit <= 'z') |
| 547 |
digit -= 'a' - 10; |
| 548 |
else |
| 549 |
digit -= 'A' - 10; |
| 550 |
bitShiftInChar = radix; |
| 551 |
} |
| 552 |
bitShiftInChar >>= 1; |
| 553 |
boolean bit = (digit & bitShiftInChar) != 0; |
| 554 |
|
| 555 |
switch (state) { |
| 556 |
case SKIP_LEADING_ZEROS: |
| 557 |
if (bit) { |
| 558 |
--exactBitsLimit; |
| 559 |
sum = 1.0; |
| 560 |
state = FIRST_EXACT_53_BITS; |
| 561 |
} |
| 562 |
break; |
| 563 |
case FIRST_EXACT_53_BITS: |
| 564 |
sum *= 2.0; |
| 565 |
if (bit) |
| 566 |
sum += 1.0; |
| 567 |
--exactBitsLimit; |
| 568 |
if (exactBitsLimit == 0) { |
| 569 |
bit53 = bit; |
| 570 |
state = AFTER_BIT_53; |
| 571 |
} |
| 572 |
break; |
| 573 |
case AFTER_BIT_53: |
| 574 |
bit54 = bit; |
| 575 |
factor = 2.0; |
| 576 |
state = ZEROS_AFTER_54; |
| 577 |
break; |
| 578 |
case ZEROS_AFTER_54: |
| 579 |
if (bit) { |
| 580 |
state = MIXED_AFTER_54; |
| 581 |
} |
| 582 |
|
| 583 |
case MIXED_AFTER_54: |
| 584 |
factor *= 2; |
| 585 |
break; |
| 586 |
} |
| 587 |
} |
| 588 |
switch (state) { |
| 589 |
case SKIP_LEADING_ZEROS: |
| 590 |
sum = 0.0; |
| 591 |
break; |
| 592 |
case FIRST_EXACT_53_BITS: |
| 593 |
case AFTER_BIT_53: |
| 594 |
|
| 595 |
break; |
| 596 |
case ZEROS_AFTER_54: |
| 597 |
|
| 598 |
|
| 599 |
if (bit54 & bit53) |
| 600 |
sum += 1.0; |
| 601 |
sum *= factor; |
| 602 |
break; |
| 603 |
case MIXED_AFTER_54: |
| 604 |
|
| 605 |
|
| 606 |
if (bit54) |
| 607 |
sum += 1.0; |
| 608 |
sum *= factor; |
| 609 |
break; |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
} |
| 614 |
return sum; |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
public static double toNumber(String s) { |
| 624 |
int len = s.length(); |
| 625 |
int start = 0; |
| 626 |
char startChar; |
| 627 |
for (;;) { |
| 628 |
if (start == len) { |
| 629 |
|
| 630 |
return +0.0; |
| 631 |
} |
| 632 |
startChar = s.charAt(start); |
| 633 |
if (!ScriptRuntime.isStrWhiteSpaceChar(startChar)) |
| 634 |
break; |
| 635 |
start++; |
| 636 |
} |
| 637 |
|
| 638 |
if (startChar == '0') { |
| 639 |
if (start + 2 < len) { |
| 640 |
int c1 = s.charAt(start + 1); |
| 641 |
if (c1 == 'x' || c1 == 'X') { |
| 642 |
|
| 643 |
return stringToNumber(s, start + 2, 16); |
| 644 |
} |
| 645 |
} |
| 646 |
} else if (startChar == '+' || startChar == '-') { |
| 647 |
if (start + 3 < len && s.charAt(start + 1) == '0') { |
| 648 |
int c2 = s.charAt(start + 2); |
| 649 |
if (c2 == 'x' || c2 == 'X') { |
| 650 |
|
| 651 |
double val = stringToNumber(s, start + 3, 16); |
| 652 |
return startChar == '-' ? -val : val; |
| 653 |
} |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
int end = len - 1; |
| 658 |
char endChar; |
| 659 |
while (ScriptRuntime.isStrWhiteSpaceChar(endChar = s.charAt(end))) |
| 660 |
end--; |
| 661 |
if (endChar == 'y') { |
| 662 |
|
| 663 |
if (startChar == '+' || startChar == '-') |
| 664 |
start++; |
| 665 |
if (start + 7 == end && s.regionMatches(start, "Infinity", 0, 8)) |
| 666 |
return startChar == '-' |
| 667 |
? Double.NEGATIVE_INFINITY |
| 668 |
: Double.POSITIVE_INFINITY; |
| 669 |
return NaN; |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
String sub = s.substring(start, end+1); |
| 674 |
|
| 675 |
|
| 676 |
for (int i = sub.length() - 1; i >= 0; i--) { |
| 677 |
char c = sub.charAt(i); |
| 678 |
if (('0' <= c && c <= '9') || c == '.' || |
| 679 |
c == 'e' || c == 'E' || |
| 680 |
c == '+' || c == '-') |
| 681 |
continue; |
| 682 |
return NaN; |
| 683 |
} |
| 684 |
try { |
| 685 |
return Double.parseDouble(sub); |
| 686 |
} catch (NumberFormatException ex) { |
| 687 |
return NaN; |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
public static Object[] padArguments(Object[] args, int count) { |
| 698 |
if (count < args.length) |
| 699 |
return args; |
| 700 |
|
| 701 |
int i; |
| 702 |
Object[] result = new Object[count]; |
| 703 |
for (i = 0; i < args.length; i++) { |
| 704 |
result[i] = args[i]; |
| 705 |
} |
| 706 |
|
| 707 |
for (; i < count; i++) { |
| 708 |
result[i] = Undefined.instance; |
| 709 |
} |
| 710 |
|
| 711 |
return result; |
| 712 |
} |
| 713 |
|
| 714 |
public static String escapeString(String s) |
| 715 |
{ |
| 716 |
return escapeString(s, '"'); |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
public static String escapeString(String s, char escapeQuote) |
| 724 |
{ |
| 725 |
if (!(escapeQuote == '"' || escapeQuote == '\'')) Kit.codeBug(); |
| 726 |
StringBuilder sb = null; |
| 727 |
|
| 728 |
for(int i = 0, L = s.length(); i != L; ++i) { |
| 729 |
int c = s.charAt(i); |
| 730 |
|
| 731 |
if (' ' <= c && c <= '~' && c != escapeQuote && c != '\\') { |
| 732 |
|
| 733 |
|
| 734 |
if (sb != null) { |
| 735 |
sb.append((char)c); |
| 736 |
} |
| 737 |
continue; |
| 738 |
} |
| 739 |
if (sb == null) { |
| 740 |
sb = new StringBuilder(L + 3); |
| 741 |
sb.append(s); |
| 742 |
sb.setLength(i); |
| 743 |
} |
| 744 |
|
| 745 |
int escape = -1; |
| 746 |
switch (c) { |
| 747 |
case '\b': escape = 'b'; break; |
| 748 |
case '\f': escape = 'f'; break; |
| 749 |
case '\n': escape = 'n'; break; |
| 750 |
case '\r': escape = 'r'; break; |
| 751 |
case '\t': escape = 't'; break; |
| 752 |
case 0xb: escape = 'v'; break; |
| 753 |
case ' ': escape = ' '; break; |
| 754 |
case '\\': escape = '\\'; break; |
| 755 |
} |
| 756 |
if (escape >= 0) { |
| 757 |
|
| 758 |
sb.append('\\'); |
| 759 |
sb.append((char)escape); |
| 760 |
} else if (c == escapeQuote) { |
| 761 |
sb.append('\\'); |
| 762 |
sb.append(escapeQuote); |
| 763 |
} else { |
| 764 |
int hexSize; |
| 765 |
if (c < 256) { |
| 766 |
|
| 767 |
sb.append("\\x"); |
| 768 |
hexSize = 2; |
| 769 |
} else { |
| 770 |
|
| 771 |
sb.append("\\u"); |
| 772 |
hexSize = 4; |
| 773 |
} |
| 774 |
|
| 775 |
for (int shift = (hexSize - 1) * 4; shift >= 0; shift -= 4) { |
| 776 |
int digit = 0xf & (c >> shift); |
| 777 |
int hc = (digit < 10) ? '0' + digit : 'a' - 10 + digit; |
| 778 |
sb.append((char)hc); |
| 779 |
} |
| 780 |
} |
| 781 |
} |
| 782 |
return (sb == null) ? s : sb.toString(); |
| 783 |
} |
| 784 |
|
| 785 |
static boolean isValidIdentifierName(String s, Context cx, boolean isStrict) |
| 786 |
{ |
| 787 |
int L = s.length(); |
| 788 |
if (L == 0) |
| 789 |
return false; |
| 790 |
if (!Character.isJavaIdentifierStart(s.charAt(0))) |
| 791 |
return false; |
| 792 |
for (int i = 1; i != L; ++i) { |
| 793 |
if (!Character.isJavaIdentifierPart(s.charAt(i))) |
| 794 |
return false; |
| 795 |
} |
| 796 |
return !TokenStream.isKeyword(s, cx.getLanguageVersion(), isStrict); |
| 797 |
} |
| 798 |
|
| 799 |
public static CharSequence toCharSequence(Object val) { |
| 800 |
if (val instanceof NativeString) { |
| 801 |
return ((NativeString)val).toCharSequence(); |
| 802 |
} |
| 803 |
return val instanceof CharSequence ? (CharSequence) val : toString(val); |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
public static String toString(Object val) { |
| 812 |
for (;;) { |
| 813 |
if (val == null) { |
| 814 |
return "null"; |
| 815 |
} |
| 816 |
if (val == Undefined.instance || val == Undefined.SCRIPTABLE_UNDEFINED) { |
| 817 |
return "undefined"; |
| 818 |
} |
| 819 |
if (val instanceof String) { |
| 820 |
return (String)val; |
| 821 |
} |
| 822 |
if (val instanceof CharSequence) { |
| 823 |
return val.toString(); |
| 824 |
} |
| 825 |
if (val instanceof Number) { |
| 826 |
|
| 827 |
|
| 828 |
return numberToString(((Number)val).doubleValue(), 10); |
| 829 |
} |
| 830 |
if (val instanceof Symbol) { |
| 831 |
throw typeError0("msg.not.a.string"); |
| 832 |
} |
| 833 |
if (val instanceof Scriptable) { |
| 834 |
val = ((Scriptable) val).getDefaultValue(StringClass); |
| 835 |
if ((val instanceof Scriptable) && !isSymbol(val)) { |
| 836 |
throw errorWithClassName("msg.primitive.expected", val); |
| 837 |
} |
| 838 |
continue; |
| 839 |
} |
| 840 |
return val.toString(); |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
static String defaultObjectToString(Scriptable obj) |
| 845 |
{ |
| 846 |
if (obj == null) |
| 847 |
return "[object Null]"; |
| 848 |
if (Undefined.isUndefined(obj)) |
| 849 |
return "[object Undefined]"; |
| 850 |
return "[object " + obj.getClassName() + ']'; |
| 851 |
} |
| 852 |
|
| 853 |
public static String toString(Object[] args, int index) |
| 854 |
{ |
| 855 |
return (index < args.length) ? toString(args[index]) : "undefined"; |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
public static String toString(double val) { |
| 862 |
return numberToString(val, 10); |
| 863 |
} |
| 864 |
|
| 865 |
public static String numberToString(double d, int base) { |
| 866 |
if ((base < 2) || (base > 36)) { |
| 867 |
throw Context.reportRuntimeError1( |
| 868 |
"msg.bad.radix", Integer.toString(base)); |
| 869 |
} |
| 870 |
|
| 871 |
if (d != d) |
| 872 |
return "NaN"; |
| 873 |
if (d == Double.POSITIVE_INFINITY) |
| 874 |
return "Infinity"; |
| 875 |
if (d == Double.NEGATIVE_INFINITY) |
| 876 |
return "-Infinity"; |
| 877 |
if (d == 0.0) |
| 878 |
return "0"; |
| 879 |
|
| 880 |
if (base != 10) { |
| 881 |
return DToA.JS_dtobasestr(base, d); |
| 882 |
} else { |
| 883 |
|
| 884 |
|
| 885 |
String result = FastDtoa.numberToString(d); |
| 886 |
if (result != null) { |
| 887 |
return result; |
| 888 |
} |
| 889 |
StringBuilder buffer = new StringBuilder(); |
| 890 |
DToA.JS_dtostr(buffer, DToA.DTOSTR_STANDARD, 0, d); |
| 891 |
return buffer.toString(); |
| 892 |
} |
| 893 |
|
| 894 |
} |
| 895 |
|
| 896 |
static String uneval(Context cx, Scriptable scope, Object value) |
| 897 |
{ |
| 898 |
if (value == null) { |
| 899 |
return "null"; |
| 900 |
} |
| 901 |
if (value == Undefined.instance) { |
| 902 |
return "undefined"; |
| 903 |
} |
| 904 |
if (value instanceof CharSequence) { |
| 905 |
String escaped = escapeString(value.toString()); |
| 906 |
StringBuilder sb = new StringBuilder(escaped.length() + 2); |
| 907 |
sb.append('\"'); |
| 908 |
sb.append(escaped); |
| 909 |
sb.append('\"'); |
| 910 |
return sb.toString(); |
| 911 |
} |
| 912 |
if (value instanceof Number) { |
| 913 |
double d = ((Number)value).doubleValue(); |
| 914 |
if (d == 0 && 1 / d < 0) { |
| 915 |
return "-0"; |
| 916 |
} |
| 917 |
return toString(d); |
| 918 |
} |
| 919 |
if (value instanceof Boolean) { |
| 920 |
return toString(value); |
| 921 |
} |
| 922 |
if (value instanceof Scriptable) { |
| 923 |
Scriptable obj = (Scriptable)value; |
| 924 |
|
| 925 |
|
| 926 |
if (ScriptableObject.hasProperty(obj, "toSource")) { |
| 927 |
Object v = ScriptableObject.getProperty(obj, "toSource"); |
| 928 |
if (v instanceof Function) { |
| 929 |
Function f = (Function)v; |
| 930 |
return toString(f.call(cx, scope, obj, emptyArgs)); |
| 931 |
} |
| 932 |
} |
| 933 |
return toString(value); |
| 934 |
} |
| 935 |
warnAboutNonJSObject(value); |
| 936 |
return value.toString(); |
| 937 |
} |
| 938 |
|
| 939 |
static String defaultObjectToSource(Context cx, Scriptable scope, |
| 940 |
Scriptable thisObj, Object[] args) |
| 941 |
{ |
| 942 |
boolean toplevel, iterating; |
| 943 |
if (cx.iterating == null) { |
| 944 |
toplevel = true; |
| 945 |
iterating = false; |
| 946 |
cx.iterating = new ObjToIntMap(31); |
| 947 |
} else { |
| 948 |
toplevel = false; |
| 949 |
iterating = cx.iterating.has(thisObj); |
| 950 |
} |
| 951 |
|
| 952 |
StringBuilder result = new StringBuilder(128); |
| 953 |
if (toplevel) { |
| 954 |
result.append("("); |
| 955 |
} |
| 956 |
result.append('{'); |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
try { |
| 961 |
if (!iterating) { |
| 962 |
cx.iterating.intern(thisObj); |
| 963 |
Object[] ids = thisObj.getIds(); |
| 964 |
for (int i=0; i < ids.length; i++) { |
| 965 |
Object id = ids[i]; |
| 966 |
Object value; |
| 967 |
if (id instanceof Integer) { |
| 968 |
int intId = ((Integer)id).intValue(); |
| 969 |
value = thisObj.get(intId, thisObj); |
| 970 |
if (value == Scriptable.NOT_FOUND) |
| 971 |
continue; |
| 972 |
if (i > 0) |
| 973 |
result.append(", "); |
| 974 |
result.append(intId); |
| 975 |
} else { |
| 976 |
String strId = (String)id; |
| 977 |
value = thisObj.get(strId, thisObj); |
| 978 |
if (value == Scriptable.NOT_FOUND) |
| 979 |
continue; |
| 980 |
if (i > 0) |
| 981 |
result.append(", "); |
| 982 |
if (ScriptRuntime.isValidIdentifierName(strId, cx, cx.isStrictMode())) { |
| 983 |
result.append(strId); |
| 984 |
} else { |
| 985 |
result.append('\''); |
| 986 |
result.append( |
| 987 |
ScriptRuntime.escapeString(strId, '\'')); |
| 988 |
result.append('\''); |
| 989 |
} |
| 990 |
} |
| 991 |
result.append(':'); |
| 992 |
result.append(ScriptRuntime.uneval(cx, scope, value)); |
| 993 |
} |
| 994 |
} |
| 995 |
} finally { |
| 996 |
if (toplevel) { |
| 997 |
cx.iterating = null; |
| 998 |
} |
| 999 |
} |
| 1000 |
|
| 1001 |
result.append('}'); |
| 1002 |
if (toplevel) { |
| 1003 |
result.append(')'); |
| 1004 |
} |
| 1005 |
return result.toString(); |
| 1006 |
} |
| 1007 |
|
| 1008 |
public static Scriptable toObject(Scriptable scope, Object val) |
| 1009 |
{ |
| 1010 |
if (val instanceof Scriptable) { |
| 1011 |
return (Scriptable)val; |
| 1012 |
} |
| 1013 |
return toObject(Context.getContext(), scope, val); |
| 1014 |
} |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
@Deprecated |
| 1023 |
public static Scriptable toObjectOrNull(Context cx, Object obj) |
| 1024 |
{ |
| 1025 |
if (obj instanceof Scriptable) { |
| 1026 |
return (Scriptable)obj; |
| 1027 |
} else if (obj != null && obj != Undefined.instance) { |
| 1028 |
return toObject(cx, getTopCallScope(cx), obj); |
| 1029 |
} |
| 1030 |
return null; |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
public static Scriptable toObjectOrNull(Context cx, Object obj, |
| 1037 |
Scriptable scope) |
| 1038 |
{ |
| 1039 |
if (obj instanceof Scriptable) { |
| 1040 |
return (Scriptable)obj; |
| 1041 |
} else if (obj != null && obj != Undefined.instance) { |
| 1042 |
return toObject(cx, scope, obj); |
| 1043 |
} |
| 1044 |
return null; |
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
@Deprecated |
| 1051 |
public static Scriptable toObject(Scriptable scope, Object val, |
| 1052 |
Class<?> staticClass) |
| 1053 |
{ |
| 1054 |
if (val instanceof Scriptable) { |
| 1055 |
return (Scriptable)val; |
| 1056 |
} |
| 1057 |
return toObject(Context.getContext(), scope, val); |
| 1058 |
} |
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
public static Scriptable toObject(Context cx, Scriptable scope, Object val) |
| 1066 |
{ |
| 1067 |
if (isSymbol(val)) { |
| 1068 |
NativeSymbol result = new NativeSymbol((NativeSymbol)val); |
| 1069 |
setBuiltinProtoAndParent(result, scope, TopLevel.Builtins.Symbol); |
| 1070 |
return result; |
| 1071 |
} |
| 1072 |
if (val instanceof Scriptable) { |
| 1073 |
return (Scriptable) val; |
| 1074 |
} |
| 1075 |
if (val instanceof CharSequence) { |
| 1076 |
|
| 1077 |
NativeString result = new NativeString((CharSequence)val); |
| 1078 |
setBuiltinProtoAndParent(result, scope, TopLevel.Builtins.String); |
| 1079 |
return result; |
| 1080 |
} |
| 1081 |
if (val instanceof Number) { |
| 1082 |
NativeNumber result = new NativeNumber(((Number)val).doubleValue()); |
| 1083 |
setBuiltinProtoAndParent(result, scope, TopLevel.Builtins.Number); |
| 1084 |
return result; |
| 1085 |
} |
| 1086 |
if (val instanceof Boolean) { |
| 1087 |
NativeBoolean result = new NativeBoolean(((Boolean)val).booleanValue()); |
| 1088 |
setBuiltinProtoAndParent(result, scope, TopLevel.Builtins.Boolean); |
| 1089 |
return result; |
| 1090 |
} |
| 1091 |
if (val == null) { |
| 1092 |
throw typeError0("msg.null.to.object"); |
| 1093 |
} |
| 1094 |
if (val == Undefined.instance) { |
| 1095 |
throw typeError0("msg.undef.to.object"); |
| 1096 |
} |
| 1097 |
|
| 1098 |
|
| 1099 |
Object wrapped = cx.getWrapFactory().wrap(cx, scope, val, null); |
| 1100 |
if (wrapped instanceof Scriptable) |
| 1101 |
return (Scriptable) wrapped; |
| 1102 |
throw errorWithClassName("msg.invalid.type", val); |
| 1103 |
} |
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
@Deprecated |
| 1109 |
public static Scriptable toObject(Context cx, Scriptable scope, Object val, |
| 1110 |
Class<?> staticClass) |
| 1111 |
{ |
| 1112 |
return toObject(cx, scope, val); |
| 1113 |
} |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
@Deprecated |
| 1119 |
public static Object call(Context cx, Object fun, Object thisArg, |
| 1120 |
Object[] args, Scriptable scope) |
| 1121 |
{ |
| 1122 |
if (!(fun instanceof Function)) { |
| 1123 |
throw notFunctionError(toString(fun)); |
| 1124 |
} |
| 1125 |
Function function = (Function)fun; |
| 1126 |
Scriptable thisObj = toObjectOrNull(cx, thisArg, scope); |
| 1127 |
if (thisObj == null) { |
| 1128 |
throw undefCallError(thisObj, "function"); |
| 1129 |
} |
| 1130 |
return function.call(cx, scope, thisObj, args); |
| 1131 |
} |
| 1132 |
|
| 1133 |
public static Scriptable newObject(Context cx, Scriptable scope, |
| 1134 |
String constructorName, Object[] args) |
| 1135 |
{ |
| 1136 |
scope = ScriptableObject.getTopLevelScope(scope); |
| 1137 |
Function ctor = getExistingCtor(cx, scope, constructorName); |
| 1138 |
if (args == null) { args = ScriptRuntime.emptyArgs; } |
| 1139 |
return ctor.construct(cx, scope, args); |
| 1140 |
} |
| 1141 |
|
| 1142 |
public static Scriptable newBuiltinObject(Context cx, Scriptable scope, |
| 1143 |
TopLevel.Builtins type, |
| 1144 |
Object[] args) |
| 1145 |
{ |
| 1146 |
scope = ScriptableObject.getTopLevelScope(scope); |
| 1147 |
Function ctor = TopLevel.getBuiltinCtor(cx, scope, type); |
| 1148 |
if (args == null) { args = ScriptRuntime.emptyArgs; } |
| 1149 |
return ctor.construct(cx, scope, args); |
| 1150 |
} |
| 1151 |
|
| 1152 |
static Scriptable newNativeError(Context cx, Scriptable scope, |
| 1153 |
TopLevel.NativeErrors type, Object[] args) |
| 1154 |
{ |
| 1155 |
scope = ScriptableObject.getTopLevelScope(scope); |
| 1156 |
Function ctor = TopLevel.getNativeErrorCtor(cx, scope, type); |
| 1157 |
if (args == null) { args = ScriptRuntime.emptyArgs; } |
| 1158 |
return ctor.construct(cx, scope, args); |
| 1159 |
} |
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
public static double toInteger(Object val) { |
| 1166 |
return toInteger(toNumber(val)); |
| 1167 |
} |
| 1168 |
|
| 1169 |
|
| 1170 |
public static double toInteger(double d) { |
| 1171 |
|
| 1172 |
if (d != d) |
| 1173 |
return +0.0; |
| 1174 |
|
| 1175 |
if (d == 0.0 || |
| 1176 |
d == Double.POSITIVE_INFINITY || |
| 1177 |
d == Double.NEGATIVE_INFINITY) |
| 1178 |
return d; |
| 1179 |
|
| 1180 |
if (d > 0.0) |
| 1181 |
return Math.floor(d); |
| 1182 |
else |
| 1183 |
return Math.ceil(d); |
| 1184 |
} |
| 1185 |
|
| 1186 |
public static double toInteger(Object[] args, int index) { |
| 1187 |
return (index < args.length) ? toInteger(args[index]) : +0.0; |
| 1188 |
} |
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
public static int toInt32(Object val) |
| 1195 |
{ |
| 1196 |
|
| 1197 |
if (val instanceof Integer) |
| 1198 |
return ((Integer)val).intValue(); |
| 1199 |
|
| 1200 |
return toInt32(toNumber(val)); |
| 1201 |
} |
| 1202 |
|
| 1203 |
public static int toInt32(Object[] args, int index) { |
| 1204 |
return (index < args.length) ? toInt32(args[index]) : 0; |
| 1205 |
} |
| 1206 |
|
| 1207 |
public static int toInt32(double d) { |
| 1208 |
return DoubleConversion.doubleToInt32(d); |
| 1209 |
} |
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
public static long toUint32(double d) { |
| 1216 |
return DoubleConversion.doubleToInt32(d) & 0xffffffffL; |
| 1217 |
} |
| 1218 |
|
| 1219 |
public static long toUint32(Object val) { |
| 1220 |
return toUint32(toNumber(val)); |
| 1221 |
} |
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
public static char toUint16(Object val) { |
| 1228 |
double d = toNumber(val); |
| 1229 |
return (char)DoubleConversion.doubleToInt32(d); |
| 1230 |
} |
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
private static final String DEFAULT_NS_TAG = "__default_namespace__"; |
| 1235 |
|
| 1236 |
public static Object setDefaultNamespace(Object namespace, Context cx) |
| 1237 |
{ |
| 1238 |
Scriptable scope = cx.currentActivationCall; |
| 1239 |
if (scope == null) { |
| 1240 |
scope = getTopCallScope(cx); |
| 1241 |
} |
| 1242 |
|
| 1243 |
XMLLib xmlLib = currentXMLLib(cx); |
| 1244 |
Object ns = xmlLib.toDefaultXmlNamespace(cx, namespace); |
| 1245 |
|
| 1246 |
|
| 1247 |
if (!scope.has(DEFAULT_NS_TAG, scope)) { |
| 1248 |
|
| 1249 |
ScriptableObject.defineProperty(scope, DEFAULT_NS_TAG, ns, |
| 1250 |
ScriptableObject.PERMANENT |
| 1251 |
| ScriptableObject.DONTENUM); |
| 1252 |
} else { |
| 1253 |
scope.put(DEFAULT_NS_TAG, scope, ns); |
| 1254 |
} |
| 1255 |
|
| 1256 |
return Undefined.instance; |
| 1257 |
} |
| 1258 |
|
| 1259 |
public static Object searchDefaultNamespace(Context cx) |
| 1260 |
{ |
| 1261 |
Scriptable scope = cx.currentActivationCall; |
| 1262 |
if (scope == null) { |
| 1263 |
scope = getTopCallScope(cx); |
| 1264 |
} |
| 1265 |
Object nsObject; |
| 1266 |
for (;;) { |
| 1267 |
Scriptable parent = scope.getParentScope(); |
| 1268 |
if (parent == null) { |
| 1269 |
nsObject = ScriptableObject.getProperty(scope, DEFAULT_NS_TAG); |
| 1270 |
if (nsObject == Scriptable.NOT_FOUND) { |
| 1271 |
return null; |
| 1272 |
} |
| 1273 |
break; |
| 1274 |
} |
| 1275 |
nsObject = scope.get(DEFAULT_NS_TAG, scope); |
| 1276 |
if (nsObject != Scriptable.NOT_FOUND) { |
| 1277 |
break; |
| 1278 |
} |
| 1279 |
scope = parent; |
| 1280 |
} |
| 1281 |
return nsObject; |
| 1282 |
} |
| 1283 |
|
| 1284 |
public static Object getTopLevelProp(Scriptable scope, String id) { |
| 1285 |
scope = ScriptableObject.getTopLevelScope(scope); |
| 1286 |
return ScriptableObject.getProperty(scope, id); |
| 1287 |
} |
| 1288 |
|
| 1289 |
static Function getExistingCtor(Context cx, Scriptable scope, |
| 1290 |
String constructorName) |
| 1291 |
{ |
| 1292 |
Object ctorVal = ScriptableObject.getProperty(scope, constructorName); |
| 1293 |
if (ctorVal instanceof Function) { |
| 1294 |
return (Function)ctorVal; |
| 1295 |
} |
| 1296 |
if (ctorVal == Scriptable.NOT_FOUND) { |
| 1297 |
throw Context.reportRuntimeError1( |
| 1298 |
"msg.ctor.not.found", constructorName); |
| 1299 |
} else { |
| 1300 |
throw Context.reportRuntimeError1( |
| 1301 |
"msg.not.ctor", constructorName); |
| 1302 |
} |
| 1303 |
} |
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
public static long indexFromString(String str) |
| 1311 |
{ |
| 1312 |
|
| 1313 |
|
| 1314 |
final int MAX_VALUE_LENGTH = 10; |
| 1315 |
|
| 1316 |
int len = str.length(); |
| 1317 |
if (len > 0) { |
| 1318 |
int i = 0; |
| 1319 |
boolean negate = false; |
| 1320 |
int c = str.charAt(0); |
| 1321 |
if (c == '-') { |
| 1322 |
if (len > 1) { |
| 1323 |
c = str.charAt(1); |
| 1324 |
if (c == '0') return -1L; |
| 1325 |
i = 1; |
| 1326 |
negate = true; |
| 1327 |
} |
| 1328 |
} |
| 1329 |
c -= '0'; |
| 1330 |
if (0 <= c && c <= 9 |
| 1331 |
&& len <= (negate ? MAX_VALUE_LENGTH + 1 : MAX_VALUE_LENGTH)) |
| 1332 |
{ |
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
int index = -c; |
| 1337 |
int oldIndex = 0; |
| 1338 |
i++; |
| 1339 |
if (index != 0) { |
| 1340 |
|
| 1341 |
while (i != len && 0 <= (c = str.charAt(i) - '0') && c <= 9) |
| 1342 |
{ |
| 1343 |
oldIndex = index; |
| 1344 |
index = 10 * index - c; |
| 1345 |
i++; |
| 1346 |
} |
| 1347 |
} |
| 1348 |
|
| 1349 |
|
| 1350 |
if (i == len && |
| 1351 |
(oldIndex > (Integer.MIN_VALUE / 10) || |
| 1352 |
(oldIndex == (Integer.MIN_VALUE / 10) && |
| 1353 |
c <= (negate ? -(Integer.MIN_VALUE % 10) |
| 1354 |
: (Integer.MAX_VALUE % 10))))) |
| 1355 |
{ |
| 1356 |
return 0xFFFFFFFFL & (negate ? index : -index); |
| 1357 |
} |
| 1358 |
} |
| 1359 |
} |
| 1360 |
return -1L; |
| 1361 |
} |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
public static long testUint32String(String str) |
| 1368 |
{ |
| 1369 |
|
| 1370 |
|
| 1371 |
final int MAX_VALUE_LENGTH = 10; |
| 1372 |
|
| 1373 |
int len = str.length(); |
| 1374 |
if (1 <= len && len <= MAX_VALUE_LENGTH) { |
| 1375 |
int c = str.charAt(0); |
| 1376 |
c -= '0'; |
| 1377 |
if (c == 0) { |
| 1378 |
|
| 1379 |
return (len == 1) ? 0L : -1L; |
| 1380 |
} |
| 1381 |
if (1 <= c && c <= 9) { |
| 1382 |
long v = c; |
| 1383 |
for (int i = 1; i != len; ++i) { |
| 1384 |
c = str.charAt(i) - '0'; |
| 1385 |
if (!(0 <= c && c <= 9)) { |
| 1386 |
return -1; |
| 1387 |
} |
| 1388 |
v = 10 * v + c; |
| 1389 |
} |
| 1390 |
|
| 1391 |
if ((v >>> 32) == 0) { |
| 1392 |
return v; |
| 1393 |
} |
| 1394 |
} |
| 1395 |
} |
| 1396 |
return -1; |
| 1397 |
} |
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
static Object getIndexObject(String s) |
| 1404 |
{ |
| 1405 |
long indexTest = indexFromString(s); |
| 1406 |
if (indexTest >= 0) { |
| 1407 |
return Integer.valueOf((int)indexTest); |
| 1408 |
} |
| 1409 |
return s; |
| 1410 |
} |
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
static Object getIndexObject(double d) |
| 1417 |
{ |
| 1418 |
int i = (int)d; |
| 1419 |
if (i == d) { |
| 1420 |
return Integer.valueOf(i); |
| 1421 |
} |
| 1422 |
return toString(d); |
| 1423 |
} |
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
static String toStringIdOrIndex(Context cx, Object id) |
| 1431 |
{ |
| 1432 |
if (id instanceof Number) { |
| 1433 |
double d = ((Number)id).doubleValue(); |
| 1434 |
int index = (int)d; |
| 1435 |
if (index == d) { |
| 1436 |
storeIndexResult(cx, index); |
| 1437 |
return null; |
| 1438 |
} |
| 1439 |
return toString(id); |
| 1440 |
} else { |
| 1441 |
String s; |
| 1442 |
if (id instanceof String) { |
| 1443 |
s = (String)id; |
| 1444 |
} else { |
| 1445 |
s = toString(id); |
| 1446 |
} |
| 1447 |
long indexTest = indexFromString(s); |
| 1448 |
if (indexTest >= 0) { |
| 1449 |
storeIndexResult(cx, (int)indexTest); |
| 1450 |
return null; |
| 1451 |
} |
| 1452 |
return s; |
| 1453 |
} |
| 1454 |
} |
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
@Deprecated |
| 1462 |
public static Object getObjectElem(Object obj, Object elem, Context cx) |
| 1463 |
{ |
| 1464 |
return getObjectElem(obj, elem, cx, getTopCallScope(cx)); |
| 1465 |
} |
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
public static Object getObjectElem(Object obj, Object elem, Context cx, Scriptable scope) |
| 1471 |
{ |
| 1472 |
Scriptable sobj = toObjectOrNull(cx, obj, scope); |
| 1473 |
if (sobj == null) { |
| 1474 |
throw undefReadError(obj, elem); |
| 1475 |
} |
| 1476 |
return getObjectElem(sobj, elem, cx); |
| 1477 |
} |
| 1478 |
|
| 1479 |
public static Object getObjectElem(Scriptable obj, Object elem, |
| 1480 |
Context cx) |
| 1481 |
{ |
| 1482 |
|
| 1483 |
Object result; |
| 1484 |
|
| 1485 |
if (obj instanceof XMLObject) { |
| 1486 |
result = ((XMLObject)obj).get(cx, elem); |
| 1487 |
} else if (isSymbol(elem)) { |
| 1488 |
result = ScriptableObject.getProperty(obj, (Symbol)elem); |
| 1489 |
} else { |
| 1490 |
String s = toStringIdOrIndex(cx, elem); |
| 1491 |
if (s == null) { |
| 1492 |
int index = lastIndexResult(cx); |
| 1493 |
result = ScriptableObject.getProperty(obj, index); |
| 1494 |
} else { |
| 1495 |
result = ScriptableObject.getProperty(obj, s); |
| 1496 |
} |
| 1497 |
} |
| 1498 |
|
| 1499 |
if (result == Scriptable.NOT_FOUND) { |
| 1500 |
result = Undefined.instance; |
| 1501 |
} |
| 1502 |
|
| 1503 |
return result; |
| 1504 |
} |
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
@Deprecated |
| 1512 |
public static Object getObjectProp(Object obj, String property, |
| 1513 |
Context cx) |
| 1514 |
{ |
| 1515 |
return getObjectProp(obj, property, cx, getTopCallScope(cx)); |
| 1516 |
} |
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
public static Object getObjectProp(Object obj, String property, |
| 1524 |
Context cx, Scriptable scope) |
| 1525 |
{ |
| 1526 |
Scriptable sobj = toObjectOrNull(cx, obj, scope); |
| 1527 |
if (sobj == null) { |
| 1528 |
throw undefReadError(obj, property); |
| 1529 |
} |
| 1530 |
return getObjectProp(sobj, property, cx); |
| 1531 |
} |
| 1532 |
|
| 1533 |
public static Object getObjectProp(Scriptable obj, String property, |
| 1534 |
Context cx) |
| 1535 |
{ |
| 1536 |
|
| 1537 |
Object result = ScriptableObject.getProperty(obj, property); |
| 1538 |
if (result == Scriptable.NOT_FOUND) { |
| 1539 |
if (cx.hasFeature(Context.FEATURE_STRICT_MODE)) { |
| 1540 |
Context.reportWarning(ScriptRuntime.getMessage1( |
| 1541 |
"msg.ref.undefined.prop", property)); |
| 1542 |
} |
| 1543 |
result = Undefined.instance; |
| 1544 |
} |
| 1545 |
|
| 1546 |
return result; |
| 1547 |
} |
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
@Deprecated |
| 1553 |
public static Object getObjectPropNoWarn(Object obj, String property, |
| 1554 |
Context cx) |
| 1555 |
{ |
| 1556 |
return getObjectPropNoWarn(obj, property, cx, getTopCallScope(cx)); |
| 1557 |
} |
| 1558 |
|
| 1559 |
public static Object getObjectPropNoWarn(Object obj, String property, |
| 1560 |
Context cx, Scriptable scope) |
| 1561 |
{ |
| 1562 |
Scriptable sobj = toObjectOrNull(cx, obj, scope); |
| 1563 |
if (sobj == null) { |
| 1564 |
throw undefReadError(obj, property); |
| 1565 |
} |
| 1566 |
Object result = ScriptableObject.getProperty(sobj, property); |
| 1567 |
if (result == Scriptable.NOT_FOUND) { |
| 1568 |
return Undefined.instance; |
| 1569 |
} |
| 1570 |
return result; |
| 1571 |
} |
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
@Deprecated |
| 1580 |
public static Object getObjectIndex(Object obj, double dblIndex, |
| 1581 |
Context cx) |
| 1582 |
{ |
| 1583 |
return getObjectIndex(obj, dblIndex, cx, getTopCallScope(cx)); |
| 1584 |
} |
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
public static Object getObjectIndex(Object obj, double dblIndex, |
| 1591 |
Context cx, Scriptable scope) |
| 1592 |
{ |
| 1593 |
Scriptable sobj = toObjectOrNull(cx, obj, scope); |
| 1594 |
if (sobj == null) { |
| 1595 |
throw undefReadError(obj, toString(dblIndex)); |
| 1596 |
} |
| 1597 |
|
| 1598 |
int index = (int)dblIndex; |
| 1599 |
if (index == dblIndex) { |
| 1600 |
return getObjectIndex(sobj, index, cx); |
| 1601 |
} else { |
| 1602 |
String s = toString(dblIndex); |
| 1603 |
return getObjectProp(sobj, s, cx); |
| 1604 |
} |
| 1605 |
} |
| 1606 |
|
| 1607 |
public static Object getObjectIndex(Scriptable obj, int index, |
| 1608 |
Context cx) |
| 1609 |
{ |
| 1610 |
Object result = ScriptableObject.getProperty(obj, index); |
| 1611 |
if (result == Scriptable.NOT_FOUND) { |
| 1612 |
result = Undefined.instance; |
| 1613 |
} |
| 1614 |
|
| 1615 |
return result; |
| 1616 |
} |
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
@Deprecated |
| 1624 |
public static Object setObjectElem(Object obj, Object elem, Object value, |
| 1625 |
Context cx) |
| 1626 |
{ |
| 1627 |
return setObjectElem(obj, elem, value, cx, getTopCallScope(cx)); |
| 1628 |
} |
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
public static Object setObjectElem(Object obj, Object elem, Object value, |
| 1634 |
Context cx, Scriptable scope) |
| 1635 |
{ |
| 1636 |
Scriptable sobj = toObjectOrNull(cx, obj, scope); |
| 1637 |
if (sobj == null) { |
| 1638 |
throw undefWriteError(obj, elem, value); |
| 1639 |
} |
| 1640 |
return setObjectElem(sobj, elem, value, cx); |
| 1641 |
} |
| 1642 |
|
| 1643 |
public static Object setObjectElem(Scriptable obj, Object elem, |
| 1644 |
Object value, Context cx) |
| 1645 |
{ |
| 1646 |
if (obj instanceof XMLObject) { |
| 1647 |
((XMLObject)obj).put(cx, elem, value); |
| 1648 |
} else if (isSymbol(elem)) { |
| 1649 |
ScriptableObject.putProperty(obj, (Symbol)elem, value); |
| 1650 |
} else { |
| 1651 |
String s = toStringIdOrIndex(cx, elem); |
| 1652 |
if (s == null) { |
| 1653 |
int index = lastIndexResult(cx); |
| 1654 |
ScriptableObject.putProperty(obj, index, value); |
| 1655 |
} else { |
| 1656 |
ScriptableObject.putProperty(obj, s, value); |
| 1657 |
} |
| 1658 |
} |
| 1659 |
|
| 1660 |
return value; |
| 1661 |
} |
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
@Deprecated |
| 1669 |
public static Object setObjectProp(Object obj, String property, |
| 1670 |
Object value, Context cx) |
| 1671 |
{ |
| 1672 |
return setObjectProp(obj, property, value, cx, getTopCallScope(cx)); |
| 1673 |
} |
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
public static Object setObjectProp(Object obj, String property, |
| 1679 |
Object value, Context cx, |
| 1680 |
Scriptable scope) |
| 1681 |
{ |
| 1682 |
Scriptable sobj = toObjectOrNull(cx, obj, scope); |
| 1683 |
if (sobj == null) { |
| 1684 |
throw undefWriteError(obj, property, value); |
| 1685 |
} |
| 1686 |
return setObjectProp(sobj, property, value, cx); |
| 1687 |
} |
| 1688 |
|
| 1689 |
public static Object setObjectProp(Scriptable obj, String property, |
| 1690 |
Object value, Context cx) |
| 1691 |
{ |
| 1692 |
ScriptableObject.putProperty(obj, property, value); |
| 1693 |
return value; |
| 1694 |
} |
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
@Deprecated |
| 1703 |
public static Object setObjectIndex(Object obj, double dblIndex, |
| 1704 |
Object value, Context cx) |
| 1705 |
{ |
| 1706 |
return setObjectIndex(obj, dblIndex, value, cx, getTopCallScope(cx)); |
| 1707 |
} |
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
public static Object setObjectIndex(Object obj, double dblIndex, |
| 1714 |
Object value, Context cx, |
| 1715 |
Scriptable scope) |
| 1716 |
{ |
| 1717 |
Scriptable sobj = toObjectOrNull(cx, obj, scope); |
| 1718 |
if (sobj == null) { |
| 1719 |
throw undefWriteError(obj, String.valueOf(dblIndex), value); |
| 1720 |
} |
| 1721 |
|
| 1722 |
int index = (int)dblIndex; |
| 1723 |
if (index == dblIndex) { |
| 1724 |
return setObjectIndex(sobj, index, value, cx); |
| 1725 |
} else { |
| 1726 |
String s = toString(dblIndex); |
| 1727 |
return setObjectProp(sobj, s, value, cx); |
| 1728 |
} |
| 1729 |
} |
| 1730 |
|
| 1731 |
public static Object setObjectIndex(Scriptable obj, int index, Object value, |
| 1732 |
Context cx) |
| 1733 |
{ |
| 1734 |
ScriptableObject.putProperty(obj, index, value); |
| 1735 |
return value; |
| 1736 |
} |
| 1737 |
|
| 1738 |
public static boolean deleteObjectElem(Scriptable target, Object elem, |
| 1739 |
Context cx) |
| 1740 |
{ |
| 1741 |
if (isSymbol(elem)) { |
| 1742 |
SymbolScriptable so = ScriptableObject.ensureSymbolScriptable(target); |
| 1743 |
Symbol s = (Symbol)elem; |
| 1744 |
so.delete(s); |
| 1745 |
return !so.has(s, target); |
| 1746 |
} |
| 1747 |
String s = toStringIdOrIndex(cx, elem); |
| 1748 |
if (s == null) { |
| 1749 |
int index = lastIndexResult(cx); |
| 1750 |
target.delete(index); |
| 1751 |
return !target.has(index, target); |
| 1752 |
} else { |
| 1753 |
target.delete(s); |
| 1754 |
return !target.has(s, target); |
| 1755 |
} |
| 1756 |
} |
| 1757 |
|
| 1758 |
public static boolean hasObjectElem(Scriptable target, Object elem, |
| 1759 |
Context cx) |
| 1760 |
{ |
| 1761 |
boolean result; |
| 1762 |
|
| 1763 |
if (isSymbol(elem)) { |
| 1764 |
result = ScriptableObject.hasProperty(target, (Symbol)elem); |
| 1765 |
} else { |
| 1766 |
String s = toStringIdOrIndex(cx, elem); |
| 1767 |
if (s == null) { |
| 1768 |
int index = lastIndexResult(cx); |
| 1769 |
result = ScriptableObject.hasProperty(target, index); |
| 1770 |
} else { |
| 1771 |
result = ScriptableObject.hasProperty(target, s); |
| 1772 |
} |
| 1773 |
} |
| 1774 |
|
| 1775 |
return result; |
| 1776 |
} |
| 1777 |
|
| 1778 |
public static Object refGet(Ref ref, Context cx) |
| 1779 |
{ |
| 1780 |
return ref.get(cx); |
| 1781 |
} |
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
@Deprecated |
| 1787 |
public static Object refSet(Ref ref, Object value, Context cx) |
| 1788 |
{ |
| 1789 |
return refSet(ref, value, cx, getTopCallScope(cx)); |
| 1790 |
} |
| 1791 |
|
| 1792 |
public static Object refSet(Ref ref, Object value, Context cx, |
| 1793 |
Scriptable scope) |
| 1794 |
{ |
| 1795 |
return ref.set(cx, scope, value); |
| 1796 |
} |
| 1797 |
|
| 1798 |
public static Object refDel(Ref ref, Context cx) |
| 1799 |
{ |
| 1800 |
return wrapBoolean(ref.delete(cx)); |
| 1801 |
} |
| 1802 |
|
| 1803 |
static boolean isSpecialProperty(String s) |
| 1804 |
{ |
| 1805 |
return s.equals("__proto__") || s.equals("__parent__"); |
| 1806 |
} |
| 1807 |
|
| 1808 |
|
| 1809 |
|
| 1810 |
|
| 1811 |
@Deprecated |
| 1812 |
public static Ref specialRef(Object obj, String specialProperty, |
| 1813 |
Context cx) |
| 1814 |
{ |
| 1815 |
return specialRef(obj, specialProperty, cx, getTopCallScope(cx)); |
| 1816 |
} |
| 1817 |
|
| 1818 |
public static Ref specialRef(Object obj, String specialProperty, |
| 1819 |
Context cx, Scriptable scope) |
| 1820 |
{ |
| 1821 |
return SpecialRef.createSpecial(cx, scope, obj, specialProperty); |
| 1822 |
} |
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
@Deprecated |
| 1828 |
public static Object delete(Object obj, Object id, Context cx) |
| 1829 |
{ |
| 1830 |
return delete(obj, id, cx, false); |
| 1831 |
} |
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
|
| 1841 |
|
| 1842 |
|
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
@Deprecated |
| 1847 |
public static Object delete(Object obj, Object id, Context cx, boolean isName) |
| 1848 |
{ |
| 1849 |
return delete(obj, id, cx, getTopCallScope(cx), isName); |
| 1850 |
} |
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
public static Object delete(Object obj, Object id, Context cx, |
| 1864 |
Scriptable scope, boolean isName) |
| 1865 |
{ |
| 1866 |
Scriptable sobj = toObjectOrNull(cx, obj, scope); |
| 1867 |
if (sobj == null) { |
| 1868 |
if (isName) { |
| 1869 |
return Boolean.TRUE; |
| 1870 |
} |
| 1871 |
throw undefDeleteError(obj, id); |
| 1872 |
} |
| 1873 |
boolean result = deleteObjectElem(sobj, id, cx); |
| 1874 |
return wrapBoolean(result); |
| 1875 |
} |
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
public static Object name(Context cx, Scriptable scope, String name) |
| 1881 |
{ |
| 1882 |
Scriptable parent = scope.getParentScope(); |
| 1883 |
if (parent == null) { |
| 1884 |
Object result = topScopeName(cx, scope, name); |
| 1885 |
if (result == Scriptable.NOT_FOUND) { |
| 1886 |
throw notFoundError(scope, name); |
| 1887 |
} |
| 1888 |
return result; |
| 1889 |
} |
| 1890 |
|
| 1891 |
return nameOrFunction(cx, scope, parent, name, false); |
| 1892 |
} |
| 1893 |
|
| 1894 |
private static Object nameOrFunction(Context cx, Scriptable scope, |
| 1895 |
Scriptable parentScope, String name, |
| 1896 |
boolean asFunctionCall) |
| 1897 |
{ |
| 1898 |
Object result; |
| 1899 |
Scriptable thisObj = scope; |
| 1900 |
|
| 1901 |
XMLObject firstXMLObject = null; |
| 1902 |
for (;;) { |
| 1903 |
if (scope instanceof NativeWith) { |
| 1904 |
Scriptable withObj = scope.getPrototype(); |
| 1905 |
if (withObj instanceof XMLObject) { |
| 1906 |
XMLObject xmlObj = (XMLObject)withObj; |
| 1907 |
if (xmlObj.has(name, xmlObj)) { |
| 1908 |
|
| 1909 |
thisObj = xmlObj; |
| 1910 |
result = xmlObj.get(name, xmlObj); |
| 1911 |
break; |
| 1912 |
} |
| 1913 |
if (firstXMLObject == null) { |
| 1914 |
firstXMLObject = xmlObj; |
| 1915 |
} |
| 1916 |
} else { |
| 1917 |
result = ScriptableObject.getProperty(withObj, name); |
| 1918 |
if (result != Scriptable.NOT_FOUND) { |
| 1919 |
|
| 1920 |
thisObj = withObj; |
| 1921 |
break; |
| 1922 |
} |
| 1923 |
} |
| 1924 |
} else if (scope instanceof NativeCall) { |
| 1925 |
|
| 1926 |
|
| 1927 |
result = scope.get(name, scope); |
| 1928 |
if (result != Scriptable.NOT_FOUND) { |
| 1929 |
if (asFunctionCall) { |
| 1930 |
|
| 1931 |
|
| 1932 |
thisObj = ScriptableObject. |
| 1933 |
getTopLevelScope(parentScope); |
| 1934 |
} |
| 1935 |
break; |
| 1936 |
} |
| 1937 |
} else { |
| 1938 |
|
| 1939 |
|
| 1940 |
result = ScriptableObject.getProperty(scope, name); |
| 1941 |
if (result != Scriptable.NOT_FOUND) { |
| 1942 |
thisObj = scope; |
| 1943 |
break; |
| 1944 |
} |
| 1945 |
} |
| 1946 |
scope = parentScope; |
| 1947 |
parentScope = parentScope.getParentScope(); |
| 1948 |
if (parentScope == null) { |
| 1949 |
result = topScopeName(cx, scope, name); |
| 1950 |
if (result == Scriptable.NOT_FOUND) { |
| 1951 |
if (firstXMLObject == null || asFunctionCall) { |
| 1952 |
throw notFoundError(scope, name); |
| 1953 |
} |
| 1954 |
|
| 1955 |
|
| 1956 |
|
| 1957 |
|
| 1958 |
result = firstXMLObject.get(name, firstXMLObject); |
| 1959 |
} |
| 1960 |
|
| 1961 |
thisObj = scope; |
| 1962 |
break; |
| 1963 |
} |
| 1964 |
} |
| 1965 |
|
| 1966 |
if (asFunctionCall) { |
| 1967 |
if (!(result instanceof Callable)) { |
| 1968 |
throw notFunctionError(result, name); |
| 1969 |
} |
| 1970 |
storeScriptable(cx, thisObj); |
| 1971 |
} |
| 1972 |
|
| 1973 |
return result; |
| 1974 |
} |
| 1975 |
|
| 1976 |
private static Object topScopeName(Context cx, Scriptable scope, |
| 1977 |
String name) |
| 1978 |
{ |
| 1979 |
if (cx.useDynamicScope) { |
| 1980 |
scope = checkDynamicScope(cx.topCallScope, scope); |
| 1981 |
} |
| 1982 |
return ScriptableObject.getProperty(scope, name); |
| 1983 |
} |
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
public static Scriptable bind(Context cx, Scriptable scope, String id) |
| 2000 |
{ |
| 2001 |
Scriptable firstXMLObject = null; |
| 2002 |
Scriptable parent = scope.getParentScope(); |
| 2003 |
childScopesChecks: if (parent != null) { |
| 2004 |
|
| 2005 |
while (scope instanceof NativeWith) { |
| 2006 |
Scriptable withObj = scope.getPrototype(); |
| 2007 |
if (withObj instanceof XMLObject) { |
| 2008 |
XMLObject xmlObject = (XMLObject)withObj; |
| 2009 |
if (xmlObject.has(cx, id)) { |
| 2010 |
return xmlObject; |
| 2011 |
} |
| 2012 |
if (firstXMLObject == null) { |
| 2013 |
firstXMLObject = xmlObject; |
| 2014 |
} |
| 2015 |
} else { |
| 2016 |
if (ScriptableObject.hasProperty(withObj, id)) { |
| 2017 |
return withObj; |
| 2018 |
} |
| 2019 |
} |
| 2020 |
scope = parent; |
| 2021 |
parent = parent.getParentScope(); |
| 2022 |
if (parent == null) { |
| 2023 |
break childScopesChecks; |
| 2024 |
} |
| 2025 |
} |
| 2026 |
for (;;) { |
| 2027 |
if (ScriptableObject.hasProperty(scope, id)) { |
| 2028 |
return scope; |
| 2029 |
} |
| 2030 |
scope = parent; |
| 2031 |
parent = parent.getParentScope(); |
| 2032 |
if (parent == null) { |
| 2033 |
break childScopesChecks; |
| 2034 |
} |
| 2035 |
} |
| 2036 |
} |
| 2037 |
|
| 2038 |
if (cx.useDynamicScope) { |
| 2039 |
scope = checkDynamicScope(cx.topCallScope, scope); |
| 2040 |
} |
| 2041 |
if (ScriptableObject.hasProperty(scope, id)) { |
| 2042 |
return scope; |
| 2043 |
} |
| 2044 |
|
| 2045 |
|
| 2046 |
return firstXMLObject; |
| 2047 |
} |
| 2048 |
|
| 2049 |
public static Object setName(Scriptable bound, Object value, |
| 2050 |
Context cx, Scriptable scope, String id) |
| 2051 |
{ |
| 2052 |
if (bound != null) { |
| 2053 |
|
| 2054 |
|
| 2055 |
ScriptableObject.putProperty(bound, id, value); |
| 2056 |
} else { |
| 2057 |
|
| 2058 |
|
| 2059 |
|
| 2060 |
if (cx.hasFeature(Context.FEATURE_STRICT_MODE) || |
| 2061 |
cx.hasFeature(Context.FEATURE_STRICT_VARS)) |
| 2062 |
{ |
| 2063 |
Context.reportWarning( |
| 2064 |
ScriptRuntime.getMessage1("msg.assn.create.strict", id)); |
| 2065 |
} |
| 2066 |
|
| 2067 |
bound = ScriptableObject.getTopLevelScope(scope); |
| 2068 |
if (cx.useDynamicScope) { |
| 2069 |
bound = checkDynamicScope(cx.topCallScope, bound); |
| 2070 |
} |
| 2071 |
bound.put(id, bound, value); |
| 2072 |
} |
| 2073 |
return value; |
| 2074 |
} |
| 2075 |
|
| 2076 |
public static Object strictSetName(Scriptable bound, Object value, |
| 2077 |
Context cx, Scriptable scope, String id) { |
| 2078 |
if (bound != null) { |
| 2079 |
|
| 2080 |
|
| 2081 |
|
| 2082 |
|
| 2083 |
|
| 2084 |
|
| 2085 |
|
| 2086 |
|
| 2087 |
ScriptableObject.putProperty(bound, id, value); |
| 2088 |
return value; |
| 2089 |
} else { |
| 2090 |
|
| 2091 |
String msg = "Assignment to undefined \"" + id + "\" in strict mode"; |
| 2092 |
throw constructError("ReferenceError", msg); |
| 2093 |
} |
| 2094 |
} |
| 2095 |
|
| 2096 |
public static Object setConst(Scriptable bound, Object value, |
| 2097 |
Context cx, String id) |
| 2098 |
{ |
| 2099 |
if (bound instanceof XMLObject) { |
| 2100 |
bound.put(id, bound, value); |
| 2101 |
} else { |
| 2102 |
ScriptableObject.putConstProperty(bound, id, value); |
| 2103 |
} |
| 2104 |
return value; |
| 2105 |
} |
| 2106 |
|
| 2107 |
|
| 2108 |
|
| 2109 |
|
| 2110 |
|
| 2111 |
|
| 2112 |
|
| 2113 |
|
| 2114 |
|
| 2115 |
|
| 2116 |
|
| 2117 |
|
| 2118 |
|
| 2119 |
|
| 2120 |
|
| 2121 |
|
| 2122 |
private static class IdEnumeration implements Serializable |
| 2123 |
{ |
| 2124 |
private static final long serialVersionUID = 1L; |
| 2125 |
Scriptable obj; |
| 2126 |
Object[] ids; |
| 2127 |
int index; |
| 2128 |
ObjToIntMap used; |
| 2129 |
Object currentId; |
| 2130 |
int enumType; |
| 2131 |
|
| 2132 |
|
| 2133 |
|
| 2134 |
boolean enumNumbers; |
| 2135 |
|
| 2136 |
Scriptable iterator; |
| 2137 |
} |
| 2138 |
|
| 2139 |
public static Scriptable toIterator(Context cx, Scriptable scope, |
| 2140 |
Scriptable obj, boolean keyOnly) |
| 2141 |
{ |
| 2142 |
if (ScriptableObject.hasProperty(obj, |
| 2143 |
NativeIterator.ITERATOR_PROPERTY_NAME)) |
| 2144 |
{ |
| 2145 |
Object v = ScriptableObject.getProperty(obj, |
| 2146 |
NativeIterator.ITERATOR_PROPERTY_NAME); |
| 2147 |
if (!(v instanceof Callable)) { |
| 2148 |
throw typeError0("msg.invalid.iterator"); |
| 2149 |
} |
| 2150 |
Callable f = (Callable) v; |
| 2151 |
Object[] args = new Object[] { keyOnly ? Boolean.TRUE |
| 2152 |
: Boolean.FALSE }; |
| 2153 |
v = f.call(cx, scope, obj, args); |
| 2154 |
if (!(v instanceof Scriptable)) { |
| 2155 |
throw typeError0("msg.iterator.primitive"); |
| 2156 |
} |
| 2157 |
return (Scriptable) v; |
| 2158 |
} |
| 2159 |
return null; |
| 2160 |
} |
| 2161 |
|
| 2162 |
|
| 2163 |
|
| 2164 |
|
| 2165 |
|
| 2166 |
|
| 2167 |
@Deprecated |
| 2168 |
public static Object enumInit(Object value, Context cx, boolean enumValues) |
| 2169 |
{ |
| 2170 |
return enumInit(value, cx, enumValues ? ENUMERATE_VALUES |
| 2171 |
: ENUMERATE_KEYS); |
| 2172 |
} |
| 2173 |
|
| 2174 |
public static final int ENUMERATE_KEYS = 0; |
| 2175 |
public static final int ENUMERATE_VALUES = 1; |
| 2176 |
public static final int ENUMERATE_ARRAY = 2; |
| 2177 |
public static final int ENUMERATE_KEYS_NO_ITERATOR = 3; |
| 2178 |
public static final int ENUMERATE_VALUES_NO_ITERATOR = 4; |
| 2179 |
public static final int ENUMERATE_ARRAY_NO_ITERATOR = 5; |
| 2180 |
public static final int ENUMERATE_VALUES_IN_ORDER = 6; |
| 2181 |
|
| 2182 |
|
| 2183 |
|
| 2184 |
|
| 2185 |
@Deprecated |
| 2186 |
public static Object enumInit(Object value, Context cx, int enumType) |
| 2187 |
{ |
| 2188 |
return enumInit(value, cx, getTopCallScope(cx), enumType); |
| 2189 |
} |
| 2190 |
|
| 2191 |
public static Object enumInit(Object value, Context cx, Scriptable scope, |
| 2192 |
int enumType) |
| 2193 |
{ |
| 2194 |
IdEnumeration x = new IdEnumeration(); |
| 2195 |
x.obj = toObjectOrNull(cx, value, scope); |
| 2196 |
|
| 2197 |
if (enumType == ENUMERATE_VALUES_IN_ORDER) { |
| 2198 |
x.enumType = enumType; |
| 2199 |
x.iterator = null; |
| 2200 |
return enumInitInOrder(cx, x); |
| 2201 |
} |
| 2202 |
if (x.obj == null) { |
| 2203 |
|
| 2204 |
|
| 2205 |
return x; |
| 2206 |
} |
| 2207 |
x.enumType = enumType; |
| 2208 |
x.iterator = null; |
| 2209 |
if (enumType != ENUMERATE_KEYS_NO_ITERATOR && |
| 2210 |
enumType != ENUMERATE_VALUES_NO_ITERATOR && |
| 2211 |
enumType != ENUMERATE_ARRAY_NO_ITERATOR) |
| 2212 |
{ |
| 2213 |
x.iterator = toIterator(cx, x.obj.getParentScope(), x.obj, |
| 2214 |
enumType == ScriptRuntime.ENUMERATE_KEYS); |
| 2215 |
} |
| 2216 |
if (x.iterator == null) { |
| 2217 |
|
| 2218 |
|
| 2219 |
enumChangeObject(x); |
| 2220 |
} |
| 2221 |
|
| 2222 |
return x; |
| 2223 |
} |
| 2224 |
|
| 2225 |
private static Object enumInitInOrder(Context cx, IdEnumeration x) { |
| 2226 |
if (!(x.obj instanceof ScriptableObject)) { |
| 2227 |
throw typeError1("msg.not.iterable", toString(x.obj)); |
| 2228 |
} |
| 2229 |
|
| 2230 |
ScriptableObject xo = (ScriptableObject)x.obj; |
| 2231 |
if (!ScriptableObject.hasProperty(xo, SymbolKey.ITERATOR)) { |
| 2232 |
throw typeError1("msg.not.iterable", toString(x.obj)); |
| 2233 |
} |
| 2234 |
Object iterator = ScriptableObject.getProperty(xo, SymbolKey.ITERATOR); |
| 2235 |
if (!(iterator instanceof Callable)) { |
| 2236 |
throw typeError1("msg.not.iterable", toString(x.obj)); |
| 2237 |
} |
| 2238 |
Callable f = (Callable) iterator; |
| 2239 |
Scriptable scope = x.obj.getParentScope(); |
| 2240 |
Object[] args = new Object[] {}; |
| 2241 |
Object v = f.call(cx, scope, x.obj, args); |
| 2242 |
if (!(v instanceof Scriptable)) { |
| 2243 |
throw typeError1("msg.not.iterable", toString(x.obj)); |
| 2244 |
} |
| 2245 |
x.iterator = (Scriptable) v; |
| 2246 |
return x; |
| 2247 |
} |
| 2248 |
|
| 2249 |
public static void setEnumNumbers(Object enumObj, boolean enumNumbers) { |
| 2250 |
((IdEnumeration)enumObj).enumNumbers = enumNumbers; |
| 2251 |
} |
| 2252 |
|
| 2253 |
public static Boolean enumNext(Object enumObj) |
| 2254 |
{ |
| 2255 |
IdEnumeration x = (IdEnumeration)enumObj; |
| 2256 |
if (x.iterator != null) { |
| 2257 |
if (x.enumType == ENUMERATE_VALUES_IN_ORDER) { |
| 2258 |
return enumNextInOrder(x); |
| 2259 |
} |
| 2260 |
Object v = ScriptableObject.getProperty(x.iterator, "next"); |
| 2261 |
if (!(v instanceof Callable)) |
| 2262 |
return Boolean.FALSE; |
| 2263 |
Callable f = (Callable) v; |
| 2264 |
Context cx = Context.getContext(); |
| 2265 |
try { |
| 2266 |
x.currentId = f.call(cx, x.iterator.getParentScope(), |
| 2267 |
x.iterator, emptyArgs); |
| 2268 |
return Boolean.TRUE; |
| 2269 |
} catch (JavaScriptException e) { |
| 2270 |
if (e.getValue() instanceof NativeIterator.StopIteration) { |
| 2271 |
return Boolean.FALSE; |
| 2272 |
} |
| 2273 |
throw e; |
| 2274 |
} |
| 2275 |
} |
| 2276 |
for (;;) { |
| 2277 |
if (x.obj == null) { |
| 2278 |
return Boolean.FALSE; |
| 2279 |
} |
| 2280 |
if (x.index == x.ids.length) { |
| 2281 |
x.obj = x.obj.getPrototype(); |
| 2282 |
enumChangeObject(x); |
| 2283 |
continue; |
| 2284 |
} |
| 2285 |
Object id = x.ids[x.index++]; |
| 2286 |
if (x.used != null && x.used.has(id)) { |
| 2287 |
continue; |
| 2288 |
} |
| 2289 |
if (id instanceof Symbol) { |
| 2290 |
continue; |
| 2291 |
} else if (id instanceof String) { |
| 2292 |
String strId = (String) id; |
| 2293 |
if (!x.obj.has(strId, x.obj)) |
| 2294 |
continue; |
| 2295 |
x.currentId = strId; |
| 2296 |
} else { |
| 2297 |
int intId = ((Number)id).intValue(); |
| 2298 |
if (!x.obj.has(intId, x.obj)) |
| 2299 |
continue; |
| 2300 |
x.currentId = x.enumNumbers ? (Object) (Integer.valueOf(intId)) |
| 2301 |
: String.valueOf(intId); |
| 2302 |
} |
| 2303 |
return Boolean.TRUE; |
| 2304 |
} |
| 2305 |
} |
| 2306 |
|
| 2307 |
private static Boolean enumNextInOrder(IdEnumeration enumObj) |
| 2308 |
{ |
| 2309 |
Object v = ScriptableObject.getProperty(enumObj.iterator, ES6Iterator.NEXT_METHOD); |
| 2310 |
if (!(v instanceof Callable)) { |
| 2311 |
throw notFunctionError(enumObj.iterator, ES6Iterator.NEXT_METHOD); |
| 2312 |
} |
| 2313 |
Callable f = (Callable) v; |
| 2314 |
Context cx = Context.getContext(); |
| 2315 |
Scriptable scope = enumObj.iterator.getParentScope(); |
| 2316 |
Object r = f.call(cx, scope, enumObj.iterator, emptyArgs); |
| 2317 |
Scriptable iteratorResult = toObject(cx, scope, r); |
| 2318 |
Object done = ScriptableObject.getProperty(iteratorResult, ES6Iterator.DONE_PROPERTY); |
| 2319 |
if (done != ScriptableObject.NOT_FOUND && toBoolean(done)) { |
| 2320 |
return Boolean.FALSE; |
| 2321 |
} |
| 2322 |
enumObj.currentId = ScriptableObject.getProperty(iteratorResult, ES6Iterator.VALUE_PROPERTY); |
| 2323 |
return Boolean.TRUE; |
| 2324 |
} |
| 2325 |
|
| 2326 |
public static Object enumId(Object enumObj, Context cx) |
| 2327 |
{ |
| 2328 |
IdEnumeration x = (IdEnumeration)enumObj; |
| 2329 |
if (x.iterator != null) { |
| 2330 |
return x.currentId; |
| 2331 |
} |
| 2332 |
switch (x.enumType) { |
| 2333 |
case ENUMERATE_KEYS: |
| 2334 |
case ENUMERATE_KEYS_NO_ITERATOR: |
| 2335 |
return x.currentId; |
| 2336 |
case ENUMERATE_VALUES: |
| 2337 |
case ENUMERATE_VALUES_NO_ITERATOR: |
| 2338 |
return enumValue(enumObj, cx); |
| 2339 |
case ENUMERATE_ARRAY: |
| 2340 |
case ENUMERATE_ARRAY_NO_ITERATOR: |
| 2341 |
Object[] elements = { x.currentId, enumValue(enumObj, cx) }; |
| 2342 |
return cx.newArray(ScriptableObject.getTopLevelScope(x.obj), elements); |
| 2343 |
default: |
| 2344 |
throw Kit.codeBug(); |
| 2345 |
} |
| 2346 |
} |
| 2347 |
|
| 2348 |
public static Object enumValue(Object enumObj, Context cx) { |
| 2349 |
IdEnumeration x = (IdEnumeration)enumObj; |
| 2350 |
|
| 2351 |
Object result; |
| 2352 |
|
| 2353 |
if (isSymbol(x.currentId)) { |
| 2354 |
SymbolScriptable so = ScriptableObject.ensureSymbolScriptable(x.obj); |
| 2355 |
result = so.get((Symbol)x.currentId, x.obj); |
| 2356 |
} else { |
| 2357 |
String s = toStringIdOrIndex(cx, x.currentId); |
| 2358 |
if (s == null) { |
| 2359 |
int index = lastIndexResult(cx); |
| 2360 |
result = x.obj.get(index, x.obj); |
| 2361 |
} else { |
| 2362 |
result = x.obj.get(s, x.obj); |
| 2363 |
} |
| 2364 |
} |
| 2365 |
|
| 2366 |
return result; |
| 2367 |
} |
| 2368 |
|
| 2369 |
private static void enumChangeObject(IdEnumeration x) |
| 2370 |
{ |
| 2371 |
Object[] ids = null; |
| 2372 |
while (x.obj != null) { |
| 2373 |
ids = x.obj.getIds(); |
| 2374 |
if (ids.length != 0) { |
| 2375 |
break; |
| 2376 |
} |
| 2377 |
x.obj = x.obj.getPrototype(); |
| 2378 |
} |
| 2379 |
if (x.obj != null && x.ids != null) { |
| 2380 |
Object[] previous = x.ids; |
| 2381 |
int L = previous.length; |
| 2382 |
if (x.used == null) { |
| 2383 |
x.used = new ObjToIntMap(L); |
| 2384 |
} |
| 2385 |
for (int i = 0; i != L; ++i) { |
| 2386 |
x.used.intern(previous[i]); |
| 2387 |
} |
| 2388 |
} |
| 2389 |
x.ids = ids; |
| 2390 |
x.index = 0; |
| 2391 |
} |
| 2392 |
|
| 2393 |
|
| 2394 |
|
| 2395 |
|
| 2396 |
|
| 2397 |
|
| 2398 |
|
| 2399 |
|
| 2400 |
public static Callable getNameFunctionAndThis(String name, |
| 2401 |
Context cx, |
| 2402 |
Scriptable scope) |
| 2403 |
{ |
| 2404 |
Scriptable parent = scope.getParentScope(); |
| 2405 |
if (parent == null) { |
| 2406 |
Object result = topScopeName(cx, scope, name); |
| 2407 |
if (!(result instanceof Callable)) { |
| 2408 |
if (result == Scriptable.NOT_FOUND) { |
| 2409 |
throw notFoundError(scope, name); |
| 2410 |
} else { |
| 2411 |
throw notFunctionError(result, name); |
| 2412 |
} |
| 2413 |
} |
| 2414 |
|
| 2415 |
Scriptable thisObj = scope; |
| 2416 |
storeScriptable(cx, thisObj); |
| 2417 |
return (Callable)result; |
| 2418 |
} |
| 2419 |
|
| 2420 |
|
| 2421 |
return (Callable)nameOrFunction(cx, scope, parent, name, true); |
| 2422 |
} |
| 2423 |
|
| 2424 |
|
| 2425 |
|
| 2426 |
|
| 2427 |
|
| 2428 |
|
| 2429 |
|
| 2430 |
|
| 2431 |
|
| 2432 |
|
| 2433 |
@Deprecated |
| 2434 |
public static Callable getElemFunctionAndThis(Object obj, |
| 2435 |
Object elem, |
| 2436 |
Context cx) |
| 2437 |
{ |
| 2438 |
return getElemFunctionAndThis(obj, elem, cx, getTopCallScope(cx)); |
| 2439 |
} |
| 2440 |
|
| 2441 |
|
| 2442 |
|
| 2443 |
|
| 2444 |
|
| 2445 |
|
| 2446 |
|
| 2447 |
|
| 2448 |
public static Callable getElemFunctionAndThis(Object obj, Object elem, |
| 2449 |
Context cx, Scriptable scope) |
| 2450 |
{ |
| 2451 |
Scriptable thisObj; |
| 2452 |
Object value; |
| 2453 |
|
| 2454 |
if (isSymbol(elem)) { |
| 2455 |
thisObj = toObjectOrNull(cx, obj, scope); |
| 2456 |
if (thisObj == null) { |
| 2457 |
throw undefCallError(obj, String.valueOf(elem)); |
| 2458 |
} |
| 2459 |
value = ScriptableObject.getProperty(thisObj, (Symbol)elem); |
| 2460 |
|
| 2461 |
} else { |
| 2462 |
String str = toStringIdOrIndex(cx, elem); |
| 2463 |
if (str != null) { |
| 2464 |
return getPropFunctionAndThis(obj, str, cx, scope); |
| 2465 |
} |
| 2466 |
int index = lastIndexResult(cx); |
| 2467 |
|
| 2468 |
thisObj = toObjectOrNull(cx, obj, scope); |
| 2469 |
if (thisObj == null) { |
| 2470 |
throw undefCallError(obj, String.valueOf(elem)); |
| 2471 |
} |
| 2472 |
|
| 2473 |
value = ScriptableObject.getProperty(thisObj, index); |
| 2474 |
} |
| 2475 |
|
| 2476 |
if (!(value instanceof Callable)) { |
| 2477 |
throw notFunctionError(value, elem); |
| 2478 |
} |
| 2479 |
|
| 2480 |
storeScriptable(cx, thisObj); |
| 2481 |
return (Callable)value; |
| 2482 |
} |
| 2483 |
|
| 2484 |
|
| 2485 |
|
| 2486 |
|
| 2487 |
|
| 2488 |
|
| 2489 |
|
| 2490 |
|
| 2491 |
|
| 2492 |
|
| 2493 |
|
| 2494 |
|
| 2495 |
@Deprecated |
| 2496 |
public static Callable getPropFunctionAndThis(Object obj, |
| 2497 |
String property, |
| 2498 |
Context cx) |
| 2499 |
{ |
| 2500 |
return getPropFunctionAndThis(obj, property, cx, getTopCallScope(cx)); |
| 2501 |
} |
| 2502 |
|
| 2503 |
|
| 2504 |
|
| 2505 |
|
| 2506 |
|
| 2507 |
|
| 2508 |
|
| 2509 |
|
| 2510 |
public static Callable getPropFunctionAndThis(Object obj, |
| 2511 |
String property, |
| 2512 |
Context cx, Scriptable scope) |
| 2513 |
{ |
| 2514 |
Scriptable thisObj = toObjectOrNull(cx, obj, scope); |
| 2515 |
return getPropFunctionAndThisHelper(obj, property, cx, thisObj); |
| 2516 |
} |
| 2517 |
|
| 2518 |
private static Callable getPropFunctionAndThisHelper(Object obj, |
| 2519 |
String property, Context cx, Scriptable thisObj) |
| 2520 |
{ |
| 2521 |
if (thisObj == null) { |
| 2522 |
throw undefCallError(obj, property); |
| 2523 |
} |
| 2524 |
|
| 2525 |
Object value = ScriptableObject.getProperty(thisObj, property); |
| 2526 |
if (!(value instanceof Callable)) { |
| 2527 |
Object noSuchMethod = ScriptableObject.getProperty(thisObj, "__noSuchMethod__"); |
| 2528 |
if (noSuchMethod instanceof Callable) |
| 2529 |
value = new NoSuchMethodShim((Callable)noSuchMethod, property); |
| 2530 |
} |
| 2531 |
|
| 2532 |
if (!(value instanceof Callable)) { |
| 2533 |
throw notFunctionError(thisObj, value, property); |
| 2534 |
} |
| 2535 |
|
| 2536 |
storeScriptable(cx, thisObj); |
| 2537 |
return (Callable)value; |
| 2538 |
} |
| 2539 |
|
| 2540 |
|
| 2541 |
|
| 2542 |
|
| 2543 |
|
| 2544 |
|
| 2545 |
|
| 2546 |
|
| 2547 |
public static Callable getValueFunctionAndThis(Object value, Context cx) |
| 2548 |
{ |
| 2549 |
if (!(value instanceof Callable)) { |
| 2550 |
throw notFunctionError(value); |
| 2551 |
} |
| 2552 |
|
| 2553 |
Callable f = (Callable)value; |
| 2554 |
Scriptable thisObj = null; |
| 2555 |
if (f instanceof Scriptable) { |
| 2556 |
thisObj = ((Scriptable)f).getParentScope(); |
| 2557 |
} |
| 2558 |
if (thisObj == null) { |
| 2559 |
if (cx.topCallScope == null) throw new IllegalStateException(); |
| 2560 |
thisObj = cx.topCallScope; |
| 2561 |
} |
| 2562 |
if (thisObj.getParentScope() != null) { |
| 2563 |
if (thisObj instanceof NativeWith) { |
| 2564 |
|
| 2565 |
|
| 2566 |
} else if (thisObj instanceof NativeCall) { |
| 2567 |
|
| 2568 |
thisObj = ScriptableObject.getTopLevelScope(thisObj); |
| 2569 |
} |
| 2570 |
} |
| 2571 |
storeScriptable(cx, thisObj); |
| 2572 |
return f; |
| 2573 |
} |
| 2574 |
|
| 2575 |
|
| 2576 |
|
| 2577 |
|
| 2578 |
|
| 2579 |
|
| 2580 |
|
| 2581 |
|
| 2582 |
|
| 2583 |
|
| 2584 |
public static Ref callRef(Callable function, Scriptable thisObj, |
| 2585 |
Object[] args, Context cx) |
| 2586 |
{ |
| 2587 |
if (function instanceof RefCallable) { |
| 2588 |
RefCallable rfunction = (RefCallable)function; |
| 2589 |
Ref ref = rfunction.refCall(cx, thisObj, args); |
| 2590 |
if (ref == null) { |
| 2591 |
throw new IllegalStateException(rfunction.getClass().getName()+".refCall() returned null"); |
| 2592 |
} |
| 2593 |
return ref; |
| 2594 |
} |
| 2595 |
|
| 2596 |
String msg = getMessage1("msg.no.ref.from.function", |
| 2597 |
toString(function)); |
| 2598 |
throw constructError("ReferenceError", msg); |
| 2599 |
} |
| 2600 |
|
| 2601 |
|
| 2602 |
|
| 2603 |
|
| 2604 |
|
| 2605 |
|
| 2606 |
public static Scriptable newObject(Object fun, Context cx, |
| 2607 |
Scriptable scope, Object[] args) |
| 2608 |
{ |
| 2609 |
if (!(fun instanceof Function)) { |
| 2610 |
throw notFunctionError(fun); |
| 2611 |
} |
| 2612 |
Function function = (Function)fun; |
| 2613 |
return function.construct(cx, scope, args); |
| 2614 |
} |
| 2615 |
|
| 2616 |
public static Object callSpecial(Context cx, Callable fun, |
| 2617 |
Scriptable thisObj, |
| 2618 |
Object[] args, Scriptable scope, |
| 2619 |
Scriptable callerThis, int callType, |
| 2620 |
String filename, int lineNumber) |
| 2621 |
{ |
| 2622 |
if (callType == Node.SPECIALCALL_EVAL) { |
| 2623 |
if (thisObj.getParentScope() == null && NativeGlobal.isEvalFunction(fun)) { |
| 2624 |
return evalSpecial(cx, scope, callerThis, args, |
| 2625 |
filename, lineNumber); |
| 2626 |
} |
| 2627 |
} else if (callType == Node.SPECIALCALL_WITH) { |
| 2628 |
if (NativeWith.isWithFunction(fun)) { |
| 2629 |
throw Context.reportRuntimeError1("msg.only.from.new", |
| 2630 |
"With"); |
| 2631 |
} |
| 2632 |
} else { |
| 2633 |
throw Kit.codeBug(); |
| 2634 |
} |
| 2635 |
|
| 2636 |
return fun.call(cx, scope, thisObj, args); |
| 2637 |
} |
| 2638 |
|
| 2639 |
public static Object newSpecial(Context cx, Object fun, |
| 2640 |
Object[] args, Scriptable scope, |
| 2641 |
int callType) |
| 2642 |
{ |
| 2643 |
if (callType == Node.SPECIALCALL_EVAL) { |
| 2644 |
if (NativeGlobal.isEvalFunction(fun)) { |
| 2645 |
throw typeError1("msg.not.ctor", "eval"); |
| 2646 |
} |
| 2647 |
} else if (callType == Node.SPECIALCALL_WITH) { |
| 2648 |
if (NativeWith.isWithFunction(fun)) { |
| 2649 |
return NativeWith.newWithSpecial(cx, scope, args); |
| 2650 |
} |
| 2651 |
} else { |
| 2652 |
throw Kit.codeBug(); |
| 2653 |
} |
| 2654 |
|
| 2655 |
return newObject(fun, cx, scope, args); |
| 2656 |
} |
| 2657 |
|
| 2658 |
|
| 2659 |
|
| 2660 |
|
| 2661 |
|
| 2662 |
|
| 2663 |
public static Object applyOrCall(boolean isApply, |
| 2664 |
Context cx, Scriptable scope, |
| 2665 |
Scriptable thisObj, Object[] args) |
| 2666 |
{ |
| 2667 |
int L = args.length; |
| 2668 |
Callable function = getCallable(thisObj); |
| 2669 |
|
| 2670 |
Scriptable callThis = null; |
| 2671 |
if (L != 0) { |
| 2672 |
if (cx.hasFeature(Context.FEATURE_OLD_UNDEF_NULL_THIS)) { |
| 2673 |
callThis = toObjectOrNull(cx, args[0], scope); |
| 2674 |
} else { |
| 2675 |
callThis = args[0] == Undefined.instance ? Undefined.SCRIPTABLE_UNDEFINED : toObjectOrNull(cx, args[0], scope); |
| 2676 |
} |
| 2677 |
} |
| 2678 |
if (callThis == null && cx.hasFeature(Context.FEATURE_OLD_UNDEF_NULL_THIS)) { |
| 2679 |
callThis = getTopCallScope(cx); |
| 2680 |
} |
| 2681 |
|
| 2682 |
Object[] callArgs; |
| 2683 |
if (isApply) { |
| 2684 |
|
| 2685 |
callArgs = L <= 1 ? ScriptRuntime.emptyArgs : |
| 2686 |
getApplyArguments(cx, args[1]); |
| 2687 |
} else { |
| 2688 |
|
| 2689 |
if (L <= 1) { |
| 2690 |
callArgs = ScriptRuntime.emptyArgs; |
| 2691 |
} else { |
| 2692 |
callArgs = new Object[L - 1]; |
| 2693 |
System.arraycopy(args, 1, callArgs, 0, L - 1); |
| 2694 |
} |
| 2695 |
} |
| 2696 |
|
| 2697 |
return function.call(cx, scope, callThis, callArgs); |
| 2698 |
} |
| 2699 |
|
| 2700 |
static Object[] getApplyArguments(Context cx, Object arg1) |
| 2701 |
{ |
| 2702 |
if (arg1 == null || arg1 == Undefined.instance) { |
| 2703 |
return ScriptRuntime.emptyArgs; |
| 2704 |
} else if (arg1 instanceof NativeArray || arg1 instanceof Arguments) { |
| 2705 |
return cx.getElements((Scriptable) arg1); |
| 2706 |
} else { |
| 2707 |
throw ScriptRuntime.typeError0("msg.arg.isnt.array"); |
| 2708 |
} |
| 2709 |
} |
| 2710 |
|
| 2711 |
static Callable getCallable(Scriptable thisObj) |
| 2712 |
{ |
| 2713 |
Callable function; |
| 2714 |
if (thisObj instanceof Callable) { |
| 2715 |
function = (Callable)thisObj; |
| 2716 |
} else { |
| 2717 |
Object value = thisObj.getDefaultValue(ScriptRuntime.FunctionClass); |
| 2718 |
if (!(value instanceof Callable)) { |
| 2719 |
throw ScriptRuntime.notFunctionError(value, thisObj); |
| 2720 |
} |
| 2721 |
function = (Callable)value; |
| 2722 |
} |
| 2723 |
return function; |
| 2724 |
} |
| 2725 |
|
| 2726 |
|
| 2727 |
|
| 2728 |
|
| 2729 |
|
| 2730 |
|
| 2731 |
public static Object evalSpecial(Context cx, Scriptable scope, |
| 2732 |
Object thisArg, Object[] args, |
| 2733 |
String filename, int lineNumber) |
| 2734 |
{ |
| 2735 |
if (args.length < 1) |
| 2736 |
return Undefined.instance; |
| 2737 |
Object x = args[0]; |
| 2738 |
if (!(x instanceof CharSequence)) { |
| 2739 |
if (cx.hasFeature(Context.FEATURE_STRICT_MODE) || |
| 2740 |
cx.hasFeature(Context.FEATURE_STRICT_EVAL)) |
| 2741 |
{ |
| 2742 |
throw Context.reportRuntimeError0("msg.eval.nonstring.strict"); |
| 2743 |
} |
| 2744 |
String message = ScriptRuntime.getMessage0("msg.eval.nonstring"); |
| 2745 |
Context.reportWarning(message); |
| 2746 |
return x; |
| 2747 |
} |
| 2748 |
if (filename == null) { |
| 2749 |
int[] linep = new int[1]; |
| 2750 |
filename = Context.getSourcePositionFromStack(linep); |
| 2751 |
if (filename != null) { |
| 2752 |
lineNumber = linep[0]; |
| 2753 |
} else { |
| 2754 |
filename = ""; |
| 2755 |
} |
| 2756 |
} |
| 2757 |
String sourceName = ScriptRuntime. |
| 2758 |
makeUrlForGeneratedScript(true, filename, lineNumber); |
| 2759 |
|
| 2760 |
ErrorReporter reporter; |
| 2761 |
reporter = DefaultErrorReporter.forEval(cx.getErrorReporter()); |
| 2762 |
|
| 2763 |
Evaluator evaluator = Context.createInterpreter(); |
| 2764 |
if (evaluator == null) { |
| 2765 |
throw new JavaScriptException("Interpreter not present", |
| 2766 |
filename, lineNumber); |
| 2767 |
} |
| 2768 |
|
| 2769 |
|
| 2770 |
|
| 2771 |
Script script = cx.compileString(x.toString(), evaluator, |
| 2772 |
reporter, sourceName, 1, null); |
| 2773 |
evaluator.setEvalScriptFlag(script); |
| 2774 |
Callable c = (Callable)script; |
| 2775 |
return c.call(cx, scope, (Scriptable)thisArg, ScriptRuntime.emptyArgs); |
| 2776 |
} |
| 2777 |
|
| 2778 |
|
| 2779 |
|
| 2780 |
|
| 2781 |
public static String typeof(Object value) |
| 2782 |
{ |
| 2783 |
if (value == null) |
| 2784 |
return "object"; |
| 2785 |
if (value == Undefined.instance) |
| 2786 |
return "undefined"; |
| 2787 |
if (value instanceof ScriptableObject) |
| 2788 |
return ((ScriptableObject) value).getTypeOf(); |
| 2789 |
if (value instanceof Scriptable) |
| 2790 |
return (value instanceof Callable) ? "function" : "object"; |
| 2791 |
if (value instanceof CharSequence) |
| 2792 |
return "string"; |
| 2793 |
if (value instanceof Number) |
| 2794 |
return "number"; |
| 2795 |
if (value instanceof Boolean) |
| 2796 |
return "boolean"; |
| 2797 |
throw errorWithClassName("msg.invalid.type", value); |
| 2798 |
} |
| 2799 |
|
| 2800 |
|
| 2801 |
|
| 2802 |
|
| 2803 |
public static String typeofName(Scriptable scope, String id) |
| 2804 |
{ |
| 2805 |
Context cx = Context.getContext(); |
| 2806 |
Scriptable val = bind(cx, scope, id); |
| 2807 |
if (val == null) |
| 2808 |
return "undefined"; |
| 2809 |
return typeof(getObjectProp(val, id, cx)); |
| 2810 |
} |
| 2811 |
|
| 2812 |
|
| 2813 |
|
| 2814 |
|
| 2815 |
|
| 2816 |
|
| 2817 |
|
| 2818 |
|
| 2819 |
|
| 2820 |
|
| 2821 |
|
| 2822 |
|
| 2823 |
|
| 2824 |
public static Object add(Object val1, Object val2, Context cx) |
| 2825 |
{ |
| 2826 |
if(val1 instanceof Number && val2 instanceof Number) { |
| 2827 |
return wrapNumber(((Number)val1).doubleValue() + |
| 2828 |
((Number)val2).doubleValue()); |
| 2829 |
} |
| 2830 |
if (val1 instanceof XMLObject) { |
| 2831 |
Object test = ((XMLObject)val1).addValues(cx, true, val2); |
| 2832 |
if (test != Scriptable.NOT_FOUND) { |
| 2833 |
return test; |
| 2834 |
} |
| 2835 |
} |
| 2836 |
if (val2 instanceof XMLObject) { |
| 2837 |
Object test = ((XMLObject)val2).addValues(cx, false, val1); |
| 2838 |
if (test != Scriptable.NOT_FOUND) { |
| 2839 |
return test; |
| 2840 |
} |
| 2841 |
} |
| 2842 |
if ((val1 instanceof Symbol) || (val2 instanceof Symbol)) { |
| 2843 |
throw typeError0("msg.not.a.number"); |
| 2844 |
} |
| 2845 |
if (val1 instanceof Scriptable) |
| 2846 |
val1 = ((Scriptable) val1).getDefaultValue(null); |
| 2847 |
if (val2 instanceof Scriptable) |
| 2848 |
val2 = ((Scriptable) val2).getDefaultValue(null); |
| 2849 |
if (!(val1 instanceof CharSequence) && !(val2 instanceof CharSequence)) |
| 2850 |
if ((val1 instanceof Number) && (val2 instanceof Number)) |
| 2851 |
return wrapNumber(((Number)val1).doubleValue() + |
| 2852 |
((Number)val2).doubleValue()); |
| 2853 |
else |
| 2854 |
return wrapNumber(toNumber(val1) + toNumber(val2)); |
| 2855 |
return new ConsString(toCharSequence(val1), toCharSequence(val2)); |
| 2856 |
} |
| 2857 |
|
| 2858 |
public static CharSequence add(CharSequence val1, Object val2) { |
| 2859 |
return new ConsString(val1, toCharSequence(val2)); |
| 2860 |
} |
| 2861 |
|
| 2862 |
public static CharSequence add(Object val1, CharSequence val2) { |
| 2863 |
return new ConsString(toCharSequence(val1), val2); |
| 2864 |
} |
| 2865 |
|
| 2866 |
|
| 2867 |
|
| 2868 |
|
| 2869 |
|
| 2870 |
|
| 2871 |
@Deprecated |
| 2872 |
public static Object nameIncrDecr(Scriptable scopeChain, String id, |
| 2873 |
int incrDecrMask) |
| 2874 |
{ |
| 2875 |
return nameIncrDecr(scopeChain, id, Context.getContext(), incrDecrMask); |
| 2876 |
} |
| 2877 |
|
| 2878 |
public static Object nameIncrDecr(Scriptable scopeChain, String id, |
| 2879 |
Context cx, int incrDecrMask) |
| 2880 |
{ |
| 2881 |
Scriptable target; |
| 2882 |
Object value; |
| 2883 |
search: { |
| 2884 |
do { |
| 2885 |
if (cx.useDynamicScope && scopeChain.getParentScope() == null) { |
| 2886 |
scopeChain = checkDynamicScope(cx.topCallScope, scopeChain); |
| 2887 |
} |
| 2888 |
target = scopeChain; |
| 2889 |
do { |
| 2890 |
if (target instanceof NativeWith && |
| 2891 |
target.getPrototype() instanceof XMLObject) { |
| 2892 |
break; |
| 2893 |
} |
| 2894 |
value = target.get(id, scopeChain); |
| 2895 |
if (value != Scriptable.NOT_FOUND) { |
| 2896 |
break search; |
| 2897 |
} |
| 2898 |
target = target.getPrototype(); |
| 2899 |
} while (target != null); |
| 2900 |
scopeChain = scopeChain.getParentScope(); |
| 2901 |
} while (scopeChain != null); |
| 2902 |
throw notFoundError(scopeChain, id); |
| 2903 |
} |
| 2904 |
return doScriptableIncrDecr(target, id, scopeChain, value, |
| 2905 |
incrDecrMask); |
| 2906 |
} |
| 2907 |
|
| 2908 |
|
| 2909 |
|
| 2910 |
|
| 2911 |
@Deprecated |
| 2912 |
public static Object propIncrDecr(Object obj, String id, |
| 2913 |
Context cx, int incrDecrMask) |
| 2914 |
{ |
| 2915 |
return propIncrDecr(obj, id, cx, getTopCallScope(cx), incrDecrMask); |
| 2916 |
} |
| 2917 |
|
| 2918 |
public static Object propIncrDecr(Object obj, String id, |
| 2919 |
Context cx, Scriptable scope, |
| 2920 |
int incrDecrMask) |
| 2921 |
{ |
| 2922 |
Scriptable start = toObjectOrNull(cx, obj, scope); |
| 2923 |
if (start == null) { |
| 2924 |
throw undefReadError(obj, id); |
| 2925 |
} |
| 2926 |
|
| 2927 |
Scriptable target = start; |
| 2928 |
Object value; |
| 2929 |
search: { |
| 2930 |
do { |
| 2931 |
value = target.get(id, start); |
| 2932 |
if (value != Scriptable.NOT_FOUND) { |
| 2933 |
break search; |
| 2934 |
} |
| 2935 |
target = target.getPrototype(); |
| 2936 |
} while (target != null); |
| 2937 |
start.put(id, start, NaNobj); |
| 2938 |
return NaNobj; |
| 2939 |
} |
| 2940 |
return doScriptableIncrDecr(target, id, start, value, |
| 2941 |
incrDecrMask); |
| 2942 |
} |
| 2943 |
|
| 2944 |
private static Object doScriptableIncrDecr(Scriptable target, |
| 2945 |
String id, |
| 2946 |
Scriptable protoChainStart, |
| 2947 |
Object value, |
| 2948 |
int incrDecrMask) |
| 2949 |
{ |
| 2950 |
boolean post = ((incrDecrMask & Node.POST_FLAG) != 0); |
| 2951 |
double number; |
| 2952 |
if (value instanceof Number) { |
| 2953 |
number = ((Number)value).doubleValue(); |
| 2954 |
} else { |
| 2955 |
number = toNumber(value); |
| 2956 |
if (post) { |
| 2957 |
|
| 2958 |
value = wrapNumber(number); |
| 2959 |
} |
| 2960 |
} |
| 2961 |
if ((incrDecrMask & Node.DECR_FLAG) == 0) { |
| 2962 |
++number; |
| 2963 |
} else { |
| 2964 |
--number; |
| 2965 |
} |
| 2966 |
Number result = wrapNumber(number); |
| 2967 |
target.put(id, protoChainStart, result); |
| 2968 |
if (post) { |
| 2969 |
return value; |
| 2970 |
} else { |
| 2971 |
return result; |
| 2972 |
} |
| 2973 |
} |
| 2974 |
|
| 2975 |
|
| 2976 |
|
| 2977 |
|
| 2978 |
@Deprecated |
| 2979 |
public static Object elemIncrDecr(Object obj, Object index, |
| 2980 |
Context cx, int incrDecrMask) |
| 2981 |
{ |
| 2982 |
return elemIncrDecr(obj, index, cx, getTopCallScope(cx), incrDecrMask); |
| 2983 |
} |
| 2984 |
|
| 2985 |
public static Object elemIncrDecr(Object obj, Object index, |
| 2986 |
Context cx, Scriptable scope, |
| 2987 |
int incrDecrMask) |
| 2988 |
{ |
| 2989 |
Object value = getObjectElem(obj, index, cx, scope); |
| 2990 |
boolean post = ((incrDecrMask & Node.POST_FLAG) != 0); |
| 2991 |
double number; |
| 2992 |
if (value instanceof Number) { |
| 2993 |
number = ((Number)value).doubleValue(); |
| 2994 |
} else { |
| 2995 |
number = toNumber(value); |
| 2996 |
if (post) { |
| 2997 |
|
| 2998 |
value = wrapNumber(number); |
| 2999 |
} |
| 3000 |
} |
| 3001 |
if ((incrDecrMask & Node.DECR_FLAG) == 0) { |
| 3002 |
++number; |
| 3003 |
} else { |
| 3004 |
--number; |
| 3005 |
} |
| 3006 |
Number result = wrapNumber(number); |
| 3007 |
setObjectElem(obj, index, result, cx, scope); |
| 3008 |
if (post) { |
| 3009 |
return value; |
| 3010 |
} else { |
| 3011 |
return result; |
| 3012 |
} |
| 3013 |
} |
| 3014 |
|
| 3015 |
|
| 3016 |
|
| 3017 |
|
| 3018 |
@Deprecated |
| 3019 |
public static Object refIncrDecr(Ref ref, Context cx, int incrDecrMask) |
| 3020 |
{ |
| 3021 |
return refIncrDecr(ref, cx, getTopCallScope(cx), incrDecrMask); |
| 3022 |
} |
| 3023 |
|
| 3024 |
public static Object refIncrDecr(Ref ref, Context cx, Scriptable scope, |
| 3025 |
int incrDecrMask) |
| 3026 |
{ |
| 3027 |
Object value = ref.get(cx); |
| 3028 |
boolean post = ((incrDecrMask & Node.POST_FLAG) != 0); |
| 3029 |
double number; |
| 3030 |
if (value instanceof Number) { |
| 3031 |
number = ((Number)value).doubleValue(); |
| 3032 |
} else { |
| 3033 |
number = toNumber(value); |
| 3034 |
if (post) { |
| 3035 |
|
| 3036 |
value = wrapNumber(number); |
| 3037 |
} |
| 3038 |
} |
| 3039 |
if ((incrDecrMask & Node.DECR_FLAG) == 0) { |
| 3040 |
++number; |
| 3041 |
} else { |
| 3042 |
--number; |
| 3043 |
} |
| 3044 |
Number result = wrapNumber(number); |
| 3045 |
ref.set(cx, scope, result); |
| 3046 |
if (post) { |
| 3047 |
return value; |
| 3048 |
} else { |
| 3049 |
return result; |
| 3050 |
} |
| 3051 |
} |
| 3052 |
|
| 3053 |
public static Object toPrimitive(Object val) { |
| 3054 |
return toPrimitive(val, null); |
| 3055 |
} |
| 3056 |
|
| 3057 |
public static Object toPrimitive(Object val, Class<?> typeHint) |
| 3058 |
{ |
| 3059 |
if (!(val instanceof Scriptable)) { |
| 3060 |
return val; |
| 3061 |
} |
| 3062 |
Scriptable s = (Scriptable)val; |
| 3063 |
Object result = s.getDefaultValue(typeHint); |
| 3064 |
if ((result instanceof Scriptable) && !isSymbol(result)) |
| 3065 |
throw typeError0("msg.bad.default.value"); |
| 3066 |
return result; |
| 3067 |
} |
| 3068 |
|
| 3069 |
|
| 3070 |
|
| 3071 |
|
| 3072 |
|
| 3073 |
|
| 3074 |
public static boolean eq(Object x, Object y) |
| 3075 |
{ |
| 3076 |
if (x == null || x == Undefined.instance) { |
| 3077 |
if (y == null || y == Undefined.instance) { |
| 3078 |
return true; |
| 3079 |
} |
| 3080 |
if (y instanceof ScriptableObject) { |
| 3081 |
Object test = ((ScriptableObject)y).equivalentValues(x); |
| 3082 |
if (test != Scriptable.NOT_FOUND) { |
| 3083 |
return ((Boolean)test).booleanValue(); |
| 3084 |
} |
| 3085 |
} |
| 3086 |
return false; |
| 3087 |
} else if (x instanceof Number) { |
| 3088 |
return eqNumber(((Number)x).doubleValue(), y); |
| 3089 |
} else if (x == y) { |
| 3090 |
return true; |
| 3091 |
} else if (x instanceof CharSequence) { |
| 3092 |
return eqString((CharSequence)x, y); |
| 3093 |
} else if (x instanceof Boolean) { |
| 3094 |
boolean b = ((Boolean)x).booleanValue(); |
| 3095 |
if (y instanceof Boolean) { |
| 3096 |
return b == ((Boolean)y).booleanValue(); |
| 3097 |
} |
| 3098 |
if (y instanceof ScriptableObject) { |
| 3099 |
Object test = ((ScriptableObject)y).equivalentValues(x); |
| 3100 |
if (test != Scriptable.NOT_FOUND) { |
| 3101 |
return ((Boolean)test).booleanValue(); |
| 3102 |
} |
| 3103 |
} |
| 3104 |
return eqNumber(b ? 1.0 : 0.0, y); |
| 3105 |
} else if (x instanceof Scriptable) { |
| 3106 |
if (y instanceof Scriptable) { |
| 3107 |
if (x instanceof ScriptableObject) { |
| 3108 |
Object test = ((ScriptableObject)x).equivalentValues(y); |
| 3109 |
if (test != Scriptable.NOT_FOUND) { |
| 3110 |
return ((Boolean)test).booleanValue(); |
| 3111 |
} |
| 3112 |
} |
| 3113 |
if (y instanceof ScriptableObject) { |
| 3114 |
Object test = ((ScriptableObject)y).equivalentValues(x); |
| 3115 |
if (test != Scriptable.NOT_FOUND) { |
| 3116 |
return ((Boolean)test).booleanValue(); |
| 3117 |
} |
| 3118 |
} |
| 3119 |
if (x instanceof Wrapper && y instanceof Wrapper) { |
| 3120 |
|
| 3121 |
|
| 3122 |
Object unwrappedX = ((Wrapper)x).unwrap(); |
| 3123 |
Object unwrappedY = ((Wrapper)y).unwrap(); |
| 3124 |
return unwrappedX == unwrappedY || |
| 3125 |
(isPrimitive(unwrappedX) && |
| 3126 |
isPrimitive(unwrappedY) && |
| 3127 |
eq(unwrappedX, unwrappedY)); |
| 3128 |
} |
| 3129 |
return false; |
| 3130 |
} else if (y instanceof Boolean) { |
| 3131 |
if (x instanceof ScriptableObject) { |
| 3132 |
Object test = ((ScriptableObject)x).equivalentValues(y); |
| 3133 |
if (test != Scriptable.NOT_FOUND) { |
| 3134 |
return ((Boolean)test).booleanValue(); |
| 3135 |
} |
| 3136 |
} |
| 3137 |
double d = ((Boolean)y).booleanValue() ? 1.0 : 0.0; |
| 3138 |
return eqNumber(d, x); |
| 3139 |
} else if (y instanceof Number) { |
| 3140 |
return eqNumber(((Number)y).doubleValue(), x); |
| 3141 |
} else if (y instanceof CharSequence) { |
| 3142 |
return eqString((CharSequence)y, x); |
| 3143 |
} |
| 3144 |
|
| 3145 |
return false; |
| 3146 |
} else { |
| 3147 |
warnAboutNonJSObject(x); |
| 3148 |
return x == y; |
| 3149 |
} |
| 3150 |
} |
| 3151 |
|
| 3152 |
|
| 3153 |
|
| 3154 |
|
| 3155 |
|
| 3156 |
public static boolean same(Object x, Object y) { |
| 3157 |
if (!typeof(x).equals(typeof(y))) { |
| 3158 |
return false; |
| 3159 |
} |
| 3160 |
if (x instanceof Number) { |
| 3161 |
if (isNaN(x) && isNaN(y)) { |
| 3162 |
return true; |
| 3163 |
} |
| 3164 |
return x.equals(y); |
| 3165 |
} |
| 3166 |
return eq(x, y); |
| 3167 |
} |
| 3168 |
|
| 3169 |
public static boolean isNaN(Object n) { |
| 3170 |
if (n == NaNobj) { |
| 3171 |
return true; |
| 3172 |
} |
| 3173 |
if (n instanceof Double) { |
| 3174 |
Double d = (Double)n; |
| 3175 |
return ((d == NaN) || Double.isNaN(d)); |
| 3176 |
} |
| 3177 |
if (n instanceof Float) { |
| 3178 |
Float f = (Float)n; |
| 3179 |
return ((f == NaN) || Float.isNaN(f)); |
| 3180 |
} |
| 3181 |
return false; |
| 3182 |
} |
| 3183 |
|
| 3184 |
public static boolean isPrimitive(Object obj) { |
| 3185 |
return obj == null || obj == Undefined.instance || |
| 3186 |
(obj instanceof Number) || (obj instanceof String) || |
| 3187 |
(obj instanceof Boolean); |
| 3188 |
} |
| 3189 |
|
| 3190 |
static boolean eqNumber(double x, Object y) |
| 3191 |
{ |
| 3192 |
for (;;) { |
| 3193 |
if (y == null || y == Undefined.instance) { |
| 3194 |
return false; |
| 3195 |
} else if (y instanceof Number) { |
| 3196 |
return x == ((Number)y).doubleValue(); |
| 3197 |
} else if (y instanceof CharSequence) { |
| 3198 |
return x == toNumber(y); |
| 3199 |
} else if (y instanceof Boolean) { |
| 3200 |
return x == (((Boolean) y).booleanValue() ? 1.0 : +0.0); |
| 3201 |
} else if (isSymbol(y)) { |
| 3202 |
return false; |
| 3203 |
} else if (y instanceof Scriptable) { |
| 3204 |
if (y instanceof ScriptableObject) { |
| 3205 |
Object xval = wrapNumber(x); |
| 3206 |
Object test = ((ScriptableObject)y).equivalentValues(xval); |
| 3207 |
if (test != Scriptable.NOT_FOUND) { |
| 3208 |
return ((Boolean)test).booleanValue(); |
| 3209 |
} |
| 3210 |
} |
| 3211 |
y = toPrimitive(y); |
| 3212 |
} else { |
| 3213 |
warnAboutNonJSObject(y); |
| 3214 |
return false; |
| 3215 |
} |
| 3216 |
} |
| 3217 |
} |
| 3218 |
|
| 3219 |
private static boolean eqString(CharSequence x, Object y) |
| 3220 |
{ |
| 3221 |
for (;;) { |
| 3222 |
if (y == null || y == Undefined.instance) { |
| 3223 |
return false; |
| 3224 |
} else if (y instanceof CharSequence) { |
| 3225 |
CharSequence c = (CharSequence)y; |
| 3226 |
return x.length() == c.length() && x.toString().equals(c.toString()); |
| 3227 |
} else if (y instanceof Number) { |
| 3228 |
return toNumber(x.toString()) == ((Number)y).doubleValue(); |
| 3229 |
} else if (y instanceof Boolean) { |
| 3230 |
return toNumber(x.toString()) == (((Boolean) y).booleanValue() ? 1.0 : 0.0); |
| 3231 |
} else if (isSymbol(y)) { |
| 3232 |
return false; |
| 3233 |
} else if (y instanceof Scriptable) { |
| 3234 |
if (y instanceof ScriptableObject) { |
| 3235 |
Object test = ((ScriptableObject)y).equivalentValues(x.toString()); |
| 3236 |
if (test != Scriptable.NOT_FOUND) { |
| 3237 |
return ((Boolean)test).booleanValue(); |
| 3238 |
} |
| 3239 |
} |
| 3240 |
y = toPrimitive(y); |
| 3241 |
continue; |
| 3242 |
} else { |
| 3243 |
warnAboutNonJSObject(y); |
| 3244 |
return false; |
| 3245 |
} |
| 3246 |
} |
| 3247 |
} |
| 3248 |
public static boolean shallowEq(Object x, Object y) |
| 3249 |
{ |
| 3250 |
if (x == y) { |
| 3251 |
if (!(x instanceof Number)) { |
| 3252 |
return true; |
| 3253 |
} |
| 3254 |
|
| 3255 |
double d = ((Number)x).doubleValue(); |
| 3256 |
return d == d; |
| 3257 |
} |
| 3258 |
if (x == null || x == Undefined.instance || x == Undefined.SCRIPTABLE_UNDEFINED) { |
| 3259 |
if ((x == Undefined.instance && y == Undefined.SCRIPTABLE_UNDEFINED) |
| 3260 |
|| (x == Undefined.SCRIPTABLE_UNDEFINED && y == Undefined.instance)) return true; |
| 3261 |
return false; |
| 3262 |
} else if (x instanceof Number) { |
| 3263 |
if (y instanceof Number) { |
| 3264 |
return ((Number)x).doubleValue() == ((Number)y).doubleValue(); |
| 3265 |
} |
| 3266 |
} else if (x instanceof CharSequence) { |
| 3267 |
if (y instanceof CharSequence) { |
| 3268 |
return x.toString().equals(y.toString()); |
| 3269 |
} |
| 3270 |
} else if (x instanceof Boolean) { |
| 3271 |
if (y instanceof Boolean) { |
| 3272 |
return x.equals(y); |
| 3273 |
} |
| 3274 |
} else if (x instanceof Scriptable) { |
| 3275 |
if (x instanceof Wrapper && y instanceof Wrapper) { |
| 3276 |
return ((Wrapper)x).unwrap() == ((Wrapper)y).unwrap(); |
| 3277 |
} |
| 3278 |
} else { |
| 3279 |
warnAboutNonJSObject(x); |
| 3280 |
return x == y; |
| 3281 |
} |
| 3282 |
return false; |
| 3283 |
} |
| 3284 |
|
| 3285 |
|
| 3286 |
|
| 3287 |
|
| 3288 |
|
| 3289 |
|
| 3290 |
public static boolean instanceOf(Object a, Object b, Context cx) |
| 3291 |
{ |
| 3292 |
|
| 3293 |
if (! (b instanceof Scriptable)) { |
| 3294 |
throw typeError0("msg.instanceof.not.object"); |
| 3295 |
} |
| 3296 |
|
| 3297 |
|
| 3298 |
if (! (a instanceof Scriptable)) |
| 3299 |
return false; |
| 3300 |
|
| 3301 |
return ((Scriptable)b).hasInstance((Scriptable)a); |
| 3302 |
} |
| 3303 |
|
| 3304 |
|
| 3305 |
|
| 3306 |
|
| 3307 |
|
| 3308 |
|
| 3309 |
public static boolean jsDelegatesTo(Scriptable lhs, Scriptable rhs) { |
| 3310 |
Scriptable proto = lhs.getPrototype(); |
| 3311 |
|
| 3312 |
while (proto != null) { |
| 3313 |
if (proto.equals(rhs)) return true; |
| 3314 |
proto = proto.getPrototype(); |
| 3315 |
} |
| 3316 |
|
| 3317 |
return false; |
| 3318 |
} |
| 3319 |
|
| 3320 |
|
| 3321 |
|
| 3322 |
|
| 3323 |
|
| 3324 |
|
| 3325 |
|
| 3326 |
|
| 3327 |
|
| 3328 |
|
| 3329 |
|
| 3330 |
|
| 3331 |
|
| 3332 |
|
| 3333 |
|
| 3334 |
public static boolean in(Object a, Object b, Context cx) |
| 3335 |
{ |
| 3336 |
if (!(b instanceof Scriptable)) { |
| 3337 |
throw typeError0("msg.in.not.object"); |
| 3338 |
} |
| 3339 |
|
| 3340 |
return hasObjectElem((Scriptable)b, a, cx); |
| 3341 |
} |
| 3342 |
|
| 3343 |
public static boolean cmp_LT(Object val1, Object val2) |
| 3344 |
{ |
| 3345 |
double d1, d2; |
| 3346 |
if (val1 instanceof Number && val2 instanceof Number) { |
| 3347 |
d1 = ((Number)val1).doubleValue(); |
| 3348 |
d2 = ((Number)val2).doubleValue(); |
| 3349 |
} else { |
| 3350 |
if ((val1 instanceof Symbol) || (val2 instanceof Symbol)) { |
| 3351 |
throw typeError0("msg.compare.symbol"); |
| 3352 |
} |
| 3353 |
if (val1 instanceof Scriptable) |
| 3354 |
val1 = ((Scriptable) val1).getDefaultValue(NumberClass); |
| 3355 |
if (val2 instanceof Scriptable) |
| 3356 |
val2 = ((Scriptable) val2).getDefaultValue(NumberClass); |
| 3357 |
if (val1 instanceof CharSequence && val2 instanceof CharSequence) { |
| 3358 |
return val1.toString().compareTo(val2.toString()) < 0; |
| 3359 |
} |
| 3360 |
d1 = toNumber(val1); |
| 3361 |
d2 = toNumber(val2); |
| 3362 |
} |
| 3363 |
return d1 < d2; |
| 3364 |
} |
| 3365 |
|
| 3366 |
public static boolean cmp_LE(Object val1, Object val2) |
| 3367 |
{ |
| 3368 |
double d1, d2; |
| 3369 |
if (val1 instanceof Number && val2 instanceof Number) { |
| 3370 |
d1 = ((Number)val1).doubleValue(); |
| 3371 |
d2 = ((Number)val2).doubleValue(); |
| 3372 |
} else { |
| 3373 |
if ((val1 instanceof Symbol) || (val2 instanceof Symbol)) { |
| 3374 |
throw typeError0("msg.compare.symbol"); |
| 3375 |
} |
| 3376 |
if (val1 instanceof Scriptable) |
| 3377 |
val1 = ((Scriptable) val1).getDefaultValue(NumberClass); |
| 3378 |
if (val2 instanceof Scriptable) |
| 3379 |
val2 = ((Scriptable) val2).getDefaultValue(NumberClass); |
| 3380 |
if (val1 instanceof CharSequence && val2 instanceof CharSequence) { |
| 3381 |
return val1.toString().compareTo(val2.toString()) <= 0; |
| 3382 |
} |
| 3383 |
d1 = toNumber(val1); |
| 3384 |
d2 = toNumber(val2); |
| 3385 |
} |
| 3386 |
return d1 <= d2; |
| 3387 |
} |
| 3388 |
|
| 3389 |
|
| 3390 |
|
| 3391 |
|
| 3392 |
|
| 3393 |
public static ScriptableObject getGlobal(Context cx) { |
| 3394 |
final String GLOBAL_CLASS = "org.mozilla.javascript.tools.shell.Global"; |
| 3395 |
Class<?> globalClass = Kit.classOrNull(GLOBAL_CLASS); |
| 3396 |
if (globalClass != null) { |
| 3397 |
try { |
| 3398 |
Class<?>[] parm = { ScriptRuntime.ContextClass }; |
| 3399 |
Constructor<?> globalClassCtor = globalClass.getConstructor(parm); |
| 3400 |
Object[] arg = { cx }; |
| 3401 |
return (ScriptableObject) globalClassCtor.newInstance(arg); |
| 3402 |
} |
| 3403 |
catch (RuntimeException e) { |
| 3404 |
throw e; |
| 3405 |
} |
| 3406 |
catch (Exception e) { |
| 3407 |
|
| 3408 |
} |
| 3409 |
} |
| 3410 |
return new ImporterTopLevel(cx); |
| 3411 |
} |
| 3412 |
|
| 3413 |
public static boolean hasTopCall(Context cx) |
| 3414 |
{ |
| 3415 |
return (cx.topCallScope != null); |
| 3416 |
} |
| 3417 |
|
| 3418 |
public static Scriptable getTopCallScope(Context cx) |
| 3419 |
{ |
| 3420 |
Scriptable scope = cx.topCallScope; |
| 3421 |
if (scope == null) { |
| 3422 |
throw new IllegalStateException(); |
| 3423 |
} |
| 3424 |
return scope; |
| 3425 |
} |
| 3426 |
|
| 3427 |
|
| 3428 |
|
| 3429 |
|
| 3430 |
public static Object doTopCall(Callable callable, |
| 3431 |
Context cx, Scriptable scope, |
| 3432 |
Scriptable thisObj, Object[] args) |
| 3433 |
{ |
| 3434 |
return doTopCall(callable, cx, scope, thisObj, args, cx.isTopLevelStrict); |
| 3435 |
} |
| 3436 |
|
| 3437 |
public static Object doTopCall(Callable callable, |
| 3438 |
Context cx, Scriptable scope, |
| 3439 |
Scriptable thisObj, Object[] args, boolean isTopLevelStrict) |
| 3440 |
{ |
| 3441 |
if (scope == null) |
| 3442 |
throw new IllegalArgumentException(); |
| 3443 |
if (cx.topCallScope != null) throw new IllegalStateException(); |
| 3444 |
|
| 3445 |
Object result; |
| 3446 |
cx.topCallScope = ScriptableObject.getTopLevelScope(scope); |
| 3447 |
cx.useDynamicScope = cx.hasFeature(Context.FEATURE_DYNAMIC_SCOPE); |
| 3448 |
boolean previousTopLevelStrict = cx.isTopLevelStrict; |
| 3449 |
cx.isTopLevelStrict = isTopLevelStrict; |
| 3450 |
ContextFactory f = cx.getFactory(); |
| 3451 |
try { |
| 3452 |
result = f.doTopCall(callable, cx, scope, thisObj, args); |
| 3453 |
} finally { |
| 3454 |
cx.topCallScope = null; |
| 3455 |
|
| 3456 |
cx.cachedXMLLib = null; |
| 3457 |
cx.isTopLevelStrict = previousTopLevelStrict; |
| 3458 |
|
| 3459 |
if (cx.currentActivationCall != null) { |
| 3460 |
|
| 3461 |
|
| 3462 |
throw new IllegalStateException(); |
| 3463 |
} |
| 3464 |
} |
| 3465 |
return result; |
| 3466 |
} |
| 3467 |
|
| 3468 |
|
| 3469 |
|
| 3470 |
|
| 3471 |
|
| 3472 |
|
| 3473 |
|
| 3474 |
static Scriptable checkDynamicScope(Scriptable possibleDynamicScope, |
| 3475 |
Scriptable staticTopScope) |
| 3476 |
{ |
| 3477 |
|
| 3478 |
if (possibleDynamicScope == staticTopScope) { |
| 3479 |
return possibleDynamicScope; |
| 3480 |
} |
| 3481 |
Scriptable proto = possibleDynamicScope; |
| 3482 |
for (;;) { |
| 3483 |
proto = proto.getPrototype(); |
| 3484 |
if (proto == staticTopScope) { |
| 3485 |
return possibleDynamicScope; |
| 3486 |
} |
| 3487 |
if (proto == null) { |
| 3488 |
return staticTopScope; |
| 3489 |
} |
| 3490 |
} |
| 3491 |
} |
| 3492 |
|
| 3493 |
public static void addInstructionCount(Context cx, int instructionsToAdd) |
| 3494 |
{ |
| 3495 |
cx.instructionCount += instructionsToAdd; |
| 3496 |
if (cx.instructionCount > cx.instructionThreshold) |
| 3497 |
{ |
| 3498 |
cx.observeInstructionCount(cx.instructionCount); |
| 3499 |
cx.instructionCount = 0; |
| 3500 |
} |
| 3501 |
} |
| 3502 |
|
| 3503 |
public static void initScript(NativeFunction funObj, Scriptable thisObj, |
| 3504 |
Context cx, Scriptable scope, |
| 3505 |
boolean evalScript) |
| 3506 |
{ |
| 3507 |
if (cx.topCallScope == null) |
| 3508 |
throw new IllegalStateException(); |
| 3509 |
|
| 3510 |
int varCount = funObj.getParamAndVarCount(); |
| 3511 |
if (varCount != 0) { |
| 3512 |
|
| 3513 |
Scriptable varScope = scope; |
| 3514 |
|
| 3515 |
|
| 3516 |
while (varScope instanceof NativeWith) { |
| 3517 |
varScope = varScope.getParentScope(); |
| 3518 |
} |
| 3519 |
|
| 3520 |
for (int i = varCount; i-- != 0;) { |
| 3521 |
String name = funObj.getParamOrVarName(i); |
| 3522 |
boolean isConst = funObj.getParamOrVarConst(i); |
| 3523 |
|
| 3524 |
|
| 3525 |
if (!ScriptableObject.hasProperty(scope, name)) { |
| 3526 |
if (isConst) { |
| 3527 |
ScriptableObject.defineConstProperty(varScope, name); |
| 3528 |
} else if (!evalScript) { |
| 3529 |
|
| 3530 |
ScriptableObject.defineProperty( |
| 3531 |
varScope, name, Undefined.instance, |
| 3532 |
ScriptableObject.PERMANENT); |
| 3533 |
} else { |
| 3534 |
varScope.put(name, varScope, Undefined.instance); |
| 3535 |
} |
| 3536 |
} else { |
| 3537 |
ScriptableObject.redefineProperty(scope, name, isConst); |
| 3538 |
} |
| 3539 |
} |
| 3540 |
} |
| 3541 |
} |
| 3542 |
|
| 3543 |
|
| 3544 |
|
| 3545 |
|
| 3546 |
@Deprecated |
| 3547 |
public static Scriptable createFunctionActivation(NativeFunction funObj, |
| 3548 |
Scriptable scope, |
| 3549 |
Object[] args) |
| 3550 |
{ |
| 3551 |
return createFunctionActivation(funObj, scope, args, false); |
| 3552 |
} |
| 3553 |
|
| 3554 |
public static Scriptable createFunctionActivation(NativeFunction funObj, |
| 3555 |
Scriptable scope, |
| 3556 |
Object[] args, |
| 3557 |
boolean isStrict) |
| 3558 |
{ |
| 3559 |
return new NativeCall(funObj, scope, args, false, isStrict); |
| 3560 |
} |
| 3561 |
|
| 3562 |
public static Scriptable createArrowFunctionActivation(NativeFunction funObj, |
| 3563 |
Scriptable scope, |
| 3564 |
Object[] args, |
| 3565 |
boolean isStrict) |
| 3566 |
{ |
| 3567 |
return new NativeCall(funObj, scope, args, true, isStrict); |
| 3568 |
} |
| 3569 |
|
| 3570 |
public static void enterActivationFunction(Context cx, |
| 3571 |
Scriptable scope) |
| 3572 |
{ |
| 3573 |
if (cx.topCallScope == null) |
| 3574 |
throw new IllegalStateException(); |
| 3575 |
NativeCall call = (NativeCall)scope; |
| 3576 |
call.parentActivationCall = cx.currentActivationCall; |
| 3577 |
cx.currentActivationCall = call; |
| 3578 |
call.defineAttributesForArguments(); |
| 3579 |
} |
| 3580 |
|
| 3581 |
public static void exitActivationFunction(Context cx) |
| 3582 |
{ |
| 3583 |
NativeCall call = cx.currentActivationCall; |
| 3584 |
cx.currentActivationCall = call.parentActivationCall; |
| 3585 |
call.parentActivationCall = null; |
| 3586 |
} |
| 3587 |
|
| 3588 |
static NativeCall findFunctionActivation(Context cx, Function f) |
| 3589 |
{ |
| 3590 |
NativeCall call = cx.currentActivationCall; |
| 3591 |
while (call != null) { |
| 3592 |
if (call.function == f) |
| 3593 |
return call; |
| 3594 |
call = call.parentActivationCall; |
| 3595 |
} |
| 3596 |
return null; |
| 3597 |
} |
| 3598 |
|
| 3599 |
public static Scriptable newCatchScope(Throwable t, |
| 3600 |
Scriptable lastCatchScope, |
| 3601 |
String exceptionName, |
| 3602 |
Context cx, Scriptable scope) |
| 3603 |
{ |
| 3604 |
Object obj; |
| 3605 |
boolean cacheObj; |
| 3606 |
|
| 3607 |
getObj: |
| 3608 |
if (t instanceof JavaScriptException) { |
| 3609 |
cacheObj = false; |
| 3610 |
obj = ((JavaScriptException)t).getValue(); |
| 3611 |
} else { |
| 3612 |
cacheObj = true; |
| 3613 |
|
| 3614 |
|
| 3615 |
|
| 3616 |
|
| 3617 |
if (lastCatchScope != null) { |
| 3618 |
NativeObject last = (NativeObject)lastCatchScope; |
| 3619 |
obj = last.getAssociatedValue(t); |
| 3620 |
if (obj == null) Kit.codeBug(); |
| 3621 |
break getObj; |
| 3622 |
} |
| 3623 |
|
| 3624 |
RhinoException re; |
| 3625 |
TopLevel.NativeErrors type; |
| 3626 |
String errorMsg; |
| 3627 |
Throwable javaException = null; |
| 3628 |
|
| 3629 |
if (t instanceof EcmaError) { |
| 3630 |
EcmaError ee = (EcmaError)t; |
| 3631 |
re = ee; |
| 3632 |
type = TopLevel.NativeErrors.valueOf(ee.getName()); |
| 3633 |
errorMsg = ee.getErrorMessage(); |
| 3634 |
} else if (t instanceof WrappedException) { |
| 3635 |
WrappedException we = (WrappedException)t; |
| 3636 |
re = we; |
| 3637 |
javaException = we.getWrappedException(); |
| 3638 |
type = TopLevel.NativeErrors.JavaException; |
| 3639 |
errorMsg = javaException.getClass().getName() |
| 3640 |
+": "+javaException.getMessage(); |
| 3641 |
} else if (t instanceof EvaluatorException) { |
| 3642 |
|
| 3643 |
EvaluatorException ee = (EvaluatorException)t; |
| 3644 |
re = ee; |
| 3645 |
type = TopLevel.NativeErrors.InternalError; |
| 3646 |
errorMsg = ee.getMessage(); |
| 3647 |
} else if (cx.hasFeature(Context.FEATURE_ENHANCED_JAVA_ACCESS)) { |
| 3648 |
|
| 3649 |
|
| 3650 |
re = new WrappedException(t); |
| 3651 |
type = TopLevel.NativeErrors.JavaException; |
| 3652 |
errorMsg = t.toString(); |
| 3653 |
} else { |
| 3654 |
|
| 3655 |
|
| 3656 |
throw Kit.codeBug(); |
| 3657 |
} |
| 3658 |
|
| 3659 |
String sourceUri = re.sourceName(); |
| 3660 |
if (sourceUri == null) { |
| 3661 |
sourceUri = ""; |
| 3662 |
} |
| 3663 |
int line = re.lineNumber(); |
| 3664 |
Object args[]; |
| 3665 |
if (line > 0) { |
| 3666 |
args = new Object[] { errorMsg, sourceUri, Integer.valueOf(line) }; |
| 3667 |
} else { |
| 3668 |
args = new Object[] { errorMsg, sourceUri }; |
| 3669 |
} |
| 3670 |
|
| 3671 |
Scriptable errorObject = newNativeError(cx, scope, type, args); |
| 3672 |
|
| 3673 |
if (errorObject instanceof NativeError) { |
| 3674 |
((NativeError) errorObject).setStackProvider(re); |
| 3675 |
} |
| 3676 |
|
| 3677 |
if (javaException != null && isVisible(cx, javaException)) { |
| 3678 |
Object wrap = cx.getWrapFactory().wrap(cx, scope, javaException, |
| 3679 |
null); |
| 3680 |
ScriptableObject.defineProperty( |
| 3681 |
errorObject, "javaException", wrap, |
| 3682 |
ScriptableObject.PERMANENT | ScriptableObject.READONLY | ScriptableObject.DONTENUM); |
| 3683 |
} |
| 3684 |
if (isVisible(cx, re)) { |
| 3685 |
Object wrap = cx.getWrapFactory().wrap(cx, scope, re, null); |
| 3686 |
ScriptableObject.defineProperty( |
| 3687 |
errorObject, "rhinoException", wrap, |
| 3688 |
ScriptableObject.PERMANENT | ScriptableObject.READONLY | ScriptableObject.DONTENUM); |
| 3689 |
} |
| 3690 |
obj = errorObject; |
| 3691 |
} |
| 3692 |
|
| 3693 |
NativeObject catchScopeObject = new NativeObject(); |
| 3694 |
|
| 3695 |
catchScopeObject.defineProperty( |
| 3696 |
exceptionName, obj, ScriptableObject.PERMANENT); |
| 3697 |
|
| 3698 |
if (isVisible(cx, t)) { |
| 3699 |
|
| 3700 |
|
| 3701 |
|
| 3702 |
catchScopeObject.defineProperty( |
| 3703 |
"__exception__", Context.javaToJS(t, scope), |
| 3704 |
ScriptableObject.PERMANENT|ScriptableObject.DONTENUM); |
| 3705 |
} |
| 3706 |
|
| 3707 |
if (cacheObj) { |
| 3708 |
catchScopeObject.associateValue(t, obj); |
| 3709 |
} |
| 3710 |
return catchScopeObject; |
| 3711 |
} |
| 3712 |
|
| 3713 |
public static Scriptable wrapException(Throwable t, |
| 3714 |
Scriptable scope, |
| 3715 |
Context cx) { |
| 3716 |
RhinoException re; |
| 3717 |
String errorName; |
| 3718 |
String errorMsg; |
| 3719 |
Throwable javaException = null; |
| 3720 |
|
| 3721 |
if (t instanceof EcmaError) { |
| 3722 |
EcmaError ee = (EcmaError)t; |
| 3723 |
re = ee; |
| 3724 |
errorName = ee.getName(); |
| 3725 |
errorMsg = ee.getErrorMessage(); |
| 3726 |
} else if (t instanceof WrappedException) { |
| 3727 |
WrappedException we = (WrappedException)t; |
| 3728 |
re = we; |
| 3729 |
javaException = we.getWrappedException(); |
| 3730 |
errorName = "JavaException"; |
| 3731 |
errorMsg = javaException.getClass().getName() |
| 3732 |
+": "+javaException.getMessage(); |
| 3733 |
} else if (t instanceof EvaluatorException) { |
| 3734 |
|
| 3735 |
EvaluatorException ee = (EvaluatorException)t; |
| 3736 |
re = ee; |
| 3737 |
errorName = "InternalError"; |
| 3738 |
errorMsg = ee.getMessage(); |
| 3739 |
} else if (cx.hasFeature(Context.FEATURE_ENHANCED_JAVA_ACCESS)) { |
| 3740 |
|
| 3741 |
|
| 3742 |
re = new WrappedException(t); |
| 3743 |
errorName = "JavaException"; |
| 3744 |
errorMsg = t.toString(); |
| 3745 |
} else { |
| 3746 |
|
| 3747 |
|
| 3748 |
throw Kit.codeBug(); |
| 3749 |
} |
| 3750 |
|
| 3751 |
String sourceUri = re.sourceName(); |
| 3752 |
if (sourceUri == null) { |
| 3753 |
sourceUri = ""; |
| 3754 |
} |
| 3755 |
int line = re.lineNumber(); |
| 3756 |
Object args[]; |
| 3757 |
if (line > 0) { |
| 3758 |
args = new Object[] { errorMsg, sourceUri, Integer.valueOf(line) }; |
| 3759 |
} else { |
| 3760 |
args = new Object[] { errorMsg, sourceUri }; |
| 3761 |
} |
| 3762 |
|
| 3763 |
Scriptable errorObject = cx.newObject(scope, errorName, args); |
| 3764 |
ScriptableObject.putProperty(errorObject, "name", errorName); |
| 3765 |
|
| 3766 |
if (errorObject instanceof NativeError) { |
| 3767 |
((NativeError) errorObject).setStackProvider(re); |
| 3768 |
} |
| 3769 |
|
| 3770 |
if (javaException != null && isVisible(cx, javaException)) { |
| 3771 |
Object wrap = cx.getWrapFactory().wrap(cx, scope, javaException, |
| 3772 |
null); |
| 3773 |
ScriptableObject.defineProperty( |
| 3774 |
errorObject, "javaException", wrap, |
| 3775 |
ScriptableObject.PERMANENT | ScriptableObject.READONLY | ScriptableObject.DONTENUM); |
| 3776 |
} |
| 3777 |
if (isVisible(cx, re)) { |
| 3778 |
Object wrap = cx.getWrapFactory().wrap(cx, scope, re, null); |
| 3779 |
ScriptableObject.defineProperty( |
| 3780 |
errorObject, "rhinoException", wrap, |
| 3781 |
ScriptableObject.PERMANENT | ScriptableObject.READONLY | ScriptableObject.DONTENUM); |
| 3782 |
} |
| 3783 |
return errorObject; |
| 3784 |
} |
| 3785 |
|
| 3786 |
private static boolean isVisible(Context cx, Object obj) { |
| 3787 |
ClassShutter shutter = cx.getClassShutter(); |
| 3788 |
return shutter == null || |
| 3789 |
shutter.visibleToScripts(obj.getClass().getName()); |
| 3790 |
} |
| 3791 |
|
| 3792 |
public static Scriptable enterWith(Object obj, Context cx, |
| 3793 |
Scriptable scope) |
| 3794 |
{ |
| 3795 |
Scriptable sobj = toObjectOrNull(cx, obj, scope); |
| 3796 |
if (sobj == null) { |
| 3797 |
throw typeError1("msg.undef.with", toString(obj)); |
| 3798 |
} |
| 3799 |
if (sobj instanceof XMLObject) { |
| 3800 |
XMLObject xmlObject = (XMLObject)sobj; |
| 3801 |
return xmlObject.enterWith(scope); |
| 3802 |
} |
| 3803 |
return new NativeWith(scope, sobj); |
| 3804 |
} |
| 3805 |
|
| 3806 |
public static Scriptable leaveWith(Scriptable scope) |
| 3807 |
{ |
| 3808 |
NativeWith nw = (NativeWith)scope; |
| 3809 |
return nw.getParentScope(); |
| 3810 |
} |
| 3811 |
|
| 3812 |
public static Scriptable enterDotQuery(Object value, Scriptable scope) |
| 3813 |
{ |
| 3814 |
if (!(value instanceof XMLObject)) { |
| 3815 |
throw notXmlError(value); |
| 3816 |
} |
| 3817 |
XMLObject object = (XMLObject)value; |
| 3818 |
return object.enterDotQuery(scope); |
| 3819 |
} |
| 3820 |
|
| 3821 |
public static Object updateDotQuery(boolean value, Scriptable scope) |
| 3822 |
{ |
| 3823 |
|
| 3824 |
NativeWith nw = (NativeWith)scope; |
| 3825 |
return nw.updateDotQuery(value); |
| 3826 |
} |
| 3827 |
|
| 3828 |
public static Scriptable leaveDotQuery(Scriptable scope) |
| 3829 |
{ |
| 3830 |
NativeWith nw = (NativeWith)scope; |
| 3831 |
return nw.getParentScope(); |
| 3832 |
} |
| 3833 |
|
| 3834 |
public static void setFunctionProtoAndParent(BaseFunction fn, |
| 3835 |
Scriptable scope) |
| 3836 |
{ |
| 3837 |
fn.setParentScope(scope); |
| 3838 |
fn.setPrototype(ScriptableObject.getFunctionPrototype(scope)); |
| 3839 |
} |
| 3840 |
|
| 3841 |
public static void setObjectProtoAndParent(ScriptableObject object, |
| 3842 |
Scriptable scope) |
| 3843 |
{ |
| 3844 |
|
| 3845 |
scope = ScriptableObject.getTopLevelScope(scope); |
| 3846 |
object.setParentScope(scope); |
| 3847 |
Scriptable proto |
| 3848 |
= ScriptableObject.getClassPrototype(scope, object.getClassName()); |
| 3849 |
object.setPrototype(proto); |
| 3850 |
} |
| 3851 |
|
| 3852 |
public static void setBuiltinProtoAndParent(ScriptableObject object, |
| 3853 |
Scriptable scope, |
| 3854 |
TopLevel.Builtins type) |
| 3855 |
{ |
| 3856 |
scope = ScriptableObject.getTopLevelScope(scope); |
| 3857 |
object.setParentScope(scope); |
| 3858 |
object.setPrototype(TopLevel.getBuiltinPrototype(scope, type)); |
| 3859 |
} |
| 3860 |
|
| 3861 |
|
| 3862 |
public static void initFunction(Context cx, Scriptable scope, |
| 3863 |
NativeFunction function, int type, |
| 3864 |
boolean fromEvalCode) |
| 3865 |
{ |
| 3866 |
if (type == FunctionNode.FUNCTION_STATEMENT) { |
| 3867 |
String name = function.getFunctionName(); |
| 3868 |
if (name != null && name.length() != 0) { |
| 3869 |
if (!fromEvalCode) { |
| 3870 |
|
| 3871 |
|
| 3872 |
ScriptableObject.defineProperty |
| 3873 |
(scope, name, function, ScriptableObject.PERMANENT); |
| 3874 |
} else { |
| 3875 |
scope.put(name, scope, function); |
| 3876 |
} |
| 3877 |
} |
| 3878 |
} else if (type == FunctionNode.FUNCTION_EXPRESSION_STATEMENT) { |
| 3879 |
String name = function.getFunctionName(); |
| 3880 |
if (name != null && name.length() != 0) { |
| 3881 |
|
| 3882 |
|
| 3883 |
|
| 3884 |
while (scope instanceof NativeWith) { |
| 3885 |
scope = scope.getParentScope(); |
| 3886 |
} |
| 3887 |
scope.put(name, scope, function); |
| 3888 |
} |
| 3889 |
} else { |
| 3890 |
throw Kit.codeBug(); |
| 3891 |
} |
| 3892 |
} |
| 3893 |
|
| 3894 |
public static Scriptable newArrayLiteral(Object[] objects, |
| 3895 |
int[] skipIndices, |
| 3896 |
Context cx, Scriptable scope) |
| 3897 |
{ |
| 3898 |
final int SKIP_DENSITY = 2; |
| 3899 |
int count = objects.length; |
| 3900 |
int skipCount = 0; |
| 3901 |
if (skipIndices != null) { |
| 3902 |
skipCount = skipIndices.length; |
| 3903 |
} |
| 3904 |
int length = count + skipCount; |
| 3905 |
if (length > 1 && skipCount * SKIP_DENSITY < length) { |
| 3906 |
|
| 3907 |
Object[] sparse; |
| 3908 |
if (skipCount == 0) { |
| 3909 |
sparse = objects; |
| 3910 |
} else { |
| 3911 |
sparse = new Object[length]; |
| 3912 |
int skip = 0; |
| 3913 |
for (int i = 0, j = 0; i != length; ++i) { |
| 3914 |
if (skip != skipCount && skipIndices[skip] == i) { |
| 3915 |
sparse[i] = Scriptable.NOT_FOUND; |
| 3916 |
++skip; |
| 3917 |
continue; |
| 3918 |
} |
| 3919 |
sparse[i] = objects[j]; |
| 3920 |
++j; |
| 3921 |
} |
| 3922 |
} |
| 3923 |
return cx.newArray(scope, sparse); |
| 3924 |
} |
| 3925 |
|
| 3926 |
Scriptable array = cx.newArray(scope, length); |
| 3927 |
|
| 3928 |
int skip = 0; |
| 3929 |
for (int i = 0, j = 0; i != length; ++i) { |
| 3930 |
if (skip != skipCount && skipIndices[skip] == i) { |
| 3931 |
++skip; |
| 3932 |
continue; |
| 3933 |
} |
| 3934 |
array.put(i, array, objects[j]); |
| 3935 |
++j; |
| 3936 |
} |
| 3937 |
return array; |
| 3938 |
} |
| 3939 |
|
| 3940 |
|
| 3941 |
|
| 3942 |
|
| 3943 |
|
| 3944 |
|
| 3945 |
|
| 3946 |
|
| 3947 |
@Deprecated |
| 3948 |
public static Scriptable newObjectLiteral(Object[] propertyIds, |
| 3949 |
Object[] propertyValues, |
| 3950 |
Context cx, Scriptable scope) |
| 3951 |
{ |
| 3952 |
|
| 3953 |
return newObjectLiteral(propertyIds, propertyValues, null, cx, scope); |
| 3954 |
} |
| 3955 |
|
| 3956 |
public static Scriptable newObjectLiteral(Object[] propertyIds, |
| 3957 |
Object[] propertyValues, |
| 3958 |
int [] getterSetters, |
| 3959 |
Context cx, Scriptable scope) |
| 3960 |
{ |
| 3961 |
Scriptable object = cx.newObject(scope); |
| 3962 |
for (int i = 0, end = propertyIds.length; i != end; ++i) { |
| 3963 |
Object id = propertyIds[i]; |
| 3964 |
int getterSetter = getterSetters == null ? 0 : getterSetters[i]; |
| 3965 |
Object value = propertyValues[i]; |
| 3966 |
if (id instanceof String) { |
| 3967 |
if (getterSetter == 0) { |
| 3968 |
if (isSpecialProperty((String)id)) { |
| 3969 |
Ref ref = specialRef(object, (String)id, cx, scope); |
| 3970 |
ref.set(cx, scope, value); |
| 3971 |
} else { |
| 3972 |
object.put((String)id, object, value); |
| 3973 |
} |
| 3974 |
} else { |
| 3975 |
ScriptableObject so = (ScriptableObject)object; |
| 3976 |
Callable getterOrSetter = (Callable)value; |
| 3977 |
boolean isSetter = getterSetter == 1; |
| 3978 |
so.setGetterOrSetter((String)id, 0, getterOrSetter, isSetter); |
| 3979 |
} |
| 3980 |
} else { |
| 3981 |
int index = ((Integer)id).intValue(); |
| 3982 |
object.put(index, object, value); |
| 3983 |
} |
| 3984 |
} |
| 3985 |
return object; |
| 3986 |
} |
| 3987 |
|
| 3988 |
public static boolean isArrayObject(Object obj) |
| 3989 |
{ |
| 3990 |
return obj instanceof NativeArray || obj instanceof Arguments; |
| 3991 |
} |
| 3992 |
|
| 3993 |
public static Object[] getArrayElements(Scriptable object) |
| 3994 |
{ |
| 3995 |
Context cx = Context.getContext(); |
| 3996 |
long longLen = NativeArray.getLengthProperty(cx, object); |
| 3997 |
if (longLen > Integer.MAX_VALUE) { |
| 3998 |
|
| 3999 |
throw new IllegalArgumentException(); |
| 4000 |
} |
| 4001 |
int len = (int) longLen; |
| 4002 |
if (len == 0) { |
| 4003 |
return ScriptRuntime.emptyArgs; |
| 4004 |
} else { |
| 4005 |
Object[] result = new Object[len]; |
| 4006 |
for (int i=0; i < len; i++) { |
| 4007 |
Object elem = ScriptableObject.getProperty(object, i); |
| 4008 |
result[i] = (elem == Scriptable.NOT_FOUND) ? Undefined.instance |
| 4009 |
: elem; |
| 4010 |
} |
| 4011 |
return result; |
| 4012 |
} |
| 4013 |
} |
| 4014 |
|
| 4015 |
static void checkDeprecated(Context cx, String name) { |
| 4016 |
int version = cx.getLanguageVersion(); |
| 4017 |
if (version >= Context.VERSION_1_4 || version == Context.VERSION_DEFAULT) { |
| 4018 |
String msg = getMessage1("msg.deprec.ctor", name); |
| 4019 |
if (version == Context.VERSION_DEFAULT) |
| 4020 |
Context.reportWarning(msg); |
| 4021 |
else |
| 4022 |
throw Context.reportRuntimeError(msg); |
| 4023 |
} |
| 4024 |
} |
| 4025 |
|
| 4026 |
public static String getMessage0(String messageId) |
| 4027 |
{ |
| 4028 |
return getMessage(messageId, null); |
| 4029 |
} |
| 4030 |
|
| 4031 |
public static String getMessage1(String messageId, Object arg1) |
| 4032 |
{ |
| 4033 |
Object[] arguments = {arg1}; |
| 4034 |
return getMessage(messageId, arguments); |
| 4035 |
} |
| 4036 |
|
| 4037 |
public static String getMessage2( |
| 4038 |
String messageId, Object arg1, Object arg2) |
| 4039 |
{ |
| 4040 |
Object[] arguments = {arg1, arg2}; |
| 4041 |
return getMessage(messageId, arguments); |
| 4042 |
} |
| 4043 |
|
| 4044 |
public static String getMessage3( |
| 4045 |
String messageId, Object arg1, Object arg2, Object arg3) |
| 4046 |
{ |
| 4047 |
Object[] arguments = {arg1, arg2, arg3}; |
| 4048 |
return getMessage(messageId, arguments); |
| 4049 |
} |
| 4050 |
|
| 4051 |
public static String getMessage4( |
| 4052 |
String messageId, Object arg1, Object arg2, Object arg3, Object arg4) |
| 4053 |
{ |
| 4054 |
Object[] arguments = {arg1, arg2, arg3, arg4}; |
| 4055 |
return getMessage(messageId, arguments); |
| 4056 |
} |
| 4057 |
|
| 4058 |
|
| 4059 |
|
| 4060 |
|
| 4061 |
|
| 4062 |
|
| 4063 |
|
| 4064 |
public interface MessageProvider { |
| 4065 |
|
| 4066 |
|
| 4067 |
|
| 4068 |
|
| 4069 |
|
| 4070 |
|
| 4071 |
|
| 4072 |
|
| 4073 |
String getMessage(String messageId, Object[] arguments); |
| 4074 |
} |
| 4075 |
|
| 4076 |
public static MessageProvider messageProvider = new DefaultMessageProvider(); |
| 4077 |
|
| 4078 |
public static String getMessage(String messageId, Object[] arguments) |
| 4079 |
{ |
| 4080 |
return messageProvider.getMessage(messageId, arguments); |
| 4081 |
} |
| 4082 |
|
| 4083 |
|
| 4084 |
|
| 4085 |
|
| 4086 |
|
| 4087 |
private static class DefaultMessageProvider implements MessageProvider { |
| 4088 |
public String getMessage(String messageId, Object[] arguments) { |
| 4089 |
final String defaultResource |
| 4090 |
= "org.mozilla.javascript.resources.Messages"; |
| 4091 |
|
| 4092 |
Context cx = Context.getCurrentContext(); |
| 4093 |
Locale locale = cx != null ? cx.getLocale() : Locale.getDefault(); |
| 4094 |
|
| 4095 |
|
| 4096 |
ResourceBundle rb = ResourceBundle.getBundle(defaultResource, locale); |
| 4097 |
|
| 4098 |
String formatString; |
| 4099 |
try { |
| 4100 |
formatString = rb.getString(messageId); |
| 4101 |
} catch (java.util.MissingResourceException mre) { |
| 4102 |
throw new RuntimeException |
| 4103 |
("no message resource found for message property "+ messageId); |
| 4104 |
} |
| 4105 |
|
| 4106 |
|
| 4107 |
|
| 4108 |
|
| 4109 |
|
| 4110 |
|
| 4111 |
MessageFormat formatter = new MessageFormat(formatString); |
| 4112 |
return formatter.format(arguments); |
| 4113 |
} |
| 4114 |
} |
| 4115 |
|
| 4116 |
public static EcmaError constructError(String error, String message) |
| 4117 |
{ |
| 4118 |
int[] linep = new int[1]; |
| 4119 |
String filename = Context.getSourcePositionFromStack(linep); |
| 4120 |
return constructError(error, message, filename, linep[0], null, 0); |
| 4121 |
} |
| 4122 |
|
| 4123 |
public static EcmaError constructError(String error, |
| 4124 |
String message, |
| 4125 |
int lineNumberDelta) |
| 4126 |
{ |
| 4127 |
int[] linep = new int[1]; |
| 4128 |
String filename = Context.getSourcePositionFromStack(linep); |
| 4129 |
if (linep[0] != 0) { |
| 4130 |
linep[0] += lineNumberDelta; |
| 4131 |
} |
| 4132 |
return constructError(error, message, filename, linep[0], null, 0); |
| 4133 |
} |
| 4134 |
|
| 4135 |
public static EcmaError constructError(String error, |
| 4136 |
String message, |
| 4137 |
String sourceName, |
| 4138 |
int lineNumber, |
| 4139 |
String lineSource, |
| 4140 |
int columnNumber) |
| 4141 |
{ |
| 4142 |
return new EcmaError(error, message, sourceName, |
| 4143 |
lineNumber, lineSource, columnNumber); |
| 4144 |
} |
| 4145 |
|
| 4146 |
public static EcmaError rangeError(String message) |
| 4147 |
{ |
| 4148 |
return constructError("RangeError", message); |
| 4149 |
} |
| 4150 |
|
| 4151 |
public static EcmaError typeError(String message) |
| 4152 |
{ |
| 4153 |
return constructError("TypeError", message); |
| 4154 |
} |
| 4155 |
|
| 4156 |
public static EcmaError typeError0(String messageId) |
| 4157 |
{ |
| 4158 |
String msg = getMessage0(messageId); |
| 4159 |
return typeError(msg); |
| 4160 |
} |
| 4161 |
|
| 4162 |
public static EcmaError typeError1(String messageId, Object arg1) |
| 4163 |
{ |
| 4164 |
String msg = getMessage1(messageId, arg1); |
| 4165 |
return typeError(msg); |
| 4166 |
} |
| 4167 |
|
| 4168 |
public static EcmaError typeError2(String messageId, Object arg1, |
| 4169 |
Object arg2) |
| 4170 |
{ |
| 4171 |
String msg = getMessage2(messageId, arg1, arg2); |
| 4172 |
return typeError(msg); |
| 4173 |
} |
| 4174 |
|
| 4175 |
public static EcmaError typeError3(String messageId, String arg1, |
| 4176 |
String arg2, String arg3) |
| 4177 |
{ |
| 4178 |
String msg = getMessage3(messageId, arg1, arg2, arg3); |
| 4179 |
return typeError(msg); |
| 4180 |
} |
| 4181 |
|
| 4182 |
public static RuntimeException undefReadError(Object object, Object id) |
| 4183 |
{ |
| 4184 |
return typeError2("msg.undef.prop.read", toString(object), toString(id)); |
| 4185 |
} |
| 4186 |
|
| 4187 |
public static RuntimeException undefCallError(Object object, Object id) |
| 4188 |
{ |
| 4189 |
return typeError2("msg.undef.method.call", toString(object), toString(id)); |
| 4190 |
} |
| 4191 |
|
| 4192 |
public static RuntimeException undefWriteError(Object object, |
| 4193 |
Object id, |
| 4194 |
Object value) |
| 4195 |
{ |
| 4196 |
return typeError3("msg.undef.prop.write", toString(object), toString(id), |
| 4197 |
toString(value)); |
| 4198 |
} |
| 4199 |
|
| 4200 |
private static RuntimeException undefDeleteError(Object object, Object id) |
| 4201 |
{ |
| 4202 |
throw typeError2("msg.undef.prop.delete", toString(object), toString(id)); |
| 4203 |
} |
| 4204 |
|
| 4205 |
public static RuntimeException notFoundError(Scriptable object, |
| 4206 |
String property) |
| 4207 |
{ |
| 4208 |
|
| 4209 |
String msg = getMessage1("msg.is.not.defined", property); |
| 4210 |
throw constructError("ReferenceError", msg); |
| 4211 |
} |
| 4212 |
|
| 4213 |
public static RuntimeException notFunctionError(Object value) |
| 4214 |
{ |
| 4215 |
return notFunctionError(value, value); |
| 4216 |
} |
| 4217 |
|
| 4218 |
public static RuntimeException notFunctionError(Object value, |
| 4219 |
Object messageHelper) |
| 4220 |
{ |
| 4221 |
|
| 4222 |
String msg = (messageHelper == null) |
| 4223 |
? "null" : messageHelper.toString(); |
| 4224 |
if (value == Scriptable.NOT_FOUND) { |
| 4225 |
return typeError1("msg.function.not.found", msg); |
| 4226 |
} |
| 4227 |
return typeError2("msg.isnt.function", msg, typeof(value)); |
| 4228 |
} |
| 4229 |
|
| 4230 |
public static RuntimeException notFunctionError(Object obj, Object value, |
| 4231 |
String propertyName) |
| 4232 |
{ |
| 4233 |
|
| 4234 |
String objString = toString(obj); |
| 4235 |
if (obj instanceof NativeFunction) { |
| 4236 |
|
| 4237 |
int paren = objString.indexOf(')'); |
| 4238 |
int curly = objString.indexOf('{', paren); |
| 4239 |
if (curly > -1) { |
| 4240 |
objString = objString.substring(0, curly + 1) + "...}"; |
| 4241 |
} |
| 4242 |
} |
| 4243 |
if (value == Scriptable.NOT_FOUND) { |
| 4244 |
return typeError2("msg.function.not.found.in", propertyName, |
| 4245 |
objString); |
| 4246 |
} |
| 4247 |
return typeError3("msg.isnt.function.in", propertyName, objString, |
| 4248 |
typeof(value)); |
| 4249 |
} |
| 4250 |
|
| 4251 |
private static RuntimeException notXmlError(Object value) |
| 4252 |
{ |
| 4253 |
throw typeError1("msg.isnt.xml.object", toString(value)); |
| 4254 |
} |
| 4255 |
|
| 4256 |
private static void warnAboutNonJSObject(Object nonJSObject) |
| 4257 |
{ |
| 4258 |
final String omitParam = ScriptRuntime.getMessage0("params.omit.non.js.object.warning"); |
| 4259 |
if (!"true".equals(omitParam)) { |
| 4260 |
String message = ScriptRuntime.getMessage2("msg.non.js.object.warning",nonJSObject,nonJSObject.getClass().getName()); |
| 4261 |
Context.reportWarning(message); |
| 4262 |
|
| 4263 |
System.err.println(message); |
| 4264 |
} |
| 4265 |
} |
| 4266 |
|
| 4267 |
public static RegExpProxy getRegExpProxy(Context cx) |
| 4268 |
{ |
| 4269 |
return cx.getRegExpProxy(); |
| 4270 |
} |
| 4271 |
|
| 4272 |
public static void setRegExpProxy(Context cx, RegExpProxy proxy) |
| 4273 |
{ |
| 4274 |
if (proxy == null) throw new IllegalArgumentException(); |
| 4275 |
cx.regExpProxy = proxy; |
| 4276 |
} |
| 4277 |
|
| 4278 |
public static RegExpProxy checkRegExpProxy(Context cx) |
| 4279 |
{ |
| 4280 |
RegExpProxy result = getRegExpProxy(cx); |
| 4281 |
if (result == null) { |
| 4282 |
throw Context.reportRuntimeError0("msg.no.regexp"); |
| 4283 |
} |
| 4284 |
return result; |
| 4285 |
} |
| 4286 |
|
| 4287 |
public static Scriptable wrapRegExp(Context cx, Scriptable scope, |
| 4288 |
Object compiled) { |
| 4289 |
return cx.getRegExpProxy().wrapRegExp(cx, scope, compiled); |
| 4290 |
} |
| 4291 |
|
| 4292 |
private static XMLLib currentXMLLib(Context cx) |
| 4293 |
{ |
| 4294 |
|
| 4295 |
if (cx.topCallScope == null) |
| 4296 |
throw new IllegalStateException(); |
| 4297 |
|
| 4298 |
XMLLib xmlLib = cx.cachedXMLLib; |
| 4299 |
if (xmlLib == null) { |
| 4300 |
xmlLib = XMLLib.extractFromScope(cx.topCallScope); |
| 4301 |
if (xmlLib == null) |
| 4302 |
throw new IllegalStateException(); |
| 4303 |
cx.cachedXMLLib = xmlLib; |
| 4304 |
} |
| 4305 |
|
| 4306 |
return xmlLib; |
| 4307 |
} |
| 4308 |
|
| 4309 |
|
| 4310 |
|
| 4311 |
|
| 4312 |
|
| 4313 |
|
| 4314 |
|
| 4315 |
public static String escapeAttributeValue(Object value, Context cx) |
| 4316 |
{ |
| 4317 |
XMLLib xmlLib = currentXMLLib(cx); |
| 4318 |
return xmlLib.escapeAttributeValue(value); |
| 4319 |
} |
| 4320 |
|
| 4321 |
|
| 4322 |
|
| 4323 |
|
| 4324 |
|
| 4325 |
|
| 4326 |
|
| 4327 |
public static String escapeTextValue(Object value, Context cx) |
| 4328 |
{ |
| 4329 |
XMLLib xmlLib = currentXMLLib(cx); |
| 4330 |
return xmlLib.escapeTextValue(value); |
| 4331 |
} |
| 4332 |
|
| 4333 |
public static Ref memberRef(Object obj, Object elem, |
| 4334 |
Context cx, int memberTypeFlags) |
| 4335 |
{ |
| 4336 |
if (!(obj instanceof XMLObject)) { |
| 4337 |
throw notXmlError(obj); |
| 4338 |
} |
| 4339 |
XMLObject xmlObject = (XMLObject)obj; |
| 4340 |
return xmlObject.memberRef(cx, elem, memberTypeFlags); |
| 4341 |
} |
| 4342 |
|
| 4343 |
public static Ref memberRef(Object obj, Object namespace, Object elem, |
| 4344 |
Context cx, int memberTypeFlags) |
| 4345 |
{ |
| 4346 |
if (!(obj instanceof XMLObject)) { |
| 4347 |
throw notXmlError(obj); |
| 4348 |
} |
| 4349 |
XMLObject xmlObject = (XMLObject)obj; |
| 4350 |
return xmlObject.memberRef(cx, namespace, elem, memberTypeFlags); |
| 4351 |
} |
| 4352 |
|
| 4353 |
public static Ref nameRef(Object name, Context cx, |
| 4354 |
Scriptable scope, int memberTypeFlags) |
| 4355 |
{ |
| 4356 |
XMLLib xmlLib = currentXMLLib(cx); |
| 4357 |
return xmlLib.nameRef(cx, name, scope, memberTypeFlags); |
| 4358 |
} |
| 4359 |
|
| 4360 |
public static Ref nameRef(Object namespace, Object name, Context cx, |
| 4361 |
Scriptable scope, int memberTypeFlags) |
| 4362 |
{ |
| 4363 |
XMLLib xmlLib = currentXMLLib(cx); |
| 4364 |
return xmlLib.nameRef(cx, namespace, name, scope, memberTypeFlags); |
| 4365 |
} |
| 4366 |
|
| 4367 |
private static void storeIndexResult(Context cx, int index) |
| 4368 |
{ |
| 4369 |
cx.scratchIndex = index; |
| 4370 |
} |
| 4371 |
|
| 4372 |
static int lastIndexResult(Context cx) |
| 4373 |
{ |
| 4374 |
return cx.scratchIndex; |
| 4375 |
} |
| 4376 |
|
| 4377 |
public static void storeUint32Result(Context cx, long value) |
| 4378 |
{ |
| 4379 |
if ((value >>> 32) != 0) |
| 4380 |
throw new IllegalArgumentException(); |
| 4381 |
cx.scratchUint32 = value; |
| 4382 |
} |
| 4383 |
|
| 4384 |
public static long lastUint32Result(Context cx) |
| 4385 |
{ |
| 4386 |
long value = cx.scratchUint32; |
| 4387 |
if ((value >>> 32) != 0) |
| 4388 |
throw new IllegalStateException(); |
| 4389 |
return value; |
| 4390 |
} |
| 4391 |
|
| 4392 |
private static void storeScriptable(Context cx, Scriptable value) |
| 4393 |
{ |
| 4394 |
|
| 4395 |
if (cx.scratchScriptable != null) |
| 4396 |
throw new IllegalStateException(); |
| 4397 |
cx.scratchScriptable = value; |
| 4398 |
} |
| 4399 |
|
| 4400 |
public static Scriptable lastStoredScriptable(Context cx) |
| 4401 |
{ |
| 4402 |
Scriptable result = cx.scratchScriptable; |
| 4403 |
cx.scratchScriptable = null; |
| 4404 |
return result; |
| 4405 |
} |
| 4406 |
|
| 4407 |
static String makeUrlForGeneratedScript |
| 4408 |
(boolean isEval, String masterScriptUrl, int masterScriptLine) |
| 4409 |
{ |
| 4410 |
if (isEval) { |
| 4411 |
return masterScriptUrl+'#'+masterScriptLine+"(eval)"; |
| 4412 |
} else { |
| 4413 |
return masterScriptUrl+'#'+masterScriptLine+"(Function)"; |
| 4414 |
} |
| 4415 |
} |
| 4416 |
|
| 4417 |
static boolean isGeneratedScript(String sourceUrl) { |
| 4418 |
|
| 4419 |
|
| 4420 |
return sourceUrl.indexOf("(eval)") >= 0 |
| 4421 |
|| sourceUrl.indexOf("(Function)") >= 0; |
| 4422 |
} |
| 4423 |
|
| 4424 |
|
| 4425 |
|
| 4426 |
|
| 4427 |
|
| 4428 |
static boolean isSymbol(Object obj) { |
| 4429 |
return (((obj instanceof NativeSymbol) && |
| 4430 |
((NativeSymbol)obj).isSymbol())) || (obj instanceof SymbolKey); |
| 4431 |
} |
| 4432 |
|
| 4433 |
private static RuntimeException errorWithClassName(String msg, Object val) |
| 4434 |
{ |
| 4435 |
return Context.reportRuntimeError1(msg, val.getClass().getName()); |
| 4436 |
} |
| 4437 |
|
| 4438 |
|
| 4439 |
|
| 4440 |
|
| 4441 |
|
| 4442 |
|
| 4443 |
|
| 4444 |
|
| 4445 |
public static JavaScriptException throwError(Context cx, Scriptable scope, |
| 4446 |
String message) { |
| 4447 |
int[] linep = { 0 }; |
| 4448 |
String filename = Context.getSourcePositionFromStack(linep); |
| 4449 |
final Scriptable error = newBuiltinObject(cx, scope, |
| 4450 |
TopLevel.Builtins.Error, new Object[] { message, filename, Integer.valueOf(linep[0]) }); |
| 4451 |
return new JavaScriptException(error, filename, linep[0]); |
| 4452 |
} |
| 4453 |
|
| 4454 |
|
| 4455 |
|
| 4456 |
|
| 4457 |
|
| 4458 |
|
| 4459 |
|
| 4460 |
|
| 4461 |
|
| 4462 |
public static JavaScriptException throwCustomError(Context cx, Scriptable scope, String constructorName, |
| 4463 |
String message) { |
| 4464 |
int[] linep = { 0 }; |
| 4465 |
String filename = Context.getSourcePositionFromStack(linep); |
| 4466 |
final Scriptable error = cx.newObject(scope, constructorName, |
| 4467 |
new Object[] { message, filename, Integer.valueOf(linep[0]) }); |
| 4468 |
return new JavaScriptException(error, filename, linep[0]); |
| 4469 |
} |
| 4470 |
|
| 4471 |
public static final Object[] emptyArgs = new Object[0]; |
| 4472 |
public static final String[] emptyStrings = new String[0]; |
| 4473 |
|
| 4474 |
|
| 4475 |
} |