1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=99:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * The Original Code is Mozilla Communicator client code, released
20 * The Initial Developer of the Original Code is
21 * Netscape Communications Corporation.
22 * Portions created by the Initial Developer are Copyright (C) 1998
23 * the Initial Developer. All Rights Reserved.
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #ifndef jsobjinlines_h___
42 #define jsobjinlines_h___
51 #include "jspropertytree.h"
54 #include "jsstaticcheck.h"
57 /* Headers included for inline implementations used by this header. */
61 #include "jsscopeinlines.h"
64 #include "jsfuninlines.h"
65 #include "jsgcinlines.h"
69 JSObject::preventExtensions(JSContext *cx, js::AutoIdVector *props)
71 JS_ASSERT(isExtensible());
73 if (js::FixOp fix = getOps()->fix) {
75 if (!fix(cx, this, &success, props))
78 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CANT_CHANGE_EXTENSIBILITY);
82 if (!GetPropertyNames(cx, this, JSITER_HIDDEN | JSITER_OWNONLY, props))
87 extensibleShapeChange(cx);
89 flags |= NOT_EXTENSIBLE;
94 JSObject::brand(JSContext *cx)
96 JS_ASSERT(!generic());
97 JS_ASSERT(!branded());
98 JS_ASSERT(isNative());
100 if (js_IsPropertyCacheDisabled(cx)) // check for rt->shapeGen overflow
107 JSObject::unbrand(JSContext *cx)
109 JS_ASSERT(isNative());
111 generateOwnShape(cx);
112 if (js_IsPropertyCacheDisabled(cx)) // check for rt->shapeGen overflow
121 JSObject::syncSpecialEquality()
123 if (clasp->ext.equality)
124 flags |= JSObject::HAS_EQUALITY;
128 JSObject::finalize(JSContext *cx)
130 /* Cope with stillborn objects that have no map. */
134 /* Finalize obj first, in case it needs map and slots. */
135 js::Class *clasp = getClass();
137 clasp->finalize(cx, this);
139 js::Probes::finalizeObject(this);
145 * Property read barrier for deferred cloning of compiler-created function
146 * objects optimized as typically non-escaping, ad-hoc methods in obj.
148 inline const js::Shape *
149 JSObject::methodReadBarrier(JSContext *cx, const js::Shape &shape, js::Value *vp)
151 JS_ASSERT(canHaveMethodBarrier());
152 JS_ASSERT(hasMethodBarrier());
153 JS_ASSERT(nativeContains(shape));
154 JS_ASSERT(shape.isMethod());
155 JS_ASSERT(&shape.methodObject() == &vp->toObject());
156 JS_ASSERT(shape.writable());
157 JS_ASSERT(shape.slot != SHAPE_INVALID_SLOT);
158 JS_ASSERT(shape.hasDefaultSetter() || shape.setterOp() == js_watch_set);
159 JS_ASSERT(!isGlobal()); /* i.e. we are not changing the global shape */
161 JSObject *funobj = &vp->toObject();
162 JSFunction *fun = funobj->getFunctionPrivate();
163 JS_ASSERT(fun == funobj);
164 JS_ASSERT(FUN_NULL_CLOSURE(fun));
166 funobj = CloneFunctionObject(cx, fun, funobj->getParent());
169 funobj->setMethodObj(*this);
172 * Replace the method property with an ordinary data property. This is
173 * equivalent to this->setProperty(cx, shape.id, vp) except that any
174 * watchpoint on the property is not triggered.
176 uint32 slot = shape.slot;
177 const js::Shape *newshape = methodShapeChange(cx, shape);
180 JS_ASSERT(!newshape->isMethod());
181 JS_ASSERT(newshape->slot == slot);
182 vp->setObject(*funobj);
183 nativeSetSlot(slot, *vp);
186 if (cx->runtime->functionMeterFilename) {
187 JS_FUNCTION_METER(cx, mreadbarrier);
189 typedef JSRuntime::FunctionCountMap HM;
190 HM &h = cx->runtime->methodReadBarrierCountMap;
191 HM::AddPtr p = h.lookupForAdd(fun);
195 JS_ASSERT(p->key == fun);
203 static JS_ALWAYS_INLINE bool
204 ChangesMethodValue(const js::Value &prev, const js::Value &v)
207 return prev.isObject() && (prevObj = &prev.toObject())->isFunction() &&
208 (!v.isObject() || &v.toObject() != prevObj);
211 inline const js::Shape *
212 JSObject::methodWriteBarrier(JSContext *cx, const js::Shape &shape, const js::Value &v)
214 if (brandedOrHasMethodBarrier() && shape.slot != SHAPE_INVALID_SLOT) {
215 const js::Value &prev = nativeGetSlot(shape.slot);
217 if (ChangesMethodValue(prev, v)) {
218 JS_FUNCTION_METER(cx, mwritebarrier);
219 return methodShapeChange(cx, shape);
226 JSObject::methodWriteBarrier(JSContext *cx, uint32 slot, const js::Value &v)
228 if (brandedOrHasMethodBarrier()) {
229 const js::Value &prev = nativeGetSlot(slot);
231 if (ChangesMethodValue(prev, v)) {
232 JS_FUNCTION_METER(cx, mwslotbarrier);
233 return methodShapeChange(cx, slot);
240 JSObject::ensureClassReservedSlots(JSContext *cx)
242 return !nativeEmpty() || ensureClassReservedSlotsForEmptyObject(cx);
246 JSObject::getReservedSlot(uintN index) const
248 return (index < numSlots()) ? getSlot(index) : js::UndefinedValue();
252 JSObject::canHaveMethodBarrier() const
254 return isObject() || isFunction() || isPrimitive() || isDate();
258 JSObject::isPrimitive() const
260 return isNumber() || isString() || isBoolean();
263 inline const js::Value &
264 JSObject::getPrimitiveThis() const
266 JS_ASSERT(isPrimitive());
267 return getSlot(JSSLOT_PRIMITIVE_THIS);
271 JSObject::setPrimitiveThis(const js::Value &pthis)
273 JS_ASSERT(isPrimitive());
274 setSlot(JSSLOT_PRIMITIVE_THIS, pthis);
277 inline /* gc::FinalizeKind */ unsigned
278 JSObject::finalizeKind() const
280 return js::gc::FinalizeKind(arena()->header()->thingKind);
284 JSObject::numFixedSlots() const
287 return JSObject::FUN_CLASS_RESERVED_SLOTS;
288 if (!hasSlotsArray())
290 return js::gc::GetGCKindSlots(js::gc::FinalizeKind(finalizeKind()));
294 JSObject::slotsAndStructSize(uint32 nslots) const
296 bool isFun = isFunction() && this == (JSObject*) getPrivate();
298 int ndslots = hasSlotsArray() ? nslots : 0;
299 int nfslots = isFun ? 0 : numFixedSlots();
301 return sizeof(js::Value) * (ndslots + nfslots)
302 + isFun ? sizeof(JSFunction) : sizeof(JSObject);
306 JSObject::getArrayLength() const
308 JS_ASSERT(isArray());
309 return (uint32)(size_t) getPrivate();
313 JSObject::setArrayLength(uint32 length)
315 JS_ASSERT(isArray());
316 setPrivate((void*) length);
320 JSObject::getDenseArrayCapacity()
322 JS_ASSERT(isDenseArray());
327 JSObject::getDenseArrayElements()
329 JS_ASSERT(isDenseArray());
333 inline const js::Value &
334 JSObject::getDenseArrayElement(uintN idx)
336 JS_ASSERT(isDenseArray());
341 JSObject::addressOfDenseArrayElement(uintN idx)
343 JS_ASSERT(isDenseArray());
344 return &getSlotRef(idx);
348 JSObject::setDenseArrayElement(uintN idx, const js::Value &val)
350 JS_ASSERT(isDenseArray());
355 JSObject::shrinkDenseArrayElements(JSContext *cx, uintN cap)
357 JS_ASSERT(isDenseArray());
358 shrinkSlots(cx, cap);
362 JSObject::setArgsLength(uint32 argc)
364 JS_ASSERT(isArguments());
365 JS_ASSERT(argc <= JS_ARGS_LENGTH_MAX);
366 JS_ASSERT(UINT32_MAX > (uint64(argc) << ARGS_PACKED_BITS_COUNT));
367 getSlotRef(JSSLOT_ARGS_LENGTH).setInt32(argc << ARGS_PACKED_BITS_COUNT);
368 JS_ASSERT(!isArgsLengthOverridden());
372 JSObject::getArgsInitialLength() const
374 JS_ASSERT(isArguments());
375 uint32 argc = uint32(getSlot(JSSLOT_ARGS_LENGTH).toInt32()) >> ARGS_PACKED_BITS_COUNT;
376 JS_ASSERT(argc <= JS_ARGS_LENGTH_MAX);
381 JSObject::setArgsLengthOverridden()
383 JS_ASSERT(isArguments());
384 getSlotRef(JSSLOT_ARGS_LENGTH).getInt32Ref() |= ARGS_LENGTH_OVERRIDDEN_BIT;
388 JSObject::isArgsLengthOverridden() const
390 JS_ASSERT(isArguments());
391 const js::Value &v = getSlot(JSSLOT_ARGS_LENGTH);
392 return v.toInt32() & ARGS_LENGTH_OVERRIDDEN_BIT;
395 inline js::ArgumentsData *
396 JSObject::getArgsData() const
398 JS_ASSERT(isArguments());
399 return (js::ArgumentsData *) getSlot(JSSLOT_ARGS_DATA).toPrivate();
403 JSObject::setArgsData(js::ArgumentsData *data)
405 JS_ASSERT(isArguments());
406 getSlotRef(JSSLOT_ARGS_DATA).setPrivate(data);
409 inline const js::Value &
410 JSObject::getArgsCallee() const
412 return getArgsData()->callee;
416 JSObject::setArgsCallee(const js::Value &callee)
418 getArgsData()->callee = callee;
421 inline const js::Value &
422 JSObject::getArgsElement(uint32 i) const
424 JS_ASSERT(isArguments());
425 JS_ASSERT(i < getArgsInitialLength());
426 return getArgsData()->slots[i];
430 JSObject::getArgsElements() const
432 JS_ASSERT(isArguments());
433 return getArgsData()->slots;
437 JSObject::addressOfArgsElement(uint32 i)
439 JS_ASSERT(isArguments());
440 JS_ASSERT(i < getArgsInitialLength());
441 return &getArgsData()->slots[i];
445 JSObject::setArgsElement(uint32 i, const js::Value &v)
447 JS_ASSERT(isArguments());
448 JS_ASSERT(i < getArgsInitialLength());
449 getArgsData()->slots[i] = v;
453 JSObject::callIsForEval() const
456 JS_ASSERT(getSlot(JSSLOT_CALL_CALLEE).isObjectOrNull());
457 JS_ASSERT_IF(getSlot(JSSLOT_CALL_CALLEE).isObject(),
458 getSlot(JSSLOT_CALL_CALLEE).toObject().isFunction());
459 return getSlot(JSSLOT_CALL_CALLEE).isNull();
462 inline JSStackFrame *
463 JSObject::maybeCallObjStackFrame() const
466 return reinterpret_cast<JSStackFrame *>(getPrivate());
470 JSObject::setCallObjCallee(JSObject *callee)
473 JS_ASSERT_IF(callee, callee->isFunction());
474 return getSlotRef(JSSLOT_CALL_CALLEE).setObjectOrNull(callee);
478 JSObject::getCallObjCallee() const
481 return getSlot(JSSLOT_CALL_CALLEE).toObjectOrNull();
485 JSObject::getCallObjCalleeFunction() const
488 return getSlot(JSSLOT_CALL_CALLEE).toObject().getFunctionPrivate();
491 inline const js::Value &
492 JSObject::getCallObjArguments() const
495 JS_ASSERT(!callIsForEval());
496 return getSlot(JSSLOT_CALL_ARGUMENTS);
500 JSObject::setCallObjArguments(const js::Value &v)
503 JS_ASSERT(!callIsForEval());
504 setSlot(JSSLOT_CALL_ARGUMENTS, v);
507 inline const js::Value &
508 JSObject::callObjArg(uintN i) const
511 JS_ASSERT(i < getCallObjCalleeFunction()->nargs);
512 return getSlot(JSObject::CALL_RESERVED_SLOTS + i);
516 JSObject::callObjArg(uintN i)
519 JS_ASSERT(i < getCallObjCalleeFunction()->nargs);
520 return getSlotRef(JSObject::CALL_RESERVED_SLOTS + i);
523 inline const js::Value &
524 JSObject::callObjVar(uintN i) const
526 JSFunction *fun = getCallObjCalleeFunction();
527 JS_ASSERT(fun->nargs == fun->script()->bindings.countArgs());
528 JS_ASSERT(i < fun->script()->bindings.countVars());
529 return getSlot(JSObject::CALL_RESERVED_SLOTS + fun->nargs + i);
533 JSObject::callObjVar(uintN i)
535 JSFunction *fun = getCallObjCalleeFunction();
536 JS_ASSERT(fun->nargs == fun->script()->bindings.countArgs());
537 JS_ASSERT(i < fun->script()->bindings.countVars());
538 return getSlotRef(JSObject::CALL_RESERVED_SLOTS + fun->nargs + i);
541 inline const js::Value &
542 JSObject::getDateUTCTime() const
545 return getSlot(JSSLOT_DATE_UTC_TIME);
549 JSObject::setDateUTCTime(const js::Value &time)
552 setSlot(JSSLOT_DATE_UTC_TIME, time);
556 JSObject::getFlatClosureUpvars() const
559 JSFunction *fun = getFunctionPrivate();
560 JS_ASSERT(fun->isFlatClosure());
561 JS_ASSERT(fun->script()->bindings.countUpvars() == fun->script()->upvars()->length);
563 return (js::Value *) getSlot(JSSLOT_FLAT_CLOSURE_UPVARS).toPrivate();
567 JSObject::getFlatClosureUpvar(uint32 i) const
569 JS_ASSERT(i < getFunctionPrivate()->script()->bindings.countUpvars());
570 return getFlatClosureUpvars()[i];
574 JSObject::getFlatClosureUpvar(uint32 i)
576 JS_ASSERT(i < getFunctionPrivate()->script()->bindings.countUpvars());
577 return getFlatClosureUpvars()[i];
581 JSObject::setFlatClosureUpvars(js::Value *upvars)
583 JS_ASSERT(isFunction());
584 JS_ASSERT(FUN_FLAT_CLOSURE(getFunctionPrivate()));
585 getSlotRef(JSSLOT_FLAT_CLOSURE_UPVARS).setPrivate(upvars);
589 JSObject::hasMethodObj(const JSObject& obj) const
591 return JSSLOT_FUN_METHOD_OBJ < numSlots() &&
592 getSlot(JSSLOT_FUN_METHOD_OBJ).isObject() &&
593 &getSlot(JSSLOT_FUN_METHOD_OBJ).toObject() == &obj;
597 JSObject::setMethodObj(JSObject& obj)
599 getSlotRef(JSSLOT_FUN_METHOD_OBJ).setObject(obj);
602 inline js::NativeIterator *
603 JSObject::getNativeIterator() const
605 return (js::NativeIterator *) getPrivate();
609 JSObject::setNativeIterator(js::NativeIterator *ni)
614 inline JSLinearString *
615 JSObject::getNamePrefix() const
617 JS_ASSERT(isNamespace() || isQName());
618 const js::Value &v = getSlot(JSSLOT_NAME_PREFIX);
619 return !v.isUndefined() ? v.toString()->assertIsLinear() : NULL;
623 JSObject::getNamePrefixVal() const
625 JS_ASSERT(isNamespace() || isQName());
626 return js::Jsvalify(getSlot(JSSLOT_NAME_PREFIX));
630 JSObject::setNamePrefix(JSLinearString *prefix)
632 JS_ASSERT(isNamespace() || isQName());
633 setSlot(JSSLOT_NAME_PREFIX, prefix ? js::StringValue(prefix) : js::UndefinedValue());
637 JSObject::clearNamePrefix()
639 JS_ASSERT(isNamespace() || isQName());
640 setSlot(JSSLOT_NAME_PREFIX, js::UndefinedValue());
643 inline JSLinearString *
644 JSObject::getNameURI() const
646 JS_ASSERT(isNamespace() || isQName());
647 const js::Value &v = getSlot(JSSLOT_NAME_URI);
648 return !v.isUndefined() ? v.toString()->assertIsLinear() : NULL;
652 JSObject::getNameURIVal() const
654 JS_ASSERT(isNamespace() || isQName());
655 return js::Jsvalify(getSlot(JSSLOT_NAME_URI));
659 JSObject::setNameURI(JSLinearString *uri)
661 JS_ASSERT(isNamespace() || isQName());
662 setSlot(JSSLOT_NAME_URI, uri ? js::StringValue(uri) : js::UndefinedValue());
666 JSObject::getNamespaceDeclared() const
668 JS_ASSERT(isNamespace());
669 return js::Jsvalify(getSlot(JSSLOT_NAMESPACE_DECLARED));
673 JSObject::setNamespaceDeclared(jsval decl)
675 JS_ASSERT(isNamespace());
676 setSlot(JSSLOT_NAMESPACE_DECLARED, js::Valueify(decl));
679 inline JSLinearString *
680 JSObject::getQNameLocalName() const
682 JS_ASSERT(isQName());
683 const js::Value &v = getSlot(JSSLOT_QNAME_LOCAL_NAME);
684 return !v.isUndefined() ? v.toString()->assertIsLinear() : NULL;
688 JSObject::getQNameLocalNameVal() const
690 JS_ASSERT(isQName());
691 return js::Jsvalify(getSlot(JSSLOT_QNAME_LOCAL_NAME));
695 JSObject::setQNameLocalName(JSLinearString *name)
697 JS_ASSERT(isQName());
698 setSlot(JSSLOT_QNAME_LOCAL_NAME, name ? js::StringValue(name) : js::UndefinedValue());
702 JSObject::getWithThis() const
704 return &getSlot(JSSLOT_WITH_THIS).toObject();
708 JSObject::setWithThis(JSObject *thisp)
710 getSlotRef(JSSLOT_WITH_THIS).setObject(*thisp);
714 JSObject::init(JSContext *cx, js::Class *aclasp, JSObject *proto, JSObject *parent,
715 void *priv, bool useHoles)
722 * NB: objShape must not be set here; rather, the caller must call setMap
723 * or setSharedNonNativeMap after calling init. To defend this requirement
724 * we set map to null in DEBUG builds, and set objShape to a value we then
725 * assert obj->shape() never returns.
728 objShape = JSObjectMap::INVALID_SHAPE;
735 slots = fixedSlots();
738 * Fill the fixed slots with undefined or array holes. This object must
739 * already have its capacity filled in, as by js_NewGCObject.
741 JS_ASSERT(capacity == numFixedSlots());
742 ClearValueRange(slots, capacity, useHoles);
748 JSObject::finish(JSContext *cx)
752 JS_LOCK_RUNTIME_VOID(cx->runtime, cx->runtime->liveObjectProps -= propertyCount());
757 cx->free(emptyShapes);
761 JSObject::initSharingEmptyShape(JSContext *cx,
766 /* js::gc::FinalizeKind */ unsigned kind)
768 init(cx, aclasp, proto, parent, privateValue, false);
770 JS_ASSERT(!isDenseArray());
772 js::EmptyShape *empty = proto->getEmptyShape(cx, aclasp, kind);
781 JSObject::freeSlotsArray(JSContext *cx)
783 JS_ASSERT(hasSlotsArray());
788 JSObject::revertToFixedSlots(JSContext *cx)
790 JS_ASSERT(hasSlotsArray());
791 size_t fixed = numFixedSlots();
792 JS_ASSERT(capacity >= fixed);
793 memcpy(fixedSlots(), slots, fixed * sizeof(js::Value));
795 slots = fixedSlots();
800 JSObject::hasProperty(JSContext *cx, jsid id, bool *foundp, uintN flags)
804 JSAutoResolveFlags rf(cx, flags);
805 if (!lookupProperty(cx, id, &pobj, &prop))
812 JSObject::isCallable()
814 return isFunction() || getClass()->call;
818 js_IsCallable(const js::Value &v)
820 return v.isObject() && v.toObject().isCallable();
825 class AutoPropDescArrayRooter : private AutoGCRooter
828 AutoPropDescArrayRooter(JSContext *cx)
829 : AutoGCRooter(cx, DESCRIPTORS), descriptors(cx)
833 if (!descriptors.append(PropDesc()))
835 return &descriptors.back();
838 PropDesc& operator[](size_t i) {
839 JS_ASSERT(i < descriptors.length());
840 return descriptors[i];
843 friend void AutoGCRooter::trace(JSTracer *trc);
846 PropDescArray descriptors;
849 class AutoPropertyDescriptorRooter : private AutoGCRooter, public PropertyDescriptor
852 AutoPropertyDescriptorRooter(JSContext *cx) : AutoGCRooter(cx, DESCRIPTOR) {
855 getter = (PropertyOp) NULL;
856 setter = (StrictPropertyOp) NULL;
857 value.setUndefined();
860 AutoPropertyDescriptorRooter(JSContext *cx, PropertyDescriptor *desc)
861 : AutoGCRooter(cx, DESCRIPTOR)
865 getter = desc->getter;
866 setter = desc->setter;
870 friend void AutoGCRooter::trace(JSTracer *trc);
874 InitScopeForObject(JSContext* cx, JSObject* obj, js::Class *clasp, JSObject* proto,
875 gc::FinalizeKind kind)
877 JS_ASSERT(clasp->isNative());
878 JS_ASSERT(proto == obj->getProto());
880 /* Share proto's emptyShape only if obj is similar to proto. */
881 js::EmptyShape *empty = NULL;
884 if (proto->canProvideEmptyShape(clasp)) {
885 empty = proto->getEmptyShape(cx, clasp, kind);
892 empty = js::EmptyShape::create(cx, clasp);
895 uint32 freeslot = JSSLOT_FREE(clasp);
896 if (freeslot > obj->numSlots() && !obj->allocSlots(cx, freeslot))
904 /* The GC nulls map initially. It should still be null on error. */
905 JS_ASSERT(!obj->map);
910 * Helper optimized for creating a native instance of the given class (not the
911 * class's prototype object). Use this in preference to NewObject, but use
912 * NewBuiltinClassInstance if you need the default class prototype as proto,
913 * and its parent global as parent.
915 static inline JSObject *
916 NewNativeClassInstance(JSContext *cx, Class *clasp, JSObject *proto,
917 JSObject *parent, gc::FinalizeKind kind)
923 * Allocate an object from the GC heap and initialize all its fields before
924 * doing any operation that can potentially trigger GC.
926 JSObject* obj = js_NewGCObject(cx, kind);
930 * Default parent to the parent of the prototype, which was set from
931 * the parent of the prototype's constructor.
933 bool useHoles = (clasp == &js_ArrayClass);
934 obj->init(cx, clasp, proto, parent, NULL, useHoles);
936 JS_ASSERT(proto->canProvideEmptyShape(clasp));
937 js::EmptyShape *empty = proto->getEmptyShape(cx, clasp, kind);
948 static inline JSObject *
949 NewNativeClassInstance(JSContext *cx, Class *clasp, JSObject *proto, JSObject *parent)
951 gc::FinalizeKind kind = gc::GetGCObjectKind(JSCLASS_RESERVED_SLOTS(clasp));
952 return NewNativeClassInstance(cx, clasp, proto, parent, kind);
956 FindClassPrototype(JSContext *cx, JSObject *scope, JSProtoKey protoKey, JSObject **protop,
960 * Helper used to create Boolean, Date, RegExp, etc. instances of built-in
961 * classes with class prototypes of the same Class. See, e.g., jsdate.cpp,
962 * jsregexp.cpp, and js_PrimitiveToObject in jsobj.cpp. Use this to get the
963 * right default proto and parent for clasp in cx.
965 static inline JSObject *
966 NewBuiltinClassInstance(JSContext *cx, Class *clasp, gc::FinalizeKind kind)
968 VOUCH_DOES_NOT_REQUIRE_STACK();
970 JSProtoKey protoKey = JSCLASS_CACHED_PROTO_KEY(clasp);
971 JS_ASSERT(protoKey != JSProto_Null);
973 /* NB: inline-expanded and specialized version of js_GetClassPrototype. */
976 global = cx->globalObject;
977 OBJ_TO_INNER_OBJECT(cx, global);
981 global = cx->fp()->scopeChain().getGlobal();
983 JS_ASSERT(global->isGlobal());
985 const Value &v = global->getReservedSlot(JSProto_LIMIT + protoKey);
988 proto = &v.toObject();
989 JS_ASSERT(proto->getParent() == global);
991 if (!FindClassPrototype(cx, global, protoKey, &proto, clasp))
995 return NewNativeClassInstance(cx, clasp, proto, global, kind);
998 static inline JSObject *
999 NewBuiltinClassInstance(JSContext *cx, Class *clasp)
1001 gc::FinalizeKind kind = gc::GetGCObjectKind(JSCLASS_RESERVED_SLOTS(clasp));
1002 return NewBuiltinClassInstance(cx, clasp, kind);
1005 static inline JSProtoKey
1006 GetClassProtoKey(js::Class *clasp)
1008 JSProtoKey key = JSCLASS_CACHED_PROTO_KEY(clasp);
1009 if (key != JSProto_Null)
1011 if (clasp->flags & JSCLASS_IS_ANONYMOUS)
1012 return JSProto_Object;
1013 return JSProto_Null;
1016 namespace WithProto {
1024 * Create an instance of any class, native or not, JSFunction-sized or not.
1026 * If withProto is 'Class':
1028 * for a built-in class:
1029 * use the memoized original value of the class constructor .prototype
1032 * the current value of .prototype
1036 * If parent is null, default it to proto->getParent() if proto is non
1037 * null, else to null.
1039 * If withProto is 'Given':
1040 * We allocate an object with exactly the given proto. A null parent
1041 * defaults to proto->getParent() if proto is non-null (else to null).
1043 * If isFunction is true, return a JSFunction-sized object. If isFunction is
1044 * false, return a normal object.
1046 * Note that as a template, there will be lots of instantiations, which means
1047 * the internals will be specialized based on the template parameters.
1049 static JS_ALWAYS_INLINE bool
1050 FindProto(JSContext *cx, js::Class *clasp, JSObject *parent, JSObject ** proto)
1052 JSProtoKey protoKey = GetClassProtoKey(clasp);
1053 if (!js_GetClassPrototype(cx, parent, protoKey, proto, clasp))
1055 if (!(*proto) && !js_GetClassPrototype(cx, parent, JSProto_Object, proto))
1063 template <bool withProto, bool isFunction>
1064 static JS_ALWAYS_INLINE JSObject *
1065 NewObject(JSContext *cx, js::Class *clasp, JSObject *proto, JSObject *parent,
1066 gc::FinalizeKind kind)
1068 /* Bootstrap the ur-object, and make it the default prototype object. */
1069 if (withProto == WithProto::Class && !proto) {
1070 if (!FindProto(cx, clasp, parent, &proto))
1075 * Allocate an object from the GC heap and initialize all its fields before
1076 * doing any operation that can potentially trigger GC. Functions have a
1077 * larger non-standard allocation size.
1079 * The should be specialized by the template.
1081 JSObject* obj = isFunction ? js_NewGCFunction(cx) : js_NewGCObject(cx, kind);
1085 /* This needs to match up with the size of JSFunction::data_padding. */
1086 JS_ASSERT_IF(isFunction, kind == gc::FINALIZE_OBJECT2);
1089 * Default parent to the parent of the prototype, which was set from
1090 * the parent of the prototype's constructor.
1092 obj->init(cx, clasp, proto,
1093 (!parent && proto) ? proto->getParent() : parent,
1094 NULL, clasp == &js_ArrayClass);
1096 if (clasp->isNative()) {
1097 if (!InitScopeForObject(cx, obj, clasp, proto, kind)) {
1102 obj->setSharedNonNativeMap();
1106 Probes::createObject(cx, obj);
1109 } /* namespace detail */
1111 static JS_ALWAYS_INLINE JSObject *
1112 NewFunction(JSContext *cx, JSObject *parent)
1114 return detail::NewObject<WithProto::Class, true>(cx, &js_FunctionClass, NULL, parent,
1115 gc::FINALIZE_OBJECT2);
1118 template <WithProto::e withProto>
1119 static JS_ALWAYS_INLINE JSObject *
1120 NewNonFunction(JSContext *cx, js::Class *clasp, JSObject *proto, JSObject *parent,
1121 gc::FinalizeKind kind)
1123 return detail::NewObject<withProto, false>(cx, clasp, proto, parent, kind);
1126 template <WithProto::e withProto>
1127 static JS_ALWAYS_INLINE JSObject *
1128 NewNonFunction(JSContext *cx, js::Class *clasp, JSObject *proto, JSObject *parent)
1130 gc::FinalizeKind kind = gc::GetGCObjectKind(JSCLASS_RESERVED_SLOTS(clasp));
1131 return detail::NewObject<withProto, false>(cx, clasp, proto, parent, kind);
1134 template <WithProto::e withProto>
1135 static JS_ALWAYS_INLINE JSObject *
1136 NewObject(JSContext *cx, js::Class *clasp, JSObject *proto, JSObject *parent,
1137 gc::FinalizeKind kind)
1139 if (clasp == &js_FunctionClass)
1140 return detail::NewObject<withProto, true>(cx, clasp, proto, parent, kind);
1141 return detail::NewObject<withProto, false>(cx, clasp, proto, parent, kind);
1144 template <WithProto::e withProto>
1145 static JS_ALWAYS_INLINE JSObject *
1146 NewObject(JSContext *cx, js::Class *clasp, JSObject *proto, JSObject *parent)
1148 gc::FinalizeKind kind = gc::GetGCObjectKind(JSCLASS_RESERVED_SLOTS(clasp));
1149 return NewObject<withProto>(cx, clasp, proto, parent, kind);
1153 * As for gc::GetGCObjectKind, where numSlots is a guess at the final size of
1154 * the object, zero if the final size is unknown.
1156 static inline gc::FinalizeKind
1157 GuessObjectGCKind(size_t numSlots, bool isArray)
1160 return gc::GetGCObjectKind(numSlots);
1161 return isArray ? gc::FINALIZE_OBJECT8 : gc::FINALIZE_OBJECT4;
1165 * Get the GC kind to use for scripted 'new' on the given class.
1166 * FIXME bug 547327: estimate the size from the allocation site.
1168 static inline gc::FinalizeKind
1169 NewObjectGCKind(JSContext *cx, js::Class *clasp)
1171 if (clasp == &js_ArrayClass || clasp == &js_SlowArrayClass)
1172 return gc::FINALIZE_OBJECT8;
1173 if (clasp == &js_FunctionClass)
1174 return gc::FINALIZE_OBJECT2;
1175 return gc::FINALIZE_OBJECT4;
1178 /* Make an object with pregenerated shape from a NEWOBJECT bytecode. */
1179 static inline JSObject *
1180 CopyInitializerObject(JSContext *cx, JSObject *baseobj)
1182 JS_ASSERT(baseobj->getClass() == &js_ObjectClass);
1183 JS_ASSERT(!baseobj->inDictionaryMode());
1185 gc::FinalizeKind kind = gc::FinalizeKind(baseobj->finalizeKind());
1186 JSObject *obj = NewBuiltinClassInstance(cx, &js_ObjectClass, kind);
1188 if (!obj || !obj->ensureSlots(cx, baseobj->numSlots()))
1191 obj->flags = baseobj->flags;
1192 obj->lastProp = baseobj->lastProp;
1193 obj->objShape = baseobj->objShape;
1199 * When we have an object of a builtin class, we don't quite know what its
1200 * valueOf/toString methods are, since these methods may have been overwritten
1201 * or shadowed. However, we can still do better than js_TryMethod by
1202 * hard-coding the necessary properties for us to find the native we expect.
1204 * TODO: a per-thread shape-based cache would be faster and simpler.
1206 static JS_ALWAYS_INLINE bool
1207 ClassMethodIsNative(JSContext *cx, JSObject *obj, Class *clasp, jsid methodid,
1210 JS_ASSERT(obj->getClass() == clasp);
1212 if (HasNativeMethod(obj, methodid, native))
1215 JSObject *pobj = obj->getProto();
1216 return pobj && pobj->getClass() == clasp &&
1217 HasNativeMethod(pobj, methodid, native);
1220 } /* namespace js */
1222 #endif /* jsobjinlines_h___ */