1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: set ts=8 sts=2 et sw=2 tw=80: 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 #ifndef vm_StringObject_h 8 #define vm_StringObject_h 9 10 #include "vm/JSObject.h" 11 #include "vm/Shape.h" 12 13 namespace js { 14 15 class StringObject : public NativeObject { 16 static const unsigned PRIMITIVE_VALUE_SLOT = 0; 17 static const unsigned LENGTH_SLOT = 1; 18 19 static const ClassSpec classSpec_; 20 21 public: 22 static const unsigned RESERVED_SLOTS = 2; 23 24 static const JSClass class_; 25 26 /* 27 * Creates a new String object boxing the given string. The object's 28 * [[Prototype]] is determined from context. 29 */ 30 static inline StringObject* create(JSContext* cx, HandleString str, 31 HandleObject proto = nullptr, 32 NewObjectKind newKind = GenericObject); 33 34 /* 35 * Compute the initial shape to associate with fresh String objects, which 36 * encodes the initial length property. Return the shape after changing 37 * |obj|'s last property to it. 38 */ 39 static Shape* assignInitialShape(JSContext* cx, Handle<StringObject*> obj); 40 unbox()41 JSString* unbox() const { 42 return getFixedSlot(PRIMITIVE_VALUE_SLOT).toString(); 43 } 44 length()45 inline size_t length() const { 46 return size_t(getFixedSlot(LENGTH_SLOT).toInt32()); 47 } 48 offsetOfPrimitiveValue()49 static size_t offsetOfPrimitiveValue() { 50 return getFixedSlotOffset(PRIMITIVE_VALUE_SLOT); 51 } offsetOfLength()52 static size_t offsetOfLength() { return getFixedSlotOffset(LENGTH_SLOT); } 53 54 private: 55 static inline bool init(JSContext* cx, Handle<StringObject*> obj, 56 HandleString str); 57 58 static JSObject* createPrototype(JSContext* cx, JSProtoKey key); 59 setStringThis(JSString * str)60 void setStringThis(JSString* str) { 61 MOZ_ASSERT(getReservedSlot(PRIMITIVE_VALUE_SLOT).isUndefined()); 62 setFixedSlot(PRIMITIVE_VALUE_SLOT, StringValue(str)); 63 setFixedSlot(LENGTH_SLOT, Int32Value(int32_t(str->length()))); 64 } 65 }; 66 67 } // namespace js 68 69 #endif /* vm_StringObject_h */ 70