erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "accessors.h" |
| 31 | #include "api.h" |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 32 | #include "arguments.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "bootstrapper.h" |
| 34 | #include "compiler.h" |
| 35 | #include "debug.h" |
| 36 | #include "execution.h" |
| 37 | #include "global-handles.h" |
| 38 | #include "natives.h" |
| 39 | #include "runtime.h" |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 40 | #include "string-search.h" |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 41 | #include "stub-cache.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 42 | #include "vm-state-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 44 | namespace v8 { |
| 45 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 47 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 48 | int HandleScope::NumberOfHandles(Isolate* isolate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 49 | HandleScopeImplementer* impl = isolate->handle_scope_implementer(); |
| 50 | int n = impl->blocks()->length(); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 51 | if (n == 0) return 0; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 52 | return ((n - 1) * kHandleBlockSize) + static_cast<int>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 53 | (isolate->handle_scope_data()->next - impl->blocks()->last())); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 57 | Object** HandleScope::Extend(Isolate* isolate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 58 | v8::ImplementationUtilities::HandleScopeData* current = |
| 59 | isolate->handle_scope_data(); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 60 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 61 | Object** result = current->next; |
| 62 | |
| 63 | ASSERT(result == current->limit); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 64 | // Make sure there's at least one scope on the stack and that the |
| 65 | // top of the scope stack isn't a barrier. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 66 | if (current->level == 0) { |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 67 | Utils::ReportApiFailure("v8::HandleScope::CreateHandle()", |
| 68 | "Cannot create a handle without a HandleScope"); |
| 69 | return NULL; |
| 70 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 71 | HandleScopeImplementer* impl = isolate->handle_scope_implementer(); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 72 | // If there's more room in the last block, we use that. This is used |
| 73 | // for fast creation of scopes after scope barriers. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 74 | if (!impl->blocks()->is_empty()) { |
| 75 | Object** limit = &impl->blocks()->last()[kHandleBlockSize]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 76 | if (current->limit != limit) { |
| 77 | current->limit = limit; |
| 78 | ASSERT(limit - current->next < kHandleBlockSize); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 82 | // If we still haven't found a slot for the handle, we extend the |
| 83 | // current handle scope by allocating a new handle block. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 84 | if (result == current->limit) { |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 85 | // If there's a spare block, use it for growing the current scope. |
| 86 | result = impl->GetSpareOrNewBlock(); |
| 87 | // Add the extension to the global list of blocks, but count the |
| 88 | // extension as part of the current scope. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 89 | impl->blocks()->Add(result); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 90 | current->limit = &result[kHandleBlockSize]; |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 91 | } |
| 92 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 93 | return result; |
| 94 | } |
| 95 | |
| 96 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 97 | void HandleScope::DeleteExtensions(Isolate* isolate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 98 | v8::ImplementationUtilities::HandleScopeData* current = |
| 99 | isolate->handle_scope_data(); |
| 100 | isolate->handle_scope_implementer()->DeleteExtensions(current->limit); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 104 | #ifdef ENABLE_HANDLE_ZAPPING |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 105 | void HandleScope::ZapRange(Object** start, Object** end) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 106 | ASSERT(end - start <= kHandleBlockSize); |
| 107 | for (Object** p = start; p != end; p++) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 108 | *reinterpret_cast<Address*>(p) = v8::internal::kHandleZapValue; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 111 | #endif |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 112 | |
| 113 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 114 | Address HandleScope::current_level_address(Isolate* isolate) { |
| 115 | return reinterpret_cast<Address>(&isolate->handle_scope_data()->level); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 119 | Address HandleScope::current_next_address(Isolate* isolate) { |
| 120 | return reinterpret_cast<Address>(&isolate->handle_scope_data()->next); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 124 | Address HandleScope::current_limit_address(Isolate* isolate) { |
| 125 | return reinterpret_cast<Address>(&isolate->handle_scope_data()->limit); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 129 | Handle<FixedArray> AddKeysFromJSArray(Handle<FixedArray> content, |
| 130 | Handle<JSArray> array) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 131 | CALL_HEAP_FUNCTION(content->GetIsolate(), |
| 132 | content->AddKeysFromJSArray(*array), FixedArray); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
| 136 | Handle<FixedArray> UnionOfKeys(Handle<FixedArray> first, |
| 137 | Handle<FixedArray> second) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 138 | CALL_HEAP_FUNCTION(first->GetIsolate(), |
| 139 | first->UnionOfKeys(*second), FixedArray); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 143 | Handle<JSGlobalProxy> ReinitializeJSGlobalProxy( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 144 | Handle<JSFunction> constructor, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 145 | Handle<JSGlobalProxy> global) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 146 | CALL_HEAP_FUNCTION( |
| 147 | constructor->GetIsolate(), |
| 148 | constructor->GetHeap()->ReinitializeJSGlobalProxy(*constructor, *global), |
| 149 | JSGlobalProxy); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
| 153 | void SetExpectedNofProperties(Handle<JSFunction> func, int nof) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 154 | // If objects constructed from this function exist then changing |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 155 | // 'estimated_nof_properties' is dangerous since the previous value might |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 156 | // have been compiled into the fast construct stub. More over, the inobject |
| 157 | // slack tracking logic might have adjusted the previous value, so even |
| 158 | // passing the same value is risky. |
| 159 | if (func->shared()->live_objects_may_exist()) return; |
| 160 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 161 | func->shared()->set_expected_nof_properties(nof); |
| 162 | if (func->has_initial_map()) { |
| 163 | Handle<Map> new_initial_map = |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 164 | func->GetIsolate()->factory()->CopyMap( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 165 | Handle<Map>(func->initial_map())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 166 | new_initial_map->set_unused_property_fields(nof); |
| 167 | func->set_initial_map(*new_initial_map); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 172 | static int ExpectedNofPropertiesFromEstimate(int estimate) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 173 | // If no properties are added in the constructor, they are more likely |
| 174 | // to be added later. |
| 175 | if (estimate == 0) estimate = 2; |
| 176 | |
| 177 | // We do not shrink objects that go into a snapshot (yet), so we adjust |
| 178 | // the estimate conservatively. |
| 179 | if (Serializer::enabled()) return estimate + 2; |
| 180 | |
| 181 | // Inobject slack tracking will reclaim redundant inobject space later, |
| 182 | // so we can afford to adjust the estimate generously. |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 183 | if (FLAG_clever_optimizations) { |
| 184 | return estimate + 8; |
| 185 | } else { |
| 186 | return estimate + 3; |
| 187 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | |
| 191 | void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared, |
| 192 | int estimate) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 193 | // See the comment in SetExpectedNofProperties. |
| 194 | if (shared->live_objects_may_exist()) return; |
| 195 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 196 | shared->set_expected_nof_properties( |
| 197 | ExpectedNofPropertiesFromEstimate(estimate)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 201 | void FlattenString(Handle<String> string) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 202 | CALL_HEAP_FUNCTION_VOID(string->GetIsolate(), string->TryFlatten()); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | |
| 206 | Handle<String> FlattenGetString(Handle<String> string) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 207 | CALL_HEAP_FUNCTION(string->GetIsolate(), string->TryFlatten(), String); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 211 | Handle<Object> SetProperty(Isolate* isolate, |
| 212 | Handle<Object> object, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 213 | Handle<Object> key, |
| 214 | Handle<Object> value, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 215 | PropertyAttributes attributes, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 216 | StrictModeFlag strict_mode) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 217 | CALL_HEAP_FUNCTION( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 218 | isolate, |
| 219 | Runtime::SetObjectProperty( |
| 220 | isolate, object, key, value, attributes, strict_mode), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 221 | Object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 225 | Handle<Object> ForceSetProperty(Handle<JSObject> object, |
| 226 | Handle<Object> key, |
| 227 | Handle<Object> value, |
| 228 | PropertyAttributes attributes) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 229 | Isolate* isolate = object->GetIsolate(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 230 | CALL_HEAP_FUNCTION( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 231 | isolate, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 232 | Runtime::ForceSetObjectProperty( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 233 | isolate, object, key, value, attributes), |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 234 | Object); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 238 | Handle<Object> DeleteProperty(Handle<JSObject> object, Handle<Object> key) { |
| 239 | Isolate* isolate = object->GetIsolate(); |
| 240 | CALL_HEAP_FUNCTION(isolate, |
| 241 | Runtime::DeleteObjectProperty( |
| 242 | isolate, object, key, JSReceiver::NORMAL_DELETION), |
| 243 | Object); |
| 244 | } |
| 245 | |
| 246 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 247 | Handle<Object> ForceDeleteProperty(Handle<JSObject> object, |
| 248 | Handle<Object> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 249 | Isolate* isolate = object->GetIsolate(); |
| 250 | CALL_HEAP_FUNCTION(isolate, |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 251 | Runtime::DeleteObjectProperty( |
| 252 | isolate, object, key, JSReceiver::FORCE_DELETION), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 253 | Object); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 257 | Handle<Object> HasProperty(Handle<JSReceiver> obj, Handle<Object> key) { |
| 258 | Isolate* isolate = obj->GetIsolate(); |
| 259 | CALL_HEAP_FUNCTION(isolate, |
| 260 | Runtime::HasObjectProperty(isolate, obj, key), Object); |
| 261 | } |
| 262 | |
| 263 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 264 | Handle<Object> GetProperty(Handle<JSReceiver> obj, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 265 | const char* name) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 266 | Isolate* isolate = obj->GetIsolate(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 267 | Handle<String> str = isolate->factory()->InternalizeUtf8String(name); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 268 | CALL_HEAP_FUNCTION(isolate, obj->GetProperty(*str), Object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 272 | Handle<Object> GetProperty(Isolate* isolate, |
| 273 | Handle<Object> obj, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 274 | Handle<Object> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 275 | CALL_HEAP_FUNCTION(isolate, |
| 276 | Runtime::GetObjectProperty(isolate, obj, key), Object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 280 | Handle<Object> LookupSingleCharacterStringFromCode(Isolate* isolate, |
| 281 | uint32_t index) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 282 | CALL_HEAP_FUNCTION( |
| 283 | isolate, |
| 284 | isolate->heap()->LookupSingleCharacterStringFromCode(index), Object); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 288 | // Wrappers for scripts are kept alive and cached in weak global |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 289 | // handles referred from foreign objects held by the scripts as long as |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 290 | // they are used. When they are not used anymore, the garbage |
| 291 | // collector will call the weak callback on the global handle |
| 292 | // associated with the wrapper and get rid of both the wrapper and the |
| 293 | // handle. |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 294 | static void ClearWrapperCache(v8::Isolate* v8_isolate, |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 295 | Persistent<v8::Value>* handle, |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 296 | void*) { |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 297 | Handle<Object> cache = Utils::OpenPersistent(handle); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 298 | JSValue* wrapper = JSValue::cast(*cache); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 299 | Foreign* foreign = Script::cast(wrapper->value())->wrapper(); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 300 | ASSERT(foreign->foreign_address() == |
| 301 | reinterpret_cast<Address>(cache.location())); |
| 302 | foreign->set_foreign_address(0); |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 303 | Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 304 | isolate->global_handles()->Destroy(cache.location()); |
| 305 | isolate->counters()->script_wrappers()->Decrement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | |
| 309 | Handle<JSValue> GetScriptWrapper(Handle<Script> script) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 310 | if (script->wrapper()->foreign_address() != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 311 | // Return the script wrapper directly from the cache. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 312 | return Handle<JSValue>( |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 313 | reinterpret_cast<JSValue**>(script->wrapper()->foreign_address())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 314 | } |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 315 | Isolate* isolate = script->GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 316 | // Construct a new script wrapper. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 317 | isolate->counters()->script_wrappers()->Increment(); |
| 318 | Handle<JSFunction> constructor = isolate->script_function(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 319 | Handle<JSValue> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 320 | Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor)); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 321 | |
| 322 | // The allocation might have triggered a GC, which could have called this |
| 323 | // function recursively, and a wrapper has already been created and cached. |
| 324 | // In that case, simply return the cached wrapper. |
| 325 | if (script->wrapper()->foreign_address() != NULL) { |
| 326 | return Handle<JSValue>( |
| 327 | reinterpret_cast<JSValue**>(script->wrapper()->foreign_address())); |
| 328 | } |
| 329 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 330 | result->set_value(*script); |
| 331 | |
| 332 | // Create a new weak global handle and use it to cache the wrapper |
| 333 | // for future use. The cache will automatically be cleared by the |
| 334 | // garbage collector when it is not used anymore. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 335 | Handle<Object> handle = isolate->global_handles()->Create(*result); |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 336 | isolate->global_handles()->MakeWeak(handle.location(), |
| 337 | NULL, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 338 | &ClearWrapperCache); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 339 | script->wrapper()->set_foreign_address( |
| 340 | reinterpret_cast<Address>(handle.location())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 341 | return result; |
| 342 | } |
| 343 | |
| 344 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 345 | // Init line_ends array with code positions of line ends inside script |
| 346 | // source. |
| 347 | void InitScriptLineEnds(Handle<Script> script) { |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 348 | if (!script->line_ends()->IsUndefined()) return; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 349 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 350 | Isolate* isolate = script->GetIsolate(); |
| 351 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 352 | if (!script->source()->IsString()) { |
| 353 | ASSERT(script->source()->IsUndefined()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 354 | Handle<FixedArray> empty = isolate->factory()->NewFixedArray(0); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 355 | script->set_line_ends(*empty); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 356 | ASSERT(script->line_ends()->IsFixedArray()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 357 | return; |
| 358 | } |
| 359 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 360 | Handle<String> src(String::cast(script->source()), isolate); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 361 | |
| 362 | Handle<FixedArray> array = CalculateLineEnds(src, true); |
| 363 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 364 | if (*array != isolate->heap()->empty_fixed_array()) { |
| 365 | array->set_map(isolate->heap()->fixed_cow_array_map()); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 366 | } |
| 367 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 368 | script->set_line_ends(*array); |
| 369 | ASSERT(script->line_ends()->IsFixedArray()); |
| 370 | } |
| 371 | |
| 372 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 373 | template <typename SourceChar> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 374 | static void CalculateLineEnds(Isolate* isolate, |
| 375 | List<int>* line_ends, |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 376 | Vector<const SourceChar> src, |
| 377 | bool with_last_line) { |
| 378 | const int src_len = src.length(); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 379 | StringSearch<uint8_t, SourceChar> search(isolate, STATIC_ASCII_VECTOR("\n")); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 380 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 381 | // Find and record line ends. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 382 | int position = 0; |
| 383 | while (position != -1 && position < src_len) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 384 | position = search.Search(src, position); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 385 | if (position != -1) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 386 | line_ends->Add(position); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 387 | position++; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 388 | } else if (with_last_line) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 389 | // Even if the last line misses a line end, it is counted. |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 390 | line_ends->Add(src_len); |
| 391 | return; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 392 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 393 | } |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 394 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 395 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 396 | |
| 397 | Handle<FixedArray> CalculateLineEnds(Handle<String> src, |
| 398 | bool with_last_line) { |
| 399 | src = FlattenGetString(src); |
| 400 | // Rough estimate of line count based on a roughly estimated average |
| 401 | // length of (unpacked) code. |
| 402 | int line_count_estimate = src->length() >> 4; |
| 403 | List<int> line_ends(line_count_estimate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 404 | Isolate* isolate = src->GetIsolate(); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 405 | { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 406 | DisallowHeapAllocation no_allocation; // ensure vectors stay valid. |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 407 | // Dispatch on type of strings. |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 408 | String::FlatContent content = src->GetFlatContent(); |
| 409 | ASSERT(content.IsFlat()); |
| 410 | if (content.IsAscii()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 411 | CalculateLineEnds(isolate, |
| 412 | &line_ends, |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 413 | content.ToOneByteVector(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 414 | with_last_line); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 415 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 416 | CalculateLineEnds(isolate, |
| 417 | &line_ends, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 418 | content.ToUC16Vector(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 419 | with_last_line); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | int line_count = line_ends.length(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 423 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(line_count); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 424 | for (int i = 0; i < line_count; i++) { |
| 425 | array->set(i, Smi::FromInt(line_ends[i])); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 426 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 427 | return array; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | |
| 431 | // Convert code position into line number. |
| 432 | int GetScriptLineNumber(Handle<Script> script, int code_pos) { |
| 433 | InitScriptLineEnds(script); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 434 | DisallowHeapAllocation no_allocation; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 435 | FixedArray* line_ends_array = FixedArray::cast(script->line_ends()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 436 | const int line_ends_len = line_ends_array->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 437 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 438 | if (!line_ends_len) return -1; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 439 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 440 | if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 441 | return script->line_offset()->value(); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 442 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 443 | |
| 444 | int left = 0; |
| 445 | int right = line_ends_len; |
| 446 | while (int half = (right - left) / 2) { |
| 447 | if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) { |
| 448 | right -= half; |
| 449 | } else { |
| 450 | left += half; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 453 | return right + script->line_offset()->value(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 454 | } |
| 455 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 456 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 457 | // Convert code position into column number. |
| 458 | int GetScriptColumnNumber(Handle<Script> script, int code_pos) { |
| 459 | int line_number = GetScriptLineNumber(script, code_pos); |
| 460 | if (line_number == -1) return -1; |
| 461 | |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 462 | DisallowHeapAllocation no_allocation; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 463 | FixedArray* line_ends_array = FixedArray::cast(script->line_ends()); |
| 464 | line_number = line_number - script->line_offset()->value(); |
| 465 | if (line_number == 0) return code_pos + script->column_offset()->value(); |
| 466 | int prev_line_end_pos = |
| 467 | Smi::cast(line_ends_array->get(line_number - 1))->value(); |
| 468 | return code_pos - (prev_line_end_pos + 1); |
| 469 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 470 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 471 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 472 | int GetScriptLineNumberSafe(Handle<Script> script, int code_pos) { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 473 | DisallowHeapAllocation no_allocation; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 474 | if (!script->line_ends()->IsUndefined()) { |
| 475 | return GetScriptLineNumber(script, code_pos); |
| 476 | } |
| 477 | // Slow mode: we do not have line_ends. We have to iterate through source. |
| 478 | if (!script->source()->IsString()) { |
| 479 | return -1; |
| 480 | } |
| 481 | String* source = String::cast(script->source()); |
| 482 | int line = 0; |
| 483 | int len = source->length(); |
| 484 | for (int pos = 0; pos < len; pos++) { |
| 485 | if (pos == code_pos) { |
| 486 | break; |
| 487 | } |
| 488 | if (source->Get(pos) == '\n') { |
| 489 | line++; |
| 490 | } |
| 491 | } |
| 492 | return line; |
| 493 | } |
| 494 | |
| 495 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 496 | // Compute the property keys from the interceptor. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 497 | // TODO(rossberg): support symbols in API, and filter here if needed. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 498 | v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSReceiver> receiver, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 499 | Handle<JSObject> object) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 500 | Isolate* isolate = receiver->GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 501 | Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor()); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 502 | PropertyCallbackArguments |
| 503 | args(isolate, interceptor->data(), *receiver, *object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 504 | v8::Handle<v8::Array> result; |
| 505 | if (!interceptor->enumerator()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 506 | v8::NamedPropertyEnumeratorCallback enum_fun = |
| 507 | v8::ToCData<v8::NamedPropertyEnumeratorCallback>( |
| 508 | interceptor->enumerator()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 509 | LOG(isolate, ApiObjectAccess("interceptor-named-enum", *object)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 510 | result = args.Call(enum_fun); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 511 | } |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 512 | #if ENABLE_EXTRA_CHECKS |
| 513 | CHECK(result.IsEmpty() || v8::Utils::OpenHandle(*result)->IsJSObject()); |
| 514 | #endif |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 515 | return v8::Local<v8::Array>::New(reinterpret_cast<v8::Isolate*>(isolate), |
| 516 | result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | |
| 520 | // Compute the element keys from the interceptor. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 521 | v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSReceiver> receiver, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 522 | Handle<JSObject> object) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 523 | Isolate* isolate = receiver->GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 524 | Handle<InterceptorInfo> interceptor(object->GetIndexedInterceptor()); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 525 | PropertyCallbackArguments |
| 526 | args(isolate, interceptor->data(), *receiver, *object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 527 | v8::Handle<v8::Array> result; |
| 528 | if (!interceptor->enumerator()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 529 | v8::IndexedPropertyEnumeratorCallback enum_fun = |
| 530 | v8::ToCData<v8::IndexedPropertyEnumeratorCallback>( |
| 531 | interceptor->enumerator()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 532 | LOG(isolate, ApiObjectAccess("interceptor-indexed-enum", *object)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 533 | result = args.Call(enum_fun); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 534 | #if ENABLE_EXTRA_CHECKS |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 535 | CHECK(result.IsEmpty() || v8::Utils::OpenHandle(*result)->IsJSObject()); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 536 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 537 | } |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 538 | return v8::Local<v8::Array>::New(reinterpret_cast<v8::Isolate*>(isolate), |
| 539 | result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 543 | Handle<Object> GetScriptNameOrSourceURL(Handle<Script> script) { |
| 544 | Isolate* isolate = script->GetIsolate(); |
| 545 | Handle<String> name_or_source_url_key = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 546 | isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 547 | STATIC_ASCII_VECTOR("nameOrSourceURL")); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 548 | Handle<JSValue> script_wrapper = GetScriptWrapper(script); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 549 | Handle<Object> property = GetProperty(isolate, |
| 550 | script_wrapper, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 551 | name_or_source_url_key); |
| 552 | ASSERT(property->IsJSFunction()); |
| 553 | Handle<JSFunction> method = Handle<JSFunction>::cast(property); |
| 554 | bool caught_exception; |
| 555 | Handle<Object> result = Execution::TryCall(method, script_wrapper, 0, |
| 556 | NULL, &caught_exception); |
| 557 | if (caught_exception) { |
| 558 | result = isolate->factory()->undefined_value(); |
| 559 | } |
| 560 | return result; |
| 561 | } |
| 562 | |
| 563 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 564 | static bool ContainsOnlyValidKeys(Handle<FixedArray> array) { |
| 565 | int len = array->length(); |
| 566 | for (int i = 0; i < len; i++) { |
| 567 | Object* e = array->get(i); |
| 568 | if (!(e->IsString() || e->IsNumber())) return false; |
| 569 | } |
| 570 | return true; |
| 571 | } |
| 572 | |
| 573 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 574 | Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSReceiver> object, |
| 575 | KeyCollectionType type, |
| 576 | bool* threw) { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 577 | USE(ContainsOnlyValidKeys); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 578 | Isolate* isolate = object->GetIsolate(); |
| 579 | Handle<FixedArray> content = isolate->factory()->empty_fixed_array(); |
| 580 | Handle<JSObject> arguments_boilerplate = Handle<JSObject>( |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 581 | isolate->context()->native_context()->arguments_boilerplate(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 582 | isolate); |
| 583 | Handle<JSFunction> arguments_function = Handle<JSFunction>( |
| 584 | JSFunction::cast(arguments_boilerplate->map()->constructor()), |
| 585 | isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 586 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 587 | // Only collect keys if access is permitted. |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 588 | for (Handle<Object> p = object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 589 | *p != isolate->heap()->null_value(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 590 | p = Handle<Object>(p->GetPrototype(isolate), isolate)) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 591 | if (p->IsJSProxy()) { |
| 592 | Handle<JSProxy> proxy(JSProxy::cast(*p), isolate); |
| 593 | Handle<Object> args[] = { proxy }; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 594 | Handle<Object> names = Execution::Call(isolate, |
| 595 | isolate->proxy_enumerate(), |
| 596 | object, |
| 597 | ARRAY_SIZE(args), |
| 598 | args, |
| 599 | threw); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 600 | if (*threw) return content; |
| 601 | content = AddKeysFromJSArray(content, Handle<JSArray>::cast(names)); |
| 602 | break; |
| 603 | } |
| 604 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 605 | Handle<JSObject> current(JSObject::cast(*p), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 606 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 607 | // Check access rights if required. |
| 608 | if (current->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 609 | !isolate->MayNamedAccess(*current, |
| 610 | isolate->heap()->undefined_value(), |
| 611 | v8::ACCESS_KEYS)) { |
| 612 | isolate->ReportFailedAccessCheck(*current, v8::ACCESS_KEYS); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 613 | if (isolate->has_scheduled_exception()) { |
| 614 | isolate->PromoteScheduledException(); |
| 615 | *threw = true; |
| 616 | } |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 617 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 618 | } |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 619 | |
| 620 | // Compute the element keys. |
| 621 | Handle<FixedArray> element_keys = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 622 | isolate->factory()->NewFixedArray(current->NumberOfEnumElements()); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 623 | current->GetEnumElementKeys(*element_keys); |
| 624 | content = UnionOfKeys(content, element_keys); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 625 | ASSERT(ContainsOnlyValidKeys(content)); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 626 | |
| 627 | // Add the element keys from the interceptor. |
| 628 | if (current->HasIndexedInterceptor()) { |
| 629 | v8::Handle<v8::Array> result = |
| 630 | GetKeysForIndexedInterceptor(object, current); |
| 631 | if (!result.IsEmpty()) |
| 632 | content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result)); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 633 | ASSERT(ContainsOnlyValidKeys(content)); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 634 | } |
| 635 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 636 | // We can cache the computed property keys if access checks are |
| 637 | // not needed and no interceptors are involved. |
| 638 | // |
| 639 | // We do not use the cache if the object has elements and |
| 640 | // therefore it does not make sense to cache the property names |
| 641 | // for arguments objects. Arguments objects will always have |
| 642 | // elements. |
fschneider@chromium.org | ed78ffd | 2010-07-21 11:05:19 +0000 | [diff] [blame] | 643 | // Wrapped strings have elements, but don't have an elements |
| 644 | // array or dictionary. So the fast inline test for whether to |
| 645 | // use the cache says yes, so we should not create a cache. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 646 | bool cache_enum_keys = |
| 647 | ((current->map()->constructor() != *arguments_function) && |
fschneider@chromium.org | ed78ffd | 2010-07-21 11:05:19 +0000 | [diff] [blame] | 648 | !current->IsJSValue() && |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 649 | !current->IsAccessCheckNeeded() && |
| 650 | !current->HasNamedInterceptor() && |
| 651 | !current->HasIndexedInterceptor()); |
| 652 | // Compute the property keys and cache them if possible. |
| 653 | content = |
| 654 | UnionOfKeys(content, GetEnumPropertyKeys(current, cache_enum_keys)); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 655 | ASSERT(ContainsOnlyValidKeys(content)); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 656 | |
| 657 | // Add the property keys from the interceptor. |
| 658 | if (current->HasNamedInterceptor()) { |
| 659 | v8::Handle<v8::Array> result = |
| 660 | GetKeysForNamedInterceptor(object, current); |
| 661 | if (!result.IsEmpty()) |
| 662 | content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result)); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 663 | ASSERT(ContainsOnlyValidKeys(content)); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | // If we only want local properties we bail out after the first |
| 667 | // iteration. |
| 668 | if (type == LOCAL_ONLY) |
| 669 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 670 | } |
| 671 | return content; |
| 672 | } |
| 673 | |
| 674 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 675 | Handle<JSArray> GetKeysFor(Handle<JSReceiver> object, bool* threw) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 676 | Isolate* isolate = object->GetIsolate(); |
| 677 | isolate->counters()->for_in()->Increment(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 678 | Handle<FixedArray> elements = |
| 679 | GetKeysInFixedArrayFor(object, INCLUDE_PROTOS, threw); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 680 | return isolate->factory()->NewJSArrayWithElements(elements); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 684 | Handle<FixedArray> ReduceFixedArrayTo(Handle<FixedArray> array, int length) { |
| 685 | ASSERT(array->length() >= length); |
| 686 | if (array->length() == length) return array; |
| 687 | |
| 688 | Handle<FixedArray> new_array = |
| 689 | array->GetIsolate()->factory()->NewFixedArray(length); |
| 690 | for (int i = 0; i < length; ++i) new_array->set(i, array->get(i)); |
| 691 | return new_array; |
| 692 | } |
| 693 | |
| 694 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 695 | Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object, |
| 696 | bool cache_result) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 697 | Isolate* isolate = object->GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 698 | if (object->HasFastProperties()) { |
| 699 | if (object->map()->instance_descriptors()->HasEnumCache()) { |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 700 | int own_property_count = object->map()->EnumLength(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 701 | // If we have an enum cache, but the enum length of the given map is set |
| 702 | // to kInvalidEnumCache, this means that the map itself has never used the |
| 703 | // present enum cache. The first step to using the cache is to set the |
| 704 | // enum length of the map by counting the number of own descriptors that |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 705 | // are not DONT_ENUM or SYMBOLIC. |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 706 | if (own_property_count == Map::kInvalidEnumCache) { |
| 707 | own_property_count = object->map()->NumberOfDescribedProperties( |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 708 | OWN_DESCRIPTORS, DONT_SHOW); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 709 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 710 | if (cache_result) object->map()->SetEnumLength(own_property_count); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 711 | } |
| 712 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 713 | DescriptorArray* desc = object->map()->instance_descriptors(); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 714 | Handle<FixedArray> keys(desc->GetEnumCache(), isolate); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 715 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 716 | // In case the number of properties required in the enum are actually |
| 717 | // present, we can reuse the enum cache. Otherwise, this means that the |
| 718 | // enum cache was generated for a previous (smaller) version of the |
| 719 | // Descriptor Array. In that case we regenerate the enum cache. |
| 720 | if (own_property_count <= keys->length()) { |
| 721 | isolate->counters()->enum_cache_hits()->Increment(); |
| 722 | return ReduceFixedArrayTo(keys, own_property_count); |
| 723 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 724 | } |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 725 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 726 | Handle<Map> map(object->map()); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 727 | |
| 728 | if (map->instance_descriptors()->IsEmpty()) { |
| 729 | isolate->counters()->enum_cache_hits()->Increment(); |
| 730 | if (cache_result) map->SetEnumLength(0); |
| 731 | return isolate->factory()->empty_fixed_array(); |
| 732 | } |
| 733 | |
| 734 | isolate->counters()->enum_cache_misses()->Increment(); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 735 | int num_enum = map->NumberOfDescribedProperties(ALL_DESCRIPTORS, DONT_SHOW); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 736 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 737 | Handle<FixedArray> storage = isolate->factory()->NewFixedArray(num_enum); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 738 | Handle<FixedArray> indices = isolate->factory()->NewFixedArray(num_enum); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 739 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 740 | Handle<DescriptorArray> descs = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 741 | Handle<DescriptorArray>(object->map()->instance_descriptors(), isolate); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 742 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 743 | int real_size = map->NumberOfOwnDescriptors(); |
| 744 | int enum_size = 0; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 745 | int index = 0; |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 746 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 747 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 748 | PropertyDetails details = descs->GetDetails(i); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 749 | Object* key = descs->GetKey(i); |
| 750 | if (!(details.IsDontEnum() || key->IsSymbol())) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 751 | if (i < real_size) ++enum_size; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 752 | storage->set(index, key); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 753 | if (!indices.is_null()) { |
| 754 | if (details.type() != FIELD) { |
| 755 | indices = Handle<FixedArray>(); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 756 | } else { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 757 | int field_index = descs->GetFieldIndex(i); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 758 | if (field_index >= map->inobject_properties()) { |
| 759 | field_index = -(field_index - map->inobject_properties() + 1); |
| 760 | } |
| 761 | indices->set(index, Smi::FromInt(field_index)); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 762 | } |
| 763 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 764 | index++; |
| 765 | } |
| 766 | } |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 767 | ASSERT(index == storage->length()); |
| 768 | |
| 769 | Handle<FixedArray> bridge_storage = |
| 770 | isolate->factory()->NewFixedArray( |
| 771 | DescriptorArray::kEnumCacheBridgeLength); |
| 772 | DescriptorArray* desc = object->map()->instance_descriptors(); |
| 773 | desc->SetEnumCache(*bridge_storage, |
| 774 | *storage, |
| 775 | indices.is_null() ? Object::cast(Smi::FromInt(0)) |
| 776 | : Object::cast(*indices)); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 777 | if (cache_result) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 778 | object->map()->SetEnumLength(enum_size); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 779 | } |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 780 | |
| 781 | return ReduceFixedArrayTo(storage, enum_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 782 | } else { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 783 | Handle<NameDictionary> dictionary(object->property_dictionary()); |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 784 | |
| 785 | int length = dictionary->NumberOfElements(); |
| 786 | if (length == 0) { |
| 787 | return Handle<FixedArray>(isolate->heap()->empty_fixed_array()); |
| 788 | } |
| 789 | |
| 790 | // The enumeration array is generated by allocating an array big enough to |
| 791 | // hold all properties that have been seen, whether they are are deleted or |
| 792 | // not. Subsequently all visible properties are added to the array. If some |
| 793 | // properties were not visible, the array is trimmed so it only contains |
| 794 | // visible properties. This improves over adding elements and sorting by |
| 795 | // index by having linear complexity rather than n*log(n). |
| 796 | |
| 797 | // By comparing the monotonous NextEnumerationIndex to the NumberOfElements, |
| 798 | // we can predict the number of holes in the final array. If there will be |
| 799 | // more than 50% holes, regenerate the enumeration indices to reduce the |
| 800 | // number of holes to a minimum. This avoids allocating a large array if |
| 801 | // many properties were added but subsequently deleted. |
| 802 | int next_enumeration = dictionary->NextEnumerationIndex(); |
| 803 | if (!object->IsGlobalObject() && next_enumeration > (length * 3) / 2) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 804 | NameDictionary::DoGenerateNewEnumerationIndices(dictionary); |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 805 | next_enumeration = dictionary->NextEnumerationIndex(); |
| 806 | } |
| 807 | |
| 808 | Handle<FixedArray> storage = |
| 809 | isolate->factory()->NewFixedArray(next_enumeration); |
| 810 | |
| 811 | storage = Handle<FixedArray>(dictionary->CopyEnumKeysTo(*storage)); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 812 | ASSERT(storage->length() == object->NumberOfLocalProperties(DONT_SHOW)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 813 | return storage; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 818 | Handle<ObjectHashSet> ObjectHashSetAdd(Handle<ObjectHashSet> table, |
| 819 | Handle<Object> key) { |
| 820 | CALL_HEAP_FUNCTION(table->GetIsolate(), |
| 821 | table->Add(*key), |
| 822 | ObjectHashSet); |
| 823 | } |
| 824 | |
| 825 | |
| 826 | Handle<ObjectHashSet> ObjectHashSetRemove(Handle<ObjectHashSet> table, |
| 827 | Handle<Object> key) { |
| 828 | CALL_HEAP_FUNCTION(table->GetIsolate(), |
| 829 | table->Remove(*key), |
| 830 | ObjectHashSet); |
| 831 | } |
| 832 | |
| 833 | |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 834 | Handle<ObjectHashTable> PutIntoObjectHashTable(Handle<ObjectHashTable> table, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 835 | Handle<Object> key, |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 836 | Handle<Object> value) { |
| 837 | CALL_HEAP_FUNCTION(table->GetIsolate(), |
| 838 | table->Put(*key, *value), |
| 839 | ObjectHashTable); |
| 840 | } |
| 841 | |
| 842 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 843 | DeferredHandleScope::DeferredHandleScope(Isolate* isolate) |
| 844 | : impl_(isolate->handle_scope_implementer()) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 845 | impl_->BeginDeferredScope(); |
| 846 | v8::ImplementationUtilities::HandleScopeData* data = |
| 847 | impl_->isolate()->handle_scope_data(); |
| 848 | Object** new_next = impl_->GetSpareOrNewBlock(); |
| 849 | Object** new_limit = &new_next[kHandleBlockSize]; |
| 850 | ASSERT(data->limit == &impl_->blocks()->last()[kHandleBlockSize]); |
| 851 | impl_->blocks()->Add(new_next); |
| 852 | |
| 853 | #ifdef DEBUG |
| 854 | prev_level_ = data->level; |
| 855 | #endif |
| 856 | data->level++; |
| 857 | prev_limit_ = data->limit; |
| 858 | prev_next_ = data->next; |
| 859 | data->next = new_next; |
| 860 | data->limit = new_limit; |
| 861 | } |
| 862 | |
| 863 | |
| 864 | DeferredHandleScope::~DeferredHandleScope() { |
| 865 | impl_->isolate()->handle_scope_data()->level--; |
| 866 | ASSERT(handles_detached_); |
| 867 | ASSERT(impl_->isolate()->handle_scope_data()->level == prev_level_); |
| 868 | } |
| 869 | |
| 870 | |
| 871 | DeferredHandles* DeferredHandleScope::Detach() { |
| 872 | DeferredHandles* deferred = impl_->Detach(prev_limit_); |
| 873 | v8::ImplementationUtilities::HandleScopeData* data = |
| 874 | impl_->isolate()->handle_scope_data(); |
| 875 | data->next = prev_next_; |
| 876 | data->limit = prev_limit_; |
| 877 | #ifdef DEBUG |
| 878 | handles_detached_ = true; |
| 879 | #endif |
| 880 | return deferred; |
| 881 | } |
| 882 | |
| 883 | |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 884 | void AddWeakObjectToCodeDependency(Heap* heap, |
| 885 | Handle<Object> object, |
| 886 | Handle<Code> code) { |
| 887 | heap->EnsureWeakObjectToCodeTable(); |
| 888 | Handle<DependentCode> dep(heap->LookupWeakObjectToCodeDependency(*object)); |
| 889 | dep = DependentCode::Insert(dep, DependentCode::kWeaklyEmbeddedGroup, code); |
| 890 | CALL_HEAP_FUNCTION_VOID(heap->isolate(), |
| 891 | heap->AddWeakObjectToCodeDependency(*object, *dep)); |
| 892 | } |
| 893 | |
| 894 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 895 | } } // namespace v8::internal |