Debian Codegraph @

rhino/src/org/mozilla/javascript/IdScriptableObject.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 4; indent-tabs-mode: 1; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript;
8
9 import java.io.IOException;
10 import java.io.ObjectInputStream;
11 import java.io.ObjectOutputStream;
12 import java.io.Serializable;
13
14 /**
15 Base class for native object implementation that uses IdFunctionObject to export its methods to script via <class-name>.prototype object.
16
17 Any descendant should implement at least the following methods:
18 findInstanceIdInfo
19 getInstanceIdName
20 execIdCall
21 methodArity
22
23 To define non-function properties, the descendant should override
24 getInstanceIdValue
25 setInstanceIdValue
26 to get/set property value and provide its default attributes.
27
28
29 To customize initialization of constructor and prototype objects, descendant
30 may override scopeInit or fillConstructorProperties methods.
31
32 */
33 public abstract class IdScriptableObject extends ScriptableObject
34 implements IdFunctionCall
35 {
36 private transient PrototypeValues prototypeValues;
37
38 private static final class PrototypeValues implements Serializable
39 {
40 static final long serialVersionUID = 3038645279153854371L;
41
42 private static final int NAME_SLOT = 1;
43 private static final int SLOT_SPAN = 2;
44
45 private IdScriptableObject obj;
46 private int maxId;
47 private Object[] valueArray;
48 private short[] attributeArray;
49
50 // The following helps to avoid creation of valueArray during runtime
51 // initialization for common case of "constructor" property
52 int constructorId;
53 private IdFunctionObject constructor;
54 private short constructorAttrs;
55
56 PrototypeValues(IdScriptableObject obj, int maxId)
57 {
58 if (obj == null) throw new IllegalArgumentException();
59 if (maxId < 1) throw new IllegalArgumentException();
60 this.obj = obj;
61 this.maxId = maxId;
62 }
63
64 final int getMaxId()
65 {
66 return maxId;
67 }
68
69 final void initValue(int id, String name, Object value, int attributes)
70 {
71 if (!(1 <= id && id <= maxId))
72 throw new IllegalArgumentException();
73 if (name == null)
74 throw new IllegalArgumentException();
75 if (value == NOT_FOUND)
76 throw new IllegalArgumentException();
77 ScriptableObject.checkValidAttributes(attributes);
78 if (obj.findPrototypeId(name) != id)
79 throw new IllegalArgumentException(name);
80
81 if (id == constructorId) {
82 if (!(value instanceof IdFunctionObject)) {
83 throw new IllegalArgumentException("consructor should be initialized with IdFunctionObject");
84 }
85 constructor = (IdFunctionObject)value;
86 constructorAttrs = (short)attributes;
87 return;
88 }
89
90 initSlot(id, name, value, attributes);
91 }
92
93 final void initValue(int id, Symbol key, Object value, int attributes)
94 {
95 if (!(1 <= id && id <= maxId))
96 throw new IllegalArgumentException();
97 if (key == null)
98 throw new IllegalArgumentException();
99 if (value == NOT_FOUND)
100 throw new IllegalArgumentException();
101 ScriptableObject.checkValidAttributes(attributes);
102 if (obj.findPrototypeId(key) != id)
103 throw new IllegalArgumentException(key.toString());
104
105 if (id == constructorId) {
106 if (!(value instanceof IdFunctionObject)) {
107 throw new IllegalArgumentException("consructor should be initialized with IdFunctionObject");
108 }
109 constructor = (IdFunctionObject)value;
110 constructorAttrs = (short)attributes;
111 return;
112 }
113
114 initSlot(id, "", value, attributes);
115 }
116
117 private void initSlot(int id, String name, Object value,
118 int attributes)
119 {
120 Object[] array = valueArray;
121 if (array == null)
122 throw new IllegalStateException();
123
124 if (value == null) {
125 value = UniqueTag.NULL_VALUE;
126 }
127 int index = (id - 1) * SLOT_SPAN;
128 synchronized (this) {
129 Object value2 = array[index];
130 if (value2 == null) {
131 array[index] = value;
132 array[index + NAME_SLOT] = name;
133 attributeArray[id - 1] = (short)attributes;
134 } else {
135 if (!name.equals(array[index + NAME_SLOT]))
136 throw new IllegalStateException();
137 }
138 }
139 }
140
141 final IdFunctionObject createPrecachedConstructor()
142 {
143 if (constructorId != 0) throw new IllegalStateException();
144 constructorId = obj.findPrototypeId("constructor");
145 if (constructorId == 0) {
146 throw new IllegalStateException(
147 "No id for constructor property");
148 }
149 obj.initPrototypeId(constructorId);
150 if (constructor == null) {
151 throw new IllegalStateException(
152 obj.getClass().getName()+".initPrototypeId() did not "
153 +"initialize id="+constructorId);
154 }
155 constructor.initFunction(obj.getClassName(),
156 ScriptableObject.getTopLevelScope(obj));
157 constructor.markAsConstructor(obj);
158 return constructor;
159 }
160
161 final int findId(String name)
162 {
163 return obj.findPrototypeId(name);
164 }
165
166 final int findId(Symbol key)
167 {
168 return obj.findPrototypeId(key);
169 }
170
171 final boolean has(int id)
172 {
173 Object[] array = valueArray;
174 if (array == null) {
175 // Not yet initialized, assume all exists
176 return true;
177 }
178 int valueSlot = (id - 1) * SLOT_SPAN;
179 Object value = array[valueSlot];
180 if (value == null) {
181 // The particular entry has not been yet initialized
182 return true;
183 }
184 return value != NOT_FOUND;
185 }
186
187 final Object get(int id)
188 {
189 Object value = ensureId(id);
190 if (value == UniqueTag.NULL_VALUE) {
191 value = null;
192 }
193 return value;
194 }
195
196 final void set(int id, Scriptable start, Object value)
197 {
198 if (value == NOT_FOUND) throw new IllegalArgumentException();
199 ensureId(id);
200 int attr = attributeArray[id - 1];
201 if ((attr & READONLY) == 0) {
202 if (start == obj) {
203 if (value == null) {
204 value = UniqueTag.NULL_VALUE;
205 }
206 int valueSlot = (id - 1) * SLOT_SPAN;
207 synchronized (this) {
208 valueArray[valueSlot] = value;
209 }
210 }
211 else {
212 int nameSlot = (id - 1) * SLOT_SPAN + NAME_SLOT;
213 String name = (String)valueArray[nameSlot];
214 start.put(name, start, value);
215 }
216 }
217 }
218
219 final void delete(int id)
220 {
221 ensureId(id);
222 int attr = attributeArray[id - 1];
223 // non-configurable
224 if ((attr & PERMANENT) != 0) {
225 Context cx = Context.getContext();
226 if (cx.isStrictMode()) {
227 int nameSlot = (id - 1) * SLOT_SPAN + NAME_SLOT;
228 String name = (String)valueArray[nameSlot];
229 throw ScriptRuntime.typeError1("msg.delete.property.with.configurable.false", name);
230 }
231 } else {
232 int valueSlot = (id - 1) * SLOT_SPAN;
233 synchronized (this) {
234 valueArray[valueSlot] = NOT_FOUND;
235 attributeArray[id - 1] = EMPTY;
236 }
237 }
238 }
239
240 final int getAttributes(int id)
241 {
242 ensureId(id);
243 return attributeArray[id - 1];
244 }
245
246 final void setAttributes(int id, int attributes)
247 {
248 ScriptableObject.checkValidAttributes(attributes);
249 ensureId(id);
250 synchronized (this) {
251 attributeArray[id - 1] = (short)attributes;
252 }
253 }
254
255 final Object[] getNames(boolean getAll, Object[] extraEntries)
256 {
257 Object[] names = null;
258 int count = 0;
259 for (int id = 1; id <= maxId; ++id) {
260 Object value = ensureId(id);
261 if (getAll || (attributeArray[id - 1] & DONTENUM) == 0) {
262 if (value != NOT_FOUND) {
263 int nameSlot = (id - 1) * SLOT_SPAN + NAME_SLOT;
264 String name = (String)valueArray[nameSlot];
265 if (names == null) {
266 names = new Object[maxId];
267 }
268 names[count++] = name;
269 }
270 }
271 }
272 if (count == 0) {
273 return extraEntries;
274 } else if (extraEntries == null || extraEntries.length == 0) {
275 if (count != names.length) {
276 Object[] tmp = new Object[count];
277 System.arraycopy(names, 0, tmp, 0, count);
278 names = tmp;
279 }
280 return names;
281 } else {
282 int extra = extraEntries.length;
283 Object[] tmp = new Object[extra + count];
284 System.arraycopy(extraEntries, 0, tmp, 0, extra);
285 System.arraycopy(names, 0, tmp, extra, count);
286 return tmp;
287 }
288 }
289
290 private Object ensureId(int id)
291 {
292 Object[] array = valueArray;
293 if (array == null) {
294 synchronized (this) {
295 array = valueArray;
296 if (array == null) {
297 array = new Object[maxId * SLOT_SPAN];
298 valueArray = array;
299 attributeArray = new short[maxId];
300 }
301 }
302 }
303 int valueSlot = (id - 1) * SLOT_SPAN;
304 Object value = array[valueSlot];
305 if (value == null) {
306 if (id == constructorId) {
307 initSlot(constructorId, "constructor",
308 constructor, constructorAttrs);
309 constructor = null; // no need to refer it any longer
310 } else {
311 obj.initPrototypeId(id);
312 }
313 value = array[valueSlot];
314 if (value == null) {
315 throw new IllegalStateException(
316 obj.getClass().getName()+".initPrototypeId(int id) "
317 +"did not initialize id="+id);
318 }
319 }
320 return value;
321 }
322 }
323
324 public IdScriptableObject()
325 {
326 }
327
328 public IdScriptableObject(Scriptable scope, Scriptable prototype)
329 {
330 super(scope, prototype);
331 }
332
333 protected final boolean defaultHas(String name)
334 {
335 return super.has(name, this);
336 }
337
338 protected final Object defaultGet(String name)
339 {
340 return super.get(name, this);
341 }
342
343 protected final void defaultPut(String name, Object value)
344 {
345 super.put(name, this, value);
346 }
347
348 @Override
349 public boolean has(String name, Scriptable start)
350 {
351 int info = findInstanceIdInfo(name);
352 if (info != 0) {
353 int attr = (info >>> 16);
354 if ((attr & PERMANENT) != 0) {
355 return true;
356 }
357 int id = (info & 0xFFFF);
358 return NOT_FOUND != getInstanceIdValue(id);
359 }
360 if (prototypeValues != null) {
361 int id = prototypeValues.findId(name);
362 if (id != 0) {
363 return prototypeValues.has(id);
364 }
365 }
366 return super.has(name, start);
367 }
368
369
370 @Override
371 public boolean has(Symbol key, Scriptable start)
372 {
373 int info = findInstanceIdInfo(key);
374 if (info != 0) {
375 int attr = (info >>> 16);
376 if ((attr & PERMANENT) != 0) {
377 return true;
378 }
379 int id = (info & 0xFFFF);
380 return NOT_FOUND != getInstanceIdValue(id);
381 }
382 if (prototypeValues != null) {
383 int id = prototypeValues.findId(key);
384 if (id != 0) {
385 return prototypeValues.has(id);
386 }
387 }
388 return super.has(key, start);
389 }
390
391 @Override
392 public Object get(String name, Scriptable start)
393 {
394 // Check for slot first for performance. This is a very hot code
395 // path that should be further optimized.
396 Object value = super.get(name, start);
397 if (value != NOT_FOUND) {
398 return value;
399 }
400 int info = findInstanceIdInfo(name);
401 if (info != 0) {
402 int id = (info & 0xFFFF);
403 value = getInstanceIdValue(id);
404 if (value != NOT_FOUND) return value;
405 }
406 if (prototypeValues != null) {
407 int id = prototypeValues.findId(name);
408 if (id != 0) {
409 value = prototypeValues.get(id);
410 if (value != NOT_FOUND) return value;
411 }
412 }
413 return NOT_FOUND;
414 }
415
416 @Override
417 public Object get(Symbol key, Scriptable start)
418 {
419 Object value = super.get(key, start);
420 if (value != NOT_FOUND) {
421 return value;
422 }
423 int info = findInstanceIdInfo(key);
424 if (info != 0) {
425 int id = (info & 0xFFFF);
426 value = getInstanceIdValue(id);
427 if (value != NOT_FOUND) return value;
428 }
429 if (prototypeValues != null) {
430 int id = prototypeValues.findId(key);
431 if (id != 0) {
432 value = prototypeValues.get(id);
433 if (value != NOT_FOUND) return value;
434 }
435 }
436 return NOT_FOUND;
437 }
438
439 @Override
440 public void put(String name, Scriptable start, Object value)
441 {
442 int info = findInstanceIdInfo(name);
443 if (info != 0) {
444 if (start == this && isSealed()) {
445 throw Context.reportRuntimeError1("msg.modify.sealed",
446 name);
447 }
448 int attr = (info >>> 16);
449 if ((attr & READONLY) == 0) {
450 if (start == this) {
451 int id = (info & 0xFFFF);
452 setInstanceIdValue(id, value);
453 }
454 else {
455 start.put(name, start, value);
456 }
457 }
458 return;
459 }
460 if (prototypeValues != null) {
461 int id = prototypeValues.findId(name);
462 if (id != 0) {
463 if (start == this && isSealed()) {
464 throw Context.reportRuntimeError1("msg.modify.sealed",
465 name);
466 }
467 prototypeValues.set(id, start, value);
468 return;
469 }
470 }
471 super.put(name, start, value);
472 }
473
474 @Override
475 public void put(Symbol key, Scriptable start, Object value)
476 {
477 int info = findInstanceIdInfo(key);
478 if (info != 0) {
479 if (start == this && isSealed()) {
480 throw Context.reportRuntimeError0("msg.modify.sealed");
481 }
482 int attr = (info >>> 16);
483 if ((attr & READONLY) == 0) {
484 if (start == this) {
485 int id = (info & 0xFFFF);
486 setInstanceIdValue(id, value);
487 }
488 else {
489 ensureSymbolScriptable(start).put(key, start, value);
490 }
491 }
492 return;
493 }
494 if (prototypeValues != null) {
495 int id = prototypeValues.findId(key);
496 if (id != 0) {
497 if (start == this && isSealed()) {
498 throw Context.reportRuntimeError0("msg.modify.sealed");
499 }
500 prototypeValues.set(id, start, value);
501 return;
502 }
503 }
504 super.put(key, start, value);
505 }
506
507 @Override
508 public void delete(String name)
509 {
510 int info = findInstanceIdInfo(name);
511 if (info != 0) {
512 // Let the super class to throw exceptions for sealed objects
513 if (!isSealed()) {
514 int attr = (info >>> 16);
515 // non-configurable
516 if ((attr & PERMANENT) != 0) {
517 Context cx = Context.getContext();
518 if (cx.isStrictMode()) {
519 throw ScriptRuntime.typeError1("msg.delete.property.with.configurable.false", name);
520 }
521 } else {
522 int id = (info & 0xFFFF);
523 setInstanceIdValue(id, NOT_FOUND);
524 }
525 return;
526 }
527 }
528 if (prototypeValues != null) {
529 int id = prototypeValues.findId(name);
530 if (id != 0) {
531 if (!isSealed()) {
532 prototypeValues.delete(id);
533 }
534 return;
535 }
536 }
537 super.delete(name);
538 }
539
540 @Override
541 public void delete(Symbol key)
542 {
543 int info = findInstanceIdInfo(key);
544 if (info != 0) {
545 // Let the super class to throw exceptions for sealed objects
546 if (!isSealed()) {
547 int attr = (info >>> 16);
548 // non-configurable
549 if ((attr & PERMANENT) != 0) {
550 Context cx = Context.getContext();
551 if (cx.isStrictMode()) {
552 throw ScriptRuntime.typeError0("msg.delete.property.with.configurable.false");
553 }
554 } else {
555 int id = (info & 0xFFFF);
556 setInstanceIdValue(id, NOT_FOUND);
557 }
558 return;
559 }
560 }
561 if (prototypeValues != null) {
562 int id = prototypeValues.findId(key);
563 if (id != 0) {
564 if (!isSealed()) {
565 prototypeValues.delete(id);
566 }
567 return;
568 }
569 }
570 super.delete(key);
571 }
572
573 @Override
574 public int getAttributes(String name)
575 {
576 int info = findInstanceIdInfo(name);
577 if (info != 0) {
578 int attr = (info >>> 16);
579 return attr;
580 }
581 if (prototypeValues != null) {
582 int id = prototypeValues.findId(name);
583 if (id != 0) {
584 return prototypeValues.getAttributes(id);
585 }
586 }
587 return super.getAttributes(name);
588 }
589
590 @Override
591 public void setAttributes(String name, int attributes)
592 {
593 ScriptableObject.checkValidAttributes(attributes);
594 int info = findInstanceIdInfo(name);
595 if (info != 0) {
596 int id = (info & 0xFFFF);
597 int currentAttributes = (info >>> 16);
598 if (attributes != currentAttributes) {
599 setInstanceIdAttributes(id, attributes);
600 }
601 return;
602 }
603 if (prototypeValues != null) {
604 int id = prototypeValues.findId(name);
605 if (id != 0) {
606 prototypeValues.setAttributes(id, attributes);
607 return;
608 }
609 }
610 super.setAttributes(name, attributes);
611 }
612
613 @Override
614 Object[] getIds(boolean getNonEnumerable, boolean getSymbols)
615 {
616 Object[] result = super.getIds(getNonEnumerable, getSymbols);
617
618 if (prototypeValues != null) {
619 result = prototypeValues.getNames(getNonEnumerable, result);
620 }
621
622 int maxInstanceId = getMaxInstanceId();
623 if (maxInstanceId != 0) {
624 Object[] ids = null;
625 int count = 0;
626
627 for (int id = maxInstanceId; id != 0; --id) {
628 String name = getInstanceIdName(id);
629 int info = findInstanceIdInfo(name);
630 if (info != 0) {
631 int attr = (info >>> 16);
632 if ((attr & PERMANENT) == 0) {
633 if (NOT_FOUND == getInstanceIdValue(id)) {
634 continue;
635 }
636 }
637 if (getNonEnumerable || (attr & DONTENUM) == 0) {
638 if (count == 0) {
639 // Need extra room for no more then [1..id] names
640 ids = new Object[id];
641 }
642 ids[count++] = name;
643 }
644 }
645 }
646 if (count != 0) {
647 if (result.length == 0 && ids.length == count) {
648 result = ids;
649 }
650 else {
651 Object[] tmp = new Object[result.length + count];
652 System.arraycopy(result, 0, tmp, 0, result.length);
653 System.arraycopy(ids, 0, tmp, result.length, count);
654 result = tmp;
655 }
656 }
657 }
658 return result;
659 }
660
661 /**
662 * Get maximum id findInstanceIdInfo can generate.
663 */
664 protected int getMaxInstanceId()
665 {
666 return 0;
667 }
668
669 protected static int instanceIdInfo(int attributes, int id)
670 {
671 return (attributes << 16) | id;
672 }
673
674 /**
675 * Map name to id of instance property.
676 * Should return 0 if not found or the result of
677 * {@link #instanceIdInfo(int, int)}.
678 */
679 protected int findInstanceIdInfo(String name)
680 {
681 return 0;
682 }
683
684 /**
685 * Map name to id of instance property.
686 * Should return 0 if not found or the result of
687 * {@link #instanceIdInfo(int, int)}.
688 */
689 protected int findInstanceIdInfo(Symbol key)
690 {
691 return 0;
692 }
693
694 /** Map id back to property name it defines.
695 */
696 protected String getInstanceIdName(int id)
697 {
698 throw new IllegalArgumentException(String.valueOf(id));
699 }
700
701 /** Get id value.
702 ** If id value is constant, descendant can call cacheIdValue to store
703 ** value in the permanent cache.
704 ** Default implementation creates IdFunctionObject instance for given id
705 ** and cache its value
706 */
707 protected Object getInstanceIdValue(int id)
708 {
709 throw new IllegalStateException(String.valueOf(id));
710 }
711
712 /**
713 * Set or delete id value. If value == NOT_FOUND , the implementation
714 * should make sure that the following getInstanceIdValue return NOT_FOUND.
715 */
716 protected void setInstanceIdValue(int id, Object value)
717 {
718 throw new IllegalStateException(String.valueOf(id));
719 }
720
721 /**
722 * Update the attributes of the given instance property. Classes which
723 * want to support changing property attributes via Object.defineProperty
724 * must override this method. The default implementation throws
725 * InternalError.
726 * @param id the instance property id
727 * @param attr the new attribute bitset
728 */
729 protected void setInstanceIdAttributes(int id, int attr) {
730 throw ScriptRuntime.constructError("InternalError",
731 "Changing attributes not supported for " + getClassName()
732 + " " + getInstanceIdName(id) + " property");
733 }
734
735 /** 'thisObj' will be null if invoked as constructor, in which case
736 ** instance of Scriptable should be returned. */
737 public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
738 Scriptable thisObj, Object[] args)
739 {
740 throw f.unknown();
741 }
742
743 public final IdFunctionObject exportAsJSClass(int maxPrototypeId,
744 Scriptable scope,
745 boolean sealed)
746 {
747 // Set scope and prototype unless this is top level scope itself
748 if (scope != this && scope != null) {
749 setParentScope(scope);
750 setPrototype(getObjectPrototype(scope));
751 }
752
753 activatePrototypeMap(maxPrototypeId);
754 IdFunctionObject ctor = prototypeValues.createPrecachedConstructor();
755 if (sealed) {
756 sealObject();
757 }
758 fillConstructorProperties(ctor);
759 if (sealed) {
760 ctor.sealObject();
761 }
762 ctor.exportAsScopeProperty();
763 return ctor;
764 }
765
766 public final boolean hasPrototypeMap()
767 {
768 return prototypeValues != null;
769 }
770
771 public final void activatePrototypeMap(int maxPrototypeId)
772 {
773 PrototypeValues values = new PrototypeValues(this, maxPrototypeId);
774 synchronized (this) {
775 if (prototypeValues != null)
776 throw new IllegalStateException();
777 prototypeValues = values;
778 }
779 }
780
781 public final IdFunctionObject initPrototypeMethod(Object tag, int id, String name,
782 int arity)
783 {
784 return initPrototypeMethod(tag, id, name, name, arity);
785 }
786
787 public final IdFunctionObject initPrototypeMethod(Object tag, int id, String propertyName, String functionName,
788 int arity)
789 {
790 Scriptable scope = ScriptableObject.getTopLevelScope(this);
791 IdFunctionObject function = newIdFunction(tag, id,
792 functionName != null ? functionName : propertyName,
793 arity, scope);
794 prototypeValues.initValue(id, propertyName, function, DONTENUM);
795 return function;
796 }
797
798 public final IdFunctionObject initPrototypeMethod(Object tag, int id, Symbol key, String functionName,
799 int arity)
800 {
801 Scriptable scope = ScriptableObject.getTopLevelScope(this);
802 IdFunctionObject function = newIdFunction(tag, id, functionName, arity, scope);
803 prototypeValues.initValue(id, key, function, DONTENUM);
804 return function;
805 }
806
807 public final void initPrototypeConstructor(IdFunctionObject f)
808 {
809 int id = prototypeValues.constructorId;
810 if (id == 0)
811 throw new IllegalStateException();
812 if (f.methodId() != id)
813 throw new IllegalArgumentException();
814 if (isSealed()) { f.sealObject(); }
815 prototypeValues.initValue(id, "constructor", f, DONTENUM);
816 }
817
818 public final void initPrototypeValue(int id, String name, Object value,
819 int attributes)
820 {
821 prototypeValues.initValue(id, name, value, attributes);
822 }
823
824 public final void initPrototypeValue(int id, Symbol key, Object value,
825 int attributes)
826 {
827 prototypeValues.initValue(id, key, value, attributes);
828 }
829
830 protected void initPrototypeId(int id)
831 {
832 throw new IllegalStateException(String.valueOf(id));
833 }
834
835 protected int findPrototypeId(String name)
836 {
837 throw new IllegalStateException(name);
838 }
839
840 protected int findPrototypeId(Symbol key)
841 {
842 return 0;
843 }
844
845 protected void fillConstructorProperties(IdFunctionObject ctor)
846 {
847 }
848
849 protected void addIdFunctionProperty(Scriptable obj, Object tag, int id,
850 String name, int arity)
851 {
852 Scriptable scope = ScriptableObject.getTopLevelScope(obj);
853 IdFunctionObject f = newIdFunction(tag, id, name, arity, scope);
854 f.addAsProperty(obj);
855 }
856
857 /**
858 * Utility method to construct type error to indicate incompatible call
859 * when converting script thisObj to a particular type is not possible.
860 * Possible usage would be to have a private function like realThis:
861 * <pre>
862 * private static NativeSomething realThis(Scriptable thisObj,
863 * IdFunctionObject f)
864 * {
865 * if (!(thisObj instanceof NativeSomething))
866 * throw incompatibleCallError(f);
867 * return (NativeSomething)thisObj;
868 * }
869 * </pre>
870 * Note that although such function can be implemented universally via
871 * java.lang.Class.isInstance(), it would be much more slower.
872 * @param f function that is attempting to convert 'this'
873 * object.
874 * @return Scriptable object suitable for a check by the instanceof
875 * operator.
876 * @throws RuntimeException if no more instanceof target can be found
877 */
878 protected static EcmaError incompatibleCallError(IdFunctionObject f)
879 {
880 throw ScriptRuntime.typeError1("msg.incompat.call",
881 f.getFunctionName());
882 }
883
884 private IdFunctionObject newIdFunction(Object tag, int id, String name,
885 int arity, Scriptable scope)
886 {
887 IdFunctionObject function = null;
888 if (Context.getContext().getLanguageVersion() < Context.VERSION_ES6) {
889 function = new IdFunctionObject(this, tag, id, name, arity, scope);
890 } else {
891 function = new IdFunctionObjectES6(this, tag, id, name, arity, scope);
892 }
893
894 if (isSealed()) { function.sealObject(); }
895 return function;
896 }
897
898 @Override
899 public void defineOwnProperty(Context cx, Object key, ScriptableObject desc) {
900 if (key instanceof String) {
901 String name = (String) key;
902 int info = findInstanceIdInfo(name);
903 if (info != 0) {
904 int id = (info & 0xFFFF);
905 if (isAccessorDescriptor(desc)) {
906 delete(id); // it will be replaced with a slot
907 } else {
908 checkPropertyDefinition(desc);
909 ScriptableObject current = getOwnPropertyDescriptor(cx, key);
910 checkPropertyChange(name, current, desc);
911 int attr = (info >>> 16);
912 Object value = getProperty(desc, "value");
913 if (value != NOT_FOUND && (attr & READONLY) == 0) {
914 Object currentValue = getInstanceIdValue(id);
915 if (!sameValue(value, currentValue)) {
916 setInstanceIdValue(id, value);
917 }
918 }
919 setAttributes(name, applyDescriptorToAttributeBitset(attr, desc));
920 return;
921 }
922 }
923 if (prototypeValues != null) {
924 int id = prototypeValues.findId(name);
925 if (id != 0) {
926 if (isAccessorDescriptor(desc)) {
927 prototypeValues.delete(id); // it will be replaced with a slot
928 } else {
929 checkPropertyDefinition(desc);
930 ScriptableObject current = getOwnPropertyDescriptor(cx, key);
931 checkPropertyChange(name, current, desc);
932 int attr = prototypeValues.getAttributes(id);
933 Object value = getProperty(desc, "value");
934 if (value != NOT_FOUND && (attr & READONLY) == 0) {
935 Object currentValue = prototypeValues.get(id);
936 if (!sameValue(value, currentValue)) {
937 prototypeValues.set(id, this, value);
938 }
939 }
940 prototypeValues.setAttributes(id, applyDescriptorToAttributeBitset(attr, desc));
941 return;
942 }
943 }
944 }
945 }
946 super.defineOwnProperty(cx, key, desc);
947 }
948
949
950 @Override
951 protected ScriptableObject getOwnPropertyDescriptor(Context cx, Object id) {
952 ScriptableObject desc = super.getOwnPropertyDescriptor(cx, id);
953 if (desc == null) {
954 if (id instanceof String) {
955 desc = getBuiltInDescriptor((String) id);
956 } else if (ScriptRuntime.isSymbol(id)) {
957 desc = getBuiltInDescriptor(((NativeSymbol)id).getKey());
958 }
959 }
960 return desc;
961 }
962
963 private ScriptableObject getBuiltInDescriptor(String name) {
964 Object value = null;
965 int attr = EMPTY;
966
967 Scriptable scope = getParentScope();
968 if (scope == null) {
969 scope = this;
970 }
971
972 int info = findInstanceIdInfo(name);
973 if (info != 0) {
974 int id = (info & 0xFFFF);
975 value = getInstanceIdValue(id);
976 attr = (info >>> 16);
977 return buildDataDescriptor(scope, value, attr);
978 }
979 if (prototypeValues != null) {
980 int id = prototypeValues.findId(name);
981 if (id != 0) {
982 value = prototypeValues.get(id);
983 attr = prototypeValues.getAttributes(id);
984 return buildDataDescriptor(scope, value, attr);
985 }
986 }
987 return null;
988 }
989
990 private ScriptableObject getBuiltInDescriptor(Symbol key) {
991 Object value = null;
992 int attr = EMPTY;
993
994 Scriptable scope = getParentScope();
995 if (scope == null) {
996 scope = this;
997 }
998
999 if (prototypeValues != null) {
1000 int id = prototypeValues.findId(key);
1001 if (id != 0) {
1002 value = prototypeValues.get(id);
1003 attr = prototypeValues.getAttributes(id);
1004 return buildDataDescriptor(scope, value, attr);
1005 }
1006 }
1007 return null;
1008 }
1009
1010 private void readObject(ObjectInputStream stream)
1011 throws IOException, ClassNotFoundException
1012 {
1013 stream.defaultReadObject();
1014 int maxPrototypeId = stream.readInt();
1015 if (maxPrototypeId != 0) {
1016 activatePrototypeMap(maxPrototypeId);
1017 }
1018 }
1019
1020 private void writeObject(ObjectOutputStream stream)
1021 throws IOException
1022 {
1023 stream.defaultWriteObject();
1024 int maxPrototypeId = 0;
1025 if (prototypeValues != null) {
1026 maxPrototypeId = prototypeValues.getMaxId();
1027 }
1028 stream.writeInt(maxPrototypeId);
1029 }
1030
1031 }
1032