Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / v8 / src / interface.cc
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "v8.h"
6
7 #include "interface.h"
8
9 namespace v8 {
10 namespace internal {
11
12 static bool Match(void* key1, void* key2) {
13   String* name1 = *static_cast<String**>(key1);
14   String* name2 = *static_cast<String**>(key2);
15   ASSERT(name1->IsInternalizedString());
16   ASSERT(name2->IsInternalizedString());
17   return name1 == name2;
18 }
19
20
21 Interface* Interface::Lookup(Handle<String> name, Zone* zone) {
22   ASSERT(IsModule());
23   ZoneHashMap* map = Chase()->exports_;
24   if (map == NULL) return NULL;
25   ZoneAllocationPolicy allocator(zone);
26   ZoneHashMap::Entry* p = map->Lookup(name.location(), name->Hash(), false,
27                                       allocator);
28   if (p == NULL) return NULL;
29   ASSERT(*static_cast<String**>(p->key) == *name);
30   ASSERT(p->value != NULL);
31   return static_cast<Interface*>(p->value);
32 }
33
34
35 #ifdef DEBUG
36 // Current nesting depth for debug output.
37 class Nesting {
38  public:
39   Nesting()  { current_ += 2; }
40   ~Nesting() { current_ -= 2; }
41   static int current() { return current_; }
42  private:
43   static int current_;
44 };
45
46 int Nesting::current_ = 0;
47 #endif
48
49
50 void Interface::DoAdd(
51     void* name, uint32_t hash, Interface* interface, Zone* zone, bool* ok) {
52   MakeModule(ok);
53   if (!*ok) return;
54
55 #ifdef DEBUG
56   if (FLAG_print_interface_details) {
57     PrintF("%*s# Adding...\n", Nesting::current(), "");
58     PrintF("%*sthis = ", Nesting::current(), "");
59     this->Print(Nesting::current());
60     PrintF("%*s%s : ", Nesting::current(), "",
61            (*static_cast<String**>(name))->ToAsciiArray());
62     interface->Print(Nesting::current());
63   }
64 #endif
65
66   ZoneHashMap** map = &Chase()->exports_;
67   ZoneAllocationPolicy allocator(zone);
68
69   if (*map == NULL) {
70     *map = new(zone->New(sizeof(ZoneHashMap)))
71         ZoneHashMap(Match, ZoneHashMap::kDefaultHashMapCapacity, allocator);
72   }
73
74   ZoneHashMap::Entry* p = (*map)->Lookup(name, hash, !IsFrozen(), allocator);
75   if (p == NULL) {
76     // This didn't have name but was frozen already, that's an error.
77     *ok = false;
78   } else if (p->value == NULL) {
79     p->value = interface;
80   } else {
81 #ifdef DEBUG
82     Nesting nested;
83 #endif
84     static_cast<Interface*>(p->value)->Unify(interface, zone, ok);
85   }
86
87 #ifdef DEBUG
88   if (FLAG_print_interface_details) {
89     PrintF("%*sthis' = ", Nesting::current(), "");
90     this->Print(Nesting::current());
91     PrintF("%*s# Added.\n", Nesting::current(), "");
92   }
93 #endif
94 }
95
96
97 void Interface::Unify(Interface* that, Zone* zone, bool* ok) {
98   if (this->forward_) return this->Chase()->Unify(that, zone, ok);
99   if (that->forward_) return this->Unify(that->Chase(), zone, ok);
100   ASSERT(this->forward_ == NULL);
101   ASSERT(that->forward_ == NULL);
102
103   *ok = true;
104   if (this == that) return;
105   if (this->IsValue()) {
106     that->MakeValue(ok);
107     if (*ok && this->IsConst()) that->MakeConst(ok);
108     return;
109   }
110   if (that->IsValue()) {
111     this->MakeValue(ok);
112     if (*ok && that->IsConst()) this->MakeConst(ok);
113     return;
114   }
115
116 #ifdef DEBUG
117   if (FLAG_print_interface_details) {
118     PrintF("%*s# Unifying...\n", Nesting::current(), "");
119     PrintF("%*sthis = ", Nesting::current(), "");
120     this->Print(Nesting::current());
121     PrintF("%*sthat = ", Nesting::current(), "");
122     that->Print(Nesting::current());
123   }
124 #endif
125
126   // Merge the smaller interface into the larger, for performance.
127   if (this->exports_ != NULL && (that->exports_ == NULL ||
128       this->exports_->occupancy() >= that->exports_->occupancy())) {
129     this->DoUnify(that, ok, zone);
130   } else {
131     that->DoUnify(this, ok, zone);
132   }
133
134 #ifdef DEBUG
135   if (FLAG_print_interface_details) {
136     PrintF("%*sthis' = ", Nesting::current(), "");
137     this->Print(Nesting::current());
138     PrintF("%*sthat' = ", Nesting::current(), "");
139     that->Print(Nesting::current());
140     PrintF("%*s# Unified.\n", Nesting::current(), "");
141   }
142 #endif
143 }
144
145
146 void Interface::DoUnify(Interface* that, bool* ok, Zone* zone) {
147   ASSERT(this->forward_ == NULL);
148   ASSERT(that->forward_ == NULL);
149   ASSERT(!this->IsValue());
150   ASSERT(!that->IsValue());
151   ASSERT(this->index_ == -1);
152   ASSERT(that->index_ == -1);
153   ASSERT(*ok);
154
155 #ifdef DEBUG
156     Nesting nested;
157 #endif
158
159   // Try to merge all members from that into this.
160   ZoneHashMap* map = that->exports_;
161   if (map != NULL) {
162     for (ZoneHashMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
163       this->DoAdd(p->key, p->hash, static_cast<Interface*>(p->value), zone, ok);
164       if (!*ok) return;
165     }
166   }
167
168   // If the new interface is larger than that's, then there were members in
169   // 'this' which 'that' didn't have. If 'that' was frozen that is an error.
170   int this_size = this->exports_ == NULL ? 0 : this->exports_->occupancy();
171   int that_size = map == NULL ? 0 : map->occupancy();
172   if (that->IsFrozen() && this_size > that_size) {
173     *ok = false;
174     return;
175   }
176
177   // Merge interfaces.
178   this->flags_ |= that->flags_;
179   that->forward_ = this;
180 }
181
182
183 #ifdef DEBUG
184 void Interface::Print(int n) {
185   int n0 = n > 0 ? n : 0;
186
187   if (FLAG_print_interface_details) {
188     PrintF("%p", static_cast<void*>(this));
189     for (Interface* link = this->forward_; link != NULL; link = link->forward_)
190       PrintF("->%p", static_cast<void*>(link));
191     PrintF(" ");
192   }
193
194   if (IsUnknown()) {
195     PrintF("unknown\n");
196   } else if (IsConst()) {
197     PrintF("const\n");
198   } else if (IsValue()) {
199     PrintF("value\n");
200   } else if (IsModule()) {
201     PrintF("module %d %s{", Index(), IsFrozen() ? "" : "(unresolved) ");
202     ZoneHashMap* map = Chase()->exports_;
203     if (map == NULL || map->occupancy() == 0) {
204       PrintF("}\n");
205     } else if (n < 0 || n0 >= 2 * FLAG_print_interface_depth) {
206       // Avoid infinite recursion on cyclic types.
207       PrintF("...}\n");
208     } else {
209       PrintF("\n");
210       for (ZoneHashMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
211         String* name = *static_cast<String**>(p->key);
212         Interface* interface = static_cast<Interface*>(p->value);
213         PrintF("%*s%s : ", n0 + 2, "", name->ToAsciiArray());
214         interface->Print(n0 + 2);
215       }
216       PrintF("%*s}\n", n0, "");
217     }
218   }
219 }
220 #endif
221
222 } }  // namespace v8::internal