Line data Source code
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 NativeObject
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 : HandleObject proto = nullptr,
33 : NewObjectKind newKind = GenericObject);
34 :
35 : /*
36 : * Compute the initial shape to associate with fresh String objects, which
37 : * encodes the initial length property. Return the shape after changing
38 : * |obj|'s last property to it.
39 : */
40 : static Shape*
41 : assignInitialShape(JSContext* cx, Handle<StringObject*> obj);
42 :
43 2 : JSString* unbox() const {
44 2 : return getFixedSlot(PRIMITIVE_VALUE_SLOT).toString();
45 : }
46 :
47 : inline size_t length() const {
48 : return size_t(getFixedSlot(LENGTH_SLOT).toInt32());
49 : }
50 :
51 0 : static size_t offsetOfPrimitiveValue() {
52 0 : return getFixedSlotOffset(PRIMITIVE_VALUE_SLOT);
53 : }
54 0 : static size_t offsetOfLength() {
55 0 : return getFixedSlotOffset(LENGTH_SLOT);
56 : }
57 :
58 : private:
59 : static inline bool init(JSContext* cx, Handle<StringObject*> obj, HandleString str);
60 :
61 49 : void setStringThis(JSString* str) {
62 49 : MOZ_ASSERT(getReservedSlot(PRIMITIVE_VALUE_SLOT).isUndefined());
63 49 : setFixedSlot(PRIMITIVE_VALUE_SLOT, StringValue(str));
64 49 : setFixedSlot(LENGTH_SLOT, Int32Value(int32_t(str->length())));
65 49 : }
66 :
67 : /* For access to init, as String.prototype is special. */
68 : friend JSObject*
69 : js::InitStringClass(JSContext* cx, HandleObject global);
70 : };
71 :
72 : } // namespace js
73 :
74 : #endif /* vm_StringObject_h */
|