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