1/*
2 * Copyright (C) 2006 Maks Orlovich
3 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#pragma once
23
24#include "JSObject.h"
25
26namespace JSC {
27
28// This class is used as a base for classes such as String,
29// Number, Boolean and Symbol which are wrappers for primitive types.
30class JSWrapperObject : public JSNonFinalObject {
31public:
32 using Base = JSNonFinalObject;
33
34 static size_t allocationSize(Checked<size_t> inlineCapacity)
35 {
36 ASSERT_UNUSED(inlineCapacity, !inlineCapacity);
37 return sizeof(JSWrapperObject);
38 }
39
40 JSValue internalValue() const;
41 void setInternalValue(VM&, JSValue);
42
43 static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
44 {
45 return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
46 }
47
48 static ptrdiff_t internalValueOffset() { return OBJECT_OFFSETOF(JSWrapperObject, m_internalValue); }
49 static ptrdiff_t internalValueCellOffset()
50 {
51#if USE(JSVALUE64)
52 return internalValueOffset();
53#else
54 return internalValueOffset() + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload);
55#endif
56 }
57
58protected:
59 explicit JSWrapperObject(VM&, Structure*);
60
61 JS_EXPORT_PRIVATE static void visitChildren(JSCell*, SlotVisitor&);
62
63private:
64 WriteBarrier<Unknown> m_internalValue;
65};
66
67inline JSWrapperObject::JSWrapperObject(VM& vm, Structure* structure)
68 : Base(vm, structure)
69{
70}
71
72inline JSValue JSWrapperObject::internalValue() const
73{
74 return m_internalValue.get();
75}
76
77inline void JSWrapperObject::setInternalValue(VM& vm, JSValue value)
78{
79 ASSERT(value);
80 ASSERT(!value.isObject());
81 m_internalValue.set(vm, this, value);
82}
83
84} // namespace JSC
85