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