| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
public class BaseFunction extends IdScriptableObject implements Function |
| 15 |
{ |
| 16 |
|
| 17 |
static final long serialVersionUID = 5311394446546053859L; |
| 18 |
|
| 19 |
private static final Object FUNCTION_TAG = "Function"; |
| 20 |
|
| 21 |
static void init(Scriptable scope, boolean sealed) |
| 22 |
{ |
| 23 |
BaseFunction obj = new BaseFunction(); |
| 24 |
|
| 25 |
obj.prototypePropertyAttributes = DONTENUM | READONLY | PERMANENT; |
| 26 |
obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed); |
| 27 |
} |
| 28 |
|
| 29 |
public BaseFunction() |
| 30 |
{ |
| 31 |
} |
| 32 |
|
| 33 |
public BaseFunction(Scriptable scope, Scriptable prototype) |
| 34 |
{ |
| 35 |
super(scope, prototype); |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public String getClassName() { |
| 40 |
return "Function"; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
@Override |
| 49 |
public String getTypeOf() |
| 50 |
{ |
| 51 |
return avoidObjectDetection() ? "undefined" : "function"; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
@Override |
| 69 |
public boolean hasInstance(Scriptable instance) |
| 70 |
{ |
| 71 |
Object protoProp = ScriptableObject.getProperty(this, "prototype"); |
| 72 |
if (protoProp instanceof Scriptable) { |
| 73 |
return ScriptRuntime.jsDelegatesTo(instance, (Scriptable)protoProp); |
| 74 |
} |
| 75 |
throw ScriptRuntime.typeError1("msg.instanceof.bad.prototype", |
| 76 |
getFunctionName()); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private static final int |
| 82 |
Id_length = 1, |
| 83 |
Id_arity = 2, |
| 84 |
Id_name = 3, |
| 85 |
Id_prototype = 4, |
| 86 |
Id_arguments = 5, |
| 87 |
|
| 88 |
MAX_INSTANCE_ID = 5; |
| 89 |
|
| 90 |
@Override |
| 91 |
protected int getMaxInstanceId() |
| 92 |
{ |
| 93 |
return MAX_INSTANCE_ID; |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
protected int findInstanceIdInfo(String s) |
| 98 |
{ |
| 99 |
int id; |
| 100 |
|
| 101 |
L0: { id = 0; String X = null; int c; |
| 102 |
L: switch (s.length()) { |
| 103 |
case 4: X="name";id=Id_name; break L; |
| 104 |
case 5: X="arity";id=Id_arity; break L; |
| 105 |
case 6: X="length";id=Id_length; break L; |
| 106 |
case 9: c=s.charAt(0); |
| 107 |
if (c=='a') { X="arguments";id=Id_arguments; } |
| 108 |
else if (c=='p') { X="prototype";id=Id_prototype; } |
| 109 |
break L; |
| 110 |
} |
| 111 |
if (X!=null && X!=s && !X.equals(s)) id = 0; |
| 112 |
break L0; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
if (id == 0) return super.findInstanceIdInfo(s); |
| 118 |
|
| 119 |
int attr; |
| 120 |
switch (id) { |
| 121 |
case Id_length: |
| 122 |
case Id_arity: |
| 123 |
case Id_name: |
| 124 |
attr = DONTENUM | READONLY | PERMANENT; |
| 125 |
break; |
| 126 |
case Id_prototype: |
| 127 |
|
| 128 |
if (!hasPrototypeProperty()) { |
| 129 |
return 0; |
| 130 |
} |
| 131 |
attr = prototypePropertyAttributes; |
| 132 |
break; |
| 133 |
case Id_arguments: |
| 134 |
attr = argumentsAttributes; |
| 135 |
break; |
| 136 |
default: throw new IllegalStateException(); |
| 137 |
} |
| 138 |
return instanceIdInfo(attr, id); |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
protected String getInstanceIdName(int id) |
| 143 |
{ |
| 144 |
switch (id) { |
| 145 |
case Id_length: return "length"; |
| 146 |
case Id_arity: return "arity"; |
| 147 |
case Id_name: return "name"; |
| 148 |
case Id_prototype: return "prototype"; |
| 149 |
case Id_arguments: return "arguments"; |
| 150 |
} |
| 151 |
return super.getInstanceIdName(id); |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
protected Object getInstanceIdValue(int id) |
| 156 |
{ |
| 157 |
switch (id) { |
| 158 |
case Id_length: return ScriptRuntime.wrapInt(getLength()); |
| 159 |
case Id_arity: return ScriptRuntime.wrapInt(getArity()); |
| 160 |
case Id_name: return getFunctionName(); |
| 161 |
case Id_prototype: return getPrototypeProperty(); |
| 162 |
case Id_arguments: return getArguments(); |
| 163 |
} |
| 164 |
return super.getInstanceIdValue(id); |
| 165 |
} |
| 166 |
|
| 167 |
@Override |
| 168 |
protected void setInstanceIdValue(int id, Object value) |
| 169 |
{ |
| 170 |
switch (id) { |
| 171 |
case Id_prototype: |
| 172 |
if ((prototypePropertyAttributes & READONLY) == 0) { |
| 173 |
prototypeProperty = (value != null) |
| 174 |
? value : UniqueTag.NULL_VALUE; |
| 175 |
} |
| 176 |
return; |
| 177 |
case Id_arguments: |
| 178 |
if (value == NOT_FOUND) { |
| 179 |
|
| 180 |
Kit.codeBug(); |
| 181 |
} |
| 182 |
if (defaultHas("arguments")) { |
| 183 |
defaultPut("arguments", value); |
| 184 |
} else if ((argumentsAttributes & READONLY) == 0) { |
| 185 |
argumentsObj = value; |
| 186 |
} |
| 187 |
return; |
| 188 |
case Id_name: |
| 189 |
case Id_arity: |
| 190 |
case Id_length: |
| 191 |
return; |
| 192 |
} |
| 193 |
super.setInstanceIdValue(id, value); |
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
protected void setInstanceIdAttributes(int id, int attr) |
| 198 |
{ |
| 199 |
switch (id) { |
| 200 |
case Id_prototype: |
| 201 |
prototypePropertyAttributes = attr; |
| 202 |
return; |
| 203 |
case Id_arguments: |
| 204 |
argumentsAttributes = attr; |
| 205 |
return; |
| 206 |
} |
| 207 |
super.setInstanceIdAttributes(id, attr); |
| 208 |
} |
| 209 |
|
| 210 |
@Override |
| 211 |
protected void fillConstructorProperties(IdFunctionObject ctor) |
| 212 |
{ |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
ctor.setPrototype(this); |
| 217 |
super.fillConstructorProperties(ctor); |
| 218 |
} |
| 219 |
|
| 220 |
@Override |
| 221 |
protected void initPrototypeId(int id) |
| 222 |
{ |
| 223 |
String s; |
| 224 |
int arity; |
| 225 |
switch (id) { |
| 226 |
case Id_constructor: arity=1; s="constructor"; break; |
| 227 |
case Id_toString: arity=0; s="toString"; break; |
| 228 |
case Id_toSource: arity=1; s="toSource"; break; |
| 229 |
case Id_apply: arity=2; s="apply"; break; |
| 230 |
case Id_call: arity=1; s="call"; break; |
| 231 |
case Id_bind: arity=1; s="bind"; break; |
| 232 |
default: throw new IllegalArgumentException(String.valueOf(id)); |
| 233 |
} |
| 234 |
initPrototypeMethod(FUNCTION_TAG, id, s, arity); |
| 235 |
} |
| 236 |
|
| 237 |
static boolean isApply(IdFunctionObject f) { |
| 238 |
return f.hasTag(FUNCTION_TAG) && f.methodId() == Id_apply; |
| 239 |
} |
| 240 |
|
| 241 |
static boolean isApplyOrCall(IdFunctionObject f) { |
| 242 |
if(f.hasTag(FUNCTION_TAG)) { |
| 243 |
switch(f.methodId()) { |
| 244 |
case Id_apply: |
| 245 |
case Id_call: |
| 246 |
return true; |
| 247 |
} |
| 248 |
} |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
@Override |
| 253 |
public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, |
| 254 |
Scriptable thisObj, Object[] args) |
| 255 |
{ |
| 256 |
if (!f.hasTag(FUNCTION_TAG)) { |
| 257 |
return super.execIdCall(f, cx, scope, thisObj, args); |
| 258 |
} |
| 259 |
int id = f.methodId(); |
| 260 |
switch (id) { |
| 261 |
case Id_constructor: |
| 262 |
return jsConstructor(cx, scope, args); |
| 263 |
|
| 264 |
case Id_toString: { |
| 265 |
BaseFunction realf = realFunction(thisObj, f); |
| 266 |
int indent = ScriptRuntime.toInt32(args, 0); |
| 267 |
return realf.decompile(indent, 0); |
| 268 |
} |
| 269 |
|
| 270 |
case Id_toSource: { |
| 271 |
BaseFunction realf = realFunction(thisObj, f); |
| 272 |
int indent = 0; |
| 273 |
int flags = Decompiler.TO_SOURCE_FLAG; |
| 274 |
if (args.length != 0) { |
| 275 |
indent = ScriptRuntime.toInt32(args[0]); |
| 276 |
if (indent >= 0) { |
| 277 |
flags = 0; |
| 278 |
} else { |
| 279 |
indent = 0; |
| 280 |
} |
| 281 |
} |
| 282 |
return realf.decompile(indent, flags); |
| 283 |
} |
| 284 |
|
| 285 |
case Id_apply: |
| 286 |
case Id_call: |
| 287 |
return ScriptRuntime.applyOrCall(id == Id_apply, |
| 288 |
cx, scope, thisObj, args); |
| 289 |
|
| 290 |
case Id_bind: |
| 291 |
if ( !(thisObj instanceof Callable) ) { |
| 292 |
throw ScriptRuntime.notFunctionError(thisObj); |
| 293 |
} |
| 294 |
Callable targetFunction = (Callable) thisObj; |
| 295 |
int argc = args.length; |
| 296 |
final Scriptable boundThis; |
| 297 |
final Object[] boundArgs; |
| 298 |
if (argc > 0) { |
| 299 |
boundThis = ScriptRuntime.toObjectOrNull(cx, args[0], scope); |
| 300 |
boundArgs = new Object[argc-1]; |
| 301 |
System.arraycopy(args, 1, boundArgs, 0, argc-1); |
| 302 |
} else { |
| 303 |
boundThis = null; |
| 304 |
boundArgs = ScriptRuntime.emptyArgs; |
| 305 |
} |
| 306 |
return new BoundFunction(cx, scope, targetFunction, boundThis, boundArgs); |
| 307 |
} |
| 308 |
throw new IllegalArgumentException(String.valueOf(id)); |
| 309 |
} |
| 310 |
|
| 311 |
private BaseFunction realFunction(Scriptable thisObj, IdFunctionObject f) |
| 312 |
{ |
| 313 |
Object x = thisObj.getDefaultValue(ScriptRuntime.FunctionClass); |
| 314 |
if (x instanceof Delegator) { |
| 315 |
x = ((Delegator)x).getDelegee(); |
| 316 |
} |
| 317 |
if (x instanceof BaseFunction) { |
| 318 |
return (BaseFunction)x; |
| 319 |
} |
| 320 |
throw ScriptRuntime.typeError1("msg.incompat.call", |
| 321 |
f.getFunctionName()); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public void setImmunePrototypeProperty(Object value) |
| 329 |
{ |
| 330 |
if ((prototypePropertyAttributes & READONLY) != 0) { |
| 331 |
throw new IllegalStateException(); |
| 332 |
} |
| 333 |
prototypeProperty = (value != null) ? value : UniqueTag.NULL_VALUE; |
| 334 |
prototypePropertyAttributes = DONTENUM | PERMANENT | READONLY; |
| 335 |
} |
| 336 |
|
| 337 |
protected Scriptable getClassPrototype() |
| 338 |
{ |
| 339 |
Object protoVal = getPrototypeProperty(); |
| 340 |
if (protoVal instanceof Scriptable) { |
| 341 |
return (Scriptable) protoVal; |
| 342 |
} |
| 343 |
return ScriptableObject.getObjectPrototype(this); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
public Object call(Context cx, Scriptable scope, Scriptable thisObj, |
| 350 |
Object[] args) |
| 351 |
{ |
| 352 |
return Undefined.instance; |
| 353 |
} |
| 354 |
|
| 355 |
public Scriptable construct(Context cx, Scriptable scope, Object[] args) |
| 356 |
{ |
| 357 |
Scriptable result = createObject(cx, scope); |
| 358 |
if (result != null) { |
| 359 |
Object val = call(cx, scope, result, args); |
| 360 |
if (val instanceof Scriptable) { |
| 361 |
result = (Scriptable)val; |
| 362 |
} |
| 363 |
} else { |
| 364 |
Object val = call(cx, scope, null, args); |
| 365 |
if (!(val instanceof Scriptable)) { |
| 366 |
|
| 367 |
|
| 368 |
throw new IllegalStateException( |
| 369 |
"Bad implementaion of call as constructor, name=" |
| 370 |
+getFunctionName()+" in "+getClass().getName()); |
| 371 |
} |
| 372 |
result = (Scriptable)val; |
| 373 |
if (result.getPrototype() == null) { |
| 374 |
Scriptable proto = getClassPrototype(); |
| 375 |
if (result != proto) { |
| 376 |
result.setPrototype(proto); |
| 377 |
} |
| 378 |
} |
| 379 |
if (result.getParentScope() == null) { |
| 380 |
Scriptable parent = getParentScope(); |
| 381 |
if (result != parent) { |
| 382 |
result.setParentScope(parent); |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
return result; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
public Scriptable createObject(Context cx, Scriptable scope) |
| 400 |
{ |
| 401 |
Scriptable newInstance = new NativeObject(); |
| 402 |
newInstance.setPrototype(getClassPrototype()); |
| 403 |
newInstance.setParentScope(getParentScope()); |
| 404 |
return newInstance; |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
String decompile(int indent, int flags) |
| 416 |
{ |
| 417 |
StringBuilder sb = new StringBuilder(); |
| 418 |
boolean justbody = (0 != (flags & Decompiler.ONLY_BODY_FLAG)); |
| 419 |
if (!justbody) { |
| 420 |
sb.append("function "); |
| 421 |
sb.append(getFunctionName()); |
| 422 |
sb.append("() {\n\t"); |
| 423 |
} |
| 424 |
sb.append("[native code, arity="); |
| 425 |
sb.append(getArity()); |
| 426 |
sb.append("]\n"); |
| 427 |
if (!justbody) { |
| 428 |
sb.append("}\n"); |
| 429 |
} |
| 430 |
return sb.toString(); |
| 431 |
} |
| 432 |
|
| 433 |
public int getArity() { return 0; } |
| 434 |
|
| 435 |
public int getLength() { return 0; } |
| 436 |
|
| 437 |
public String getFunctionName() { |
| 438 |
return ""; |
| 439 |
} |
| 440 |
|
| 441 |
protected boolean hasPrototypeProperty() { |
| 442 |
return prototypeProperty != null || this instanceof NativeFunction; |
| 443 |
} |
| 444 |
|
| 445 |
protected Object getPrototypeProperty() { |
| 446 |
Object result = prototypeProperty; |
| 447 |
if (result == null) { |
| 448 |
|
| 449 |
|
| 450 |
if (this instanceof NativeFunction) { |
| 451 |
result = setupDefaultPrototype(); |
| 452 |
} else { |
| 453 |
result = Undefined.instance; |
| 454 |
} |
| 455 |
} else if (result == UniqueTag.NULL_VALUE) { |
| 456 |
result = null; |
| 457 |
} |
| 458 |
return result; |
| 459 |
} |
| 460 |
|
| 461 |
private synchronized Object setupDefaultPrototype() { |
| 462 |
if (prototypeProperty != null) { |
| 463 |
return prototypeProperty; |
| 464 |
} |
| 465 |
NativeObject obj = new NativeObject(); |
| 466 |
final int attr = ScriptableObject.DONTENUM; |
| 467 |
obj.defineProperty("constructor", this, attr); |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
prototypeProperty = obj; |
| 472 |
Scriptable proto = getObjectPrototype(this); |
| 473 |
if (proto != obj) { |
| 474 |
|
| 475 |
obj.setPrototype(proto); |
| 476 |
} |
| 477 |
return obj; |
| 478 |
} |
| 479 |
|
| 480 |
private Object getArguments() |
| 481 |
{ |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
Object value = defaultHas("arguments") ? defaultGet("arguments") : argumentsObj; |
| 486 |
if (value != NOT_FOUND) { |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
return value; |
| 493 |
} |
| 494 |
Context cx = Context.getContext(); |
| 495 |
NativeCall activation = ScriptRuntime.findFunctionActivation(cx, this); |
| 496 |
return (activation == null) |
| 497 |
? null |
| 498 |
: activation.get("arguments", activation); |
| 499 |
} |
| 500 |
|
| 501 |
private static Object jsConstructor(Context cx, Scriptable scope, |
| 502 |
Object[] args) |
| 503 |
{ |
| 504 |
int arglen = args.length; |
| 505 |
StringBuilder sourceBuf = new StringBuilder(); |
| 506 |
|
| 507 |
sourceBuf.append("function "); |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
if (cx.getLanguageVersion() != Context.VERSION_1_2) { |
| 515 |
sourceBuf.append("anonymous"); |
| 516 |
} |
| 517 |
sourceBuf.append('('); |
| 518 |
|
| 519 |
|
| 520 |
for (int i = 0; i < arglen - 1; i++) { |
| 521 |
if (i > 0) { |
| 522 |
sourceBuf.append(','); |
| 523 |
} |
| 524 |
sourceBuf.append(ScriptRuntime.toString(args[i])); |
| 525 |
} |
| 526 |
sourceBuf.append(") {"); |
| 527 |
if (arglen != 0) { |
| 528 |
|
| 529 |
String funBody = ScriptRuntime.toString(args[arglen - 1]); |
| 530 |
sourceBuf.append(funBody); |
| 531 |
} |
| 532 |
sourceBuf.append("\n}"); |
| 533 |
String source = sourceBuf.toString(); |
| 534 |
|
| 535 |
int[] linep = new int[1]; |
| 536 |
String filename = Context.getSourcePositionFromStack(linep); |
| 537 |
if (filename == null) { |
| 538 |
filename = "<eval'ed string>"; |
| 539 |
linep[0] = 1; |
| 540 |
} |
| 541 |
|
| 542 |
String sourceURI = ScriptRuntime. |
| 543 |
makeUrlForGeneratedScript(false, filename, linep[0]); |
| 544 |
|
| 545 |
Scriptable global = ScriptableObject.getTopLevelScope(scope); |
| 546 |
|
| 547 |
ErrorReporter reporter; |
| 548 |
reporter = DefaultErrorReporter.forEval(cx.getErrorReporter()); |
| 549 |
|
| 550 |
Evaluator evaluator = Context.createInterpreter(); |
| 551 |
if (evaluator == null) { |
| 552 |
throw new JavaScriptException("Interpreter not present", |
| 553 |
filename, linep[0]); |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
return cx.compileFunction(global, source, evaluator, reporter, |
| 559 |
sourceURI, 1, null); |
| 560 |
} |
| 561 |
|
| 562 |
@Override |
| 563 |
protected int findPrototypeId(String s) |
| 564 |
{ |
| 565 |
int id; |
| 566 |
|
| 567 |
|
| 568 |
L0: { id = 0; String X = null; int c; |
| 569 |
L: switch (s.length()) { |
| 570 |
case 4: c=s.charAt(0); |
| 571 |
if (c=='b') { X="bind";id=Id_bind; } |
| 572 |
else if (c=='c') { X="call";id=Id_call; } |
| 573 |
break L; |
| 574 |
case 5: X="apply";id=Id_apply; break L; |
| 575 |
case 8: c=s.charAt(3); |
| 576 |
if (c=='o') { X="toSource";id=Id_toSource; } |
| 577 |
else if (c=='t') { X="toString";id=Id_toString; } |
| 578 |
break L; |
| 579 |
case 11: X="constructor";id=Id_constructor; break L; |
| 580 |
} |
| 581 |
if (X!=null && X!=s && !X.equals(s)) id = 0; |
| 582 |
break L0; |
| 583 |
} |
| 584 |
|
| 585 |
return id; |
| 586 |
} |
| 587 |
|
| 588 |
private static final int |
| 589 |
Id_constructor = 1, |
| 590 |
Id_toString = 2, |
| 591 |
Id_toSource = 3, |
| 592 |
Id_apply = 4, |
| 593 |
Id_call = 5, |
| 594 |
Id_bind = 6, |
| 595 |
|
| 596 |
MAX_PROTOTYPE_ID = Id_bind; |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
private Object prototypeProperty; |
| 601 |
private Object argumentsObj = NOT_FOUND; |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
private int prototypePropertyAttributes = PERMANENT|DONTENUM; |
| 607 |
private int argumentsAttributes = PERMANENT|DONTENUM; |
| 608 |
} |
| 609 |
|