Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "accessors.h" |
| 31 | #include "api.h" |
| 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 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 217 | Handle<NumberDictionary> NormalizeElements(Handle<JSObject> object) { |
| 218 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 219 | object->NormalizeElements(), |
| 220 | NumberDictionary); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | |
| 224 | void TransformToFastProperties(Handle<JSObject> object, |
| 225 | int unused_property_fields) { |
| 226 | CALL_HEAP_FUNCTION_VOID( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 227 | object->GetIsolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 228 | object->TransformToFastProperties(unused_property_fields)); |
| 229 | } |
| 230 | |
| 231 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 232 | Handle<NumberDictionary> NumberDictionarySet( |
| 233 | Handle<NumberDictionary> dictionary, |
| 234 | uint32_t index, |
| 235 | Handle<Object> value, |
| 236 | PropertyDetails details) { |
| 237 | CALL_HEAP_FUNCTION(dictionary->GetIsolate(), |
| 238 | dictionary->Set(index, *value, details), |
| 239 | NumberDictionary); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 243 | void FlattenString(Handle<String> string) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 244 | CALL_HEAP_FUNCTION_VOID(string->GetIsolate(), string->TryFlatten()); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | |
| 248 | Handle<String> FlattenGetString(Handle<String> string) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 249 | CALL_HEAP_FUNCTION(string->GetIsolate(), string->TryFlatten(), String); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | |
| 253 | Handle<Object> SetPrototype(Handle<JSFunction> function, |
| 254 | Handle<Object> prototype) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 255 | ASSERT(function->should_have_prototype()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 256 | CALL_HEAP_FUNCTION(function->GetIsolate(), |
| 257 | Accessors::FunctionSetPrototype(*function, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 258 | *prototype, |
| 259 | NULL), |
| 260 | Object); |
| 261 | } |
| 262 | |
| 263 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 264 | Handle<Object> SetProperty(Handle<JSReceiver> object, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 265 | Handle<String> key, |
| 266 | Handle<Object> value, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 267 | PropertyAttributes attributes, |
| 268 | StrictModeFlag strict_mode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 269 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 270 | object->SetProperty(*key, *value, attributes, strict_mode), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 271 | Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
| 275 | Handle<Object> SetProperty(Handle<Object> object, |
| 276 | Handle<Object> key, |
| 277 | Handle<Object> value, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 278 | PropertyAttributes attributes, |
| 279 | StrictModeFlag strict_mode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 280 | Isolate* isolate = Isolate::Current(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 281 | CALL_HEAP_FUNCTION( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 282 | isolate, |
| 283 | Runtime::SetObjectProperty( |
| 284 | isolate, object, key, value, attributes, strict_mode), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 285 | Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | |
| 289 | Handle<Object> ForceSetProperty(Handle<JSObject> object, |
| 290 | Handle<Object> key, |
| 291 | Handle<Object> value, |
| 292 | PropertyAttributes attributes) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 293 | Isolate* isolate = object->GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 294 | CALL_HEAP_FUNCTION( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 295 | isolate, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 296 | Runtime::ForceSetObjectProperty( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 297 | isolate, object, key, value, attributes), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 298 | Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 302 | Handle<Object> SetNormalizedProperty(Handle<JSObject> object, |
| 303 | Handle<String> key, |
| 304 | Handle<Object> value, |
| 305 | PropertyDetails details) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 306 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 307 | object->SetNormalizedProperty(*key, *value, details), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 308 | Object); |
| 309 | } |
| 310 | |
| 311 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 312 | Handle<Object> ForceDeleteProperty(Handle<JSObject> object, |
| 313 | Handle<Object> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 314 | Isolate* isolate = object->GetIsolate(); |
| 315 | CALL_HEAP_FUNCTION(isolate, |
| 316 | Runtime::ForceDeleteObjectProperty(isolate, object, key), |
| 317 | Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 321 | Handle<Object> SetLocalPropertyIgnoreAttributes( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 322 | Handle<JSObject> object, |
| 323 | Handle<String> key, |
| 324 | Handle<Object> value, |
| 325 | PropertyAttributes attributes) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 326 | CALL_HEAP_FUNCTION( |
| 327 | object->GetIsolate(), |
| 328 | object->SetLocalPropertyIgnoreAttributes(*key, *value, attributes), |
| 329 | Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 333 | void SetLocalPropertyNoThrow(Handle<JSObject> object, |
| 334 | Handle<String> key, |
| 335 | Handle<Object> value, |
| 336 | PropertyAttributes attributes) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 337 | Isolate* isolate = object->GetIsolate(); |
| 338 | ASSERT(!isolate->has_pending_exception()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 339 | CHECK(!SetLocalPropertyIgnoreAttributes( |
| 340 | object, key, value, attributes).is_null()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 341 | CHECK(!isolate->has_pending_exception()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 345 | Handle<Object> SetPropertyWithInterceptor(Handle<JSObject> object, |
| 346 | Handle<String> key, |
| 347 | Handle<Object> value, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 348 | PropertyAttributes attributes, |
| 349 | StrictModeFlag strict_mode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 350 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 351 | object->SetPropertyWithInterceptor(*key, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 352 | *value, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 353 | attributes, |
| 354 | strict_mode), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 355 | Object); |
| 356 | } |
| 357 | |
| 358 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 359 | Handle<Object> GetProperty(Handle<JSReceiver> obj, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 360 | const char* name) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 361 | Isolate* isolate = obj->GetIsolate(); |
| 362 | Handle<String> str = isolate->factory()->LookupAsciiSymbol(name); |
| 363 | CALL_HEAP_FUNCTION(isolate, obj->GetProperty(*str), Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | |
| 367 | Handle<Object> GetProperty(Handle<Object> obj, |
| 368 | Handle<Object> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 369 | Isolate* isolate = Isolate::Current(); |
| 370 | CALL_HEAP_FUNCTION(isolate, |
| 371 | Runtime::GetObjectProperty(isolate, obj, key), Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 375 | Handle<Object> GetProperty(Handle<JSReceiver> obj, |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 376 | Handle<String> name, |
| 377 | LookupResult* result) { |
| 378 | PropertyAttributes attributes; |
| 379 | Isolate* isolate = Isolate::Current(); |
| 380 | CALL_HEAP_FUNCTION(isolate, |
| 381 | obj->GetProperty(*obj, result, *name, &attributes), |
| 382 | Object); |
| 383 | } |
| 384 | |
| 385 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 386 | Handle<Object> GetElement(Handle<Object> obj, |
| 387 | uint32_t index) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 388 | Isolate* isolate = Isolate::Current(); |
| 389 | CALL_HEAP_FUNCTION(isolate, Runtime::GetElement(obj, index), Object); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 393 | Handle<Object> GetPropertyWithInterceptor(Handle<JSObject> receiver, |
| 394 | Handle<JSObject> holder, |
| 395 | Handle<String> name, |
| 396 | PropertyAttributes* attributes) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 397 | Isolate* isolate = receiver->GetIsolate(); |
| 398 | CALL_HEAP_FUNCTION(isolate, |
| 399 | holder->GetPropertyWithInterceptor(*receiver, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 400 | *name, |
| 401 | attributes), |
| 402 | Object); |
| 403 | } |
| 404 | |
| 405 | |
| 406 | Handle<Object> GetPrototype(Handle<Object> obj) { |
| 407 | Handle<Object> result(obj->GetPrototype()); |
| 408 | return result; |
| 409 | } |
| 410 | |
| 411 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 412 | Handle<Object> SetPrototype(Handle<JSObject> obj, Handle<Object> value) { |
| 413 | const bool skip_hidden_prototypes = false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 414 | CALL_HEAP_FUNCTION(obj->GetIsolate(), |
| 415 | obj->SetPrototype(*value, skip_hidden_prototypes), Object); |
| 416 | } |
| 417 | |
| 418 | |
| 419 | Handle<Object> PreventExtensions(Handle<JSObject> object) { |
| 420 | CALL_HEAP_FUNCTION(object->GetIsolate(), object->PreventExtensions(), Object); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 424 | Handle<Object> GetHiddenProperties(Handle<JSObject> obj, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 425 | JSObject::HiddenPropertiesFlag flag) { |
| 426 | CALL_HEAP_FUNCTION(obj->GetIsolate(), |
| 427 | obj->GetHiddenProperties(flag), |
| 428 | Object); |
| 429 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 430 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 431 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 432 | int GetIdentityHash(Handle<JSObject> obj) { |
| 433 | CALL_AND_RETRY(obj->GetIsolate(), |
| 434 | obj->GetIdentityHash(JSObject::ALLOW_CREATION), |
| 435 | return Smi::cast(__object__)->value(), |
| 436 | return 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | |
| 440 | Handle<Object> DeleteElement(Handle<JSObject> obj, |
| 441 | uint32_t index) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 442 | CALL_HEAP_FUNCTION(obj->GetIsolate(), |
| 443 | obj->DeleteElement(index, JSObject::NORMAL_DELETION), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 444 | Object); |
| 445 | } |
| 446 | |
| 447 | |
| 448 | Handle<Object> DeleteProperty(Handle<JSObject> obj, |
| 449 | Handle<String> prop) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 450 | CALL_HEAP_FUNCTION(obj->GetIsolate(), |
| 451 | obj->DeleteProperty(*prop, JSObject::NORMAL_DELETION), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 452 | Object); |
| 453 | } |
| 454 | |
| 455 | |
| 456 | Handle<Object> LookupSingleCharacterStringFromCode(uint32_t index) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 457 | Isolate* isolate = Isolate::Current(); |
| 458 | CALL_HEAP_FUNCTION( |
| 459 | isolate, |
| 460 | isolate->heap()->LookupSingleCharacterStringFromCode(index), Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 464 | Handle<String> SubString(Handle<String> str, |
| 465 | int start, |
| 466 | int end, |
| 467 | PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 468 | CALL_HEAP_FUNCTION(str->GetIsolate(), |
| 469 | str->SubString(start, end, pretenure), String); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | |
| 473 | Handle<Object> SetElement(Handle<JSObject> object, |
| 474 | uint32_t index, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 475 | Handle<Object> value, |
| 476 | StrictModeFlag strict_mode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 477 | if (object->HasExternalArrayElements()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 478 | if (!value->IsSmi() && !value->IsHeapNumber() && !value->IsUndefined()) { |
| 479 | bool has_exception; |
| 480 | Handle<Object> number = Execution::ToNumber(value, &has_exception); |
| 481 | if (has_exception) return Handle<Object>(); |
| 482 | value = number; |
| 483 | } |
| 484 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 485 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 486 | object->SetElement(index, *value, strict_mode, true), |
| 487 | Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 491 | Handle<Object> SetOwnElement(Handle<JSObject> object, |
| 492 | uint32_t index, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 493 | Handle<Object> value, |
| 494 | StrictModeFlag strict_mode) { |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 495 | ASSERT(!object->HasExternalArrayElements()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 496 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 497 | object->SetElement(index, *value, strict_mode, false), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 498 | Object); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 502 | Handle<JSObject> Copy(Handle<JSObject> obj) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 503 | Isolate* isolate = obj->GetIsolate(); |
| 504 | CALL_HEAP_FUNCTION(isolate, |
| 505 | isolate->heap()->CopyJSObject(*obj), JSObject); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 509 | Handle<Object> SetAccessor(Handle<JSObject> obj, Handle<AccessorInfo> info) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 510 | CALL_HEAP_FUNCTION(obj->GetIsolate(), obj->DefineAccessor(*info), Object); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 514 | // Wrappers for scripts are kept alive and cached in weak global |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 515 | // handles referred from foreign objects held by the scripts as long as |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 516 | // they are used. When they are not used anymore, the garbage |
| 517 | // collector will call the weak callback on the global handle |
| 518 | // associated with the wrapper and get rid of both the wrapper and the |
| 519 | // handle. |
| 520 | static void ClearWrapperCache(Persistent<v8::Value> handle, void*) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 521 | Handle<Object> cache = Utils::OpenHandle(*handle); |
| 522 | JSValue* wrapper = JSValue::cast(*cache); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 523 | Foreign* foreign = Script::cast(wrapper->value())->wrapper(); |
| 524 | ASSERT(foreign->address() == reinterpret_cast<Address>(cache.location())); |
| 525 | foreign->set_address(0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 526 | Isolate* isolate = Isolate::Current(); |
| 527 | isolate->global_handles()->Destroy(cache.location()); |
| 528 | isolate->counters()->script_wrappers()->Decrement(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | |
| 532 | Handle<JSValue> GetScriptWrapper(Handle<Script> script) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 533 | if (script->wrapper()->address() != NULL) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 534 | // Return the script wrapper directly from the cache. |
| 535 | return Handle<JSValue>( |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 536 | reinterpret_cast<JSValue**>(script->wrapper()->address())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 537 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 538 | Isolate* isolate = Isolate::Current(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 539 | // Construct a new script wrapper. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 540 | isolate->counters()->script_wrappers()->Increment(); |
| 541 | Handle<JSFunction> constructor = isolate->script_function(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 542 | Handle<JSValue> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 543 | Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 544 | result->set_value(*script); |
| 545 | |
| 546 | // Create a new weak global handle and use it to cache the wrapper |
| 547 | // for future use. The cache will automatically be cleared by the |
| 548 | // garbage collector when it is not used anymore. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 549 | Handle<Object> handle = isolate->global_handles()->Create(*result); |
| 550 | isolate->global_handles()->MakeWeak(handle.location(), NULL, |
| 551 | &ClearWrapperCache); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 552 | script->wrapper()->set_address(reinterpret_cast<Address>(handle.location())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 553 | return result; |
| 554 | } |
| 555 | |
| 556 | |
| 557 | // Init line_ends array with code positions of line ends inside script |
| 558 | // source. |
| 559 | void InitScriptLineEnds(Handle<Script> script) { |
| 560 | if (!script->line_ends()->IsUndefined()) return; |
| 561 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 562 | Isolate* isolate = script->GetIsolate(); |
| 563 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 564 | if (!script->source()->IsString()) { |
| 565 | ASSERT(script->source()->IsUndefined()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 566 | Handle<FixedArray> empty = isolate->factory()->NewFixedArray(0); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 567 | script->set_line_ends(*empty); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 568 | ASSERT(script->line_ends()->IsFixedArray()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 569 | return; |
| 570 | } |
| 571 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 572 | Handle<String> src(String::cast(script->source()), isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 573 | |
| 574 | Handle<FixedArray> array = CalculateLineEnds(src, true); |
| 575 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 576 | if (*array != isolate->heap()->empty_fixed_array()) { |
| 577 | array->set_map(isolate->heap()->fixed_cow_array_map()); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 580 | script->set_line_ends(*array); |
| 581 | ASSERT(script->line_ends()->IsFixedArray()); |
| 582 | } |
| 583 | |
| 584 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 585 | template <typename SourceChar> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 586 | static void CalculateLineEnds(Isolate* isolate, |
| 587 | List<int>* line_ends, |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 588 | Vector<const SourceChar> src, |
| 589 | bool with_last_line) { |
| 590 | const int src_len = src.length(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 591 | StringSearch<char, SourceChar> search(isolate, CStrVector("\n")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 592 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 593 | // Find and record line ends. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 594 | int position = 0; |
| 595 | while (position != -1 && position < src_len) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 596 | position = search.Search(src, position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 597 | if (position != -1) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 598 | line_ends->Add(position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 599 | position++; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 600 | } else if (with_last_line) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 601 | // 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] | 602 | line_ends->Add(src_len); |
| 603 | return; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 604 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 605 | } |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 606 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 607 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 608 | |
| 609 | Handle<FixedArray> CalculateLineEnds(Handle<String> src, |
| 610 | bool with_last_line) { |
| 611 | src = FlattenGetString(src); |
| 612 | // Rough estimate of line count based on a roughly estimated average |
| 613 | // length of (unpacked) code. |
| 614 | int line_count_estimate = src->length() >> 4; |
| 615 | List<int> line_ends(line_count_estimate); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 616 | Isolate* isolate = src->GetIsolate(); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 617 | { |
| 618 | AssertNoAllocation no_heap_allocation; // ensure vectors stay valid. |
| 619 | // Dispatch on type of strings. |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 620 | String::FlatContent content = src->GetFlatContent(); |
| 621 | ASSERT(content.IsFlat()); |
| 622 | if (content.IsAscii()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 623 | CalculateLineEnds(isolate, |
| 624 | &line_ends, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 625 | content.ToAsciiVector(), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 626 | with_last_line); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 627 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 628 | CalculateLineEnds(isolate, |
| 629 | &line_ends, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 630 | content.ToUC16Vector(), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 631 | with_last_line); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | int line_count = line_ends.length(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 635 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(line_count); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 636 | for (int i = 0; i < line_count; i++) { |
| 637 | array->set(i, Smi::FromInt(line_ends[i])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 638 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 639 | return array; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | |
| 643 | // Convert code position into line number. |
| 644 | int GetScriptLineNumber(Handle<Script> script, int code_pos) { |
| 645 | InitScriptLineEnds(script); |
| 646 | AssertNoAllocation no_allocation; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 647 | FixedArray* line_ends_array = FixedArray::cast(script->line_ends()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 648 | const int line_ends_len = line_ends_array->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 649 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 650 | if (!line_ends_len) return -1; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 651 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 652 | if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 653 | return script->line_offset()->value(); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 654 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 655 | |
| 656 | int left = 0; |
| 657 | int right = line_ends_len; |
| 658 | while (int half = (right - left) / 2) { |
| 659 | if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) { |
| 660 | right -= half; |
| 661 | } else { |
| 662 | left += half; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 663 | } |
| 664 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 665 | return right + script->line_offset()->value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 669 | int GetScriptLineNumberSafe(Handle<Script> script, int code_pos) { |
| 670 | AssertNoAllocation no_allocation; |
| 671 | if (!script->line_ends()->IsUndefined()) { |
| 672 | return GetScriptLineNumber(script, code_pos); |
| 673 | } |
| 674 | // Slow mode: we do not have line_ends. We have to iterate through source. |
| 675 | if (!script->source()->IsString()) { |
| 676 | return -1; |
| 677 | } |
| 678 | String* source = String::cast(script->source()); |
| 679 | int line = 0; |
| 680 | int len = source->length(); |
| 681 | for (int pos = 0; pos < len; pos++) { |
| 682 | if (pos == code_pos) { |
| 683 | break; |
| 684 | } |
| 685 | if (source->Get(pos) == '\n') { |
| 686 | line++; |
| 687 | } |
| 688 | } |
| 689 | return line; |
| 690 | } |
| 691 | |
| 692 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 693 | void CustomArguments::IterateInstance(ObjectVisitor* v) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 694 | v->VisitPointers(values_, values_ + ARRAY_SIZE(values_)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | |
| 698 | // Compute the property keys from the interceptor. |
| 699 | v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSObject> receiver, |
| 700 | Handle<JSObject> object) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 701 | Isolate* isolate = receiver->GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 702 | Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 703 | CustomArguments args(isolate, interceptor->data(), *receiver, *object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 704 | v8::AccessorInfo info(args.end()); |
| 705 | v8::Handle<v8::Array> result; |
| 706 | if (!interceptor->enumerator()->IsUndefined()) { |
| 707 | v8::NamedPropertyEnumerator enum_fun = |
| 708 | v8::ToCData<v8::NamedPropertyEnumerator>(interceptor->enumerator()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 709 | LOG(isolate, ApiObjectAccess("interceptor-named-enum", *object)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 710 | { |
| 711 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 712 | VMState state(isolate, EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 713 | result = enum_fun(info); |
| 714 | } |
| 715 | } |
| 716 | return result; |
| 717 | } |
| 718 | |
| 719 | |
| 720 | // Compute the element keys from the interceptor. |
| 721 | v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSObject> receiver, |
| 722 | Handle<JSObject> object) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 723 | Isolate* isolate = receiver->GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 724 | Handle<InterceptorInfo> interceptor(object->GetIndexedInterceptor()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 725 | CustomArguments args(isolate, interceptor->data(), *receiver, *object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 726 | v8::AccessorInfo info(args.end()); |
| 727 | v8::Handle<v8::Array> result; |
| 728 | if (!interceptor->enumerator()->IsUndefined()) { |
| 729 | v8::IndexedPropertyEnumerator enum_fun = |
| 730 | v8::ToCData<v8::IndexedPropertyEnumerator>(interceptor->enumerator()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 731 | LOG(isolate, ApiObjectAccess("interceptor-indexed-enum", *object)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 732 | { |
| 733 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 734 | VMState state(isolate, EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 735 | result = enum_fun(info); |
| 736 | } |
| 737 | } |
| 738 | return result; |
| 739 | } |
| 740 | |
| 741 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 742 | static bool ContainsOnlyValidKeys(Handle<FixedArray> array) { |
| 743 | int len = array->length(); |
| 744 | for (int i = 0; i < len; i++) { |
| 745 | Object* e = array->get(i); |
| 746 | if (!(e->IsString() || e->IsNumber())) return false; |
| 747 | } |
| 748 | return true; |
| 749 | } |
| 750 | |
| 751 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 752 | Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSObject> object, |
| 753 | KeyCollectionType type) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 754 | USE(ContainsOnlyValidKeys); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 755 | Isolate* isolate = object->GetIsolate(); |
| 756 | Handle<FixedArray> content = isolate->factory()->empty_fixed_array(); |
| 757 | Handle<JSObject> arguments_boilerplate = Handle<JSObject>( |
| 758 | isolate->context()->global_context()->arguments_boilerplate(), |
| 759 | isolate); |
| 760 | Handle<JSFunction> arguments_function = Handle<JSFunction>( |
| 761 | JSFunction::cast(arguments_boilerplate->map()->constructor()), |
| 762 | isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 763 | |
| 764 | // Only collect keys if access is permitted. |
| 765 | for (Handle<Object> p = object; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 766 | *p != isolate->heap()->null_value(); |
| 767 | p = Handle<Object>(p->GetPrototype(), isolate)) { |
| 768 | Handle<JSObject> current(JSObject::cast(*p), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 769 | |
| 770 | // Check access rights if required. |
| 771 | if (current->IsAccessCheckNeeded() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 772 | !isolate->MayNamedAccess(*current, |
| 773 | isolate->heap()->undefined_value(), |
| 774 | v8::ACCESS_KEYS)) { |
| 775 | isolate->ReportFailedAccessCheck(*current, v8::ACCESS_KEYS); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 776 | break; |
| 777 | } |
| 778 | |
| 779 | // Compute the element keys. |
| 780 | Handle<FixedArray> element_keys = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 781 | isolate->factory()->NewFixedArray(current->NumberOfEnumElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 782 | current->GetEnumElementKeys(*element_keys); |
| 783 | content = UnionOfKeys(content, element_keys); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 784 | ASSERT(ContainsOnlyValidKeys(content)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 785 | |
| 786 | // Add the element keys from the interceptor. |
| 787 | if (current->HasIndexedInterceptor()) { |
| 788 | v8::Handle<v8::Array> result = |
| 789 | GetKeysForIndexedInterceptor(object, current); |
| 790 | if (!result.IsEmpty()) |
| 791 | content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result)); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 792 | ASSERT(ContainsOnlyValidKeys(content)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 795 | // We can cache the computed property keys if access checks are |
| 796 | // not needed and no interceptors are involved. |
| 797 | // |
| 798 | // We do not use the cache if the object has elements and |
| 799 | // therefore it does not make sense to cache the property names |
| 800 | // for arguments objects. Arguments objects will always have |
| 801 | // elements. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 802 | // Wrapped strings have elements, but don't have an elements |
| 803 | // array or dictionary. So the fast inline test for whether to |
| 804 | // use the cache says yes, so we should not create a cache. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 805 | bool cache_enum_keys = |
| 806 | ((current->map()->constructor() != *arguments_function) && |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 807 | !current->IsJSValue() && |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 808 | !current->IsAccessCheckNeeded() && |
| 809 | !current->HasNamedInterceptor() && |
| 810 | !current->HasIndexedInterceptor()); |
| 811 | // Compute the property keys and cache them if possible. |
| 812 | content = |
| 813 | UnionOfKeys(content, GetEnumPropertyKeys(current, cache_enum_keys)); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 814 | ASSERT(ContainsOnlyValidKeys(content)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 815 | |
| 816 | // Add the property keys from the interceptor. |
| 817 | if (current->HasNamedInterceptor()) { |
| 818 | v8::Handle<v8::Array> result = |
| 819 | GetKeysForNamedInterceptor(object, current); |
| 820 | if (!result.IsEmpty()) |
| 821 | content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result)); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 822 | ASSERT(ContainsOnlyValidKeys(content)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | // If we only want local properties we bail out after the first |
| 826 | // iteration. |
| 827 | if (type == LOCAL_ONLY) |
| 828 | break; |
| 829 | } |
| 830 | return content; |
| 831 | } |
| 832 | |
| 833 | |
| 834 | Handle<JSArray> GetKeysFor(Handle<JSObject> object) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 835 | Isolate* isolate = object->GetIsolate(); |
| 836 | isolate->counters()->for_in()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 837 | Handle<FixedArray> elements = GetKeysInFixedArrayFor(object, |
| 838 | INCLUDE_PROTOS); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 839 | return isolate->factory()->NewJSArrayWithElements(elements); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 843 | Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object, |
| 844 | bool cache_result) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 845 | int index = 0; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 846 | Isolate* isolate = object->GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 847 | if (object->HasFastProperties()) { |
| 848 | if (object->map()->instance_descriptors()->HasEnumCache()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 849 | isolate->counters()->enum_cache_hits()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 850 | DescriptorArray* desc = object->map()->instance_descriptors(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 851 | return Handle<FixedArray>(FixedArray::cast(desc->GetEnumCache()), |
| 852 | isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 853 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 854 | isolate->counters()->enum_cache_misses()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 855 | int num_enum = object->NumberOfEnumProperties(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 856 | Handle<FixedArray> storage = isolate->factory()->NewFixedArray(num_enum); |
| 857 | Handle<FixedArray> sort_array = isolate->factory()->NewFixedArray(num_enum); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 858 | Handle<DescriptorArray> descs = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 859 | Handle<DescriptorArray>(object->map()->instance_descriptors(), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 860 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 861 | if (descs->IsProperty(i) && !descs->IsDontEnum(i)) { |
| 862 | (*storage)->set(index, descs->GetKey(i)); |
| 863 | PropertyDetails details(descs->GetDetails(i)); |
| 864 | (*sort_array)->set(index, Smi::FromInt(details.index())); |
| 865 | index++; |
| 866 | } |
| 867 | } |
| 868 | (*storage)->SortPairs(*sort_array, sort_array->length()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 869 | if (cache_result) { |
| 870 | Handle<FixedArray> bridge_storage = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 871 | isolate->factory()->NewFixedArray( |
| 872 | DescriptorArray::kEnumCacheBridgeLength); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 873 | DescriptorArray* desc = object->map()->instance_descriptors(); |
| 874 | desc->SetEnumCache(*bridge_storage, *storage); |
| 875 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 876 | ASSERT(storage->length() == index); |
| 877 | return storage; |
| 878 | } else { |
| 879 | int num_enum = object->NumberOfEnumProperties(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 880 | Handle<FixedArray> storage = isolate->factory()->NewFixedArray(num_enum); |
| 881 | Handle<FixedArray> sort_array = isolate->factory()->NewFixedArray(num_enum); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 882 | object->property_dictionary()->CopyEnumKeysTo(*storage, *sort_array); |
| 883 | return storage; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 888 | Handle<ObjectHashTable> PutIntoObjectHashTable(Handle<ObjectHashTable> table, |
| 889 | Handle<JSObject> key, |
| 890 | Handle<Object> value) { |
| 891 | CALL_HEAP_FUNCTION(table->GetIsolate(), |
| 892 | table->Put(*key, *value), |
| 893 | ObjectHashTable); |
| 894 | } |
| 895 | |
| 896 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 897 | bool EnsureCompiled(Handle<SharedFunctionInfo> shared, |
| 898 | ClearExceptionFlag flag) { |
| 899 | return shared->is_compiled() || CompileLazyShared(shared, flag); |
| 900 | } |
| 901 | |
| 902 | |
| 903 | static bool CompileLazyHelper(CompilationInfo* info, |
| 904 | ClearExceptionFlag flag) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 905 | // Compile the source information to a code object. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 906 | ASSERT(info->IsOptimizing() || !info->shared_info()->is_compiled()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 907 | ASSERT(!info->isolate()->has_pending_exception()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 908 | bool result = Compiler::CompileLazy(info); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 909 | ASSERT(result != Isolate::Current()->has_pending_exception()); |
| 910 | if (!result && flag == CLEAR_EXCEPTION) { |
| 911 | info->isolate()->clear_pending_exception(); |
| 912 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 913 | return result; |
| 914 | } |
| 915 | |
| 916 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 917 | bool CompileLazyShared(Handle<SharedFunctionInfo> shared, |
| 918 | ClearExceptionFlag flag) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 919 | CompilationInfo info(shared); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 920 | return CompileLazyHelper(&info, flag); |
| 921 | } |
| 922 | |
| 923 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 924 | static bool CompileLazyFunction(Handle<JSFunction> function, |
| 925 | ClearExceptionFlag flag, |
| 926 | InLoopFlag in_loop_flag) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 927 | bool result = true; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 928 | if (function->shared()->is_compiled()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 929 | function->ReplaceCode(function->shared()->code()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 930 | function->shared()->set_code_age(0); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 931 | } else { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 932 | CompilationInfo info(function); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 933 | if (in_loop_flag == IN_LOOP) info.MarkAsInLoop(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 934 | result = CompileLazyHelper(&info, flag); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 935 | ASSERT(!result || function->is_compiled()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 936 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 937 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 941 | bool CompileLazy(Handle<JSFunction> function, |
| 942 | ClearExceptionFlag flag) { |
| 943 | return CompileLazyFunction(function, flag, NOT_IN_LOOP); |
| 944 | } |
| 945 | |
| 946 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 947 | bool CompileLazyInLoop(Handle<JSFunction> function, |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 948 | ClearExceptionFlag flag) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 949 | return CompileLazyFunction(function, flag, IN_LOOP); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 953 | bool CompileOptimized(Handle<JSFunction> function, |
| 954 | int osr_ast_id, |
| 955 | ClearExceptionFlag flag) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 956 | CompilationInfo info(function); |
| 957 | info.SetOptimizing(osr_ast_id); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 958 | return CompileLazyHelper(&info, flag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 961 | } } // namespace v8::internal |