1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/vm/StringObject.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,78 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef vm_StringObject_h 1.11 +#define vm_StringObject_h 1.12 + 1.13 +#include "jsobj.h" 1.14 +#include "jsstr.h" 1.15 + 1.16 +#include "vm/Shape.h" 1.17 + 1.18 +namespace js { 1.19 + 1.20 +class StringObject : public JSObject 1.21 +{ 1.22 + static const unsigned PRIMITIVE_VALUE_SLOT = 0; 1.23 + static const unsigned LENGTH_SLOT = 1; 1.24 + 1.25 + public: 1.26 + static const unsigned RESERVED_SLOTS = 2; 1.27 + 1.28 + static const Class class_; 1.29 + 1.30 + /* 1.31 + * Creates a new String object boxing the given string. The object's 1.32 + * [[Prototype]] is determined from context. 1.33 + */ 1.34 + static inline StringObject *create(JSContext *cx, HandleString str, 1.35 + NewObjectKind newKind = GenericObject); 1.36 + 1.37 + JSString *unbox() const { 1.38 + return getFixedSlot(PRIMITIVE_VALUE_SLOT).toString(); 1.39 + } 1.40 + 1.41 + inline size_t length() const { 1.42 + return size_t(getFixedSlot(LENGTH_SLOT).toInt32()); 1.43 + } 1.44 + 1.45 + static size_t offsetOfPrimitiveValue() { 1.46 + return getFixedSlotOffset(PRIMITIVE_VALUE_SLOT); 1.47 + } 1.48 + static size_t offsetOfLength() { 1.49 + return getFixedSlotOffset(LENGTH_SLOT); 1.50 + } 1.51 + 1.52 + private: 1.53 + inline bool init(JSContext *cx, HandleString str); 1.54 + 1.55 + void setStringThis(JSString *str) { 1.56 + JS_ASSERT(getReservedSlot(PRIMITIVE_VALUE_SLOT).isUndefined()); 1.57 + setFixedSlot(PRIMITIVE_VALUE_SLOT, StringValue(str)); 1.58 + setFixedSlot(LENGTH_SLOT, Int32Value(int32_t(str->length()))); 1.59 + } 1.60 + 1.61 + /* For access to init, as String.prototype is special. */ 1.62 + friend JSObject * 1.63 + ::js_InitStringClass(JSContext *cx, js::HandleObject global); 1.64 + 1.65 + /* For access to assignInitialShape. */ 1.66 + friend bool 1.67 + EmptyShape::ensureInitialCustomShape<StringObject>(ExclusiveContext *cx, 1.68 + Handle<StringObject*> obj); 1.69 + 1.70 + /* 1.71 + * Compute the initial shape to associate with fresh String objects, which 1.72 + * encodes the initial length property. Return the shape after changing 1.73 + * |obj|'s last property to it. 1.74 + */ 1.75 + static Shape * 1.76 + assignInitialShape(ExclusiveContext *cx, Handle<StringObject*> obj); 1.77 +}; 1.78 + 1.79 +} // namespace js 1.80 + 1.81 +#endif /* vm_StringObject_h */