| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
import java.io.*; |
| 10 |
import java.lang.reflect.*; |
| 11 |
import java.util.Map; |
| 12 |
import java.util.Date; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class NativeJavaObject implements Scriptable, Wrapper, Serializable |
| 26 |
{ |
| 27 |
static final long serialVersionUID = -6948590651130498591L; |
| 28 |
|
| 29 |
public NativeJavaObject() { } |
| 30 |
|
| 31 |
public NativeJavaObject(Scriptable scope, Object javaObject, |
| 32 |
Class<?> staticType) |
| 33 |
{ |
| 34 |
this(scope, javaObject, staticType, false); |
| 35 |
} |
| 36 |
|
| 37 |
public NativeJavaObject(Scriptable scope, Object javaObject, |
| 38 |
Class<?> staticType, boolean isAdapter) |
| 39 |
{ |
| 40 |
this.parent = scope; |
| 41 |
this.javaObject = javaObject; |
| 42 |
this.staticType = staticType; |
| 43 |
this.isAdapter = isAdapter; |
| 44 |
initMembers(); |
| 45 |
} |
| 46 |
|
| 47 |
protected void initMembers() { |
| 48 |
Class<?> dynamicType; |
| 49 |
if (javaObject != null) { |
| 50 |
dynamicType = javaObject.getClass(); |
| 51 |
} else { |
| 52 |
dynamicType = staticType; |
| 53 |
} |
| 54 |
members = JavaMembers.lookupClass(parent, dynamicType, staticType, |
| 55 |
isAdapter); |
| 56 |
fieldAndMethods |
| 57 |
= members.getFieldAndMethodsObjects(this, javaObject, false); |
| 58 |
} |
| 59 |
|
| 60 |
public boolean has(String name, Scriptable start) { |
| 61 |
return members.has(name, false); |
| 62 |
} |
| 63 |
|
| 64 |
public boolean has(int index, Scriptable start) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
public Object get(String name, Scriptable start) { |
| 69 |
if (fieldAndMethods != null) { |
| 70 |
Object result = fieldAndMethods.get(name); |
| 71 |
if (result != null) { |
| 72 |
return result; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
return members.get(this, name, javaObject, false); |
| 78 |
} |
| 79 |
|
| 80 |
public Object get(int index, Scriptable start) { |
| 81 |
throw members.reportMemberNotFound(Integer.toString(index)); |
| 82 |
} |
| 83 |
|
| 84 |
public void put(String name, Scriptable start, Object value) { |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
if (prototype == null || members.has(name, false)) |
| 89 |
members.put(this, name, javaObject, value, false); |
| 90 |
else |
| 91 |
prototype.put(name, prototype, value); |
| 92 |
} |
| 93 |
|
| 94 |
public void put(int index, Scriptable start, Object value) { |
| 95 |
throw members.reportMemberNotFound(Integer.toString(index)); |
| 96 |
} |
| 97 |
|
| 98 |
public boolean hasInstance(Scriptable value) { |
| 99 |
|
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
public void delete(String name) { |
| 104 |
} |
| 105 |
|
| 106 |
public void delete(int index) { |
| 107 |
} |
| 108 |
|
| 109 |
public Scriptable getPrototype() { |
| 110 |
if (prototype == null && javaObject instanceof String) { |
| 111 |
return TopLevel.getBuiltinPrototype( |
| 112 |
ScriptableObject.getTopLevelScope(parent), |
| 113 |
TopLevel.Builtins.String); |
| 114 |
} |
| 115 |
return prototype; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public void setPrototype(Scriptable m) { |
| 122 |
prototype = m; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public Scriptable getParentScope() { |
| 129 |
return parent; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public void setParentScope(Scriptable m) { |
| 136 |
parent = m; |
| 137 |
} |
| 138 |
|
| 139 |
public Object[] getIds() { |
| 140 |
return members.getIds(false); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
@Deprecated |
| 148 |
public static Object wrap(Scriptable scope, Object obj, Class<?> staticType) { |
| 149 |
|
| 150 |
Context cx = Context.getContext(); |
| 151 |
return cx.getWrapFactory().wrap(cx, scope, obj, staticType); |
| 152 |
} |
| 153 |
|
| 154 |
public Object unwrap() { |
| 155 |
return javaObject; |
| 156 |
} |
| 157 |
|
| 158 |
public String getClassName() { |
| 159 |
return "JavaObject"; |
| 160 |
} |
| 161 |
|
| 162 |
public Object getDefaultValue(Class<?> hint) |
| 163 |
{ |
| 164 |
Object value; |
| 165 |
if (hint == null) { |
| 166 |
if (javaObject instanceof Boolean) { |
| 167 |
hint = ScriptRuntime.BooleanClass; |
| 168 |
} |
| 169 |
} |
| 170 |
if (hint == null || hint == ScriptRuntime.StringClass) { |
| 171 |
value = javaObject.toString(); |
| 172 |
} else { |
| 173 |
String converterName; |
| 174 |
if (hint == ScriptRuntime.BooleanClass) { |
| 175 |
converterName = "booleanValue"; |
| 176 |
} else if (hint == ScriptRuntime.NumberClass) { |
| 177 |
converterName = "doubleValue"; |
| 178 |
} else { |
| 179 |
throw Context.reportRuntimeError0("msg.default.value"); |
| 180 |
} |
| 181 |
Object converterObject = get(converterName, this); |
| 182 |
if (converterObject instanceof Function) { |
| 183 |
Function f = (Function)converterObject; |
| 184 |
value = f.call(Context.getContext(), f.getParentScope(), |
| 185 |
this, ScriptRuntime.emptyArgs); |
| 186 |
} else { |
| 187 |
if (hint == ScriptRuntime.NumberClass |
| 188 |
&& javaObject instanceof Boolean) |
| 189 |
{ |
| 190 |
boolean b = ((Boolean)javaObject).booleanValue(); |
| 191 |
value = ScriptRuntime.wrapNumber(b ? 1.0 : 0.0); |
| 192 |
} else { |
| 193 |
value = javaObject.toString(); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
return value; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public static boolean canConvert(Object fromObj, Class<?> to) { |
| 206 |
int weight = getConversionWeight(fromObj, to); |
| 207 |
|
| 208 |
return (weight < CONVERSION_NONE); |
| 209 |
} |
| 210 |
|
| 211 |
private static final int JSTYPE_UNDEFINED = 0; |
| 212 |
private static final int JSTYPE_NULL = 1; |
| 213 |
private static final int JSTYPE_BOOLEAN = 2; |
| 214 |
private static final int JSTYPE_NUMBER = 3; |
| 215 |
private static final int JSTYPE_STRING = 4; |
| 216 |
private static final int JSTYPE_JAVA_CLASS = 5; |
| 217 |
private static final int JSTYPE_JAVA_OBJECT = 6; |
| 218 |
private static final int JSTYPE_JAVA_ARRAY = 7; |
| 219 |
private static final int JSTYPE_OBJECT = 8; |
| 220 |
|
| 221 |
static final byte CONVERSION_TRIVIAL = 1; |
| 222 |
static final byte CONVERSION_NONTRIVIAL = 0; |
| 223 |
static final byte CONVERSION_NONE = 99; |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
static int getConversionWeight(Object fromObj, Class<?> to) { |
| 235 |
int fromCode = getJSTypeCode(fromObj); |
| 236 |
|
| 237 |
switch (fromCode) { |
| 238 |
|
| 239 |
case JSTYPE_UNDEFINED: |
| 240 |
if (to == ScriptRuntime.StringClass || |
| 241 |
to == ScriptRuntime.ObjectClass) { |
| 242 |
return 1; |
| 243 |
} |
| 244 |
break; |
| 245 |
|
| 246 |
case JSTYPE_NULL: |
| 247 |
if (!to.isPrimitive()) { |
| 248 |
return 1; |
| 249 |
} |
| 250 |
break; |
| 251 |
|
| 252 |
case JSTYPE_BOOLEAN: |
| 253 |
|
| 254 |
if (to == Boolean.TYPE) { |
| 255 |
return 1; |
| 256 |
} |
| 257 |
else if (to == ScriptRuntime.BooleanClass) { |
| 258 |
return 2; |
| 259 |
} |
| 260 |
else if (to == ScriptRuntime.ObjectClass) { |
| 261 |
return 3; |
| 262 |
} |
| 263 |
else if (to == ScriptRuntime.StringClass) { |
| 264 |
return 4; |
| 265 |
} |
| 266 |
break; |
| 267 |
|
| 268 |
case JSTYPE_NUMBER: |
| 269 |
if (to.isPrimitive()) { |
| 270 |
if (to == Double.TYPE) { |
| 271 |
return 1; |
| 272 |
} |
| 273 |
else if (to != Boolean.TYPE) { |
| 274 |
return 1 + getSizeRank(to); |
| 275 |
} |
| 276 |
} |
| 277 |
else { |
| 278 |
if (to == ScriptRuntime.StringClass) { |
| 279 |
|
| 280 |
return 9; |
| 281 |
} |
| 282 |
else if (to == ScriptRuntime.ObjectClass) { |
| 283 |
return 10; |
| 284 |
} |
| 285 |
else if (ScriptRuntime.NumberClass.isAssignableFrom(to)) { |
| 286 |
|
| 287 |
return 2; |
| 288 |
} |
| 289 |
} |
| 290 |
break; |
| 291 |
|
| 292 |
case JSTYPE_STRING: |
| 293 |
if (to == ScriptRuntime.StringClass) { |
| 294 |
return 1; |
| 295 |
} |
| 296 |
else if (to.isInstance(fromObj)) { |
| 297 |
return 2; |
| 298 |
} |
| 299 |
else if (to.isPrimitive()) { |
| 300 |
if (to == Character.TYPE) { |
| 301 |
return 3; |
| 302 |
} else if (to != Boolean.TYPE) { |
| 303 |
return 4; |
| 304 |
} |
| 305 |
} |
| 306 |
break; |
| 307 |
|
| 308 |
case JSTYPE_JAVA_CLASS: |
| 309 |
if (to == ScriptRuntime.ClassClass) { |
| 310 |
return 1; |
| 311 |
} |
| 312 |
else if (to == ScriptRuntime.ObjectClass) { |
| 313 |
return 3; |
| 314 |
} |
| 315 |
else if (to == ScriptRuntime.StringClass) { |
| 316 |
return 4; |
| 317 |
} |
| 318 |
break; |
| 319 |
|
| 320 |
case JSTYPE_JAVA_OBJECT: |
| 321 |
case JSTYPE_JAVA_ARRAY: |
| 322 |
Object javaObj = fromObj; |
| 323 |
if (javaObj instanceof Wrapper) { |
| 324 |
javaObj = ((Wrapper)javaObj).unwrap(); |
| 325 |
} |
| 326 |
if (to.isInstance(javaObj)) { |
| 327 |
return CONVERSION_NONTRIVIAL; |
| 328 |
} |
| 329 |
if (to == ScriptRuntime.StringClass) { |
| 330 |
return 2; |
| 331 |
} |
| 332 |
else if (to.isPrimitive() && to != Boolean.TYPE) { |
| 333 |
return (fromCode == JSTYPE_JAVA_ARRAY) |
| 334 |
? CONVERSION_NONE : 2 + getSizeRank(to); |
| 335 |
} |
| 336 |
break; |
| 337 |
|
| 338 |
case JSTYPE_OBJECT: |
| 339 |
|
| 340 |
if (to != ScriptRuntime.ObjectClass && to.isInstance(fromObj)) { |
| 341 |
|
| 342 |
return 1; |
| 343 |
} |
| 344 |
if (to.isArray()) { |
| 345 |
if (fromObj instanceof NativeArray) { |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
return 2; |
| 350 |
} |
| 351 |
} |
| 352 |
else if (to == ScriptRuntime.ObjectClass) { |
| 353 |
return 3; |
| 354 |
} |
| 355 |
else if (to == ScriptRuntime.StringClass) { |
| 356 |
return 4; |
| 357 |
} |
| 358 |
else if (to == ScriptRuntime.DateClass) { |
| 359 |
if (fromObj instanceof NativeDate) { |
| 360 |
|
| 361 |
return 1; |
| 362 |
} |
| 363 |
} |
| 364 |
else if (to.isInterface()) { |
| 365 |
|
| 366 |
if (fromObj instanceof NativeFunction) { |
| 367 |
|
| 368 |
return 1; |
| 369 |
} |
| 370 |
if (fromObj instanceof NativeObject) { |
| 371 |
return 2; |
| 372 |
} |
| 373 |
return 12; |
| 374 |
} |
| 375 |
else if (to.isPrimitive() && to != Boolean.TYPE) { |
| 376 |
return 4 + getSizeRank(to); |
| 377 |
} |
| 378 |
break; |
| 379 |
} |
| 380 |
|
| 381 |
return CONVERSION_NONE; |
| 382 |
} |
| 383 |
|
| 384 |
static int getSizeRank(Class<?> aType) { |
| 385 |
if (aType == Double.TYPE) { |
| 386 |
return 1; |
| 387 |
} |
| 388 |
else if (aType == Float.TYPE) { |
| 389 |
return 2; |
| 390 |
} |
| 391 |
else if (aType == Long.TYPE) { |
| 392 |
return 3; |
| 393 |
} |
| 394 |
else if (aType == Integer.TYPE) { |
| 395 |
return 4; |
| 396 |
} |
| 397 |
else if (aType == Short.TYPE) { |
| 398 |
return 5; |
| 399 |
} |
| 400 |
else if (aType == Character.TYPE) { |
| 401 |
return 6; |
| 402 |
} |
| 403 |
else if (aType == Byte.TYPE) { |
| 404 |
return 7; |
| 405 |
} |
| 406 |
else if (aType == Boolean.TYPE) { |
| 407 |
return CONVERSION_NONE; |
| 408 |
} |
| 409 |
else { |
| 410 |
return 8; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
private static int getJSTypeCode(Object value) { |
| 415 |
if (value == null) { |
| 416 |
return JSTYPE_NULL; |
| 417 |
} |
| 418 |
else if (value == Undefined.instance) { |
| 419 |
return JSTYPE_UNDEFINED; |
| 420 |
} |
| 421 |
else if (value instanceof CharSequence) { |
| 422 |
return JSTYPE_STRING; |
| 423 |
} |
| 424 |
else if (value instanceof Number) { |
| 425 |
return JSTYPE_NUMBER; |
| 426 |
} |
| 427 |
else if (value instanceof Boolean) { |
| 428 |
return JSTYPE_BOOLEAN; |
| 429 |
} |
| 430 |
else if (value instanceof Scriptable) { |
| 431 |
if (value instanceof NativeJavaClass) { |
| 432 |
return JSTYPE_JAVA_CLASS; |
| 433 |
} |
| 434 |
else if (value instanceof NativeJavaArray) { |
| 435 |
return JSTYPE_JAVA_ARRAY; |
| 436 |
} |
| 437 |
else if (value instanceof Wrapper) { |
| 438 |
return JSTYPE_JAVA_OBJECT; |
| 439 |
} |
| 440 |
else { |
| 441 |
return JSTYPE_OBJECT; |
| 442 |
} |
| 443 |
} |
| 444 |
else if (value instanceof Class) { |
| 445 |
return JSTYPE_JAVA_CLASS; |
| 446 |
} |
| 447 |
else { |
| 448 |
Class<?> valueClass = value.getClass(); |
| 449 |
if (valueClass.isArray()) { |
| 450 |
return JSTYPE_JAVA_ARRAY; |
| 451 |
} |
| 452 |
else { |
| 453 |
return JSTYPE_JAVA_OBJECT; |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
@Deprecated |
| 465 |
public static Object coerceType(Class<?> type, Object value) |
| 466 |
{ |
| 467 |
return coerceTypeImpl(type, value); |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
static Object coerceTypeImpl(Class<?> type, Object value) |
| 475 |
{ |
| 476 |
if (value != null && value.getClass() == type) { |
| 477 |
return value; |
| 478 |
} |
| 479 |
|
| 480 |
switch (getJSTypeCode(value)) { |
| 481 |
|
| 482 |
case JSTYPE_NULL: |
| 483 |
|
| 484 |
if (type.isPrimitive()) { |
| 485 |
reportConversionError(value, type); |
| 486 |
} |
| 487 |
return null; |
| 488 |
|
| 489 |
case JSTYPE_UNDEFINED: |
| 490 |
if (type == ScriptRuntime.StringClass || |
| 491 |
type == ScriptRuntime.ObjectClass) { |
| 492 |
return "undefined"; |
| 493 |
} |
| 494 |
else { |
| 495 |
reportConversionError("undefined", type); |
| 496 |
} |
| 497 |
break; |
| 498 |
|
| 499 |
case JSTYPE_BOOLEAN: |
| 500 |
|
| 501 |
if (type == Boolean.TYPE || |
| 502 |
type == ScriptRuntime.BooleanClass || |
| 503 |
type == ScriptRuntime.ObjectClass) { |
| 504 |
return value; |
| 505 |
} |
| 506 |
else if (type == ScriptRuntime.StringClass) { |
| 507 |
return value.toString(); |
| 508 |
} |
| 509 |
else { |
| 510 |
reportConversionError(value, type); |
| 511 |
} |
| 512 |
break; |
| 513 |
|
| 514 |
case JSTYPE_NUMBER: |
| 515 |
if (type == ScriptRuntime.StringClass) { |
| 516 |
return ScriptRuntime.toString(value); |
| 517 |
} |
| 518 |
else if (type == ScriptRuntime.ObjectClass) { |
| 519 |
return coerceToNumber(Double.TYPE, value); |
| 520 |
} |
| 521 |
else if ((type.isPrimitive() && type != Boolean.TYPE) || |
| 522 |
ScriptRuntime.NumberClass.isAssignableFrom(type)) { |
| 523 |
return coerceToNumber(type, value); |
| 524 |
} |
| 525 |
else { |
| 526 |
reportConversionError(value, type); |
| 527 |
} |
| 528 |
break; |
| 529 |
|
| 530 |
case JSTYPE_STRING: |
| 531 |
if (type == ScriptRuntime.StringClass || type.isInstance(value)) { |
| 532 |
return value.toString(); |
| 533 |
} |
| 534 |
else if (type == Character.TYPE |
| 535 |
|| type == ScriptRuntime.CharacterClass) |
| 536 |
{ |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
if (((CharSequence)value).length() == 1) { |
| 542 |
return Character.valueOf(((CharSequence)value).charAt(0)); |
| 543 |
} |
| 544 |
else { |
| 545 |
return coerceToNumber(type, value); |
| 546 |
} |
| 547 |
} |
| 548 |
else if ((type.isPrimitive() && type != Boolean.TYPE) |
| 549 |
|| ScriptRuntime.NumberClass.isAssignableFrom(type)) |
| 550 |
{ |
| 551 |
return coerceToNumber(type, value); |
| 552 |
} |
| 553 |
else { |
| 554 |
reportConversionError(value, type); |
| 555 |
} |
| 556 |
break; |
| 557 |
|
| 558 |
case JSTYPE_JAVA_CLASS: |
| 559 |
if (value instanceof Wrapper) { |
| 560 |
value = ((Wrapper)value).unwrap(); |
| 561 |
} |
| 562 |
|
| 563 |
if (type == ScriptRuntime.ClassClass || |
| 564 |
type == ScriptRuntime.ObjectClass) { |
| 565 |
return value; |
| 566 |
} |
| 567 |
else if (type == ScriptRuntime.StringClass) { |
| 568 |
return value.toString(); |
| 569 |
} |
| 570 |
else { |
| 571 |
reportConversionError(value, type); |
| 572 |
} |
| 573 |
break; |
| 574 |
|
| 575 |
case JSTYPE_JAVA_OBJECT: |
| 576 |
case JSTYPE_JAVA_ARRAY: |
| 577 |
if (value instanceof Wrapper) { |
| 578 |
value = ((Wrapper)value).unwrap(); |
| 579 |
} |
| 580 |
if (type.isPrimitive()) { |
| 581 |
if (type == Boolean.TYPE) { |
| 582 |
reportConversionError(value, type); |
| 583 |
} |
| 584 |
return coerceToNumber(type, value); |
| 585 |
} |
| 586 |
else { |
| 587 |
if (type == ScriptRuntime.StringClass) { |
| 588 |
return value.toString(); |
| 589 |
} |
| 590 |
else { |
| 591 |
if (type.isInstance(value)) { |
| 592 |
return value; |
| 593 |
} |
| 594 |
else { |
| 595 |
reportConversionError(value, type); |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
break; |
| 600 |
|
| 601 |
case JSTYPE_OBJECT: |
| 602 |
if (type == ScriptRuntime.StringClass) { |
| 603 |
return ScriptRuntime.toString(value); |
| 604 |
} |
| 605 |
else if (type.isPrimitive()) { |
| 606 |
if (type == Boolean.TYPE) { |
| 607 |
reportConversionError(value, type); |
| 608 |
} |
| 609 |
return coerceToNumber(type, value); |
| 610 |
} |
| 611 |
else if (type.isInstance(value)) { |
| 612 |
return value; |
| 613 |
} |
| 614 |
else if (type == ScriptRuntime.DateClass |
| 615 |
&& value instanceof NativeDate) |
| 616 |
{ |
| 617 |
double time = ((NativeDate)value).getJSTimeValue(); |
| 618 |
|
| 619 |
return new Date((long)time); |
| 620 |
} |
| 621 |
else if (type.isArray() && value instanceof NativeArray) { |
| 622 |
|
| 623 |
|
| 624 |
NativeArray array = (NativeArray) value; |
| 625 |
long length = array.getLength(); |
| 626 |
Class<?> arrayType = type.getComponentType(); |
| 627 |
Object Result = Array.newInstance(arrayType, (int)length); |
| 628 |
for (int i = 0 ; i < length ; ++i) { |
| 629 |
try { |
| 630 |
Array.set(Result, i, coerceTypeImpl( |
| 631 |
arrayType, array.get(i, array))); |
| 632 |
} |
| 633 |
catch (EvaluatorException ee) { |
| 634 |
reportConversionError(value, type); |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
return Result; |
| 639 |
} |
| 640 |
else if (value instanceof Wrapper) { |
| 641 |
value = ((Wrapper)value).unwrap(); |
| 642 |
if (type.isInstance(value)) |
| 643 |
return value; |
| 644 |
reportConversionError(value, type); |
| 645 |
} |
| 646 |
else if (type.isInterface() && (value instanceof NativeObject |
| 647 |
|| value instanceof NativeFunction)) { |
| 648 |
|
| 649 |
return createInterfaceAdapter(type, (ScriptableObject) value); |
| 650 |
} else { |
| 651 |
reportConversionError(value, type); |
| 652 |
} |
| 653 |
break; |
| 654 |
} |
| 655 |
|
| 656 |
return value; |
| 657 |
} |
| 658 |
|
| 659 |
protected static Object createInterfaceAdapter(Class<?>type, ScriptableObject so) { |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
Object key = Kit.makeHashKeyFromPair(COERCED_INTERFACE_KEY, type); |
| 667 |
Object old = so.getAssociatedValue(key); |
| 668 |
if (old != null) { |
| 669 |
|
| 670 |
return old; |
| 671 |
} |
| 672 |
Context cx = Context.getContext(); |
| 673 |
Object glue = InterfaceAdapter.create(cx, type, so); |
| 674 |
|
| 675 |
glue = so.associateValue(key, glue); |
| 676 |
return glue; |
| 677 |
} |
| 678 |
|
| 679 |
private static Object coerceToNumber(Class<?> type, Object value) |
| 680 |
{ |
| 681 |
Class<?> valueClass = value.getClass(); |
| 682 |
|
| 683 |
|
| 684 |
if (type == Character.TYPE || type == ScriptRuntime.CharacterClass) { |
| 685 |
if (valueClass == ScriptRuntime.CharacterClass) { |
| 686 |
return value; |
| 687 |
} |
| 688 |
return Character.valueOf((char)toInteger(value, |
| 689 |
ScriptRuntime.CharacterClass, |
| 690 |
Character.MIN_VALUE, |
| 691 |
Character.MAX_VALUE)); |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
if (type == ScriptRuntime.ObjectClass || |
| 696 |
type == ScriptRuntime.DoubleClass || type == Double.TYPE) { |
| 697 |
return valueClass == ScriptRuntime.DoubleClass |
| 698 |
? value |
| 699 |
: new Double(toDouble(value)); |
| 700 |
} |
| 701 |
|
| 702 |
if (type == ScriptRuntime.FloatClass || type == Float.TYPE) { |
| 703 |
if (valueClass == ScriptRuntime.FloatClass) { |
| 704 |
return value; |
| 705 |
} |
| 706 |
else { |
| 707 |
double number = toDouble(value); |
| 708 |
if (Double.isInfinite(number) || Double.isNaN(number) |
| 709 |
|| number == 0.0) { |
| 710 |
return new Float((float)number); |
| 711 |
} |
| 712 |
else { |
| 713 |
double absNumber = Math.abs(number); |
| 714 |
if (absNumber < Float.MIN_VALUE) { |
| 715 |
return new Float((number > 0.0) ? +0.0 : -0.0); |
| 716 |
} |
| 717 |
else if (absNumber > Float.MAX_VALUE) { |
| 718 |
return new Float((number > 0.0) ? |
| 719 |
Float.POSITIVE_INFINITY : |
| 720 |
Float.NEGATIVE_INFINITY); |
| 721 |
} |
| 722 |
else { |
| 723 |
return new Float((float)number); |
| 724 |
} |
| 725 |
} |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
if (type == ScriptRuntime.IntegerClass || type == Integer.TYPE) { |
| 731 |
if (valueClass == ScriptRuntime.IntegerClass) { |
| 732 |
return value; |
| 733 |
} |
| 734 |
else { |
| 735 |
return Integer.valueOf((int)toInteger(value, |
| 736 |
ScriptRuntime.IntegerClass, |
| 737 |
Integer.MIN_VALUE, |
| 738 |
Integer.MAX_VALUE)); |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
if (type == ScriptRuntime.LongClass || type == Long.TYPE) { |
| 743 |
if (valueClass == ScriptRuntime.LongClass) { |
| 744 |
return value; |
| 745 |
} else { |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
final double max = Double.longBitsToDouble(0x43dfffffffffffffL); |
| 754 |
final double min = Double.longBitsToDouble(0xc3e0000000000000L); |
| 755 |
return Long.valueOf(toInteger(value, |
| 756 |
ScriptRuntime.LongClass, |
| 757 |
min, |
| 758 |
max)); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
if (type == ScriptRuntime.ShortClass || type == Short.TYPE) { |
| 763 |
if (valueClass == ScriptRuntime.ShortClass) { |
| 764 |
return value; |
| 765 |
} |
| 766 |
else { |
| 767 |
return Short.valueOf((short)toInteger(value, |
| 768 |
ScriptRuntime.ShortClass, |
| 769 |
Short.MIN_VALUE, |
| 770 |
Short.MAX_VALUE)); |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
if (type == ScriptRuntime.ByteClass || type == Byte.TYPE) { |
| 775 |
if (valueClass == ScriptRuntime.ByteClass) { |
| 776 |
return value; |
| 777 |
} |
| 778 |
else { |
| 779 |
return Byte.valueOf((byte)toInteger(value, |
| 780 |
ScriptRuntime.ByteClass, |
| 781 |
Byte.MIN_VALUE, |
| 782 |
Byte.MAX_VALUE)); |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
return new Double(toDouble(value)); |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
private static double toDouble(Object value) |
| 791 |
{ |
| 792 |
if (value instanceof Number) { |
| 793 |
return ((Number)value).doubleValue(); |
| 794 |
} |
| 795 |
else if (value instanceof String) { |
| 796 |
return ScriptRuntime.toNumber((String)value); |
| 797 |
} |
| 798 |
else if (value instanceof Scriptable) { |
| 799 |
if (value instanceof Wrapper) { |
| 800 |
|
| 801 |
return toDouble(((Wrapper)value).unwrap()); |
| 802 |
} |
| 803 |
else { |
| 804 |
return ScriptRuntime.toNumber(value); |
| 805 |
} |
| 806 |
} |
| 807 |
else { |
| 808 |
Method meth; |
| 809 |
try { |
| 810 |
meth = value.getClass().getMethod("doubleValue", (Class [])null); |
| 811 |
} |
| 812 |
catch (NoSuchMethodException e) { |
| 813 |
meth = null; |
| 814 |
} |
| 815 |
catch (SecurityException e) { |
| 816 |
meth = null; |
| 817 |
} |
| 818 |
if (meth != null) { |
| 819 |
try { |
| 820 |
return ((Number)meth.invoke(value, (Object [])null)).doubleValue(); |
| 821 |
} |
| 822 |
catch (IllegalAccessException e) { |
| 823 |
|
| 824 |
reportConversionError(value, Double.TYPE); |
| 825 |
} |
| 826 |
catch (InvocationTargetException e) { |
| 827 |
|
| 828 |
reportConversionError(value, Double.TYPE); |
| 829 |
} |
| 830 |
} |
| 831 |
return ScriptRuntime.toNumber(value.toString()); |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
private static long toInteger(Object value, Class<?> type, |
| 836 |
double min, double max) |
| 837 |
{ |
| 838 |
double d = toDouble(value); |
| 839 |
|
| 840 |
if (Double.isInfinite(d) || Double.isNaN(d)) { |
| 841 |
|
| 842 |
reportConversionError(ScriptRuntime.toString(value), type); |
| 843 |
} |
| 844 |
|
| 845 |
if (d > 0.0) { |
| 846 |
d = Math.floor(d); |
| 847 |
} |
| 848 |
else { |
| 849 |
d = Math.ceil(d); |
| 850 |
} |
| 851 |
|
| 852 |
if (d < min || d > max) { |
| 853 |
|
| 854 |
reportConversionError(ScriptRuntime.toString(value), type); |
| 855 |
} |
| 856 |
return (long)d; |
| 857 |
} |
| 858 |
|
| 859 |
static void reportConversionError(Object value, Class<?> type) |
| 860 |
{ |
| 861 |
|
| 862 |
|
| 863 |
throw Context.reportRuntimeError2( |
| 864 |
"msg.conversion.not.allowed", |
| 865 |
String.valueOf(value), |
| 866 |
JavaMembers.javaSignature(type)); |
| 867 |
} |
| 868 |
|
| 869 |
private void writeObject(ObjectOutputStream out) |
| 870 |
throws IOException |
| 871 |
{ |
| 872 |
out.defaultWriteObject(); |
| 873 |
|
| 874 |
out.writeBoolean(isAdapter); |
| 875 |
if (isAdapter) { |
| 876 |
if (adapter_writeAdapterObject == null) { |
| 877 |
throw new IOException(); |
| 878 |
} |
| 879 |
Object[] args = { javaObject, out }; |
| 880 |
try { |
| 881 |
adapter_writeAdapterObject.invoke(null, args); |
| 882 |
} catch (Exception ex) { |
| 883 |
throw new IOException(); |
| 884 |
} |
| 885 |
} else { |
| 886 |
out.writeObject(javaObject); |
| 887 |
} |
| 888 |
|
| 889 |
if (staticType != null) { |
| 890 |
out.writeObject(staticType.getClass().getName()); |
| 891 |
} else { |
| 892 |
out.writeObject(null); |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
private void readObject(ObjectInputStream in) |
| 897 |
throws IOException, ClassNotFoundException |
| 898 |
{ |
| 899 |
in.defaultReadObject(); |
| 900 |
|
| 901 |
isAdapter = in.readBoolean(); |
| 902 |
if (isAdapter) { |
| 903 |
if (adapter_readAdapterObject == null) |
| 904 |
throw new ClassNotFoundException(); |
| 905 |
Object[] args = { this, in }; |
| 906 |
try { |
| 907 |
javaObject = adapter_readAdapterObject.invoke(null, args); |
| 908 |
} catch (Exception ex) { |
| 909 |
throw new IOException(); |
| 910 |
} |
| 911 |
} else { |
| 912 |
javaObject = in.readObject(); |
| 913 |
} |
| 914 |
|
| 915 |
String className = (String)in.readObject(); |
| 916 |
if (className != null) { |
| 917 |
staticType = Class.forName(className); |
| 918 |
} else { |
| 919 |
staticType = null; |
| 920 |
} |
| 921 |
|
| 922 |
initMembers(); |
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
protected Scriptable prototype; |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
protected Scriptable parent; |
| 934 |
|
| 935 |
protected transient Object javaObject; |
| 936 |
|
| 937 |
protected transient Class<?> staticType; |
| 938 |
protected transient JavaMembers members; |
| 939 |
private transient Map<String,FieldAndMethods> fieldAndMethods; |
| 940 |
protected transient boolean isAdapter; |
| 941 |
|
| 942 |
private static final Object COERCED_INTERFACE_KEY = "Coerced Interface"; |
| 943 |
private static Method adapter_writeAdapterObject; |
| 944 |
private static Method adapter_readAdapterObject; |
| 945 |
|
| 946 |
static { |
| 947 |
|
| 948 |
Class<?>[] sig2 = new Class[2]; |
| 949 |
Class<?> cl = Kit.classOrNull("org.mozilla.javascript.JavaAdapter"); |
| 950 |
if (cl != null) { |
| 951 |
try { |
| 952 |
sig2[0] = ScriptRuntime.ObjectClass; |
| 953 |
sig2[1] = Kit.classOrNull("java.io.ObjectOutputStream"); |
| 954 |
adapter_writeAdapterObject = cl.getMethod("writeAdapterObject", |
| 955 |
sig2); |
| 956 |
|
| 957 |
sig2[0] = ScriptRuntime.ScriptableClass; |
| 958 |
sig2[1] = Kit.classOrNull("java.io.ObjectInputStream"); |
| 959 |
adapter_readAdapterObject = cl.getMethod("readAdapterObject", |
| 960 |
sig2); |
| 961 |
|
| 962 |
} catch (NoSuchMethodException e) { |
| 963 |
adapter_writeAdapterObject = null; |
| 964 |
adapter_readAdapterObject = null; |
| 965 |
} |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
} |