| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
final class Arguments extends IdScriptableObject |
| 18 |
{ |
| 19 |
static final long serialVersionUID = 4275508002492040609L; |
| 20 |
|
| 21 |
private static final String FTAG = "Arguments"; |
| 22 |
|
| 23 |
public Arguments(NativeCall activation) |
| 24 |
{ |
| 25 |
this.activation = activation; |
| 26 |
|
| 27 |
Scriptable parent = activation.getParentScope(); |
| 28 |
setParentScope(parent); |
| 29 |
setPrototype(ScriptableObject.getObjectPrototype(parent)); |
| 30 |
|
| 31 |
args = activation.originalArgs; |
| 32 |
lengthObj = Integer.valueOf(args.length); |
| 33 |
|
| 34 |
NativeFunction f = activation.function; |
| 35 |
calleeObj = f; |
| 36 |
|
| 37 |
int version = f.getLanguageVersion(); |
| 38 |
if (version <= Context.VERSION_1_3 |
| 39 |
&& version != Context.VERSION_DEFAULT) |
| 40 |
{ |
| 41 |
callerObj = null; |
| 42 |
} else { |
| 43 |
callerObj = NOT_FOUND; |
| 44 |
} |
| 45 |
|
| 46 |
defineProperty(SymbolKey.ITERATOR, iteratorMethod, ScriptableObject.DONTENUM); |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public String getClassName() |
| 51 |
{ |
| 52 |
return FTAG; |
| 53 |
} |
| 54 |
|
| 55 |
private Object arg(int index) { |
| 56 |
if (index < 0 || args.length <= index) return NOT_FOUND; |
| 57 |
return args[index]; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
private void putIntoActivation(int index, Object value) { |
| 63 |
String argName = activation.function.getParamOrVarName(index); |
| 64 |
activation.put(argName, activation, value); |
| 65 |
} |
| 66 |
|
| 67 |
private Object getFromActivation(int index) { |
| 68 |
String argName = activation.function.getParamOrVarName(index); |
| 69 |
return activation.get(argName, activation); |
| 70 |
} |
| 71 |
|
| 72 |
private void replaceArg(int index, Object value) { |
| 73 |
if (sharedWithActivation(index)) { |
| 74 |
putIntoActivation(index, value); |
| 75 |
} |
| 76 |
synchronized (this) { |
| 77 |
if (args == activation.originalArgs) { |
| 78 |
args = args.clone(); |
| 79 |
} |
| 80 |
args[index] = value; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
private void removeArg(int index) { |
| 85 |
synchronized (this) { |
| 86 |
if (args[index] != NOT_FOUND) { |
| 87 |
if (args == activation.originalArgs) { |
| 88 |
args = args.clone(); |
| 89 |
} |
| 90 |
args[index] = NOT_FOUND; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
@Override |
| 98 |
public boolean has(int index, Scriptable start) |
| 99 |
{ |
| 100 |
if (arg(index) != NOT_FOUND) { |
| 101 |
return true; |
| 102 |
} |
| 103 |
return super.has(index, start); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public Object get(int index, Scriptable start) |
| 108 |
{ |
| 109 |
final Object value = arg(index); |
| 110 |
if (value == NOT_FOUND) { |
| 111 |
return super.get(index, start); |
| 112 |
} else { |
| 113 |
if (sharedWithActivation(index)) { |
| 114 |
return getFromActivation(index); |
| 115 |
} else { |
| 116 |
return value; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
private boolean sharedWithActivation(int index) |
| 122 |
{ |
| 123 |
Context cx = Context.getContext(); |
| 124 |
if (cx.isStrictMode()) { |
| 125 |
return false; |
| 126 |
} |
| 127 |
NativeFunction f = activation.function; |
| 128 |
int definedCount = f.getParamCount(); |
| 129 |
if (index < definedCount) { |
| 130 |
|
| 131 |
|
| 132 |
if (index < definedCount - 1) { |
| 133 |
String argName = f.getParamOrVarName(index); |
| 134 |
for (int i = index + 1; i < definedCount; i++) { |
| 135 |
if (argName.equals(f.getParamOrVarName(i))) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
return true; |
| 141 |
} |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public void put(int index, Scriptable start, Object value) |
| 147 |
{ |
| 148 |
if (arg(index) == NOT_FOUND) { |
| 149 |
super.put(index, start, value); |
| 150 |
} else { |
| 151 |
replaceArg(index, value); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public void put(String name, Scriptable start, Object value) |
| 157 |
{ |
| 158 |
super.put(name, start, value); |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public void delete(int index) |
| 163 |
{ |
| 164 |
if (0 <= index && index < args.length) { |
| 165 |
removeArg(index); |
| 166 |
} |
| 167 |
super.delete(index); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
private static final int |
| 173 |
Id_callee = 1, |
| 174 |
Id_length = 2, |
| 175 |
Id_caller = 3, |
| 176 |
|
| 177 |
MAX_INSTANCE_ID = Id_caller; |
| 178 |
|
| 179 |
@Override |
| 180 |
protected int getMaxInstanceId() |
| 181 |
{ |
| 182 |
return MAX_INSTANCE_ID; |
| 183 |
} |
| 184 |
|
| 185 |
@Override |
| 186 |
protected int findInstanceIdInfo(String s) |
| 187 |
{ |
| 188 |
int id; |
| 189 |
|
| 190 |
L0: { id = 0; String X = null; int c; |
| 191 |
int s_length = s.length(); |
| 192 |
if (s_length==6) { |
| 193 |
c=s.charAt(5); |
| 194 |
if (c=='e') { X="callee";id=Id_callee; } |
| 195 |
else if (c=='h') { X="length";id=Id_length; } |
| 196 |
else if (c=='r') { X="caller";id=Id_caller; } |
| 197 |
} |
| 198 |
if (X!=null && X!=s && !X.equals(s)) id = 0; |
| 199 |
break L0; |
| 200 |
} |
| 201 |
|
| 202 |
Context cx = Context.getContext(); |
| 203 |
if (cx.isStrictMode()) { |
| 204 |
if (id == Id_callee || id == Id_caller) { |
| 205 |
return super.findInstanceIdInfo(s); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
if (id == 0) return super.findInstanceIdInfo(s); |
| 211 |
|
| 212 |
int attr; |
| 213 |
switch (id) { |
| 214 |
case Id_callee: |
| 215 |
attr = calleeAttr; |
| 216 |
break; |
| 217 |
case Id_caller: |
| 218 |
attr = callerAttr; |
| 219 |
break; |
| 220 |
case Id_length: |
| 221 |
attr = lengthAttr; |
| 222 |
break; |
| 223 |
default: throw new IllegalStateException(); |
| 224 |
} |
| 225 |
return instanceIdInfo(attr, id); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
@Override |
| 231 |
protected String getInstanceIdName(int id) |
| 232 |
{ |
| 233 |
switch (id) { |
| 234 |
case Id_callee: return "callee"; |
| 235 |
case Id_length: return "length"; |
| 236 |
case Id_caller: return "caller"; |
| 237 |
} |
| 238 |
return null; |
| 239 |
} |
| 240 |
|
| 241 |
@Override |
| 242 |
protected Object getInstanceIdValue(int id) |
| 243 |
{ |
| 244 |
switch (id) { |
| 245 |
case Id_callee: return calleeObj; |
| 246 |
case Id_length: return lengthObj; |
| 247 |
case Id_caller: { |
| 248 |
Object value = callerObj; |
| 249 |
if (value == UniqueTag.NULL_VALUE) { value = null; } |
| 250 |
else if (value == null) { |
| 251 |
NativeCall caller = activation.parentActivationCall; |
| 252 |
if (caller != null) { |
| 253 |
value = caller.get("arguments", caller); |
| 254 |
} |
| 255 |
} |
| 256 |
return value; |
| 257 |
} |
| 258 |
} |
| 259 |
return super.getInstanceIdValue(id); |
| 260 |
} |
| 261 |
|
| 262 |
@Override |
| 263 |
protected void setInstanceIdValue(int id, Object value) |
| 264 |
{ |
| 265 |
switch (id) { |
| 266 |
case Id_callee: calleeObj = value; return; |
| 267 |
case Id_length: lengthObj = value; return; |
| 268 |
case Id_caller: |
| 269 |
callerObj = (value != null) ? value : UniqueTag.NULL_VALUE; |
| 270 |
return; |
| 271 |
} |
| 272 |
super.setInstanceIdValue(id, value); |
| 273 |
} |
| 274 |
|
| 275 |
@Override |
| 276 |
protected void setInstanceIdAttributes(int id, int attr) |
| 277 |
{ |
| 278 |
switch (id) { |
| 279 |
case Id_callee: calleeAttr = attr; return; |
| 280 |
case Id_length: lengthAttr = attr; return; |
| 281 |
case Id_caller: callerAttr = attr; return; |
| 282 |
} |
| 283 |
super.setInstanceIdAttributes(id, attr); |
| 284 |
} |
| 285 |
|
| 286 |
@Override |
| 287 |
Object[] getIds(boolean getNonEnumerable, boolean getSymbols) |
| 288 |
{ |
| 289 |
Object[] ids = super.getIds(getNonEnumerable, getSymbols); |
| 290 |
if (args.length != 0) { |
| 291 |
boolean[] present = new boolean[args.length]; |
| 292 |
int extraCount = args.length; |
| 293 |
for (int i = 0; i != ids.length; ++i) { |
| 294 |
Object id = ids[i]; |
| 295 |
if (id instanceof Integer) { |
| 296 |
int index = ((Integer)id).intValue(); |
| 297 |
if (0 <= index && index < args.length) { |
| 298 |
if (!present[index]) { |
| 299 |
present[index] = true; |
| 300 |
extraCount--; |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
if (!getNonEnumerable) { |
| 306 |
for (int i = 0; i < present.length; i++) { |
| 307 |
if (!present[i] && super.has(i, this)) { |
| 308 |
present[i] = true; |
| 309 |
extraCount--; |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
if (extraCount != 0) { |
| 314 |
Object[] tmp = new Object[extraCount + ids.length]; |
| 315 |
System.arraycopy(ids, 0, tmp, extraCount, ids.length); |
| 316 |
ids = tmp; |
| 317 |
int offset = 0; |
| 318 |
for (int i = 0; i != args.length; ++i) { |
| 319 |
if (present == null || !present[i]) { |
| 320 |
ids[offset] = Integer.valueOf(i); |
| 321 |
++offset; |
| 322 |
} |
| 323 |
} |
| 324 |
if (offset != extraCount) Kit.codeBug(); |
| 325 |
} |
| 326 |
} |
| 327 |
return ids; |
| 328 |
} |
| 329 |
|
| 330 |
@Override |
| 331 |
protected ScriptableObject getOwnPropertyDescriptor(Context cx, Object id) { |
| 332 |
if (id instanceof Scriptable) { |
| 333 |
return super.getOwnPropertyDescriptor(cx, id); |
| 334 |
} |
| 335 |
double d = ScriptRuntime.toNumber(id); |
| 336 |
int index = (int) d; |
| 337 |
if (d != index) { |
| 338 |
return super.getOwnPropertyDescriptor(cx, id); |
| 339 |
} |
| 340 |
Object value = arg(index); |
| 341 |
if (value == NOT_FOUND) { |
| 342 |
return super.getOwnPropertyDescriptor(cx, id); |
| 343 |
} |
| 344 |
if (sharedWithActivation(index)) { |
| 345 |
value = getFromActivation(index); |
| 346 |
} |
| 347 |
if (super.has(index, this)) { |
| 348 |
ScriptableObject desc = super.getOwnPropertyDescriptor(cx, id); |
| 349 |
desc.put("value", desc, value); |
| 350 |
return desc; |
| 351 |
} else { |
| 352 |
Scriptable scope = getParentScope(); |
| 353 |
if (scope == null) scope = this; |
| 354 |
return buildDataDescriptor(scope, value, EMPTY); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
@Override |
| 359 |
protected void defineOwnProperty(Context cx, Object id, |
| 360 |
ScriptableObject desc, |
| 361 |
boolean checkValid) { |
| 362 |
super.defineOwnProperty(cx, id, desc, checkValid); |
| 363 |
|
| 364 |
double d = ScriptRuntime.toNumber(id); |
| 365 |
int index = (int) d; |
| 366 |
if (d != index) return; |
| 367 |
|
| 368 |
Object value = arg(index); |
| 369 |
if (value == NOT_FOUND) return; |
| 370 |
|
| 371 |
if (isAccessorDescriptor(desc)) { |
| 372 |
removeArg(index); |
| 373 |
return; |
| 374 |
} |
| 375 |
|
| 376 |
Object newValue = getProperty(desc, "value"); |
| 377 |
if (newValue == NOT_FOUND) return; |
| 378 |
|
| 379 |
replaceArg(index, newValue); |
| 380 |
|
| 381 |
if (isFalse(getProperty(desc, "writable"))) { |
| 382 |
removeArg(index); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
void defineAttributesForStrictMode() { |
| 393 |
Context cx = Context.getContext(); |
| 394 |
if (!cx.isStrictMode()) { |
| 395 |
return; |
| 396 |
} |
| 397 |
setGetterOrSetter("caller", 0, new ThrowTypeError("caller"), true); |
| 398 |
setGetterOrSetter("caller", 0, new ThrowTypeError("caller"), false); |
| 399 |
setGetterOrSetter("callee", 0, new ThrowTypeError("callee"), true); |
| 400 |
setGetterOrSetter("callee", 0, new ThrowTypeError("callee"), false); |
| 401 |
setAttributes("caller", DONTENUM | PERMANENT); |
| 402 |
setAttributes("callee", DONTENUM | PERMANENT); |
| 403 |
callerObj = null; |
| 404 |
calleeObj = null; |
| 405 |
} |
| 406 |
|
| 407 |
private static BaseFunction iteratorMethod = new BaseFunction() { |
| 408 |
@Override |
| 409 |
public Object call(Context cx, Scriptable scope, Scriptable thisObj, |
| 410 |
Object[] args) { |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
return new NativeArrayIterator(scope, thisObj); |
| 416 |
} |
| 417 |
}; |
| 418 |
|
| 419 |
private static class ThrowTypeError extends BaseFunction { |
| 420 |
private String propertyName; |
| 421 |
|
| 422 |
ThrowTypeError(String propertyName) { |
| 423 |
this.propertyName = propertyName; |
| 424 |
} |
| 425 |
|
| 426 |
@Override |
| 427 |
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { |
| 428 |
throw ScriptRuntime.typeError1("msg.arguments.not.access.strict", propertyName); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
private Object callerObj; |
| 438 |
private Object calleeObj; |
| 439 |
private Object lengthObj; |
| 440 |
|
| 441 |
private int callerAttr = DONTENUM; |
| 442 |
private int calleeAttr = DONTENUM; |
| 443 |
private int lengthAttr = DONTENUM; |
| 444 |
|
| 445 |
private NativeCall activation; |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
private Object[] args; |
| 451 |
} |