michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* JSClass definition and its component types, plus related interfaces. */ michael@0: michael@0: #ifndef js_Class_h michael@0: #define js_Class_h michael@0: michael@0: #include "mozilla/NullPtr.h" michael@0: michael@0: #include "jstypes.h" michael@0: michael@0: #include "js/CallArgs.h" michael@0: #include "js/Id.h" michael@0: #include "js/TypeDecls.h" michael@0: michael@0: /* michael@0: * A JSClass acts as a vtable for JS objects that allows JSAPI clients to michael@0: * control various aspects of the behavior of an object like property lookup. michael@0: * js::Class is an engine-private extension that allows more control over michael@0: * object behavior and, e.g., allows custom slow layout. michael@0: */ michael@0: michael@0: class JSFreeOp; michael@0: struct JSFunctionSpec; michael@0: michael@0: namespace js { michael@0: michael@0: class Class; michael@0: class FreeOp; michael@0: class PropertyName; michael@0: class Shape; michael@0: michael@0: // This is equal to JSFunction::class_. Use it in places where you don't want michael@0: // to #include jsfun.h. michael@0: extern JS_FRIEND_DATA(const js::Class* const) FunctionClassPtr; michael@0: michael@0: } // namespace js michael@0: michael@0: // JSClass operation signatures. michael@0: michael@0: // Add or get a property named by id in obj. Note the jsid id type -- id may michael@0: // be a string (Unicode property identifier) or an int (element index). The michael@0: // *vp out parameter, on success, is the new property value after the action. michael@0: typedef bool michael@0: (* JSPropertyOp)(JSContext *cx, JS::HandleObject obj, JS::HandleId id, michael@0: JS::MutableHandleValue vp); michael@0: michael@0: // Set a property named by id in obj, treating the assignment as strict michael@0: // mode code if strict is true. Note the jsid id type -- id may be a string michael@0: // (Unicode property identifier) or an int (element index). The *vp out michael@0: // parameter, on success, is the new property value after the michael@0: // set. michael@0: typedef bool michael@0: (* JSStrictPropertyOp)(JSContext *cx, JS::HandleObject obj, JS::HandleId id, michael@0: bool strict, JS::MutableHandleValue vp); michael@0: michael@0: // Delete a property named by id in obj. michael@0: // michael@0: // If an error occurred, return false as per normal JSAPI error practice. michael@0: // michael@0: // If no error occurred, but the deletion attempt wasn't allowed (perhaps michael@0: // because the property was non-configurable), set *succeeded to false and michael@0: // return true. This will cause |delete obj[id]| to evaluate to false in michael@0: // non-strict mode code, and to throw a TypeError in strict mode code. michael@0: // michael@0: // If no error occurred and the deletion wasn't disallowed (this is *not* the michael@0: // same as saying that a deletion actually occurred -- deleting a non-existent michael@0: // property, or an inherited property, is allowed -- it's just pointless), michael@0: // set *succeeded to true and return true. michael@0: typedef bool michael@0: (* JSDeletePropertyOp)(JSContext *cx, JS::HandleObject obj, JS::HandleId id, michael@0: bool *succeeded); michael@0: michael@0: // This function type is used for callbacks that enumerate the properties of michael@0: // a JSObject. The behavior depends on the value of enum_op: michael@0: // michael@0: // JSENUMERATE_INIT michael@0: // A new, opaque iterator state should be allocated and stored in *statep. michael@0: // (You can use PRIVATE_TO_JSVAL() to tag the pointer to be stored). michael@0: // michael@0: // The number of properties that will be enumerated should be returned as michael@0: // an integer jsval in *idp, if idp is non-null, and provided the number of michael@0: // enumerable properties is known. If idp is non-null and the number of michael@0: // enumerable properties can't be computed in advance, *idp should be set michael@0: // to JSVAL_ZERO. michael@0: // michael@0: // JSENUMERATE_INIT_ALL michael@0: // Used identically to JSENUMERATE_INIT, but exposes all properties of the michael@0: // object regardless of enumerability. michael@0: // michael@0: // JSENUMERATE_NEXT michael@0: // A previously allocated opaque iterator state is passed in via statep. michael@0: // Return the next jsid in the iteration using *idp. The opaque iterator michael@0: // state pointed at by statep is destroyed and *statep is set to JSVAL_NULL michael@0: // if there are no properties left to enumerate. michael@0: // michael@0: // JSENUMERATE_DESTROY michael@0: // Destroy the opaque iterator state previously allocated in *statep by a michael@0: // call to this function when enum_op was JSENUMERATE_INIT or michael@0: // JSENUMERATE_INIT_ALL. michael@0: // michael@0: // The return value is used to indicate success, with a value of false michael@0: // indicating failure. michael@0: typedef bool michael@0: (* JSNewEnumerateOp)(JSContext *cx, JS::HandleObject obj, JSIterateOp enum_op, michael@0: JS::MutableHandleValue statep, JS::MutableHandleId idp); michael@0: michael@0: // The old-style JSClass.enumerate op should define all lazy properties not michael@0: // yet reflected in obj. michael@0: typedef bool michael@0: (* JSEnumerateOp)(JSContext *cx, JS::HandleObject obj); michael@0: michael@0: // Resolve a lazy property named by id in obj by defining it directly in obj. michael@0: // Lazy properties are those reflected from some peer native property space michael@0: // (e.g., the DOM attributes for a given node reflected as obj) on demand. michael@0: // michael@0: // JS looks for a property in an object, and if not found, tries to resolve michael@0: // the given id. If resolve succeeds, the engine looks again in case resolve michael@0: // defined obj[id]. If no such property exists directly in obj, the process michael@0: // is repeated with obj's prototype, etc. michael@0: // michael@0: // NB: JSNewResolveOp provides a cheaper way to resolve lazy properties. michael@0: typedef bool michael@0: (* JSResolveOp)(JSContext *cx, JS::HandleObject obj, JS::HandleId id); michael@0: michael@0: // Like JSResolveOp, except the *objp out parameter, on success, should be null michael@0: // to indicate that id was not resolved; and non-null, referring to obj or one michael@0: // of its prototypes, if id was resolved. The hook may assume *objp is null on michael@0: // entry. michael@0: // michael@0: // This hook instead of JSResolveOp is called via the JSClass.resolve member michael@0: // if JSCLASS_NEW_RESOLVE is set in JSClass.flags. michael@0: typedef bool michael@0: (* JSNewResolveOp)(JSContext *cx, JS::HandleObject obj, JS::HandleId id, michael@0: JS::MutableHandleObject objp); michael@0: michael@0: // Convert obj to the given type, returning true with the resulting value in michael@0: // *vp on success, and returning false on error or exception. michael@0: typedef bool michael@0: (* JSConvertOp)(JSContext *cx, JS::HandleObject obj, JSType type, michael@0: JS::MutableHandleValue vp); michael@0: michael@0: // Finalize obj, which the garbage collector has determined to be unreachable michael@0: // from other live objects or from GC roots. Obviously, finalizers must never michael@0: // store a reference to obj. michael@0: typedef void michael@0: (* JSFinalizeOp)(JSFreeOp *fop, JSObject *obj); michael@0: michael@0: // Finalizes external strings created by JS_NewExternalString. michael@0: struct JSStringFinalizer { michael@0: void (*finalize)(const JSStringFinalizer *fin, jschar *chars); michael@0: }; michael@0: michael@0: // Check whether v is an instance of obj. Return false on error or exception, michael@0: // true on success with true in *bp if v is an instance of obj, false in michael@0: // *bp otherwise. michael@0: typedef bool michael@0: (* JSHasInstanceOp)(JSContext *cx, JS::HandleObject obj, JS::MutableHandleValue vp, michael@0: bool *bp); michael@0: michael@0: // Function type for trace operation of the class called to enumerate all michael@0: // traceable things reachable from obj's private data structure. For each such michael@0: // thing, a trace implementation must call one of the JS_Call*Tracer variants michael@0: // on the thing. michael@0: // michael@0: // JSTraceOp implementation can assume that no other threads mutates object michael@0: // state. It must not change state of the object or corresponding native michael@0: // structures. The only exception for this rule is the case when the embedding michael@0: // needs a tight integration with GC. In that case the embedding can check if michael@0: // the traversal is a part of the marking phase through calling michael@0: // JS_IsGCMarkingTracer and apply a special code like emptying caches or michael@0: // marking its native structures. michael@0: typedef void michael@0: (* JSTraceOp)(JSTracer *trc, JSObject *obj); michael@0: michael@0: // A generic type for functions mapping an object to another object, or null michael@0: // if an error or exception was thrown on cx. michael@0: typedef JSObject * michael@0: (* JSObjectOp)(JSContext *cx, JS::HandleObject obj); michael@0: michael@0: // Hook that creates an iterator object for a given object. Returns the michael@0: // iterator object or null if an error or exception was thrown on cx. michael@0: typedef JSObject * michael@0: (* JSIteratorOp)(JSContext *cx, JS::HandleObject obj, bool keysonly); michael@0: michael@0: typedef JSObject * michael@0: (* JSWeakmapKeyDelegateOp)(JSObject *obj); michael@0: michael@0: /* js::Class operation signatures. */ michael@0: michael@0: namespace js { michael@0: michael@0: typedef bool michael@0: (* LookupGenericOp)(JSContext *cx, JS::HandleObject obj, JS::HandleId id, michael@0: JS::MutableHandleObject objp, JS::MutableHandle propp); michael@0: typedef bool michael@0: (* LookupPropOp)(JSContext *cx, JS::HandleObject obj, JS::Handle name, michael@0: JS::MutableHandleObject objp, JS::MutableHandle propp); michael@0: typedef bool michael@0: (* LookupElementOp)(JSContext *cx, JS::HandleObject obj, uint32_t index, michael@0: JS::MutableHandleObject objp, JS::MutableHandle propp); michael@0: typedef bool michael@0: (* DefineGenericOp)(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::HandleValue value, michael@0: JSPropertyOp getter, JSStrictPropertyOp setter, unsigned attrs); michael@0: typedef bool michael@0: (* DefinePropOp)(JSContext *cx, JS::HandleObject obj, JS::Handle name, michael@0: JS::HandleValue value, JSPropertyOp getter, JSStrictPropertyOp setter, michael@0: unsigned attrs); michael@0: typedef bool michael@0: (* DefineElementOp)(JSContext *cx, JS::HandleObject obj, uint32_t index, JS::HandleValue value, michael@0: JSPropertyOp getter, JSStrictPropertyOp setter, unsigned attrs); michael@0: typedef bool michael@0: (* GenericIdOp)(JSContext *cx, JS::HandleObject obj, JS::HandleObject receiver, JS::HandleId id, michael@0: JS::MutableHandleValue vp); michael@0: typedef bool michael@0: (* PropertyIdOp)(JSContext *cx, JS::HandleObject obj, JS::HandleObject receiver, michael@0: JS::Handle name, JS::MutableHandleValue vp); michael@0: typedef bool michael@0: (* ElementIdOp)(JSContext *cx, JS::HandleObject obj, JS::HandleObject receiver, uint32_t index, michael@0: JS::MutableHandleValue vp); michael@0: typedef bool michael@0: (* StrictGenericIdOp)(JSContext *cx, JS::HandleObject obj, JS::HandleId id, michael@0: JS::MutableHandleValue vp, bool strict); michael@0: typedef bool michael@0: (* StrictPropertyIdOp)(JSContext *cx, JS::HandleObject obj, JS::Handle name, michael@0: JS::MutableHandleValue vp, bool strict); michael@0: typedef bool michael@0: (* StrictElementIdOp)(JSContext *cx, JS::HandleObject obj, uint32_t index, michael@0: JS::MutableHandleValue vp, bool strict); michael@0: typedef bool michael@0: (* GenericAttributesOp)(JSContext *cx, JS::HandleObject obj, JS::HandleId id, unsigned *attrsp); michael@0: typedef bool michael@0: (* PropertyAttributesOp)(JSContext *cx, JS::HandleObject obj, JS::Handle name, michael@0: unsigned *attrsp); michael@0: typedef bool michael@0: (* DeletePropertyOp)(JSContext *cx, JS::HandleObject obj, JS::Handle name, michael@0: bool *succeeded); michael@0: typedef bool michael@0: (* DeleteElementOp)(JSContext *cx, JS::HandleObject obj, uint32_t index, bool *succeeded); michael@0: michael@0: typedef bool michael@0: (* WatchOp)(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::HandleObject callable); michael@0: michael@0: typedef bool michael@0: (* UnwatchOp)(JSContext *cx, JS::HandleObject obj, JS::HandleId id); michael@0: michael@0: typedef bool michael@0: (* SliceOp)(JSContext *cx, JS::HandleObject obj, uint32_t begin, uint32_t end, michael@0: JS::HandleObject result); // result is actually preallocted. michael@0: michael@0: typedef JSObject * michael@0: (* ObjectOp)(JSContext *cx, JS::HandleObject obj); michael@0: typedef void michael@0: (* FinalizeOp)(FreeOp *fop, JSObject *obj); michael@0: michael@0: #define JS_CLASS_MEMBERS(FinalizeOpType) \ michael@0: const char *name; \ michael@0: uint32_t flags; \ michael@0: \ michael@0: /* Mandatory function pointer members. */ \ michael@0: JSPropertyOp addProperty; \ michael@0: JSDeletePropertyOp delProperty; \ michael@0: JSPropertyOp getProperty; \ michael@0: JSStrictPropertyOp setProperty; \ michael@0: JSEnumerateOp enumerate; \ michael@0: JSResolveOp resolve; \ michael@0: JSConvertOp convert; \ michael@0: \ michael@0: /* Optional members (may be null). */ \ michael@0: FinalizeOpType finalize; \ michael@0: JSNative call; \ michael@0: JSHasInstanceOp hasInstance; \ michael@0: JSNative construct; \ michael@0: JSTraceOp trace michael@0: michael@0: // Callback for the creation of constructor and prototype objects. michael@0: typedef JSObject *(*ClassObjectCreationOp)(JSContext *cx, JSProtoKey key); michael@0: michael@0: // Callback for custom post-processing after class initialization via ClassSpec. michael@0: typedef bool (*FinishClassInitOp)(JSContext *cx, JS::HandleObject ctor, michael@0: JS::HandleObject proto); michael@0: michael@0: struct ClassSpec michael@0: { michael@0: ClassObjectCreationOp createConstructor; michael@0: ClassObjectCreationOp createPrototype; michael@0: const JSFunctionSpec *constructorFunctions; michael@0: const JSFunctionSpec *prototypeFunctions; michael@0: FinishClassInitOp finishInit; michael@0: bool defined() const { return !!createConstructor; } michael@0: }; michael@0: michael@0: struct ClassExtension michael@0: { michael@0: JSObjectOp outerObject; michael@0: JSObjectOp innerObject; michael@0: JSIteratorOp iteratorObject; michael@0: michael@0: /* michael@0: * isWrappedNative is true only if the class is an XPCWrappedNative. michael@0: * WeakMaps use this to override the wrapper disposal optimization. michael@0: */ michael@0: bool isWrappedNative; michael@0: michael@0: /* michael@0: * If an object is used as a key in a weakmap, it may be desirable for the michael@0: * garbage collector to keep that object around longer than it otherwise michael@0: * would. A common case is when the key is a wrapper around an object in michael@0: * another compartment, and we want to avoid collecting the wrapper (and michael@0: * removing the weakmap entry) as long as the wrapped object is alive. In michael@0: * that case, the wrapped object is returned by the wrapper's michael@0: * weakmapKeyDelegateOp hook. As long as the wrapper is used as a weakmap michael@0: * key, it will not be collected (and remain in the weakmap) until the michael@0: * wrapped object is collected. michael@0: */ michael@0: JSWeakmapKeyDelegateOp weakmapKeyDelegateOp; michael@0: }; michael@0: michael@0: #define JS_NULL_CLASS_SPEC {nullptr,nullptr,nullptr,nullptr,nullptr} michael@0: #define JS_NULL_CLASS_EXT {nullptr,nullptr,nullptr,false,nullptr} michael@0: michael@0: struct ObjectOps michael@0: { michael@0: LookupGenericOp lookupGeneric; michael@0: LookupPropOp lookupProperty; michael@0: LookupElementOp lookupElement; michael@0: DefineGenericOp defineGeneric; michael@0: DefinePropOp defineProperty; michael@0: DefineElementOp defineElement; michael@0: GenericIdOp getGeneric; michael@0: PropertyIdOp getProperty; michael@0: ElementIdOp getElement; michael@0: StrictGenericIdOp setGeneric; michael@0: StrictPropertyIdOp setProperty; michael@0: StrictElementIdOp setElement; michael@0: GenericAttributesOp getGenericAttributes; michael@0: GenericAttributesOp setGenericAttributes; michael@0: DeletePropertyOp deleteProperty; michael@0: DeleteElementOp deleteElement; michael@0: WatchOp watch; michael@0: UnwatchOp unwatch; michael@0: SliceOp slice; // Optimized slice, can be null. michael@0: michael@0: JSNewEnumerateOp enumerate; michael@0: ObjectOp thisObject; michael@0: }; michael@0: michael@0: #define JS_NULL_OBJECT_OPS \ michael@0: {nullptr,nullptr,nullptr,nullptr,nullptr,nullptr,nullptr,nullptr,nullptr, \ michael@0: nullptr,nullptr,nullptr,nullptr,nullptr,nullptr,nullptr,nullptr,nullptr, \ michael@0: nullptr,nullptr} michael@0: michael@0: } // namespace js michael@0: michael@0: // Classes, objects, and properties. michael@0: michael@0: typedef void (*JSClassInternal)(); michael@0: michael@0: struct JSClass { michael@0: JS_CLASS_MEMBERS(JSFinalizeOp); michael@0: michael@0: void *reserved[31]; michael@0: }; michael@0: michael@0: #define JSCLASS_HAS_PRIVATE (1<<0) // objects have private slot michael@0: #define JSCLASS_NEW_ENUMERATE (1<<1) // has JSNewEnumerateOp hook michael@0: #define JSCLASS_NEW_RESOLVE (1<<2) // has JSNewResolveOp hook michael@0: #define JSCLASS_PRIVATE_IS_NSISUPPORTS (1<<3) // private is (nsISupports *) michael@0: #define JSCLASS_IS_DOMJSCLASS (1<<4) // objects are DOM michael@0: #define JSCLASS_IMPLEMENTS_BARRIERS (1<<5) // Correctly implements GC read michael@0: // and write barriers michael@0: #define JSCLASS_EMULATES_UNDEFINED (1<<6) // objects of this class act michael@0: // like the value undefined, michael@0: // in some contexts michael@0: #define JSCLASS_USERBIT1 (1<<7) // Reserved for embeddings. michael@0: michael@0: // To reserve slots fetched and stored via JS_Get/SetReservedSlot, bitwise-or michael@0: // JSCLASS_HAS_RESERVED_SLOTS(n) into the initializer for JSClass.flags, where michael@0: // n is a constant in [1, 255]. Reserved slots are indexed from 0 to n-1. michael@0: #define JSCLASS_RESERVED_SLOTS_SHIFT 8 // room for 8 flags below */ michael@0: #define JSCLASS_RESERVED_SLOTS_WIDTH 8 // and 16 above this field */ michael@0: #define JSCLASS_RESERVED_SLOTS_MASK JS_BITMASK(JSCLASS_RESERVED_SLOTS_WIDTH) michael@0: #define JSCLASS_HAS_RESERVED_SLOTS(n) (((n) & JSCLASS_RESERVED_SLOTS_MASK) \ michael@0: << JSCLASS_RESERVED_SLOTS_SHIFT) michael@0: #define JSCLASS_RESERVED_SLOTS(clasp) (((clasp)->flags \ michael@0: >> JSCLASS_RESERVED_SLOTS_SHIFT) \ michael@0: & JSCLASS_RESERVED_SLOTS_MASK) michael@0: michael@0: #define JSCLASS_HIGH_FLAGS_SHIFT (JSCLASS_RESERVED_SLOTS_SHIFT + \ michael@0: JSCLASS_RESERVED_SLOTS_WIDTH) michael@0: michael@0: #define JSCLASS_IS_ANONYMOUS (1<<(JSCLASS_HIGH_FLAGS_SHIFT+0)) michael@0: #define JSCLASS_IS_GLOBAL (1<<(JSCLASS_HIGH_FLAGS_SHIFT+1)) michael@0: #define JSCLASS_INTERNAL_FLAG2 (1<<(JSCLASS_HIGH_FLAGS_SHIFT+2)) michael@0: #define JSCLASS_INTERNAL_FLAG3 (1<<(JSCLASS_HIGH_FLAGS_SHIFT+3)) michael@0: michael@0: #define JSCLASS_IS_PROXY (1<<(JSCLASS_HIGH_FLAGS_SHIFT+4)) michael@0: michael@0: // Bit 22 unused. michael@0: michael@0: // Reserved for embeddings. michael@0: #define JSCLASS_USERBIT2 (1<<(JSCLASS_HIGH_FLAGS_SHIFT+6)) michael@0: #define JSCLASS_USERBIT3 (1<<(JSCLASS_HIGH_FLAGS_SHIFT+7)) michael@0: michael@0: #define JSCLASS_BACKGROUND_FINALIZE (1<<(JSCLASS_HIGH_FLAGS_SHIFT+8)) michael@0: michael@0: // Bits 26 through 31 are reserved for the CACHED_PROTO_KEY mechanism, see michael@0: // below. michael@0: michael@0: // ECMA-262 requires that most constructors used internally create objects michael@0: // with "the original Foo.prototype value" as their [[Prototype]] (__proto__) michael@0: // member initial value. The "original ... value" verbiage is there because michael@0: // in ECMA-262, global properties naming class objects are read/write and michael@0: // deleteable, for the most part. michael@0: // michael@0: // Implementing this efficiently requires that global objects have classes michael@0: // with the following flags. Failure to use JSCLASS_GLOBAL_FLAGS was michael@0: // previously allowed, but is now an ES5 violation and thus unsupported. michael@0: // michael@0: #define JSCLASS_GLOBAL_SLOT_COUNT (3 + JSProto_LIMIT * 3 + 31) michael@0: #define JSCLASS_GLOBAL_FLAGS_WITH_SLOTS(n) \ michael@0: (JSCLASS_IS_GLOBAL | JSCLASS_HAS_RESERVED_SLOTS(JSCLASS_GLOBAL_SLOT_COUNT + (n))) michael@0: #define JSCLASS_GLOBAL_FLAGS \ michael@0: JSCLASS_GLOBAL_FLAGS_WITH_SLOTS(0) michael@0: #define JSCLASS_HAS_GLOBAL_FLAG_AND_SLOTS(clasp) \ michael@0: (((clasp)->flags & JSCLASS_IS_GLOBAL) \ michael@0: && JSCLASS_RESERVED_SLOTS(clasp) >= JSCLASS_GLOBAL_SLOT_COUNT) michael@0: michael@0: // Fast access to the original value of each standard class's prototype. michael@0: #define JSCLASS_CACHED_PROTO_SHIFT (JSCLASS_HIGH_FLAGS_SHIFT + 10) michael@0: #define JSCLASS_CACHED_PROTO_WIDTH 6 michael@0: #define JSCLASS_CACHED_PROTO_MASK JS_BITMASK(JSCLASS_CACHED_PROTO_WIDTH) michael@0: #define JSCLASS_HAS_CACHED_PROTO(key) (uint32_t(key) << JSCLASS_CACHED_PROTO_SHIFT) michael@0: #define JSCLASS_CACHED_PROTO_KEY(clasp) ((JSProtoKey) \ michael@0: (((clasp)->flags \ michael@0: >> JSCLASS_CACHED_PROTO_SHIFT) \ michael@0: & JSCLASS_CACHED_PROTO_MASK)) michael@0: michael@0: // Initializer for unused members of statically initialized JSClass structs. michael@0: #define JSCLASS_NO_INTERNAL_MEMBERS {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} michael@0: #define JSCLASS_NO_OPTIONAL_MEMBERS 0,0,0,0,0,JSCLASS_NO_INTERNAL_MEMBERS michael@0: michael@0: namespace js { michael@0: michael@0: struct Class michael@0: { michael@0: JS_CLASS_MEMBERS(FinalizeOp); michael@0: ClassSpec spec; michael@0: ClassExtension ext; michael@0: ObjectOps ops; michael@0: michael@0: /* Class is not native and its map is not a scope. */ michael@0: static const uint32_t NON_NATIVE = JSCLASS_INTERNAL_FLAG2; michael@0: michael@0: bool isNative() const { michael@0: return !(flags & NON_NATIVE); michael@0: } michael@0: michael@0: bool hasPrivate() const { michael@0: return !!(flags & JSCLASS_HAS_PRIVATE); michael@0: } michael@0: michael@0: bool emulatesUndefined() const { michael@0: return flags & JSCLASS_EMULATES_UNDEFINED; michael@0: } michael@0: michael@0: bool isJSFunction() const { michael@0: return this == js::FunctionClassPtr; michael@0: } michael@0: michael@0: bool isCallable() const { michael@0: return isJSFunction() || call; michael@0: } michael@0: michael@0: bool isProxy() const { michael@0: return flags & JSCLASS_IS_PROXY; michael@0: } michael@0: michael@0: bool isDOMClass() const { michael@0: return flags & JSCLASS_IS_DOMJSCLASS; michael@0: } michael@0: michael@0: static size_t offsetOfFlags() { return offsetof(Class, flags); } michael@0: }; michael@0: michael@0: JS_STATIC_ASSERT(offsetof(JSClass, name) == offsetof(Class, name)); michael@0: JS_STATIC_ASSERT(offsetof(JSClass, flags) == offsetof(Class, flags)); michael@0: JS_STATIC_ASSERT(offsetof(JSClass, addProperty) == offsetof(Class, addProperty)); michael@0: JS_STATIC_ASSERT(offsetof(JSClass, delProperty) == offsetof(Class, delProperty)); michael@0: JS_STATIC_ASSERT(offsetof(JSClass, getProperty) == offsetof(Class, getProperty)); michael@0: JS_STATIC_ASSERT(offsetof(JSClass, setProperty) == offsetof(Class, setProperty)); michael@0: JS_STATIC_ASSERT(offsetof(JSClass, enumerate) == offsetof(Class, enumerate)); michael@0: JS_STATIC_ASSERT(offsetof(JSClass, resolve) == offsetof(Class, resolve)); michael@0: JS_STATIC_ASSERT(offsetof(JSClass, convert) == offsetof(Class, convert)); michael@0: JS_STATIC_ASSERT(offsetof(JSClass, finalize) == offsetof(Class, finalize)); michael@0: JS_STATIC_ASSERT(offsetof(JSClass, call) == offsetof(Class, call)); michael@0: JS_STATIC_ASSERT(offsetof(JSClass, construct) == offsetof(Class, construct)); michael@0: JS_STATIC_ASSERT(offsetof(JSClass, hasInstance) == offsetof(Class, hasInstance)); michael@0: JS_STATIC_ASSERT(offsetof(JSClass, trace) == offsetof(Class, trace)); michael@0: JS_STATIC_ASSERT(sizeof(JSClass) == sizeof(Class)); michael@0: michael@0: static MOZ_ALWAYS_INLINE const JSClass * michael@0: Jsvalify(const Class *c) michael@0: { michael@0: return (const JSClass *)c; michael@0: } michael@0: michael@0: static MOZ_ALWAYS_INLINE const Class * michael@0: Valueify(const JSClass *c) michael@0: { michael@0: return (const Class *)c; michael@0: } michael@0: michael@0: /* michael@0: * Enumeration describing possible values of the [[Class]] internal property michael@0: * value of objects. michael@0: */ michael@0: enum ESClassValue { michael@0: ESClass_Array, ESClass_Number, ESClass_String, ESClass_Boolean, michael@0: ESClass_RegExp, ESClass_ArrayBuffer, ESClass_Date michael@0: }; michael@0: michael@0: /* michael@0: * Return whether the given object has the given [[Class]] internal property michael@0: * value. Beware, this query says nothing about the js::Class of the JSObject michael@0: * so the caller must not assume anything about obj's representation (e.g., obj michael@0: * may be a proxy). michael@0: */ michael@0: inline bool michael@0: ObjectClassIs(JSObject &obj, ESClassValue classValue, JSContext *cx); michael@0: michael@0: /* Just a helper that checks v.isObject before calling ObjectClassIs. */ michael@0: inline bool michael@0: IsObjectWithClass(const JS::Value &v, ESClassValue classValue, JSContext *cx); michael@0: michael@0: } /* namespace js */ michael@0: michael@0: #endif /* js_Class_h */