ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1 | // Copyright 2011 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 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 48 | int HandleScope::NumberOfHandles() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 49 | Isolate* isolate = Isolate::Current(); |
| 50 | HandleScopeImplementer* impl = isolate->handle_scope_implementer(); |
| 51 | int n = impl->blocks()->length(); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 52 | if (n == 0) return 0; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 53 | return ((n - 1) * kHandleBlockSize) + static_cast<int>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 54 | (isolate->handle_scope_data()->next - impl->blocks()->last())); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 58 | Object** HandleScope::Extend() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 59 | Isolate* isolate = Isolate::Current(); |
| 60 | v8::ImplementationUtilities::HandleScopeData* current = |
| 61 | isolate->handle_scope_data(); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 62 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 63 | Object** result = current->next; |
| 64 | |
| 65 | ASSERT(result == current->limit); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 66 | // Make sure there's at least one scope on the stack and that the |
| 67 | // top of the scope stack isn't a barrier. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 68 | if (current->level == 0) { |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 69 | Utils::ReportApiFailure("v8::HandleScope::CreateHandle()", |
| 70 | "Cannot create a handle without a HandleScope"); |
| 71 | return NULL; |
| 72 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 73 | HandleScopeImplementer* impl = isolate->handle_scope_implementer(); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 74 | // If there's more room in the last block, we use that. This is used |
| 75 | // for fast creation of scopes after scope barriers. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 76 | if (!impl->blocks()->is_empty()) { |
| 77 | Object** limit = &impl->blocks()->last()[kHandleBlockSize]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 78 | if (current->limit != limit) { |
| 79 | current->limit = limit; |
| 80 | ASSERT(limit - current->next < kHandleBlockSize); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 84 | // If we still haven't found a slot for the handle, we extend the |
| 85 | // current handle scope by allocating a new handle block. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 86 | if (result == current->limit) { |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 87 | // If there's a spare block, use it for growing the current scope. |
| 88 | result = impl->GetSpareOrNewBlock(); |
| 89 | // Add the extension to the global list of blocks, but count the |
| 90 | // extension as part of the current scope. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 91 | impl->blocks()->Add(result); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 92 | current->limit = &result[kHandleBlockSize]; |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 93 | } |
| 94 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 95 | return result; |
| 96 | } |
| 97 | |
| 98 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 99 | void HandleScope::DeleteExtensions(Isolate* isolate) { |
| 100 | ASSERT(isolate == Isolate::Current()); |
| 101 | v8::ImplementationUtilities::HandleScopeData* current = |
| 102 | isolate->handle_scope_data(); |
| 103 | isolate->handle_scope_implementer()->DeleteExtensions(current->limit); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 107 | void HandleScope::ZapRange(Object** start, Object** end) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 108 | ASSERT(end - start <= kHandleBlockSize); |
| 109 | for (Object** p = start; p != end; p++) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 110 | *reinterpret_cast<Address*>(p) = v8::internal::kHandleZapValue; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 115 | Address HandleScope::current_level_address() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 116 | return reinterpret_cast<Address>( |
| 117 | &Isolate::Current()->handle_scope_data()->level); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
| 121 | Address HandleScope::current_next_address() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 122 | return reinterpret_cast<Address>( |
| 123 | &Isolate::Current()->handle_scope_data()->next); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | |
| 127 | Address HandleScope::current_limit_address() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 128 | return reinterpret_cast<Address>( |
| 129 | &Isolate::Current()->handle_scope_data()->limit); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | Handle<FixedArray> AddKeysFromJSArray(Handle<FixedArray> content, |
| 134 | Handle<JSArray> array) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 135 | CALL_HEAP_FUNCTION(content->GetIsolate(), |
| 136 | content->AddKeysFromJSArray(*array), FixedArray); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
| 140 | Handle<FixedArray> UnionOfKeys(Handle<FixedArray> first, |
| 141 | Handle<FixedArray> second) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 142 | CALL_HEAP_FUNCTION(first->GetIsolate(), |
| 143 | first->UnionOfKeys(*second), FixedArray); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 147 | Handle<JSGlobalProxy> ReinitializeJSGlobalProxy( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 148 | Handle<JSFunction> constructor, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 149 | Handle<JSGlobalProxy> global) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 150 | CALL_HEAP_FUNCTION( |
| 151 | constructor->GetIsolate(), |
| 152 | constructor->GetHeap()->ReinitializeJSGlobalProxy(*constructor, *global), |
| 153 | JSGlobalProxy); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
| 157 | void SetExpectedNofProperties(Handle<JSFunction> func, int nof) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 158 | // If objects constructed from this function exist then changing |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 159 | // 'estimated_nof_properties' is dangerous since the previous value might |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 160 | // have been compiled into the fast construct stub. More over, the inobject |
| 161 | // slack tracking logic might have adjusted the previous value, so even |
| 162 | // passing the same value is risky. |
| 163 | if (func->shared()->live_objects_may_exist()) return; |
| 164 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 165 | func->shared()->set_expected_nof_properties(nof); |
| 166 | if (func->has_initial_map()) { |
| 167 | Handle<Map> new_initial_map = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 168 | func->GetIsolate()->factory()->CopyMapDropTransitions( |
| 169 | Handle<Map>(func->initial_map())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 170 | new_initial_map->set_unused_property_fields(nof); |
| 171 | func->set_initial_map(*new_initial_map); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
| 176 | void SetPrototypeProperty(Handle<JSFunction> func, Handle<JSObject> value) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 177 | CALL_HEAP_FUNCTION_VOID(func->GetIsolate(), |
| 178 | func->SetPrototype(*value)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 182 | static int ExpectedNofPropertiesFromEstimate(int estimate) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 183 | // If no properties are added in the constructor, they are more likely |
| 184 | // to be added later. |
| 185 | if (estimate == 0) estimate = 2; |
| 186 | |
| 187 | // We do not shrink objects that go into a snapshot (yet), so we adjust |
| 188 | // the estimate conservatively. |
| 189 | if (Serializer::enabled()) return estimate + 2; |
| 190 | |
| 191 | // Inobject slack tracking will reclaim redundant inobject space later, |
| 192 | // so we can afford to adjust the estimate generously. |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 193 | return estimate + 8; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | |
| 197 | void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared, |
| 198 | int estimate) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 199 | // See the comment in SetExpectedNofProperties. |
| 200 | if (shared->live_objects_may_exist()) return; |
| 201 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 202 | shared->set_expected_nof_properties( |
| 203 | ExpectedNofPropertiesFromEstimate(estimate)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 207 | void NormalizeProperties(Handle<JSObject> object, |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 208 | PropertyNormalizationMode mode, |
| 209 | int expected_additional_properties) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 210 | CALL_HEAP_FUNCTION_VOID(object->GetIsolate(), |
| 211 | object->NormalizeProperties( |
| 212 | mode, |
| 213 | expected_additional_properties)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 217 | Handle<NumberDictionary> NormalizeElements(Handle<JSObject> object) { |
| 218 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 219 | object->NormalizeElements(), |
| 220 | NumberDictionary); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | |
| 224 | void TransformToFastProperties(Handle<JSObject> object, |
| 225 | int unused_property_fields) { |
| 226 | CALL_HEAP_FUNCTION_VOID( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 227 | object->GetIsolate(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 228 | object->TransformToFastProperties(unused_property_fields)); |
| 229 | } |
| 230 | |
| 231 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 232 | Handle<NumberDictionary> NumberDictionarySet( |
| 233 | Handle<NumberDictionary> dictionary, |
| 234 | uint32_t index, |
| 235 | Handle<Object> value, |
| 236 | PropertyDetails details) { |
| 237 | CALL_HEAP_FUNCTION(dictionary->GetIsolate(), |
| 238 | dictionary->Set(index, *value, details), |
| 239 | NumberDictionary); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 243 | void FlattenString(Handle<String> string) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 244 | CALL_HEAP_FUNCTION_VOID(string->GetIsolate(), string->TryFlatten()); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | |
| 248 | Handle<String> FlattenGetString(Handle<String> string) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 249 | CALL_HEAP_FUNCTION(string->GetIsolate(), string->TryFlatten(), String); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | |
| 253 | Handle<Object> SetPrototype(Handle<JSFunction> function, |
| 254 | Handle<Object> prototype) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 255 | ASSERT(function->should_have_prototype()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 256 | CALL_HEAP_FUNCTION(function->GetIsolate(), |
| 257 | Accessors::FunctionSetPrototype(*function, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 258 | *prototype, |
| 259 | NULL), |
| 260 | Object); |
| 261 | } |
| 262 | |
| 263 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 264 | Handle<Object> SetProperty(Handle<JSReceiver> object, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 265 | Handle<String> key, |
| 266 | Handle<Object> value, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 267 | PropertyAttributes attributes, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 268 | StrictModeFlag strict_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 269 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 270 | object->SetProperty(*key, *value, attributes, strict_mode), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 271 | Object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
| 275 | Handle<Object> SetProperty(Handle<Object> object, |
| 276 | Handle<Object> key, |
| 277 | Handle<Object> value, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 278 | PropertyAttributes attributes, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 279 | StrictModeFlag strict_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 280 | Isolate* isolate = Isolate::Current(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 281 | CALL_HEAP_FUNCTION( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 282 | isolate, |
| 283 | Runtime::SetObjectProperty( |
| 284 | isolate, object, key, value, attributes, strict_mode), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 285 | Object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 289 | Handle<Object> ForceSetProperty(Handle<JSObject> object, |
| 290 | Handle<Object> key, |
| 291 | Handle<Object> value, |
| 292 | PropertyAttributes attributes) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 293 | Isolate* isolate = object->GetIsolate(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 294 | CALL_HEAP_FUNCTION( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 295 | isolate, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 296 | Runtime::ForceSetObjectProperty( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 297 | isolate, object, key, value, attributes), |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 298 | Object); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 302 | Handle<Object> SetNormalizedProperty(Handle<JSObject> object, |
| 303 | Handle<String> key, |
| 304 | Handle<Object> value, |
| 305 | PropertyDetails details) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 306 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 307 | object->SetNormalizedProperty(*key, *value, details), |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 308 | Object); |
| 309 | } |
| 310 | |
| 311 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 312 | Handle<Object> ForceDeleteProperty(Handle<JSObject> object, |
| 313 | Handle<Object> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 314 | Isolate* isolate = object->GetIsolate(); |
| 315 | CALL_HEAP_FUNCTION(isolate, |
| 316 | Runtime::ForceDeleteObjectProperty(isolate, object, key), |
| 317 | Object); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 321 | Handle<Object> SetLocalPropertyIgnoreAttributes( |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 322 | Handle<JSObject> object, |
| 323 | Handle<String> key, |
| 324 | Handle<Object> value, |
| 325 | PropertyAttributes attributes) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 326 | CALL_HEAP_FUNCTION( |
| 327 | object->GetIsolate(), |
| 328 | object->SetLocalPropertyIgnoreAttributes(*key, *value, attributes), |
| 329 | Object); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 330 | } |
| 331 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 332 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 333 | void SetLocalPropertyNoThrow(Handle<JSObject> object, |
| 334 | Handle<String> key, |
| 335 | Handle<Object> value, |
| 336 | PropertyAttributes attributes) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 337 | Isolate* isolate = object->GetIsolate(); |
| 338 | ASSERT(!isolate->has_pending_exception()); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 339 | CHECK(!SetLocalPropertyIgnoreAttributes( |
| 340 | object, key, value, attributes).is_null()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 341 | CHECK(!isolate->has_pending_exception()); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 345 | Handle<Object> SetPropertyWithInterceptor(Handle<JSObject> object, |
| 346 | Handle<String> key, |
| 347 | Handle<Object> value, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 348 | PropertyAttributes attributes, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 349 | StrictModeFlag strict_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 350 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 351 | object->SetPropertyWithInterceptor(*key, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 352 | *value, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 353 | attributes, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 354 | strict_mode), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 355 | Object); |
| 356 | } |
| 357 | |
| 358 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 359 | Handle<Object> GetProperty(Handle<JSReceiver> obj, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 360 | const char* name) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 361 | Isolate* isolate = obj->GetIsolate(); |
| 362 | Handle<String> str = isolate->factory()->LookupAsciiSymbol(name); |
| 363 | CALL_HEAP_FUNCTION(isolate, obj->GetProperty(*str), Object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | |
| 367 | Handle<Object> GetProperty(Handle<Object> obj, |
| 368 | Handle<Object> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 369 | Isolate* isolate = Isolate::Current(); |
| 370 | CALL_HEAP_FUNCTION(isolate, |
| 371 | Runtime::GetObjectProperty(isolate, obj, key), Object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 375 | Handle<Object> GetProperty(Handle<JSReceiver> obj, |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 376 | Handle<String> name, |
| 377 | LookupResult* result) { |
| 378 | PropertyAttributes attributes; |
| 379 | Isolate* isolate = Isolate::Current(); |
| 380 | CALL_HEAP_FUNCTION(isolate, |
| 381 | obj->GetProperty(*obj, result, *name, &attributes), |
| 382 | Object); |
| 383 | } |
| 384 | |
| 385 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 386 | Handle<Object> GetElement(Handle<Object> obj, |
| 387 | uint32_t index) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 388 | Isolate* isolate = Isolate::Current(); |
| 389 | CALL_HEAP_FUNCTION(isolate, Runtime::GetElement(obj, index), Object); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 393 | Handle<Object> GetPropertyWithInterceptor(Handle<JSObject> receiver, |
| 394 | Handle<JSObject> holder, |
| 395 | Handle<String> name, |
| 396 | PropertyAttributes* attributes) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 397 | Isolate* isolate = receiver->GetIsolate(); |
| 398 | CALL_HEAP_FUNCTION(isolate, |
| 399 | holder->GetPropertyWithInterceptor(*receiver, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 400 | *name, |
| 401 | attributes), |
| 402 | Object); |
| 403 | } |
| 404 | |
| 405 | |
| 406 | Handle<Object> GetPrototype(Handle<Object> obj) { |
| 407 | Handle<Object> result(obj->GetPrototype()); |
| 408 | return result; |
| 409 | } |
| 410 | |
| 411 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 412 | Handle<Object> SetPrototype(Handle<JSObject> obj, Handle<Object> value) { |
| 413 | const bool skip_hidden_prototypes = false; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 414 | CALL_HEAP_FUNCTION(obj->GetIsolate(), |
| 415 | obj->SetPrototype(*value, skip_hidden_prototypes), Object); |
| 416 | } |
| 417 | |
| 418 | |
| 419 | Handle<Object> PreventExtensions(Handle<JSObject> object) { |
| 420 | CALL_HEAP_FUNCTION(object->GetIsolate(), object->PreventExtensions(), Object); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 424 | Handle<Object> GetHiddenProperties(Handle<JSObject> obj, |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 425 | JSObject::HiddenPropertiesFlag flag) { |
| 426 | CALL_HEAP_FUNCTION(obj->GetIsolate(), |
| 427 | obj->GetHiddenProperties(flag), |
| 428 | Object); |
| 429 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 430 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 431 | |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 432 | int GetIdentityHash(Handle<JSObject> obj) { |
| 433 | CALL_AND_RETRY(obj->GetIsolate(), |
| 434 | obj->GetIdentityHash(JSObject::ALLOW_CREATION), |
| 435 | return Smi::cast(__object__)->value(), |
| 436 | return 0); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 440 | Handle<Object> DeleteElement(Handle<JSObject> obj, |
| 441 | uint32_t index) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 442 | CALL_HEAP_FUNCTION(obj->GetIsolate(), |
| 443 | obj->DeleteElement(index, JSObject::NORMAL_DELETION), |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 444 | Object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | |
| 448 | Handle<Object> DeleteProperty(Handle<JSObject> obj, |
| 449 | Handle<String> prop) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 450 | CALL_HEAP_FUNCTION(obj->GetIsolate(), |
| 451 | obj->DeleteProperty(*prop, JSObject::NORMAL_DELETION), |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 452 | Object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 456 | Handle<Object> LookupSingleCharacterStringFromCode(uint32_t index) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 457 | Isolate* isolate = Isolate::Current(); |
| 458 | CALL_HEAP_FUNCTION( |
| 459 | isolate, |
| 460 | isolate->heap()->LookupSingleCharacterStringFromCode(index), Object); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 464 | Handle<String> SubString(Handle<String> str, |
| 465 | int start, |
| 466 | int end, |
| 467 | PretenureFlag pretenure) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 468 | CALL_HEAP_FUNCTION(str->GetIsolate(), |
| 469 | str->SubString(start, end, pretenure), String); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | |
| 473 | Handle<Object> SetElement(Handle<JSObject> object, |
| 474 | uint32_t index, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 475 | Handle<Object> value, |
| 476 | StrictModeFlag strict_mode) { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 477 | if (object->HasExternalArrayElements()) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 478 | if (!value->IsSmi() && !value->IsHeapNumber() && !value->IsUndefined()) { |
| 479 | bool has_exception; |
| 480 | Handle<Object> number = Execution::ToNumber(value, &has_exception); |
| 481 | if (has_exception) return Handle<Object>(); |
| 482 | value = number; |
| 483 | } |
| 484 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 485 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 486 | object->SetElement(index, *value, strict_mode, true), |
| 487 | Object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 491 | Handle<Object> SetOwnElement(Handle<JSObject> object, |
| 492 | uint32_t index, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 493 | Handle<Object> value, |
| 494 | StrictModeFlag strict_mode) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 495 | ASSERT(!object->HasExternalArrayElements()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 496 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 497 | object->SetElement(index, *value, strict_mode, false), |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 498 | Object); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 502 | Handle<JSObject> Copy(Handle<JSObject> obj) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 503 | Isolate* isolate = obj->GetIsolate(); |
| 504 | CALL_HEAP_FUNCTION(isolate, |
| 505 | isolate->heap()->CopyJSObject(*obj), JSObject); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 509 | Handle<Object> SetAccessor(Handle<JSObject> obj, Handle<AccessorInfo> info) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 510 | CALL_HEAP_FUNCTION(obj->GetIsolate(), obj->DefineAccessor(*info), Object); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 514 | // Wrappers for scripts are kept alive and cached in weak global |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 515 | // 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] | 516 | // they are used. When they are not used anymore, the garbage |
| 517 | // collector will call the weak callback on the global handle |
| 518 | // associated with the wrapper and get rid of both the wrapper and the |
| 519 | // handle. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 520 | static void ClearWrapperCache(Persistent<v8::Value> handle, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 521 | Handle<Object> cache = Utils::OpenHandle(*handle); |
| 522 | JSValue* wrapper = JSValue::cast(*cache); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 523 | Foreign* foreign = Script::cast(wrapper->value())->wrapper(); |
| 524 | ASSERT(foreign->address() == reinterpret_cast<Address>(cache.location())); |
| 525 | foreign->set_address(0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 526 | Isolate* isolate = Isolate::Current(); |
| 527 | isolate->global_handles()->Destroy(cache.location()); |
| 528 | isolate->counters()->script_wrappers()->Decrement(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | |
| 532 | Handle<JSValue> GetScriptWrapper(Handle<Script> script) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 533 | if (script->wrapper()->address() != NULL) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 534 | // Return the script wrapper directly from the cache. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 535 | return Handle<JSValue>( |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 536 | reinterpret_cast<JSValue**>(script->wrapper()->address())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 537 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 538 | Isolate* isolate = Isolate::Current(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 539 | // Construct a new script wrapper. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 540 | isolate->counters()->script_wrappers()->Increment(); |
| 541 | Handle<JSFunction> constructor = isolate->script_function(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 542 | Handle<JSValue> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 543 | Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 544 | result->set_value(*script); |
| 545 | |
| 546 | // Create a new weak global handle and use it to cache the wrapper |
| 547 | // for future use. The cache will automatically be cleared by the |
| 548 | // garbage collector when it is not used anymore. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 549 | Handle<Object> handle = isolate->global_handles()->Create(*result); |
| 550 | isolate->global_handles()->MakeWeak(handle.location(), NULL, |
| 551 | &ClearWrapperCache); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 552 | script->wrapper()->set_address(reinterpret_cast<Address>(handle.location())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 553 | return result; |
| 554 | } |
| 555 | |
| 556 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 557 | // Init line_ends array with code positions of line ends inside script |
| 558 | // source. |
| 559 | void InitScriptLineEnds(Handle<Script> script) { |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 560 | if (!script->line_ends()->IsUndefined()) return; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 561 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 562 | Isolate* isolate = script->GetIsolate(); |
| 563 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 564 | if (!script->source()->IsString()) { |
| 565 | ASSERT(script->source()->IsUndefined()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 566 | Handle<FixedArray> empty = isolate->factory()->NewFixedArray(0); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 567 | script->set_line_ends(*empty); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 568 | ASSERT(script->line_ends()->IsFixedArray()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 569 | return; |
| 570 | } |
| 571 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 572 | Handle<String> src(String::cast(script->source()), isolate); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 573 | |
| 574 | Handle<FixedArray> array = CalculateLineEnds(src, true); |
| 575 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 576 | if (*array != isolate->heap()->empty_fixed_array()) { |
| 577 | array->set_map(isolate->heap()->fixed_cow_array_map()); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 578 | } |
| 579 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 580 | script->set_line_ends(*array); |
| 581 | ASSERT(script->line_ends()->IsFixedArray()); |
| 582 | } |
| 583 | |
| 584 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 585 | template <typename SourceChar> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 586 | static void CalculateLineEnds(Isolate* isolate, |
| 587 | List<int>* line_ends, |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 588 | Vector<const SourceChar> src, |
| 589 | bool with_last_line) { |
| 590 | const int src_len = src.length(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 591 | StringSearch<char, SourceChar> search(isolate, CStrVector("\n")); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 592 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 593 | // Find and record line ends. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 594 | int position = 0; |
| 595 | while (position != -1 && position < src_len) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 596 | position = search.Search(src, position); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 597 | if (position != -1) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 598 | line_ends->Add(position); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 599 | position++; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 600 | } else if (with_last_line) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 601 | // 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] | 602 | line_ends->Add(src_len); |
| 603 | return; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 604 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 605 | } |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 606 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 607 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 608 | |
| 609 | Handle<FixedArray> CalculateLineEnds(Handle<String> src, |
| 610 | bool with_last_line) { |
| 611 | src = FlattenGetString(src); |
| 612 | // Rough estimate of line count based on a roughly estimated average |
| 613 | // length of (unpacked) code. |
| 614 | int line_count_estimate = src->length() >> 4; |
| 615 | List<int> line_ends(line_count_estimate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 616 | Isolate* isolate = src->GetIsolate(); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 617 | { |
| 618 | AssertNoAllocation no_heap_allocation; // ensure vectors stay valid. |
| 619 | // Dispatch on type of strings. |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 620 | String::FlatContent content = src->GetFlatContent(); |
| 621 | ASSERT(content.IsFlat()); |
| 622 | if (content.IsAscii()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 623 | CalculateLineEnds(isolate, |
| 624 | &line_ends, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 625 | content.ToAsciiVector(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 626 | with_last_line); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 627 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 628 | CalculateLineEnds(isolate, |
| 629 | &line_ends, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 630 | content.ToUC16Vector(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 631 | with_last_line); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | int line_count = line_ends.length(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 635 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(line_count); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 636 | for (int i = 0; i < line_count; i++) { |
| 637 | array->set(i, Smi::FromInt(line_ends[i])); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 638 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 639 | return array; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | |
| 643 | // Convert code position into line number. |
| 644 | int GetScriptLineNumber(Handle<Script> script, int code_pos) { |
| 645 | InitScriptLineEnds(script); |
| 646 | AssertNoAllocation no_allocation; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 647 | FixedArray* line_ends_array = FixedArray::cast(script->line_ends()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 648 | const int line_ends_len = line_ends_array->length(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 649 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 650 | if (!line_ends_len) return -1; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 651 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 652 | if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 653 | return script->line_offset()->value(); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 654 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 655 | |
| 656 | int left = 0; |
| 657 | int right = line_ends_len; |
| 658 | while (int half = (right - left) / 2) { |
| 659 | if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) { |
| 660 | right -= half; |
| 661 | } else { |
| 662 | left += half; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 663 | } |
| 664 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 665 | return right + script->line_offset()->value(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 669 | int GetScriptLineNumberSafe(Handle<Script> script, int code_pos) { |
| 670 | AssertNoAllocation no_allocation; |
| 671 | if (!script->line_ends()->IsUndefined()) { |
| 672 | return GetScriptLineNumber(script, code_pos); |
| 673 | } |
| 674 | // Slow mode: we do not have line_ends. We have to iterate through source. |
| 675 | if (!script->source()->IsString()) { |
| 676 | return -1; |
| 677 | } |
| 678 | String* source = String::cast(script->source()); |
| 679 | int line = 0; |
| 680 | int len = source->length(); |
| 681 | for (int pos = 0; pos < len; pos++) { |
| 682 | if (pos == code_pos) { |
| 683 | break; |
| 684 | } |
| 685 | if (source->Get(pos) == '\n') { |
| 686 | line++; |
| 687 | } |
| 688 | } |
| 689 | return line; |
| 690 | } |
| 691 | |
| 692 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 693 | void CustomArguments::IterateInstance(ObjectVisitor* v) { |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 694 | v->VisitPointers(values_, values_ + ARRAY_SIZE(values_)); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 698 | // Compute the property keys from the interceptor. |
| 699 | v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSObject> receiver, |
| 700 | Handle<JSObject> object) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 701 | Isolate* isolate = receiver->GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 702 | Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 703 | CustomArguments args(isolate, interceptor->data(), *receiver, *object); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 704 | v8::AccessorInfo info(args.end()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 705 | v8::Handle<v8::Array> result; |
| 706 | if (!interceptor->enumerator()->IsUndefined()) { |
| 707 | v8::NamedPropertyEnumerator enum_fun = |
| 708 | v8::ToCData<v8::NamedPropertyEnumerator>(interceptor->enumerator()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 709 | LOG(isolate, ApiObjectAccess("interceptor-named-enum", *object)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 710 | { |
| 711 | // Leaving JavaScript. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 712 | VMState state(isolate, EXTERNAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 713 | result = enum_fun(info); |
| 714 | } |
| 715 | } |
| 716 | return result; |
| 717 | } |
| 718 | |
| 719 | |
| 720 | // Compute the element keys from the interceptor. |
| 721 | v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSObject> receiver, |
| 722 | Handle<JSObject> object) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 723 | Isolate* isolate = receiver->GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 724 | Handle<InterceptorInfo> interceptor(object->GetIndexedInterceptor()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 725 | CustomArguments args(isolate, interceptor->data(), *receiver, *object); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 726 | v8::AccessorInfo info(args.end()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 727 | v8::Handle<v8::Array> result; |
| 728 | if (!interceptor->enumerator()->IsUndefined()) { |
| 729 | v8::IndexedPropertyEnumerator enum_fun = |
| 730 | v8::ToCData<v8::IndexedPropertyEnumerator>(interceptor->enumerator()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 731 | LOG(isolate, ApiObjectAccess("interceptor-indexed-enum", *object)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 732 | { |
| 733 | // Leaving JavaScript. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 734 | VMState state(isolate, EXTERNAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 735 | result = enum_fun(info); |
| 736 | } |
| 737 | } |
| 738 | return result; |
| 739 | } |
| 740 | |
| 741 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 742 | static bool ContainsOnlyValidKeys(Handle<FixedArray> array) { |
| 743 | int len = array->length(); |
| 744 | for (int i = 0; i < len; i++) { |
| 745 | Object* e = array->get(i); |
| 746 | if (!(e->IsString() || e->IsNumber())) return false; |
| 747 | } |
| 748 | return true; |
| 749 | } |
| 750 | |
| 751 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 752 | Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSObject> object, |
| 753 | KeyCollectionType type) { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 754 | USE(ContainsOnlyValidKeys); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 755 | Isolate* isolate = object->GetIsolate(); |
| 756 | Handle<FixedArray> content = isolate->factory()->empty_fixed_array(); |
| 757 | Handle<JSObject> arguments_boilerplate = Handle<JSObject>( |
| 758 | isolate->context()->global_context()->arguments_boilerplate(), |
| 759 | isolate); |
| 760 | Handle<JSFunction> arguments_function = Handle<JSFunction>( |
| 761 | JSFunction::cast(arguments_boilerplate->map()->constructor()), |
| 762 | isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 763 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 764 | // Only collect keys if access is permitted. |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 765 | for (Handle<Object> p = object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 766 | *p != isolate->heap()->null_value(); |
| 767 | p = Handle<Object>(p->GetPrototype(), isolate)) { |
| 768 | Handle<JSObject> current(JSObject::cast(*p), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 769 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 770 | // Check access rights if required. |
| 771 | if (current->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 772 | !isolate->MayNamedAccess(*current, |
| 773 | isolate->heap()->undefined_value(), |
| 774 | v8::ACCESS_KEYS)) { |
| 775 | isolate->ReportFailedAccessCheck(*current, v8::ACCESS_KEYS); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 776 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 777 | } |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 778 | |
| 779 | // Compute the element keys. |
| 780 | Handle<FixedArray> element_keys = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 781 | isolate->factory()->NewFixedArray(current->NumberOfEnumElements()); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 782 | current->GetEnumElementKeys(*element_keys); |
| 783 | content = UnionOfKeys(content, element_keys); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 784 | ASSERT(ContainsOnlyValidKeys(content)); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 785 | |
| 786 | // Add the element keys from the interceptor. |
| 787 | if (current->HasIndexedInterceptor()) { |
| 788 | v8::Handle<v8::Array> result = |
| 789 | GetKeysForIndexedInterceptor(object, current); |
| 790 | if (!result.IsEmpty()) |
| 791 | content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result)); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 792 | ASSERT(ContainsOnlyValidKeys(content)); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 793 | } |
| 794 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 795 | // We can cache the computed property keys if access checks are |
| 796 | // not needed and no interceptors are involved. |
| 797 | // |
| 798 | // We do not use the cache if the object has elements and |
| 799 | // therefore it does not make sense to cache the property names |
| 800 | // for arguments objects. Arguments objects will always have |
| 801 | // elements. |
fschneider@chromium.org | ed78ffd | 2010-07-21 11:05:19 +0000 | [diff] [blame] | 802 | // Wrapped strings have elements, but don't have an elements |
| 803 | // array or dictionary. So the fast inline test for whether to |
| 804 | // 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] | 805 | bool cache_enum_keys = |
| 806 | ((current->map()->constructor() != *arguments_function) && |
fschneider@chromium.org | ed78ffd | 2010-07-21 11:05:19 +0000 | [diff] [blame] | 807 | !current->IsJSValue() && |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 808 | !current->IsAccessCheckNeeded() && |
| 809 | !current->HasNamedInterceptor() && |
| 810 | !current->HasIndexedInterceptor()); |
| 811 | // Compute the property keys and cache them if possible. |
| 812 | content = |
| 813 | UnionOfKeys(content, GetEnumPropertyKeys(current, cache_enum_keys)); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 814 | ASSERT(ContainsOnlyValidKeys(content)); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 815 | |
| 816 | // Add the property keys from the interceptor. |
| 817 | if (current->HasNamedInterceptor()) { |
| 818 | v8::Handle<v8::Array> result = |
| 819 | GetKeysForNamedInterceptor(object, current); |
| 820 | if (!result.IsEmpty()) |
| 821 | content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result)); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 822 | ASSERT(ContainsOnlyValidKeys(content)); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | // If we only want local properties we bail out after the first |
| 826 | // iteration. |
| 827 | if (type == LOCAL_ONLY) |
| 828 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 829 | } |
| 830 | return content; |
| 831 | } |
| 832 | |
| 833 | |
| 834 | Handle<JSArray> GetKeysFor(Handle<JSObject> object) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 835 | Isolate* isolate = object->GetIsolate(); |
| 836 | isolate->counters()->for_in()->Increment(); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 837 | Handle<FixedArray> elements = GetKeysInFixedArrayFor(object, |
| 838 | INCLUDE_PROTOS); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 839 | return isolate->factory()->NewJSArrayWithElements(elements); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 843 | Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object, |
| 844 | bool cache_result) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 845 | int index = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 846 | Isolate* isolate = object->GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 847 | if (object->HasFastProperties()) { |
| 848 | if (object->map()->instance_descriptors()->HasEnumCache()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 849 | isolate->counters()->enum_cache_hits()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 850 | DescriptorArray* desc = object->map()->instance_descriptors(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 851 | return Handle<FixedArray>(FixedArray::cast(desc->GetEnumCache()), |
| 852 | isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 853 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 854 | isolate->counters()->enum_cache_misses()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 855 | int num_enum = object->NumberOfEnumProperties(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 856 | Handle<FixedArray> storage = isolate->factory()->NewFixedArray(num_enum); |
| 857 | Handle<FixedArray> sort_array = isolate->factory()->NewFixedArray(num_enum); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 858 | Handle<DescriptorArray> descs = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 859 | Handle<DescriptorArray>(object->map()->instance_descriptors(), isolate); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 860 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 861 | if (descs->IsProperty(i) && !descs->IsDontEnum(i)) { |
| 862 | (*storage)->set(index, descs->GetKey(i)); |
| 863 | PropertyDetails details(descs->GetDetails(i)); |
| 864 | (*sort_array)->set(index, Smi::FromInt(details.index())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 865 | index++; |
| 866 | } |
| 867 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 868 | (*storage)->SortPairs(*sort_array, sort_array->length()); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 869 | if (cache_result) { |
| 870 | Handle<FixedArray> bridge_storage = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 871 | isolate->factory()->NewFixedArray( |
| 872 | DescriptorArray::kEnumCacheBridgeLength); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 873 | DescriptorArray* desc = object->map()->instance_descriptors(); |
| 874 | desc->SetEnumCache(*bridge_storage, *storage); |
| 875 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 876 | ASSERT(storage->length() == index); |
| 877 | return storage; |
| 878 | } else { |
| 879 | int num_enum = object->NumberOfEnumProperties(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 880 | Handle<FixedArray> storage = isolate->factory()->NewFixedArray(num_enum); |
| 881 | Handle<FixedArray> sort_array = isolate->factory()->NewFixedArray(num_enum); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 882 | object->property_dictionary()->CopyEnumKeysTo(*storage, *sort_array); |
| 883 | return storage; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 888 | Handle<ObjectHashTable> PutIntoObjectHashTable(Handle<ObjectHashTable> table, |
| 889 | Handle<JSObject> key, |
| 890 | Handle<Object> value) { |
| 891 | CALL_HEAP_FUNCTION(table->GetIsolate(), |
| 892 | table->Put(*key, *value), |
| 893 | ObjectHashTable); |
| 894 | } |
| 895 | |
| 896 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 897 | bool EnsureCompiled(Handle<SharedFunctionInfo> shared, |
| 898 | ClearExceptionFlag flag) { |
| 899 | return shared->is_compiled() || CompileLazyShared(shared, flag); |
| 900 | } |
| 901 | |
| 902 | |
| 903 | static bool CompileLazyHelper(CompilationInfo* info, |
| 904 | ClearExceptionFlag flag) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 905 | // Compile the source information to a code object. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 906 | ASSERT(info->IsOptimizing() || !info->shared_info()->is_compiled()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 907 | ASSERT(!info->isolate()->has_pending_exception()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 908 | bool result = Compiler::CompileLazy(info); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 909 | ASSERT(result != Isolate::Current()->has_pending_exception()); |
| 910 | if (!result && flag == CLEAR_EXCEPTION) { |
| 911 | info->isolate()->clear_pending_exception(); |
| 912 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 913 | return result; |
| 914 | } |
| 915 | |
| 916 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 917 | bool CompileLazyShared(Handle<SharedFunctionInfo> shared, |
| 918 | ClearExceptionFlag flag) { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 919 | CompilationInfo info(shared); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 920 | return CompileLazyHelper(&info, flag); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 924 | static bool CompileLazyFunction(Handle<JSFunction> function, |
| 925 | ClearExceptionFlag flag, |
| 926 | InLoopFlag in_loop_flag) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 927 | bool result = true; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 928 | if (function->shared()->is_compiled()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 929 | function->ReplaceCode(function->shared()->code()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 930 | function->shared()->set_code_age(0); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 931 | } else { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 932 | CompilationInfo info(function); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 933 | if (in_loop_flag == IN_LOOP) info.MarkAsInLoop(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 934 | result = CompileLazyHelper(&info, flag); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 935 | ASSERT(!result || function->is_compiled()); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 936 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 937 | return result; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 938 | } |
| 939 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 940 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 941 | bool CompileLazy(Handle<JSFunction> function, |
| 942 | ClearExceptionFlag flag) { |
| 943 | return CompileLazyFunction(function, flag, NOT_IN_LOOP); |
| 944 | } |
| 945 | |
| 946 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 947 | bool CompileLazyInLoop(Handle<JSFunction> function, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 948 | ClearExceptionFlag flag) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 949 | return CompileLazyFunction(function, flag, IN_LOOP); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 953 | bool CompileOptimized(Handle<JSFunction> function, |
| 954 | int osr_ast_id, |
| 955 | ClearExceptionFlag flag) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 956 | CompilationInfo info(function); |
| 957 | info.SetOptimizing(osr_ast_id); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 958 | return CompileLazyHelper(&info, flag); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 959 | } |
| 960 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 961 | } } // namespace v8::internal |