Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
| 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" |
| 32 | #include "arguments.h" |
| 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" |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 40 | #include "stub-cache.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 41 | |
| 42 | namespace v8 { |
| 43 | namespace internal { |
| 44 | |
| 45 | |
| 46 | v8::ImplementationUtilities::HandleScopeData HandleScope::current_ = |
| 47 | { -1, NULL, NULL }; |
| 48 | |
| 49 | |
| 50 | int HandleScope::NumberOfHandles() { |
| 51 | int n = HandleScopeImplementer::instance()->blocks()->length(); |
| 52 | if (n == 0) return 0; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 53 | return ((n - 1) * kHandleBlockSize) + static_cast<int>( |
| 54 | (current_.next - HandleScopeImplementer::instance()->blocks()->last())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | |
| 58 | Object** HandleScope::Extend() { |
| 59 | Object** result = current_.next; |
| 60 | |
| 61 | ASSERT(result == current_.limit); |
| 62 | // Make sure there's at least one scope on the stack and that the |
| 63 | // top of the scope stack isn't a barrier. |
| 64 | if (current_.extensions < 0) { |
| 65 | Utils::ReportApiFailure("v8::HandleScope::CreateHandle()", |
| 66 | "Cannot create a handle without a HandleScope"); |
| 67 | return NULL; |
| 68 | } |
| 69 | HandleScopeImplementer* impl = HandleScopeImplementer::instance(); |
| 70 | // If there's more room in the last block, we use that. This is used |
| 71 | // for fast creation of scopes after scope barriers. |
| 72 | if (!impl->blocks()->is_empty()) { |
| 73 | Object** limit = &impl->blocks()->last()[kHandleBlockSize]; |
| 74 | if (current_.limit != limit) { |
| 75 | current_.limit = limit; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // If we still haven't found a slot for the handle, we extend the |
| 80 | // current handle scope by allocating a new handle block. |
| 81 | if (result == current_.limit) { |
| 82 | // If there's a spare block, use it for growing the current scope. |
| 83 | result = impl->GetSpareOrNewBlock(); |
| 84 | // Add the extension to the global list of blocks, but count the |
| 85 | // extension as part of the current scope. |
| 86 | impl->blocks()->Add(result); |
| 87 | current_.extensions++; |
| 88 | current_.limit = &result[kHandleBlockSize]; |
| 89 | } |
| 90 | |
| 91 | return result; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | void HandleScope::DeleteExtensions() { |
| 96 | ASSERT(current_.extensions != 0); |
| 97 | HandleScopeImplementer::instance()->DeleteExtensions(current_.extensions); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | void HandleScope::ZapRange(Object** start, Object** end) { |
| 102 | if (start == NULL) return; |
| 103 | for (Object** p = start; p < end; p++) { |
| 104 | *reinterpret_cast<Address*>(p) = v8::internal::kHandleZapValue; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 109 | Address HandleScope::current_extensions_address() { |
| 110 | return reinterpret_cast<Address>(¤t_.extensions); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | Address HandleScope::current_next_address() { |
| 115 | return reinterpret_cast<Address>(¤t_.next); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | Address HandleScope::current_limit_address() { |
| 120 | return reinterpret_cast<Address>(¤t_.limit); |
| 121 | } |
| 122 | |
| 123 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 124 | Handle<FixedArray> AddKeysFromJSArray(Handle<FixedArray> content, |
| 125 | Handle<JSArray> array) { |
| 126 | CALL_HEAP_FUNCTION(content->AddKeysFromJSArray(*array), FixedArray); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | Handle<FixedArray> UnionOfKeys(Handle<FixedArray> first, |
| 131 | Handle<FixedArray> second) { |
| 132 | CALL_HEAP_FUNCTION(first->UnionOfKeys(*second), FixedArray); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | Handle<JSGlobalProxy> ReinitializeJSGlobalProxy( |
| 137 | Handle<JSFunction> constructor, |
| 138 | Handle<JSGlobalProxy> global) { |
| 139 | CALL_HEAP_FUNCTION(Heap::ReinitializeJSGlobalProxy(*constructor, *global), |
| 140 | JSGlobalProxy); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | void SetExpectedNofProperties(Handle<JSFunction> func, int nof) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 145 | // If objects constructed from this function exist then changing |
| 146 | // 'estimated_nof_properties' is dangerous since the previois value might |
| 147 | // have been compiled into the fast construct stub. More over, the inobject |
| 148 | // slack tracking logic might have adjusted the previous value, so even |
| 149 | // passing the same value is risky. |
| 150 | if (func->shared()->live_objects_may_exist()) return; |
| 151 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 152 | func->shared()->set_expected_nof_properties(nof); |
| 153 | if (func->has_initial_map()) { |
| 154 | Handle<Map> new_initial_map = |
| 155 | Factory::CopyMapDropTransitions(Handle<Map>(func->initial_map())); |
| 156 | new_initial_map->set_unused_property_fields(nof); |
| 157 | func->set_initial_map(*new_initial_map); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | |
| 162 | void SetPrototypeProperty(Handle<JSFunction> func, Handle<JSObject> value) { |
| 163 | CALL_HEAP_FUNCTION_VOID(func->SetPrototype(*value)); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | static int ExpectedNofPropertiesFromEstimate(int estimate) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 168 | // If no properties are added in the constructor, they are more likely |
| 169 | // to be added later. |
| 170 | if (estimate == 0) estimate = 2; |
| 171 | |
| 172 | // We do not shrink objects that go into a snapshot (yet), so we adjust |
| 173 | // the estimate conservatively. |
| 174 | if (Serializer::enabled()) return estimate + 2; |
| 175 | |
| 176 | // Inobject slack tracking will reclaim redundant inobject space later, |
| 177 | // so we can afford to adjust the estimate generously. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame^] | 178 | return estimate + 8; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
| 182 | void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared, |
| 183 | int estimate) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 184 | // See the comment in SetExpectedNofProperties. |
| 185 | if (shared->live_objects_may_exist()) return; |
| 186 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 187 | shared->set_expected_nof_properties( |
| 188 | ExpectedNofPropertiesFromEstimate(estimate)); |
| 189 | } |
| 190 | |
| 191 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 192 | void NormalizeProperties(Handle<JSObject> object, |
| 193 | PropertyNormalizationMode mode, |
| 194 | int expected_additional_properties) { |
| 195 | CALL_HEAP_FUNCTION_VOID(object->NormalizeProperties( |
| 196 | mode, |
| 197 | expected_additional_properties)); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | void NormalizeElements(Handle<JSObject> object) { |
| 202 | CALL_HEAP_FUNCTION_VOID(object->NormalizeElements()); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | void TransformToFastProperties(Handle<JSObject> object, |
| 207 | int unused_property_fields) { |
| 208 | CALL_HEAP_FUNCTION_VOID( |
| 209 | object->TransformToFastProperties(unused_property_fields)); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | void FlattenString(Handle<String> string) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 214 | CALL_HEAP_FUNCTION_VOID(string->TryFlatten()); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | |
| 218 | Handle<String> FlattenGetString(Handle<String> string) { |
| 219 | Handle<String> result; |
| 220 | CALL_AND_RETRY(string->TryFlatten(), |
| 221 | { result = Handle<String>(String::cast(__object__)); |
| 222 | break; }, |
| 223 | return Handle<String>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 224 | ASSERT(string->IsFlat()); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 225 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | |
| 229 | Handle<Object> SetPrototype(Handle<JSFunction> function, |
| 230 | Handle<Object> prototype) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 231 | ASSERT(function->should_have_prototype()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 232 | CALL_HEAP_FUNCTION(Accessors::FunctionSetPrototype(*function, |
| 233 | *prototype, |
| 234 | NULL), |
| 235 | Object); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | Handle<Object> SetProperty(Handle<JSObject> object, |
| 240 | Handle<String> key, |
| 241 | Handle<Object> value, |
| 242 | PropertyAttributes attributes) { |
| 243 | CALL_HEAP_FUNCTION(object->SetProperty(*key, *value, attributes), Object); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | Handle<Object> SetProperty(Handle<Object> object, |
| 248 | Handle<Object> key, |
| 249 | Handle<Object> value, |
| 250 | PropertyAttributes attributes) { |
| 251 | CALL_HEAP_FUNCTION( |
| 252 | Runtime::SetObjectProperty(object, key, value, attributes), Object); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | Handle<Object> ForceSetProperty(Handle<JSObject> object, |
| 257 | Handle<Object> key, |
| 258 | Handle<Object> value, |
| 259 | PropertyAttributes attributes) { |
| 260 | CALL_HEAP_FUNCTION( |
| 261 | Runtime::ForceSetObjectProperty(object, key, value, attributes), Object); |
| 262 | } |
| 263 | |
| 264 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 265 | Handle<Object> SetNormalizedProperty(Handle<JSObject> object, |
| 266 | Handle<String> key, |
| 267 | Handle<Object> value, |
| 268 | PropertyDetails details) { |
| 269 | CALL_HEAP_FUNCTION(object->SetNormalizedProperty(*key, *value, details), |
| 270 | Object); |
| 271 | } |
| 272 | |
| 273 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 274 | Handle<Object> ForceDeleteProperty(Handle<JSObject> object, |
| 275 | Handle<Object> key) { |
| 276 | CALL_HEAP_FUNCTION(Runtime::ForceDeleteObjectProperty(object, key), Object); |
| 277 | } |
| 278 | |
| 279 | |
| 280 | Handle<Object> IgnoreAttributesAndSetLocalProperty( |
| 281 | Handle<JSObject> object, |
| 282 | Handle<String> key, |
| 283 | Handle<Object> value, |
| 284 | PropertyAttributes attributes) { |
| 285 | CALL_HEAP_FUNCTION(object-> |
| 286 | IgnoreAttributesAndSetLocalProperty(*key, *value, attributes), Object); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | Handle<Object> SetPropertyWithInterceptor(Handle<JSObject> object, |
| 291 | Handle<String> key, |
| 292 | Handle<Object> value, |
| 293 | PropertyAttributes attributes) { |
| 294 | CALL_HEAP_FUNCTION(object->SetPropertyWithInterceptor(*key, |
| 295 | *value, |
| 296 | attributes), |
| 297 | Object); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | Handle<Object> GetProperty(Handle<JSObject> obj, |
| 302 | const char* name) { |
| 303 | Handle<String> str = Factory::LookupAsciiSymbol(name); |
| 304 | CALL_HEAP_FUNCTION(obj->GetProperty(*str), Object); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | Handle<Object> GetProperty(Handle<Object> obj, |
| 309 | Handle<Object> key) { |
| 310 | CALL_HEAP_FUNCTION(Runtime::GetObjectProperty(obj, key), Object); |
| 311 | } |
| 312 | |
| 313 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 314 | Handle<Object> GetElement(Handle<Object> obj, |
| 315 | uint32_t index) { |
| 316 | CALL_HEAP_FUNCTION(Runtime::GetElement(obj, index), Object); |
| 317 | } |
| 318 | |
| 319 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 320 | Handle<Object> GetPropertyWithInterceptor(Handle<JSObject> receiver, |
| 321 | Handle<JSObject> holder, |
| 322 | Handle<String> name, |
| 323 | PropertyAttributes* attributes) { |
| 324 | CALL_HEAP_FUNCTION(holder->GetPropertyWithInterceptor(*receiver, |
| 325 | *name, |
| 326 | attributes), |
| 327 | Object); |
| 328 | } |
| 329 | |
| 330 | |
| 331 | Handle<Object> GetPrototype(Handle<Object> obj) { |
| 332 | Handle<Object> result(obj->GetPrototype()); |
| 333 | return result; |
| 334 | } |
| 335 | |
| 336 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 337 | Handle<Object> SetPrototype(Handle<JSObject> obj, Handle<Object> value) { |
| 338 | const bool skip_hidden_prototypes = false; |
| 339 | CALL_HEAP_FUNCTION(obj->SetPrototype(*value, skip_hidden_prototypes), Object); |
| 340 | } |
| 341 | |
| 342 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 343 | Handle<Object> GetHiddenProperties(Handle<JSObject> obj, |
| 344 | bool create_if_needed) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 345 | Object* holder = obj->BypassGlobalProxy(); |
| 346 | if (holder->IsUndefined()) return Factory::undefined_value(); |
| 347 | obj = Handle<JSObject>(JSObject::cast(holder)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 348 | |
| 349 | if (obj->HasFastProperties()) { |
| 350 | // If the object has fast properties, check whether the first slot |
| 351 | // in the descriptor array matches the hidden symbol. Since the |
| 352 | // hidden symbols hash code is zero (and no other string has hash |
| 353 | // code zero) it will always occupy the first entry if present. |
| 354 | DescriptorArray* descriptors = obj->map()->instance_descriptors(); |
| 355 | if ((descriptors->number_of_descriptors() > 0) && |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 356 | (descriptors->GetKey(0) == Heap::hidden_symbol()) && |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 357 | descriptors->IsProperty(0)) { |
| 358 | ASSERT(descriptors->GetType(0) == FIELD); |
| 359 | return Handle<Object>(obj->FastPropertyAt(descriptors->GetFieldIndex(0))); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // Only attempt to find the hidden properties in the local object and not |
| 364 | // in the prototype chain. Note that HasLocalProperty() can cause a GC in |
| 365 | // the general case in the presence of interceptors. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 366 | if (!obj->HasHiddenPropertiesObject()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 367 | // Hidden properties object not found. Allocate a new hidden properties |
| 368 | // object if requested. Otherwise return the undefined value. |
| 369 | if (create_if_needed) { |
| 370 | Handle<Object> hidden_obj = Factory::NewJSObject(Top::object_function()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 371 | CALL_HEAP_FUNCTION(obj->SetHiddenPropertiesObject(*hidden_obj), Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 372 | } else { |
| 373 | return Factory::undefined_value(); |
| 374 | } |
| 375 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 376 | return Handle<Object>(obj->GetHiddenPropertiesObject()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | |
| 380 | Handle<Object> DeleteElement(Handle<JSObject> obj, |
| 381 | uint32_t index) { |
| 382 | CALL_HEAP_FUNCTION(obj->DeleteElement(index, JSObject::NORMAL_DELETION), |
| 383 | Object); |
| 384 | } |
| 385 | |
| 386 | |
| 387 | Handle<Object> DeleteProperty(Handle<JSObject> obj, |
| 388 | Handle<String> prop) { |
| 389 | CALL_HEAP_FUNCTION(obj->DeleteProperty(*prop, JSObject::NORMAL_DELETION), |
| 390 | Object); |
| 391 | } |
| 392 | |
| 393 | |
| 394 | Handle<Object> LookupSingleCharacterStringFromCode(uint32_t index) { |
| 395 | CALL_HEAP_FUNCTION(Heap::LookupSingleCharacterStringFromCode(index), Object); |
| 396 | } |
| 397 | |
| 398 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 399 | Handle<String> SubString(Handle<String> str, |
| 400 | int start, |
| 401 | int end, |
| 402 | PretenureFlag pretenure) { |
| 403 | CALL_HEAP_FUNCTION(str->SubString(start, end, pretenure), String); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | |
| 407 | Handle<Object> SetElement(Handle<JSObject> object, |
| 408 | uint32_t index, |
| 409 | Handle<Object> value) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 410 | if (object->HasPixelElements() || object->HasExternalArrayElements()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 411 | if (!value->IsSmi() && !value->IsHeapNumber() && !value->IsUndefined()) { |
| 412 | bool has_exception; |
| 413 | Handle<Object> number = Execution::ToNumber(value, &has_exception); |
| 414 | if (has_exception) return Handle<Object>(); |
| 415 | value = number; |
| 416 | } |
| 417 | } |
| 418 | CALL_HEAP_FUNCTION(object->SetElement(index, *value), Object); |
| 419 | } |
| 420 | |
| 421 | |
| 422 | Handle<JSObject> Copy(Handle<JSObject> obj) { |
| 423 | CALL_HEAP_FUNCTION(Heap::CopyJSObject(*obj), JSObject); |
| 424 | } |
| 425 | |
| 426 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 427 | Handle<Object> SetAccessor(Handle<JSObject> obj, Handle<AccessorInfo> info) { |
| 428 | CALL_HEAP_FUNCTION(obj->DefineAccessor(*info), Object); |
| 429 | } |
| 430 | |
| 431 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 432 | // Wrappers for scripts are kept alive and cached in weak global |
| 433 | // handles referred from proxy objects held by the scripts as long as |
| 434 | // they are used. When they are not used anymore, the garbage |
| 435 | // collector will call the weak callback on the global handle |
| 436 | // associated with the wrapper and get rid of both the wrapper and the |
| 437 | // handle. |
| 438 | static void ClearWrapperCache(Persistent<v8::Value> handle, void*) { |
| 439 | #ifdef ENABLE_HEAP_PROTECTION |
| 440 | // Weak reference callbacks are called as if from outside V8. We |
| 441 | // need to reeenter to unprotect the heap. |
| 442 | VMState state(OTHER); |
| 443 | #endif |
| 444 | Handle<Object> cache = Utils::OpenHandle(*handle); |
| 445 | JSValue* wrapper = JSValue::cast(*cache); |
| 446 | Proxy* proxy = Script::cast(wrapper->value())->wrapper(); |
| 447 | ASSERT(proxy->proxy() == reinterpret_cast<Address>(cache.location())); |
| 448 | proxy->set_proxy(0); |
| 449 | GlobalHandles::Destroy(cache.location()); |
| 450 | Counters::script_wrappers.Decrement(); |
| 451 | } |
| 452 | |
| 453 | |
| 454 | Handle<JSValue> GetScriptWrapper(Handle<Script> script) { |
| 455 | if (script->wrapper()->proxy() != NULL) { |
| 456 | // Return the script wrapper directly from the cache. |
| 457 | return Handle<JSValue>( |
| 458 | reinterpret_cast<JSValue**>(script->wrapper()->proxy())); |
| 459 | } |
| 460 | |
| 461 | // Construct a new script wrapper. |
| 462 | Counters::script_wrappers.Increment(); |
| 463 | Handle<JSFunction> constructor = Top::script_function(); |
| 464 | Handle<JSValue> result = |
| 465 | Handle<JSValue>::cast(Factory::NewJSObject(constructor)); |
| 466 | result->set_value(*script); |
| 467 | |
| 468 | // Create a new weak global handle and use it to cache the wrapper |
| 469 | // for future use. The cache will automatically be cleared by the |
| 470 | // garbage collector when it is not used anymore. |
| 471 | Handle<Object> handle = GlobalHandles::Create(*result); |
| 472 | GlobalHandles::MakeWeak(handle.location(), NULL, &ClearWrapperCache); |
| 473 | script->wrapper()->set_proxy(reinterpret_cast<Address>(handle.location())); |
| 474 | return result; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | // Init line_ends array with code positions of line ends inside script |
| 479 | // source. |
| 480 | void InitScriptLineEnds(Handle<Script> script) { |
| 481 | if (!script->line_ends()->IsUndefined()) return; |
| 482 | |
| 483 | if (!script->source()->IsString()) { |
| 484 | ASSERT(script->source()->IsUndefined()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 485 | Handle<FixedArray> empty = Factory::NewFixedArray(0); |
| 486 | script->set_line_ends(*empty); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 487 | ASSERT(script->line_ends()->IsFixedArray()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 488 | return; |
| 489 | } |
| 490 | |
| 491 | Handle<String> src(String::cast(script->source())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 492 | |
| 493 | Handle<FixedArray> array = CalculateLineEnds(src, true); |
| 494 | |
| 495 | script->set_line_ends(*array); |
| 496 | ASSERT(script->line_ends()->IsFixedArray()); |
| 497 | } |
| 498 | |
| 499 | |
| 500 | Handle<FixedArray> CalculateLineEnds(Handle<String> src, |
| 501 | bool with_imaginary_last_new_line) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 502 | const int src_len = src->length(); |
| 503 | Handle<String> new_line = Factory::NewStringFromAscii(CStrVector("\n")); |
| 504 | |
| 505 | // Pass 1: Identify line count. |
| 506 | int line_count = 0; |
| 507 | int position = 0; |
| 508 | while (position != -1 && position < src_len) { |
| 509 | position = Runtime::StringMatch(src, new_line, position); |
| 510 | if (position != -1) { |
| 511 | position++; |
| 512 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 513 | if (position != -1) { |
| 514 | line_count++; |
| 515 | } else if (with_imaginary_last_new_line) { |
| 516 | // Even if the last line misses a line end, it is counted. |
| 517 | line_count++; |
| 518 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | // Pass 2: Fill in line ends positions |
| 522 | Handle<FixedArray> array = Factory::NewFixedArray(line_count); |
| 523 | int array_index = 0; |
| 524 | position = 0; |
| 525 | while (position != -1 && position < src_len) { |
| 526 | position = Runtime::StringMatch(src, new_line, position); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 527 | if (position != -1) { |
| 528 | array->set(array_index++, Smi::FromInt(position++)); |
| 529 | } else if (with_imaginary_last_new_line) { |
| 530 | // If the script does not end with a line ending add the final end |
| 531 | // position as just past the last line ending. |
| 532 | array->set(array_index++, Smi::FromInt(src_len)); |
| 533 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 534 | } |
| 535 | ASSERT(array_index == line_count); |
| 536 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 537 | return array; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | |
| 541 | // Convert code position into line number. |
| 542 | int GetScriptLineNumber(Handle<Script> script, int code_pos) { |
| 543 | InitScriptLineEnds(script); |
| 544 | AssertNoAllocation no_allocation; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 545 | FixedArray* line_ends_array = FixedArray::cast(script->line_ends()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 546 | const int line_ends_len = line_ends_array->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 547 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 548 | if (!line_ends_len) |
| 549 | return -1; |
| 550 | |
| 551 | if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) |
| 552 | return script->line_offset()->value(); |
| 553 | |
| 554 | int left = 0; |
| 555 | int right = line_ends_len; |
| 556 | while (int half = (right - left) / 2) { |
| 557 | if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) { |
| 558 | right -= half; |
| 559 | } else { |
| 560 | left += half; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 563 | return right + script->line_offset()->value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 567 | int GetScriptLineNumberSafe(Handle<Script> script, int code_pos) { |
| 568 | AssertNoAllocation no_allocation; |
| 569 | if (!script->line_ends()->IsUndefined()) { |
| 570 | return GetScriptLineNumber(script, code_pos); |
| 571 | } |
| 572 | // Slow mode: we do not have line_ends. We have to iterate through source. |
| 573 | if (!script->source()->IsString()) { |
| 574 | return -1; |
| 575 | } |
| 576 | String* source = String::cast(script->source()); |
| 577 | int line = 0; |
| 578 | int len = source->length(); |
| 579 | for (int pos = 0; pos < len; pos++) { |
| 580 | if (pos == code_pos) { |
| 581 | break; |
| 582 | } |
| 583 | if (source->Get(pos) == '\n') { |
| 584 | line++; |
| 585 | } |
| 586 | } |
| 587 | return line; |
| 588 | } |
| 589 | |
| 590 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 591 | void CustomArguments::IterateInstance(ObjectVisitor* v) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 592 | v->VisitPointers(values_, values_ + ARRAY_SIZE(values_)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | |
| 596 | // Compute the property keys from the interceptor. |
| 597 | v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSObject> receiver, |
| 598 | Handle<JSObject> object) { |
| 599 | Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor()); |
| 600 | CustomArguments args(interceptor->data(), *receiver, *object); |
| 601 | v8::AccessorInfo info(args.end()); |
| 602 | v8::Handle<v8::Array> result; |
| 603 | if (!interceptor->enumerator()->IsUndefined()) { |
| 604 | v8::NamedPropertyEnumerator enum_fun = |
| 605 | v8::ToCData<v8::NamedPropertyEnumerator>(interceptor->enumerator()); |
| 606 | LOG(ApiObjectAccess("interceptor-named-enum", *object)); |
| 607 | { |
| 608 | // Leaving JavaScript. |
| 609 | VMState state(EXTERNAL); |
| 610 | result = enum_fun(info); |
| 611 | } |
| 612 | } |
| 613 | return result; |
| 614 | } |
| 615 | |
| 616 | |
| 617 | // Compute the element keys from the interceptor. |
| 618 | v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSObject> receiver, |
| 619 | Handle<JSObject> object) { |
| 620 | Handle<InterceptorInfo> interceptor(object->GetIndexedInterceptor()); |
| 621 | CustomArguments args(interceptor->data(), *receiver, *object); |
| 622 | v8::AccessorInfo info(args.end()); |
| 623 | v8::Handle<v8::Array> result; |
| 624 | if (!interceptor->enumerator()->IsUndefined()) { |
| 625 | v8::IndexedPropertyEnumerator enum_fun = |
| 626 | v8::ToCData<v8::IndexedPropertyEnumerator>(interceptor->enumerator()); |
| 627 | LOG(ApiObjectAccess("interceptor-indexed-enum", *object)); |
| 628 | { |
| 629 | // Leaving JavaScript. |
| 630 | VMState state(EXTERNAL); |
| 631 | result = enum_fun(info); |
| 632 | } |
| 633 | } |
| 634 | return result; |
| 635 | } |
| 636 | |
| 637 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame^] | 638 | static bool ContainsOnlyValidKeys(Handle<FixedArray> array) { |
| 639 | int len = array->length(); |
| 640 | for (int i = 0; i < len; i++) { |
| 641 | Object* e = array->get(i); |
| 642 | if (!(e->IsString() || e->IsNumber())) return false; |
| 643 | } |
| 644 | return true; |
| 645 | } |
| 646 | |
| 647 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 648 | Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSObject> object, |
| 649 | KeyCollectionType type) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame^] | 650 | USE(ContainsOnlyValidKeys); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 651 | Handle<FixedArray> content = Factory::empty_fixed_array(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 652 | Handle<JSObject> arguments_boilerplate = |
| 653 | Handle<JSObject>( |
| 654 | Top::context()->global_context()->arguments_boilerplate()); |
| 655 | Handle<JSFunction> arguments_function = |
| 656 | Handle<JSFunction>( |
| 657 | JSFunction::cast(arguments_boilerplate->map()->constructor())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 658 | |
| 659 | // Only collect keys if access is permitted. |
| 660 | for (Handle<Object> p = object; |
| 661 | *p != Heap::null_value(); |
| 662 | p = Handle<Object>(p->GetPrototype())) { |
| 663 | Handle<JSObject> current(JSObject::cast(*p)); |
| 664 | |
| 665 | // Check access rights if required. |
| 666 | if (current->IsAccessCheckNeeded() && |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 667 | !Top::MayNamedAccess(*current, Heap::undefined_value(), |
| 668 | v8::ACCESS_KEYS)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 669 | Top::ReportFailedAccessCheck(*current, v8::ACCESS_KEYS); |
| 670 | break; |
| 671 | } |
| 672 | |
| 673 | // Compute the element keys. |
| 674 | Handle<FixedArray> element_keys = |
| 675 | Factory::NewFixedArray(current->NumberOfEnumElements()); |
| 676 | current->GetEnumElementKeys(*element_keys); |
| 677 | content = UnionOfKeys(content, element_keys); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame^] | 678 | ASSERT(ContainsOnlyValidKeys(content)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 679 | |
| 680 | // Add the element keys from the interceptor. |
| 681 | if (current->HasIndexedInterceptor()) { |
| 682 | v8::Handle<v8::Array> result = |
| 683 | GetKeysForIndexedInterceptor(object, current); |
| 684 | if (!result.IsEmpty()) |
| 685 | content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result)); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame^] | 686 | ASSERT(ContainsOnlyValidKeys(content)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 689 | // We can cache the computed property keys if access checks are |
| 690 | // not needed and no interceptors are involved. |
| 691 | // |
| 692 | // We do not use the cache if the object has elements and |
| 693 | // therefore it does not make sense to cache the property names |
| 694 | // for arguments objects. Arguments objects will always have |
| 695 | // elements. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 696 | // Wrapped strings have elements, but don't have an elements |
| 697 | // array or dictionary. So the fast inline test for whether to |
| 698 | // use the cache says yes, so we should not create a cache. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 699 | bool cache_enum_keys = |
| 700 | ((current->map()->constructor() != *arguments_function) && |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 701 | !current->IsJSValue() && |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 702 | !current->IsAccessCheckNeeded() && |
| 703 | !current->HasNamedInterceptor() && |
| 704 | !current->HasIndexedInterceptor()); |
| 705 | // Compute the property keys and cache them if possible. |
| 706 | content = |
| 707 | UnionOfKeys(content, GetEnumPropertyKeys(current, cache_enum_keys)); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame^] | 708 | ASSERT(ContainsOnlyValidKeys(content)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 709 | |
| 710 | // Add the property keys from the interceptor. |
| 711 | if (current->HasNamedInterceptor()) { |
| 712 | v8::Handle<v8::Array> result = |
| 713 | GetKeysForNamedInterceptor(object, current); |
| 714 | if (!result.IsEmpty()) |
| 715 | content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result)); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame^] | 716 | ASSERT(ContainsOnlyValidKeys(content)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | // If we only want local properties we bail out after the first |
| 720 | // iteration. |
| 721 | if (type == LOCAL_ONLY) |
| 722 | break; |
| 723 | } |
| 724 | return content; |
| 725 | } |
| 726 | |
| 727 | |
| 728 | Handle<JSArray> GetKeysFor(Handle<JSObject> object) { |
| 729 | Counters::for_in.Increment(); |
| 730 | Handle<FixedArray> elements = GetKeysInFixedArrayFor(object, |
| 731 | INCLUDE_PROTOS); |
| 732 | return Factory::NewJSArrayWithElements(elements); |
| 733 | } |
| 734 | |
| 735 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 736 | Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object, |
| 737 | bool cache_result) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 738 | int index = 0; |
| 739 | if (object->HasFastProperties()) { |
| 740 | if (object->map()->instance_descriptors()->HasEnumCache()) { |
| 741 | Counters::enum_cache_hits.Increment(); |
| 742 | DescriptorArray* desc = object->map()->instance_descriptors(); |
| 743 | return Handle<FixedArray>(FixedArray::cast(desc->GetEnumCache())); |
| 744 | } |
| 745 | Counters::enum_cache_misses.Increment(); |
| 746 | int num_enum = object->NumberOfEnumProperties(); |
| 747 | Handle<FixedArray> storage = Factory::NewFixedArray(num_enum); |
| 748 | Handle<FixedArray> sort_array = Factory::NewFixedArray(num_enum); |
| 749 | Handle<DescriptorArray> descs = |
| 750 | Handle<DescriptorArray>(object->map()->instance_descriptors()); |
| 751 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 752 | if (descs->IsProperty(i) && !descs->IsDontEnum(i)) { |
| 753 | (*storage)->set(index, descs->GetKey(i)); |
| 754 | PropertyDetails details(descs->GetDetails(i)); |
| 755 | (*sort_array)->set(index, Smi::FromInt(details.index())); |
| 756 | index++; |
| 757 | } |
| 758 | } |
| 759 | (*storage)->SortPairs(*sort_array, sort_array->length()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 760 | if (cache_result) { |
| 761 | Handle<FixedArray> bridge_storage = |
| 762 | Factory::NewFixedArray(DescriptorArray::kEnumCacheBridgeLength); |
| 763 | DescriptorArray* desc = object->map()->instance_descriptors(); |
| 764 | desc->SetEnumCache(*bridge_storage, *storage); |
| 765 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 766 | ASSERT(storage->length() == index); |
| 767 | return storage; |
| 768 | } else { |
| 769 | int num_enum = object->NumberOfEnumProperties(); |
| 770 | Handle<FixedArray> storage = Factory::NewFixedArray(num_enum); |
| 771 | Handle<FixedArray> sort_array = Factory::NewFixedArray(num_enum); |
| 772 | object->property_dictionary()->CopyEnumKeysTo(*storage, *sort_array); |
| 773 | return storage; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 778 | bool EnsureCompiled(Handle<SharedFunctionInfo> shared, |
| 779 | ClearExceptionFlag flag) { |
| 780 | return shared->is_compiled() || CompileLazyShared(shared, flag); |
| 781 | } |
| 782 | |
| 783 | |
| 784 | static bool CompileLazyHelper(CompilationInfo* info, |
| 785 | ClearExceptionFlag flag) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 786 | // Compile the source information to a code object. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 787 | ASSERT(!info->shared_info()->is_compiled()); |
| 788 | bool result = Compiler::CompileLazy(info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 789 | ASSERT(result != Top::has_pending_exception()); |
| 790 | if (!result && flag == CLEAR_EXCEPTION) Top::clear_pending_exception(); |
| 791 | return result; |
| 792 | } |
| 793 | |
| 794 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 795 | bool CompileLazyShared(Handle<SharedFunctionInfo> shared, |
| 796 | ClearExceptionFlag flag) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 797 | CompilationInfo info(shared); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 798 | return CompileLazyHelper(&info, flag); |
| 799 | } |
| 800 | |
| 801 | |
| 802 | bool CompileLazy(Handle<JSFunction> function, |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 803 | ClearExceptionFlag flag) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 804 | if (function->shared()->is_compiled()) { |
| 805 | function->set_code(function->shared()->code()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame^] | 806 | PROFILE(FunctionCreateEvent(*function)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 807 | function->shared()->set_code_age(0); |
| 808 | return true; |
| 809 | } else { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame^] | 810 | CompilationInfo info(function); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 811 | bool result = CompileLazyHelper(&info, flag); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame^] | 812 | ASSERT(!result || function->is_compiled()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 813 | PROFILE(FunctionCreateEvent(*function)); |
| 814 | return result; |
| 815 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 819 | bool CompileLazyInLoop(Handle<JSFunction> function, |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 820 | ClearExceptionFlag flag) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 821 | if (function->shared()->is_compiled()) { |
| 822 | function->set_code(function->shared()->code()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame^] | 823 | PROFILE(FunctionCreateEvent(*function)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 824 | function->shared()->set_code_age(0); |
| 825 | return true; |
| 826 | } else { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame^] | 827 | CompilationInfo info(function); |
| 828 | info.MarkAsInLoop(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 829 | bool result = CompileLazyHelper(&info, flag); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame^] | 830 | ASSERT(!result || function->is_compiled()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 831 | PROFILE(FunctionCreateEvent(*function)); |
| 832 | return result; |
| 833 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 836 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 837 | OptimizedObjectForAddingMultipleProperties:: |
| 838 | OptimizedObjectForAddingMultipleProperties(Handle<JSObject> object, |
| 839 | int expected_additional_properties, |
| 840 | bool condition) { |
| 841 | object_ = object; |
| 842 | if (condition && object_->HasFastProperties()) { |
| 843 | // Normalize the properties of object to avoid n^2 behavior |
| 844 | // when extending the object multiple properties. Indicate the number of |
| 845 | // properties to be added. |
| 846 | unused_property_fields_ = object->map()->unused_property_fields(); |
| 847 | NormalizeProperties(object_, |
| 848 | KEEP_INOBJECT_PROPERTIES, |
| 849 | expected_additional_properties); |
| 850 | has_been_transformed_ = true; |
| 851 | |
| 852 | } else { |
| 853 | has_been_transformed_ = false; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 858 | OptimizedObjectForAddingMultipleProperties:: |
| 859 | ~OptimizedObjectForAddingMultipleProperties() { |
| 860 | // Reoptimize the object to allow fast property access. |
| 861 | if (has_been_transformed_) { |
| 862 | TransformToFastProperties(object_, unused_property_fields_); |
| 863 | } |
| 864 | } |
| 865 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 866 | } } // namespace v8::internal |