Ben Murdoch | 257744e | 2011-11-30 15:57:28 +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 "api.h" |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 31 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | #include "arguments.h" |
| 33 | #include "bootstrapper.h" |
| 34 | #include "compiler.h" |
| 35 | #include "debug.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 36 | #include "deoptimizer.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 37 | #include "execution.h" |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 38 | #include "flags.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | #include "global-handles.h" |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 40 | #include "heap-profiler.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 41 | #include "messages.h" |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 42 | #include "natives.h" |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 43 | #include "parser.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | #include "platform.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 45 | #include "profile-generator-inl.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 46 | #include "runtime-profiler.h" |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 47 | #include "scanner-character-streams.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 48 | #include "serialize.h" |
| 49 | #include "snapshot.h" |
| 50 | #include "v8threads.h" |
| 51 | #include "version.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 52 | #include "vm-state-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 53 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 54 | #include "../include/v8-profiler.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 55 | #include "../include/v8-testing.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 56 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 57 | #define LOG_API(isolate, expr) LOG(isolate, ApiEntryCall(expr)) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 58 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 59 | #define ENTER_V8(isolate) \ |
| 60 | ASSERT((isolate)->IsInitialized()); \ |
| 61 | i::VMState __state__((isolate), i::OTHER) |
| 62 | #define LEAVE_V8(isolate) \ |
| 63 | i::VMState __state__((isolate), i::EXTERNAL) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | |
| 65 | namespace v8 { |
| 66 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 67 | #define ON_BAILOUT(isolate, location, code) \ |
| 68 | if (IsDeadCheck(isolate, location) || \ |
| 69 | IsExecutionTerminatingCheck(isolate)) { \ |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 70 | code; \ |
| 71 | UNREACHABLE(); \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 75 | #define EXCEPTION_PREAMBLE(isolate) \ |
| 76 | (isolate)->handle_scope_implementer()->IncrementCallDepth(); \ |
| 77 | ASSERT(!(isolate)->external_caught_exception()); \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 78 | bool has_pending_exception = false |
| 79 | |
| 80 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 81 | #define EXCEPTION_BAILOUT_CHECK(isolate, value) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 82 | do { \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 83 | i::HandleScopeImplementer* handle_scope_implementer = \ |
| 84 | (isolate)->handle_scope_implementer(); \ |
| 85 | handle_scope_implementer->DecrementCallDepth(); \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | if (has_pending_exception) { \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 87 | if (handle_scope_implementer->CallDepthIsZero() && \ |
| 88 | (isolate)->is_out_of_memory()) { \ |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 89 | if (!(isolate)->ignore_out_of_memory()) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 90 | i::V8::FatalProcessOutOfMemory(NULL); \ |
| 91 | } \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 92 | bool call_depth_is_zero = handle_scope_implementer->CallDepthIsZero(); \ |
| 93 | (isolate)->OptionalRescheduleException(call_depth_is_zero); \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 94 | return value; \ |
| 95 | } \ |
| 96 | } while (false) |
| 97 | |
| 98 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 99 | #define API_ENTRY_CHECK(isolate, msg) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 100 | do { \ |
| 101 | if (v8::Locker::IsActive()) { \ |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 102 | ApiCheck(isolate->thread_manager()->IsLockedByCurrentThread(), \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 103 | msg, \ |
| 104 | "Entering the V8 API without proper locking in place"); \ |
| 105 | } \ |
| 106 | } while (false) |
| 107 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 108 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 109 | // --- E x c e p t i o n B e h a v i o r --- |
| 110 | |
| 111 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 112 | static void DefaultFatalErrorHandler(const char* location, |
| 113 | const char* message) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 114 | i::VMState __state__(i::Isolate::Current(), i::OTHER); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 115 | API_Fatal(location, message); |
| 116 | } |
| 117 | |
| 118 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 119 | static FatalErrorCallback GetFatalErrorHandler() { |
| 120 | i::Isolate* isolate = i::Isolate::Current(); |
| 121 | if (isolate->exception_behavior() == NULL) { |
| 122 | isolate->set_exception_behavior(DefaultFatalErrorHandler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 123 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 124 | return isolate->exception_behavior(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 128 | void i::FatalProcessOutOfMemory(const char* location) { |
| 129 | i::V8::FatalProcessOutOfMemory(location, false); |
| 130 | } |
| 131 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 132 | |
| 133 | // When V8 cannot allocated memory FatalProcessOutOfMemory is called. |
| 134 | // The default fatal error handler is called and execution is stopped. |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 135 | void i::V8::FatalProcessOutOfMemory(const char* location, bool take_snapshot) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 136 | i::HeapStats heap_stats; |
| 137 | int start_marker; |
| 138 | heap_stats.start_marker = &start_marker; |
| 139 | int new_space_size; |
| 140 | heap_stats.new_space_size = &new_space_size; |
| 141 | int new_space_capacity; |
| 142 | heap_stats.new_space_capacity = &new_space_capacity; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 143 | intptr_t old_pointer_space_size; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 144 | heap_stats.old_pointer_space_size = &old_pointer_space_size; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 145 | intptr_t old_pointer_space_capacity; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 146 | heap_stats.old_pointer_space_capacity = &old_pointer_space_capacity; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 147 | intptr_t old_data_space_size; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 148 | heap_stats.old_data_space_size = &old_data_space_size; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 149 | intptr_t old_data_space_capacity; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 150 | heap_stats.old_data_space_capacity = &old_data_space_capacity; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 151 | intptr_t code_space_size; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 152 | heap_stats.code_space_size = &code_space_size; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 153 | intptr_t code_space_capacity; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 154 | heap_stats.code_space_capacity = &code_space_capacity; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 155 | intptr_t map_space_size; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 156 | heap_stats.map_space_size = &map_space_size; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 157 | intptr_t map_space_capacity; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 158 | heap_stats.map_space_capacity = &map_space_capacity; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 159 | intptr_t cell_space_size; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 160 | heap_stats.cell_space_size = &cell_space_size; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 161 | intptr_t cell_space_capacity; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 162 | heap_stats.cell_space_capacity = &cell_space_capacity; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 163 | intptr_t lo_space_size; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 164 | heap_stats.lo_space_size = &lo_space_size; |
| 165 | int global_handle_count; |
| 166 | heap_stats.global_handle_count = &global_handle_count; |
| 167 | int weak_global_handle_count; |
| 168 | heap_stats.weak_global_handle_count = &weak_global_handle_count; |
| 169 | int pending_global_handle_count; |
| 170 | heap_stats.pending_global_handle_count = &pending_global_handle_count; |
| 171 | int near_death_global_handle_count; |
| 172 | heap_stats.near_death_global_handle_count = &near_death_global_handle_count; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 173 | int free_global_handle_count; |
| 174 | heap_stats.free_global_handle_count = &free_global_handle_count; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 175 | intptr_t memory_allocator_size; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 176 | heap_stats.memory_allocator_size = &memory_allocator_size; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 177 | intptr_t memory_allocator_capacity; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 178 | heap_stats.memory_allocator_capacity = &memory_allocator_capacity; |
| 179 | int objects_per_type[LAST_TYPE + 1] = {0}; |
| 180 | heap_stats.objects_per_type = objects_per_type; |
| 181 | int size_per_type[LAST_TYPE + 1] = {0}; |
| 182 | heap_stats.size_per_type = size_per_type; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 183 | int os_error; |
| 184 | heap_stats.os_error = &os_error; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 185 | int end_marker; |
| 186 | heap_stats.end_marker = &end_marker; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 187 | i::Isolate* isolate = i::Isolate::Current(); |
| 188 | isolate->heap()->RecordStats(&heap_stats, take_snapshot); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 189 | i::V8::SetFatalError(); |
| 190 | FatalErrorCallback callback = GetFatalErrorHandler(); |
| 191 | { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 192 | LEAVE_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 193 | callback(location, "Allocation failed - process out of memory"); |
| 194 | } |
| 195 | // If the callback returns, we stop execution. |
| 196 | UNREACHABLE(); |
| 197 | } |
| 198 | |
| 199 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 200 | bool Utils::ReportApiFailure(const char* location, const char* message) { |
| 201 | FatalErrorCallback callback = GetFatalErrorHandler(); |
| 202 | callback(location, message); |
| 203 | i::V8::SetFatalError(); |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | bool V8::IsDead() { |
| 209 | return i::V8::IsDead(); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | static inline bool ApiCheck(bool condition, |
| 214 | const char* location, |
| 215 | const char* message) { |
| 216 | return condition ? true : Utils::ReportApiFailure(location, message); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | static bool ReportV8Dead(const char* location) { |
| 221 | FatalErrorCallback callback = GetFatalErrorHandler(); |
| 222 | callback(location, "V8 is no longer usable"); |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | static bool ReportEmptyHandle(const char* location) { |
| 228 | FatalErrorCallback callback = GetFatalErrorHandler(); |
| 229 | callback(location, "Reading from empty handle"); |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | /** |
| 235 | * IsDeadCheck checks that the vm is usable. If, for instance, the vm has been |
| 236 | * out of memory at some point this check will fail. It should be called on |
| 237 | * entry to all methods that touch anything in the heap, except destructors |
| 238 | * which you sometimes can't avoid calling after the vm has crashed. Functions |
| 239 | * that call EnsureInitialized or ON_BAILOUT don't have to also call |
| 240 | * IsDeadCheck. ON_BAILOUT has the advantage over EnsureInitialized that you |
| 241 | * can arrange to return if the VM is dead. This is needed to ensure that no VM |
| 242 | * heap allocations are attempted on a dead VM. EnsureInitialized has the |
| 243 | * advantage over ON_BAILOUT that it actually initializes the VM if this has not |
| 244 | * yet been done. |
| 245 | */ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 246 | static inline bool IsDeadCheck(i::Isolate* isolate, const char* location) { |
| 247 | return !isolate->IsInitialized() |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 248 | && i::V8::IsDead() ? ReportV8Dead(location) : false; |
| 249 | } |
| 250 | |
| 251 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 252 | static inline bool IsExecutionTerminatingCheck(i::Isolate* isolate) { |
| 253 | if (!isolate->IsInitialized()) return false; |
| 254 | if (isolate->has_scheduled_exception()) { |
| 255 | return isolate->scheduled_exception() == |
| 256 | isolate->heap()->termination_exception(); |
| 257 | } |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 262 | static inline bool EmptyCheck(const char* location, v8::Handle<v8::Data> obj) { |
| 263 | return obj.IsEmpty() ? ReportEmptyHandle(location) : false; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | static inline bool EmptyCheck(const char* location, const v8::Data* obj) { |
| 268 | return (obj == 0) ? ReportEmptyHandle(location) : false; |
| 269 | } |
| 270 | |
| 271 | // --- S t a t i c s --- |
| 272 | |
| 273 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 274 | static bool InitializeHelper() { |
| 275 | if (i::Snapshot::Initialize()) return true; |
| 276 | return i::V8::Initialize(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 280 | static inline bool EnsureInitializedForIsolate(i::Isolate* isolate, |
| 281 | const char* location) { |
| 282 | if (IsDeadCheck(isolate, location)) return false; |
| 283 | if (isolate != NULL) { |
| 284 | if (isolate->IsInitialized()) return true; |
| 285 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 286 | ASSERT(isolate == i::Isolate::Current()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 287 | return ApiCheck(InitializeHelper(), location, "Error initializing V8"); |
| 288 | } |
| 289 | |
| 290 | // Some initializing API functions are called early and may be |
| 291 | // called on a thread different from static initializer thread. |
| 292 | // If Isolate API is used, Isolate::Enter() will initialize TLS so |
| 293 | // Isolate::Current() works. If it's a legacy case, then the thread |
| 294 | // may not have TLS initialized yet. However, in initializing APIs it |
| 295 | // may be too early to call EnsureInitialized() - some pre-init |
| 296 | // parameters still have to be configured. |
| 297 | static inline i::Isolate* EnterIsolateIfNeeded() { |
| 298 | i::Isolate* isolate = i::Isolate::UncheckedCurrent(); |
| 299 | if (isolate != NULL) |
| 300 | return isolate; |
| 301 | |
| 302 | i::Isolate::EnterDefaultIsolate(); |
| 303 | isolate = i::Isolate::Current(); |
| 304 | return isolate; |
| 305 | } |
| 306 | |
| 307 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 308 | StartupDataDecompressor::StartupDataDecompressor() |
| 309 | : raw_data(i::NewArray<char*>(V8::GetCompressedStartupDataCount())) { |
| 310 | for (int i = 0; i < V8::GetCompressedStartupDataCount(); ++i) { |
| 311 | raw_data[i] = NULL; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | |
| 316 | StartupDataDecompressor::~StartupDataDecompressor() { |
| 317 | for (int i = 0; i < V8::GetCompressedStartupDataCount(); ++i) { |
| 318 | i::DeleteArray(raw_data[i]); |
| 319 | } |
| 320 | i::DeleteArray(raw_data); |
| 321 | } |
| 322 | |
| 323 | |
| 324 | int StartupDataDecompressor::Decompress() { |
| 325 | int compressed_data_count = V8::GetCompressedStartupDataCount(); |
| 326 | StartupData* compressed_data = |
| 327 | i::NewArray<StartupData>(compressed_data_count); |
| 328 | V8::GetCompressedStartupData(compressed_data); |
| 329 | for (int i = 0; i < compressed_data_count; ++i) { |
| 330 | char* decompressed = raw_data[i] = |
| 331 | i::NewArray<char>(compressed_data[i].raw_size); |
| 332 | if (compressed_data[i].compressed_size != 0) { |
| 333 | int result = DecompressData(decompressed, |
| 334 | &compressed_data[i].raw_size, |
| 335 | compressed_data[i].data, |
| 336 | compressed_data[i].compressed_size); |
| 337 | if (result != 0) return result; |
| 338 | } else { |
| 339 | ASSERT_EQ(0, compressed_data[i].raw_size); |
| 340 | } |
| 341 | compressed_data[i].data = decompressed; |
| 342 | } |
| 343 | V8::SetDecompressedStartupData(compressed_data); |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 348 | StartupData::CompressionAlgorithm V8::GetCompressedStartupDataAlgorithm() { |
| 349 | #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 350 | return StartupData::kBZip2; |
| 351 | #else |
| 352 | return StartupData::kUncompressed; |
| 353 | #endif |
| 354 | } |
| 355 | |
| 356 | |
| 357 | enum CompressedStartupDataItems { |
| 358 | kSnapshot = 0, |
| 359 | kSnapshotContext, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 360 | kLibraries, |
| 361 | kExperimentalLibraries, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 362 | kCompressedStartupDataCount |
| 363 | }; |
| 364 | |
| 365 | int V8::GetCompressedStartupDataCount() { |
| 366 | #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 367 | return kCompressedStartupDataCount; |
| 368 | #else |
| 369 | return 0; |
| 370 | #endif |
| 371 | } |
| 372 | |
| 373 | |
| 374 | void V8::GetCompressedStartupData(StartupData* compressed_data) { |
| 375 | #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 376 | compressed_data[kSnapshot].data = |
| 377 | reinterpret_cast<const char*>(i::Snapshot::data()); |
| 378 | compressed_data[kSnapshot].compressed_size = i::Snapshot::size(); |
| 379 | compressed_data[kSnapshot].raw_size = i::Snapshot::raw_size(); |
| 380 | |
| 381 | compressed_data[kSnapshotContext].data = |
| 382 | reinterpret_cast<const char*>(i::Snapshot::context_data()); |
| 383 | compressed_data[kSnapshotContext].compressed_size = |
| 384 | i::Snapshot::context_size(); |
| 385 | compressed_data[kSnapshotContext].raw_size = i::Snapshot::context_raw_size(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 386 | |
| 387 | i::Vector<const i::byte> libraries_source = i::Natives::GetScriptsSource(); |
| 388 | compressed_data[kLibraries].data = |
| 389 | reinterpret_cast<const char*>(libraries_source.start()); |
| 390 | compressed_data[kLibraries].compressed_size = libraries_source.length(); |
| 391 | compressed_data[kLibraries].raw_size = i::Natives::GetRawScriptsSize(); |
| 392 | |
| 393 | i::Vector<const i::byte> exp_libraries_source = |
| 394 | i::ExperimentalNatives::GetScriptsSource(); |
| 395 | compressed_data[kExperimentalLibraries].data = |
| 396 | reinterpret_cast<const char*>(exp_libraries_source.start()); |
| 397 | compressed_data[kExperimentalLibraries].compressed_size = |
| 398 | exp_libraries_source.length(); |
| 399 | compressed_data[kExperimentalLibraries].raw_size = |
| 400 | i::ExperimentalNatives::GetRawScriptsSize(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 401 | #endif |
| 402 | } |
| 403 | |
| 404 | |
| 405 | void V8::SetDecompressedStartupData(StartupData* decompressed_data) { |
| 406 | #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 407 | ASSERT_EQ(i::Snapshot::raw_size(), decompressed_data[kSnapshot].raw_size); |
| 408 | i::Snapshot::set_raw_data( |
| 409 | reinterpret_cast<const i::byte*>(decompressed_data[kSnapshot].data)); |
| 410 | |
| 411 | ASSERT_EQ(i::Snapshot::context_raw_size(), |
| 412 | decompressed_data[kSnapshotContext].raw_size); |
| 413 | i::Snapshot::set_context_raw_data( |
| 414 | reinterpret_cast<const i::byte*>( |
| 415 | decompressed_data[kSnapshotContext].data)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 416 | |
| 417 | ASSERT_EQ(i::Natives::GetRawScriptsSize(), |
| 418 | decompressed_data[kLibraries].raw_size); |
| 419 | i::Vector<const char> libraries_source( |
| 420 | decompressed_data[kLibraries].data, |
| 421 | decompressed_data[kLibraries].raw_size); |
| 422 | i::Natives::SetRawScriptsSource(libraries_source); |
| 423 | |
| 424 | ASSERT_EQ(i::ExperimentalNatives::GetRawScriptsSize(), |
| 425 | decompressed_data[kExperimentalLibraries].raw_size); |
| 426 | i::Vector<const char> exp_libraries_source( |
| 427 | decompressed_data[kExperimentalLibraries].data, |
| 428 | decompressed_data[kExperimentalLibraries].raw_size); |
| 429 | i::ExperimentalNatives::SetRawScriptsSource(exp_libraries_source); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 430 | #endif |
| 431 | } |
| 432 | |
| 433 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 434 | void V8::SetFatalErrorHandler(FatalErrorCallback that) { |
| 435 | i::Isolate* isolate = EnterIsolateIfNeeded(); |
| 436 | isolate->set_exception_behavior(that); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 440 | void V8::SetAllowCodeGenerationFromStringsCallback( |
| 441 | AllowCodeGenerationFromStringsCallback callback) { |
| 442 | i::Isolate* isolate = EnterIsolateIfNeeded(); |
| 443 | isolate->set_allow_code_gen_callback(callback); |
| 444 | } |
| 445 | |
| 446 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 447 | #ifdef DEBUG |
| 448 | void ImplementationUtilities::ZapHandleRange(i::Object** begin, |
| 449 | i::Object** end) { |
| 450 | i::HandleScope::ZapRange(begin, end); |
| 451 | } |
| 452 | #endif |
| 453 | |
| 454 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 455 | void V8::SetFlagsFromString(const char* str, int length) { |
| 456 | i::FlagList::SetFlagsFromString(str, length); |
| 457 | } |
| 458 | |
| 459 | |
| 460 | void V8::SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags) { |
| 461 | i::FlagList::SetFlagsFromCommandLine(argc, argv, remove_flags); |
| 462 | } |
| 463 | |
| 464 | |
| 465 | v8::Handle<Value> ThrowException(v8::Handle<v8::Value> value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 466 | i::Isolate* isolate = i::Isolate::Current(); |
| 467 | if (IsDeadCheck(isolate, "v8::ThrowException()")) { |
| 468 | return v8::Handle<Value>(); |
| 469 | } |
| 470 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 471 | // If we're passed an empty handle, we throw an undefined exception |
| 472 | // to deal more gracefully with out of memory situations. |
| 473 | if (value.IsEmpty()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 474 | isolate->ScheduleThrow(isolate->heap()->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 475 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 476 | isolate->ScheduleThrow(*Utils::OpenHandle(*value)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 477 | } |
| 478 | return v8::Undefined(); |
| 479 | } |
| 480 | |
| 481 | |
| 482 | RegisteredExtension* RegisteredExtension::first_extension_ = NULL; |
| 483 | |
| 484 | |
| 485 | RegisteredExtension::RegisteredExtension(Extension* extension) |
| 486 | : extension_(extension), state_(UNVISITED) { } |
| 487 | |
| 488 | |
| 489 | void RegisteredExtension::Register(RegisteredExtension* that) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 490 | that->next_ = first_extension_; |
| 491 | first_extension_ = that; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | |
| 495 | void RegisterExtension(Extension* that) { |
| 496 | RegisteredExtension* extension = new RegisteredExtension(that); |
| 497 | RegisteredExtension::Register(extension); |
| 498 | } |
| 499 | |
| 500 | |
| 501 | Extension::Extension(const char* name, |
| 502 | const char* source, |
| 503 | int dep_count, |
| 504 | const char** deps) |
| 505 | : name_(name), |
| 506 | source_(source), |
| 507 | dep_count_(dep_count), |
| 508 | deps_(deps), |
| 509 | auto_enable_(false) { } |
| 510 | |
| 511 | |
| 512 | v8::Handle<Primitive> Undefined() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 513 | i::Isolate* isolate = i::Isolate::Current(); |
| 514 | if (!EnsureInitializedForIsolate(isolate, "v8::Undefined()")) { |
| 515 | return v8::Handle<v8::Primitive>(); |
| 516 | } |
| 517 | return v8::Handle<Primitive>(ToApi<Primitive>( |
| 518 | isolate->factory()->undefined_value())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | |
| 522 | v8::Handle<Primitive> Null() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 523 | i::Isolate* isolate = i::Isolate::Current(); |
| 524 | if (!EnsureInitializedForIsolate(isolate, "v8::Null()")) { |
| 525 | return v8::Handle<v8::Primitive>(); |
| 526 | } |
| 527 | return v8::Handle<Primitive>( |
| 528 | ToApi<Primitive>(isolate->factory()->null_value())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | |
| 532 | v8::Handle<Boolean> True() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 533 | i::Isolate* isolate = i::Isolate::Current(); |
| 534 | if (!EnsureInitializedForIsolate(isolate, "v8::True()")) { |
| 535 | return v8::Handle<Boolean>(); |
| 536 | } |
| 537 | return v8::Handle<Boolean>( |
| 538 | ToApi<Boolean>(isolate->factory()->true_value())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | |
| 542 | v8::Handle<Boolean> False() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 543 | i::Isolate* isolate = i::Isolate::Current(); |
| 544 | if (!EnsureInitializedForIsolate(isolate, "v8::False()")) { |
| 545 | return v8::Handle<Boolean>(); |
| 546 | } |
| 547 | return v8::Handle<Boolean>( |
| 548 | ToApi<Boolean>(isolate->factory()->false_value())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | |
| 552 | ResourceConstraints::ResourceConstraints() |
| 553 | : max_young_space_size_(0), |
| 554 | max_old_space_size_(0), |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 555 | max_executable_size_(0), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 556 | stack_limit_(NULL) { } |
| 557 | |
| 558 | |
| 559 | bool SetResourceConstraints(ResourceConstraints* constraints) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 560 | i::Isolate* isolate = EnterIsolateIfNeeded(); |
| 561 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 562 | int young_space_size = constraints->max_young_space_size(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 563 | int old_gen_size = constraints->max_old_space_size(); |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 564 | int max_executable_size = constraints->max_executable_size(); |
| 565 | if (young_space_size != 0 || old_gen_size != 0 || max_executable_size != 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 566 | // After initialization it's too late to change Heap constraints. |
| 567 | ASSERT(!isolate->IsInitialized()); |
| 568 | bool result = isolate->heap()->ConfigureHeap(young_space_size / 2, |
| 569 | old_gen_size, |
| 570 | max_executable_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 571 | if (!result) return false; |
| 572 | } |
| 573 | if (constraints->stack_limit() != NULL) { |
| 574 | uintptr_t limit = reinterpret_cast<uintptr_t>(constraints->stack_limit()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 575 | isolate->stack_guard()->SetStackLimit(limit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 576 | } |
| 577 | return true; |
| 578 | } |
| 579 | |
| 580 | |
| 581 | i::Object** V8::GlobalizeReference(i::Object** obj) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 582 | i::Isolate* isolate = i::Isolate::Current(); |
| 583 | if (IsDeadCheck(isolate, "V8::Persistent::New")) return NULL; |
| 584 | LOG_API(isolate, "Persistent::New"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 585 | i::Handle<i::Object> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 586 | isolate->global_handles()->Create(*obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 587 | return result.location(); |
| 588 | } |
| 589 | |
| 590 | |
| 591 | void V8::MakeWeak(i::Object** object, void* parameters, |
| 592 | WeakReferenceCallback callback) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 593 | i::Isolate* isolate = i::Isolate::Current(); |
| 594 | LOG_API(isolate, "MakeWeak"); |
| 595 | isolate->global_handles()->MakeWeak(object, parameters, |
| 596 | callback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | |
| 600 | void V8::ClearWeak(i::Object** obj) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 601 | i::Isolate* isolate = i::Isolate::Current(); |
| 602 | LOG_API(isolate, "ClearWeak"); |
| 603 | isolate->global_handles()->ClearWeakness(obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 607 | void V8::MarkIndependent(i::Object** object) { |
| 608 | i::Isolate* isolate = i::Isolate::Current(); |
| 609 | LOG_API(isolate, "MakeIndependent"); |
| 610 | isolate->global_handles()->MarkIndependent(object); |
| 611 | } |
| 612 | |
| 613 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 614 | bool V8::IsGlobalNearDeath(i::Object** obj) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 615 | i::Isolate* isolate = i::Isolate::Current(); |
| 616 | LOG_API(isolate, "IsGlobalNearDeath"); |
| 617 | if (!isolate->IsInitialized()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 618 | return i::GlobalHandles::IsNearDeath(obj); |
| 619 | } |
| 620 | |
| 621 | |
| 622 | bool V8::IsGlobalWeak(i::Object** obj) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 623 | i::Isolate* isolate = i::Isolate::Current(); |
| 624 | LOG_API(isolate, "IsGlobalWeak"); |
| 625 | if (!isolate->IsInitialized()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 626 | return i::GlobalHandles::IsWeak(obj); |
| 627 | } |
| 628 | |
| 629 | |
| 630 | void V8::DisposeGlobal(i::Object** obj) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 631 | i::Isolate* isolate = i::Isolate::Current(); |
| 632 | LOG_API(isolate, "DisposeGlobal"); |
| 633 | if (!isolate->IsInitialized()) return; |
| 634 | isolate->global_handles()->Destroy(obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | // --- H a n d l e s --- |
| 638 | |
| 639 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 640 | HandleScope::HandleScope() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 641 | i::Isolate* isolate = i::Isolate::Current(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 642 | API_ENTRY_CHECK(isolate, "HandleScope::HandleScope"); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 643 | v8::ImplementationUtilities::HandleScopeData* current = |
| 644 | isolate->handle_scope_data(); |
| 645 | isolate_ = isolate; |
| 646 | prev_next_ = current->next; |
| 647 | prev_limit_ = current->limit; |
| 648 | is_closed_ = false; |
| 649 | current->level++; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | |
| 653 | HandleScope::~HandleScope() { |
| 654 | if (!is_closed_) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 655 | Leave(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
| 659 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 660 | void HandleScope::Leave() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 661 | ASSERT(isolate_ == i::Isolate::Current()); |
| 662 | v8::ImplementationUtilities::HandleScopeData* current = |
| 663 | isolate_->handle_scope_data(); |
| 664 | current->level--; |
| 665 | ASSERT(current->level >= 0); |
| 666 | current->next = prev_next_; |
| 667 | if (current->limit != prev_limit_) { |
| 668 | current->limit = prev_limit_; |
| 669 | i::HandleScope::DeleteExtensions(isolate_); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | #ifdef DEBUG |
| 673 | i::HandleScope::ZapRange(prev_next_, prev_limit_); |
| 674 | #endif |
| 675 | } |
| 676 | |
| 677 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 678 | int HandleScope::NumberOfHandles() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 679 | EnsureInitializedForIsolate( |
| 680 | i::Isolate::Current(), "HandleScope::NumberOfHandles"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 681 | return i::HandleScope::NumberOfHandles(); |
| 682 | } |
| 683 | |
| 684 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 685 | i::Object** HandleScope::CreateHandle(i::Object* value) { |
| 686 | return i::HandleScope::CreateHandle(value, i::Isolate::Current()); |
| 687 | } |
| 688 | |
| 689 | |
| 690 | i::Object** HandleScope::CreateHandle(i::HeapObject* value) { |
| 691 | ASSERT(value->IsHeapObject()); |
| 692 | return reinterpret_cast<i::Object**>( |
| 693 | i::HandleScope::CreateHandle(value, value->GetIsolate())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | |
| 697 | void Context::Enter() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 698 | i::Handle<i::Context> env = Utils::OpenHandle(this); |
| 699 | i::Isolate* isolate = env->GetIsolate(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 700 | if (IsDeadCheck(isolate, "v8::Context::Enter()")) return; |
| 701 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 702 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 703 | isolate->handle_scope_implementer()->EnterContext(env); |
| 704 | |
| 705 | isolate->handle_scope_implementer()->SaveContext(isolate->context()); |
| 706 | isolate->set_context(*env); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | |
| 710 | void Context::Exit() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 711 | // Exit is essentially a static function and doesn't use the |
| 712 | // receiver, so we have to get the current isolate from the thread |
| 713 | // local. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 714 | i::Isolate* isolate = i::Isolate::Current(); |
| 715 | if (!isolate->IsInitialized()) return; |
| 716 | |
| 717 | if (!ApiCheck(isolate->handle_scope_implementer()->LeaveLastContext(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 718 | "v8::Context::Exit()", |
| 719 | "Cannot exit non-entered context")) { |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | // Content of 'last_context' could be NULL. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 724 | i::Context* last_context = |
| 725 | isolate->handle_scope_implementer()->RestoreContext(); |
| 726 | isolate->set_context(last_context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 730 | void Context::SetData(v8::Handle<String> data) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 731 | i::Handle<i::Context> env = Utils::OpenHandle(this); |
| 732 | i::Isolate* isolate = env->GetIsolate(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 733 | if (IsDeadCheck(isolate, "v8::Context::SetData()")) return; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 734 | i::Handle<i::Object> raw_data = Utils::OpenHandle(*data); |
| 735 | ASSERT(env->IsGlobalContext()); |
| 736 | if (env->IsGlobalContext()) { |
| 737 | env->set_data(*raw_data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
| 741 | |
| 742 | v8::Local<v8::Value> Context::GetData() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 743 | i::Handle<i::Context> env = Utils::OpenHandle(this); |
| 744 | i::Isolate* isolate = env->GetIsolate(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 745 | if (IsDeadCheck(isolate, "v8::Context::GetData()")) { |
| 746 | return v8::Local<Value>(); |
| 747 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 748 | i::Object* raw_result = NULL; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 749 | ASSERT(env->IsGlobalContext()); |
| 750 | if (env->IsGlobalContext()) { |
| 751 | raw_result = env->data(); |
| 752 | } else { |
| 753 | return Local<Value>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 754 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 755 | i::Handle<i::Object> result(raw_result, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 756 | return Utils::ToLocal(result); |
| 757 | } |
| 758 | |
| 759 | |
| 760 | i::Object** v8::HandleScope::RawClose(i::Object** value) { |
| 761 | if (!ApiCheck(!is_closed_, |
| 762 | "v8::HandleScope::Close()", |
| 763 | "Local scope has already been closed")) { |
| 764 | return 0; |
| 765 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 766 | LOG_API(isolate_, "CloseHandleScope"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 767 | |
| 768 | // Read the result before popping the handle block. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 769 | i::Object* result = NULL; |
| 770 | if (value != NULL) { |
| 771 | result = *value; |
| 772 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 773 | is_closed_ = true; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 774 | Leave(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 775 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 776 | if (value == NULL) { |
| 777 | return NULL; |
| 778 | } |
| 779 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 780 | // Allocate a new handle on the previous handle block. |
| 781 | i::Handle<i::Object> handle(result); |
| 782 | return handle.location(); |
| 783 | } |
| 784 | |
| 785 | |
| 786 | // --- N e a n d e r --- |
| 787 | |
| 788 | |
| 789 | // A constructor cannot easily return an error value, therefore it is necessary |
| 790 | // to check for a dead VM with ON_BAILOUT before constructing any Neander |
| 791 | // objects. To remind you about this there is no HandleScope in the |
| 792 | // NeanderObject constructor. When you add one to the site calling the |
| 793 | // constructor you should check that you ensured the VM was not dead first. |
| 794 | NeanderObject::NeanderObject(int size) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 795 | i::Isolate* isolate = i::Isolate::Current(); |
| 796 | EnsureInitializedForIsolate(isolate, "v8::Nowhere"); |
| 797 | ENTER_V8(isolate); |
| 798 | value_ = isolate->factory()->NewNeanderObject(); |
| 799 | i::Handle<i::FixedArray> elements = isolate->factory()->NewFixedArray(size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 800 | value_->set_elements(*elements); |
| 801 | } |
| 802 | |
| 803 | |
| 804 | int NeanderObject::size() { |
| 805 | return i::FixedArray::cast(value_->elements())->length(); |
| 806 | } |
| 807 | |
| 808 | |
| 809 | NeanderArray::NeanderArray() : obj_(2) { |
| 810 | obj_.set(0, i::Smi::FromInt(0)); |
| 811 | } |
| 812 | |
| 813 | |
| 814 | int NeanderArray::length() { |
| 815 | return i::Smi::cast(obj_.get(0))->value(); |
| 816 | } |
| 817 | |
| 818 | |
| 819 | i::Object* NeanderArray::get(int offset) { |
| 820 | ASSERT(0 <= offset); |
| 821 | ASSERT(offset < length()); |
| 822 | return obj_.get(offset + 1); |
| 823 | } |
| 824 | |
| 825 | |
| 826 | // This method cannot easily return an error value, therefore it is necessary |
| 827 | // to check for a dead VM with ON_BAILOUT before calling it. To remind you |
| 828 | // about this there is no HandleScope in this method. When you add one to the |
| 829 | // site calling this method you should check that you ensured the VM was not |
| 830 | // dead first. |
| 831 | void NeanderArray::add(i::Handle<i::Object> value) { |
| 832 | int length = this->length(); |
| 833 | int size = obj_.size(); |
| 834 | if (length == size - 1) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 835 | i::Handle<i::FixedArray> new_elms = FACTORY->NewFixedArray(2 * size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 836 | for (int i = 0; i < length; i++) |
| 837 | new_elms->set(i + 1, get(i)); |
| 838 | obj_.value()->set_elements(*new_elms); |
| 839 | } |
| 840 | obj_.set(length + 1, *value); |
| 841 | obj_.set(0, i::Smi::FromInt(length + 1)); |
| 842 | } |
| 843 | |
| 844 | |
| 845 | void NeanderArray::set(int index, i::Object* value) { |
| 846 | if (index < 0 || index >= this->length()) return; |
| 847 | obj_.set(index + 1, value); |
| 848 | } |
| 849 | |
| 850 | |
| 851 | // --- T e m p l a t e --- |
| 852 | |
| 853 | |
| 854 | static void InitializeTemplate(i::Handle<i::TemplateInfo> that, int type) { |
| 855 | that->set_tag(i::Smi::FromInt(type)); |
| 856 | } |
| 857 | |
| 858 | |
| 859 | void Template::Set(v8::Handle<String> name, v8::Handle<Data> value, |
| 860 | v8::PropertyAttribute attribute) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 861 | i::Isolate* isolate = i::Isolate::Current(); |
| 862 | if (IsDeadCheck(isolate, "v8::Template::Set()")) return; |
| 863 | ENTER_V8(isolate); |
| 864 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 865 | i::Handle<i::Object> list(Utils::OpenHandle(this)->property_list()); |
| 866 | if (list->IsUndefined()) { |
| 867 | list = NeanderArray().value(); |
| 868 | Utils::OpenHandle(this)->set_property_list(*list); |
| 869 | } |
| 870 | NeanderArray array(list); |
| 871 | array.add(Utils::OpenHandle(*name)); |
| 872 | array.add(Utils::OpenHandle(*value)); |
| 873 | array.add(Utils::OpenHandle(*v8::Integer::New(attribute))); |
| 874 | } |
| 875 | |
| 876 | |
| 877 | // --- F u n c t i o n T e m p l a t e --- |
| 878 | static void InitializeFunctionTemplate( |
| 879 | i::Handle<i::FunctionTemplateInfo> info) { |
| 880 | info->set_tag(i::Smi::FromInt(Consts::FUNCTION_TEMPLATE)); |
| 881 | info->set_flag(0); |
| 882 | } |
| 883 | |
| 884 | |
| 885 | Local<ObjectTemplate> FunctionTemplate::PrototypeTemplate() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 886 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 887 | if (IsDeadCheck(isolate, "v8::FunctionTemplate::PrototypeTemplate()")) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 888 | return Local<ObjectTemplate>(); |
| 889 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 890 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 891 | i::Handle<i::Object> result(Utils::OpenHandle(this)->prototype_template()); |
| 892 | if (result->IsUndefined()) { |
| 893 | result = Utils::OpenHandle(*ObjectTemplate::New()); |
| 894 | Utils::OpenHandle(this)->set_prototype_template(*result); |
| 895 | } |
| 896 | return Local<ObjectTemplate>(ToApi<ObjectTemplate>(result)); |
| 897 | } |
| 898 | |
| 899 | |
| 900 | void FunctionTemplate::Inherit(v8::Handle<FunctionTemplate> value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 901 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 902 | if (IsDeadCheck(isolate, "v8::FunctionTemplate::Inherit()")) return; |
| 903 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 904 | Utils::OpenHandle(this)->set_parent_template(*Utils::OpenHandle(*value)); |
| 905 | } |
| 906 | |
| 907 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 908 | Local<FunctionTemplate> FunctionTemplate::New(InvocationCallback callback, |
| 909 | v8::Handle<Value> data, v8::Handle<Signature> signature) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 910 | i::Isolate* isolate = i::Isolate::Current(); |
| 911 | EnsureInitializedForIsolate(isolate, "v8::FunctionTemplate::New()"); |
| 912 | LOG_API(isolate, "FunctionTemplate::New"); |
| 913 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 914 | i::Handle<i::Struct> struct_obj = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 915 | isolate->factory()->NewStruct(i::FUNCTION_TEMPLATE_INFO_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 916 | i::Handle<i::FunctionTemplateInfo> obj = |
| 917 | i::Handle<i::FunctionTemplateInfo>::cast(struct_obj); |
| 918 | InitializeFunctionTemplate(obj); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 919 | int next_serial_number = isolate->next_serial_number(); |
| 920 | isolate->set_next_serial_number(next_serial_number + 1); |
| 921 | obj->set_serial_number(i::Smi::FromInt(next_serial_number)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 922 | if (callback != 0) { |
| 923 | if (data.IsEmpty()) data = v8::Undefined(); |
| 924 | Utils::ToLocal(obj)->SetCallHandler(callback, data); |
| 925 | } |
| 926 | obj->set_undetectable(false); |
| 927 | obj->set_needs_access_check(false); |
| 928 | |
| 929 | if (!signature.IsEmpty()) |
| 930 | obj->set_signature(*Utils::OpenHandle(*signature)); |
| 931 | return Utils::ToLocal(obj); |
| 932 | } |
| 933 | |
| 934 | |
| 935 | Local<Signature> Signature::New(Handle<FunctionTemplate> receiver, |
| 936 | int argc, Handle<FunctionTemplate> argv[]) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 937 | i::Isolate* isolate = i::Isolate::Current(); |
| 938 | EnsureInitializedForIsolate(isolate, "v8::Signature::New()"); |
| 939 | LOG_API(isolate, "Signature::New"); |
| 940 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 941 | i::Handle<i::Struct> struct_obj = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 942 | isolate->factory()->NewStruct(i::SIGNATURE_INFO_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 943 | i::Handle<i::SignatureInfo> obj = |
| 944 | i::Handle<i::SignatureInfo>::cast(struct_obj); |
| 945 | if (!receiver.IsEmpty()) obj->set_receiver(*Utils::OpenHandle(*receiver)); |
| 946 | if (argc > 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 947 | i::Handle<i::FixedArray> args = isolate->factory()->NewFixedArray(argc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 948 | for (int i = 0; i < argc; i++) { |
| 949 | if (!argv[i].IsEmpty()) |
| 950 | args->set(i, *Utils::OpenHandle(*argv[i])); |
| 951 | } |
| 952 | obj->set_args(*args); |
| 953 | } |
| 954 | return Utils::ToLocal(obj); |
| 955 | } |
| 956 | |
| 957 | |
| 958 | Local<TypeSwitch> TypeSwitch::New(Handle<FunctionTemplate> type) { |
| 959 | Handle<FunctionTemplate> types[1] = { type }; |
| 960 | return TypeSwitch::New(1, types); |
| 961 | } |
| 962 | |
| 963 | |
| 964 | Local<TypeSwitch> TypeSwitch::New(int argc, Handle<FunctionTemplate> types[]) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 965 | i::Isolate* isolate = i::Isolate::Current(); |
| 966 | EnsureInitializedForIsolate(isolate, "v8::TypeSwitch::New()"); |
| 967 | LOG_API(isolate, "TypeSwitch::New"); |
| 968 | ENTER_V8(isolate); |
| 969 | i::Handle<i::FixedArray> vector = isolate->factory()->NewFixedArray(argc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 970 | for (int i = 0; i < argc; i++) |
| 971 | vector->set(i, *Utils::OpenHandle(*types[i])); |
| 972 | i::Handle<i::Struct> struct_obj = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 973 | isolate->factory()->NewStruct(i::TYPE_SWITCH_INFO_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 974 | i::Handle<i::TypeSwitchInfo> obj = |
| 975 | i::Handle<i::TypeSwitchInfo>::cast(struct_obj); |
| 976 | obj->set_types(*vector); |
| 977 | return Utils::ToLocal(obj); |
| 978 | } |
| 979 | |
| 980 | |
| 981 | int TypeSwitch::match(v8::Handle<Value> value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 982 | i::Isolate* isolate = i::Isolate::Current(); |
| 983 | LOG_API(isolate, "TypeSwitch::match"); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 984 | USE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 985 | i::Handle<i::Object> obj = Utils::OpenHandle(*value); |
| 986 | i::Handle<i::TypeSwitchInfo> info = Utils::OpenHandle(this); |
| 987 | i::FixedArray* types = i::FixedArray::cast(info->types()); |
| 988 | for (int i = 0; i < types->length(); i++) { |
| 989 | if (obj->IsInstanceOf(i::FunctionTemplateInfo::cast(types->get(i)))) |
| 990 | return i + 1; |
| 991 | } |
| 992 | return 0; |
| 993 | } |
| 994 | |
| 995 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 996 | #define SET_FIELD_WRAPPED(obj, setter, cdata) do { \ |
| 997 | i::Handle<i::Object> foreign = FromCData(cdata); \ |
| 998 | (obj)->setter(*foreign); \ |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 999 | } while (false) |
| 1000 | |
| 1001 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1002 | void FunctionTemplate::SetCallHandler(InvocationCallback callback, |
| 1003 | v8::Handle<Value> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1004 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1005 | if (IsDeadCheck(isolate, "v8::FunctionTemplate::SetCallHandler()")) return; |
| 1006 | ENTER_V8(isolate); |
| 1007 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1008 | i::Handle<i::Struct> struct_obj = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1009 | isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1010 | i::Handle<i::CallHandlerInfo> obj = |
| 1011 | i::Handle<i::CallHandlerInfo>::cast(struct_obj); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1012 | SET_FIELD_WRAPPED(obj, set_callback, callback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1013 | if (data.IsEmpty()) data = v8::Undefined(); |
| 1014 | obj->set_data(*Utils::OpenHandle(*data)); |
| 1015 | Utils::OpenHandle(this)->set_call_code(*obj); |
| 1016 | } |
| 1017 | |
| 1018 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1019 | static i::Handle<i::AccessorInfo> MakeAccessorInfo( |
| 1020 | v8::Handle<String> name, |
| 1021 | AccessorGetter getter, |
| 1022 | AccessorSetter setter, |
| 1023 | v8::Handle<Value> data, |
| 1024 | v8::AccessControl settings, |
| 1025 | v8::PropertyAttribute attributes) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1026 | i::Handle<i::AccessorInfo> obj = FACTORY->NewAccessorInfo(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1027 | ASSERT(getter != NULL); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1028 | SET_FIELD_WRAPPED(obj, set_getter, getter); |
| 1029 | SET_FIELD_WRAPPED(obj, set_setter, setter); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1030 | if (data.IsEmpty()) data = v8::Undefined(); |
| 1031 | obj->set_data(*Utils::OpenHandle(*data)); |
| 1032 | obj->set_name(*Utils::OpenHandle(*name)); |
| 1033 | if (settings & ALL_CAN_READ) obj->set_all_can_read(true); |
| 1034 | if (settings & ALL_CAN_WRITE) obj->set_all_can_write(true); |
| 1035 | if (settings & PROHIBITS_OVERWRITING) obj->set_prohibits_overwriting(true); |
| 1036 | obj->set_property_attributes(static_cast<PropertyAttributes>(attributes)); |
| 1037 | return obj; |
| 1038 | } |
| 1039 | |
| 1040 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1041 | void FunctionTemplate::AddInstancePropertyAccessor( |
| 1042 | v8::Handle<String> name, |
| 1043 | AccessorGetter getter, |
| 1044 | AccessorSetter setter, |
| 1045 | v8::Handle<Value> data, |
| 1046 | v8::AccessControl settings, |
| 1047 | v8::PropertyAttribute attributes) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1048 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1049 | if (IsDeadCheck(isolate, |
| 1050 | "v8::FunctionTemplate::AddInstancePropertyAccessor()")) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1051 | return; |
| 1052 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1053 | ENTER_V8(isolate); |
| 1054 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1055 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1056 | i::Handle<i::AccessorInfo> obj = MakeAccessorInfo(name, |
| 1057 | getter, setter, data, |
| 1058 | settings, attributes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1059 | i::Handle<i::Object> list(Utils::OpenHandle(this)->property_accessors()); |
| 1060 | if (list->IsUndefined()) { |
| 1061 | list = NeanderArray().value(); |
| 1062 | Utils::OpenHandle(this)->set_property_accessors(*list); |
| 1063 | } |
| 1064 | NeanderArray array(list); |
| 1065 | array.add(obj); |
| 1066 | } |
| 1067 | |
| 1068 | |
| 1069 | Local<ObjectTemplate> FunctionTemplate::InstanceTemplate() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1070 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1071 | if (IsDeadCheck(isolate, "v8::FunctionTemplate::InstanceTemplate()") |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1072 | || EmptyCheck("v8::FunctionTemplate::InstanceTemplate()", this)) |
| 1073 | return Local<ObjectTemplate>(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1074 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1075 | if (Utils::OpenHandle(this)->instance_template()->IsUndefined()) { |
| 1076 | Local<ObjectTemplate> templ = |
| 1077 | ObjectTemplate::New(v8::Handle<FunctionTemplate>(this)); |
| 1078 | Utils::OpenHandle(this)->set_instance_template(*Utils::OpenHandle(*templ)); |
| 1079 | } |
| 1080 | i::Handle<i::ObjectTemplateInfo> result(i::ObjectTemplateInfo::cast( |
| 1081 | Utils::OpenHandle(this)->instance_template())); |
| 1082 | return Utils::ToLocal(result); |
| 1083 | } |
| 1084 | |
| 1085 | |
| 1086 | void FunctionTemplate::SetClassName(Handle<String> name) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1087 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1088 | if (IsDeadCheck(isolate, "v8::FunctionTemplate::SetClassName()")) return; |
| 1089 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1090 | Utils::OpenHandle(this)->set_class_name(*Utils::OpenHandle(*name)); |
| 1091 | } |
| 1092 | |
| 1093 | |
| 1094 | void FunctionTemplate::SetHiddenPrototype(bool value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1095 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1096 | if (IsDeadCheck(isolate, "v8::FunctionTemplate::SetHiddenPrototype()")) { |
| 1097 | return; |
| 1098 | } |
| 1099 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1100 | Utils::OpenHandle(this)->set_hidden_prototype(value); |
| 1101 | } |
| 1102 | |
| 1103 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1104 | void FunctionTemplate::ReadOnlyPrototype() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1105 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1106 | if (IsDeadCheck(isolate, "v8::FunctionTemplate::SetPrototypeAttributes()")) { |
| 1107 | return; |
| 1108 | } |
| 1109 | ENTER_V8(isolate); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1110 | Utils::OpenHandle(this)->set_read_only_prototype(true); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1114 | void FunctionTemplate::SetNamedInstancePropertyHandler( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1115 | NamedPropertyGetter getter, |
| 1116 | NamedPropertySetter setter, |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1117 | NamedPropertyQuery query, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1118 | NamedPropertyDeleter remover, |
| 1119 | NamedPropertyEnumerator enumerator, |
| 1120 | Handle<Value> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1121 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1122 | if (IsDeadCheck(isolate, |
| 1123 | "v8::FunctionTemplate::SetNamedInstancePropertyHandler()")) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1124 | return; |
| 1125 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1126 | ENTER_V8(isolate); |
| 1127 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1128 | i::Handle<i::Struct> struct_obj = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1129 | isolate->factory()->NewStruct(i::INTERCEPTOR_INFO_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1130 | i::Handle<i::InterceptorInfo> obj = |
| 1131 | i::Handle<i::InterceptorInfo>::cast(struct_obj); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1132 | |
| 1133 | if (getter != 0) SET_FIELD_WRAPPED(obj, set_getter, getter); |
| 1134 | if (setter != 0) SET_FIELD_WRAPPED(obj, set_setter, setter); |
| 1135 | if (query != 0) SET_FIELD_WRAPPED(obj, set_query, query); |
| 1136 | if (remover != 0) SET_FIELD_WRAPPED(obj, set_deleter, remover); |
| 1137 | if (enumerator != 0) SET_FIELD_WRAPPED(obj, set_enumerator, enumerator); |
| 1138 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1139 | if (data.IsEmpty()) data = v8::Undefined(); |
| 1140 | obj->set_data(*Utils::OpenHandle(*data)); |
| 1141 | Utils::OpenHandle(this)->set_named_property_handler(*obj); |
| 1142 | } |
| 1143 | |
| 1144 | |
| 1145 | void FunctionTemplate::SetIndexedInstancePropertyHandler( |
| 1146 | IndexedPropertyGetter getter, |
| 1147 | IndexedPropertySetter setter, |
| 1148 | IndexedPropertyQuery query, |
| 1149 | IndexedPropertyDeleter remover, |
| 1150 | IndexedPropertyEnumerator enumerator, |
| 1151 | Handle<Value> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1152 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1153 | if (IsDeadCheck(isolate, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1154 | "v8::FunctionTemplate::SetIndexedInstancePropertyHandler()")) { |
| 1155 | return; |
| 1156 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1157 | ENTER_V8(isolate); |
| 1158 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1159 | i::Handle<i::Struct> struct_obj = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1160 | isolate->factory()->NewStruct(i::INTERCEPTOR_INFO_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1161 | i::Handle<i::InterceptorInfo> obj = |
| 1162 | i::Handle<i::InterceptorInfo>::cast(struct_obj); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1163 | |
| 1164 | if (getter != 0) SET_FIELD_WRAPPED(obj, set_getter, getter); |
| 1165 | if (setter != 0) SET_FIELD_WRAPPED(obj, set_setter, setter); |
| 1166 | if (query != 0) SET_FIELD_WRAPPED(obj, set_query, query); |
| 1167 | if (remover != 0) SET_FIELD_WRAPPED(obj, set_deleter, remover); |
| 1168 | if (enumerator != 0) SET_FIELD_WRAPPED(obj, set_enumerator, enumerator); |
| 1169 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1170 | if (data.IsEmpty()) data = v8::Undefined(); |
| 1171 | obj->set_data(*Utils::OpenHandle(*data)); |
| 1172 | Utils::OpenHandle(this)->set_indexed_property_handler(*obj); |
| 1173 | } |
| 1174 | |
| 1175 | |
| 1176 | void FunctionTemplate::SetInstanceCallAsFunctionHandler( |
| 1177 | InvocationCallback callback, |
| 1178 | Handle<Value> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1179 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1180 | if (IsDeadCheck(isolate, |
| 1181 | "v8::FunctionTemplate::SetInstanceCallAsFunctionHandler()")) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1182 | return; |
| 1183 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1184 | ENTER_V8(isolate); |
| 1185 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1186 | i::Handle<i::Struct> struct_obj = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1187 | isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1188 | i::Handle<i::CallHandlerInfo> obj = |
| 1189 | i::Handle<i::CallHandlerInfo>::cast(struct_obj); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1190 | SET_FIELD_WRAPPED(obj, set_callback, callback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1191 | if (data.IsEmpty()) data = v8::Undefined(); |
| 1192 | obj->set_data(*Utils::OpenHandle(*data)); |
| 1193 | Utils::OpenHandle(this)->set_instance_call_handler(*obj); |
| 1194 | } |
| 1195 | |
| 1196 | |
| 1197 | // --- O b j e c t T e m p l a t e --- |
| 1198 | |
| 1199 | |
| 1200 | Local<ObjectTemplate> ObjectTemplate::New() { |
| 1201 | return New(Local<FunctionTemplate>()); |
| 1202 | } |
| 1203 | |
| 1204 | |
| 1205 | Local<ObjectTemplate> ObjectTemplate::New( |
| 1206 | v8::Handle<FunctionTemplate> constructor) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1207 | i::Isolate* isolate = i::Isolate::Current(); |
| 1208 | if (IsDeadCheck(isolate, "v8::ObjectTemplate::New()")) { |
| 1209 | return Local<ObjectTemplate>(); |
| 1210 | } |
| 1211 | EnsureInitializedForIsolate(isolate, "v8::ObjectTemplate::New()"); |
| 1212 | LOG_API(isolate, "ObjectTemplate::New"); |
| 1213 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1214 | i::Handle<i::Struct> struct_obj = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1215 | isolate->factory()->NewStruct(i::OBJECT_TEMPLATE_INFO_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1216 | i::Handle<i::ObjectTemplateInfo> obj = |
| 1217 | i::Handle<i::ObjectTemplateInfo>::cast(struct_obj); |
| 1218 | InitializeTemplate(obj, Consts::OBJECT_TEMPLATE); |
| 1219 | if (!constructor.IsEmpty()) |
| 1220 | obj->set_constructor(*Utils::OpenHandle(*constructor)); |
| 1221 | obj->set_internal_field_count(i::Smi::FromInt(0)); |
| 1222 | return Utils::ToLocal(obj); |
| 1223 | } |
| 1224 | |
| 1225 | |
| 1226 | // Ensure that the object template has a constructor. If no |
| 1227 | // constructor is available we create one. |
| 1228 | static void EnsureConstructor(ObjectTemplate* object_template) { |
| 1229 | if (Utils::OpenHandle(object_template)->constructor()->IsUndefined()) { |
| 1230 | Local<FunctionTemplate> templ = FunctionTemplate::New(); |
| 1231 | i::Handle<i::FunctionTemplateInfo> constructor = Utils::OpenHandle(*templ); |
| 1232 | constructor->set_instance_template(*Utils::OpenHandle(object_template)); |
| 1233 | Utils::OpenHandle(object_template)->set_constructor(*constructor); |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | |
| 1238 | void ObjectTemplate::SetAccessor(v8::Handle<String> name, |
| 1239 | AccessorGetter getter, |
| 1240 | AccessorSetter setter, |
| 1241 | v8::Handle<Value> data, |
| 1242 | AccessControl settings, |
| 1243 | PropertyAttribute attribute) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1244 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1245 | if (IsDeadCheck(isolate, "v8::ObjectTemplate::SetAccessor()")) return; |
| 1246 | ENTER_V8(isolate); |
| 1247 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1248 | EnsureConstructor(this); |
| 1249 | i::FunctionTemplateInfo* constructor = |
| 1250 | i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor()); |
| 1251 | i::Handle<i::FunctionTemplateInfo> cons(constructor); |
| 1252 | Utils::ToLocal(cons)->AddInstancePropertyAccessor(name, |
| 1253 | getter, |
| 1254 | setter, |
| 1255 | data, |
| 1256 | settings, |
| 1257 | attribute); |
| 1258 | } |
| 1259 | |
| 1260 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1261 | void ObjectTemplate::SetNamedPropertyHandler(NamedPropertyGetter getter, |
| 1262 | NamedPropertySetter setter, |
| 1263 | NamedPropertyQuery query, |
| 1264 | NamedPropertyDeleter remover, |
| 1265 | NamedPropertyEnumerator enumerator, |
| 1266 | Handle<Value> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1267 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1268 | if (IsDeadCheck(isolate, "v8::ObjectTemplate::SetNamedPropertyHandler()")) { |
| 1269 | return; |
| 1270 | } |
| 1271 | ENTER_V8(isolate); |
| 1272 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1273 | EnsureConstructor(this); |
| 1274 | i::FunctionTemplateInfo* constructor = |
| 1275 | i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor()); |
| 1276 | i::Handle<i::FunctionTemplateInfo> cons(constructor); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1277 | Utils::ToLocal(cons)->SetNamedInstancePropertyHandler(getter, |
| 1278 | setter, |
| 1279 | query, |
| 1280 | remover, |
| 1281 | enumerator, |
| 1282 | data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | |
| 1286 | void ObjectTemplate::MarkAsUndetectable() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1287 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1288 | if (IsDeadCheck(isolate, "v8::ObjectTemplate::MarkAsUndetectable()")) return; |
| 1289 | ENTER_V8(isolate); |
| 1290 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1291 | EnsureConstructor(this); |
| 1292 | i::FunctionTemplateInfo* constructor = |
| 1293 | i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor()); |
| 1294 | i::Handle<i::FunctionTemplateInfo> cons(constructor); |
| 1295 | cons->set_undetectable(true); |
| 1296 | } |
| 1297 | |
| 1298 | |
| 1299 | void ObjectTemplate::SetAccessCheckCallbacks( |
| 1300 | NamedSecurityCallback named_callback, |
| 1301 | IndexedSecurityCallback indexed_callback, |
| 1302 | Handle<Value> data, |
| 1303 | bool turned_on_by_default) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1304 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1305 | if (IsDeadCheck(isolate, "v8::ObjectTemplate::SetAccessCheckCallbacks()")) { |
| 1306 | return; |
| 1307 | } |
| 1308 | ENTER_V8(isolate); |
| 1309 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1310 | EnsureConstructor(this); |
| 1311 | |
| 1312 | i::Handle<i::Struct> struct_info = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1313 | isolate->factory()->NewStruct(i::ACCESS_CHECK_INFO_TYPE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1314 | i::Handle<i::AccessCheckInfo> info = |
| 1315 | i::Handle<i::AccessCheckInfo>::cast(struct_info); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1316 | |
| 1317 | SET_FIELD_WRAPPED(info, set_named_callback, named_callback); |
| 1318 | SET_FIELD_WRAPPED(info, set_indexed_callback, indexed_callback); |
| 1319 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1320 | if (data.IsEmpty()) data = v8::Undefined(); |
| 1321 | info->set_data(*Utils::OpenHandle(*data)); |
| 1322 | |
| 1323 | i::FunctionTemplateInfo* constructor = |
| 1324 | i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor()); |
| 1325 | i::Handle<i::FunctionTemplateInfo> cons(constructor); |
| 1326 | cons->set_access_check_info(*info); |
| 1327 | cons->set_needs_access_check(turned_on_by_default); |
| 1328 | } |
| 1329 | |
| 1330 | |
| 1331 | void ObjectTemplate::SetIndexedPropertyHandler( |
| 1332 | IndexedPropertyGetter getter, |
| 1333 | IndexedPropertySetter setter, |
| 1334 | IndexedPropertyQuery query, |
| 1335 | IndexedPropertyDeleter remover, |
| 1336 | IndexedPropertyEnumerator enumerator, |
| 1337 | Handle<Value> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1338 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1339 | if (IsDeadCheck(isolate, "v8::ObjectTemplate::SetIndexedPropertyHandler()")) { |
| 1340 | return; |
| 1341 | } |
| 1342 | ENTER_V8(isolate); |
| 1343 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1344 | EnsureConstructor(this); |
| 1345 | i::FunctionTemplateInfo* constructor = |
| 1346 | i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor()); |
| 1347 | i::Handle<i::FunctionTemplateInfo> cons(constructor); |
| 1348 | Utils::ToLocal(cons)->SetIndexedInstancePropertyHandler(getter, |
| 1349 | setter, |
| 1350 | query, |
| 1351 | remover, |
| 1352 | enumerator, |
| 1353 | data); |
| 1354 | } |
| 1355 | |
| 1356 | |
| 1357 | void ObjectTemplate::SetCallAsFunctionHandler(InvocationCallback callback, |
| 1358 | Handle<Value> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1359 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1360 | if (IsDeadCheck(isolate, |
| 1361 | "v8::ObjectTemplate::SetCallAsFunctionHandler()")) { |
| 1362 | return; |
| 1363 | } |
| 1364 | ENTER_V8(isolate); |
| 1365 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1366 | EnsureConstructor(this); |
| 1367 | i::FunctionTemplateInfo* constructor = |
| 1368 | i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor()); |
| 1369 | i::Handle<i::FunctionTemplateInfo> cons(constructor); |
| 1370 | Utils::ToLocal(cons)->SetInstanceCallAsFunctionHandler(callback, data); |
| 1371 | } |
| 1372 | |
| 1373 | |
| 1374 | int ObjectTemplate::InternalFieldCount() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1375 | if (IsDeadCheck(Utils::OpenHandle(this)->GetIsolate(), |
| 1376 | "v8::ObjectTemplate::InternalFieldCount()")) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1377 | return 0; |
| 1378 | } |
| 1379 | return i::Smi::cast(Utils::OpenHandle(this)->internal_field_count())->value(); |
| 1380 | } |
| 1381 | |
| 1382 | |
| 1383 | void ObjectTemplate::SetInternalFieldCount(int value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1384 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1385 | if (IsDeadCheck(isolate, "v8::ObjectTemplate::SetInternalFieldCount()")) { |
| 1386 | return; |
| 1387 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1388 | if (!ApiCheck(i::Smi::IsValid(value), |
| 1389 | "v8::ObjectTemplate::SetInternalFieldCount()", |
| 1390 | "Invalid internal field count")) { |
| 1391 | return; |
| 1392 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1393 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1394 | if (value > 0) { |
| 1395 | // The internal field count is set by the constructor function's |
| 1396 | // construct code, so we ensure that there is a constructor |
| 1397 | // function to do the setting. |
| 1398 | EnsureConstructor(this); |
| 1399 | } |
| 1400 | Utils::OpenHandle(this)->set_internal_field_count(i::Smi::FromInt(value)); |
| 1401 | } |
| 1402 | |
| 1403 | |
| 1404 | // --- S c r i p t D a t a --- |
| 1405 | |
| 1406 | |
| 1407 | ScriptData* ScriptData::PreCompile(const char* input, int length) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1408 | i::Utf8ToUC16CharacterStream stream( |
| 1409 | reinterpret_cast<const unsigned char*>(input), length); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1410 | return i::ParserApi::PreParse(&stream, NULL, i::FLAG_harmony_block_scoping); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1414 | ScriptData* ScriptData::PreCompile(v8::Handle<String> source) { |
| 1415 | i::Handle<i::String> str = Utils::OpenHandle(*source); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1416 | if (str->IsExternalTwoByteString()) { |
| 1417 | i::ExternalTwoByteStringUC16CharacterStream stream( |
| 1418 | i::Handle<i::ExternalTwoByteString>::cast(str), 0, str->length()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1419 | return i::ParserApi::PreParse(&stream, NULL, i::FLAG_harmony_block_scoping); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1420 | } else { |
| 1421 | i::GenericStringUC16CharacterStream stream(str, 0, str->length()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1422 | return i::ParserApi::PreParse(&stream, NULL, i::FLAG_harmony_block_scoping); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1423 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1427 | ScriptData* ScriptData::New(const char* data, int length) { |
| 1428 | // Return an empty ScriptData if the length is obviously invalid. |
| 1429 | if (length % sizeof(unsigned) != 0) { |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 1430 | return new i::ScriptDataImpl(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | // Copy the data to ensure it is properly aligned. |
| 1434 | int deserialized_data_length = length / sizeof(unsigned); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 1435 | // If aligned, don't create a copy of the data. |
| 1436 | if (reinterpret_cast<intptr_t>(data) % sizeof(unsigned) == 0) { |
| 1437 | return new i::ScriptDataImpl(data, length); |
| 1438 | } |
| 1439 | // Copy the data to align it. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1440 | unsigned* deserialized_data = i::NewArray<unsigned>(deserialized_data_length); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1441 | i::OS::MemCopy(deserialized_data, data, length); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1442 | |
| 1443 | return new i::ScriptDataImpl( |
| 1444 | i::Vector<unsigned>(deserialized_data, deserialized_data_length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | |
| 1448 | // --- S c r i p t --- |
| 1449 | |
| 1450 | |
| 1451 | Local<Script> Script::New(v8::Handle<String> source, |
| 1452 | v8::ScriptOrigin* origin, |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1453 | v8::ScriptData* pre_data, |
| 1454 | v8::Handle<String> script_data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1455 | i::Isolate* isolate = i::Isolate::Current(); |
| 1456 | ON_BAILOUT(isolate, "v8::Script::New()", return Local<Script>()); |
| 1457 | LOG_API(isolate, "Script::New"); |
| 1458 | ENTER_V8(isolate); |
Ben Murdoch | 692be65 | 2012-01-10 18:47:50 +0000 | [diff] [blame^] | 1459 | i::SharedFunctionInfo* raw_result = NULL; |
| 1460 | { i::HandleScope scope(isolate); |
| 1461 | i::Handle<i::String> str = Utils::OpenHandle(*source); |
| 1462 | i::Handle<i::Object> name_obj; |
| 1463 | int line_offset = 0; |
| 1464 | int column_offset = 0; |
| 1465 | if (origin != NULL) { |
| 1466 | if (!origin->ResourceName().IsEmpty()) { |
| 1467 | name_obj = Utils::OpenHandle(*origin->ResourceName()); |
| 1468 | } |
| 1469 | if (!origin->ResourceLineOffset().IsEmpty()) { |
| 1470 | line_offset = static_cast<int>(origin->ResourceLineOffset()->Value()); |
| 1471 | } |
| 1472 | if (!origin->ResourceColumnOffset().IsEmpty()) { |
| 1473 | column_offset = |
| 1474 | static_cast<int>(origin->ResourceColumnOffset()->Value()); |
| 1475 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1476 | } |
Ben Murdoch | 692be65 | 2012-01-10 18:47:50 +0000 | [diff] [blame^] | 1477 | EXCEPTION_PREAMBLE(isolate); |
| 1478 | i::ScriptDataImpl* pre_data_impl = |
| 1479 | static_cast<i::ScriptDataImpl*>(pre_data); |
| 1480 | // We assert that the pre-data is sane, even though we can actually |
| 1481 | // handle it if it turns out not to be in release mode. |
| 1482 | ASSERT(pre_data_impl == NULL || pre_data_impl->SanityCheck()); |
| 1483 | // If the pre-data isn't sane we simply ignore it |
| 1484 | if (pre_data_impl != NULL && !pre_data_impl->SanityCheck()) { |
| 1485 | pre_data_impl = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1486 | } |
Ben Murdoch | 692be65 | 2012-01-10 18:47:50 +0000 | [diff] [blame^] | 1487 | i::Handle<i::SharedFunctionInfo> result = |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1488 | i::Compiler::Compile(str, |
| 1489 | name_obj, |
| 1490 | line_offset, |
| 1491 | column_offset, |
| 1492 | NULL, |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1493 | pre_data_impl, |
| 1494 | Utils::OpenHandle(*script_data), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1495 | i::NOT_NATIVES_CODE); |
Ben Murdoch | 692be65 | 2012-01-10 18:47:50 +0000 | [diff] [blame^] | 1496 | has_pending_exception = result.is_null(); |
| 1497 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Script>()); |
| 1498 | raw_result = *result; |
| 1499 | } |
| 1500 | i::Handle<i::SharedFunctionInfo> result(raw_result, isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1501 | return Local<Script>(ToApi<Script>(result)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | |
| 1505 | Local<Script> Script::New(v8::Handle<String> source, |
| 1506 | v8::Handle<Value> file_name) { |
| 1507 | ScriptOrigin origin(file_name); |
| 1508 | return New(source, &origin); |
| 1509 | } |
| 1510 | |
| 1511 | |
| 1512 | Local<Script> Script::Compile(v8::Handle<String> source, |
| 1513 | v8::ScriptOrigin* origin, |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1514 | v8::ScriptData* pre_data, |
| 1515 | v8::Handle<String> script_data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1516 | i::Isolate* isolate = i::Isolate::Current(); |
| 1517 | ON_BAILOUT(isolate, "v8::Script::Compile()", return Local<Script>()); |
| 1518 | LOG_API(isolate, "Script::Compile"); |
| 1519 | ENTER_V8(isolate); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1520 | Local<Script> generic = New(source, origin, pre_data, script_data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1521 | if (generic.IsEmpty()) |
| 1522 | return generic; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1523 | i::Handle<i::Object> obj = Utils::OpenHandle(*generic); |
| 1524 | i::Handle<i::SharedFunctionInfo> function = |
| 1525 | i::Handle<i::SharedFunctionInfo>(i::SharedFunctionInfo::cast(*obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1526 | i::Handle<i::JSFunction> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1527 | isolate->factory()->NewFunctionFromSharedFunctionInfo( |
| 1528 | function, |
| 1529 | isolate->global_context()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1530 | return Local<Script>(ToApi<Script>(result)); |
| 1531 | } |
| 1532 | |
| 1533 | |
| 1534 | Local<Script> Script::Compile(v8::Handle<String> source, |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1535 | v8::Handle<Value> file_name, |
| 1536 | v8::Handle<String> script_data) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1537 | ScriptOrigin origin(file_name); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1538 | return Compile(source, &origin, 0, script_data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | |
| 1542 | Local<Value> Script::Run() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1543 | i::Isolate* isolate = i::Isolate::Current(); |
| 1544 | ON_BAILOUT(isolate, "v8::Script::Run()", return Local<Value>()); |
| 1545 | LOG_API(isolate, "Script::Run"); |
| 1546 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1547 | i::Object* raw_result = NULL; |
| 1548 | { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1549 | i::HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1550 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 1551 | i::Handle<i::JSFunction> fun; |
| 1552 | if (obj->IsSharedFunctionInfo()) { |
| 1553 | i::Handle<i::SharedFunctionInfo> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1554 | function_info(i::SharedFunctionInfo::cast(*obj), isolate); |
| 1555 | fun = isolate->factory()->NewFunctionFromSharedFunctionInfo( |
| 1556 | function_info, isolate->global_context()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1557 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1558 | fun = i::Handle<i::JSFunction>(i::JSFunction::cast(*obj), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1559 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1560 | EXCEPTION_PREAMBLE(isolate); |
| 1561 | i::Handle<i::Object> receiver( |
| 1562 | isolate->context()->global_proxy(), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1563 | i::Handle<i::Object> result = |
| 1564 | i::Execution::Call(fun, receiver, 0, NULL, &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1565 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1566 | raw_result = *result; |
| 1567 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1568 | i::Handle<i::Object> result(raw_result, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1569 | return Utils::ToLocal(result); |
| 1570 | } |
| 1571 | |
| 1572 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1573 | static i::Handle<i::SharedFunctionInfo> OpenScript(Script* script) { |
| 1574 | i::Handle<i::Object> obj = Utils::OpenHandle(script); |
| 1575 | i::Handle<i::SharedFunctionInfo> result; |
| 1576 | if (obj->IsSharedFunctionInfo()) { |
| 1577 | result = |
| 1578 | i::Handle<i::SharedFunctionInfo>(i::SharedFunctionInfo::cast(*obj)); |
| 1579 | } else { |
| 1580 | result = |
| 1581 | i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared()); |
| 1582 | } |
| 1583 | return result; |
| 1584 | } |
| 1585 | |
| 1586 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1587 | Local<Value> Script::Id() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1588 | i::Isolate* isolate = i::Isolate::Current(); |
| 1589 | ON_BAILOUT(isolate, "v8::Script::Id()", return Local<Value>()); |
| 1590 | LOG_API(isolate, "Script::Id"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1591 | i::Object* raw_id = NULL; |
| 1592 | { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1593 | i::HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1594 | i::Handle<i::SharedFunctionInfo> function_info = OpenScript(this); |
| 1595 | i::Handle<i::Script> script(i::Script::cast(function_info->script())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1596 | i::Handle<i::Object> id(script->id()); |
| 1597 | raw_id = *id; |
| 1598 | } |
| 1599 | i::Handle<i::Object> id(raw_id); |
| 1600 | return Utils::ToLocal(id); |
| 1601 | } |
| 1602 | |
| 1603 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1604 | void Script::SetData(v8::Handle<String> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1605 | i::Isolate* isolate = i::Isolate::Current(); |
| 1606 | ON_BAILOUT(isolate, "v8::Script::SetData()", return); |
| 1607 | LOG_API(isolate, "Script::SetData"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1608 | { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1609 | i::HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1610 | i::Handle<i::SharedFunctionInfo> function_info = OpenScript(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1611 | i::Handle<i::Object> raw_data = Utils::OpenHandle(*data); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1612 | i::Handle<i::Script> script(i::Script::cast(function_info->script())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1613 | script->set_data(*raw_data); |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | |
| 1618 | // --- E x c e p t i o n s --- |
| 1619 | |
| 1620 | |
| 1621 | v8::TryCatch::TryCatch() |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1622 | : isolate_(i::Isolate::Current()), |
| 1623 | next_(isolate_->try_catch_handler_address()), |
| 1624 | exception_(isolate_->heap()->the_hole_value()), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1625 | message_(i::Smi::FromInt(0)), |
| 1626 | is_verbose_(false), |
| 1627 | can_continue_(true), |
| 1628 | capture_message_(true), |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1629 | rethrow_(false) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1630 | isolate_->RegisterTryCatchHandler(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | |
| 1634 | v8::TryCatch::~TryCatch() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1635 | ASSERT(isolate_ == i::Isolate::Current()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1636 | if (rethrow_) { |
| 1637 | v8::HandleScope scope; |
| 1638 | v8::Local<v8::Value> exc = v8::Local<v8::Value>::New(Exception()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1639 | isolate_->UnregisterTryCatchHandler(this); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1640 | v8::ThrowException(exc); |
| 1641 | } else { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1642 | isolate_->UnregisterTryCatchHandler(this); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1643 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | |
| 1647 | bool v8::TryCatch::HasCaught() const { |
| 1648 | return !reinterpret_cast<i::Object*>(exception_)->IsTheHole(); |
| 1649 | } |
| 1650 | |
| 1651 | |
| 1652 | bool v8::TryCatch::CanContinue() const { |
| 1653 | return can_continue_; |
| 1654 | } |
| 1655 | |
| 1656 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1657 | v8::Handle<v8::Value> v8::TryCatch::ReThrow() { |
| 1658 | if (!HasCaught()) return v8::Local<v8::Value>(); |
| 1659 | rethrow_ = true; |
| 1660 | return v8::Undefined(); |
| 1661 | } |
| 1662 | |
| 1663 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1664 | v8::Local<Value> v8::TryCatch::Exception() const { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1665 | ASSERT(isolate_ == i::Isolate::Current()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1666 | if (HasCaught()) { |
| 1667 | // Check for out of memory exception. |
| 1668 | i::Object* exception = reinterpret_cast<i::Object*>(exception_); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1669 | return v8::Utils::ToLocal(i::Handle<i::Object>(exception, isolate_)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1670 | } else { |
| 1671 | return v8::Local<Value>(); |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | |
| 1676 | v8::Local<Value> v8::TryCatch::StackTrace() const { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1677 | ASSERT(isolate_ == i::Isolate::Current()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1678 | if (HasCaught()) { |
| 1679 | i::Object* raw_obj = reinterpret_cast<i::Object*>(exception_); |
| 1680 | if (!raw_obj->IsJSObject()) return v8::Local<Value>(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1681 | i::HandleScope scope(isolate_); |
| 1682 | i::Handle<i::JSObject> obj(i::JSObject::cast(raw_obj), isolate_); |
| 1683 | i::Handle<i::String> name = isolate_->factory()->LookupAsciiSymbol("stack"); |
| 1684 | if (!obj->HasProperty(*name)) return v8::Local<Value>(); |
| 1685 | i::Handle<i::Object> value = i::GetProperty(obj, name); |
| 1686 | if (value.is_null()) return v8::Local<Value>(); |
| 1687 | return v8::Utils::ToLocal(scope.CloseAndEscape(value)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1688 | } else { |
| 1689 | return v8::Local<Value>(); |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | |
| 1694 | v8::Local<v8::Message> v8::TryCatch::Message() const { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1695 | ASSERT(isolate_ == i::Isolate::Current()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1696 | if (HasCaught() && message_ != i::Smi::FromInt(0)) { |
| 1697 | i::Object* message = reinterpret_cast<i::Object*>(message_); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1698 | return v8::Utils::MessageToLocal(i::Handle<i::Object>(message, isolate_)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1699 | } else { |
| 1700 | return v8::Local<v8::Message>(); |
| 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | |
| 1705 | void v8::TryCatch::Reset() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1706 | ASSERT(isolate_ == i::Isolate::Current()); |
| 1707 | exception_ = isolate_->heap()->the_hole_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1708 | message_ = i::Smi::FromInt(0); |
| 1709 | } |
| 1710 | |
| 1711 | |
| 1712 | void v8::TryCatch::SetVerbose(bool value) { |
| 1713 | is_verbose_ = value; |
| 1714 | } |
| 1715 | |
| 1716 | |
| 1717 | void v8::TryCatch::SetCaptureMessage(bool value) { |
| 1718 | capture_message_ = value; |
| 1719 | } |
| 1720 | |
| 1721 | |
| 1722 | // --- M e s s a g e --- |
| 1723 | |
| 1724 | |
| 1725 | Local<String> Message::Get() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1726 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1727 | ON_BAILOUT(isolate, "v8::Message::Get()", return Local<String>()); |
| 1728 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1729 | HandleScope scope; |
| 1730 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 1731 | i::Handle<i::String> raw_result = i::MessageHandler::GetMessage(obj); |
| 1732 | Local<String> result = Utils::ToLocal(raw_result); |
| 1733 | return scope.Close(result); |
| 1734 | } |
| 1735 | |
| 1736 | |
| 1737 | v8::Handle<Value> Message::GetScriptResourceName() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1738 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1739 | if (IsDeadCheck(isolate, "v8::Message::GetScriptResourceName()")) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1740 | return Local<String>(); |
| 1741 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1742 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1743 | HandleScope scope; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1744 | i::Handle<i::JSMessageObject> message = |
| 1745 | i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1746 | // Return this.script.name. |
| 1747 | i::Handle<i::JSValue> script = |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1748 | i::Handle<i::JSValue>::cast(i::Handle<i::Object>(message->script())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1749 | i::Handle<i::Object> resource_name(i::Script::cast(script->value())->name()); |
| 1750 | return scope.Close(Utils::ToLocal(resource_name)); |
| 1751 | } |
| 1752 | |
| 1753 | |
| 1754 | v8::Handle<Value> Message::GetScriptData() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1755 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1756 | if (IsDeadCheck(isolate, "v8::Message::GetScriptResourceData()")) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1757 | return Local<Value>(); |
| 1758 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1759 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1760 | HandleScope scope; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1761 | i::Handle<i::JSMessageObject> message = |
| 1762 | i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1763 | // Return this.script.data. |
| 1764 | i::Handle<i::JSValue> script = |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1765 | i::Handle<i::JSValue>::cast(i::Handle<i::Object>(message->script())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1766 | i::Handle<i::Object> data(i::Script::cast(script->value())->data()); |
| 1767 | return scope.Close(Utils::ToLocal(data)); |
| 1768 | } |
| 1769 | |
| 1770 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1771 | v8::Handle<v8::StackTrace> Message::GetStackTrace() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1772 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1773 | if (IsDeadCheck(isolate, "v8::Message::GetStackTrace()")) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1774 | return Local<v8::StackTrace>(); |
| 1775 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1776 | ENTER_V8(isolate); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1777 | HandleScope scope; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1778 | i::Handle<i::JSMessageObject> message = |
| 1779 | i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this)); |
| 1780 | i::Handle<i::Object> stackFramesObj(message->stack_frames()); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1781 | if (!stackFramesObj->IsJSArray()) return v8::Handle<v8::StackTrace>(); |
| 1782 | i::Handle<i::JSArray> stackTrace = |
| 1783 | i::Handle<i::JSArray>::cast(stackFramesObj); |
| 1784 | return scope.Close(Utils::StackTraceToLocal(stackTrace)); |
| 1785 | } |
| 1786 | |
| 1787 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1788 | static i::Handle<i::Object> CallV8HeapFunction(const char* name, |
| 1789 | i::Handle<i::Object> recv, |
| 1790 | int argc, |
| 1791 | i::Object** argv[], |
| 1792 | bool* has_pending_exception) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1793 | i::Isolate* isolate = i::Isolate::Current(); |
| 1794 | i::Handle<i::String> fmt_str = isolate->factory()->LookupAsciiSymbol(name); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1795 | i::Object* object_fun = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1796 | isolate->js_builtins_object()->GetPropertyNoExceptionThrown(*fmt_str); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1797 | i::Handle<i::JSFunction> fun = |
| 1798 | i::Handle<i::JSFunction>(i::JSFunction::cast(object_fun)); |
| 1799 | i::Handle<i::Object> value = |
| 1800 | i::Execution::Call(fun, recv, argc, argv, has_pending_exception); |
| 1801 | return value; |
| 1802 | } |
| 1803 | |
| 1804 | |
| 1805 | static i::Handle<i::Object> CallV8HeapFunction(const char* name, |
| 1806 | i::Handle<i::Object> data, |
| 1807 | bool* has_pending_exception) { |
| 1808 | i::Object** argv[1] = { data.location() }; |
| 1809 | return CallV8HeapFunction(name, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1810 | i::Isolate::Current()->js_builtins_object(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1811 | 1, |
| 1812 | argv, |
| 1813 | has_pending_exception); |
| 1814 | } |
| 1815 | |
| 1816 | |
| 1817 | int Message::GetLineNumber() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1818 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1819 | ON_BAILOUT(isolate, "v8::Message::GetLineNumber()", return kNoLineNumberInfo); |
| 1820 | ENTER_V8(isolate); |
| 1821 | i::HandleScope scope(isolate); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1822 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1823 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1824 | i::Handle<i::Object> result = CallV8HeapFunction("GetLineNumber", |
| 1825 | Utils::OpenHandle(this), |
| 1826 | &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1827 | EXCEPTION_BAILOUT_CHECK(isolate, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1828 | return static_cast<int>(result->Number()); |
| 1829 | } |
| 1830 | |
| 1831 | |
| 1832 | int Message::GetStartPosition() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1833 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1834 | if (IsDeadCheck(isolate, "v8::Message::GetStartPosition()")) return 0; |
| 1835 | ENTER_V8(isolate); |
| 1836 | i::HandleScope scope(isolate); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1837 | i::Handle<i::JSMessageObject> message = |
| 1838 | i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this)); |
| 1839 | return message->start_position(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | |
| 1843 | int Message::GetEndPosition() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1844 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1845 | if (IsDeadCheck(isolate, "v8::Message::GetEndPosition()")) return 0; |
| 1846 | ENTER_V8(isolate); |
| 1847 | i::HandleScope scope(isolate); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1848 | i::Handle<i::JSMessageObject> message = |
| 1849 | i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this)); |
| 1850 | return message->end_position(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | |
| 1854 | int Message::GetStartColumn() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1855 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1856 | if (IsDeadCheck(isolate, "v8::Message::GetStartColumn()")) { |
| 1857 | return kNoColumnInfo; |
| 1858 | } |
| 1859 | ENTER_V8(isolate); |
| 1860 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1861 | i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1862 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1863 | i::Handle<i::Object> start_col_obj = CallV8HeapFunction( |
| 1864 | "GetPositionInLine", |
| 1865 | data_obj, |
| 1866 | &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1867 | EXCEPTION_BAILOUT_CHECK(isolate, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1868 | return static_cast<int>(start_col_obj->Number()); |
| 1869 | } |
| 1870 | |
| 1871 | |
| 1872 | int Message::GetEndColumn() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1873 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1874 | if (IsDeadCheck(isolate, "v8::Message::GetEndColumn()")) return kNoColumnInfo; |
| 1875 | ENTER_V8(isolate); |
| 1876 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1877 | i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1878 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1879 | i::Handle<i::Object> start_col_obj = CallV8HeapFunction( |
| 1880 | "GetPositionInLine", |
| 1881 | data_obj, |
| 1882 | &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1883 | EXCEPTION_BAILOUT_CHECK(isolate, 0); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1884 | i::Handle<i::JSMessageObject> message = |
| 1885 | i::Handle<i::JSMessageObject>::cast(data_obj); |
| 1886 | int start = message->start_position(); |
| 1887 | int end = message->end_position(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1888 | return static_cast<int>(start_col_obj->Number()) + (end - start); |
| 1889 | } |
| 1890 | |
| 1891 | |
| 1892 | Local<String> Message::GetSourceLine() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1893 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1894 | ON_BAILOUT(isolate, "v8::Message::GetSourceLine()", return Local<String>()); |
| 1895 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1896 | HandleScope scope; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1897 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1898 | i::Handle<i::Object> result = CallV8HeapFunction("GetSourceLine", |
| 1899 | Utils::OpenHandle(this), |
| 1900 | &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1901 | EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::String>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1902 | if (result->IsString()) { |
| 1903 | return scope.Close(Utils::ToLocal(i::Handle<i::String>::cast(result))); |
| 1904 | } else { |
| 1905 | return Local<String>(); |
| 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | |
| 1910 | void Message::PrintCurrentStackTrace(FILE* out) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1911 | i::Isolate* isolate = i::Isolate::Current(); |
| 1912 | if (IsDeadCheck(isolate, "v8::Message::PrintCurrentStackTrace()")) return; |
| 1913 | ENTER_V8(isolate); |
| 1914 | isolate->PrintCurrentStackTrace(out); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1918 | // --- S t a c k T r a c e --- |
| 1919 | |
| 1920 | Local<StackFrame> StackTrace::GetFrame(uint32_t index) const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1921 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1922 | if (IsDeadCheck(isolate, "v8::StackTrace::GetFrame()")) { |
| 1923 | return Local<StackFrame>(); |
| 1924 | } |
| 1925 | ENTER_V8(isolate); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1926 | HandleScope scope; |
| 1927 | i::Handle<i::JSArray> self = Utils::OpenHandle(this); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1928 | i::Object* raw_object = self->GetElementNoExceptionThrown(index); |
| 1929 | i::Handle<i::JSObject> obj(i::JSObject::cast(raw_object)); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1930 | return scope.Close(Utils::StackFrameToLocal(obj)); |
| 1931 | } |
| 1932 | |
| 1933 | |
| 1934 | int StackTrace::GetFrameCount() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1935 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1936 | if (IsDeadCheck(isolate, "v8::StackTrace::GetFrameCount()")) return -1; |
| 1937 | ENTER_V8(isolate); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1938 | return i::Smi::cast(Utils::OpenHandle(this)->length())->value(); |
| 1939 | } |
| 1940 | |
| 1941 | |
| 1942 | Local<Array> StackTrace::AsArray() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1943 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1944 | if (IsDeadCheck(isolate, "v8::StackTrace::AsArray()")) Local<Array>(); |
| 1945 | ENTER_V8(isolate); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1946 | return Utils::ToLocal(Utils::OpenHandle(this)); |
| 1947 | } |
| 1948 | |
| 1949 | |
| 1950 | Local<StackTrace> StackTrace::CurrentStackTrace(int frame_limit, |
| 1951 | StackTraceOptions options) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1952 | i::Isolate* isolate = i::Isolate::Current(); |
| 1953 | if (IsDeadCheck(isolate, "v8::StackTrace::CurrentStackTrace()")) { |
| 1954 | Local<StackTrace>(); |
| 1955 | } |
| 1956 | ENTER_V8(isolate); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1957 | i::Handle<i::JSArray> stackTrace = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1958 | isolate->CaptureCurrentStackTrace(frame_limit, options); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1959 | return Utils::StackTraceToLocal(stackTrace); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | |
| 1963 | // --- S t a c k F r a m e --- |
| 1964 | |
| 1965 | int StackFrame::GetLineNumber() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1966 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1967 | if (IsDeadCheck(isolate, "v8::StackFrame::GetLineNumber()")) { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1968 | return Message::kNoLineNumberInfo; |
| 1969 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1970 | ENTER_V8(isolate); |
| 1971 | i::HandleScope scope(isolate); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1972 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 1973 | i::Handle<i::Object> line = GetProperty(self, "lineNumber"); |
| 1974 | if (!line->IsSmi()) { |
| 1975 | return Message::kNoLineNumberInfo; |
| 1976 | } |
| 1977 | return i::Smi::cast(*line)->value(); |
| 1978 | } |
| 1979 | |
| 1980 | |
| 1981 | int StackFrame::GetColumn() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1982 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1983 | if (IsDeadCheck(isolate, "v8::StackFrame::GetColumn()")) { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1984 | return Message::kNoColumnInfo; |
| 1985 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1986 | ENTER_V8(isolate); |
| 1987 | i::HandleScope scope(isolate); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1988 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 1989 | i::Handle<i::Object> column = GetProperty(self, "column"); |
| 1990 | if (!column->IsSmi()) { |
| 1991 | return Message::kNoColumnInfo; |
| 1992 | } |
| 1993 | return i::Smi::cast(*column)->value(); |
| 1994 | } |
| 1995 | |
| 1996 | |
| 1997 | Local<String> StackFrame::GetScriptName() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1998 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 1999 | if (IsDeadCheck(isolate, "v8::StackFrame::GetScriptName()")) { |
| 2000 | return Local<String>(); |
| 2001 | } |
| 2002 | ENTER_V8(isolate); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2003 | HandleScope scope; |
| 2004 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2005 | i::Handle<i::Object> name = GetProperty(self, "scriptName"); |
| 2006 | if (!name->IsString()) { |
| 2007 | return Local<String>(); |
| 2008 | } |
| 2009 | return scope.Close(Local<String>::Cast(Utils::ToLocal(name))); |
| 2010 | } |
| 2011 | |
| 2012 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 2013 | Local<String> StackFrame::GetScriptNameOrSourceURL() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2014 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2015 | if (IsDeadCheck(isolate, "v8::StackFrame::GetScriptNameOrSourceURL()")) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 2016 | return Local<String>(); |
| 2017 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2018 | ENTER_V8(isolate); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 2019 | HandleScope scope; |
| 2020 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2021 | i::Handle<i::Object> name = GetProperty(self, "scriptNameOrSourceURL"); |
| 2022 | if (!name->IsString()) { |
| 2023 | return Local<String>(); |
| 2024 | } |
| 2025 | return scope.Close(Local<String>::Cast(Utils::ToLocal(name))); |
| 2026 | } |
| 2027 | |
| 2028 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2029 | Local<String> StackFrame::GetFunctionName() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2030 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2031 | if (IsDeadCheck(isolate, "v8::StackFrame::GetFunctionName()")) { |
| 2032 | return Local<String>(); |
| 2033 | } |
| 2034 | ENTER_V8(isolate); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2035 | HandleScope scope; |
| 2036 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2037 | i::Handle<i::Object> name = GetProperty(self, "functionName"); |
| 2038 | if (!name->IsString()) { |
| 2039 | return Local<String>(); |
| 2040 | } |
| 2041 | return scope.Close(Local<String>::Cast(Utils::ToLocal(name))); |
| 2042 | } |
| 2043 | |
| 2044 | |
| 2045 | bool StackFrame::IsEval() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2046 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2047 | if (IsDeadCheck(isolate, "v8::StackFrame::IsEval()")) return false; |
| 2048 | ENTER_V8(isolate); |
| 2049 | i::HandleScope scope(isolate); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2050 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2051 | i::Handle<i::Object> is_eval = GetProperty(self, "isEval"); |
| 2052 | return is_eval->IsTrue(); |
| 2053 | } |
| 2054 | |
| 2055 | |
| 2056 | bool StackFrame::IsConstructor() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2057 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2058 | if (IsDeadCheck(isolate, "v8::StackFrame::IsConstructor()")) return false; |
| 2059 | ENTER_V8(isolate); |
| 2060 | i::HandleScope scope(isolate); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2061 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2062 | i::Handle<i::Object> is_constructor = GetProperty(self, "isConstructor"); |
| 2063 | return is_constructor->IsTrue(); |
| 2064 | } |
| 2065 | |
| 2066 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2067 | // --- D a t a --- |
| 2068 | |
| 2069 | bool Value::IsUndefined() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2070 | if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsUndefined()")) { |
| 2071 | return false; |
| 2072 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2073 | return Utils::OpenHandle(this)->IsUndefined(); |
| 2074 | } |
| 2075 | |
| 2076 | |
| 2077 | bool Value::IsNull() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2078 | if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsNull()")) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2079 | return Utils::OpenHandle(this)->IsNull(); |
| 2080 | } |
| 2081 | |
| 2082 | |
| 2083 | bool Value::IsTrue() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2084 | if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsTrue()")) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2085 | return Utils::OpenHandle(this)->IsTrue(); |
| 2086 | } |
| 2087 | |
| 2088 | |
| 2089 | bool Value::IsFalse() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2090 | if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsFalse()")) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2091 | return Utils::OpenHandle(this)->IsFalse(); |
| 2092 | } |
| 2093 | |
| 2094 | |
| 2095 | bool Value::IsFunction() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2096 | if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsFunction()")) { |
| 2097 | return false; |
| 2098 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2099 | return Utils::OpenHandle(this)->IsJSFunction(); |
| 2100 | } |
| 2101 | |
| 2102 | |
| 2103 | bool Value::FullIsString() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2104 | if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsString()")) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2105 | bool result = Utils::OpenHandle(this)->IsString(); |
| 2106 | ASSERT_EQ(result, QuickIsString()); |
| 2107 | return result; |
| 2108 | } |
| 2109 | |
| 2110 | |
| 2111 | bool Value::IsArray() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2112 | if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsArray()")) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2113 | return Utils::OpenHandle(this)->IsJSArray(); |
| 2114 | } |
| 2115 | |
| 2116 | |
| 2117 | bool Value::IsObject() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2118 | if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsObject()")) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2119 | return Utils::OpenHandle(this)->IsJSObject(); |
| 2120 | } |
| 2121 | |
| 2122 | |
| 2123 | bool Value::IsNumber() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2124 | if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsNumber()")) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2125 | return Utils::OpenHandle(this)->IsNumber(); |
| 2126 | } |
| 2127 | |
| 2128 | |
| 2129 | bool Value::IsBoolean() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2130 | if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsBoolean()")) { |
| 2131 | return false; |
| 2132 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2133 | return Utils::OpenHandle(this)->IsBoolean(); |
| 2134 | } |
| 2135 | |
| 2136 | |
| 2137 | bool Value::IsExternal() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2138 | if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsExternal()")) { |
| 2139 | return false; |
| 2140 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2141 | return Utils::OpenHandle(this)->IsForeign(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | |
| 2145 | bool Value::IsInt32() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2146 | if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsInt32()")) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2147 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2148 | if (obj->IsSmi()) return true; |
| 2149 | if (obj->IsNumber()) { |
| 2150 | double value = obj->Number(); |
| 2151 | return i::FastI2D(i::FastD2I(value)) == value; |
| 2152 | } |
| 2153 | return false; |
| 2154 | } |
| 2155 | |
| 2156 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2157 | bool Value::IsUint32() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2158 | if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsUint32()")) return false; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2159 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2160 | if (obj->IsSmi()) return i::Smi::cast(*obj)->value() >= 0; |
| 2161 | if (obj->IsNumber()) { |
| 2162 | double value = obj->Number(); |
| 2163 | return i::FastUI2D(i::FastD2UI(value)) == value; |
| 2164 | } |
| 2165 | return false; |
| 2166 | } |
| 2167 | |
| 2168 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2169 | bool Value::IsDate() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2170 | i::Isolate* isolate = i::Isolate::Current(); |
| 2171 | if (IsDeadCheck(isolate, "v8::Value::IsDate()")) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2172 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2173 | return obj->HasSpecificClassOf(isolate->heap()->Date_symbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2177 | bool Value::IsStringObject() const { |
| 2178 | i::Isolate* isolate = i::Isolate::Current(); |
| 2179 | if (IsDeadCheck(isolate, "v8::Value::IsStringObject()")) return false; |
| 2180 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2181 | return obj->HasSpecificClassOf(isolate->heap()->String_symbol()); |
| 2182 | } |
| 2183 | |
| 2184 | |
| 2185 | bool Value::IsNumberObject() const { |
| 2186 | i::Isolate* isolate = i::Isolate::Current(); |
| 2187 | if (IsDeadCheck(isolate, "v8::Value::IsNumberObject()")) return false; |
| 2188 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2189 | return obj->HasSpecificClassOf(isolate->heap()->Number_symbol()); |
| 2190 | } |
| 2191 | |
| 2192 | |
| 2193 | static i::Object* LookupBuiltin(i::Isolate* isolate, |
| 2194 | const char* builtin_name) { |
| 2195 | i::Handle<i::String> symbol = |
| 2196 | isolate->factory()->LookupAsciiSymbol(builtin_name); |
| 2197 | i::Handle<i::JSBuiltinsObject> builtins = isolate->js_builtins_object(); |
| 2198 | return builtins->GetPropertyNoExceptionThrown(*symbol); |
| 2199 | } |
| 2200 | |
| 2201 | |
| 2202 | static bool CheckConstructor(i::Isolate* isolate, |
| 2203 | i::Handle<i::JSObject> obj, |
| 2204 | const char* class_name) { |
| 2205 | return obj->map()->constructor() == LookupBuiltin(isolate, class_name); |
| 2206 | } |
| 2207 | |
| 2208 | |
| 2209 | bool Value::IsNativeError() const { |
| 2210 | i::Isolate* isolate = i::Isolate::Current(); |
| 2211 | if (IsDeadCheck(isolate, "v8::Value::IsNativeError()")) return false; |
| 2212 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2213 | if (obj->IsJSObject()) { |
| 2214 | i::Handle<i::JSObject> js_obj(i::JSObject::cast(*obj)); |
| 2215 | return CheckConstructor(isolate, js_obj, "$Error") || |
| 2216 | CheckConstructor(isolate, js_obj, "$EvalError") || |
| 2217 | CheckConstructor(isolate, js_obj, "$RangeError") || |
| 2218 | CheckConstructor(isolate, js_obj, "$ReferenceError") || |
| 2219 | CheckConstructor(isolate, js_obj, "$SyntaxError") || |
| 2220 | CheckConstructor(isolate, js_obj, "$TypeError") || |
| 2221 | CheckConstructor(isolate, js_obj, "$URIError"); |
| 2222 | } else { |
| 2223 | return false; |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | |
| 2228 | bool Value::IsBooleanObject() const { |
| 2229 | i::Isolate* isolate = i::Isolate::Current(); |
| 2230 | if (IsDeadCheck(isolate, "v8::Value::IsBooleanObject()")) return false; |
| 2231 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2232 | return obj->HasSpecificClassOf(isolate->heap()->Boolean_symbol()); |
| 2233 | } |
| 2234 | |
| 2235 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2236 | bool Value::IsRegExp() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2237 | if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsRegExp()")) return false; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2238 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2239 | return obj->IsJSRegExp(); |
| 2240 | } |
| 2241 | |
| 2242 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2243 | Local<String> Value::ToString() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2244 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2245 | i::Handle<i::Object> str; |
| 2246 | if (obj->IsString()) { |
| 2247 | str = obj; |
| 2248 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2249 | i::Isolate* isolate = i::Isolate::Current(); |
| 2250 | if (IsDeadCheck(isolate, "v8::Value::ToString()")) { |
| 2251 | return Local<String>(); |
| 2252 | } |
| 2253 | LOG_API(isolate, "ToString"); |
| 2254 | ENTER_V8(isolate); |
| 2255 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2256 | str = i::Execution::ToString(obj, &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2257 | EXCEPTION_BAILOUT_CHECK(isolate, Local<String>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2258 | } |
| 2259 | return Local<String>(ToApi<String>(str)); |
| 2260 | } |
| 2261 | |
| 2262 | |
| 2263 | Local<String> Value::ToDetailString() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2264 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2265 | i::Handle<i::Object> str; |
| 2266 | if (obj->IsString()) { |
| 2267 | str = obj; |
| 2268 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2269 | i::Isolate* isolate = i::Isolate::Current(); |
| 2270 | if (IsDeadCheck(isolate, "v8::Value::ToDetailString()")) { |
| 2271 | return Local<String>(); |
| 2272 | } |
| 2273 | LOG_API(isolate, "ToDetailString"); |
| 2274 | ENTER_V8(isolate); |
| 2275 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2276 | str = i::Execution::ToDetailString(obj, &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2277 | EXCEPTION_BAILOUT_CHECK(isolate, Local<String>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2278 | } |
| 2279 | return Local<String>(ToApi<String>(str)); |
| 2280 | } |
| 2281 | |
| 2282 | |
| 2283 | Local<v8::Object> Value::ToObject() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2284 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2285 | i::Handle<i::Object> val; |
| 2286 | if (obj->IsJSObject()) { |
| 2287 | val = obj; |
| 2288 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2289 | i::Isolate* isolate = i::Isolate::Current(); |
| 2290 | if (IsDeadCheck(isolate, "v8::Value::ToObject()")) { |
| 2291 | return Local<v8::Object>(); |
| 2292 | } |
| 2293 | LOG_API(isolate, "ToObject"); |
| 2294 | ENTER_V8(isolate); |
| 2295 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2296 | val = i::Execution::ToObject(obj, &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2297 | EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2298 | } |
| 2299 | return Local<v8::Object>(ToApi<Object>(val)); |
| 2300 | } |
| 2301 | |
| 2302 | |
| 2303 | Local<Boolean> Value::ToBoolean() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2304 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2305 | if (obj->IsBoolean()) { |
| 2306 | return Local<Boolean>(ToApi<Boolean>(obj)); |
| 2307 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2308 | i::Isolate* isolate = i::Isolate::Current(); |
| 2309 | if (IsDeadCheck(isolate, "v8::Value::ToBoolean()")) { |
| 2310 | return Local<Boolean>(); |
| 2311 | } |
| 2312 | LOG_API(isolate, "ToBoolean"); |
| 2313 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2314 | i::Handle<i::Object> val = i::Execution::ToBoolean(obj); |
| 2315 | return Local<Boolean>(ToApi<Boolean>(val)); |
| 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | |
| 2320 | Local<Number> Value::ToNumber() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2321 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2322 | i::Handle<i::Object> num; |
| 2323 | if (obj->IsNumber()) { |
| 2324 | num = obj; |
| 2325 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2326 | i::Isolate* isolate = i::Isolate::Current(); |
| 2327 | if (IsDeadCheck(isolate, "v8::Value::ToNumber()")) { |
| 2328 | return Local<Number>(); |
| 2329 | } |
| 2330 | LOG_API(isolate, "ToNumber"); |
| 2331 | ENTER_V8(isolate); |
| 2332 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2333 | num = i::Execution::ToNumber(obj, &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2334 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Number>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2335 | } |
| 2336 | return Local<Number>(ToApi<Number>(num)); |
| 2337 | } |
| 2338 | |
| 2339 | |
| 2340 | Local<Integer> Value::ToInteger() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2341 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2342 | i::Handle<i::Object> num; |
| 2343 | if (obj->IsSmi()) { |
| 2344 | num = obj; |
| 2345 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2346 | i::Isolate* isolate = i::Isolate::Current(); |
| 2347 | if (IsDeadCheck(isolate, "v8::Value::ToInteger()")) return Local<Integer>(); |
| 2348 | LOG_API(isolate, "ToInteger"); |
| 2349 | ENTER_V8(isolate); |
| 2350 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2351 | num = i::Execution::ToInteger(obj, &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2352 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Integer>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2353 | } |
| 2354 | return Local<Integer>(ToApi<Integer>(num)); |
| 2355 | } |
| 2356 | |
| 2357 | |
| 2358 | void External::CheckCast(v8::Value* that) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2359 | if (IsDeadCheck(i::Isolate::Current(), "v8::External::Cast()")) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2360 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2361 | ApiCheck(obj->IsForeign(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2362 | "v8::External::Cast()", |
| 2363 | "Could not convert to external"); |
| 2364 | } |
| 2365 | |
| 2366 | |
| 2367 | void v8::Object::CheckCast(Value* that) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2368 | if (IsDeadCheck(i::Isolate::Current(), "v8::Object::Cast()")) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2369 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2370 | ApiCheck(obj->IsJSObject(), |
| 2371 | "v8::Object::Cast()", |
| 2372 | "Could not convert to object"); |
| 2373 | } |
| 2374 | |
| 2375 | |
| 2376 | void v8::Function::CheckCast(Value* that) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2377 | if (IsDeadCheck(i::Isolate::Current(), "v8::Function::Cast()")) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2378 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2379 | ApiCheck(obj->IsJSFunction(), |
| 2380 | "v8::Function::Cast()", |
| 2381 | "Could not convert to function"); |
| 2382 | } |
| 2383 | |
| 2384 | |
| 2385 | void v8::String::CheckCast(v8::Value* that) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2386 | if (IsDeadCheck(i::Isolate::Current(), "v8::String::Cast()")) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2387 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2388 | ApiCheck(obj->IsString(), |
| 2389 | "v8::String::Cast()", |
| 2390 | "Could not convert to string"); |
| 2391 | } |
| 2392 | |
| 2393 | |
| 2394 | void v8::Number::CheckCast(v8::Value* that) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2395 | if (IsDeadCheck(i::Isolate::Current(), "v8::Number::Cast()")) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2396 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2397 | ApiCheck(obj->IsNumber(), |
| 2398 | "v8::Number::Cast()", |
| 2399 | "Could not convert to number"); |
| 2400 | } |
| 2401 | |
| 2402 | |
| 2403 | void v8::Integer::CheckCast(v8::Value* that) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2404 | if (IsDeadCheck(i::Isolate::Current(), "v8::Integer::Cast()")) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2405 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2406 | ApiCheck(obj->IsNumber(), |
| 2407 | "v8::Integer::Cast()", |
| 2408 | "Could not convert to number"); |
| 2409 | } |
| 2410 | |
| 2411 | |
| 2412 | void v8::Array::CheckCast(Value* that) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2413 | if (IsDeadCheck(i::Isolate::Current(), "v8::Array::Cast()")) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2414 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2415 | ApiCheck(obj->IsJSArray(), |
| 2416 | "v8::Array::Cast()", |
| 2417 | "Could not convert to array"); |
| 2418 | } |
| 2419 | |
| 2420 | |
| 2421 | void v8::Date::CheckCast(v8::Value* that) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2422 | i::Isolate* isolate = i::Isolate::Current(); |
| 2423 | if (IsDeadCheck(isolate, "v8::Date::Cast()")) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2424 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2425 | ApiCheck(obj->HasSpecificClassOf(isolate->heap()->Date_symbol()), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2426 | "v8::Date::Cast()", |
| 2427 | "Could not convert to date"); |
| 2428 | } |
| 2429 | |
| 2430 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2431 | void v8::StringObject::CheckCast(v8::Value* that) { |
| 2432 | i::Isolate* isolate = i::Isolate::Current(); |
| 2433 | if (IsDeadCheck(isolate, "v8::StringObject::Cast()")) return; |
| 2434 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2435 | ApiCheck(obj->HasSpecificClassOf(isolate->heap()->String_symbol()), |
| 2436 | "v8::StringObject::Cast()", |
| 2437 | "Could not convert to StringObject"); |
| 2438 | } |
| 2439 | |
| 2440 | |
| 2441 | void v8::NumberObject::CheckCast(v8::Value* that) { |
| 2442 | i::Isolate* isolate = i::Isolate::Current(); |
| 2443 | if (IsDeadCheck(isolate, "v8::NumberObject::Cast()")) return; |
| 2444 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2445 | ApiCheck(obj->HasSpecificClassOf(isolate->heap()->Number_symbol()), |
| 2446 | "v8::NumberObject::Cast()", |
| 2447 | "Could not convert to NumberObject"); |
| 2448 | } |
| 2449 | |
| 2450 | |
| 2451 | void v8::BooleanObject::CheckCast(v8::Value* that) { |
| 2452 | i::Isolate* isolate = i::Isolate::Current(); |
| 2453 | if (IsDeadCheck(isolate, "v8::BooleanObject::Cast()")) return; |
| 2454 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2455 | ApiCheck(obj->HasSpecificClassOf(isolate->heap()->Boolean_symbol()), |
| 2456 | "v8::BooleanObject::Cast()", |
| 2457 | "Could not convert to BooleanObject"); |
| 2458 | } |
| 2459 | |
| 2460 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 2461 | void v8::RegExp::CheckCast(v8::Value* that) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2462 | if (IsDeadCheck(i::Isolate::Current(), "v8::RegExp::Cast()")) return; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 2463 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2464 | ApiCheck(obj->IsJSRegExp(), |
| 2465 | "v8::RegExp::Cast()", |
| 2466 | "Could not convert to regular expression"); |
| 2467 | } |
| 2468 | |
| 2469 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2470 | bool Value::BooleanValue() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2471 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2472 | if (obj->IsBoolean()) { |
| 2473 | return obj->IsTrue(); |
| 2474 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2475 | i::Isolate* isolate = i::Isolate::Current(); |
| 2476 | if (IsDeadCheck(isolate, "v8::Value::BooleanValue()")) return false; |
| 2477 | LOG_API(isolate, "BooleanValue"); |
| 2478 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2479 | i::Handle<i::Object> value = i::Execution::ToBoolean(obj); |
| 2480 | return value->IsTrue(); |
| 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | |
| 2485 | double Value::NumberValue() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2486 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2487 | i::Handle<i::Object> num; |
| 2488 | if (obj->IsNumber()) { |
| 2489 | num = obj; |
| 2490 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2491 | i::Isolate* isolate = i::Isolate::Current(); |
| 2492 | if (IsDeadCheck(isolate, "v8::Value::NumberValue()")) { |
| 2493 | return i::OS::nan_value(); |
| 2494 | } |
| 2495 | LOG_API(isolate, "NumberValue"); |
| 2496 | ENTER_V8(isolate); |
| 2497 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2498 | num = i::Execution::ToNumber(obj, &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2499 | EXCEPTION_BAILOUT_CHECK(isolate, i::OS::nan_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2500 | } |
| 2501 | return num->Number(); |
| 2502 | } |
| 2503 | |
| 2504 | |
| 2505 | int64_t Value::IntegerValue() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2506 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2507 | i::Handle<i::Object> num; |
| 2508 | if (obj->IsNumber()) { |
| 2509 | num = obj; |
| 2510 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2511 | i::Isolate* isolate = i::Isolate::Current(); |
| 2512 | if (IsDeadCheck(isolate, "v8::Value::IntegerValue()")) return 0; |
| 2513 | LOG_API(isolate, "IntegerValue"); |
| 2514 | ENTER_V8(isolate); |
| 2515 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2516 | num = i::Execution::ToInteger(obj, &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2517 | EXCEPTION_BAILOUT_CHECK(isolate, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2518 | } |
| 2519 | if (num->IsSmi()) { |
| 2520 | return i::Smi::cast(*num)->value(); |
| 2521 | } else { |
| 2522 | return static_cast<int64_t>(num->Number()); |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | |
| 2527 | Local<Int32> Value::ToInt32() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2528 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2529 | i::Handle<i::Object> num; |
| 2530 | if (obj->IsSmi()) { |
| 2531 | num = obj; |
| 2532 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2533 | i::Isolate* isolate = i::Isolate::Current(); |
| 2534 | if (IsDeadCheck(isolate, "v8::Value::ToInt32()")) return Local<Int32>(); |
| 2535 | LOG_API(isolate, "ToInt32"); |
| 2536 | ENTER_V8(isolate); |
| 2537 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2538 | num = i::Execution::ToInt32(obj, &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2539 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Int32>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2540 | } |
| 2541 | return Local<Int32>(ToApi<Int32>(num)); |
| 2542 | } |
| 2543 | |
| 2544 | |
| 2545 | Local<Uint32> Value::ToUint32() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2546 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2547 | i::Handle<i::Object> num; |
| 2548 | if (obj->IsSmi()) { |
| 2549 | num = obj; |
| 2550 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2551 | i::Isolate* isolate = i::Isolate::Current(); |
| 2552 | if (IsDeadCheck(isolate, "v8::Value::ToUint32()")) return Local<Uint32>(); |
| 2553 | LOG_API(isolate, "ToUInt32"); |
| 2554 | ENTER_V8(isolate); |
| 2555 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2556 | num = i::Execution::ToUint32(obj, &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2557 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Uint32>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2558 | } |
| 2559 | return Local<Uint32>(ToApi<Uint32>(num)); |
| 2560 | } |
| 2561 | |
| 2562 | |
| 2563 | Local<Uint32> Value::ToArrayIndex() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2564 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2565 | if (obj->IsSmi()) { |
| 2566 | if (i::Smi::cast(*obj)->value() >= 0) return Utils::Uint32ToLocal(obj); |
| 2567 | return Local<Uint32>(); |
| 2568 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2569 | i::Isolate* isolate = i::Isolate::Current(); |
| 2570 | if (IsDeadCheck(isolate, "v8::Value::ToArrayIndex()")) return Local<Uint32>(); |
| 2571 | LOG_API(isolate, "ToArrayIndex"); |
| 2572 | ENTER_V8(isolate); |
| 2573 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2574 | i::Handle<i::Object> string_obj = |
| 2575 | i::Execution::ToString(obj, &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2576 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Uint32>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2577 | i::Handle<i::String> str = i::Handle<i::String>::cast(string_obj); |
| 2578 | uint32_t index; |
| 2579 | if (str->AsArrayIndex(&index)) { |
| 2580 | i::Handle<i::Object> value; |
| 2581 | if (index <= static_cast<uint32_t>(i::Smi::kMaxValue)) { |
| 2582 | value = i::Handle<i::Object>(i::Smi::FromInt(index)); |
| 2583 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2584 | value = isolate->factory()->NewNumber(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2585 | } |
| 2586 | return Utils::Uint32ToLocal(value); |
| 2587 | } |
| 2588 | return Local<Uint32>(); |
| 2589 | } |
| 2590 | |
| 2591 | |
| 2592 | int32_t Value::Int32Value() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2593 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2594 | if (obj->IsSmi()) { |
| 2595 | return i::Smi::cast(*obj)->value(); |
| 2596 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2597 | i::Isolate* isolate = i::Isolate::Current(); |
| 2598 | if (IsDeadCheck(isolate, "v8::Value::Int32Value()")) return 0; |
| 2599 | LOG_API(isolate, "Int32Value (slow)"); |
| 2600 | ENTER_V8(isolate); |
| 2601 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2602 | i::Handle<i::Object> num = |
| 2603 | i::Execution::ToInt32(obj, &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2604 | EXCEPTION_BAILOUT_CHECK(isolate, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2605 | if (num->IsSmi()) { |
| 2606 | return i::Smi::cast(*num)->value(); |
| 2607 | } else { |
| 2608 | return static_cast<int32_t>(num->Number()); |
| 2609 | } |
| 2610 | } |
| 2611 | } |
| 2612 | |
| 2613 | |
| 2614 | bool Value::Equals(Handle<Value> that) const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2615 | i::Isolate* isolate = i::Isolate::Current(); |
| 2616 | if (IsDeadCheck(isolate, "v8::Value::Equals()") |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2617 | || EmptyCheck("v8::Value::Equals()", this) |
| 2618 | || EmptyCheck("v8::Value::Equals()", that)) { |
| 2619 | return false; |
| 2620 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2621 | LOG_API(isolate, "Equals"); |
| 2622 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2623 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2624 | i::Handle<i::Object> other = Utils::OpenHandle(*that); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2625 | // If both obj and other are JSObjects, we'd better compare by identity |
| 2626 | // immediately when going into JS builtin. The reason is Invoke |
| 2627 | // would overwrite global object receiver with global proxy. |
| 2628 | if (obj->IsJSObject() && other->IsJSObject()) { |
| 2629 | return *obj == *other; |
| 2630 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2631 | i::Object** args[1] = { other.location() }; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2632 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2633 | i::Handle<i::Object> result = |
| 2634 | CallV8HeapFunction("EQUALS", obj, 1, args, &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2635 | EXCEPTION_BAILOUT_CHECK(isolate, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2636 | return *result == i::Smi::FromInt(i::EQUAL); |
| 2637 | } |
| 2638 | |
| 2639 | |
| 2640 | bool Value::StrictEquals(Handle<Value> that) const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2641 | i::Isolate* isolate = i::Isolate::Current(); |
| 2642 | if (IsDeadCheck(isolate, "v8::Value::StrictEquals()") |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2643 | || EmptyCheck("v8::Value::StrictEquals()", this) |
| 2644 | || EmptyCheck("v8::Value::StrictEquals()", that)) { |
| 2645 | return false; |
| 2646 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2647 | LOG_API(isolate, "StrictEquals"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2648 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2649 | i::Handle<i::Object> other = Utils::OpenHandle(*that); |
| 2650 | // Must check HeapNumber first, since NaN !== NaN. |
| 2651 | if (obj->IsHeapNumber()) { |
| 2652 | if (!other->IsNumber()) return false; |
| 2653 | double x = obj->Number(); |
| 2654 | double y = other->Number(); |
| 2655 | // Must check explicitly for NaN:s on Windows, but -0 works fine. |
| 2656 | return x == y && !isnan(x) && !isnan(y); |
| 2657 | } else if (*obj == *other) { // Also covers Booleans. |
| 2658 | return true; |
| 2659 | } else if (obj->IsSmi()) { |
| 2660 | return other->IsNumber() && obj->Number() == other->Number(); |
| 2661 | } else if (obj->IsString()) { |
| 2662 | return other->IsString() && |
| 2663 | i::String::cast(*obj)->Equals(i::String::cast(*other)); |
| 2664 | } else if (obj->IsUndefined() || obj->IsUndetectableObject()) { |
| 2665 | return other->IsUndefined() || other->IsUndetectableObject(); |
| 2666 | } else { |
| 2667 | return false; |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | |
| 2672 | uint32_t Value::Uint32Value() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2673 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2674 | if (obj->IsSmi()) { |
| 2675 | return i::Smi::cast(*obj)->value(); |
| 2676 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2677 | i::Isolate* isolate = i::Isolate::Current(); |
| 2678 | if (IsDeadCheck(isolate, "v8::Value::Uint32Value()")) return 0; |
| 2679 | LOG_API(isolate, "Uint32Value"); |
| 2680 | ENTER_V8(isolate); |
| 2681 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2682 | i::Handle<i::Object> num = |
| 2683 | i::Execution::ToUint32(obj, &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2684 | EXCEPTION_BAILOUT_CHECK(isolate, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2685 | if (num->IsSmi()) { |
| 2686 | return i::Smi::cast(*num)->value(); |
| 2687 | } else { |
| 2688 | return static_cast<uint32_t>(num->Number()); |
| 2689 | } |
| 2690 | } |
| 2691 | } |
| 2692 | |
| 2693 | |
| 2694 | bool v8::Object::Set(v8::Handle<Value> key, v8::Handle<Value> value, |
| 2695 | v8::PropertyAttribute attribs) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2696 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2697 | ON_BAILOUT(isolate, "v8::Object::Set()", return false); |
| 2698 | ENTER_V8(isolate); |
| 2699 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2700 | i::Handle<i::Object> self = Utils::OpenHandle(this); |
| 2701 | i::Handle<i::Object> key_obj = Utils::OpenHandle(*key); |
| 2702 | i::Handle<i::Object> value_obj = Utils::OpenHandle(*value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2703 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2704 | i::Handle<i::Object> obj = i::SetProperty( |
| 2705 | self, |
| 2706 | key_obj, |
| 2707 | value_obj, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2708 | static_cast<PropertyAttributes>(attribs), |
| 2709 | i::kNonStrictMode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2710 | has_pending_exception = obj.is_null(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2711 | EXCEPTION_BAILOUT_CHECK(isolate, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2712 | return true; |
| 2713 | } |
| 2714 | |
| 2715 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2716 | bool v8::Object::Set(uint32_t index, v8::Handle<Value> value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2717 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2718 | ON_BAILOUT(isolate, "v8::Object::Set()", return false); |
| 2719 | ENTER_V8(isolate); |
| 2720 | i::HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2721 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2722 | i::Handle<i::Object> value_obj = Utils::OpenHandle(*value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2723 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2724 | i::Handle<i::Object> obj = i::SetElement( |
| 2725 | self, |
| 2726 | index, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2727 | value_obj, |
| 2728 | i::kNonStrictMode); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2729 | has_pending_exception = obj.is_null(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2730 | EXCEPTION_BAILOUT_CHECK(isolate, false); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2731 | return true; |
| 2732 | } |
| 2733 | |
| 2734 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2735 | bool v8::Object::ForceSet(v8::Handle<Value> key, |
| 2736 | v8::Handle<Value> value, |
| 2737 | v8::PropertyAttribute attribs) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2738 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2739 | ON_BAILOUT(isolate, "v8::Object::ForceSet()", return false); |
| 2740 | ENTER_V8(isolate); |
| 2741 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2742 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2743 | i::Handle<i::Object> key_obj = Utils::OpenHandle(*key); |
| 2744 | i::Handle<i::Object> value_obj = Utils::OpenHandle(*value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2745 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2746 | i::Handle<i::Object> obj = i::ForceSetProperty( |
| 2747 | self, |
| 2748 | key_obj, |
| 2749 | value_obj, |
| 2750 | static_cast<PropertyAttributes>(attribs)); |
| 2751 | has_pending_exception = obj.is_null(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2752 | EXCEPTION_BAILOUT_CHECK(isolate, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2753 | return true; |
| 2754 | } |
| 2755 | |
| 2756 | |
| 2757 | bool v8::Object::ForceDelete(v8::Handle<Value> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2758 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2759 | ON_BAILOUT(isolate, "v8::Object::ForceDelete()", return false); |
| 2760 | ENTER_V8(isolate); |
| 2761 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2762 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2763 | i::Handle<i::Object> key_obj = Utils::OpenHandle(*key); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2764 | |
| 2765 | // When turning on access checks for a global object deoptimize all functions |
| 2766 | // as optimized code does not always handle access checks. |
| 2767 | i::Deoptimizer::DeoptimizeGlobalObject(*self); |
| 2768 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2769 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2770 | i::Handle<i::Object> obj = i::ForceDeleteProperty(self, key_obj); |
| 2771 | has_pending_exception = obj.is_null(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2772 | EXCEPTION_BAILOUT_CHECK(isolate, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2773 | return obj->IsTrue(); |
| 2774 | } |
| 2775 | |
| 2776 | |
| 2777 | Local<Value> v8::Object::Get(v8::Handle<Value> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2778 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2779 | ON_BAILOUT(isolate, "v8::Object::Get()", return Local<v8::Value>()); |
| 2780 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2781 | i::Handle<i::Object> self = Utils::OpenHandle(this); |
| 2782 | i::Handle<i::Object> key_obj = Utils::OpenHandle(*key); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2783 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2784 | i::Handle<i::Object> result = i::GetProperty(self, key_obj); |
| 2785 | has_pending_exception = result.is_null(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2786 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2787 | return Utils::ToLocal(result); |
| 2788 | } |
| 2789 | |
| 2790 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2791 | Local<Value> v8::Object::Get(uint32_t index) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2792 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2793 | ON_BAILOUT(isolate, "v8::Object::Get()", return Local<v8::Value>()); |
| 2794 | ENTER_V8(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2795 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2796 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2797 | i::Handle<i::Object> result = i::GetElement(self, index); |
| 2798 | has_pending_exception = result.is_null(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2799 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2800 | return Utils::ToLocal(result); |
| 2801 | } |
| 2802 | |
| 2803 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2804 | PropertyAttribute v8::Object::GetPropertyAttributes(v8::Handle<Value> key) { |
| 2805 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2806 | ON_BAILOUT(isolate, "v8::Object::GetPropertyAttribute()", |
| 2807 | return static_cast<PropertyAttribute>(NONE)); |
| 2808 | ENTER_V8(isolate); |
| 2809 | i::HandleScope scope(isolate); |
| 2810 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2811 | i::Handle<i::Object> key_obj = Utils::OpenHandle(*key); |
| 2812 | if (!key_obj->IsString()) { |
| 2813 | EXCEPTION_PREAMBLE(isolate); |
| 2814 | key_obj = i::Execution::ToString(key_obj, &has_pending_exception); |
| 2815 | EXCEPTION_BAILOUT_CHECK(isolate, static_cast<PropertyAttribute>(NONE)); |
| 2816 | } |
| 2817 | i::Handle<i::String> key_string = i::Handle<i::String>::cast(key_obj); |
| 2818 | PropertyAttributes result = self->GetPropertyAttribute(*key_string); |
| 2819 | if (result == ABSENT) return static_cast<PropertyAttribute>(NONE); |
| 2820 | return static_cast<PropertyAttribute>(result); |
| 2821 | } |
| 2822 | |
| 2823 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2824 | Local<Value> v8::Object::GetPrototype() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2825 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2826 | ON_BAILOUT(isolate, "v8::Object::GetPrototype()", |
| 2827 | return Local<v8::Value>()); |
| 2828 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2829 | i::Handle<i::Object> self = Utils::OpenHandle(this); |
| 2830 | i::Handle<i::Object> result = i::GetPrototype(self); |
| 2831 | return Utils::ToLocal(result); |
| 2832 | } |
| 2833 | |
| 2834 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2835 | bool v8::Object::SetPrototype(Handle<Value> value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2836 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2837 | ON_BAILOUT(isolate, "v8::Object::SetPrototype()", return false); |
| 2838 | ENTER_V8(isolate); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2839 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2840 | i::Handle<i::Object> value_obj = Utils::OpenHandle(*value); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2841 | // We do not allow exceptions thrown while setting the prototype |
| 2842 | // to propagate outside. |
| 2843 | TryCatch try_catch; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2844 | EXCEPTION_PREAMBLE(isolate); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2845 | i::Handle<i::Object> result = i::SetPrototype(self, value_obj); |
| 2846 | has_pending_exception = result.is_null(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2847 | EXCEPTION_BAILOUT_CHECK(isolate, false); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2848 | return true; |
| 2849 | } |
| 2850 | |
| 2851 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2852 | Local<Object> v8::Object::FindInstanceInPrototypeChain( |
| 2853 | v8::Handle<FunctionTemplate> tmpl) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2854 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2855 | ON_BAILOUT(isolate, |
| 2856 | "v8::Object::FindInstanceInPrototypeChain()", |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2857 | return Local<v8::Object>()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2858 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2859 | i::JSObject* object = *Utils::OpenHandle(this); |
| 2860 | i::FunctionTemplateInfo* tmpl_info = *Utils::OpenHandle(*tmpl); |
| 2861 | while (!object->IsInstanceOf(tmpl_info)) { |
| 2862 | i::Object* prototype = object->GetPrototype(); |
| 2863 | if (!prototype->IsJSObject()) return Local<Object>(); |
| 2864 | object = i::JSObject::cast(prototype); |
| 2865 | } |
| 2866 | return Utils::ToLocal(i::Handle<i::JSObject>(object)); |
| 2867 | } |
| 2868 | |
| 2869 | |
| 2870 | Local<Array> v8::Object::GetPropertyNames() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2871 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2872 | ON_BAILOUT(isolate, "v8::Object::GetPropertyNames()", |
| 2873 | return Local<v8::Array>()); |
| 2874 | ENTER_V8(isolate); |
| 2875 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2876 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2877 | i::Handle<i::FixedArray> value = |
| 2878 | i::GetKeysInFixedArrayFor(self, i::INCLUDE_PROTOS); |
| 2879 | // Because we use caching to speed up enumeration it is important |
| 2880 | // to never change the result of the basic enumeration function so |
| 2881 | // we clone the result. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2882 | i::Handle<i::FixedArray> elms = isolate->factory()->CopyFixedArray(value); |
| 2883 | i::Handle<i::JSArray> result = |
| 2884 | isolate->factory()->NewJSArrayWithElements(elms); |
| 2885 | return Utils::ToLocal(scope.CloseAndEscape(result)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
| 2888 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2889 | Local<Array> v8::Object::GetOwnPropertyNames() { |
| 2890 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2891 | ON_BAILOUT(isolate, "v8::Object::GetOwnPropertyNames()", |
| 2892 | return Local<v8::Array>()); |
| 2893 | ENTER_V8(isolate); |
| 2894 | i::HandleScope scope(isolate); |
| 2895 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2896 | i::Handle<i::FixedArray> value = |
| 2897 | i::GetKeysInFixedArrayFor(self, i::LOCAL_ONLY); |
| 2898 | // Because we use caching to speed up enumeration it is important |
| 2899 | // to never change the result of the basic enumeration function so |
| 2900 | // we clone the result. |
| 2901 | i::Handle<i::FixedArray> elms = isolate->factory()->CopyFixedArray(value); |
| 2902 | i::Handle<i::JSArray> result = |
| 2903 | isolate->factory()->NewJSArrayWithElements(elms); |
| 2904 | return Utils::ToLocal(scope.CloseAndEscape(result)); |
| 2905 | } |
| 2906 | |
| 2907 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2908 | Local<String> v8::Object::ObjectProtoToString() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2909 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2910 | ON_BAILOUT(isolate, "v8::Object::ObjectProtoToString()", |
| 2911 | return Local<v8::String>()); |
| 2912 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2913 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2914 | |
| 2915 | i::Handle<i::Object> name(self->class_name()); |
| 2916 | |
| 2917 | // Native implementation of Object.prototype.toString (v8natives.js): |
| 2918 | // var c = %ClassOf(this); |
| 2919 | // if (c === 'Arguments') c = 'Object'; |
| 2920 | // return "[object " + c + "]"; |
| 2921 | |
| 2922 | if (!name->IsString()) { |
| 2923 | return v8::String::New("[object ]"); |
| 2924 | |
| 2925 | } else { |
| 2926 | i::Handle<i::String> class_name = i::Handle<i::String>::cast(name); |
| 2927 | if (class_name->IsEqualTo(i::CStrVector("Arguments"))) { |
| 2928 | return v8::String::New("[object Object]"); |
| 2929 | |
| 2930 | } else { |
| 2931 | const char* prefix = "[object "; |
| 2932 | Local<String> str = Utils::ToLocal(class_name); |
| 2933 | const char* postfix = "]"; |
| 2934 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2935 | int prefix_len = i::StrLength(prefix); |
| 2936 | int str_len = str->Length(); |
| 2937 | int postfix_len = i::StrLength(postfix); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2938 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2939 | int buf_len = prefix_len + str_len + postfix_len; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2940 | i::ScopedVector<char> buf(buf_len); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2941 | |
| 2942 | // Write prefix. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2943 | char* ptr = buf.start(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2944 | memcpy(ptr, prefix, prefix_len * v8::internal::kCharSize); |
| 2945 | ptr += prefix_len; |
| 2946 | |
| 2947 | // Write real content. |
| 2948 | str->WriteAscii(ptr, 0, str_len); |
| 2949 | ptr += str_len; |
| 2950 | |
| 2951 | // Write postfix. |
| 2952 | memcpy(ptr, postfix, postfix_len * v8::internal::kCharSize); |
| 2953 | |
| 2954 | // Copy the buffer into a heap-allocated string and return it. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2955 | Local<String> result = v8::String::New(buf.start(), buf_len); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2956 | return result; |
| 2957 | } |
| 2958 | } |
| 2959 | } |
| 2960 | |
| 2961 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 2962 | Local<String> v8::Object::GetConstructorName() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2963 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2964 | ON_BAILOUT(isolate, "v8::Object::GetConstructorName()", |
| 2965 | return Local<v8::String>()); |
| 2966 | ENTER_V8(isolate); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 2967 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2968 | i::Handle<i::String> name(self->constructor_name()); |
| 2969 | return Utils::ToLocal(name); |
| 2970 | } |
| 2971 | |
| 2972 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2973 | bool v8::Object::Delete(v8::Handle<String> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2974 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2975 | ON_BAILOUT(isolate, "v8::Object::Delete()", return false); |
| 2976 | ENTER_V8(isolate); |
| 2977 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2978 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2979 | i::Handle<i::String> key_obj = Utils::OpenHandle(*key); |
| 2980 | return i::DeleteProperty(self, key_obj)->IsTrue(); |
| 2981 | } |
| 2982 | |
| 2983 | |
| 2984 | bool v8::Object::Has(v8::Handle<String> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2985 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2986 | ON_BAILOUT(isolate, "v8::Object::Has()", return false); |
| 2987 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2988 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2989 | i::Handle<i::String> key_obj = Utils::OpenHandle(*key); |
| 2990 | return self->HasProperty(*key_obj); |
| 2991 | } |
| 2992 | |
| 2993 | |
| 2994 | bool v8::Object::Delete(uint32_t index) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2995 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 2996 | ON_BAILOUT(isolate, "v8::Object::DeleteProperty()", |
| 2997 | return false); |
| 2998 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2999 | HandleScope scope; |
| 3000 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3001 | return i::DeleteElement(self, index)->IsTrue(); |
| 3002 | } |
| 3003 | |
| 3004 | |
| 3005 | bool v8::Object::Has(uint32_t index) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3006 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3007 | ON_BAILOUT(isolate, "v8::Object::HasProperty()", return false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3008 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3009 | return self->HasElement(index); |
| 3010 | } |
| 3011 | |
| 3012 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3013 | bool Object::SetAccessor(Handle<String> name, |
| 3014 | AccessorGetter getter, |
| 3015 | AccessorSetter setter, |
| 3016 | v8::Handle<Value> data, |
| 3017 | AccessControl settings, |
| 3018 | PropertyAttribute attributes) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3019 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3020 | ON_BAILOUT(isolate, "v8::Object::SetAccessor()", return false); |
| 3021 | ENTER_V8(isolate); |
| 3022 | i::HandleScope scope(isolate); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3023 | i::Handle<i::AccessorInfo> info = MakeAccessorInfo(name, |
| 3024 | getter, setter, data, |
| 3025 | settings, attributes); |
| 3026 | i::Handle<i::Object> result = i::SetAccessor(Utils::OpenHandle(this), info); |
| 3027 | return !result.is_null() && !result->IsUndefined(); |
| 3028 | } |
| 3029 | |
| 3030 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3031 | bool v8::Object::HasOwnProperty(Handle<String> key) { |
| 3032 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3033 | ON_BAILOUT(isolate, "v8::Object::HasOwnProperty()", |
| 3034 | return false); |
| 3035 | return Utils::OpenHandle(this)->HasLocalProperty( |
| 3036 | *Utils::OpenHandle(*key)); |
| 3037 | } |
| 3038 | |
| 3039 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3040 | bool v8::Object::HasRealNamedProperty(Handle<String> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3041 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3042 | ON_BAILOUT(isolate, "v8::Object::HasRealNamedProperty()", |
| 3043 | return false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3044 | return Utils::OpenHandle(this)->HasRealNamedProperty( |
| 3045 | *Utils::OpenHandle(*key)); |
| 3046 | } |
| 3047 | |
| 3048 | |
| 3049 | bool v8::Object::HasRealIndexedProperty(uint32_t index) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3050 | ON_BAILOUT(Utils::OpenHandle(this)->GetIsolate(), |
| 3051 | "v8::Object::HasRealIndexedProperty()", |
| 3052 | return false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3053 | return Utils::OpenHandle(this)->HasRealElementProperty(index); |
| 3054 | } |
| 3055 | |
| 3056 | |
| 3057 | bool v8::Object::HasRealNamedCallbackProperty(Handle<String> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3058 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3059 | ON_BAILOUT(isolate, |
| 3060 | "v8::Object::HasRealNamedCallbackProperty()", |
| 3061 | return false); |
| 3062 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3063 | return Utils::OpenHandle(this)->HasRealNamedCallbackProperty( |
| 3064 | *Utils::OpenHandle(*key)); |
| 3065 | } |
| 3066 | |
| 3067 | |
| 3068 | bool v8::Object::HasNamedLookupInterceptor() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3069 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3070 | ON_BAILOUT(isolate, "v8::Object::HasNamedLookupInterceptor()", |
| 3071 | return false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3072 | return Utils::OpenHandle(this)->HasNamedInterceptor(); |
| 3073 | } |
| 3074 | |
| 3075 | |
| 3076 | bool v8::Object::HasIndexedLookupInterceptor() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3077 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3078 | ON_BAILOUT(isolate, "v8::Object::HasIndexedLookupInterceptor()", |
| 3079 | return false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3080 | return Utils::OpenHandle(this)->HasIndexedInterceptor(); |
| 3081 | } |
| 3082 | |
| 3083 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3084 | static Local<Value> GetPropertyByLookup(i::Isolate* isolate, |
| 3085 | i::Handle<i::JSObject> receiver, |
| 3086 | i::Handle<i::String> name, |
| 3087 | i::LookupResult* lookup) { |
| 3088 | if (!lookup->IsProperty()) { |
| 3089 | // No real property was found. |
| 3090 | return Local<Value>(); |
| 3091 | } |
| 3092 | |
| 3093 | // If the property being looked up is a callback, it can throw |
| 3094 | // an exception. |
| 3095 | EXCEPTION_PREAMBLE(isolate); |
| 3096 | i::Handle<i::Object> result = i::GetProperty(receiver, name, lookup); |
| 3097 | has_pending_exception = result.is_null(); |
| 3098 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>()); |
| 3099 | |
| 3100 | return Utils::ToLocal(result); |
| 3101 | } |
| 3102 | |
| 3103 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3104 | Local<Value> v8::Object::GetRealNamedPropertyInPrototypeChain( |
| 3105 | Handle<String> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3106 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3107 | ON_BAILOUT(isolate, |
| 3108 | "v8::Object::GetRealNamedPropertyInPrototypeChain()", |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3109 | return Local<Value>()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3110 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3111 | i::Handle<i::JSObject> self_obj = Utils::OpenHandle(this); |
| 3112 | i::Handle<i::String> key_obj = Utils::OpenHandle(*key); |
| 3113 | i::LookupResult lookup; |
| 3114 | self_obj->LookupRealNamedPropertyInPrototypes(*key_obj, &lookup); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3115 | return GetPropertyByLookup(isolate, self_obj, key_obj, &lookup); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | |
| 3119 | Local<Value> v8::Object::GetRealNamedProperty(Handle<String> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3120 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3121 | ON_BAILOUT(isolate, "v8::Object::GetRealNamedProperty()", |
| 3122 | return Local<Value>()); |
| 3123 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3124 | i::Handle<i::JSObject> self_obj = Utils::OpenHandle(this); |
| 3125 | i::Handle<i::String> key_obj = Utils::OpenHandle(*key); |
| 3126 | i::LookupResult lookup; |
| 3127 | self_obj->LookupRealNamedProperty(*key_obj, &lookup); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3128 | return GetPropertyByLookup(isolate, self_obj, key_obj, &lookup); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
| 3131 | |
| 3132 | // Turns on access checks by copying the map and setting the check flag. |
| 3133 | // Because the object gets a new map, existing inline cache caching |
| 3134 | // the old map of this object will fail. |
| 3135 | void v8::Object::TurnOnAccessCheck() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3136 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3137 | ON_BAILOUT(isolate, "v8::Object::TurnOnAccessCheck()", return); |
| 3138 | ENTER_V8(isolate); |
| 3139 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3140 | i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
| 3141 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3142 | // When turning on access checks for a global object deoptimize all functions |
| 3143 | // as optimized code does not always handle access checks. |
| 3144 | i::Deoptimizer::DeoptimizeGlobalObject(*obj); |
| 3145 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3146 | i::Handle<i::Map> new_map = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3147 | isolate->factory()->CopyMapDropTransitions(i::Handle<i::Map>(obj->map())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3148 | new_map->set_is_access_check_needed(true); |
| 3149 | obj->set_map(*new_map); |
| 3150 | } |
| 3151 | |
| 3152 | |
| 3153 | bool v8::Object::IsDirty() { |
| 3154 | return Utils::OpenHandle(this)->IsDirty(); |
| 3155 | } |
| 3156 | |
| 3157 | |
| 3158 | Local<v8::Object> v8::Object::Clone() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3159 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3160 | ON_BAILOUT(isolate, "v8::Object::Clone()", return Local<Object>()); |
| 3161 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3162 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3163 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3164 | i::Handle<i::JSObject> result = i::Copy(self); |
| 3165 | has_pending_exception = result.is_null(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3166 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3167 | return Utils::ToLocal(result); |
| 3168 | } |
| 3169 | |
| 3170 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3171 | static i::Context* GetCreationContext(i::JSObject* object) { |
| 3172 | i::Object* constructor = object->map()->constructor(); |
| 3173 | i::JSFunction* function; |
| 3174 | if (!constructor->IsJSFunction()) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3175 | // Functions have null as a constructor, |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3176 | // but any JSFunction knows its context immediately. |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3177 | ASSERT(object->IsJSFunction()); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3178 | function = i::JSFunction::cast(object); |
| 3179 | } else { |
| 3180 | function = i::JSFunction::cast(constructor); |
| 3181 | } |
| 3182 | return function->context()->global_context(); |
| 3183 | } |
| 3184 | |
| 3185 | |
| 3186 | Local<v8::Context> v8::Object::CreationContext() { |
| 3187 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3188 | ON_BAILOUT(isolate, |
| 3189 | "v8::Object::CreationContext()", return Local<v8::Context>()); |
| 3190 | ENTER_V8(isolate); |
| 3191 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3192 | i::Context* context = GetCreationContext(*self); |
| 3193 | return Utils::ToLocal(i::Handle<i::Context>(context)); |
| 3194 | } |
| 3195 | |
| 3196 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3197 | int v8::Object::GetIdentityHash() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3198 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3199 | ON_BAILOUT(isolate, "v8::Object::GetIdentityHash()", return 0); |
| 3200 | ENTER_V8(isolate); |
| 3201 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3202 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3203 | return i::GetIdentityHash(self); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | |
| 3207 | bool v8::Object::SetHiddenValue(v8::Handle<v8::String> key, |
| 3208 | v8::Handle<v8::Value> value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3209 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3210 | ON_BAILOUT(isolate, "v8::Object::SetHiddenValue()", return false); |
| 3211 | ENTER_V8(isolate); |
| 3212 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3213 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3214 | i::Handle<i::Object> hidden_props(i::GetHiddenProperties( |
| 3215 | self, |
| 3216 | i::JSObject::ALLOW_CREATION)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3217 | i::Handle<i::Object> key_obj = Utils::OpenHandle(*key); |
| 3218 | i::Handle<i::Object> value_obj = Utils::OpenHandle(*value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3219 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3220 | i::Handle<i::Object> obj = i::SetProperty( |
| 3221 | hidden_props, |
| 3222 | key_obj, |
| 3223 | value_obj, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 3224 | static_cast<PropertyAttributes>(None), |
| 3225 | i::kNonStrictMode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3226 | has_pending_exception = obj.is_null(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3227 | EXCEPTION_BAILOUT_CHECK(isolate, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3228 | return true; |
| 3229 | } |
| 3230 | |
| 3231 | |
| 3232 | v8::Local<v8::Value> v8::Object::GetHiddenValue(v8::Handle<v8::String> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3233 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3234 | ON_BAILOUT(isolate, "v8::Object::GetHiddenValue()", |
| 3235 | return Local<v8::Value>()); |
| 3236 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3237 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3238 | i::Handle<i::Object> hidden_props(i::GetHiddenProperties( |
| 3239 | self, |
| 3240 | i::JSObject::OMIT_CREATION)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3241 | if (hidden_props->IsUndefined()) { |
| 3242 | return v8::Local<v8::Value>(); |
| 3243 | } |
| 3244 | i::Handle<i::String> key_obj = Utils::OpenHandle(*key); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3245 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3246 | i::Handle<i::Object> result = i::GetProperty(hidden_props, key_obj); |
| 3247 | has_pending_exception = result.is_null(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3248 | EXCEPTION_BAILOUT_CHECK(isolate, v8::Local<v8::Value>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3249 | if (result->IsUndefined()) { |
| 3250 | return v8::Local<v8::Value>(); |
| 3251 | } |
| 3252 | return Utils::ToLocal(result); |
| 3253 | } |
| 3254 | |
| 3255 | |
| 3256 | bool v8::Object::DeleteHiddenValue(v8::Handle<v8::String> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3257 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3258 | ON_BAILOUT(isolate, "v8::DeleteHiddenValue()", return false); |
| 3259 | ENTER_V8(isolate); |
| 3260 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3261 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3262 | i::Handle<i::Object> hidden_props(i::GetHiddenProperties( |
| 3263 | self, |
| 3264 | i::JSObject::OMIT_CREATION)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3265 | if (hidden_props->IsUndefined()) { |
| 3266 | return true; |
| 3267 | } |
| 3268 | i::Handle<i::JSObject> js_obj(i::JSObject::cast(*hidden_props)); |
| 3269 | i::Handle<i::String> key_obj = Utils::OpenHandle(*key); |
| 3270 | return i::DeleteProperty(js_obj, key_obj)->IsTrue(); |
| 3271 | } |
| 3272 | |
| 3273 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3274 | namespace { |
| 3275 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 3276 | static i::ElementsKind GetElementsKindFromExternalArrayType( |
| 3277 | ExternalArrayType array_type) { |
| 3278 | switch (array_type) { |
| 3279 | case kExternalByteArray: |
| 3280 | return i::EXTERNAL_BYTE_ELEMENTS; |
| 3281 | break; |
| 3282 | case kExternalUnsignedByteArray: |
| 3283 | return i::EXTERNAL_UNSIGNED_BYTE_ELEMENTS; |
| 3284 | break; |
| 3285 | case kExternalShortArray: |
| 3286 | return i::EXTERNAL_SHORT_ELEMENTS; |
| 3287 | break; |
| 3288 | case kExternalUnsignedShortArray: |
| 3289 | return i::EXTERNAL_UNSIGNED_SHORT_ELEMENTS; |
| 3290 | break; |
| 3291 | case kExternalIntArray: |
| 3292 | return i::EXTERNAL_INT_ELEMENTS; |
| 3293 | break; |
| 3294 | case kExternalUnsignedIntArray: |
| 3295 | return i::EXTERNAL_UNSIGNED_INT_ELEMENTS; |
| 3296 | break; |
| 3297 | case kExternalFloatArray: |
| 3298 | return i::EXTERNAL_FLOAT_ELEMENTS; |
| 3299 | break; |
| 3300 | case kExternalDoubleArray: |
| 3301 | return i::EXTERNAL_DOUBLE_ELEMENTS; |
| 3302 | break; |
| 3303 | case kExternalPixelArray: |
| 3304 | return i::EXTERNAL_PIXEL_ELEMENTS; |
| 3305 | break; |
| 3306 | } |
| 3307 | UNREACHABLE(); |
| 3308 | return i::DICTIONARY_ELEMENTS; |
| 3309 | } |
| 3310 | |
| 3311 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3312 | void PrepareExternalArrayElements(i::Handle<i::JSObject> object, |
| 3313 | void* data, |
| 3314 | ExternalArrayType array_type, |
| 3315 | int length) { |
| 3316 | i::Isolate* isolate = object->GetIsolate(); |
| 3317 | i::Handle<i::ExternalArray> array = |
| 3318 | isolate->factory()->NewExternalArray(length, array_type, data); |
| 3319 | |
| 3320 | // If the object already has external elements, create a new, unique |
| 3321 | // map if the element type is now changing, because assumptions about |
| 3322 | // generated code based on the receiver's map will be invalid. |
| 3323 | i::Handle<i::HeapObject> elements(object->elements()); |
| 3324 | bool cant_reuse_map = |
| 3325 | elements->map()->IsUndefined() || |
| 3326 | !elements->map()->has_external_array_elements() || |
| 3327 | elements->map() != isolate->heap()->MapForExternalArrayType(array_type); |
| 3328 | if (cant_reuse_map) { |
| 3329 | i::Handle<i::Map> external_array_map = |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 3330 | isolate->factory()->GetElementsTransitionMap( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3331 | i::Handle<i::Map>(object->map()), |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 3332 | GetElementsKindFromExternalArrayType(array_type), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3333 | object->HasFastProperties()); |
| 3334 | object->set_map(*external_array_map); |
| 3335 | } |
| 3336 | object->set_elements(*array); |
| 3337 | } |
| 3338 | |
| 3339 | } // namespace |
| 3340 | |
| 3341 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3342 | void v8::Object::SetIndexedPropertiesToPixelData(uint8_t* data, int length) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3343 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3344 | ON_BAILOUT(isolate, "v8::SetElementsToPixelData()", return); |
| 3345 | ENTER_V8(isolate); |
| 3346 | i::HandleScope scope(isolate); |
| 3347 | if (!ApiCheck(length <= i::ExternalPixelArray::kMaxLength, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3348 | "v8::Object::SetIndexedPropertiesToPixelData()", |
| 3349 | "length exceeds max acceptable value")) { |
| 3350 | return; |
| 3351 | } |
| 3352 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3353 | if (!ApiCheck(!self->IsJSArray(), |
| 3354 | "v8::Object::SetIndexedPropertiesToPixelData()", |
| 3355 | "JSArray is not supported")) { |
| 3356 | return; |
| 3357 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3358 | PrepareExternalArrayElements(self, data, kExternalPixelArray, length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
| 3361 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3362 | bool v8::Object::HasIndexedPropertiesInPixelData() { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3363 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3364 | ON_BAILOUT(self->GetIsolate(), "v8::HasIndexedPropertiesInPixelData()", |
| 3365 | return false); |
| 3366 | return self->HasExternalPixelElements(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | |
| 3370 | uint8_t* v8::Object::GetIndexedPropertiesPixelData() { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3371 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3372 | ON_BAILOUT(self->GetIsolate(), "v8::GetIndexedPropertiesPixelData()", |
| 3373 | return NULL); |
| 3374 | if (self->HasExternalPixelElements()) { |
| 3375 | return i::ExternalPixelArray::cast(self->elements())-> |
| 3376 | external_pixel_pointer(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3377 | } else { |
| 3378 | return NULL; |
| 3379 | } |
| 3380 | } |
| 3381 | |
| 3382 | |
| 3383 | int v8::Object::GetIndexedPropertiesPixelDataLength() { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3384 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3385 | ON_BAILOUT(self->GetIsolate(), "v8::GetIndexedPropertiesPixelDataLength()", |
| 3386 | return -1); |
| 3387 | if (self->HasExternalPixelElements()) { |
| 3388 | return i::ExternalPixelArray::cast(self->elements())->length(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3389 | } else { |
| 3390 | return -1; |
| 3391 | } |
| 3392 | } |
| 3393 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 3394 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3395 | void v8::Object::SetIndexedPropertiesToExternalArrayData( |
| 3396 | void* data, |
| 3397 | ExternalArrayType array_type, |
| 3398 | int length) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3399 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3400 | ON_BAILOUT(isolate, "v8::SetIndexedPropertiesToExternalArrayData()", return); |
| 3401 | ENTER_V8(isolate); |
| 3402 | i::HandleScope scope(isolate); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3403 | if (!ApiCheck(length <= i::ExternalArray::kMaxLength, |
| 3404 | "v8::Object::SetIndexedPropertiesToExternalArrayData()", |
| 3405 | "length exceeds max acceptable value")) { |
| 3406 | return; |
| 3407 | } |
| 3408 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3409 | if (!ApiCheck(!self->IsJSArray(), |
| 3410 | "v8::Object::SetIndexedPropertiesToExternalArrayData()", |
| 3411 | "JSArray is not supported")) { |
| 3412 | return; |
| 3413 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3414 | PrepareExternalArrayElements(self, data, array_type, length); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3418 | bool v8::Object::HasIndexedPropertiesInExternalArrayData() { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3419 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3420 | ON_BAILOUT(self->GetIsolate(), |
| 3421 | "v8::HasIndexedPropertiesInExternalArrayData()", |
| 3422 | return false); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3423 | return self->HasExternalArrayElements(); |
| 3424 | } |
| 3425 | |
| 3426 | |
| 3427 | void* v8::Object::GetIndexedPropertiesExternalArrayData() { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3428 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3429 | ON_BAILOUT(self->GetIsolate(), |
| 3430 | "v8::GetIndexedPropertiesExternalArrayData()", |
| 3431 | return NULL); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3432 | if (self->HasExternalArrayElements()) { |
| 3433 | return i::ExternalArray::cast(self->elements())->external_pointer(); |
| 3434 | } else { |
| 3435 | return NULL; |
| 3436 | } |
| 3437 | } |
| 3438 | |
| 3439 | |
| 3440 | ExternalArrayType v8::Object::GetIndexedPropertiesExternalArrayDataType() { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3441 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3442 | ON_BAILOUT(self->GetIsolate(), |
| 3443 | "v8::GetIndexedPropertiesExternalArrayDataType()", |
| 3444 | return static_cast<ExternalArrayType>(-1)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3445 | switch (self->elements()->map()->instance_type()) { |
| 3446 | case i::EXTERNAL_BYTE_ARRAY_TYPE: |
| 3447 | return kExternalByteArray; |
| 3448 | case i::EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 3449 | return kExternalUnsignedByteArray; |
| 3450 | case i::EXTERNAL_SHORT_ARRAY_TYPE: |
| 3451 | return kExternalShortArray; |
| 3452 | case i::EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 3453 | return kExternalUnsignedShortArray; |
| 3454 | case i::EXTERNAL_INT_ARRAY_TYPE: |
| 3455 | return kExternalIntArray; |
| 3456 | case i::EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 3457 | return kExternalUnsignedIntArray; |
| 3458 | case i::EXTERNAL_FLOAT_ARRAY_TYPE: |
| 3459 | return kExternalFloatArray; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3460 | case i::EXTERNAL_DOUBLE_ARRAY_TYPE: |
| 3461 | return kExternalDoubleArray; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3462 | case i::EXTERNAL_PIXEL_ARRAY_TYPE: |
| 3463 | return kExternalPixelArray; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3464 | default: |
| 3465 | return static_cast<ExternalArrayType>(-1); |
| 3466 | } |
| 3467 | } |
| 3468 | |
| 3469 | |
| 3470 | int v8::Object::GetIndexedPropertiesExternalArrayDataLength() { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3471 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3472 | ON_BAILOUT(self->GetIsolate(), |
| 3473 | "v8::GetIndexedPropertiesExternalArrayDataLength()", |
| 3474 | return 0); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3475 | if (self->HasExternalArrayElements()) { |
| 3476 | return i::ExternalArray::cast(self->elements())->length(); |
| 3477 | } else { |
| 3478 | return -1; |
| 3479 | } |
| 3480 | } |
| 3481 | |
| 3482 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3483 | bool v8::Object::IsCallable() { |
| 3484 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3485 | ON_BAILOUT(isolate, "v8::Object::IsCallable()", return false); |
| 3486 | ENTER_V8(isolate); |
| 3487 | i::HandleScope scope(isolate); |
| 3488 | i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
| 3489 | if (obj->IsJSFunction()) return true; |
| 3490 | return i::Execution::GetFunctionDelegate(obj)->IsJSFunction(); |
| 3491 | } |
| 3492 | |
| 3493 | |
| 3494 | Local<v8::Value> Object::CallAsFunction(v8::Handle<v8::Object> recv, int argc, |
| 3495 | v8::Handle<v8::Value> argv[]) { |
| 3496 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3497 | ON_BAILOUT(isolate, "v8::Object::CallAsFunction()", |
| 3498 | return Local<v8::Value>()); |
| 3499 | LOG_API(isolate, "Object::CallAsFunction"); |
| 3500 | ENTER_V8(isolate); |
| 3501 | i::HandleScope scope(isolate); |
| 3502 | i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
| 3503 | i::Handle<i::Object> recv_obj = Utils::OpenHandle(*recv); |
| 3504 | STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**)); |
| 3505 | i::Object*** args = reinterpret_cast<i::Object***>(argv); |
| 3506 | i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>(); |
| 3507 | if (obj->IsJSFunction()) { |
| 3508 | fun = i::Handle<i::JSFunction>::cast(obj); |
| 3509 | } else { |
| 3510 | EXCEPTION_PREAMBLE(isolate); |
| 3511 | i::Handle<i::Object> delegate = |
| 3512 | i::Execution::TryGetFunctionDelegate(obj, &has_pending_exception); |
| 3513 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>()); |
| 3514 | fun = i::Handle<i::JSFunction>::cast(delegate); |
| 3515 | recv_obj = obj; |
| 3516 | } |
| 3517 | EXCEPTION_PREAMBLE(isolate); |
| 3518 | i::Handle<i::Object> returned = |
| 3519 | i::Execution::Call(fun, recv_obj, argc, args, &has_pending_exception); |
| 3520 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>()); |
| 3521 | return Utils::ToLocal(scope.CloseAndEscape(returned)); |
| 3522 | } |
| 3523 | |
| 3524 | |
| 3525 | Local<v8::Value> Object::CallAsConstructor(int argc, |
| 3526 | v8::Handle<v8::Value> argv[]) { |
| 3527 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3528 | ON_BAILOUT(isolate, "v8::Object::CallAsConstructor()", |
| 3529 | return Local<v8::Object>()); |
| 3530 | LOG_API(isolate, "Object::CallAsConstructor"); |
| 3531 | ENTER_V8(isolate); |
| 3532 | i::HandleScope scope(isolate); |
| 3533 | i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
| 3534 | STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**)); |
| 3535 | i::Object*** args = reinterpret_cast<i::Object***>(argv); |
| 3536 | if (obj->IsJSFunction()) { |
| 3537 | i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(obj); |
| 3538 | EXCEPTION_PREAMBLE(isolate); |
| 3539 | i::Handle<i::Object> returned = |
| 3540 | i::Execution::New(fun, argc, args, &has_pending_exception); |
| 3541 | EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>()); |
| 3542 | return Utils::ToLocal(scope.CloseAndEscape( |
| 3543 | i::Handle<i::JSObject>::cast(returned))); |
| 3544 | } |
| 3545 | EXCEPTION_PREAMBLE(isolate); |
| 3546 | i::Handle<i::Object> delegate = |
| 3547 | i::Execution::TryGetConstructorDelegate(obj, &has_pending_exception); |
| 3548 | EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>()); |
| 3549 | if (!delegate->IsUndefined()) { |
| 3550 | i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(delegate); |
| 3551 | EXCEPTION_PREAMBLE(isolate); |
| 3552 | i::Handle<i::Object> returned = |
| 3553 | i::Execution::Call(fun, obj, argc, args, &has_pending_exception); |
| 3554 | EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>()); |
| 3555 | ASSERT(!delegate->IsUndefined()); |
| 3556 | return Utils::ToLocal(scope.CloseAndEscape(returned)); |
| 3557 | } |
| 3558 | return Local<v8::Object>(); |
| 3559 | } |
| 3560 | |
| 3561 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3562 | Local<v8::Object> Function::NewInstance() const { |
| 3563 | return NewInstance(0, NULL); |
| 3564 | } |
| 3565 | |
| 3566 | |
| 3567 | Local<v8::Object> Function::NewInstance(int argc, |
| 3568 | v8::Handle<v8::Value> argv[]) const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3569 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3570 | ON_BAILOUT(isolate, "v8::Function::NewInstance()", |
| 3571 | return Local<v8::Object>()); |
| 3572 | LOG_API(isolate, "Function::NewInstance"); |
| 3573 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3574 | HandleScope scope; |
| 3575 | i::Handle<i::JSFunction> function = Utils::OpenHandle(this); |
| 3576 | STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**)); |
| 3577 | i::Object*** args = reinterpret_cast<i::Object***>(argv); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3578 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3579 | i::Handle<i::Object> returned = |
| 3580 | i::Execution::New(function, argc, args, &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3581 | EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3582 | return scope.Close(Utils::ToLocal(i::Handle<i::JSObject>::cast(returned))); |
| 3583 | } |
| 3584 | |
| 3585 | |
| 3586 | Local<v8::Value> Function::Call(v8::Handle<v8::Object> recv, int argc, |
| 3587 | v8::Handle<v8::Value> argv[]) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3588 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3589 | ON_BAILOUT(isolate, "v8::Function::Call()", return Local<v8::Value>()); |
| 3590 | LOG_API(isolate, "Function::Call"); |
| 3591 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3592 | i::Object* raw_result = NULL; |
| 3593 | { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3594 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3595 | i::Handle<i::JSFunction> fun = Utils::OpenHandle(this); |
| 3596 | i::Handle<i::Object> recv_obj = Utils::OpenHandle(*recv); |
| 3597 | STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**)); |
| 3598 | i::Object*** args = reinterpret_cast<i::Object***>(argv); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3599 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3600 | i::Handle<i::Object> returned = |
| 3601 | i::Execution::Call(fun, recv_obj, argc, args, &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3602 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3603 | raw_result = *returned; |
| 3604 | } |
| 3605 | i::Handle<i::Object> result(raw_result); |
| 3606 | return Utils::ToLocal(result); |
| 3607 | } |
| 3608 | |
| 3609 | |
| 3610 | void Function::SetName(v8::Handle<v8::String> name) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3611 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3612 | ENTER_V8(isolate); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3613 | USE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3614 | i::Handle<i::JSFunction> func = Utils::OpenHandle(this); |
| 3615 | func->shared()->set_name(*Utils::OpenHandle(*name)); |
| 3616 | } |
| 3617 | |
| 3618 | |
| 3619 | Handle<Value> Function::GetName() const { |
| 3620 | i::Handle<i::JSFunction> func = Utils::OpenHandle(this); |
| 3621 | return Utils::ToLocal(i::Handle<i::Object>(func->shared()->name())); |
| 3622 | } |
| 3623 | |
| 3624 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3625 | ScriptOrigin Function::GetScriptOrigin() const { |
| 3626 | i::Handle<i::JSFunction> func = Utils::OpenHandle(this); |
| 3627 | if (func->shared()->script()->IsScript()) { |
| 3628 | i::Handle<i::Script> script(i::Script::cast(func->shared()->script())); |
| 3629 | v8::ScriptOrigin origin( |
| 3630 | Utils::ToLocal(i::Handle<i::Object>(script->name())), |
| 3631 | v8::Integer::New(script->line_offset()->value()), |
| 3632 | v8::Integer::New(script->column_offset()->value())); |
| 3633 | return origin; |
| 3634 | } |
| 3635 | return v8::ScriptOrigin(Handle<Value>()); |
| 3636 | } |
| 3637 | |
| 3638 | |
| 3639 | const int Function::kLineOffsetNotFound = -1; |
| 3640 | |
| 3641 | |
| 3642 | int Function::GetScriptLineNumber() const { |
| 3643 | i::Handle<i::JSFunction> func = Utils::OpenHandle(this); |
| 3644 | if (func->shared()->script()->IsScript()) { |
| 3645 | i::Handle<i::Script> script(i::Script::cast(func->shared()->script())); |
| 3646 | return i::GetScriptLineNumber(script, func->shared()->start_position()); |
| 3647 | } |
| 3648 | return kLineOffsetNotFound; |
| 3649 | } |
| 3650 | |
| 3651 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3652 | int String::Length() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3653 | i::Handle<i::String> str = Utils::OpenHandle(this); |
| 3654 | if (IsDeadCheck(str->GetIsolate(), "v8::String::Length()")) return 0; |
| 3655 | return str->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3656 | } |
| 3657 | |
| 3658 | |
| 3659 | int String::Utf8Length() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3660 | i::Handle<i::String> str = Utils::OpenHandle(this); |
| 3661 | if (IsDeadCheck(str->GetIsolate(), "v8::String::Utf8Length()")) return 0; |
| 3662 | return str->Utf8Length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3663 | } |
| 3664 | |
| 3665 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3666 | int String::WriteUtf8(char* buffer, |
| 3667 | int capacity, |
| 3668 | int* nchars_ref, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3669 | int options) const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3670 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3671 | if (IsDeadCheck(isolate, "v8::String::WriteUtf8()")) return 0; |
| 3672 | LOG_API(isolate, "String::WriteUtf8"); |
| 3673 | ENTER_V8(isolate); |
| 3674 | i::StringInputBuffer& write_input_buffer = *isolate->write_input_buffer(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3675 | i::Handle<i::String> str = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3676 | isolate->string_tracker()->RecordWrite(str); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3677 | if (options & HINT_MANY_WRITES_EXPECTED) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3678 | // Flatten the string for efficiency. This applies whether we are |
| 3679 | // using StringInputBuffer or Get(i) to access the characters. |
| 3680 | str->TryFlatten(); |
| 3681 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3682 | write_input_buffer.Reset(0, *str); |
| 3683 | int len = str->length(); |
| 3684 | // Encode the first K - 3 bytes directly into the buffer since we |
| 3685 | // know there's room for them. If no capacity is given we copy all |
| 3686 | // of them here. |
| 3687 | int fast_end = capacity - (unibrow::Utf8::kMaxEncodedSize - 1); |
| 3688 | int i; |
| 3689 | int pos = 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3690 | int nchars = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3691 | for (i = 0; i < len && (capacity == -1 || pos < fast_end); i++) { |
| 3692 | i::uc32 c = write_input_buffer.GetNext(); |
| 3693 | int written = unibrow::Utf8::Encode(buffer + pos, c); |
| 3694 | pos += written; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3695 | nchars++; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3696 | } |
| 3697 | if (i < len) { |
| 3698 | // For the last characters we need to check the length for each one |
| 3699 | // because they may be longer than the remaining space in the |
| 3700 | // buffer. |
| 3701 | char intermediate[unibrow::Utf8::kMaxEncodedSize]; |
| 3702 | for (; i < len && pos < capacity; i++) { |
| 3703 | i::uc32 c = write_input_buffer.GetNext(); |
| 3704 | int written = unibrow::Utf8::Encode(intermediate, c); |
| 3705 | if (pos + written <= capacity) { |
| 3706 | for (int j = 0; j < written; j++) |
| 3707 | buffer[pos + j] = intermediate[j]; |
| 3708 | pos += written; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3709 | nchars++; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3710 | } else { |
| 3711 | // We've reached the end of the buffer |
| 3712 | break; |
| 3713 | } |
| 3714 | } |
| 3715 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3716 | if (nchars_ref != NULL) *nchars_ref = nchars; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3717 | if (!(options & NO_NULL_TERMINATION) && |
| 3718 | (i == len && (capacity == -1 || pos < capacity))) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3719 | buffer[pos++] = '\0'; |
| 3720 | return pos; |
| 3721 | } |
| 3722 | |
| 3723 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3724 | int String::WriteAscii(char* buffer, |
| 3725 | int start, |
| 3726 | int length, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3727 | int options) const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3728 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3729 | if (IsDeadCheck(isolate, "v8::String::WriteAscii()")) return 0; |
| 3730 | LOG_API(isolate, "String::WriteAscii"); |
| 3731 | ENTER_V8(isolate); |
| 3732 | i::StringInputBuffer& write_input_buffer = *isolate->write_input_buffer(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3733 | ASSERT(start >= 0 && length >= -1); |
| 3734 | i::Handle<i::String> str = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3735 | isolate->string_tracker()->RecordWrite(str); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3736 | if (options & HINT_MANY_WRITES_EXPECTED) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3737 | // Flatten the string for efficiency. This applies whether we are |
| 3738 | // using StringInputBuffer or Get(i) to access the characters. |
| 3739 | str->TryFlatten(); |
| 3740 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3741 | int end = length; |
| 3742 | if ( (length == -1) || (length > str->length() - start) ) |
| 3743 | end = str->length() - start; |
| 3744 | if (end < 0) return 0; |
| 3745 | write_input_buffer.Reset(start, *str); |
| 3746 | int i; |
| 3747 | for (i = 0; i < end; i++) { |
| 3748 | char c = static_cast<char>(write_input_buffer.GetNext()); |
| 3749 | if (c == '\0') c = ' '; |
| 3750 | buffer[i] = c; |
| 3751 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3752 | if (!(options & NO_NULL_TERMINATION) && (length == -1 || i < length)) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3753 | buffer[i] = '\0'; |
| 3754 | return i; |
| 3755 | } |
| 3756 | |
| 3757 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3758 | int String::Write(uint16_t* buffer, |
| 3759 | int start, |
| 3760 | int length, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3761 | int options) const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3762 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3763 | if (IsDeadCheck(isolate, "v8::String::Write()")) return 0; |
| 3764 | LOG_API(isolate, "String::Write"); |
| 3765 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3766 | ASSERT(start >= 0 && length >= -1); |
| 3767 | i::Handle<i::String> str = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3768 | isolate->string_tracker()->RecordWrite(str); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3769 | if (options & HINT_MANY_WRITES_EXPECTED) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3770 | // Flatten the string for efficiency. This applies whether we are |
| 3771 | // using StringInputBuffer or Get(i) to access the characters. |
| 3772 | str->TryFlatten(); |
| 3773 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3774 | int end = start + length; |
| 3775 | if ((length == -1) || (length > str->length() - start) ) |
| 3776 | end = str->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3777 | if (end < 0) return 0; |
| 3778 | i::String::WriteToFlat(*str, buffer, start, end); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3779 | if (!(options & NO_NULL_TERMINATION) && |
| 3780 | (length == -1 || end - start < length)) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3781 | buffer[end - start] = '\0'; |
| 3782 | } |
| 3783 | return end - start; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
| 3786 | |
| 3787 | bool v8::String::IsExternal() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3788 | i::Handle<i::String> str = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3789 | if (IsDeadCheck(str->GetIsolate(), "v8::String::IsExternal()")) { |
| 3790 | return false; |
| 3791 | } |
| 3792 | EnsureInitializedForIsolate(str->GetIsolate(), "v8::String::IsExternal()"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3793 | return i::StringShape(*str).IsExternalTwoByte(); |
| 3794 | } |
| 3795 | |
| 3796 | |
| 3797 | bool v8::String::IsExternalAscii() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3798 | i::Handle<i::String> str = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3799 | if (IsDeadCheck(str->GetIsolate(), "v8::String::IsExternalAscii()")) { |
| 3800 | return false; |
| 3801 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3802 | return i::StringShape(*str).IsExternalAscii(); |
| 3803 | } |
| 3804 | |
| 3805 | |
| 3806 | void v8::String::VerifyExternalStringResource( |
| 3807 | v8::String::ExternalStringResource* value) const { |
| 3808 | i::Handle<i::String> str = Utils::OpenHandle(this); |
| 3809 | v8::String::ExternalStringResource* expected; |
| 3810 | if (i::StringShape(*str).IsExternalTwoByte()) { |
| 3811 | void* resource = i::Handle<i::ExternalTwoByteString>::cast(str)->resource(); |
| 3812 | expected = reinterpret_cast<ExternalStringResource*>(resource); |
| 3813 | } else { |
| 3814 | expected = NULL; |
| 3815 | } |
| 3816 | CHECK_EQ(expected, value); |
| 3817 | } |
| 3818 | |
| 3819 | |
| 3820 | v8::String::ExternalAsciiStringResource* |
| 3821 | v8::String::GetExternalAsciiStringResource() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3822 | i::Handle<i::String> str = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3823 | if (IsDeadCheck(str->GetIsolate(), |
| 3824 | "v8::String::GetExternalAsciiStringResource()")) { |
| 3825 | return NULL; |
| 3826 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3827 | if (i::StringShape(*str).IsExternalAscii()) { |
| 3828 | void* resource = i::Handle<i::ExternalAsciiString>::cast(str)->resource(); |
| 3829 | return reinterpret_cast<ExternalAsciiStringResource*>(resource); |
| 3830 | } else { |
| 3831 | return NULL; |
| 3832 | } |
| 3833 | } |
| 3834 | |
| 3835 | |
| 3836 | double Number::Value() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3837 | if (IsDeadCheck(i::Isolate::Current(), "v8::Number::Value()")) return 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3838 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 3839 | return obj->Number(); |
| 3840 | } |
| 3841 | |
| 3842 | |
| 3843 | bool Boolean::Value() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3844 | if (IsDeadCheck(i::Isolate::Current(), "v8::Boolean::Value()")) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3845 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 3846 | return obj->IsTrue(); |
| 3847 | } |
| 3848 | |
| 3849 | |
| 3850 | int64_t Integer::Value() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3851 | if (IsDeadCheck(i::Isolate::Current(), "v8::Integer::Value()")) return 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3852 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 3853 | if (obj->IsSmi()) { |
| 3854 | return i::Smi::cast(*obj)->value(); |
| 3855 | } else { |
| 3856 | return static_cast<int64_t>(obj->Number()); |
| 3857 | } |
| 3858 | } |
| 3859 | |
| 3860 | |
| 3861 | int32_t Int32::Value() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3862 | if (IsDeadCheck(i::Isolate::Current(), "v8::Int32::Value()")) return 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3863 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 3864 | if (obj->IsSmi()) { |
| 3865 | return i::Smi::cast(*obj)->value(); |
| 3866 | } else { |
| 3867 | return static_cast<int32_t>(obj->Number()); |
| 3868 | } |
| 3869 | } |
| 3870 | |
| 3871 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3872 | uint32_t Uint32::Value() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3873 | if (IsDeadCheck(i::Isolate::Current(), "v8::Uint32::Value()")) return 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3874 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 3875 | if (obj->IsSmi()) { |
| 3876 | return i::Smi::cast(*obj)->value(); |
| 3877 | } else { |
| 3878 | return static_cast<uint32_t>(obj->Number()); |
| 3879 | } |
| 3880 | } |
| 3881 | |
| 3882 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3883 | int v8::Object::InternalFieldCount() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3884 | i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3885 | if (IsDeadCheck(obj->GetIsolate(), "v8::Object::InternalFieldCount()")) { |
| 3886 | return 0; |
| 3887 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3888 | return obj->GetInternalFieldCount(); |
| 3889 | } |
| 3890 | |
| 3891 | |
| 3892 | Local<Value> v8::Object::CheckedGetInternalField(int index) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3893 | i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3894 | if (IsDeadCheck(obj->GetIsolate(), "v8::Object::GetInternalField()")) { |
| 3895 | return Local<Value>(); |
| 3896 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3897 | if (!ApiCheck(index < obj->GetInternalFieldCount(), |
| 3898 | "v8::Object::GetInternalField()", |
| 3899 | "Reading internal field out of bounds")) { |
| 3900 | return Local<Value>(); |
| 3901 | } |
| 3902 | i::Handle<i::Object> value(obj->GetInternalField(index)); |
| 3903 | Local<Value> result = Utils::ToLocal(value); |
| 3904 | #ifdef DEBUG |
| 3905 | Local<Value> unchecked = UncheckedGetInternalField(index); |
| 3906 | ASSERT(unchecked.IsEmpty() || (unchecked == result)); |
| 3907 | #endif |
| 3908 | return result; |
| 3909 | } |
| 3910 | |
| 3911 | |
| 3912 | void v8::Object::SetInternalField(int index, v8::Handle<Value> value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3913 | i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3914 | i::Isolate* isolate = obj->GetIsolate(); |
| 3915 | if (IsDeadCheck(isolate, "v8::Object::SetInternalField()")) { |
| 3916 | return; |
| 3917 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3918 | if (!ApiCheck(index < obj->GetInternalFieldCount(), |
| 3919 | "v8::Object::SetInternalField()", |
| 3920 | "Writing internal field out of bounds")) { |
| 3921 | return; |
| 3922 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3923 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3924 | i::Handle<i::Object> val = Utils::OpenHandle(*value); |
| 3925 | obj->SetInternalField(index, *val); |
| 3926 | } |
| 3927 | |
| 3928 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3929 | static bool CanBeEncodedAsSmi(void* ptr) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3930 | const uintptr_t address = reinterpret_cast<uintptr_t>(ptr); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3931 | return ((address & i::kEncodablePointerMask) == 0); |
| 3932 | } |
| 3933 | |
| 3934 | |
| 3935 | static i::Smi* EncodeAsSmi(void* ptr) { |
| 3936 | ASSERT(CanBeEncodedAsSmi(ptr)); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3937 | const uintptr_t address = reinterpret_cast<uintptr_t>(ptr); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3938 | i::Smi* result = reinterpret_cast<i::Smi*>(address << i::kPointerToSmiShift); |
| 3939 | ASSERT(i::Internals::HasSmiTag(result)); |
| 3940 | ASSERT_EQ(result, i::Smi::FromInt(result->value())); |
| 3941 | ASSERT_EQ(ptr, i::Internals::GetExternalPointerFromSmi(result)); |
| 3942 | return result; |
| 3943 | } |
| 3944 | |
| 3945 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3946 | void v8::Object::SetPointerInInternalField(int index, void* value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3947 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3948 | ENTER_V8(isolate); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3949 | if (CanBeEncodedAsSmi(value)) { |
| 3950 | Utils::OpenHandle(this)->SetInternalField(index, EncodeAsSmi(value)); |
| 3951 | } else { |
| 3952 | HandleScope scope; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3953 | i::Handle<i::Foreign> foreign = |
| 3954 | isolate->factory()->NewForeign( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3955 | reinterpret_cast<i::Address>(value), i::TENURED); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3956 | if (!foreign.is_null()) |
| 3957 | Utils::OpenHandle(this)->SetInternalField(index, *foreign); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3958 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3959 | ASSERT_EQ(value, GetPointerFromInternalField(index)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3960 | } |
| 3961 | |
| 3962 | |
| 3963 | // --- E n v i r o n m e n t --- |
| 3964 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3965 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3966 | bool v8::V8::Initialize() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3967 | i::Isolate* isolate = i::Isolate::UncheckedCurrent(); |
| 3968 | if (isolate != NULL && isolate->IsInitialized()) { |
| 3969 | return true; |
| 3970 | } |
| 3971 | return InitializeHelper(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
| 3974 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3975 | void v8::V8::SetEntropySource(EntropySource source) { |
| 3976 | i::V8::SetEntropySource(source); |
| 3977 | } |
| 3978 | |
| 3979 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3980 | bool v8::V8::Dispose() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3981 | i::Isolate* isolate = i::Isolate::Current(); |
| 3982 | if (!ApiCheck(isolate != NULL && isolate->IsDefaultIsolate(), |
| 3983 | "v8::V8::Dispose()", |
| 3984 | "Use v8::Isolate::Dispose() for a non-default isolate.")) { |
| 3985 | return false; |
| 3986 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3987 | i::V8::TearDown(); |
| 3988 | return true; |
| 3989 | } |
| 3990 | |
| 3991 | |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 3992 | HeapStatistics::HeapStatistics(): total_heap_size_(0), |
| 3993 | total_heap_size_executable_(0), |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3994 | used_heap_size_(0), |
| 3995 | heap_size_limit_(0) { } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3996 | |
| 3997 | |
| 3998 | void v8::V8::GetHeapStatistics(HeapStatistics* heap_statistics) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3999 | i::Heap* heap = i::Isolate::Current()->heap(); |
| 4000 | heap_statistics->set_total_heap_size(heap->CommittedMemory()); |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 4001 | heap_statistics->set_total_heap_size_executable( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4002 | heap->CommittedMemoryExecutable()); |
| 4003 | heap_statistics->set_used_heap_size(heap->SizeOfObjects()); |
| 4004 | heap_statistics->set_heap_size_limit(heap->MaxReserved()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4005 | } |
| 4006 | |
| 4007 | |
| 4008 | bool v8::V8::IdleNotification() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4009 | // Returning true tells the caller that it need not |
| 4010 | // continue to call IdleNotification. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4011 | if (!i::Isolate::Current()->IsInitialized()) return true; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4012 | return i::V8::IdleNotification(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | |
| 4016 | void v8::V8::LowMemoryNotification() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4017 | i::Isolate* isolate = i::Isolate::Current(); |
| 4018 | if (!isolate->IsInitialized()) return; |
| 4019 | isolate->heap()->CollectAllGarbage(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4020 | } |
| 4021 | |
| 4022 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4023 | int v8::V8::ContextDisposedNotification() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4024 | i::Isolate* isolate = i::Isolate::Current(); |
| 4025 | if (!isolate->IsInitialized()) return 0; |
| 4026 | return isolate->heap()->NotifyContextDisposed(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4027 | } |
| 4028 | |
| 4029 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4030 | const char* v8::V8::GetVersion() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4031 | return i::Version::GetVersion(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4032 | } |
| 4033 | |
| 4034 | |
| 4035 | static i::Handle<i::FunctionTemplateInfo> |
| 4036 | EnsureConstructor(i::Handle<i::ObjectTemplateInfo> templ) { |
| 4037 | if (templ->constructor()->IsUndefined()) { |
| 4038 | Local<FunctionTemplate> constructor = FunctionTemplate::New(); |
| 4039 | Utils::OpenHandle(*constructor)->set_instance_template(*templ); |
| 4040 | templ->set_constructor(*Utils::OpenHandle(*constructor)); |
| 4041 | } |
| 4042 | return i::Handle<i::FunctionTemplateInfo>( |
| 4043 | i::FunctionTemplateInfo::cast(templ->constructor())); |
| 4044 | } |
| 4045 | |
| 4046 | |
| 4047 | Persistent<Context> v8::Context::New( |
| 4048 | v8::ExtensionConfiguration* extensions, |
| 4049 | v8::Handle<ObjectTemplate> global_template, |
| 4050 | v8::Handle<Value> global_object) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4051 | i::Isolate* isolate = i::Isolate::Current(); |
| 4052 | EnsureInitializedForIsolate(isolate, "v8::Context::New()"); |
| 4053 | LOG_API(isolate, "Context::New"); |
| 4054 | ON_BAILOUT(isolate, "v8::Context::New()", return Persistent<Context>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4055 | |
| 4056 | // Enter V8 via an ENTER_V8 scope. |
| 4057 | i::Handle<i::Context> env; |
| 4058 | { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4059 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4060 | v8::Handle<ObjectTemplate> proxy_template = global_template; |
| 4061 | i::Handle<i::FunctionTemplateInfo> proxy_constructor; |
| 4062 | i::Handle<i::FunctionTemplateInfo> global_constructor; |
| 4063 | |
| 4064 | if (!global_template.IsEmpty()) { |
| 4065 | // Make sure that the global_template has a constructor. |
| 4066 | global_constructor = |
| 4067 | EnsureConstructor(Utils::OpenHandle(*global_template)); |
| 4068 | |
| 4069 | // Create a fresh template for the global proxy object. |
| 4070 | proxy_template = ObjectTemplate::New(); |
| 4071 | proxy_constructor = |
| 4072 | EnsureConstructor(Utils::OpenHandle(*proxy_template)); |
| 4073 | |
| 4074 | // Set the global template to be the prototype template of |
| 4075 | // global proxy template. |
| 4076 | proxy_constructor->set_prototype_template( |
| 4077 | *Utils::OpenHandle(*global_template)); |
| 4078 | |
| 4079 | // Migrate security handlers from global_template to |
| 4080 | // proxy_template. Temporarily removing access check |
| 4081 | // information from the global template. |
| 4082 | if (!global_constructor->access_check_info()->IsUndefined()) { |
| 4083 | proxy_constructor->set_access_check_info( |
| 4084 | global_constructor->access_check_info()); |
| 4085 | proxy_constructor->set_needs_access_check( |
| 4086 | global_constructor->needs_access_check()); |
| 4087 | global_constructor->set_needs_access_check(false); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4088 | global_constructor->set_access_check_info( |
| 4089 | isolate->heap()->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4090 | } |
| 4091 | } |
| 4092 | |
| 4093 | // Create the environment. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4094 | env = isolate->bootstrapper()->CreateEnvironment( |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4095 | isolate, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4096 | Utils::OpenHandle(*global_object), |
| 4097 | proxy_template, |
| 4098 | extensions); |
| 4099 | |
| 4100 | // Restore the access check info on the global template. |
| 4101 | if (!global_template.IsEmpty()) { |
| 4102 | ASSERT(!global_constructor.is_null()); |
| 4103 | ASSERT(!proxy_constructor.is_null()); |
| 4104 | global_constructor->set_access_check_info( |
| 4105 | proxy_constructor->access_check_info()); |
| 4106 | global_constructor->set_needs_access_check( |
| 4107 | proxy_constructor->needs_access_check()); |
| 4108 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4109 | isolate->runtime_profiler()->Reset(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4110 | } |
| 4111 | // Leave V8. |
| 4112 | |
| 4113 | if (env.is_null()) |
| 4114 | return Persistent<Context>(); |
| 4115 | return Persistent<Context>(Utils::ToLocal(env)); |
| 4116 | } |
| 4117 | |
| 4118 | |
| 4119 | void v8::Context::SetSecurityToken(Handle<Value> token) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4120 | i::Isolate* isolate = i::Isolate::Current(); |
| 4121 | if (IsDeadCheck(isolate, "v8::Context::SetSecurityToken()")) { |
| 4122 | return; |
| 4123 | } |
| 4124 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4125 | i::Handle<i::Context> env = Utils::OpenHandle(this); |
| 4126 | i::Handle<i::Object> token_handle = Utils::OpenHandle(*token); |
| 4127 | env->set_security_token(*token_handle); |
| 4128 | } |
| 4129 | |
| 4130 | |
| 4131 | void v8::Context::UseDefaultSecurityToken() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4132 | i::Isolate* isolate = i::Isolate::Current(); |
| 4133 | if (IsDeadCheck(isolate, |
| 4134 | "v8::Context::UseDefaultSecurityToken()")) { |
| 4135 | return; |
| 4136 | } |
| 4137 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4138 | i::Handle<i::Context> env = Utils::OpenHandle(this); |
| 4139 | env->set_security_token(env->global()); |
| 4140 | } |
| 4141 | |
| 4142 | |
| 4143 | Handle<Value> v8::Context::GetSecurityToken() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4144 | i::Isolate* isolate = i::Isolate::Current(); |
| 4145 | if (IsDeadCheck(isolate, "v8::Context::GetSecurityToken()")) { |
| 4146 | return Handle<Value>(); |
| 4147 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4148 | i::Handle<i::Context> env = Utils::OpenHandle(this); |
| 4149 | i::Object* security_token = env->security_token(); |
| 4150 | i::Handle<i::Object> token_handle(security_token); |
| 4151 | return Utils::ToLocal(token_handle); |
| 4152 | } |
| 4153 | |
| 4154 | |
| 4155 | bool Context::HasOutOfMemoryException() { |
| 4156 | i::Handle<i::Context> env = Utils::OpenHandle(this); |
| 4157 | return env->has_out_of_memory(); |
| 4158 | } |
| 4159 | |
| 4160 | |
| 4161 | bool Context::InContext() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4162 | return i::Isolate::Current()->context() != NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
| 4165 | |
| 4166 | v8::Local<v8::Context> Context::GetEntered() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4167 | i::Isolate* isolate = i::Isolate::Current(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4168 | if (!EnsureInitializedForIsolate(isolate, "v8::Context::GetEntered()")) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4169 | return Local<Context>(); |
| 4170 | } |
| 4171 | i::Handle<i::Object> last = |
| 4172 | isolate->handle_scope_implementer()->LastEnteredContext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4173 | if (last.is_null()) return Local<Context>(); |
| 4174 | i::Handle<i::Context> context = i::Handle<i::Context>::cast(last); |
| 4175 | return Utils::ToLocal(context); |
| 4176 | } |
| 4177 | |
| 4178 | |
| 4179 | v8::Local<v8::Context> Context::GetCurrent() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4180 | i::Isolate* isolate = i::Isolate::Current(); |
| 4181 | if (IsDeadCheck(isolate, "v8::Context::GetCurrent()")) { |
| 4182 | return Local<Context>(); |
| 4183 | } |
| 4184 | i::Handle<i::Object> current = isolate->global_context(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4185 | if (current.is_null()) return Local<Context>(); |
| 4186 | i::Handle<i::Context> context = i::Handle<i::Context>::cast(current); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4187 | return Utils::ToLocal(context); |
| 4188 | } |
| 4189 | |
| 4190 | |
| 4191 | v8::Local<v8::Context> Context::GetCalling() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4192 | i::Isolate* isolate = i::Isolate::Current(); |
| 4193 | if (IsDeadCheck(isolate, "v8::Context::GetCalling()")) { |
| 4194 | return Local<Context>(); |
| 4195 | } |
| 4196 | i::Handle<i::Object> calling = |
| 4197 | isolate->GetCallingGlobalContext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4198 | if (calling.is_null()) return Local<Context>(); |
| 4199 | i::Handle<i::Context> context = i::Handle<i::Context>::cast(calling); |
| 4200 | return Utils::ToLocal(context); |
| 4201 | } |
| 4202 | |
| 4203 | |
| 4204 | v8::Local<v8::Object> Context::Global() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4205 | if (IsDeadCheck(i::Isolate::Current(), "v8::Context::Global()")) { |
| 4206 | return Local<v8::Object>(); |
| 4207 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4208 | i::Object** ctx = reinterpret_cast<i::Object**>(this); |
| 4209 | i::Handle<i::Context> context = |
| 4210 | i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx)); |
| 4211 | i::Handle<i::Object> global(context->global_proxy()); |
| 4212 | return Utils::ToLocal(i::Handle<i::JSObject>::cast(global)); |
| 4213 | } |
| 4214 | |
| 4215 | |
| 4216 | void Context::DetachGlobal() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4217 | i::Isolate* isolate = i::Isolate::Current(); |
| 4218 | if (IsDeadCheck(isolate, "v8::Context::DetachGlobal()")) return; |
| 4219 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4220 | i::Object** ctx = reinterpret_cast<i::Object**>(this); |
| 4221 | i::Handle<i::Context> context = |
| 4222 | i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4223 | isolate->bootstrapper()->DetachGlobal(context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
| 4226 | |
Andrei Popescu | 74b3c14 | 2010-03-29 12:03:09 +0100 | [diff] [blame] | 4227 | void Context::ReattachGlobal(Handle<Object> global_object) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4228 | i::Isolate* isolate = i::Isolate::Current(); |
| 4229 | if (IsDeadCheck(isolate, "v8::Context::ReattachGlobal()")) return; |
| 4230 | ENTER_V8(isolate); |
Andrei Popescu | 74b3c14 | 2010-03-29 12:03:09 +0100 | [diff] [blame] | 4231 | i::Object** ctx = reinterpret_cast<i::Object**>(this); |
| 4232 | i::Handle<i::Context> context = |
| 4233 | i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4234 | isolate->bootstrapper()->ReattachGlobal( |
| 4235 | context, |
| 4236 | Utils::OpenHandle(*global_object)); |
| 4237 | } |
| 4238 | |
| 4239 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4240 | void Context::AllowCodeGenerationFromStrings(bool allow) { |
| 4241 | i::Isolate* isolate = i::Isolate::Current(); |
| 4242 | if (IsDeadCheck(isolate, "v8::Context::AllowCodeGenerationFromStrings()")) { |
| 4243 | return; |
| 4244 | } |
| 4245 | ENTER_V8(isolate); |
| 4246 | i::Object** ctx = reinterpret_cast<i::Object**>(this); |
| 4247 | i::Handle<i::Context> context = |
| 4248 | i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx)); |
| 4249 | context->set_allow_code_gen_from_strings( |
| 4250 | allow ? isolate->heap()->true_value() : isolate->heap()->false_value()); |
| 4251 | } |
| 4252 | |
| 4253 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4254 | void V8::SetWrapperClassId(i::Object** global_handle, uint16_t class_id) { |
| 4255 | i::GlobalHandles::SetWrapperClassId(global_handle, class_id); |
Andrei Popescu | 74b3c14 | 2010-03-29 12:03:09 +0100 | [diff] [blame] | 4256 | } |
| 4257 | |
| 4258 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4259 | Local<v8::Object> ObjectTemplate::NewInstance() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4260 | i::Isolate* isolate = i::Isolate::Current(); |
| 4261 | ON_BAILOUT(isolate, "v8::ObjectTemplate::NewInstance()", |
| 4262 | return Local<v8::Object>()); |
| 4263 | LOG_API(isolate, "ObjectTemplate::NewInstance"); |
| 4264 | ENTER_V8(isolate); |
| 4265 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4266 | i::Handle<i::Object> obj = |
| 4267 | i::Execution::InstantiateObject(Utils::OpenHandle(this), |
| 4268 | &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4269 | EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4270 | return Utils::ToLocal(i::Handle<i::JSObject>::cast(obj)); |
| 4271 | } |
| 4272 | |
| 4273 | |
| 4274 | Local<v8::Function> FunctionTemplate::GetFunction() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4275 | i::Isolate* isolate = i::Isolate::Current(); |
| 4276 | ON_BAILOUT(isolate, "v8::FunctionTemplate::GetFunction()", |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4277 | return Local<v8::Function>()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4278 | LOG_API(isolate, "FunctionTemplate::GetFunction"); |
| 4279 | ENTER_V8(isolate); |
| 4280 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4281 | i::Handle<i::Object> obj = |
| 4282 | i::Execution::InstantiateFunction(Utils::OpenHandle(this), |
| 4283 | &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4284 | EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Function>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4285 | return Utils::ToLocal(i::Handle<i::JSFunction>::cast(obj)); |
| 4286 | } |
| 4287 | |
| 4288 | |
| 4289 | bool FunctionTemplate::HasInstance(v8::Handle<v8::Value> value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4290 | ON_BAILOUT(i::Isolate::Current(), "v8::FunctionTemplate::HasInstanceOf()", |
| 4291 | return false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4292 | i::Object* obj = *Utils::OpenHandle(*value); |
| 4293 | return obj->IsInstanceOf(*Utils::OpenHandle(this)); |
| 4294 | } |
| 4295 | |
| 4296 | |
| 4297 | static Local<External> ExternalNewImpl(void* data) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4298 | return Utils::ToLocal(FACTORY->NewForeign(static_cast<i::Address>(data))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4299 | } |
| 4300 | |
| 4301 | static void* ExternalValueImpl(i::Handle<i::Object> obj) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4302 | return reinterpret_cast<void*>(i::Foreign::cast(*obj)->address()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4303 | } |
| 4304 | |
| 4305 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4306 | Local<Value> v8::External::Wrap(void* data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4307 | i::Isolate* isolate = i::Isolate::Current(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4308 | STATIC_ASSERT(sizeof(data) == sizeof(i::Address)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4309 | EnsureInitializedForIsolate(isolate, "v8::External::Wrap()"); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4310 | LOG_API(isolate, "External::Wrap"); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4311 | ENTER_V8(isolate); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 4312 | |
| 4313 | v8::Local<v8::Value> result = CanBeEncodedAsSmi(data) |
| 4314 | ? Utils::ToLocal(i::Handle<i::Object>(EncodeAsSmi(data))) |
| 4315 | : v8::Local<v8::Value>(ExternalNewImpl(data)); |
| 4316 | |
| 4317 | ASSERT_EQ(data, Unwrap(result)); |
| 4318 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4322 | void* v8::Object::SlowGetPointerFromInternalField(int index) { |
| 4323 | i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
| 4324 | i::Object* value = obj->GetInternalField(index); |
| 4325 | if (value->IsSmi()) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 4326 | return i::Internals::GetExternalPointerFromSmi(value); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4327 | } else if (value->IsForeign()) { |
| 4328 | return reinterpret_cast<void*>(i::Foreign::cast(value)->address()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4329 | } else { |
| 4330 | return NULL; |
| 4331 | } |
| 4332 | } |
| 4333 | |
| 4334 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4335 | void* v8::External::FullUnwrap(v8::Handle<v8::Value> wrapper) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4336 | if (IsDeadCheck(i::Isolate::Current(), "v8::External::Unwrap()")) return 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4337 | i::Handle<i::Object> obj = Utils::OpenHandle(*wrapper); |
| 4338 | void* result; |
| 4339 | if (obj->IsSmi()) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 4340 | result = i::Internals::GetExternalPointerFromSmi(*obj); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4341 | } else if (obj->IsForeign()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4342 | result = ExternalValueImpl(obj); |
| 4343 | } else { |
| 4344 | result = NULL; |
| 4345 | } |
| 4346 | ASSERT_EQ(result, QuickUnwrap(wrapper)); |
| 4347 | return result; |
| 4348 | } |
| 4349 | |
| 4350 | |
| 4351 | Local<External> v8::External::New(void* data) { |
| 4352 | STATIC_ASSERT(sizeof(data) == sizeof(i::Address)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4353 | i::Isolate* isolate = i::Isolate::Current(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4354 | EnsureInitializedForIsolate(isolate, "v8::External::New()"); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4355 | LOG_API(isolate, "External::New"); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4356 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4357 | return ExternalNewImpl(data); |
| 4358 | } |
| 4359 | |
| 4360 | |
| 4361 | void* External::Value() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4362 | if (IsDeadCheck(i::Isolate::Current(), "v8::External::Value()")) return 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4363 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 4364 | return ExternalValueImpl(obj); |
| 4365 | } |
| 4366 | |
| 4367 | |
| 4368 | Local<String> v8::String::Empty() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4369 | i::Isolate* isolate = i::Isolate::Current(); |
| 4370 | EnsureInitializedForIsolate(isolate, "v8::String::Empty()"); |
| 4371 | LOG_API(isolate, "String::Empty()"); |
| 4372 | return Utils::ToLocal(isolate->factory()->empty_symbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
| 4375 | |
| 4376 | Local<String> v8::String::New(const char* data, int length) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4377 | i::Isolate* isolate = i::Isolate::Current(); |
| 4378 | EnsureInitializedForIsolate(isolate, "v8::String::New()"); |
| 4379 | LOG_API(isolate, "String::New(char)"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4380 | if (length == 0) return Empty(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4381 | ENTER_V8(isolate); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4382 | if (length == -1) length = i::StrLength(data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4383 | i::Handle<i::String> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4384 | isolate->factory()->NewStringFromUtf8( |
| 4385 | i::Vector<const char>(data, length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4386 | return Utils::ToLocal(result); |
| 4387 | } |
| 4388 | |
| 4389 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4390 | Local<String> v8::String::Concat(Handle<String> left, Handle<String> right) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4391 | i::Handle<i::String> left_string = Utils::OpenHandle(*left); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4392 | i::Isolate* isolate = left_string->GetIsolate(); |
| 4393 | EnsureInitializedForIsolate(isolate, "v8::String::New()"); |
| 4394 | LOG_API(isolate, "String::New(char)"); |
| 4395 | ENTER_V8(isolate); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4396 | i::Handle<i::String> right_string = Utils::OpenHandle(*right); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4397 | i::Handle<i::String> result = isolate->factory()->NewConsString(left_string, |
| 4398 | right_string); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4399 | return Utils::ToLocal(result); |
| 4400 | } |
| 4401 | |
| 4402 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4403 | Local<String> v8::String::NewUndetectable(const char* data, int length) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4404 | i::Isolate* isolate = i::Isolate::Current(); |
| 4405 | EnsureInitializedForIsolate(isolate, "v8::String::NewUndetectable()"); |
| 4406 | LOG_API(isolate, "String::NewUndetectable(char)"); |
| 4407 | ENTER_V8(isolate); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4408 | if (length == -1) length = i::StrLength(data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4409 | i::Handle<i::String> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4410 | isolate->factory()->NewStringFromUtf8( |
| 4411 | i::Vector<const char>(data, length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4412 | result->MarkAsUndetectable(); |
| 4413 | return Utils::ToLocal(result); |
| 4414 | } |
| 4415 | |
| 4416 | |
| 4417 | static int TwoByteStringLength(const uint16_t* data) { |
| 4418 | int length = 0; |
| 4419 | while (data[length] != '\0') length++; |
| 4420 | return length; |
| 4421 | } |
| 4422 | |
| 4423 | |
| 4424 | Local<String> v8::String::New(const uint16_t* data, int length) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4425 | i::Isolate* isolate = i::Isolate::Current(); |
| 4426 | EnsureInitializedForIsolate(isolate, "v8::String::New()"); |
| 4427 | LOG_API(isolate, "String::New(uint16_)"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4428 | if (length == 0) return Empty(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4429 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4430 | if (length == -1) length = TwoByteStringLength(data); |
| 4431 | i::Handle<i::String> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4432 | isolate->factory()->NewStringFromTwoByte( |
| 4433 | i::Vector<const uint16_t>(data, length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4434 | return Utils::ToLocal(result); |
| 4435 | } |
| 4436 | |
| 4437 | |
| 4438 | Local<String> v8::String::NewUndetectable(const uint16_t* data, int length) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4439 | i::Isolate* isolate = i::Isolate::Current(); |
| 4440 | EnsureInitializedForIsolate(isolate, "v8::String::NewUndetectable()"); |
| 4441 | LOG_API(isolate, "String::NewUndetectable(uint16_)"); |
| 4442 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4443 | if (length == -1) length = TwoByteStringLength(data); |
| 4444 | i::Handle<i::String> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4445 | isolate->factory()->NewStringFromTwoByte( |
| 4446 | i::Vector<const uint16_t>(data, length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4447 | result->MarkAsUndetectable(); |
| 4448 | return Utils::ToLocal(result); |
| 4449 | } |
| 4450 | |
| 4451 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4452 | i::Handle<i::String> NewExternalStringHandle(i::Isolate* isolate, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4453 | v8::String::ExternalStringResource* resource) { |
| 4454 | i::Handle<i::String> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4455 | isolate->factory()->NewExternalStringFromTwoByte(resource); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4456 | return result; |
| 4457 | } |
| 4458 | |
| 4459 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4460 | i::Handle<i::String> NewExternalAsciiStringHandle(i::Isolate* isolate, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4461 | v8::String::ExternalAsciiStringResource* resource) { |
| 4462 | i::Handle<i::String> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4463 | isolate->factory()->NewExternalStringFromAscii(resource); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4464 | return result; |
| 4465 | } |
| 4466 | |
| 4467 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4468 | Local<String> v8::String::NewExternal( |
| 4469 | v8::String::ExternalStringResource* resource) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4470 | i::Isolate* isolate = i::Isolate::Current(); |
| 4471 | EnsureInitializedForIsolate(isolate, "v8::String::NewExternal()"); |
| 4472 | LOG_API(isolate, "String::NewExternal"); |
| 4473 | ENTER_V8(isolate); |
| 4474 | i::Handle<i::String> result = NewExternalStringHandle(isolate, resource); |
| 4475 | isolate->heap()->external_string_table()->AddString(*result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4476 | return Utils::ToLocal(result); |
| 4477 | } |
| 4478 | |
| 4479 | |
| 4480 | bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4481 | i::Handle<i::String> obj = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4482 | i::Isolate* isolate = obj->GetIsolate(); |
| 4483 | if (IsDeadCheck(isolate, "v8::String::MakeExternal()")) return false; |
| 4484 | if (i::StringShape(*obj).IsExternalTwoByte()) { |
| 4485 | return false; // Already an external string. |
| 4486 | } |
| 4487 | ENTER_V8(isolate); |
| 4488 | if (isolate->string_tracker()->IsFreshUnusedString(obj)) { |
| 4489 | return false; |
| 4490 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4491 | if (isolate->heap()->IsInGCPostProcessing()) { |
| 4492 | return false; |
| 4493 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4494 | bool result = obj->MakeExternal(resource); |
| 4495 | if (result && !obj->IsSymbol()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4496 | isolate->heap()->external_string_table()->AddString(*obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4497 | } |
| 4498 | return result; |
| 4499 | } |
| 4500 | |
| 4501 | |
| 4502 | Local<String> v8::String::NewExternal( |
| 4503 | v8::String::ExternalAsciiStringResource* resource) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4504 | i::Isolate* isolate = i::Isolate::Current(); |
| 4505 | EnsureInitializedForIsolate(isolate, "v8::String::NewExternal()"); |
| 4506 | LOG_API(isolate, "String::NewExternal"); |
| 4507 | ENTER_V8(isolate); |
| 4508 | i::Handle<i::String> result = NewExternalAsciiStringHandle(isolate, resource); |
| 4509 | isolate->heap()->external_string_table()->AddString(*result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4510 | return Utils::ToLocal(result); |
| 4511 | } |
| 4512 | |
| 4513 | |
| 4514 | bool v8::String::MakeExternal( |
| 4515 | v8::String::ExternalAsciiStringResource* resource) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4516 | i::Handle<i::String> obj = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4517 | i::Isolate* isolate = obj->GetIsolate(); |
| 4518 | if (IsDeadCheck(isolate, "v8::String::MakeExternal()")) return false; |
| 4519 | if (i::StringShape(*obj).IsExternalTwoByte()) { |
| 4520 | return false; // Already an external string. |
| 4521 | } |
| 4522 | ENTER_V8(isolate); |
| 4523 | if (isolate->string_tracker()->IsFreshUnusedString(obj)) { |
| 4524 | return false; |
| 4525 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4526 | if (isolate->heap()->IsInGCPostProcessing()) { |
| 4527 | return false; |
| 4528 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4529 | bool result = obj->MakeExternal(resource); |
| 4530 | if (result && !obj->IsSymbol()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4531 | isolate->heap()->external_string_table()->AddString(*obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4532 | } |
| 4533 | return result; |
| 4534 | } |
| 4535 | |
| 4536 | |
| 4537 | bool v8::String::CanMakeExternal() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4538 | i::Handle<i::String> obj = Utils::OpenHandle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4539 | i::Isolate* isolate = obj->GetIsolate(); |
| 4540 | if (IsDeadCheck(isolate, "v8::String::CanMakeExternal()")) return false; |
| 4541 | if (isolate->string_tracker()->IsFreshUnusedString(obj)) { |
| 4542 | return false; |
| 4543 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4544 | int size = obj->Size(); // Byte size of the original string. |
| 4545 | if (size < i::ExternalString::kSize) |
| 4546 | return false; |
| 4547 | i::StringShape shape(*obj); |
| 4548 | return !shape.IsExternal(); |
| 4549 | } |
| 4550 | |
| 4551 | |
| 4552 | Local<v8::Object> v8::Object::New() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4553 | i::Isolate* isolate = i::Isolate::Current(); |
| 4554 | EnsureInitializedForIsolate(isolate, "v8::Object::New()"); |
| 4555 | LOG_API(isolate, "Object::New"); |
| 4556 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4557 | i::Handle<i::JSObject> obj = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4558 | isolate->factory()->NewJSObject(isolate->object_function()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4559 | return Utils::ToLocal(obj); |
| 4560 | } |
| 4561 | |
| 4562 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4563 | Local<v8::Value> v8::NumberObject::New(double value) { |
| 4564 | i::Isolate* isolate = i::Isolate::Current(); |
| 4565 | EnsureInitializedForIsolate(isolate, "v8::NumberObject::New()"); |
| 4566 | LOG_API(isolate, "NumberObject::New"); |
| 4567 | ENTER_V8(isolate); |
| 4568 | i::Handle<i::Object> number = isolate->factory()->NewNumber(value); |
| 4569 | i::Handle<i::Object> obj = isolate->factory()->ToObject(number); |
| 4570 | return Utils::ToLocal(obj); |
| 4571 | } |
| 4572 | |
| 4573 | |
| 4574 | double v8::NumberObject::NumberValue() const { |
| 4575 | i::Isolate* isolate = i::Isolate::Current(); |
| 4576 | if (IsDeadCheck(isolate, "v8::NumberObject::NumberValue()")) return 0; |
| 4577 | LOG_API(isolate, "NumberObject::NumberValue"); |
| 4578 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 4579 | i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj); |
| 4580 | return jsvalue->value()->Number(); |
| 4581 | } |
| 4582 | |
| 4583 | |
| 4584 | Local<v8::Value> v8::BooleanObject::New(bool value) { |
| 4585 | i::Isolate* isolate = i::Isolate::Current(); |
| 4586 | EnsureInitializedForIsolate(isolate, "v8::BooleanObject::New()"); |
| 4587 | LOG_API(isolate, "BooleanObject::New"); |
| 4588 | ENTER_V8(isolate); |
| 4589 | i::Handle<i::Object> boolean(value ? isolate->heap()->true_value() |
| 4590 | : isolate->heap()->false_value()); |
| 4591 | i::Handle<i::Object> obj = isolate->factory()->ToObject(boolean); |
| 4592 | return Utils::ToLocal(obj); |
| 4593 | } |
| 4594 | |
| 4595 | |
| 4596 | bool v8::BooleanObject::BooleanValue() const { |
| 4597 | i::Isolate* isolate = i::Isolate::Current(); |
| 4598 | if (IsDeadCheck(isolate, "v8::BooleanObject::BooleanValue()")) return 0; |
| 4599 | LOG_API(isolate, "BooleanObject::BooleanValue"); |
| 4600 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 4601 | i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj); |
| 4602 | return jsvalue->value()->IsTrue(); |
| 4603 | } |
| 4604 | |
| 4605 | |
| 4606 | Local<v8::Value> v8::StringObject::New(Handle<String> value) { |
| 4607 | i::Isolate* isolate = i::Isolate::Current(); |
| 4608 | EnsureInitializedForIsolate(isolate, "v8::StringObject::New()"); |
| 4609 | LOG_API(isolate, "StringObject::New"); |
| 4610 | ENTER_V8(isolate); |
| 4611 | i::Handle<i::Object> obj = |
| 4612 | isolate->factory()->ToObject(Utils::OpenHandle(*value)); |
| 4613 | return Utils::ToLocal(obj); |
| 4614 | } |
| 4615 | |
| 4616 | |
| 4617 | Local<v8::String> v8::StringObject::StringValue() const { |
| 4618 | i::Isolate* isolate = i::Isolate::Current(); |
| 4619 | if (IsDeadCheck(isolate, "v8::StringObject::StringValue()")) { |
| 4620 | return Local<v8::String>(); |
| 4621 | } |
| 4622 | LOG_API(isolate, "StringObject::StringValue"); |
| 4623 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 4624 | i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj); |
| 4625 | return Utils::ToLocal( |
| 4626 | i::Handle<i::String>(i::String::cast(jsvalue->value()))); |
| 4627 | } |
| 4628 | |
| 4629 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4630 | Local<v8::Value> v8::Date::New(double time) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4631 | i::Isolate* isolate = i::Isolate::Current(); |
| 4632 | EnsureInitializedForIsolate(isolate, "v8::Date::New()"); |
| 4633 | LOG_API(isolate, "Date::New"); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4634 | if (isnan(time)) { |
| 4635 | // Introduce only canonical NaN value into the VM, to avoid signaling NaNs. |
| 4636 | time = i::OS::nan_value(); |
| 4637 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4638 | ENTER_V8(isolate); |
| 4639 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4640 | i::Handle<i::Object> obj = |
| 4641 | i::Execution::NewDate(time, &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4642 | EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Value>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4643 | return Utils::ToLocal(obj); |
| 4644 | } |
| 4645 | |
| 4646 | |
| 4647 | double v8::Date::NumberValue() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4648 | i::Isolate* isolate = i::Isolate::Current(); |
| 4649 | if (IsDeadCheck(isolate, "v8::Date::NumberValue()")) return 0; |
| 4650 | LOG_API(isolate, "Date::NumberValue"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4651 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 4652 | i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj); |
| 4653 | return jsvalue->value()->Number(); |
| 4654 | } |
| 4655 | |
| 4656 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4657 | void v8::Date::DateTimeConfigurationChangeNotification() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4658 | i::Isolate* isolate = i::Isolate::Current(); |
| 4659 | ON_BAILOUT(isolate, "v8::Date::DateTimeConfigurationChangeNotification()", |
| 4660 | return); |
| 4661 | LOG_API(isolate, "Date::DateTimeConfigurationChangeNotification"); |
| 4662 | ENTER_V8(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4663 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4664 | i::HandleScope scope(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4665 | // Get the function ResetDateCache (defined in date-delay.js). |
| 4666 | i::Handle<i::String> func_name_str = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4667 | isolate->factory()->LookupAsciiSymbol("ResetDateCache"); |
| 4668 | i::MaybeObject* result = |
| 4669 | isolate->js_builtins_object()->GetProperty(*func_name_str); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4670 | i::Object* object_func; |
| 4671 | if (!result->ToObject(&object_func)) { |
| 4672 | return; |
| 4673 | } |
| 4674 | |
| 4675 | if (object_func->IsJSFunction()) { |
| 4676 | i::Handle<i::JSFunction> func = |
| 4677 | i::Handle<i::JSFunction>(i::JSFunction::cast(object_func)); |
| 4678 | |
| 4679 | // Call ResetDateCache(0 but expect no exceptions: |
| 4680 | bool caught_exception = false; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4681 | i::Execution::TryCall(func, |
| 4682 | isolate->js_builtins_object(), |
| 4683 | 0, |
| 4684 | NULL, |
| 4685 | &caught_exception); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4686 | } |
| 4687 | } |
| 4688 | |
| 4689 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 4690 | static i::Handle<i::String> RegExpFlagsToString(RegExp::Flags flags) { |
| 4691 | char flags_buf[3]; |
| 4692 | int num_flags = 0; |
| 4693 | if ((flags & RegExp::kGlobal) != 0) flags_buf[num_flags++] = 'g'; |
| 4694 | if ((flags & RegExp::kMultiline) != 0) flags_buf[num_flags++] = 'm'; |
| 4695 | if ((flags & RegExp::kIgnoreCase) != 0) flags_buf[num_flags++] = 'i'; |
| 4696 | ASSERT(num_flags <= static_cast<int>(ARRAY_SIZE(flags_buf))); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4697 | return FACTORY->LookupSymbol( |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 4698 | i::Vector<const char>(flags_buf, num_flags)); |
| 4699 | } |
| 4700 | |
| 4701 | |
| 4702 | Local<v8::RegExp> v8::RegExp::New(Handle<String> pattern, |
| 4703 | Flags flags) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4704 | i::Isolate* isolate = Utils::OpenHandle(*pattern)->GetIsolate(); |
| 4705 | EnsureInitializedForIsolate(isolate, "v8::RegExp::New()"); |
| 4706 | LOG_API(isolate, "RegExp::New"); |
| 4707 | ENTER_V8(isolate); |
| 4708 | EXCEPTION_PREAMBLE(isolate); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 4709 | i::Handle<i::JSRegExp> obj = i::Execution::NewJSRegExp( |
| 4710 | Utils::OpenHandle(*pattern), |
| 4711 | RegExpFlagsToString(flags), |
| 4712 | &has_pending_exception); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4713 | EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::RegExp>()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 4714 | return Utils::ToLocal(i::Handle<i::JSRegExp>::cast(obj)); |
| 4715 | } |
| 4716 | |
| 4717 | |
| 4718 | Local<v8::String> v8::RegExp::GetSource() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4719 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 4720 | if (IsDeadCheck(isolate, "v8::RegExp::GetSource()")) { |
| 4721 | return Local<v8::String>(); |
| 4722 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 4723 | i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this); |
| 4724 | return Utils::ToLocal(i::Handle<i::String>(obj->Pattern())); |
| 4725 | } |
| 4726 | |
| 4727 | |
| 4728 | // Assert that the static flags cast in GetFlags is valid. |
| 4729 | #define REGEXP_FLAG_ASSERT_EQ(api_flag, internal_flag) \ |
| 4730 | STATIC_ASSERT(static_cast<int>(v8::RegExp::api_flag) == \ |
| 4731 | static_cast<int>(i::JSRegExp::internal_flag)) |
| 4732 | REGEXP_FLAG_ASSERT_EQ(kNone, NONE); |
| 4733 | REGEXP_FLAG_ASSERT_EQ(kGlobal, GLOBAL); |
| 4734 | REGEXP_FLAG_ASSERT_EQ(kIgnoreCase, IGNORE_CASE); |
| 4735 | REGEXP_FLAG_ASSERT_EQ(kMultiline, MULTILINE); |
| 4736 | #undef REGEXP_FLAG_ASSERT_EQ |
| 4737 | |
| 4738 | v8::RegExp::Flags v8::RegExp::GetFlags() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4739 | if (IsDeadCheck(i::Isolate::Current(), "v8::RegExp::GetFlags()")) { |
| 4740 | return v8::RegExp::kNone; |
| 4741 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 4742 | i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this); |
| 4743 | return static_cast<RegExp::Flags>(obj->GetFlags().value()); |
| 4744 | } |
| 4745 | |
| 4746 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4747 | Local<v8::Array> v8::Array::New(int length) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4748 | i::Isolate* isolate = i::Isolate::Current(); |
| 4749 | EnsureInitializedForIsolate(isolate, "v8::Array::New()"); |
| 4750 | LOG_API(isolate, "Array::New"); |
| 4751 | ENTER_V8(isolate); |
| 4752 | int real_length = length > 0 ? length : 0; |
| 4753 | i::Handle<i::JSArray> obj = isolate->factory()->NewJSArray(real_length); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4754 | i::Handle<i::Object> length_obj = |
| 4755 | isolate->factory()->NewNumberFromInt(real_length); |
| 4756 | obj->set_length(*length_obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4757 | return Utils::ToLocal(obj); |
| 4758 | } |
| 4759 | |
| 4760 | |
| 4761 | uint32_t v8::Array::Length() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4762 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 4763 | if (IsDeadCheck(isolate, "v8::Array::Length()")) return 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4764 | i::Handle<i::JSArray> obj = Utils::OpenHandle(this); |
| 4765 | i::Object* length = obj->length(); |
| 4766 | if (length->IsSmi()) { |
| 4767 | return i::Smi::cast(length)->value(); |
| 4768 | } else { |
| 4769 | return static_cast<uint32_t>(length->Number()); |
| 4770 | } |
| 4771 | } |
| 4772 | |
| 4773 | |
| 4774 | Local<Object> Array::CloneElementAt(uint32_t index) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4775 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 4776 | ON_BAILOUT(isolate, "v8::Array::CloneElementAt()", return Local<Object>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4777 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 4778 | if (!self->HasFastElements()) { |
| 4779 | return Local<Object>(); |
| 4780 | } |
| 4781 | i::FixedArray* elms = i::FixedArray::cast(self->elements()); |
| 4782 | i::Object* paragon = elms->get(index); |
| 4783 | if (!paragon->IsJSObject()) { |
| 4784 | return Local<Object>(); |
| 4785 | } |
| 4786 | i::Handle<i::JSObject> paragon_handle(i::JSObject::cast(paragon)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4787 | EXCEPTION_PREAMBLE(isolate); |
| 4788 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4789 | i::Handle<i::JSObject> result = i::Copy(paragon_handle); |
| 4790 | has_pending_exception = result.is_null(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4791 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4792 | return Utils::ToLocal(result); |
| 4793 | } |
| 4794 | |
| 4795 | |
| 4796 | Local<String> v8::String::NewSymbol(const char* data, int length) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4797 | i::Isolate* isolate = i::Isolate::Current(); |
| 4798 | EnsureInitializedForIsolate(isolate, "v8::String::NewSymbol()"); |
| 4799 | LOG_API(isolate, "String::NewSymbol(char)"); |
| 4800 | ENTER_V8(isolate); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4801 | if (length == -1) length = i::StrLength(data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4802 | i::Handle<i::String> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4803 | isolate->factory()->LookupSymbol(i::Vector<const char>(data, length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4804 | return Utils::ToLocal(result); |
| 4805 | } |
| 4806 | |
| 4807 | |
| 4808 | Local<Number> v8::Number::New(double value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4809 | i::Isolate* isolate = i::Isolate::Current(); |
| 4810 | EnsureInitializedForIsolate(isolate, "v8::Number::New()"); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4811 | if (isnan(value)) { |
| 4812 | // Introduce only canonical NaN value into the VM, to avoid signaling NaNs. |
| 4813 | value = i::OS::nan_value(); |
| 4814 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4815 | ENTER_V8(isolate); |
| 4816 | i::Handle<i::Object> result = isolate->factory()->NewNumber(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4817 | return Utils::NumberToLocal(result); |
| 4818 | } |
| 4819 | |
| 4820 | |
| 4821 | Local<Integer> v8::Integer::New(int32_t value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4822 | i::Isolate* isolate = i::Isolate::UncheckedCurrent(); |
| 4823 | EnsureInitializedForIsolate(isolate, "v8::Integer::New()"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4824 | if (i::Smi::IsValid(value)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4825 | return Utils::IntegerToLocal(i::Handle<i::Object>(i::Smi::FromInt(value), |
| 4826 | isolate)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4827 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4828 | ENTER_V8(isolate); |
| 4829 | i::Handle<i::Object> result = isolate->factory()->NewNumber(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4830 | return Utils::IntegerToLocal(result); |
| 4831 | } |
| 4832 | |
| 4833 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4834 | Local<Integer> Integer::NewFromUnsigned(uint32_t value) { |
| 4835 | bool fits_into_int32_t = (value & (1 << 31)) == 0; |
| 4836 | if (fits_into_int32_t) { |
| 4837 | return Integer::New(static_cast<int32_t>(value)); |
| 4838 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4839 | i::Isolate* isolate = i::Isolate::Current(); |
| 4840 | ENTER_V8(isolate); |
| 4841 | i::Handle<i::Object> result = isolate->factory()->NewNumber(value); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4842 | return Utils::IntegerToLocal(result); |
| 4843 | } |
| 4844 | |
| 4845 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4846 | void V8::IgnoreOutOfMemoryException() { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4847 | EnterIsolateIfNeeded()->set_ignore_out_of_memory(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4848 | } |
| 4849 | |
| 4850 | |
| 4851 | bool V8::AddMessageListener(MessageCallback that, Handle<Value> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4852 | i::Isolate* isolate = i::Isolate::Current(); |
| 4853 | EnsureInitializedForIsolate(isolate, "v8::V8::AddMessageListener()"); |
| 4854 | ON_BAILOUT(isolate, "v8::V8::AddMessageListener()", return false); |
| 4855 | ENTER_V8(isolate); |
| 4856 | i::HandleScope scope(isolate); |
| 4857 | NeanderArray listeners(isolate->factory()->message_listeners()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4858 | NeanderObject obj(2); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4859 | obj.set(0, *isolate->factory()->NewForeign(FUNCTION_ADDR(that))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4860 | obj.set(1, data.IsEmpty() ? |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4861 | isolate->heap()->undefined_value() : |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4862 | *Utils::OpenHandle(*data)); |
| 4863 | listeners.add(obj.value()); |
| 4864 | return true; |
| 4865 | } |
| 4866 | |
| 4867 | |
| 4868 | void V8::RemoveMessageListeners(MessageCallback that) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4869 | i::Isolate* isolate = i::Isolate::Current(); |
| 4870 | EnsureInitializedForIsolate(isolate, "v8::V8::RemoveMessageListener()"); |
| 4871 | ON_BAILOUT(isolate, "v8::V8::RemoveMessageListeners()", return); |
| 4872 | ENTER_V8(isolate); |
| 4873 | i::HandleScope scope(isolate); |
| 4874 | NeanderArray listeners(isolate->factory()->message_listeners()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4875 | for (int i = 0; i < listeners.length(); i++) { |
| 4876 | if (listeners.get(i)->IsUndefined()) continue; // skip deleted ones |
| 4877 | |
| 4878 | NeanderObject listener(i::JSObject::cast(listeners.get(i))); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4879 | i::Handle<i::Foreign> callback_obj(i::Foreign::cast(listener.get(0))); |
| 4880 | if (callback_obj->address() == FUNCTION_ADDR(that)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4881 | listeners.set(i, isolate->heap()->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4882 | } |
| 4883 | } |
| 4884 | } |
| 4885 | |
| 4886 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 4887 | void V8::SetCaptureStackTraceForUncaughtExceptions( |
| 4888 | bool capture, |
| 4889 | int frame_limit, |
| 4890 | StackTrace::StackTraceOptions options) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4891 | i::Isolate::Current()->SetCaptureStackTraceForUncaughtExceptions( |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 4892 | capture, |
| 4893 | frame_limit, |
| 4894 | options); |
| 4895 | } |
| 4896 | |
| 4897 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4898 | void V8::SetCounterFunction(CounterLookupCallback callback) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4899 | i::Isolate* isolate = EnterIsolateIfNeeded(); |
| 4900 | if (IsDeadCheck(isolate, "v8::V8::SetCounterFunction()")) return; |
| 4901 | isolate->stats_table()->SetCounterFunction(callback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4902 | } |
| 4903 | |
| 4904 | void V8::SetCreateHistogramFunction(CreateHistogramCallback callback) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4905 | i::Isolate* isolate = EnterIsolateIfNeeded(); |
| 4906 | if (IsDeadCheck(isolate, "v8::V8::SetCreateHistogramFunction()")) return; |
| 4907 | isolate->stats_table()->SetCreateHistogramFunction(callback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | void V8::SetAddHistogramSampleFunction(AddHistogramSampleCallback callback) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4911 | i::Isolate* isolate = EnterIsolateIfNeeded(); |
| 4912 | if (IsDeadCheck(isolate, "v8::V8::SetAddHistogramSampleFunction()")) return; |
| 4913 | isolate->stats_table()-> |
| 4914 | SetAddHistogramSampleFunction(callback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4915 | } |
| 4916 | |
| 4917 | void V8::EnableSlidingStateWindow() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4918 | i::Isolate* isolate = i::Isolate::Current(); |
| 4919 | if (IsDeadCheck(isolate, "v8::V8::EnableSlidingStateWindow()")) return; |
| 4920 | isolate->logger()->EnableSlidingStateWindow(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4921 | } |
| 4922 | |
| 4923 | |
| 4924 | void V8::SetFailedAccessCheckCallbackFunction( |
| 4925 | FailedAccessCheckCallback callback) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4926 | i::Isolate* isolate = i::Isolate::Current(); |
| 4927 | if (IsDeadCheck(isolate, "v8::V8::SetFailedAccessCheckCallbackFunction()")) { |
| 4928 | return; |
| 4929 | } |
| 4930 | isolate->SetFailedAccessCheckCallback(callback); |
| 4931 | } |
| 4932 | |
| 4933 | void V8::AddObjectGroup(Persistent<Value>* objects, |
| 4934 | size_t length, |
| 4935 | RetainedObjectInfo* info) { |
| 4936 | i::Isolate* isolate = i::Isolate::Current(); |
| 4937 | if (IsDeadCheck(isolate, "v8::V8::AddObjectGroup()")) return; |
| 4938 | STATIC_ASSERT(sizeof(Persistent<Value>) == sizeof(i::Object**)); |
| 4939 | isolate->global_handles()->AddObjectGroup( |
| 4940 | reinterpret_cast<i::Object***>(objects), length, info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4941 | } |
| 4942 | |
| 4943 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4944 | void V8::AddImplicitReferences(Persistent<Object> parent, |
| 4945 | Persistent<Value>* children, |
| 4946 | size_t length) { |
| 4947 | i::Isolate* isolate = i::Isolate::Current(); |
| 4948 | if (IsDeadCheck(isolate, "v8::V8::AddImplicitReferences()")) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4949 | STATIC_ASSERT(sizeof(Persistent<Value>) == sizeof(i::Object**)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4950 | isolate->global_handles()->AddImplicitReferences( |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4951 | i::Handle<i::HeapObject>::cast(Utils::OpenHandle(*parent)).location(), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4952 | reinterpret_cast<i::Object***>(children), length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4953 | } |
| 4954 | |
| 4955 | |
| 4956 | int V8::AdjustAmountOfExternalAllocatedMemory(int change_in_bytes) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4957 | i::Isolate* isolate = i::Isolate::Current(); |
| 4958 | if (IsDeadCheck(isolate, "v8::V8::AdjustAmountOfExternalAllocatedMemory()")) { |
| 4959 | return 0; |
| 4960 | } |
| 4961 | return isolate->heap()->AdjustAmountOfExternalAllocatedMemory( |
| 4962 | change_in_bytes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | |
| 4966 | void V8::SetGlobalGCPrologueCallback(GCCallback callback) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4967 | i::Isolate* isolate = i::Isolate::Current(); |
| 4968 | if (IsDeadCheck(isolate, "v8::V8::SetGlobalGCPrologueCallback()")) return; |
| 4969 | isolate->heap()->SetGlobalGCPrologueCallback(callback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4970 | } |
| 4971 | |
| 4972 | |
| 4973 | void V8::SetGlobalGCEpilogueCallback(GCCallback callback) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4974 | i::Isolate* isolate = i::Isolate::Current(); |
| 4975 | if (IsDeadCheck(isolate, "v8::V8::SetGlobalGCEpilogueCallback()")) return; |
| 4976 | isolate->heap()->SetGlobalGCEpilogueCallback(callback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4977 | } |
| 4978 | |
| 4979 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4980 | void V8::AddGCPrologueCallback(GCPrologueCallback callback, GCType gc_type) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4981 | i::Isolate* isolate = i::Isolate::Current(); |
| 4982 | if (IsDeadCheck(isolate, "v8::V8::AddGCPrologueCallback()")) return; |
| 4983 | isolate->heap()->AddGCPrologueCallback(callback, gc_type); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4984 | } |
| 4985 | |
| 4986 | |
| 4987 | void V8::RemoveGCPrologueCallback(GCPrologueCallback callback) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4988 | i::Isolate* isolate = i::Isolate::Current(); |
| 4989 | if (IsDeadCheck(isolate, "v8::V8::RemoveGCPrologueCallback()")) return; |
| 4990 | isolate->heap()->RemoveGCPrologueCallback(callback); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4991 | } |
| 4992 | |
| 4993 | |
| 4994 | void V8::AddGCEpilogueCallback(GCEpilogueCallback callback, GCType gc_type) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4995 | i::Isolate* isolate = i::Isolate::Current(); |
| 4996 | if (IsDeadCheck(isolate, "v8::V8::AddGCEpilogueCallback()")) return; |
| 4997 | isolate->heap()->AddGCEpilogueCallback(callback, gc_type); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4998 | } |
| 4999 | |
| 5000 | |
| 5001 | void V8::RemoveGCEpilogueCallback(GCEpilogueCallback callback) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5002 | i::Isolate* isolate = i::Isolate::Current(); |
| 5003 | if (IsDeadCheck(isolate, "v8::V8::RemoveGCEpilogueCallback()")) return; |
| 5004 | isolate->heap()->RemoveGCEpilogueCallback(callback); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5005 | } |
| 5006 | |
| 5007 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 5008 | void V8::AddMemoryAllocationCallback(MemoryAllocationCallback callback, |
| 5009 | ObjectSpace space, |
| 5010 | AllocationAction action) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5011 | i::Isolate* isolate = i::Isolate::Current(); |
| 5012 | if (IsDeadCheck(isolate, "v8::V8::AddMemoryAllocationCallback()")) return; |
| 5013 | isolate->memory_allocator()->AddMemoryAllocationCallback( |
| 5014 | callback, space, action); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 5015 | } |
| 5016 | |
| 5017 | |
| 5018 | void V8::RemoveMemoryAllocationCallback(MemoryAllocationCallback callback) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5019 | i::Isolate* isolate = i::Isolate::Current(); |
| 5020 | if (IsDeadCheck(isolate, "v8::V8::RemoveMemoryAllocationCallback()")) return; |
| 5021 | isolate->memory_allocator()->RemoveMemoryAllocationCallback( |
| 5022 | callback); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 5023 | } |
| 5024 | |
| 5025 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5026 | void V8::PauseProfiler() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5027 | i::Isolate* isolate = i::Isolate::Current(); |
| 5028 | isolate->logger()->PauseProfiler(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5029 | } |
| 5030 | |
| 5031 | |
| 5032 | void V8::ResumeProfiler() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5033 | i::Isolate* isolate = i::Isolate::Current(); |
| 5034 | isolate->logger()->ResumeProfiler(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5035 | } |
| 5036 | |
| 5037 | |
| 5038 | bool V8::IsProfilerPaused() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5039 | i::Isolate* isolate = i::Isolate::Current(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5040 | return isolate->logger()->IsProfilerPaused(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5041 | } |
| 5042 | |
| 5043 | |
| 5044 | int V8::GetCurrentThreadId() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5045 | i::Isolate* isolate = i::Isolate::Current(); |
| 5046 | EnsureInitializedForIsolate(isolate, "V8::GetCurrentThreadId()"); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5047 | return isolate->thread_id().ToInteger(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5048 | } |
| 5049 | |
| 5050 | |
| 5051 | void V8::TerminateExecution(int thread_id) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5052 | i::Isolate* isolate = i::Isolate::Current(); |
| 5053 | if (!isolate->IsInitialized()) return; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5054 | API_ENTRY_CHECK(isolate, "V8::TerminateExecution()"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5055 | // If the thread_id identifies the current thread just terminate |
| 5056 | // execution right away. Otherwise, ask the thread manager to |
| 5057 | // terminate the thread with the given id if any. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5058 | i::ThreadId internal_tid = i::ThreadId::FromInteger(thread_id); |
| 5059 | if (isolate->thread_id().Equals(internal_tid)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5060 | isolate->stack_guard()->TerminateExecution(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5061 | } else { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5062 | isolate->thread_manager()->TerminateExecution(internal_tid); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5063 | } |
| 5064 | } |
| 5065 | |
| 5066 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5067 | void V8::TerminateExecution(Isolate* isolate) { |
| 5068 | // If no isolate is supplied, use the default isolate. |
| 5069 | if (isolate != NULL) { |
| 5070 | reinterpret_cast<i::Isolate*>(isolate)->stack_guard()->TerminateExecution(); |
| 5071 | } else { |
| 5072 | i::Isolate::GetDefaultIsolateStackGuard()->TerminateExecution(); |
| 5073 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5074 | } |
| 5075 | |
| 5076 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5077 | bool V8::IsExecutionTerminating(Isolate* isolate) { |
| 5078 | i::Isolate* i_isolate = isolate != NULL ? |
| 5079 | reinterpret_cast<i::Isolate*>(isolate) : i::Isolate::Current(); |
| 5080 | return IsExecutionTerminatingCheck(i_isolate); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5081 | } |
| 5082 | |
| 5083 | |
| 5084 | Isolate* Isolate::GetCurrent() { |
| 5085 | i::Isolate* isolate = i::Isolate::UncheckedCurrent(); |
| 5086 | return reinterpret_cast<Isolate*>(isolate); |
| 5087 | } |
| 5088 | |
| 5089 | |
| 5090 | Isolate* Isolate::New() { |
| 5091 | i::Isolate* isolate = new i::Isolate(); |
| 5092 | return reinterpret_cast<Isolate*>(isolate); |
| 5093 | } |
| 5094 | |
| 5095 | |
| 5096 | void Isolate::Dispose() { |
| 5097 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 5098 | if (!ApiCheck(!isolate->IsInUse(), |
| 5099 | "v8::Isolate::Dispose()", |
| 5100 | "Disposing the isolate that is entered by a thread.")) { |
| 5101 | return; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5102 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5103 | isolate->TearDown(); |
| 5104 | } |
| 5105 | |
| 5106 | |
| 5107 | void Isolate::Enter() { |
| 5108 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 5109 | isolate->Enter(); |
| 5110 | } |
| 5111 | |
| 5112 | |
| 5113 | void Isolate::Exit() { |
| 5114 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 5115 | isolate->Exit(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5116 | } |
| 5117 | |
| 5118 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5119 | void Isolate::SetData(void* data) { |
| 5120 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 5121 | isolate->SetData(data); |
| 5122 | } |
| 5123 | |
| 5124 | void* Isolate::GetData() { |
| 5125 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 5126 | return isolate->GetData(); |
| 5127 | } |
| 5128 | |
| 5129 | |
| 5130 | String::Utf8Value::Utf8Value(v8::Handle<v8::Value> obj) |
| 5131 | : str_(NULL), length_(0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5132 | i::Isolate* isolate = i::Isolate::Current(); |
| 5133 | if (IsDeadCheck(isolate, "v8::String::Utf8Value::Utf8Value()")) return; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5134 | if (obj.IsEmpty()) return; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5135 | ENTER_V8(isolate); |
| 5136 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5137 | TryCatch try_catch; |
| 5138 | Handle<String> str = obj->ToString(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5139 | if (str.IsEmpty()) return; |
| 5140 | length_ = str->Utf8Length(); |
| 5141 | str_ = i::NewArray<char>(length_ + 1); |
| 5142 | str->WriteUtf8(str_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5143 | } |
| 5144 | |
| 5145 | |
| 5146 | String::Utf8Value::~Utf8Value() { |
| 5147 | i::DeleteArray(str_); |
| 5148 | } |
| 5149 | |
| 5150 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5151 | String::AsciiValue::AsciiValue(v8::Handle<v8::Value> obj) |
| 5152 | : str_(NULL), length_(0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5153 | i::Isolate* isolate = i::Isolate::Current(); |
| 5154 | if (IsDeadCheck(isolate, "v8::String::AsciiValue::AsciiValue()")) return; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5155 | if (obj.IsEmpty()) return; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5156 | ENTER_V8(isolate); |
| 5157 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5158 | TryCatch try_catch; |
| 5159 | Handle<String> str = obj->ToString(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5160 | if (str.IsEmpty()) return; |
| 5161 | length_ = str->Length(); |
| 5162 | str_ = i::NewArray<char>(length_ + 1); |
| 5163 | str->WriteAscii(str_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5164 | } |
| 5165 | |
| 5166 | |
| 5167 | String::AsciiValue::~AsciiValue() { |
| 5168 | i::DeleteArray(str_); |
| 5169 | } |
| 5170 | |
| 5171 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5172 | String::Value::Value(v8::Handle<v8::Value> obj) |
| 5173 | : str_(NULL), length_(0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5174 | i::Isolate* isolate = i::Isolate::Current(); |
| 5175 | if (IsDeadCheck(isolate, "v8::String::Value::Value()")) return; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5176 | if (obj.IsEmpty()) return; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5177 | ENTER_V8(isolate); |
| 5178 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5179 | TryCatch try_catch; |
| 5180 | Handle<String> str = obj->ToString(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5181 | if (str.IsEmpty()) return; |
| 5182 | length_ = str->Length(); |
| 5183 | str_ = i::NewArray<uint16_t>(length_ + 1); |
| 5184 | str->Write(str_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5185 | } |
| 5186 | |
| 5187 | |
| 5188 | String::Value::~Value() { |
| 5189 | i::DeleteArray(str_); |
| 5190 | } |
| 5191 | |
| 5192 | Local<Value> Exception::RangeError(v8::Handle<v8::String> raw_message) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5193 | i::Isolate* isolate = i::Isolate::Current(); |
| 5194 | LOG_API(isolate, "RangeError"); |
| 5195 | ON_BAILOUT(isolate, "v8::Exception::RangeError()", return Local<Value>()); |
| 5196 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5197 | i::Object* error; |
| 5198 | { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5199 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5200 | i::Handle<i::String> message = Utils::OpenHandle(*raw_message); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5201 | i::Handle<i::Object> result = isolate->factory()->NewRangeError(message); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5202 | error = *result; |
| 5203 | } |
| 5204 | i::Handle<i::Object> result(error); |
| 5205 | return Utils::ToLocal(result); |
| 5206 | } |
| 5207 | |
| 5208 | Local<Value> Exception::ReferenceError(v8::Handle<v8::String> raw_message) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5209 | i::Isolate* isolate = i::Isolate::Current(); |
| 5210 | LOG_API(isolate, "ReferenceError"); |
| 5211 | ON_BAILOUT(isolate, "v8::Exception::ReferenceError()", return Local<Value>()); |
| 5212 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5213 | i::Object* error; |
| 5214 | { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5215 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5216 | i::Handle<i::String> message = Utils::OpenHandle(*raw_message); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5217 | i::Handle<i::Object> result = |
| 5218 | isolate->factory()->NewReferenceError(message); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5219 | error = *result; |
| 5220 | } |
| 5221 | i::Handle<i::Object> result(error); |
| 5222 | return Utils::ToLocal(result); |
| 5223 | } |
| 5224 | |
| 5225 | Local<Value> Exception::SyntaxError(v8::Handle<v8::String> raw_message) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5226 | i::Isolate* isolate = i::Isolate::Current(); |
| 5227 | LOG_API(isolate, "SyntaxError"); |
| 5228 | ON_BAILOUT(isolate, "v8::Exception::SyntaxError()", return Local<Value>()); |
| 5229 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5230 | i::Object* error; |
| 5231 | { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5232 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5233 | i::Handle<i::String> message = Utils::OpenHandle(*raw_message); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5234 | i::Handle<i::Object> result = isolate->factory()->NewSyntaxError(message); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5235 | error = *result; |
| 5236 | } |
| 5237 | i::Handle<i::Object> result(error); |
| 5238 | return Utils::ToLocal(result); |
| 5239 | } |
| 5240 | |
| 5241 | Local<Value> Exception::TypeError(v8::Handle<v8::String> raw_message) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5242 | i::Isolate* isolate = i::Isolate::Current(); |
| 5243 | LOG_API(isolate, "TypeError"); |
| 5244 | ON_BAILOUT(isolate, "v8::Exception::TypeError()", return Local<Value>()); |
| 5245 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5246 | i::Object* error; |
| 5247 | { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5248 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5249 | i::Handle<i::String> message = Utils::OpenHandle(*raw_message); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5250 | i::Handle<i::Object> result = isolate->factory()->NewTypeError(message); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5251 | error = *result; |
| 5252 | } |
| 5253 | i::Handle<i::Object> result(error); |
| 5254 | return Utils::ToLocal(result); |
| 5255 | } |
| 5256 | |
| 5257 | Local<Value> Exception::Error(v8::Handle<v8::String> raw_message) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5258 | i::Isolate* isolate = i::Isolate::Current(); |
| 5259 | LOG_API(isolate, "Error"); |
| 5260 | ON_BAILOUT(isolate, "v8::Exception::Error()", return Local<Value>()); |
| 5261 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5262 | i::Object* error; |
| 5263 | { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5264 | i::HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5265 | i::Handle<i::String> message = Utils::OpenHandle(*raw_message); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5266 | i::Handle<i::Object> result = isolate->factory()->NewError(message); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5267 | error = *result; |
| 5268 | } |
| 5269 | i::Handle<i::Object> result(error); |
| 5270 | return Utils::ToLocal(result); |
| 5271 | } |
| 5272 | |
| 5273 | |
| 5274 | // --- D e b u g S u p p o r t --- |
| 5275 | |
| 5276 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5277 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5278 | static void EventCallbackWrapper(const v8::Debug::EventDetails& event_details) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5279 | i::Isolate* isolate = i::Isolate::Current(); |
| 5280 | if (isolate->debug_event_callback() != NULL) { |
| 5281 | isolate->debug_event_callback()(event_details.GetEvent(), |
| 5282 | event_details.GetExecutionState(), |
| 5283 | event_details.GetEventData(), |
| 5284 | event_details.GetCallbackData()); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5285 | } |
| 5286 | } |
| 5287 | |
| 5288 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5289 | bool Debug::SetDebugEventListener(EventCallback that, Handle<Value> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5290 | i::Isolate* isolate = i::Isolate::Current(); |
| 5291 | EnsureInitializedForIsolate(isolate, "v8::Debug::SetDebugEventListener()"); |
| 5292 | ON_BAILOUT(isolate, "v8::Debug::SetDebugEventListener()", return false); |
| 5293 | ENTER_V8(isolate); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5294 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5295 | isolate->set_debug_event_callback(that); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5296 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5297 | i::HandleScope scope(isolate); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5298 | i::Handle<i::Object> foreign = isolate->factory()->undefined_value(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5299 | if (that != NULL) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5300 | foreign = |
| 5301 | isolate->factory()->NewForeign(FUNCTION_ADDR(EventCallbackWrapper)); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5302 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5303 | isolate->debugger()->SetEventListener(foreign, Utils::OpenHandle(*data)); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5304 | return true; |
| 5305 | } |
| 5306 | |
| 5307 | |
| 5308 | bool Debug::SetDebugEventListener2(EventCallback2 that, Handle<Value> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5309 | i::Isolate* isolate = i::Isolate::Current(); |
| 5310 | EnsureInitializedForIsolate(isolate, "v8::Debug::SetDebugEventListener2()"); |
| 5311 | ON_BAILOUT(isolate, "v8::Debug::SetDebugEventListener2()", return false); |
| 5312 | ENTER_V8(isolate); |
| 5313 | i::HandleScope scope(isolate); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5314 | i::Handle<i::Object> foreign = isolate->factory()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5315 | if (that != NULL) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5316 | foreign = isolate->factory()->NewForeign(FUNCTION_ADDR(that)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5317 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5318 | isolate->debugger()->SetEventListener(foreign, Utils::OpenHandle(*data)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5319 | return true; |
| 5320 | } |
| 5321 | |
| 5322 | |
| 5323 | bool Debug::SetDebugEventListener(v8::Handle<v8::Object> that, |
| 5324 | Handle<Value> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5325 | i::Isolate* isolate = i::Isolate::Current(); |
| 5326 | ON_BAILOUT(isolate, "v8::Debug::SetDebugEventListener()", return false); |
| 5327 | ENTER_V8(isolate); |
| 5328 | isolate->debugger()->SetEventListener(Utils::OpenHandle(*that), |
| 5329 | Utils::OpenHandle(*data)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5330 | return true; |
| 5331 | } |
| 5332 | |
| 5333 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5334 | void Debug::DebugBreak(Isolate* isolate) { |
| 5335 | // If no isolate is supplied, use the default isolate. |
| 5336 | if (isolate != NULL) { |
| 5337 | reinterpret_cast<i::Isolate*>(isolate)->stack_guard()->DebugBreak(); |
| 5338 | } else { |
| 5339 | i::Isolate::GetDefaultIsolateStackGuard()->DebugBreak(); |
| 5340 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5341 | } |
| 5342 | |
| 5343 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5344 | void Debug::CancelDebugBreak(Isolate* isolate) { |
| 5345 | // If no isolate is supplied, use the default isolate. |
| 5346 | if (isolate != NULL) { |
| 5347 | i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 5348 | internal_isolate->stack_guard()->Continue(i::DEBUGBREAK); |
| 5349 | } else { |
| 5350 | i::Isolate::GetDefaultIsolateStackGuard()->Continue(i::DEBUGBREAK); |
| 5351 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5352 | } |
| 5353 | |
| 5354 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5355 | void Debug::DebugBreakForCommand(ClientData* data, Isolate* isolate) { |
| 5356 | // If no isolate is supplied, use the default isolate. |
| 5357 | if (isolate != NULL) { |
| 5358 | i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 5359 | internal_isolate->debugger()->EnqueueDebugCommand(data); |
| 5360 | } else { |
| 5361 | i::Isolate::GetDefaultIsolateDebugger()->EnqueueDebugCommand(data); |
| 5362 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 5363 | } |
| 5364 | |
| 5365 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5366 | static void MessageHandlerWrapper(const v8::Debug::Message& message) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5367 | i::Isolate* isolate = i::Isolate::Current(); |
| 5368 | if (isolate->message_handler()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5369 | v8::String::Value json(message.GetJSON()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5370 | (isolate->message_handler())(*json, json.length(), message.GetClientData()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5371 | } |
| 5372 | } |
| 5373 | |
| 5374 | |
| 5375 | void Debug::SetMessageHandler(v8::Debug::MessageHandler handler, |
| 5376 | bool message_handler_thread) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5377 | i::Isolate* isolate = i::Isolate::Current(); |
| 5378 | EnsureInitializedForIsolate(isolate, "v8::Debug::SetMessageHandler"); |
| 5379 | ENTER_V8(isolate); |
| 5380 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5381 | // Message handler thread not supported any more. Parameter temporally left in |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5382 | // the API for client compatibility reasons. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5383 | CHECK(!message_handler_thread); |
| 5384 | |
| 5385 | // TODO(sgjesse) support the old message handler API through a simple wrapper. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5386 | isolate->set_message_handler(handler); |
| 5387 | if (handler != NULL) { |
| 5388 | isolate->debugger()->SetMessageHandler(MessageHandlerWrapper); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5389 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5390 | isolate->debugger()->SetMessageHandler(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5391 | } |
| 5392 | } |
| 5393 | |
| 5394 | |
| 5395 | void Debug::SetMessageHandler2(v8::Debug::MessageHandler2 handler) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5396 | i::Isolate* isolate = i::Isolate::Current(); |
| 5397 | EnsureInitializedForIsolate(isolate, "v8::Debug::SetMessageHandler"); |
| 5398 | ENTER_V8(isolate); |
| 5399 | isolate->debugger()->SetMessageHandler(handler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5400 | } |
| 5401 | |
| 5402 | |
| 5403 | void Debug::SendCommand(const uint16_t* command, int length, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5404 | ClientData* client_data, |
| 5405 | Isolate* isolate) { |
| 5406 | // If no isolate is supplied, use the default isolate. |
| 5407 | if (isolate != NULL) { |
| 5408 | i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 5409 | internal_isolate->debugger()->ProcessCommand( |
| 5410 | i::Vector<const uint16_t>(command, length), client_data); |
| 5411 | } else { |
| 5412 | i::Isolate::GetDefaultIsolateDebugger()->ProcessCommand( |
| 5413 | i::Vector<const uint16_t>(command, length), client_data); |
| 5414 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5415 | } |
| 5416 | |
| 5417 | |
| 5418 | void Debug::SetHostDispatchHandler(HostDispatchHandler handler, |
| 5419 | int period) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5420 | i::Isolate* isolate = i::Isolate::Current(); |
| 5421 | EnsureInitializedForIsolate(isolate, "v8::Debug::SetHostDispatchHandler"); |
| 5422 | ENTER_V8(isolate); |
| 5423 | isolate->debugger()->SetHostDispatchHandler(handler, period); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5424 | } |
| 5425 | |
| 5426 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5427 | void Debug::SetDebugMessageDispatchHandler( |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5428 | DebugMessageDispatchHandler handler, bool provide_locker) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5429 | i::Isolate* isolate = i::Isolate::Current(); |
| 5430 | EnsureInitializedForIsolate(isolate, |
| 5431 | "v8::Debug::SetDebugMessageDispatchHandler"); |
| 5432 | ENTER_V8(isolate); |
| 5433 | isolate->debugger()->SetDebugMessageDispatchHandler( |
| 5434 | handler, provide_locker); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5435 | } |
| 5436 | |
| 5437 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5438 | Local<Value> Debug::Call(v8::Handle<v8::Function> fun, |
| 5439 | v8::Handle<v8::Value> data) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5440 | i::Isolate* isolate = i::Isolate::Current(); |
| 5441 | if (!isolate->IsInitialized()) return Local<Value>(); |
| 5442 | ON_BAILOUT(isolate, "v8::Debug::Call()", return Local<Value>()); |
| 5443 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5444 | i::Handle<i::Object> result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5445 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5446 | if (data.IsEmpty()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5447 | result = isolate->debugger()->Call(Utils::OpenHandle(*fun), |
| 5448 | isolate->factory()->undefined_value(), |
| 5449 | &has_pending_exception); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5450 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5451 | result = isolate->debugger()->Call(Utils::OpenHandle(*fun), |
| 5452 | Utils::OpenHandle(*data), |
| 5453 | &has_pending_exception); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5454 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5455 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5456 | return Utils::ToLocal(result); |
| 5457 | } |
| 5458 | |
| 5459 | |
| 5460 | Local<Value> Debug::GetMirror(v8::Handle<v8::Value> obj) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5461 | i::Isolate* isolate = i::Isolate::Current(); |
| 5462 | if (!isolate->IsInitialized()) return Local<Value>(); |
| 5463 | ON_BAILOUT(isolate, "v8::Debug::GetMirror()", return Local<Value>()); |
| 5464 | ENTER_V8(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5465 | v8::HandleScope scope; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5466 | i::Debug* isolate_debug = isolate->debug(); |
| 5467 | isolate_debug->Load(); |
| 5468 | i::Handle<i::JSObject> debug(isolate_debug->debug_context()->global()); |
| 5469 | i::Handle<i::String> name = |
| 5470 | isolate->factory()->LookupAsciiSymbol("MakeMirror"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5471 | i::Handle<i::Object> fun_obj = i::GetProperty(debug, name); |
| 5472 | i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(fun_obj); |
| 5473 | v8::Handle<v8::Function> v8_fun = Utils::ToLocal(fun); |
| 5474 | const int kArgc = 1; |
| 5475 | v8::Handle<v8::Value> argv[kArgc] = { obj }; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5476 | EXCEPTION_PREAMBLE(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5477 | v8::Handle<v8::Value> result = v8_fun->Call(Utils::ToLocal(debug), |
| 5478 | kArgc, |
| 5479 | argv); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5480 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5481 | return scope.Close(result); |
| 5482 | } |
| 5483 | |
| 5484 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5485 | bool Debug::EnableAgent(const char* name, int port, bool wait_for_connection) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5486 | return i::Isolate::Current()->debugger()->StartAgent(name, port, |
| 5487 | wait_for_connection); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5488 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5489 | |
| 5490 | void Debug::ProcessDebugMessages() { |
| 5491 | i::Execution::ProcessDebugMesssages(true); |
| 5492 | } |
| 5493 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5494 | Local<Context> Debug::GetDebugContext() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5495 | i::Isolate* isolate = i::Isolate::Current(); |
| 5496 | EnsureInitializedForIsolate(isolate, "v8::Debug::GetDebugContext()"); |
| 5497 | ENTER_V8(isolate); |
| 5498 | return Utils::ToLocal(i::Isolate::Current()->debugger()->GetDebugContext()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5499 | } |
| 5500 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5501 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 5502 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5503 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5504 | Handle<String> CpuProfileNode::GetFunctionName() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5505 | i::Isolate* isolate = i::Isolate::Current(); |
| 5506 | IsDeadCheck(isolate, "v8::CpuProfileNode::GetFunctionName"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5507 | const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this); |
| 5508 | const i::CodeEntry* entry = node->entry(); |
| 5509 | if (!entry->has_name_prefix()) { |
| 5510 | return Handle<String>(ToApi<String>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5511 | isolate->factory()->LookupAsciiSymbol(entry->name()))); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5512 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5513 | return Handle<String>(ToApi<String>(isolate->factory()->NewConsString( |
| 5514 | isolate->factory()->LookupAsciiSymbol(entry->name_prefix()), |
| 5515 | isolate->factory()->LookupAsciiSymbol(entry->name())))); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5516 | } |
| 5517 | } |
| 5518 | |
| 5519 | |
| 5520 | Handle<String> CpuProfileNode::GetScriptResourceName() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5521 | i::Isolate* isolate = i::Isolate::Current(); |
| 5522 | IsDeadCheck(isolate, "v8::CpuProfileNode::GetScriptResourceName"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5523 | const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5524 | return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol( |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5525 | node->entry()->resource_name()))); |
| 5526 | } |
| 5527 | |
| 5528 | |
| 5529 | int CpuProfileNode::GetLineNumber() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5530 | i::Isolate* isolate = i::Isolate::Current(); |
| 5531 | IsDeadCheck(isolate, "v8::CpuProfileNode::GetLineNumber"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5532 | return reinterpret_cast<const i::ProfileNode*>(this)->entry()->line_number(); |
| 5533 | } |
| 5534 | |
| 5535 | |
| 5536 | double CpuProfileNode::GetTotalTime() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5537 | i::Isolate* isolate = i::Isolate::Current(); |
| 5538 | IsDeadCheck(isolate, "v8::CpuProfileNode::GetTotalTime"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5539 | return reinterpret_cast<const i::ProfileNode*>(this)->GetTotalMillis(); |
| 5540 | } |
| 5541 | |
| 5542 | |
| 5543 | double CpuProfileNode::GetSelfTime() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5544 | i::Isolate* isolate = i::Isolate::Current(); |
| 5545 | IsDeadCheck(isolate, "v8::CpuProfileNode::GetSelfTime"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5546 | return reinterpret_cast<const i::ProfileNode*>(this)->GetSelfMillis(); |
| 5547 | } |
| 5548 | |
| 5549 | |
| 5550 | double CpuProfileNode::GetTotalSamplesCount() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5551 | i::Isolate* isolate = i::Isolate::Current(); |
| 5552 | IsDeadCheck(isolate, "v8::CpuProfileNode::GetTotalSamplesCount"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5553 | return reinterpret_cast<const i::ProfileNode*>(this)->total_ticks(); |
| 5554 | } |
| 5555 | |
| 5556 | |
| 5557 | double CpuProfileNode::GetSelfSamplesCount() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5558 | i::Isolate* isolate = i::Isolate::Current(); |
| 5559 | IsDeadCheck(isolate, "v8::CpuProfileNode::GetSelfSamplesCount"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5560 | return reinterpret_cast<const i::ProfileNode*>(this)->self_ticks(); |
| 5561 | } |
| 5562 | |
| 5563 | |
| 5564 | unsigned CpuProfileNode::GetCallUid() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5565 | i::Isolate* isolate = i::Isolate::Current(); |
| 5566 | IsDeadCheck(isolate, "v8::CpuProfileNode::GetCallUid"); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 5567 | return reinterpret_cast<const i::ProfileNode*>(this)->entry()->GetCallUid(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5568 | } |
| 5569 | |
| 5570 | |
| 5571 | int CpuProfileNode::GetChildrenCount() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5572 | i::Isolate* isolate = i::Isolate::Current(); |
| 5573 | IsDeadCheck(isolate, "v8::CpuProfileNode::GetChildrenCount"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5574 | return reinterpret_cast<const i::ProfileNode*>(this)->children()->length(); |
| 5575 | } |
| 5576 | |
| 5577 | |
| 5578 | const CpuProfileNode* CpuProfileNode::GetChild(int index) const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5579 | i::Isolate* isolate = i::Isolate::Current(); |
| 5580 | IsDeadCheck(isolate, "v8::CpuProfileNode::GetChild"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5581 | const i::ProfileNode* child = |
| 5582 | reinterpret_cast<const i::ProfileNode*>(this)->children()->at(index); |
| 5583 | return reinterpret_cast<const CpuProfileNode*>(child); |
| 5584 | } |
| 5585 | |
| 5586 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5587 | void CpuProfile::Delete() { |
| 5588 | i::Isolate* isolate = i::Isolate::Current(); |
| 5589 | IsDeadCheck(isolate, "v8::CpuProfile::Delete"); |
| 5590 | i::CpuProfiler::DeleteProfile(reinterpret_cast<i::CpuProfile*>(this)); |
| 5591 | if (i::CpuProfiler::GetProfilesCount() == 0 && |
| 5592 | !i::CpuProfiler::HasDetachedProfiles()) { |
| 5593 | // If this was the last profile, clean up all accessory data as well. |
| 5594 | i::CpuProfiler::DeleteAllProfiles(); |
| 5595 | } |
| 5596 | } |
| 5597 | |
| 5598 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5599 | unsigned CpuProfile::GetUid() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5600 | i::Isolate* isolate = i::Isolate::Current(); |
| 5601 | IsDeadCheck(isolate, "v8::CpuProfile::GetUid"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5602 | return reinterpret_cast<const i::CpuProfile*>(this)->uid(); |
| 5603 | } |
| 5604 | |
| 5605 | |
| 5606 | Handle<String> CpuProfile::GetTitle() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5607 | i::Isolate* isolate = i::Isolate::Current(); |
| 5608 | IsDeadCheck(isolate, "v8::CpuProfile::GetTitle"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5609 | const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5610 | return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol( |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5611 | profile->title()))); |
| 5612 | } |
| 5613 | |
| 5614 | |
| 5615 | const CpuProfileNode* CpuProfile::GetBottomUpRoot() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5616 | i::Isolate* isolate = i::Isolate::Current(); |
| 5617 | IsDeadCheck(isolate, "v8::CpuProfile::GetBottomUpRoot"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5618 | const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this); |
| 5619 | return reinterpret_cast<const CpuProfileNode*>(profile->bottom_up()->root()); |
| 5620 | } |
| 5621 | |
| 5622 | |
| 5623 | const CpuProfileNode* CpuProfile::GetTopDownRoot() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5624 | i::Isolate* isolate = i::Isolate::Current(); |
| 5625 | IsDeadCheck(isolate, "v8::CpuProfile::GetTopDownRoot"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5626 | const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this); |
| 5627 | return reinterpret_cast<const CpuProfileNode*>(profile->top_down()->root()); |
| 5628 | } |
| 5629 | |
| 5630 | |
| 5631 | int CpuProfiler::GetProfilesCount() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5632 | i::Isolate* isolate = i::Isolate::Current(); |
| 5633 | IsDeadCheck(isolate, "v8::CpuProfiler::GetProfilesCount"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5634 | return i::CpuProfiler::GetProfilesCount(); |
| 5635 | } |
| 5636 | |
| 5637 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5638 | const CpuProfile* CpuProfiler::GetProfile(int index, |
| 5639 | Handle<Value> security_token) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5640 | i::Isolate* isolate = i::Isolate::Current(); |
| 5641 | IsDeadCheck(isolate, "v8::CpuProfiler::GetProfile"); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5642 | return reinterpret_cast<const CpuProfile*>( |
| 5643 | i::CpuProfiler::GetProfile( |
| 5644 | security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token), |
| 5645 | index)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5646 | } |
| 5647 | |
| 5648 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5649 | const CpuProfile* CpuProfiler::FindProfile(unsigned uid, |
| 5650 | Handle<Value> security_token) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5651 | i::Isolate* isolate = i::Isolate::Current(); |
| 5652 | IsDeadCheck(isolate, "v8::CpuProfiler::FindProfile"); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5653 | return reinterpret_cast<const CpuProfile*>( |
| 5654 | i::CpuProfiler::FindProfile( |
| 5655 | security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token), |
| 5656 | uid)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5657 | } |
| 5658 | |
| 5659 | |
| 5660 | void CpuProfiler::StartProfiling(Handle<String> title) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5661 | i::Isolate* isolate = i::Isolate::Current(); |
| 5662 | IsDeadCheck(isolate, "v8::CpuProfiler::StartProfiling"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5663 | i::CpuProfiler::StartProfiling(*Utils::OpenHandle(*title)); |
| 5664 | } |
| 5665 | |
| 5666 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5667 | const CpuProfile* CpuProfiler::StopProfiling(Handle<String> title, |
| 5668 | Handle<Value> security_token) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5669 | i::Isolate* isolate = i::Isolate::Current(); |
| 5670 | IsDeadCheck(isolate, "v8::CpuProfiler::StopProfiling"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5671 | return reinterpret_cast<const CpuProfile*>( |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5672 | i::CpuProfiler::StopProfiling( |
| 5673 | security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token), |
| 5674 | *Utils::OpenHandle(*title))); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5675 | } |
| 5676 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5677 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5678 | void CpuProfiler::DeleteAllProfiles() { |
| 5679 | i::Isolate* isolate = i::Isolate::Current(); |
| 5680 | IsDeadCheck(isolate, "v8::CpuProfiler::DeleteAllProfiles"); |
| 5681 | i::CpuProfiler::DeleteAllProfiles(); |
| 5682 | } |
| 5683 | |
| 5684 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5685 | static i::HeapGraphEdge* ToInternal(const HeapGraphEdge* edge) { |
| 5686 | return const_cast<i::HeapGraphEdge*>( |
| 5687 | reinterpret_cast<const i::HeapGraphEdge*>(edge)); |
| 5688 | } |
| 5689 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5690 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5691 | HeapGraphEdge::Type HeapGraphEdge::GetType() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5692 | i::Isolate* isolate = i::Isolate::Current(); |
| 5693 | IsDeadCheck(isolate, "v8::HeapGraphEdge::GetType"); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5694 | return static_cast<HeapGraphEdge::Type>(ToInternal(this)->type()); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5695 | } |
| 5696 | |
| 5697 | |
| 5698 | Handle<Value> HeapGraphEdge::GetName() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5699 | i::Isolate* isolate = i::Isolate::Current(); |
| 5700 | IsDeadCheck(isolate, "v8::HeapGraphEdge::GetName"); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5701 | i::HeapGraphEdge* edge = ToInternal(this); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5702 | switch (edge->type()) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5703 | case i::HeapGraphEdge::kContextVariable: |
| 5704 | case i::HeapGraphEdge::kInternal: |
| 5705 | case i::HeapGraphEdge::kProperty: |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5706 | case i::HeapGraphEdge::kShortcut: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5707 | return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol( |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5708 | edge->name()))); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5709 | case i::HeapGraphEdge::kElement: |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5710 | case i::HeapGraphEdge::kHidden: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5711 | return Handle<Number>(ToApi<Number>(isolate->factory()->NewNumberFromInt( |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5712 | edge->index()))); |
| 5713 | default: UNREACHABLE(); |
| 5714 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5715 | return v8::Undefined(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5716 | } |
| 5717 | |
| 5718 | |
| 5719 | const HeapGraphNode* HeapGraphEdge::GetFromNode() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5720 | i::Isolate* isolate = i::Isolate::Current(); |
| 5721 | IsDeadCheck(isolate, "v8::HeapGraphEdge::GetFromNode"); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5722 | const i::HeapEntry* from = ToInternal(this)->From(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5723 | return reinterpret_cast<const HeapGraphNode*>(from); |
| 5724 | } |
| 5725 | |
| 5726 | |
| 5727 | const HeapGraphNode* HeapGraphEdge::GetToNode() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5728 | i::Isolate* isolate = i::Isolate::Current(); |
| 5729 | IsDeadCheck(isolate, "v8::HeapGraphEdge::GetToNode"); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5730 | const i::HeapEntry* to = ToInternal(this)->to(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5731 | return reinterpret_cast<const HeapGraphNode*>(to); |
| 5732 | } |
| 5733 | |
| 5734 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5735 | static i::HeapEntry* ToInternal(const HeapGraphNode* entry) { |
| 5736 | return const_cast<i::HeapEntry*>( |
| 5737 | reinterpret_cast<const i::HeapEntry*>(entry)); |
| 5738 | } |
| 5739 | |
| 5740 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5741 | HeapGraphNode::Type HeapGraphNode::GetType() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5742 | i::Isolate* isolate = i::Isolate::Current(); |
| 5743 | IsDeadCheck(isolate, "v8::HeapGraphNode::GetType"); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5744 | return static_cast<HeapGraphNode::Type>(ToInternal(this)->type()); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5745 | } |
| 5746 | |
| 5747 | |
| 5748 | Handle<String> HeapGraphNode::GetName() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5749 | i::Isolate* isolate = i::Isolate::Current(); |
| 5750 | IsDeadCheck(isolate, "v8::HeapGraphNode::GetName"); |
| 5751 | return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol( |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5752 | ToInternal(this)->name()))); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5753 | } |
| 5754 | |
| 5755 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 5756 | uint64_t HeapGraphNode::GetId() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5757 | i::Isolate* isolate = i::Isolate::Current(); |
| 5758 | IsDeadCheck(isolate, "v8::HeapGraphNode::GetId"); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5759 | return ToInternal(this)->id(); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 5760 | } |
| 5761 | |
| 5762 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5763 | int HeapGraphNode::GetSelfSize() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5764 | i::Isolate* isolate = i::Isolate::Current(); |
| 5765 | IsDeadCheck(isolate, "v8::HeapGraphNode::GetSelfSize"); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5766 | return ToInternal(this)->self_size(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5767 | } |
| 5768 | |
| 5769 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5770 | int HeapGraphNode::GetRetainedSize(bool exact) const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5771 | i::Isolate* isolate = i::Isolate::Current(); |
| 5772 | IsDeadCheck(isolate, "v8::HeapSnapshot::GetRetainedSize"); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5773 | return ToInternal(this)->RetainedSize(exact); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5774 | } |
| 5775 | |
| 5776 | |
| 5777 | int HeapGraphNode::GetChildrenCount() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5778 | i::Isolate* isolate = i::Isolate::Current(); |
| 5779 | IsDeadCheck(isolate, "v8::HeapSnapshot::GetChildrenCount"); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5780 | return ToInternal(this)->children().length(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5781 | } |
| 5782 | |
| 5783 | |
| 5784 | const HeapGraphEdge* HeapGraphNode::GetChild(int index) const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5785 | i::Isolate* isolate = i::Isolate::Current(); |
| 5786 | IsDeadCheck(isolate, "v8::HeapSnapshot::GetChild"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5787 | return reinterpret_cast<const HeapGraphEdge*>( |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5788 | &ToInternal(this)->children()[index]); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5789 | } |
| 5790 | |
| 5791 | |
| 5792 | int HeapGraphNode::GetRetainersCount() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5793 | i::Isolate* isolate = i::Isolate::Current(); |
| 5794 | IsDeadCheck(isolate, "v8::HeapSnapshot::GetRetainersCount"); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5795 | return ToInternal(this)->retainers().length(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5796 | } |
| 5797 | |
| 5798 | |
| 5799 | const HeapGraphEdge* HeapGraphNode::GetRetainer(int index) const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5800 | i::Isolate* isolate = i::Isolate::Current(); |
| 5801 | IsDeadCheck(isolate, "v8::HeapSnapshot::GetRetainer"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5802 | return reinterpret_cast<const HeapGraphEdge*>( |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5803 | ToInternal(this)->retainers()[index]); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5804 | } |
| 5805 | |
| 5806 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5807 | const HeapGraphNode* HeapGraphNode::GetDominatorNode() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5808 | i::Isolate* isolate = i::Isolate::Current(); |
| 5809 | IsDeadCheck(isolate, "v8::HeapSnapshot::GetDominatorNode"); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5810 | return reinterpret_cast<const HeapGraphNode*>(ToInternal(this)->dominator()); |
| 5811 | } |
| 5812 | |
| 5813 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5814 | v8::Handle<v8::Value> HeapGraphNode::GetHeapValue() const { |
| 5815 | i::Isolate* isolate = i::Isolate::Current(); |
| 5816 | IsDeadCheck(isolate, "v8::HeapGraphNode::GetHeapValue"); |
| 5817 | i::Handle<i::HeapObject> object = ToInternal(this)->GetHeapObject(); |
| 5818 | return v8::Handle<Value>(!object.is_null() ? |
| 5819 | ToApi<Value>(object) : ToApi<Value>( |
| 5820 | isolate->factory()->undefined_value())); |
| 5821 | } |
| 5822 | |
| 5823 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5824 | static i::HeapSnapshot* ToInternal(const HeapSnapshot* snapshot) { |
| 5825 | return const_cast<i::HeapSnapshot*>( |
| 5826 | reinterpret_cast<const i::HeapSnapshot*>(snapshot)); |
| 5827 | } |
| 5828 | |
| 5829 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5830 | void HeapSnapshot::Delete() { |
| 5831 | i::Isolate* isolate = i::Isolate::Current(); |
| 5832 | IsDeadCheck(isolate, "v8::HeapSnapshot::Delete"); |
| 5833 | if (i::HeapProfiler::GetSnapshotsCount() > 1) { |
| 5834 | ToInternal(this)->Delete(); |
| 5835 | } else { |
| 5836 | // If this is the last snapshot, clean up all accessory data as well. |
| 5837 | i::HeapProfiler::DeleteAllSnapshots(); |
| 5838 | } |
| 5839 | } |
| 5840 | |
| 5841 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 5842 | HeapSnapshot::Type HeapSnapshot::GetType() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5843 | i::Isolate* isolate = i::Isolate::Current(); |
| 5844 | IsDeadCheck(isolate, "v8::HeapSnapshot::GetType"); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 5845 | return static_cast<HeapSnapshot::Type>(ToInternal(this)->type()); |
| 5846 | } |
| 5847 | |
| 5848 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5849 | unsigned HeapSnapshot::GetUid() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5850 | i::Isolate* isolate = i::Isolate::Current(); |
| 5851 | IsDeadCheck(isolate, "v8::HeapSnapshot::GetUid"); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5852 | return ToInternal(this)->uid(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5853 | } |
| 5854 | |
| 5855 | |
| 5856 | Handle<String> HeapSnapshot::GetTitle() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5857 | i::Isolate* isolate = i::Isolate::Current(); |
| 5858 | IsDeadCheck(isolate, "v8::HeapSnapshot::GetTitle"); |
| 5859 | return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol( |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5860 | ToInternal(this)->title()))); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5861 | } |
| 5862 | |
| 5863 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 5864 | const HeapGraphNode* HeapSnapshot::GetRoot() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5865 | i::Isolate* isolate = i::Isolate::Current(); |
| 5866 | IsDeadCheck(isolate, "v8::HeapSnapshot::GetHead"); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5867 | return reinterpret_cast<const HeapGraphNode*>(ToInternal(this)->root()); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5868 | } |
| 5869 | |
| 5870 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5871 | const HeapGraphNode* HeapSnapshot::GetNodeById(uint64_t id) const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5872 | i::Isolate* isolate = i::Isolate::Current(); |
| 5873 | IsDeadCheck(isolate, "v8::HeapSnapshot::GetNodeById"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5874 | return reinterpret_cast<const HeapGraphNode*>( |
| 5875 | ToInternal(this)->GetEntryById(id)); |
| 5876 | } |
| 5877 | |
| 5878 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5879 | int HeapSnapshot::GetNodesCount() const { |
| 5880 | i::Isolate* isolate = i::Isolate::Current(); |
| 5881 | IsDeadCheck(isolate, "v8::HeapSnapshot::GetNodesCount"); |
| 5882 | return ToInternal(this)->entries()->length(); |
| 5883 | } |
| 5884 | |
| 5885 | |
| 5886 | const HeapGraphNode* HeapSnapshot::GetNode(int index) const { |
| 5887 | i::Isolate* isolate = i::Isolate::Current(); |
| 5888 | IsDeadCheck(isolate, "v8::HeapSnapshot::GetNode"); |
| 5889 | return reinterpret_cast<const HeapGraphNode*>( |
| 5890 | ToInternal(this)->entries()->at(index)); |
| 5891 | } |
| 5892 | |
| 5893 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 5894 | void HeapSnapshot::Serialize(OutputStream* stream, |
| 5895 | HeapSnapshot::SerializationFormat format) const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5896 | i::Isolate* isolate = i::Isolate::Current(); |
| 5897 | IsDeadCheck(isolate, "v8::HeapSnapshot::Serialize"); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 5898 | ApiCheck(format == kJSON, |
| 5899 | "v8::HeapSnapshot::Serialize", |
| 5900 | "Unknown serialization format"); |
| 5901 | ApiCheck(stream->GetOutputEncoding() == OutputStream::kAscii, |
| 5902 | "v8::HeapSnapshot::Serialize", |
| 5903 | "Unsupported output encoding"); |
| 5904 | ApiCheck(stream->GetChunkSize() > 0, |
| 5905 | "v8::HeapSnapshot::Serialize", |
| 5906 | "Invalid stream chunk size"); |
| 5907 | i::HeapSnapshotJSONSerializer serializer(ToInternal(this)); |
| 5908 | serializer.Serialize(stream); |
| 5909 | } |
| 5910 | |
| 5911 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5912 | int HeapProfiler::GetSnapshotsCount() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5913 | i::Isolate* isolate = i::Isolate::Current(); |
| 5914 | IsDeadCheck(isolate, "v8::HeapProfiler::GetSnapshotsCount"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5915 | return i::HeapProfiler::GetSnapshotsCount(); |
| 5916 | } |
| 5917 | |
| 5918 | |
| 5919 | const HeapSnapshot* HeapProfiler::GetSnapshot(int index) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5920 | i::Isolate* isolate = i::Isolate::Current(); |
| 5921 | IsDeadCheck(isolate, "v8::HeapProfiler::GetSnapshot"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5922 | return reinterpret_cast<const HeapSnapshot*>( |
| 5923 | i::HeapProfiler::GetSnapshot(index)); |
| 5924 | } |
| 5925 | |
| 5926 | |
| 5927 | const HeapSnapshot* HeapProfiler::FindSnapshot(unsigned uid) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5928 | i::Isolate* isolate = i::Isolate::Current(); |
| 5929 | IsDeadCheck(isolate, "v8::HeapProfiler::FindSnapshot"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5930 | return reinterpret_cast<const HeapSnapshot*>( |
| 5931 | i::HeapProfiler::FindSnapshot(uid)); |
| 5932 | } |
| 5933 | |
| 5934 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 5935 | const HeapSnapshot* HeapProfiler::TakeSnapshot(Handle<String> title, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5936 | HeapSnapshot::Type type, |
| 5937 | ActivityControl* control) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5938 | i::Isolate* isolate = i::Isolate::Current(); |
| 5939 | IsDeadCheck(isolate, "v8::HeapProfiler::TakeSnapshot"); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 5940 | i::HeapSnapshot::Type internal_type = i::HeapSnapshot::kFull; |
| 5941 | switch (type) { |
| 5942 | case HeapSnapshot::kFull: |
| 5943 | internal_type = i::HeapSnapshot::kFull; |
| 5944 | break; |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 5945 | default: |
| 5946 | UNREACHABLE(); |
| 5947 | } |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5948 | return reinterpret_cast<const HeapSnapshot*>( |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5949 | i::HeapProfiler::TakeSnapshot( |
| 5950 | *Utils::OpenHandle(*title), internal_type, control)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 5951 | } |
| 5952 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5953 | |
| 5954 | void HeapProfiler::DeleteAllSnapshots() { |
| 5955 | i::Isolate* isolate = i::Isolate::Current(); |
| 5956 | IsDeadCheck(isolate, "v8::HeapProfiler::DeleteAllSnapshots"); |
| 5957 | i::HeapProfiler::DeleteAllSnapshots(); |
| 5958 | } |
| 5959 | |
| 5960 | |
| 5961 | void HeapProfiler::DefineWrapperClass(uint16_t class_id, |
| 5962 | WrapperInfoCallback callback) { |
| 5963 | i::Isolate::Current()->heap_profiler()->DefineWrapperClass(class_id, |
| 5964 | callback); |
| 5965 | } |
| 5966 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5967 | |
| 5968 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5969 | v8::Testing::StressType internal::Testing::stress_type_ = |
| 5970 | v8::Testing::kStressTypeOpt; |
| 5971 | |
| 5972 | |
| 5973 | void Testing::SetStressRunType(Testing::StressType type) { |
| 5974 | internal::Testing::set_stress_type(type); |
| 5975 | } |
| 5976 | |
| 5977 | int Testing::GetStressRuns() { |
| 5978 | if (internal::FLAG_stress_runs != 0) return internal::FLAG_stress_runs; |
| 5979 | #ifdef DEBUG |
| 5980 | // In debug mode the code runs much slower so stressing will only make two |
| 5981 | // runs. |
| 5982 | return 2; |
| 5983 | #else |
| 5984 | return 5; |
| 5985 | #endif |
| 5986 | } |
| 5987 | |
| 5988 | |
| 5989 | static void SetFlagsFromString(const char* flags) { |
| 5990 | V8::SetFlagsFromString(flags, i::StrLength(flags)); |
| 5991 | } |
| 5992 | |
| 5993 | |
| 5994 | void Testing::PrepareStressRun(int run) { |
| 5995 | static const char* kLazyOptimizations = |
| 5996 | "--prepare-always-opt --nolimit-inlining " |
| 5997 | "--noalways-opt --noopt-eagerly"; |
| 5998 | static const char* kEagerOptimizations = "--opt-eagerly"; |
| 5999 | static const char* kForcedOptimizations = "--always-opt"; |
| 6000 | |
| 6001 | // If deoptimization stressed turn on frequent deoptimization. If no value |
| 6002 | // is spefified through --deopt-every-n-times use a default default value. |
| 6003 | static const char* kDeoptEvery13Times = "--deopt-every-n-times=13"; |
| 6004 | if (internal::Testing::stress_type() == Testing::kStressTypeDeopt && |
| 6005 | internal::FLAG_deopt_every_n_times == 0) { |
| 6006 | SetFlagsFromString(kDeoptEvery13Times); |
| 6007 | } |
| 6008 | |
| 6009 | #ifdef DEBUG |
| 6010 | // As stressing in debug mode only make two runs skip the deopt stressing |
| 6011 | // here. |
| 6012 | if (run == GetStressRuns() - 1) { |
| 6013 | SetFlagsFromString(kForcedOptimizations); |
| 6014 | } else { |
| 6015 | SetFlagsFromString(kEagerOptimizations); |
| 6016 | SetFlagsFromString(kLazyOptimizations); |
| 6017 | } |
| 6018 | #else |
| 6019 | if (run == GetStressRuns() - 1) { |
| 6020 | SetFlagsFromString(kForcedOptimizations); |
| 6021 | } else if (run == GetStressRuns() - 2) { |
| 6022 | SetFlagsFromString(kEagerOptimizations); |
| 6023 | } else { |
| 6024 | SetFlagsFromString(kLazyOptimizations); |
| 6025 | } |
| 6026 | #endif |
| 6027 | } |
| 6028 | |
| 6029 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6030 | void Testing::DeoptimizeAll() { |
| 6031 | internal::Deoptimizer::DeoptimizeAll(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6032 | } |
| 6033 | |
| 6034 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6035 | namespace internal { |
| 6036 | |
| 6037 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6038 | void HandleScopeImplementer::FreeThreadResources() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6039 | Free(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6040 | } |
| 6041 | |
| 6042 | |
| 6043 | char* HandleScopeImplementer::ArchiveThread(char* storage) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6044 | v8::ImplementationUtilities::HandleScopeData* current = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6045 | isolate_->handle_scope_data(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6046 | handle_scope_data_ = *current; |
| 6047 | memcpy(storage, this, sizeof(*this)); |
| 6048 | |
| 6049 | ResetAfterArchive(); |
| 6050 | current->Initialize(); |
| 6051 | |
| 6052 | return storage + ArchiveSpacePerThread(); |
| 6053 | } |
| 6054 | |
| 6055 | |
| 6056 | int HandleScopeImplementer::ArchiveSpacePerThread() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6057 | return sizeof(HandleScopeImplementer); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6058 | } |
| 6059 | |
| 6060 | |
| 6061 | char* HandleScopeImplementer::RestoreThread(char* storage) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6062 | memcpy(this, storage, sizeof(*this)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6063 | *isolate_->handle_scope_data() = handle_scope_data_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6064 | return storage + ArchiveSpacePerThread(); |
| 6065 | } |
| 6066 | |
| 6067 | |
| 6068 | void HandleScopeImplementer::IterateThis(ObjectVisitor* v) { |
| 6069 | // Iterate over all handles in the blocks except for the last. |
| 6070 | for (int i = blocks()->length() - 2; i >= 0; --i) { |
| 6071 | Object** block = blocks()->at(i); |
| 6072 | v->VisitPointers(block, &block[kHandleBlockSize]); |
| 6073 | } |
| 6074 | |
| 6075 | // Iterate over live handles in the last block (if any). |
| 6076 | if (!blocks()->is_empty()) { |
| 6077 | v->VisitPointers(blocks()->last(), handle_scope_data_.next); |
| 6078 | } |
| 6079 | |
| 6080 | if (!saved_contexts_.is_empty()) { |
| 6081 | Object** start = reinterpret_cast<Object**>(&saved_contexts_.first()); |
| 6082 | v->VisitPointers(start, start + saved_contexts_.length()); |
| 6083 | } |
| 6084 | } |
| 6085 | |
| 6086 | |
| 6087 | void HandleScopeImplementer::Iterate(ObjectVisitor* v) { |
| 6088 | v8::ImplementationUtilities::HandleScopeData* current = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6089 | isolate_->handle_scope_data(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6090 | handle_scope_data_ = *current; |
| 6091 | IterateThis(v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6092 | } |
| 6093 | |
| 6094 | |
| 6095 | char* HandleScopeImplementer::Iterate(ObjectVisitor* v, char* storage) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6096 | HandleScopeImplementer* scope_implementer = |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6097 | reinterpret_cast<HandleScopeImplementer*>(storage); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6098 | scope_implementer->IterateThis(v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6099 | return storage + ArchiveSpacePerThread(); |
| 6100 | } |
| 6101 | |
| 6102 | } } // namespace v8::internal |