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