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: #ifndef vm_StringObject_h michael@0: #define vm_StringObject_h michael@0: michael@0: #include "jsobj.h" michael@0: #include "jsstr.h" michael@0: michael@0: #include "vm/Shape.h" michael@0: michael@0: namespace js { michael@0: michael@0: class StringObject : public JSObject michael@0: { michael@0: static const unsigned PRIMITIVE_VALUE_SLOT = 0; michael@0: static const unsigned LENGTH_SLOT = 1; michael@0: michael@0: public: michael@0: static const unsigned RESERVED_SLOTS = 2; michael@0: michael@0: static const Class class_; michael@0: michael@0: /* michael@0: * Creates a new String object boxing the given string. The object's michael@0: * [[Prototype]] is determined from context. michael@0: */ michael@0: static inline StringObject *create(JSContext *cx, HandleString str, michael@0: NewObjectKind newKind = GenericObject); michael@0: michael@0: JSString *unbox() const { michael@0: return getFixedSlot(PRIMITIVE_VALUE_SLOT).toString(); michael@0: } michael@0: michael@0: inline size_t length() const { michael@0: return size_t(getFixedSlot(LENGTH_SLOT).toInt32()); michael@0: } michael@0: michael@0: static size_t offsetOfPrimitiveValue() { michael@0: return getFixedSlotOffset(PRIMITIVE_VALUE_SLOT); michael@0: } michael@0: static size_t offsetOfLength() { michael@0: return getFixedSlotOffset(LENGTH_SLOT); michael@0: } michael@0: michael@0: private: michael@0: inline bool init(JSContext *cx, HandleString str); michael@0: michael@0: void setStringThis(JSString *str) { michael@0: JS_ASSERT(getReservedSlot(PRIMITIVE_VALUE_SLOT).isUndefined()); michael@0: setFixedSlot(PRIMITIVE_VALUE_SLOT, StringValue(str)); michael@0: setFixedSlot(LENGTH_SLOT, Int32Value(int32_t(str->length()))); michael@0: } michael@0: michael@0: /* For access to init, as String.prototype is special. */ michael@0: friend JSObject * michael@0: ::js_InitStringClass(JSContext *cx, js::HandleObject global); michael@0: michael@0: /* For access to assignInitialShape. */ michael@0: friend bool michael@0: EmptyShape::ensureInitialCustomShape(ExclusiveContext *cx, michael@0: Handle obj); michael@0: michael@0: /* michael@0: * Compute the initial shape to associate with fresh String objects, which michael@0: * encodes the initial length property. Return the shape after changing michael@0: * |obj|'s last property to it. michael@0: */ michael@0: static Shape * michael@0: assignInitialShape(ExclusiveContext *cx, Handle obj); michael@0: }; michael@0: michael@0: } // namespace js michael@0: michael@0: #endif /* vm_StringObject_h */