1 | // -*- mode: c++; c-basic-offset: 4 -*-
|
---|
2 | /*
|
---|
3 | * Copyright (C) 2008 Apple Inc. All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | * 1. Redistributions of source code must retain the above copyright
|
---|
9 | * notice, this list of conditions and the following disclaimer.
|
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer in the
|
---|
12 | * documentation and/or other materials provided with the distribution.
|
---|
13 | *
|
---|
14 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
|
---|
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
|
---|
18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
---|
22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
25 | */
|
---|
26 |
|
---|
27 | #include "config.h"
|
---|
28 | #include "StructureID.h"
|
---|
29 |
|
---|
30 | #include "identifier.h"
|
---|
31 | #include "JSObject.h"
|
---|
32 | #include <wtf/RefPtr.h>
|
---|
33 |
|
---|
34 | namespace JSC {
|
---|
35 |
|
---|
36 | StructureID::StructureID(JSValue* prototype)
|
---|
37 | : m_isDictionary(false)
|
---|
38 | , m_prototype(prototype)
|
---|
39 | , m_cachedPrototypeChain(0)
|
---|
40 | , m_previous(0)
|
---|
41 | , m_nameInPrevious(0)
|
---|
42 | , m_transitionCount(0)
|
---|
43 | {
|
---|
44 | ASSERT(m_prototype);
|
---|
45 | ASSERT(m_prototype->isObject() || m_prototype->isNull());
|
---|
46 | }
|
---|
47 |
|
---|
48 | PassRefPtr<StructureID> StructureID::addPropertyTransition(StructureID* structureID, const Identifier& name)
|
---|
49 | {
|
---|
50 | ASSERT(!structureID->m_isDictionary);
|
---|
51 |
|
---|
52 | if (StructureID* existingTransition = structureID->m_transitionTable.get(name.ustring().rep()))
|
---|
53 | return existingTransition;
|
---|
54 |
|
---|
55 | if (structureID->m_transitionCount > s_maxTransitionLength)
|
---|
56 | return toDictionaryTransition(structureID);
|
---|
57 |
|
---|
58 | RefPtr<StructureID> transition = create(structureID->m_prototype);
|
---|
59 | transition->m_cachedPrototypeChain = structureID->m_cachedPrototypeChain;
|
---|
60 | transition->m_previous = structureID;
|
---|
61 | transition->m_nameInPrevious = name.ustring().rep();
|
---|
62 | transition->m_transitionCount = structureID->m_transitionCount + 1;
|
---|
63 |
|
---|
64 | structureID->m_transitionTable.add(name.ustring().rep(), transition.get());
|
---|
65 | return transition.release();
|
---|
66 | }
|
---|
67 |
|
---|
68 | PassRefPtr<StructureID> StructureID::toDictionaryTransition(StructureID* structureID)
|
---|
69 | {
|
---|
70 | ASSERT(!structureID->m_isDictionary);
|
---|
71 |
|
---|
72 | RefPtr<StructureID> transition = create(structureID->m_prototype);
|
---|
73 | transition->m_isDictionary = true;
|
---|
74 | return transition.release();
|
---|
75 | }
|
---|
76 |
|
---|
77 | PassRefPtr<StructureID> StructureID::fromDictionaryTransition(StructureID* structureID)
|
---|
78 | {
|
---|
79 | ASSERT(structureID->m_isDictionary);
|
---|
80 |
|
---|
81 | // Since dictionary StructureIDs are not shared, and no opcodes specialize
|
---|
82 | // for them, we don't need to allocate a new StructureID when transitioning
|
---|
83 | // to non-dictionary status.
|
---|
84 | structureID->m_isDictionary = false;
|
---|
85 | return structureID;
|
---|
86 | }
|
---|
87 |
|
---|
88 | PassRefPtr<StructureID> StructureID::changePrototypeTransition(StructureID* structureID, JSValue* prototype)
|
---|
89 | {
|
---|
90 | RefPtr<StructureID> transition = create(prototype);
|
---|
91 | transition->m_transitionCount = structureID->m_transitionCount + 1;
|
---|
92 | return transition.release();
|
---|
93 | }
|
---|
94 |
|
---|
95 | PassRefPtr<StructureID> StructureID::getterSetterTransition(StructureID* structureID)
|
---|
96 | {
|
---|
97 | RefPtr<StructureID> transition = create(structureID->prototype());
|
---|
98 | transition->m_transitionCount = structureID->m_transitionCount + 1;
|
---|
99 | return transition.release();
|
---|
100 | }
|
---|
101 |
|
---|
102 | StructureID::~StructureID()
|
---|
103 | {
|
---|
104 | if (m_previous) {
|
---|
105 | ASSERT(m_previous->m_transitionTable.contains(m_nameInPrevious));
|
---|
106 | m_previous->m_transitionTable.remove(m_nameInPrevious);
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | StructureIDChain::StructureIDChain(StructureID* structureID)
|
---|
111 | {
|
---|
112 | size_t size = 1;
|
---|
113 |
|
---|
114 | StructureID* tmp = structureID;
|
---|
115 | while (!tmp->prototype()->isNull()) {
|
---|
116 | ++size;
|
---|
117 | tmp = static_cast<JSCell*>(tmp->prototype())->structureID();
|
---|
118 | }
|
---|
119 |
|
---|
120 | m_vector.set(new RefPtr<StructureID>[size]);
|
---|
121 |
|
---|
122 | size_t i;
|
---|
123 | for (i = 0; i < size - 1; ++i) {
|
---|
124 | m_vector[i] = structureID;
|
---|
125 | structureID = static_cast<JSObject*>(structureID->prototype())->structureID();
|
---|
126 | }
|
---|
127 | m_vector[i] = structureID;
|
---|
128 | }
|
---|
129 |
|
---|
130 | } // namespace JSC
|
---|