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