| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
import java.util.*; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
public class NativeObject extends IdScriptableObject implements Map |
| 17 |
{ |
| 18 |
static final long serialVersionUID = -6345305608474346996L; |
| 19 |
|
| 20 |
private static final Object OBJECT_TAG = "Object"; |
| 21 |
|
| 22 |
static void init(Scriptable scope, boolean sealed) |
| 23 |
{ |
| 24 |
NativeObject obj = new NativeObject(); |
| 25 |
obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed); |
| 26 |
} |
| 27 |
|
| 28 |
@Override |
| 29 |
public String getClassName() |
| 30 |
{ |
| 31 |
return "Object"; |
| 32 |
} |
| 33 |
|
| 34 |
@Override |
| 35 |
public String toString() |
| 36 |
{ |
| 37 |
return ScriptRuntime.defaultObjectToString(this); |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
protected void fillConstructorProperties(IdFunctionObject ctor) |
| 42 |
{ |
| 43 |
addIdFunctionProperty(ctor, OBJECT_TAG, ConstructorId_getPrototypeOf, |
| 44 |
"getPrototypeOf", 1); |
| 45 |
addIdFunctionProperty(ctor, OBJECT_TAG, ConstructorId_keys, |
| 46 |
"keys", 1); |
| 47 |
addIdFunctionProperty(ctor, OBJECT_TAG, ConstructorId_getOwnPropertyNames, |
| 48 |
"getOwnPropertyNames", 1); |
| 49 |
addIdFunctionProperty(ctor, OBJECT_TAG, ConstructorId_getOwnPropertySymbols, |
| 50 |
"getOwnPropertySymbols", 1); |
| 51 |
addIdFunctionProperty(ctor, OBJECT_TAG, ConstructorId_getOwnPropertyDescriptor, |
| 52 |
"getOwnPropertyDescriptor", 2); |
| 53 |
addIdFunctionProperty(ctor, OBJECT_TAG, ConstructorId_defineProperty, |
| 54 |
"defineProperty", 3); |
| 55 |
addIdFunctionProperty(ctor, OBJECT_TAG, ConstructorId_isExtensible, |
| 56 |
"isExtensible", 1); |
| 57 |
addIdFunctionProperty(ctor, OBJECT_TAG, ConstructorId_preventExtensions, |
| 58 |
"preventExtensions", 1); |
| 59 |
addIdFunctionProperty(ctor, OBJECT_TAG, ConstructorId_defineProperties, |
| 60 |
"defineProperties", 2); |
| 61 |
addIdFunctionProperty(ctor, OBJECT_TAG, ConstructorId_create, |
| 62 |
"create", 2); |
| 63 |
addIdFunctionProperty(ctor, OBJECT_TAG, ConstructorId_isSealed, |
| 64 |
"isSealed", 1); |
| 65 |
addIdFunctionProperty(ctor, OBJECT_TAG, ConstructorId_isFrozen, |
| 66 |
"isFrozen", 1); |
| 67 |
addIdFunctionProperty(ctor, OBJECT_TAG, ConstructorId_seal, |
| 68 |
"seal", 1); |
| 69 |
addIdFunctionProperty(ctor, OBJECT_TAG, ConstructorId_freeze, |
| 70 |
"freeze", 1); |
| 71 |
addIdFunctionProperty(ctor, OBJECT_TAG, ConstructorId_assign, |
| 72 |
"assign", 2); |
| 73 |
addIdFunctionProperty(ctor, OBJECT_TAG, ConstructorId_is, |
| 74 |
"is", 2); |
| 75 |
super.fillConstructorProperties(ctor); |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
protected void initPrototypeId(int id) |
| 80 |
{ |
| 81 |
String s; |
| 82 |
int arity; |
| 83 |
switch (id) { |
| 84 |
case Id_constructor: arity=1; s="constructor"; break; |
| 85 |
case Id_toString: arity=0; s="toString"; break; |
| 86 |
case Id_toLocaleString: arity=0; s="toLocaleString"; break; |
| 87 |
case Id_valueOf: arity=0; s="valueOf"; break; |
| 88 |
case Id_hasOwnProperty: arity=1; s="hasOwnProperty"; break; |
| 89 |
case Id_propertyIsEnumerable: |
| 90 |
arity=1; s="propertyIsEnumerable"; break; |
| 91 |
case Id_isPrototypeOf: arity=1; s="isPrototypeOf"; break; |
| 92 |
case Id_toSource: arity=0; s="toSource"; break; |
| 93 |
case Id___defineGetter__: |
| 94 |
arity=2; s="__defineGetter__"; break; |
| 95 |
case Id___defineSetter__: |
| 96 |
arity=2; s="__defineSetter__"; break; |
| 97 |
case Id___lookupGetter__: |
| 98 |
arity=1; s="__lookupGetter__"; break; |
| 99 |
case Id___lookupSetter__: |
| 100 |
arity=1; s="__lookupSetter__"; break; |
| 101 |
default: throw new IllegalArgumentException(String.valueOf(id)); |
| 102 |
} |
| 103 |
initPrototypeMethod(OBJECT_TAG, id, s, arity); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, |
| 108 |
Scriptable thisObj, Object[] args) |
| 109 |
{ |
| 110 |
if (!f.hasTag(OBJECT_TAG)) { |
| 111 |
return super.execIdCall(f, cx, scope, thisObj, args); |
| 112 |
} |
| 113 |
int id = f.methodId(); |
| 114 |
switch (id) { |
| 115 |
case Id_constructor: { |
| 116 |
if (thisObj != null) { |
| 117 |
|
| 118 |
return f.construct(cx, scope, args); |
| 119 |
} |
| 120 |
if (args.length == 0 || args[0] == null |
| 121 |
|| args[0] == Undefined.instance) |
| 122 |
{ |
| 123 |
return new NativeObject(); |
| 124 |
} |
| 125 |
return ScriptRuntime.toObject(cx, scope, args[0]); |
| 126 |
} |
| 127 |
|
| 128 |
case Id_toLocaleString: { |
| 129 |
Object toString = ScriptableObject.getProperty(thisObj, "toString"); |
| 130 |
if(!(toString instanceof Callable)) { |
| 131 |
throw ScriptRuntime.notFunctionError(toString); |
| 132 |
} |
| 133 |
Callable fun = (Callable)toString; |
| 134 |
return fun.call(cx, scope, thisObj, ScriptRuntime.emptyArgs); |
| 135 |
} |
| 136 |
|
| 137 |
case Id_toString: { |
| 138 |
if (cx.hasFeature(Context.FEATURE_TO_STRING_AS_SOURCE)) { |
| 139 |
String s = ScriptRuntime.defaultObjectToSource(cx, scope, |
| 140 |
thisObj, args); |
| 141 |
int L = s.length(); |
| 142 |
if (L != 0 && s.charAt(0) == '(' && s.charAt(L - 1) == ')') { |
| 143 |
|
| 144 |
s = s.substring(1, L - 1); |
| 145 |
} |
| 146 |
return s; |
| 147 |
} |
| 148 |
return ScriptRuntime.defaultObjectToString(thisObj); |
| 149 |
} |
| 150 |
|
| 151 |
case Id_valueOf: |
| 152 |
return thisObj; |
| 153 |
|
| 154 |
case Id_hasOwnProperty: { |
| 155 |
boolean result; |
| 156 |
Object arg = args.length < 1 ? Undefined.instance : args[0]; |
| 157 |
if (arg instanceof Symbol) { |
| 158 |
result = ensureSymbolScriptable(thisObj).has((Symbol) arg, thisObj); |
| 159 |
} else { |
| 160 |
String s = ScriptRuntime.toStringIdOrIndex(cx, arg); |
| 161 |
if (s == null) { |
| 162 |
int index = ScriptRuntime.lastIndexResult(cx); |
| 163 |
result = thisObj.has(index, thisObj); |
| 164 |
} else { |
| 165 |
result = thisObj.has(s, thisObj); |
| 166 |
} |
| 167 |
} |
| 168 |
return ScriptRuntime.wrapBoolean(result); |
| 169 |
} |
| 170 |
|
| 171 |
case Id_propertyIsEnumerable: { |
| 172 |
boolean result; |
| 173 |
Object arg = args.length < 1 ? Undefined.instance : args[0]; |
| 174 |
|
| 175 |
if (arg instanceof Symbol) { |
| 176 |
result = ((SymbolScriptable)thisObj).has((Symbol)arg, thisObj); |
| 177 |
if (result && thisObj instanceof ScriptableObject) { |
| 178 |
ScriptableObject so = (ScriptableObject)thisObj; |
| 179 |
int attrs = so.getAttributes((Symbol)arg); |
| 180 |
result = ((attrs & ScriptableObject.DONTENUM) == 0); |
| 181 |
} |
| 182 |
} else { |
| 183 |
String s = ScriptRuntime.toStringIdOrIndex(cx, arg); |
| 184 |
if (s == null) { |
| 185 |
int index = ScriptRuntime.lastIndexResult(cx); |
| 186 |
result = thisObj.has(index, thisObj); |
| 187 |
if (result && thisObj instanceof ScriptableObject) { |
| 188 |
ScriptableObject so = (ScriptableObject) thisObj; |
| 189 |
int attrs = so.getAttributes(index); |
| 190 |
result = ((attrs & ScriptableObject.DONTENUM) == 0); |
| 191 |
} |
| 192 |
} else { |
| 193 |
result = thisObj.has(s, thisObj); |
| 194 |
if (result && thisObj instanceof ScriptableObject) { |
| 195 |
ScriptableObject so = (ScriptableObject) thisObj; |
| 196 |
int attrs = so.getAttributes(s); |
| 197 |
result = ((attrs & ScriptableObject.DONTENUM) == 0); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
return ScriptRuntime.wrapBoolean(result); |
| 202 |
} |
| 203 |
|
| 204 |
case Id_isPrototypeOf: { |
| 205 |
boolean result = false; |
| 206 |
if (args.length != 0 && args[0] instanceof Scriptable) { |
| 207 |
Scriptable v = (Scriptable) args[0]; |
| 208 |
do { |
| 209 |
v = v.getPrototype(); |
| 210 |
if (v == thisObj) { |
| 211 |
result = true; |
| 212 |
break; |
| 213 |
} |
| 214 |
} while (v != null); |
| 215 |
} |
| 216 |
return ScriptRuntime.wrapBoolean(result); |
| 217 |
} |
| 218 |
|
| 219 |
case Id_toSource: |
| 220 |
return ScriptRuntime.defaultObjectToSource(cx, scope, thisObj, |
| 221 |
args); |
| 222 |
case Id___defineGetter__: |
| 223 |
case Id___defineSetter__: |
| 224 |
{ |
| 225 |
if (args.length < 2 || !(args[1] instanceof Callable)) { |
| 226 |
Object badArg = (args.length >= 2 ? args[1] |
| 227 |
: Undefined.instance); |
| 228 |
throw ScriptRuntime.notFunctionError(badArg); |
| 229 |
} |
| 230 |
if (!(thisObj instanceof ScriptableObject)) { |
| 231 |
throw Context.reportRuntimeError2( |
| 232 |
"msg.extend.scriptable", |
| 233 |
thisObj.getClass().getName(), |
| 234 |
String.valueOf(args[0])); |
| 235 |
} |
| 236 |
ScriptableObject so = (ScriptableObject)thisObj; |
| 237 |
String name = ScriptRuntime.toStringIdOrIndex(cx, args[0]); |
| 238 |
int index = (name != null ? 0 |
| 239 |
: ScriptRuntime.lastIndexResult(cx)); |
| 240 |
Callable getterOrSetter = (Callable)args[1]; |
| 241 |
boolean isSetter = (id == Id___defineSetter__); |
| 242 |
so.setGetterOrSetter(name, index, getterOrSetter, isSetter); |
| 243 |
if (so instanceof NativeArray) |
| 244 |
((NativeArray)so).setDenseOnly(false); |
| 245 |
} |
| 246 |
return Undefined.instance; |
| 247 |
|
| 248 |
case Id___lookupGetter__: |
| 249 |
case Id___lookupSetter__: |
| 250 |
{ |
| 251 |
if (args.length < 1 || |
| 252 |
!(thisObj instanceof ScriptableObject)) |
| 253 |
return Undefined.instance; |
| 254 |
|
| 255 |
ScriptableObject so = (ScriptableObject)thisObj; |
| 256 |
String name = ScriptRuntime.toStringIdOrIndex(cx, args[0]); |
| 257 |
int index = (name != null ? 0 |
| 258 |
: ScriptRuntime.lastIndexResult(cx)); |
| 259 |
boolean isSetter = (id == Id___lookupSetter__); |
| 260 |
Object gs; |
| 261 |
for (;;) { |
| 262 |
gs = so.getGetterOrSetter(name, index, isSetter); |
| 263 |
if (gs != null) |
| 264 |
break; |
| 265 |
|
| 266 |
|
| 267 |
Scriptable v = so.getPrototype(); |
| 268 |
if (v == null) |
| 269 |
break; |
| 270 |
if (v instanceof ScriptableObject) |
| 271 |
so = (ScriptableObject)v; |
| 272 |
else |
| 273 |
break; |
| 274 |
} |
| 275 |
if (gs != null) |
| 276 |
return gs; |
| 277 |
} |
| 278 |
return Undefined.instance; |
| 279 |
|
| 280 |
case ConstructorId_getPrototypeOf: |
| 281 |
{ |
| 282 |
Object arg = args.length < 1 ? Undefined.instance : args[0]; |
| 283 |
Scriptable obj = getCompatibleObject(cx, scope, arg); |
| 284 |
return obj.getPrototype(); |
| 285 |
} |
| 286 |
case ConstructorId_keys: |
| 287 |
{ |
| 288 |
Object arg = args.length < 1 ? Undefined.instance : args[0]; |
| 289 |
Scriptable obj = getCompatibleObject(cx, scope, arg); |
| 290 |
Object[] ids = obj.getIds(); |
| 291 |
for (int i = 0; i < ids.length; i++) { |
| 292 |
ids[i] = ScriptRuntime.toString(ids[i]); |
| 293 |
} |
| 294 |
return cx.newArray(scope, ids); |
| 295 |
} |
| 296 |
case ConstructorId_getOwnPropertyNames: |
| 297 |
{ |
| 298 |
Object arg = args.length < 1 ? Undefined.instance : args[0]; |
| 299 |
Scriptable s = getCompatibleObject(cx, scope, arg); |
| 300 |
ScriptableObject obj = ensureScriptableObject(s); |
| 301 |
Object[] ids = obj.getIds(true, false); |
| 302 |
for (int i = 0; i < ids.length; i++) { |
| 303 |
ids[i] = ScriptRuntime.toString(ids[i]); |
| 304 |
} |
| 305 |
return cx.newArray(scope, ids); |
| 306 |
} |
| 307 |
case ConstructorId_getOwnPropertySymbols: |
| 308 |
{ |
| 309 |
Object arg = args.length < 1 ? Undefined.instance : args[0]; |
| 310 |
Scriptable s = getCompatibleObject(cx, scope, arg); |
| 311 |
ScriptableObject obj = ensureScriptableObject(s); |
| 312 |
Object[] ids = obj.getIds(true, true); |
| 313 |
ArrayList<Object> syms = new ArrayList<Object>(); |
| 314 |
for (int i = 0; i < ids.length; i++) { |
| 315 |
if (ids[i] instanceof Symbol) { |
| 316 |
syms.add(ids[i]); |
| 317 |
} |
| 318 |
} |
| 319 |
return cx.newArray(scope, syms.toArray()); |
| 320 |
} |
| 321 |
case ConstructorId_getOwnPropertyDescriptor: |
| 322 |
{ |
| 323 |
Object arg = args.length < 1 ? Undefined.instance : args[0]; |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
Scriptable s = getCompatibleObject(cx, scope, arg); |
| 328 |
ScriptableObject obj = ensureScriptableObject(s); |
| 329 |
Object nameArg = args.length < 2 ? Undefined.instance : args[1]; |
| 330 |
Scriptable desc = obj.getOwnPropertyDescriptor(cx, nameArg); |
| 331 |
return desc == null ? Undefined.instance : desc; |
| 332 |
} |
| 333 |
case ConstructorId_defineProperty: |
| 334 |
{ |
| 335 |
Object arg = args.length < 1 ? Undefined.instance : args[0]; |
| 336 |
ScriptableObject obj = ensureScriptableObject(arg); |
| 337 |
Object name = args.length < 2 ? Undefined.instance : args[1]; |
| 338 |
Object descArg = args.length < 3 ? Undefined.instance : args[2]; |
| 339 |
ScriptableObject desc = ensureScriptableObject(descArg); |
| 340 |
obj.defineOwnProperty(cx, name, desc); |
| 341 |
return obj; |
| 342 |
} |
| 343 |
case ConstructorId_isExtensible: |
| 344 |
{ |
| 345 |
Object arg = args.length < 1 ? Undefined.instance : args[0]; |
| 346 |
ScriptableObject obj = ensureScriptableObject(arg); |
| 347 |
return Boolean.valueOf(obj.isExtensible()); |
| 348 |
} |
| 349 |
case ConstructorId_preventExtensions: |
| 350 |
{ |
| 351 |
Object arg = args.length < 1 ? Undefined.instance : args[0]; |
| 352 |
ScriptableObject obj = ensureScriptableObject(arg); |
| 353 |
obj.preventExtensions(); |
| 354 |
return obj; |
| 355 |
} |
| 356 |
case ConstructorId_defineProperties: |
| 357 |
{ |
| 358 |
Object arg = args.length < 1 ? Undefined.instance : args[0]; |
| 359 |
ScriptableObject obj = ensureScriptableObject(arg); |
| 360 |
Object propsObj = args.length < 2 ? Undefined.instance : args[1]; |
| 361 |
Scriptable props = Context.toObject(propsObj, getParentScope()); |
| 362 |
obj.defineOwnProperties(cx, ensureScriptableObject(props)); |
| 363 |
return obj; |
| 364 |
} |
| 365 |
case ConstructorId_create: |
| 366 |
{ |
| 367 |
Object arg = args.length < 1 ? Undefined.instance : args[0]; |
| 368 |
Scriptable obj = (arg == null) ? null : ensureScriptable(arg); |
| 369 |
|
| 370 |
ScriptableObject newObject = new NativeObject(); |
| 371 |
newObject.setParentScope(getParentScope()); |
| 372 |
newObject.setPrototype(obj); |
| 373 |
|
| 374 |
if (args.length > 1 && args[1] != Undefined.instance) { |
| 375 |
Scriptable props = Context.toObject(args[1], getParentScope()); |
| 376 |
newObject.defineOwnProperties(cx, ensureScriptableObject(props)); |
| 377 |
} |
| 378 |
|
| 379 |
return newObject; |
| 380 |
} |
| 381 |
case ConstructorId_isSealed: |
| 382 |
{ |
| 383 |
Object arg = args.length < 1 ? Undefined.instance : args[0]; |
| 384 |
ScriptableObject obj = ensureScriptableObject(arg); |
| 385 |
|
| 386 |
if (obj.isExtensible()) return Boolean.FALSE; |
| 387 |
|
| 388 |
for (Object name: obj.getAllIds()) { |
| 389 |
Object configurable = obj.getOwnPropertyDescriptor(cx, name).get("configurable"); |
| 390 |
if (Boolean.TRUE.equals(configurable)) |
| 391 |
return Boolean.FALSE; |
| 392 |
} |
| 393 |
|
| 394 |
return Boolean.TRUE; |
| 395 |
} |
| 396 |
case ConstructorId_isFrozen: |
| 397 |
{ |
| 398 |
Object arg = args.length < 1 ? Undefined.instance : args[0]; |
| 399 |
ScriptableObject obj = ensureScriptableObject(arg); |
| 400 |
|
| 401 |
if (obj.isExtensible()) return Boolean.FALSE; |
| 402 |
|
| 403 |
for (Object name: obj.getAllIds()) { |
| 404 |
ScriptableObject desc = obj.getOwnPropertyDescriptor(cx, name); |
| 405 |
if (Boolean.TRUE.equals(desc.get("configurable"))) |
| 406 |
return Boolean.FALSE; |
| 407 |
if (isDataDescriptor(desc) && Boolean.TRUE.equals(desc.get("writable"))) |
| 408 |
return Boolean.FALSE; |
| 409 |
} |
| 410 |
|
| 411 |
return Boolean.TRUE; |
| 412 |
} |
| 413 |
case ConstructorId_seal: |
| 414 |
{ |
| 415 |
Object arg = args.length < 1 ? Undefined.instance : args[0]; |
| 416 |
ScriptableObject obj = ensureScriptableObject(arg); |
| 417 |
|
| 418 |
for (Object name: obj.getAllIds()) { |
| 419 |
ScriptableObject desc = obj.getOwnPropertyDescriptor(cx, name); |
| 420 |
if (Boolean.TRUE.equals(desc.get("configurable"))) { |
| 421 |
desc.put("configurable", desc, Boolean.FALSE); |
| 422 |
obj.defineOwnProperty(cx, name, desc, false); |
| 423 |
} |
| 424 |
} |
| 425 |
obj.preventExtensions(); |
| 426 |
|
| 427 |
return obj; |
| 428 |
} |
| 429 |
case ConstructorId_freeze: |
| 430 |
{ |
| 431 |
Object arg = args.length < 1 ? Undefined.instance : args[0]; |
| 432 |
ScriptableObject obj = ensureScriptableObject(arg); |
| 433 |
|
| 434 |
for (Object name: obj.getAllIds()) { |
| 435 |
ScriptableObject desc = obj.getOwnPropertyDescriptor(cx, name); |
| 436 |
if (isDataDescriptor(desc) && Boolean.TRUE.equals(desc.get("writable"))) |
| 437 |
desc.put("writable", desc, Boolean.FALSE); |
| 438 |
if (Boolean.TRUE.equals(desc.get("configurable"))) |
| 439 |
desc.put("configurable", desc, Boolean.FALSE); |
| 440 |
obj.defineOwnProperty(cx, name, desc, false); |
| 441 |
} |
| 442 |
obj.preventExtensions(); |
| 443 |
|
| 444 |
return obj; |
| 445 |
} |
| 446 |
|
| 447 |
case ConstructorId_assign: |
| 448 |
{ |
| 449 |
if (args.length < 1) { |
| 450 |
throw ScriptRuntime.typeError1("msg.incompat.call", "assign"); |
| 451 |
} |
| 452 |
Scriptable t = ScriptRuntime.toObject(cx, thisObj, args[0]); |
| 453 |
for (int i = 1; i < args.length; i++) { |
| 454 |
if ((args[i] == null) || Undefined.instance.equals(args[i])) { |
| 455 |
continue; |
| 456 |
} |
| 457 |
Scriptable s = ScriptRuntime.toObject(cx, thisObj, args[i]); |
| 458 |
Object[] ids = s.getIds(); |
| 459 |
for (Object key : ids) { |
| 460 |
if (key instanceof String) { |
| 461 |
Object val = s.get((String) key, t); |
| 462 |
if ((val != Scriptable.NOT_FOUND) && (val != Undefined.instance)) { |
| 463 |
t.put((String) key, t, val); |
| 464 |
} |
| 465 |
} else if (key instanceof Number) { |
| 466 |
int ii = ScriptRuntime.toInt32(key); |
| 467 |
Object val = s.get(ii, t); |
| 468 |
if ((val != Scriptable.NOT_FOUND) && (val != Undefined.instance)) { |
| 469 |
t.put(ii, t, val); |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
} |
| 474 |
return t; |
| 475 |
} |
| 476 |
|
| 477 |
case ConstructorId_is: |
| 478 |
{ |
| 479 |
Object a1 = args.length < 1 ? Undefined.instance : args[0]; |
| 480 |
Object a2 = args.length < 2 ? Undefined.instance : args[1]; |
| 481 |
return ScriptRuntime.wrapBoolean(ScriptRuntime.same(a1, a2)); |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
default: |
| 486 |
throw new IllegalArgumentException(String.valueOf(id)); |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
private Scriptable getCompatibleObject(Context cx, Scriptable scope, Object arg) |
| 491 |
{ |
| 492 |
if (cx.getLanguageVersion() >= Context.VERSION_ES6) { |
| 493 |
Scriptable s = ScriptRuntime.toObject(cx, scope, arg); |
| 494 |
return ensureScriptable(s); |
| 495 |
} |
| 496 |
return ensureScriptable(arg); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
public boolean containsKey(Object key) { |
| 502 |
if (key instanceof String) { |
| 503 |
return has((String) key, this); |
| 504 |
} else if (key instanceof Number) { |
| 505 |
return has(((Number) key).intValue(), this); |
| 506 |
} |
| 507 |
return false; |
| 508 |
} |
| 509 |
|
| 510 |
public boolean containsValue(Object value) { |
| 511 |
for (Object obj : values()) { |
| 512 |
if (value == obj || |
| 513 |
value != null && value.equals(obj)) { |
| 514 |
return true; |
| 515 |
} |
| 516 |
} |
| 517 |
return false; |
| 518 |
} |
| 519 |
|
| 520 |
public Object remove(Object key) { |
| 521 |
Object value = get(key); |
| 522 |
if (key instanceof String) { |
| 523 |
delete((String) key); |
| 524 |
} else if (key instanceof Number) { |
| 525 |
delete(((Number) key).intValue()); |
| 526 |
} |
| 527 |
return value; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
public Set<Object> keySet() { |
| 532 |
return new KeySet(); |
| 533 |
} |
| 534 |
|
| 535 |
public Collection<Object> values() { |
| 536 |
return new ValueCollection(); |
| 537 |
} |
| 538 |
|
| 539 |
public Set<Map.Entry<Object, Object>> entrySet() { |
| 540 |
return new EntrySet(); |
| 541 |
} |
| 542 |
|
| 543 |
public Object put(Object key, Object value) { |
| 544 |
throw new UnsupportedOperationException(); |
| 545 |
} |
| 546 |
|
| 547 |
public void putAll(Map m) { |
| 548 |
throw new UnsupportedOperationException(); |
| 549 |
} |
| 550 |
|
| 551 |
public void clear() { |
| 552 |
throw new UnsupportedOperationException(); |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
class EntrySet extends AbstractSet<Entry<Object, Object>> { |
| 557 |
@Override |
| 558 |
public Iterator<Entry<Object, Object>> iterator() { |
| 559 |
return new Iterator<Map.Entry<Object, Object>>() { |
| 560 |
Object[] ids = getIds(); |
| 561 |
Object key = null; |
| 562 |
int index = 0; |
| 563 |
|
| 564 |
public boolean hasNext() { |
| 565 |
return index < ids.length; |
| 566 |
} |
| 567 |
|
| 568 |
public Map.Entry<Object, Object> next() { |
| 569 |
final Object ekey = key = ids[index++]; |
| 570 |
final Object value = get(key); |
| 571 |
return new Map.Entry<Object, Object>() { |
| 572 |
public Object getKey() { |
| 573 |
return ekey; |
| 574 |
} |
| 575 |
|
| 576 |
public Object getValue() { |
| 577 |
return value; |
| 578 |
} |
| 579 |
|
| 580 |
public Object setValue(Object value) { |
| 581 |
throw new UnsupportedOperationException(); |
| 582 |
} |
| 583 |
|
| 584 |
@Override |
| 585 |
public boolean equals(Object other) { |
| 586 |
if (!(other instanceof Map.Entry)) { |
| 587 |
return false; |
| 588 |
} |
| 589 |
Map.Entry<?, ?> e = (Map.Entry<?, ?>) other; |
| 590 |
return (ekey == null ? e.getKey() == null : ekey.equals(e.getKey())) |
| 591 |
&& (value == null ? e.getValue() == null : value.equals(e.getValue())); |
| 592 |
} |
| 593 |
|
| 594 |
@Override |
| 595 |
public int hashCode() { |
| 596 |
return (ekey == null ? 0 : ekey.hashCode()) ^ |
| 597 |
(value == null ? 0 : value.hashCode()); |
| 598 |
} |
| 599 |
|
| 600 |
@Override |
| 601 |
public String toString() { |
| 602 |
return ekey + "=" + value; |
| 603 |
} |
| 604 |
}; |
| 605 |
} |
| 606 |
|
| 607 |
public void remove() { |
| 608 |
if (key == null) { |
| 609 |
throw new IllegalStateException(); |
| 610 |
} |
| 611 |
NativeObject.this.remove(key); |
| 612 |
key = null; |
| 613 |
} |
| 614 |
}; |
| 615 |
} |
| 616 |
|
| 617 |
@Override |
| 618 |
public int size() { |
| 619 |
return NativeObject.this.size(); |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
class KeySet extends AbstractSet<Object> { |
| 624 |
|
| 625 |
@Override |
| 626 |
public boolean contains(Object key) { |
| 627 |
return containsKey(key); |
| 628 |
} |
| 629 |
|
| 630 |
@Override |
| 631 |
public Iterator<Object> iterator() { |
| 632 |
return new Iterator<Object>() { |
| 633 |
Object[] ids = getIds(); |
| 634 |
Object key; |
| 635 |
int index = 0; |
| 636 |
|
| 637 |
public boolean hasNext() { |
| 638 |
return index < ids.length; |
| 639 |
} |
| 640 |
|
| 641 |
public Object next() { |
| 642 |
try { |
| 643 |
return (key = ids[index++]); |
| 644 |
} catch(ArrayIndexOutOfBoundsException e) { |
| 645 |
key = null; |
| 646 |
throw new NoSuchElementException(); |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
public void remove() { |
| 651 |
if (key == null) { |
| 652 |
throw new IllegalStateException(); |
| 653 |
} |
| 654 |
NativeObject.this.remove(key); |
| 655 |
key = null; |
| 656 |
} |
| 657 |
}; |
| 658 |
} |
| 659 |
|
| 660 |
@Override |
| 661 |
public int size() { |
| 662 |
return NativeObject.this.size(); |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
class ValueCollection extends AbstractCollection<Object> { |
| 667 |
|
| 668 |
@Override |
| 669 |
public Iterator<Object> iterator() { |
| 670 |
return new Iterator<Object>() { |
| 671 |
Object[] ids = getIds(); |
| 672 |
Object key; |
| 673 |
int index = 0; |
| 674 |
|
| 675 |
public boolean hasNext() { |
| 676 |
return index < ids.length; |
| 677 |
} |
| 678 |
|
| 679 |
public Object next() { |
| 680 |
return get((key = ids[index++])); |
| 681 |
} |
| 682 |
|
| 683 |
public void remove() { |
| 684 |
if (key == null) { |
| 685 |
throw new IllegalStateException(); |
| 686 |
} |
| 687 |
NativeObject.this.remove(key); |
| 688 |
key = null; |
| 689 |
} |
| 690 |
}; |
| 691 |
} |
| 692 |
|
| 693 |
@Override |
| 694 |
public int size() { |
| 695 |
return NativeObject.this.size(); |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
@Override |
| 703 |
protected int findPrototypeId(String s) |
| 704 |
{ |
| 705 |
int id; |
| 706 |
|
| 707 |
L0: { id = 0; String X = null; int c; |
| 708 |
L: switch (s.length()) { |
| 709 |
case 7: X="valueOf";id=Id_valueOf; break L; |
| 710 |
case 8: c=s.charAt(3); |
| 711 |
if (c=='o') { X="toSource";id=Id_toSource; } |
| 712 |
else if (c=='t') { X="toString";id=Id_toString; } |
| 713 |
break L; |
| 714 |
case 11: X="constructor";id=Id_constructor; break L; |
| 715 |
case 13: X="isPrototypeOf";id=Id_isPrototypeOf; break L; |
| 716 |
case 14: c=s.charAt(0); |
| 717 |
if (c=='h') { X="hasOwnProperty";id=Id_hasOwnProperty; } |
| 718 |
else if (c=='t') { X="toLocaleString";id=Id_toLocaleString; } |
| 719 |
break L; |
| 720 |
case 16: c=s.charAt(2); |
| 721 |
if (c=='d') { |
| 722 |
c=s.charAt(8); |
| 723 |
if (c=='G') { X="__defineGetter__";id=Id___defineGetter__; } |
| 724 |
else if (c=='S') { X="__defineSetter__";id=Id___defineSetter__; } |
| 725 |
} |
| 726 |
else if (c=='l') { |
| 727 |
c=s.charAt(8); |
| 728 |
if (c=='G') { X="__lookupGetter__";id=Id___lookupGetter__; } |
| 729 |
else if (c=='S') { X="__lookupSetter__";id=Id___lookupSetter__; } |
| 730 |
} |
| 731 |
break L; |
| 732 |
case 20: X="propertyIsEnumerable";id=Id_propertyIsEnumerable; break L; |
| 733 |
} |
| 734 |
if (X!=null && X!=s && !X.equals(s)) id = 0; |
| 735 |
break L0; |
| 736 |
} |
| 737 |
|
| 738 |
return id; |
| 739 |
} |
| 740 |
|
| 741 |
private static final int |
| 742 |
ConstructorId_getPrototypeOf = -1, |
| 743 |
ConstructorId_keys = -2, |
| 744 |
ConstructorId_getOwnPropertyNames = -3, |
| 745 |
ConstructorId_getOwnPropertyDescriptor = -4, |
| 746 |
ConstructorId_defineProperty = -5, |
| 747 |
ConstructorId_isExtensible = -6, |
| 748 |
ConstructorId_preventExtensions = -7, |
| 749 |
ConstructorId_defineProperties= -8, |
| 750 |
ConstructorId_create = -9, |
| 751 |
ConstructorId_isSealed = -10, |
| 752 |
ConstructorId_isFrozen = -11, |
| 753 |
ConstructorId_seal = -12, |
| 754 |
ConstructorId_freeze = -13, |
| 755 |
ConstructorId_getOwnPropertySymbols = -14, |
| 756 |
ConstructorId_assign = -15, |
| 757 |
ConstructorId_is = -16, |
| 758 |
|
| 759 |
Id_constructor = 1, |
| 760 |
Id_toString = 2, |
| 761 |
Id_toLocaleString = 3, |
| 762 |
Id_valueOf = 4, |
| 763 |
Id_hasOwnProperty = 5, |
| 764 |
Id_propertyIsEnumerable = 6, |
| 765 |
Id_isPrototypeOf = 7, |
| 766 |
Id_toSource = 8, |
| 767 |
Id___defineGetter__ = 9, |
| 768 |
Id___defineSetter__ = 10, |
| 769 |
Id___lookupGetter__ = 11, |
| 770 |
Id___lookupSetter__ = 12, |
| 771 |
MAX_PROTOTYPE_ID = 12; |
| 772 |
|
| 773 |
|
| 774 |
} |