Backed out changeset 8f976ed899d7 (bug 1847231) for causing bc failures on browser_se...
[gecko.git] / js / src / vm / StringObject.h
blobad90e649f05f27366a18d21cc3575fab220d7353
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/. */
7 #ifndef vm_StringObject_h
8 #define vm_StringObject_h
10 #include "vm/JSObject.h"
11 #include "vm/NativeObject.h"
13 namespace js {
15 class Shape;
17 class StringObject : public NativeObject {
18 static const unsigned PRIMITIVE_VALUE_SLOT = 0;
19 static const unsigned LENGTH_SLOT = 1;
21 static const ClassSpec classSpec_;
23 public:
24 static const unsigned RESERVED_SLOTS = 2;
26 static const JSClass class_;
29 * Creates a new String object boxing the given string. The object's
30 * [[Prototype]] is determined from context.
32 static inline StringObject* create(JSContext* cx, HandleString str,
33 HandleObject proto = nullptr,
34 NewObjectKind newKind = GenericObject);
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.
41 static SharedShape* assignInitialShape(JSContext* cx,
42 Handle<StringObject*> obj);
44 JSString* unbox() const {
45 return getFixedSlot(PRIMITIVE_VALUE_SLOT).toString();
48 inline size_t length() const {
49 return size_t(getFixedSlot(LENGTH_SLOT).toInt32());
52 static size_t offsetOfPrimitiveValue() {
53 return getFixedSlotOffset(PRIMITIVE_VALUE_SLOT);
55 static size_t offsetOfLength() { return getFixedSlotOffset(LENGTH_SLOT); }
57 private:
58 static inline bool init(JSContext* cx, Handle<StringObject*> obj,
59 HandleString str);
61 static JSObject* createPrototype(JSContext* cx, JSProtoKey key);
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())));
70 } // namespace js
72 #endif /* vm_StringObject_h */