erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 28 | #include "api.h" |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 29 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 30 | #include <string.h> // For memcpy, strlen. |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 31 | #include <cmath> // For isnan. |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 32 | #include "../include/v8-debug.h" |
| 33 | #include "../include/v8-profiler.h" |
| 34 | #include "../include/v8-testing.h" |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 35 | #include "assert-scope.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | #include "bootstrapper.h" |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 37 | #include "code-stubs.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | #include "compiler.h" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 39 | #include "conversions-inl.h" |
| 40 | #include "counters.h" |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 41 | #include "cpu-profiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | #include "debug.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 43 | #include "deoptimizer.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | #include "execution.h" |
| 45 | #include "global-handles.h" |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 46 | #include "heap-profiler.h" |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 47 | #include "heap-snapshot-generator-inl.h" |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 48 | #include "icu_util.h" |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 49 | #include "json-parser.h" |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 50 | #include "messages.h" |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 51 | #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 52 | #include "natives.h" |
| 53 | #endif |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 54 | #include "parser.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 55 | #include "platform.h" |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 56 | #include "platform/time.h" |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 57 | #include "profile-generator-inl.h" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 58 | #include "property-details.h" |
| 59 | #include "property.h" |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 60 | #include "runtime.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 61 | #include "runtime-profiler.h" |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 62 | #include "scanner-character-streams.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 63 | #include "snapshot.h" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 64 | #include "unicode-inl.h" |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 65 | #include "utils/random-number-generator.h" |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 66 | #include "v8threads.h" |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 67 | #include "version.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 68 | #include "vm-state-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 69 | |
| 70 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 71 | #define LOG_API(isolate, expr) LOG(isolate, ApiEntryCall(expr)) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 72 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 73 | #define ENTER_V8(isolate) \ |
| 74 | ASSERT((isolate)->IsInitialized()); \ |
| 75 | i::VMState<i::OTHER> __state__((isolate)) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 76 | |
| 77 | namespace v8 { |
| 78 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 79 | #define ON_BAILOUT(isolate, location, code) \ |
machenbach@chromium.org | ae16103 | 2013-09-24 09:12:30 +0000 | [diff] [blame] | 80 | if (IsExecutionTerminatingCheck(isolate)) { \ |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 81 | code; \ |
| 82 | UNREACHABLE(); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 86 | #define EXCEPTION_PREAMBLE(isolate) \ |
| 87 | (isolate)->handle_scope_implementer()->IncrementCallDepth(); \ |
| 88 | ASSERT(!(isolate)->external_caught_exception()); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 89 | bool has_pending_exception = false |
| 90 | |
| 91 | |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 92 | #define EXCEPTION_BAILOUT_CHECK_GENERIC(isolate, value, do_callback) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 93 | do { \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 94 | i::HandleScopeImplementer* handle_scope_implementer = \ |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 95 | (isolate)->handle_scope_implementer(); \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 96 | handle_scope_implementer->DecrementCallDepth(); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 97 | if (has_pending_exception) { \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 98 | if (handle_scope_implementer->CallDepthIsZero() && \ |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 99 | (isolate)->is_out_of_memory()) { \ |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 100 | if (!(isolate)->ignore_out_of_memory()) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 101 | i::V8::FatalProcessOutOfMemory(NULL); \ |
| 102 | } \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 103 | bool call_depth_is_zero = handle_scope_implementer->CallDepthIsZero(); \ |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 104 | (isolate)->OptionalRescheduleException(call_depth_is_zero); \ |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 105 | do_callback \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 106 | return value; \ |
| 107 | } \ |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 108 | do_callback \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 109 | } while (false) |
| 110 | |
| 111 | |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 112 | #define EXCEPTION_BAILOUT_CHECK_DO_CALLBACK(isolate, value) \ |
| 113 | EXCEPTION_BAILOUT_CHECK_GENERIC( \ |
| 114 | isolate, value, i::V8::FireCallCompletedCallback(isolate);) |
| 115 | |
| 116 | |
| 117 | #define EXCEPTION_BAILOUT_CHECK(isolate, value) \ |
| 118 | EXCEPTION_BAILOUT_CHECK_GENERIC(isolate, value, ;) |
| 119 | |
| 120 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 121 | #define API_ENTRY_CHECK(isolate, msg) \ |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 122 | do { \ |
| 123 | if (v8::Locker::IsActive()) { \ |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 124 | ApiCheck(isolate->thread_manager()->IsLockedByCurrentThread(), \ |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 125 | msg, \ |
| 126 | "Entering the V8 API without proper locking in place"); \ |
| 127 | } \ |
| 128 | } while (false) |
| 129 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 130 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 131 | // --- E x c e p t i o n B e h a v i o r --- |
| 132 | |
| 133 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 134 | static void DefaultFatalErrorHandler(const char* location, |
| 135 | const char* message) { |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 136 | i::Isolate* isolate = i::Isolate::Current(); |
| 137 | if (isolate->IsInitialized()) { |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 138 | i::VMState<i::OTHER> state(isolate); |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 139 | API_Fatal(location, message); |
| 140 | } else { |
| 141 | API_Fatal(location, message); |
| 142 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 146 | static FatalErrorCallback GetFatalErrorHandler() { |
| 147 | i::Isolate* isolate = i::Isolate::Current(); |
| 148 | if (isolate->exception_behavior() == NULL) { |
| 149 | isolate->set_exception_behavior(DefaultFatalErrorHandler); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 150 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 151 | return isolate->exception_behavior(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 155 | void i::FatalProcessOutOfMemory(const char* location) { |
| 156 | i::V8::FatalProcessOutOfMemory(location, false); |
| 157 | } |
| 158 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 159 | |
| 160 | // When V8 cannot allocated memory FatalProcessOutOfMemory is called. |
| 161 | // The default fatal error handler is called and execution is stopped. |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 162 | void i::V8::FatalProcessOutOfMemory(const char* location, bool take_snapshot) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 163 | i::HeapStats heap_stats; |
| 164 | int start_marker; |
| 165 | heap_stats.start_marker = &start_marker; |
| 166 | int new_space_size; |
| 167 | heap_stats.new_space_size = &new_space_size; |
| 168 | int new_space_capacity; |
| 169 | heap_stats.new_space_capacity = &new_space_capacity; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 170 | intptr_t old_pointer_space_size; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 171 | heap_stats.old_pointer_space_size = &old_pointer_space_size; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 172 | intptr_t old_pointer_space_capacity; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 173 | heap_stats.old_pointer_space_capacity = &old_pointer_space_capacity; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 174 | intptr_t old_data_space_size; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 175 | heap_stats.old_data_space_size = &old_data_space_size; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 176 | intptr_t old_data_space_capacity; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 177 | heap_stats.old_data_space_capacity = &old_data_space_capacity; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 178 | intptr_t code_space_size; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 179 | heap_stats.code_space_size = &code_space_size; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 180 | intptr_t code_space_capacity; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 181 | heap_stats.code_space_capacity = &code_space_capacity; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 182 | intptr_t map_space_size; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 183 | heap_stats.map_space_size = &map_space_size; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 184 | intptr_t map_space_capacity; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 185 | heap_stats.map_space_capacity = &map_space_capacity; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 186 | intptr_t cell_space_size; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 187 | heap_stats.cell_space_size = &cell_space_size; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 188 | intptr_t cell_space_capacity; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 189 | heap_stats.cell_space_capacity = &cell_space_capacity; |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 190 | intptr_t property_cell_space_size; |
| 191 | heap_stats.property_cell_space_size = &property_cell_space_size; |
| 192 | intptr_t property_cell_space_capacity; |
| 193 | heap_stats.property_cell_space_capacity = &property_cell_space_capacity; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 194 | intptr_t lo_space_size; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 195 | heap_stats.lo_space_size = &lo_space_size; |
| 196 | int global_handle_count; |
| 197 | heap_stats.global_handle_count = &global_handle_count; |
| 198 | int weak_global_handle_count; |
| 199 | heap_stats.weak_global_handle_count = &weak_global_handle_count; |
| 200 | int pending_global_handle_count; |
| 201 | heap_stats.pending_global_handle_count = &pending_global_handle_count; |
| 202 | int near_death_global_handle_count; |
| 203 | heap_stats.near_death_global_handle_count = &near_death_global_handle_count; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 204 | int free_global_handle_count; |
| 205 | heap_stats.free_global_handle_count = &free_global_handle_count; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 206 | intptr_t memory_allocator_size; |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 207 | heap_stats.memory_allocator_size = &memory_allocator_size; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 208 | intptr_t memory_allocator_capacity; |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 209 | heap_stats.memory_allocator_capacity = &memory_allocator_capacity; |
| 210 | int objects_per_type[LAST_TYPE + 1] = {0}; |
| 211 | heap_stats.objects_per_type = objects_per_type; |
| 212 | int size_per_type[LAST_TYPE + 1] = {0}; |
| 213 | heap_stats.size_per_type = size_per_type; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 214 | int os_error; |
| 215 | heap_stats.os_error = &os_error; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 216 | int end_marker; |
| 217 | heap_stats.end_marker = &end_marker; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 218 | i::Isolate* isolate = i::Isolate::Current(); |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 219 | if (isolate->heap()->HasBeenSetUp()) { |
| 220 | // BUG(1718): Don't use the take_snapshot since we don't support |
| 221 | // HeapIterator here without doing a special GC. |
| 222 | isolate->heap()->RecordStats(&heap_stats, false); |
| 223 | } |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 224 | isolate->SignalFatalError(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 225 | FatalErrorCallback callback = GetFatalErrorHandler(); |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 226 | const char* message = "Allocation failed - process out of memory"; |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 227 | callback(location, message); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 228 | // If the callback returns, we stop execution. |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 229 | FATAL("API fatal error handler returned after process out of memory"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 233 | bool Utils::ReportApiFailure(const char* location, const char* message) { |
| 234 | FatalErrorCallback callback = GetFatalErrorHandler(); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 235 | callback(location, message); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 236 | i::Isolate* isolate = i::Isolate::Current(); |
| 237 | isolate->SignalFatalError(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 238 | return false; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | bool V8::IsDead() { |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 243 | i::Isolate* isolate = i::Isolate::Current(); |
| 244 | return isolate->IsDead(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | |
| 248 | static inline bool ApiCheck(bool condition, |
| 249 | const char* location, |
| 250 | const char* message) { |
| 251 | return condition ? true : Utils::ReportApiFailure(location, message); |
| 252 | } |
| 253 | |
| 254 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 255 | static bool ReportEmptyHandle(const char* location) { |
| 256 | FatalErrorCallback callback = GetFatalErrorHandler(); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 257 | callback(location, "Reading from empty handle"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 258 | return true; |
| 259 | } |
| 260 | |
| 261 | |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 262 | static inline bool IsExecutionTerminatingCheck(i::Isolate* isolate) { |
| 263 | if (!isolate->IsInitialized()) return false; |
| 264 | if (isolate->has_scheduled_exception()) { |
| 265 | return isolate->scheduled_exception() == |
| 266 | isolate->heap()->termination_exception(); |
| 267 | } |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 272 | static inline bool EmptyCheck(const char* location, v8::Handle<v8::Data> obj) { |
| 273 | return obj.IsEmpty() ? ReportEmptyHandle(location) : false; |
| 274 | } |
| 275 | |
| 276 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 277 | static inline bool EmptyCheck(const char* location, const v8::Data* obj) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 278 | return (obj == 0) ? ReportEmptyHandle(location) : false; |
| 279 | } |
| 280 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 281 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 282 | // --- S t a t i c s --- |
| 283 | |
| 284 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 285 | static bool InitializeHelper(i::Isolate* isolate) { |
| 286 | // If the isolate has a function entry hook, it needs to re-build all its |
| 287 | // code stubs with entry hooks embedded, so let's deserialize a snapshot. |
| 288 | if (isolate == NULL || isolate->function_entry_hook() == NULL) { |
| 289 | if (i::Snapshot::Initialize()) |
| 290 | return true; |
| 291 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 292 | return i::V8::Initialize(NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 296 | static inline bool EnsureInitializedForIsolate(i::Isolate* isolate, |
| 297 | const char* location) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 298 | if (isolate != NULL) { |
| 299 | if (isolate->IsInitialized()) return true; |
| 300 | } |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 301 | ASSERT(isolate == i::Isolate::Current()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 302 | return ApiCheck(InitializeHelper(isolate), location, "Error initializing V8"); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 303 | } |
| 304 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 305 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 306 | // Some initializing API functions are called early and may be |
| 307 | // called on a thread different from static initializer thread. |
| 308 | // If Isolate API is used, Isolate::Enter() will initialize TLS so |
| 309 | // Isolate::Current() works. If it's a legacy case, then the thread |
| 310 | // may not have TLS initialized yet. However, in initializing APIs it |
| 311 | // may be too early to call EnsureInitialized() - some pre-init |
| 312 | // parameters still have to be configured. |
| 313 | static inline i::Isolate* EnterIsolateIfNeeded() { |
| 314 | i::Isolate* isolate = i::Isolate::UncheckedCurrent(); |
| 315 | if (isolate != NULL) |
| 316 | return isolate; |
| 317 | |
| 318 | i::Isolate::EnterDefaultIsolate(); |
| 319 | isolate = i::Isolate::Current(); |
| 320 | return isolate; |
| 321 | } |
| 322 | |
| 323 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 324 | StartupDataDecompressor::StartupDataDecompressor() |
| 325 | : raw_data(i::NewArray<char*>(V8::GetCompressedStartupDataCount())) { |
| 326 | for (int i = 0; i < V8::GetCompressedStartupDataCount(); ++i) { |
| 327 | raw_data[i] = NULL; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | |
| 332 | StartupDataDecompressor::~StartupDataDecompressor() { |
| 333 | for (int i = 0; i < V8::GetCompressedStartupDataCount(); ++i) { |
| 334 | i::DeleteArray(raw_data[i]); |
| 335 | } |
| 336 | i::DeleteArray(raw_data); |
| 337 | } |
| 338 | |
| 339 | |
| 340 | int StartupDataDecompressor::Decompress() { |
| 341 | int compressed_data_count = V8::GetCompressedStartupDataCount(); |
| 342 | StartupData* compressed_data = |
| 343 | i::NewArray<StartupData>(compressed_data_count); |
| 344 | V8::GetCompressedStartupData(compressed_data); |
| 345 | for (int i = 0; i < compressed_data_count; ++i) { |
| 346 | char* decompressed = raw_data[i] = |
| 347 | i::NewArray<char>(compressed_data[i].raw_size); |
| 348 | if (compressed_data[i].compressed_size != 0) { |
| 349 | int result = DecompressData(decompressed, |
| 350 | &compressed_data[i].raw_size, |
| 351 | compressed_data[i].data, |
| 352 | compressed_data[i].compressed_size); |
| 353 | if (result != 0) return result; |
| 354 | } else { |
| 355 | ASSERT_EQ(0, compressed_data[i].raw_size); |
| 356 | } |
| 357 | compressed_data[i].data = decompressed; |
| 358 | } |
| 359 | V8::SetDecompressedStartupData(compressed_data); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 360 | i::DeleteArray(compressed_data); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 365 | StartupData::CompressionAlgorithm V8::GetCompressedStartupDataAlgorithm() { |
| 366 | #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 367 | return StartupData::kBZip2; |
| 368 | #else |
| 369 | return StartupData::kUncompressed; |
| 370 | #endif |
| 371 | } |
| 372 | |
| 373 | |
| 374 | enum CompressedStartupDataItems { |
| 375 | kSnapshot = 0, |
| 376 | kSnapshotContext, |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 377 | kLibraries, |
| 378 | kExperimentalLibraries, |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 379 | kCompressedStartupDataCount |
| 380 | }; |
| 381 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 382 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 383 | int V8::GetCompressedStartupDataCount() { |
| 384 | #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 385 | return kCompressedStartupDataCount; |
| 386 | #else |
| 387 | return 0; |
| 388 | #endif |
| 389 | } |
| 390 | |
| 391 | |
| 392 | void V8::GetCompressedStartupData(StartupData* compressed_data) { |
| 393 | #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 394 | compressed_data[kSnapshot].data = |
| 395 | reinterpret_cast<const char*>(i::Snapshot::data()); |
| 396 | compressed_data[kSnapshot].compressed_size = i::Snapshot::size(); |
| 397 | compressed_data[kSnapshot].raw_size = i::Snapshot::raw_size(); |
| 398 | |
| 399 | compressed_data[kSnapshotContext].data = |
| 400 | reinterpret_cast<const char*>(i::Snapshot::context_data()); |
| 401 | compressed_data[kSnapshotContext].compressed_size = |
| 402 | i::Snapshot::context_size(); |
| 403 | compressed_data[kSnapshotContext].raw_size = i::Snapshot::context_raw_size(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 404 | |
| 405 | i::Vector<const i::byte> libraries_source = i::Natives::GetScriptsSource(); |
| 406 | compressed_data[kLibraries].data = |
| 407 | reinterpret_cast<const char*>(libraries_source.start()); |
| 408 | compressed_data[kLibraries].compressed_size = libraries_source.length(); |
| 409 | compressed_data[kLibraries].raw_size = i::Natives::GetRawScriptsSize(); |
| 410 | |
| 411 | i::Vector<const i::byte> exp_libraries_source = |
| 412 | i::ExperimentalNatives::GetScriptsSource(); |
| 413 | compressed_data[kExperimentalLibraries].data = |
| 414 | reinterpret_cast<const char*>(exp_libraries_source.start()); |
| 415 | compressed_data[kExperimentalLibraries].compressed_size = |
| 416 | exp_libraries_source.length(); |
| 417 | compressed_data[kExperimentalLibraries].raw_size = |
| 418 | i::ExperimentalNatives::GetRawScriptsSize(); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 419 | #endif |
| 420 | } |
| 421 | |
| 422 | |
| 423 | void V8::SetDecompressedStartupData(StartupData* decompressed_data) { |
| 424 | #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 425 | ASSERT_EQ(i::Snapshot::raw_size(), decompressed_data[kSnapshot].raw_size); |
| 426 | i::Snapshot::set_raw_data( |
| 427 | reinterpret_cast<const i::byte*>(decompressed_data[kSnapshot].data)); |
| 428 | |
| 429 | ASSERT_EQ(i::Snapshot::context_raw_size(), |
| 430 | decompressed_data[kSnapshotContext].raw_size); |
| 431 | i::Snapshot::set_context_raw_data( |
| 432 | reinterpret_cast<const i::byte*>( |
| 433 | decompressed_data[kSnapshotContext].data)); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 434 | |
| 435 | ASSERT_EQ(i::Natives::GetRawScriptsSize(), |
| 436 | decompressed_data[kLibraries].raw_size); |
| 437 | i::Vector<const char> libraries_source( |
| 438 | decompressed_data[kLibraries].data, |
| 439 | decompressed_data[kLibraries].raw_size); |
| 440 | i::Natives::SetRawScriptsSource(libraries_source); |
| 441 | |
| 442 | ASSERT_EQ(i::ExperimentalNatives::GetRawScriptsSize(), |
| 443 | decompressed_data[kExperimentalLibraries].raw_size); |
| 444 | i::Vector<const char> exp_libraries_source( |
| 445 | decompressed_data[kExperimentalLibraries].data, |
| 446 | decompressed_data[kExperimentalLibraries].raw_size); |
| 447 | i::ExperimentalNatives::SetRawScriptsSource(exp_libraries_source); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 448 | #endif |
| 449 | } |
| 450 | |
| 451 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 452 | void V8::SetFatalErrorHandler(FatalErrorCallback that) { |
| 453 | i::Isolate* isolate = EnterIsolateIfNeeded(); |
| 454 | isolate->set_exception_behavior(that); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 458 | void V8::SetAllowCodeGenerationFromStringsCallback( |
| 459 | AllowCodeGenerationFromStringsCallback callback) { |
| 460 | i::Isolate* isolate = EnterIsolateIfNeeded(); |
| 461 | isolate->set_allow_code_gen_callback(callback); |
| 462 | } |
| 463 | |
| 464 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 465 | void V8::SetFlagsFromString(const char* str, int length) { |
| 466 | i::FlagList::SetFlagsFromString(str, length); |
| 467 | } |
| 468 | |
| 469 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 470 | void V8::SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags) { |
| 471 | i::FlagList::SetFlagsFromCommandLine(argc, argv, remove_flags); |
| 472 | } |
| 473 | |
| 474 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 475 | v8::Handle<Value> ThrowException(v8::Handle<v8::Value> value) { |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 476 | return v8::Isolate::GetCurrent()->ThrowException(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | |
| 480 | RegisteredExtension* RegisteredExtension::first_extension_ = NULL; |
| 481 | |
| 482 | |
| 483 | RegisteredExtension::RegisteredExtension(Extension* extension) |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 484 | : extension_(extension) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 485 | |
| 486 | |
| 487 | void RegisteredExtension::Register(RegisteredExtension* that) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 488 | that->next_ = first_extension_; |
| 489 | first_extension_ = that; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 493 | void RegisteredExtension::UnregisterAll() { |
| 494 | RegisteredExtension* re = first_extension_; |
| 495 | while (re != NULL) { |
| 496 | RegisteredExtension* next = re->next(); |
| 497 | delete re; |
| 498 | re = next; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 503 | void RegisterExtension(Extension* that) { |
| 504 | RegisteredExtension* extension = new RegisteredExtension(that); |
| 505 | RegisteredExtension::Register(extension); |
| 506 | } |
| 507 | |
| 508 | |
| 509 | Extension::Extension(const char* name, |
| 510 | const char* source, |
| 511 | int dep_count, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 512 | const char** deps, |
| 513 | int source_length) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 514 | : name_(name), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 515 | source_length_(source_length >= 0 ? |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 516 | source_length : |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 517 | (source ? static_cast<int>(strlen(source)) : 0)), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 518 | source_(source, source_length_), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 519 | dep_count_(dep_count), |
| 520 | deps_(deps), |
danno@chromium.org | 412fa51 | 2012-09-14 13:28:26 +0000 | [diff] [blame] | 521 | auto_enable_(false) { |
| 522 | CHECK(source != NULL || source_length_ == 0); |
| 523 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 524 | |
| 525 | |
| 526 | v8::Handle<Primitive> Undefined() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 527 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 528 | if (!EnsureInitializedForIsolate(isolate, "v8::Undefined()")) { |
| 529 | return v8::Handle<v8::Primitive>(); |
| 530 | } |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 531 | return ToApiHandle<Primitive>(isolate->factory()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | |
| 535 | v8::Handle<Primitive> Null() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 536 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 537 | if (!EnsureInitializedForIsolate(isolate, "v8::Null()")) { |
| 538 | return v8::Handle<v8::Primitive>(); |
| 539 | } |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 540 | return ToApiHandle<Primitive>(isolate->factory()->null_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | |
| 544 | v8::Handle<Boolean> True() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 545 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 546 | if (!EnsureInitializedForIsolate(isolate, "v8::True()")) { |
| 547 | return v8::Handle<Boolean>(); |
| 548 | } |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 549 | return ToApiHandle<Boolean>(isolate->factory()->true_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | |
| 553 | v8::Handle<Boolean> False() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 554 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 555 | if (!EnsureInitializedForIsolate(isolate, "v8::False()")) { |
| 556 | return v8::Handle<Boolean>(); |
| 557 | } |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 558 | return ToApiHandle<Boolean>(isolate->factory()->false_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | |
| 562 | ResourceConstraints::ResourceConstraints() |
| 563 | : max_young_space_size_(0), |
| 564 | max_old_space_size_(0), |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 565 | max_executable_size_(0), |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 566 | stack_limit_(NULL) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 567 | |
| 568 | |
| 569 | bool SetResourceConstraints(ResourceConstraints* constraints) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 570 | i::Isolate* isolate = EnterIsolateIfNeeded(); |
| 571 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 572 | int young_space_size = constraints->max_young_space_size(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 573 | int old_gen_size = constraints->max_old_space_size(); |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 574 | int max_executable_size = constraints->max_executable_size(); |
| 575 | if (young_space_size != 0 || old_gen_size != 0 || max_executable_size != 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 576 | // After initialization it's too late to change Heap constraints. |
| 577 | ASSERT(!isolate->IsInitialized()); |
| 578 | bool result = isolate->heap()->ConfigureHeap(young_space_size / 2, |
| 579 | old_gen_size, |
| 580 | max_executable_size); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 581 | if (!result) return false; |
| 582 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 583 | if (constraints->stack_limit() != NULL) { |
| 584 | uintptr_t limit = reinterpret_cast<uintptr_t>(constraints->stack_limit()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 585 | isolate->stack_guard()->SetStackLimit(limit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 586 | } |
| 587 | return true; |
| 588 | } |
| 589 | |
| 590 | |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 591 | i::Object** V8::GlobalizeReference(i::Isolate* isolate, i::Object** obj) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 592 | LOG_API(isolate, "Persistent::New"); |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 593 | i::Handle<i::Object> result = isolate->global_handles()->Create(*obj); |
mstarzinger@chromium.org | b228be0 | 2013-04-18 14:56:59 +0000 | [diff] [blame] | 594 | #ifdef DEBUG |
| 595 | (*obj)->Verify(); |
| 596 | #endif // DEBUG |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 597 | return result.location(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 601 | i::Object** V8::CopyPersistent(i::Object** obj) { |
| 602 | i::Handle<i::Object> result = i::GlobalHandles::CopyGlobal(obj); |
| 603 | #ifdef DEBUG |
| 604 | (*obj)->Verify(); |
| 605 | #endif // DEBUG |
| 606 | return result.location(); |
| 607 | } |
| 608 | |
| 609 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 610 | void V8::MakeWeak(i::Object** object, |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 611 | void* parameters, |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 612 | WeakCallback weak_callback, |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 613 | RevivableCallback weak_reference_callback) { |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 614 | i::GlobalHandles::MakeWeak(object, |
| 615 | parameters, |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 616 | weak_callback, |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 617 | weak_reference_callback); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 621 | void V8::ClearWeak(i::Object** obj) { |
| 622 | i::GlobalHandles::ClearWeakness(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 626 | void V8::DisposeGlobal(i::Object** obj) { |
| 627 | i::GlobalHandles::Destroy(obj); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 628 | } |
| 629 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 630 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 631 | void V8::Eternalize(Isolate* v8_isolate, Value* value, int* index) { |
| 632 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); |
| 633 | i::Object* object = *Utils::OpenHandle(value); |
| 634 | isolate->eternal_handles()->Create(isolate, object, index); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 638 | Local<Value> V8::GetEternal(Isolate* v8_isolate, int index) { |
| 639 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); |
| 640 | return Utils::ToLocal(isolate->eternal_handles()->Get(index)); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 644 | // --- H a n d l e s --- |
| 645 | |
| 646 | |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 647 | HandleScope::HandleScope(Isolate* isolate) { |
| 648 | Initialize(isolate); |
| 649 | } |
| 650 | |
| 651 | |
| 652 | void HandleScope::Initialize(Isolate* isolate) { |
| 653 | i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 654 | API_ENTRY_CHECK(internal_isolate, "HandleScope::HandleScope"); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 655 | v8::ImplementationUtilities::HandleScopeData* current = |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 656 | internal_isolate->handle_scope_data(); |
| 657 | isolate_ = internal_isolate; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 658 | prev_next_ = current->next; |
| 659 | prev_limit_ = current->limit; |
| 660 | is_closed_ = false; |
| 661 | current->level++; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | |
| 665 | HandleScope::~HandleScope() { |
| 666 | if (!is_closed_) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 667 | Leave(); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 668 | } |
| 669 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 670 | |
| 671 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 672 | void HandleScope::Leave() { |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 673 | return i::HandleScope::CloseScope(isolate_, prev_next_, prev_limit_); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 677 | int HandleScope::NumberOfHandles() { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 678 | i::Isolate* isolate = i::Isolate::Current(); |
| 679 | if (!EnsureInitializedForIsolate(isolate, "HandleScope::NumberOfHandles")) { |
| 680 | return 0; |
| 681 | } |
| 682 | return i::HandleScope::NumberOfHandles(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 686 | i::Object** HandleScope::CreateHandle(i::Isolate* isolate, i::Object* value) { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 687 | return i::HandleScope::CreateHandle(isolate, value); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 691 | i::Object** HandleScope::CreateHandle(i::HeapObject* heap_object, |
| 692 | i::Object* value) { |
| 693 | ASSERT(heap_object->IsHeapObject()); |
| 694 | return i::HandleScope::CreateHandle(heap_object->GetIsolate(), value); |
| 695 | } |
| 696 | |
| 697 | |
| 698 | EscapableHandleScope::EscapableHandleScope(Isolate* v8_isolate) { |
| 699 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); |
| 700 | escape_slot_ = CreateHandle(isolate, isolate->heap()->the_hole_value()); |
| 701 | Initialize(v8_isolate); |
| 702 | } |
| 703 | |
| 704 | |
| 705 | i::Object** EscapableHandleScope::Escape(i::Object** escape_value) { |
| 706 | ApiCheck(*escape_slot_ == isolate_->heap()->the_hole_value(), |
| 707 | "EscapeableHandleScope::Escape", |
| 708 | "Escape value set twice"); |
| 709 | if (escape_value == NULL) { |
| 710 | *escape_slot_ = isolate_->heap()->undefined_value(); |
| 711 | return NULL; |
| 712 | } |
| 713 | *escape_slot_ = *escape_value; |
| 714 | return escape_slot_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | |
| 718 | void Context::Enter() { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 719 | i::Handle<i::Context> env = Utils::OpenHandle(this); |
| 720 | i::Isolate* isolate = env->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 721 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 722 | isolate->handle_scope_implementer()->EnterContext(env); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 723 | isolate->handle_scope_implementer()->SaveContext(isolate->context()); |
| 724 | isolate->set_context(*env); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | |
| 728 | void Context::Exit() { |
machenbach@chromium.org | ae16103 | 2013-09-24 09:12:30 +0000 | [diff] [blame] | 729 | // TODO(dcarney): fix this once chrome is fixed. |
| 730 | i::Isolate* isolate = i::Isolate::Current(); |
| 731 | i::Handle<i::Context> context = i::Handle<i::Context>::null(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 732 | ENTER_V8(isolate); |
| 733 | if (!ApiCheck(isolate->handle_scope_implementer()->LeaveContext(context), |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 734 | "v8::Context::Exit()", |
| 735 | "Cannot exit non-entered context")) { |
| 736 | return; |
| 737 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 738 | // Content of 'last_context' could be NULL. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 739 | i::Context* last_context = |
| 740 | isolate->handle_scope_implementer()->RestoreContext(); |
| 741 | isolate->set_context(last_context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 745 | static void* DecodeSmiToAligned(i::Object* value, const char* location) { |
| 746 | ApiCheck(value->IsSmi(), location, "Not a Smi"); |
| 747 | return reinterpret_cast<void*>(value); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | |
mstarzinger@chromium.org | 0c977e0 | 2013-07-26 10:45:47 +0000 | [diff] [blame] | 751 | static i::Smi* EncodeAlignedAsSmi(void* value, const char* location) { |
| 752 | i::Smi* smi = reinterpret_cast<i::Smi*>(value); |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 753 | ApiCheck(smi->IsSmi(), location, "Pointer is not aligned"); |
| 754 | return smi; |
| 755 | } |
| 756 | |
| 757 | |
| 758 | static i::Handle<i::FixedArray> EmbedderDataFor(Context* context, |
| 759 | int index, |
| 760 | bool can_grow, |
| 761 | const char* location) { |
| 762 | i::Handle<i::Context> env = Utils::OpenHandle(context); |
machenbach@chromium.org | ae16103 | 2013-09-24 09:12:30 +0000 | [diff] [blame] | 763 | bool ok = |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 764 | ApiCheck(env->IsNativeContext(), location, "Not a native context") && |
| 765 | ApiCheck(index >= 0, location, "Negative index"); |
| 766 | if (!ok) return i::Handle<i::FixedArray>(); |
| 767 | i::Handle<i::FixedArray> data(env->embedder_data()); |
| 768 | if (index < data->length()) return data; |
| 769 | if (!can_grow) { |
| 770 | Utils::ReportApiFailure(location, "Index too large"); |
| 771 | return i::Handle<i::FixedArray>(); |
svenpanne@chromium.org | fc2a4f4 | 2012-11-07 09:25:55 +0000 | [diff] [blame] | 772 | } |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 773 | int new_size = i::Max(index, data->length() << 1) + 1; |
| 774 | data = env->GetIsolate()->factory()->CopySizeFixedArray(data, new_size); |
| 775 | env->set_embedder_data(*data); |
| 776 | return data; |
| 777 | } |
| 778 | |
| 779 | |
| 780 | v8::Local<v8::Value> Context::SlowGetEmbedderData(int index) { |
| 781 | const char* location = "v8::Context::GetEmbedderData()"; |
| 782 | i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, false, location); |
| 783 | if (data.is_null()) return Local<Value>(); |
| 784 | i::Handle<i::Object> result(data->get(index), data->GetIsolate()); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 785 | return Utils::ToLocal(result); |
| 786 | } |
| 787 | |
| 788 | |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 789 | void Context::SetEmbedderData(int index, v8::Handle<Value> value) { |
| 790 | const char* location = "v8::Context::SetEmbedderData()"; |
| 791 | i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, true, location); |
| 792 | if (data.is_null()) return; |
| 793 | i::Handle<i::Object> val = Utils::OpenHandle(*value); |
| 794 | data->set(index, *val); |
| 795 | ASSERT_EQ(*Utils::OpenHandle(*value), |
| 796 | *Utils::OpenHandle(*GetEmbedderData(index))); |
| 797 | } |
| 798 | |
| 799 | |
| 800 | void* Context::SlowGetAlignedPointerFromEmbedderData(int index) { |
| 801 | const char* location = "v8::Context::GetAlignedPointerFromEmbedderData()"; |
| 802 | i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, false, location); |
| 803 | if (data.is_null()) return NULL; |
| 804 | return DecodeSmiToAligned(data->get(index), location); |
| 805 | } |
| 806 | |
| 807 | |
| 808 | void Context::SetAlignedPointerInEmbedderData(int index, void* value) { |
| 809 | const char* location = "v8::Context::SetAlignedPointerInEmbedderData()"; |
| 810 | i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, true, location); |
| 811 | data->set(index, EncodeAlignedAsSmi(value, location)); |
| 812 | ASSERT_EQ(value, GetAlignedPointerFromEmbedderData(index)); |
| 813 | } |
| 814 | |
| 815 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 816 | i::Object** v8::HandleScope::RawClose(i::Object** value) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 817 | if (!ApiCheck(!is_closed_, |
| 818 | "v8::HandleScope::Close()", |
| 819 | "Local scope has already been closed")) { |
| 820 | return 0; |
| 821 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 822 | LOG_API(isolate_, "CloseHandleScope"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 823 | |
| 824 | // Read the result before popping the handle block. |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 825 | i::Object* result = NULL; |
| 826 | if (value != NULL) { |
| 827 | result = *value; |
| 828 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 829 | is_closed_ = true; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 830 | Leave(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 831 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 832 | if (value == NULL) { |
| 833 | return NULL; |
| 834 | } |
| 835 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 836 | // Allocate a new handle on the previous handle block. |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 837 | i::Handle<i::Object> handle(result, isolate_); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 838 | return handle.location(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | |
| 842 | // --- N e a n d e r --- |
| 843 | |
| 844 | |
| 845 | // A constructor cannot easily return an error value, therefore it is necessary |
| 846 | // to check for a dead VM with ON_BAILOUT before constructing any Neander |
| 847 | // objects. To remind you about this there is no HandleScope in the |
| 848 | // NeanderObject constructor. When you add one to the site calling the |
| 849 | // constructor you should check that you ensured the VM was not dead first. |
| 850 | NeanderObject::NeanderObject(int size) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 851 | i::Isolate* isolate = i::Isolate::Current(); |
| 852 | EnsureInitializedForIsolate(isolate, "v8::Nowhere"); |
| 853 | ENTER_V8(isolate); |
| 854 | value_ = isolate->factory()->NewNeanderObject(); |
| 855 | i::Handle<i::FixedArray> elements = isolate->factory()->NewFixedArray(size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 856 | value_->set_elements(*elements); |
| 857 | } |
| 858 | |
| 859 | |
| 860 | int NeanderObject::size() { |
| 861 | return i::FixedArray::cast(value_->elements())->length(); |
| 862 | } |
| 863 | |
| 864 | |
| 865 | NeanderArray::NeanderArray() : obj_(2) { |
| 866 | obj_.set(0, i::Smi::FromInt(0)); |
| 867 | } |
| 868 | |
| 869 | |
| 870 | int NeanderArray::length() { |
| 871 | return i::Smi::cast(obj_.get(0))->value(); |
| 872 | } |
| 873 | |
| 874 | |
| 875 | i::Object* NeanderArray::get(int offset) { |
| 876 | ASSERT(0 <= offset); |
| 877 | ASSERT(offset < length()); |
| 878 | return obj_.get(offset + 1); |
| 879 | } |
| 880 | |
| 881 | |
| 882 | // This method cannot easily return an error value, therefore it is necessary |
| 883 | // to check for a dead VM with ON_BAILOUT before calling it. To remind you |
| 884 | // about this there is no HandleScope in this method. When you add one to the |
| 885 | // site calling this method you should check that you ensured the VM was not |
| 886 | // dead first. |
| 887 | void NeanderArray::add(i::Handle<i::Object> value) { |
| 888 | int length = this->length(); |
| 889 | int size = obj_.size(); |
| 890 | if (length == size - 1) { |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 891 | i::Factory* factory = i::Isolate::Current()->factory(); |
| 892 | i::Handle<i::FixedArray> new_elms = factory->NewFixedArray(2 * size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 893 | for (int i = 0; i < length; i++) |
| 894 | new_elms->set(i + 1, get(i)); |
| 895 | obj_.value()->set_elements(*new_elms); |
| 896 | } |
| 897 | obj_.set(length + 1, *value); |
| 898 | obj_.set(0, i::Smi::FromInt(length + 1)); |
| 899 | } |
| 900 | |
| 901 | |
| 902 | void NeanderArray::set(int index, i::Object* value) { |
| 903 | if (index < 0 || index >= this->length()) return; |
| 904 | obj_.set(index + 1, value); |
| 905 | } |
| 906 | |
| 907 | |
| 908 | // --- T e m p l a t e --- |
| 909 | |
| 910 | |
| 911 | static void InitializeTemplate(i::Handle<i::TemplateInfo> that, int type) { |
| 912 | that->set_tag(i::Smi::FromInt(type)); |
| 913 | } |
| 914 | |
| 915 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 916 | static void TemplateSet(i::Isolate* isolate, |
| 917 | v8::Template* templ, |
| 918 | int length, |
| 919 | v8::Handle<v8::Data>* data) { |
| 920 | i::Handle<i::Object> list(Utils::OpenHandle(templ)->property_list(), isolate); |
| 921 | if (list->IsUndefined()) { |
| 922 | list = NeanderArray().value(); |
| 923 | Utils::OpenHandle(templ)->set_property_list(*list); |
| 924 | } |
| 925 | NeanderArray array(list); |
| 926 | array.add(Utils::OpenHandle(*v8::Integer::New(length))); |
| 927 | for (int i = 0; i < length; i++) { |
| 928 | i::Handle<i::Object> value = data[i].IsEmpty() ? |
| 929 | i::Handle<i::Object>(isolate->factory()->undefined_value()) : |
| 930 | Utils::OpenHandle(*data[i]); |
| 931 | array.add(value); |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | |
| 936 | void Template::Set(v8::Handle<String> name, |
| 937 | v8::Handle<Data> value, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 938 | v8::PropertyAttribute attribute) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 939 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 940 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 941 | i::HandleScope scope(isolate); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 942 | const int kSize = 3; |
| 943 | v8::Handle<v8::Data> data[kSize] = { |
| 944 | name, |
| 945 | value, |
| 946 | v8::Integer::New(attribute)}; |
| 947 | TemplateSet(isolate, this, kSize, data); |
| 948 | } |
| 949 | |
| 950 | |
| 951 | void Template::SetAccessorProperty( |
| 952 | v8::Local<v8::String> name, |
| 953 | v8::Local<FunctionTemplate> getter, |
| 954 | v8::Local<FunctionTemplate> setter, |
| 955 | v8::PropertyAttribute attribute, |
| 956 | v8::AccessControl access_control) { |
| 957 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 958 | ENTER_V8(isolate); |
| 959 | ASSERT(!name.IsEmpty()); |
| 960 | ASSERT(!getter.IsEmpty() || !setter.IsEmpty()); |
| 961 | i::HandleScope scope(isolate); |
| 962 | const int kSize = 5; |
| 963 | v8::Handle<v8::Data> data[kSize] = { |
| 964 | name, |
| 965 | getter, |
| 966 | setter, |
| 967 | v8::Integer::New(attribute), |
| 968 | v8::Integer::New(access_control)}; |
| 969 | TemplateSet(isolate, this, kSize, data); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | |
| 973 | // --- F u n c t i o n T e m p l a t e --- |
| 974 | static void InitializeFunctionTemplate( |
| 975 | i::Handle<i::FunctionTemplateInfo> info) { |
| 976 | info->set_tag(i::Smi::FromInt(Consts::FUNCTION_TEMPLATE)); |
| 977 | info->set_flag(0); |
| 978 | } |
| 979 | |
| 980 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 981 | Local<ObjectTemplate> FunctionTemplate::PrototypeTemplate() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 982 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 983 | ENTER_V8(isolate); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 984 | i::Handle<i::Object> result(Utils::OpenHandle(this)->prototype_template(), |
| 985 | isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 986 | if (result->IsUndefined()) { |
| 987 | result = Utils::OpenHandle(*ObjectTemplate::New()); |
| 988 | Utils::OpenHandle(this)->set_prototype_template(*result); |
| 989 | } |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 990 | return ToApiHandle<ObjectTemplate>(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | |
| 994 | void FunctionTemplate::Inherit(v8::Handle<FunctionTemplate> value) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 995 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 996 | ENTER_V8(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 997 | Utils::OpenHandle(this)->set_parent_template(*Utils::OpenHandle(*value)); |
| 998 | } |
| 999 | |
| 1000 | |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 1001 | static Local<FunctionTemplate> FunctionTemplateNew( |
| 1002 | i::Isolate* isolate, |
| 1003 | FunctionCallback callback, |
| 1004 | v8::Handle<Value> data, |
| 1005 | v8::Handle<Signature> signature, |
| 1006 | int length, |
| 1007 | bool do_not_cache) { |
| 1008 | i::Handle<i::Struct> struct_obj = |
| 1009 | isolate->factory()->NewStruct(i::FUNCTION_TEMPLATE_INFO_TYPE); |
| 1010 | i::Handle<i::FunctionTemplateInfo> obj = |
| 1011 | i::Handle<i::FunctionTemplateInfo>::cast(struct_obj); |
| 1012 | InitializeFunctionTemplate(obj); |
| 1013 | obj->set_do_not_cache(do_not_cache); |
| 1014 | int next_serial_number = 0; |
| 1015 | if (!do_not_cache) { |
| 1016 | next_serial_number = isolate->next_serial_number() + 1; |
| 1017 | isolate->set_next_serial_number(next_serial_number); |
| 1018 | } |
| 1019 | obj->set_serial_number(i::Smi::FromInt(next_serial_number)); |
| 1020 | if (callback != 0) { |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1021 | if (data.IsEmpty()) { |
| 1022 | data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate)); |
| 1023 | } |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 1024 | Utils::ToLocal(obj)->SetCallHandler(callback, data); |
| 1025 | } |
| 1026 | obj->set_length(length); |
| 1027 | obj->set_undetectable(false); |
| 1028 | obj->set_needs_access_check(false); |
| 1029 | if (!signature.IsEmpty()) |
| 1030 | obj->set_signature(*Utils::OpenHandle(*signature)); |
| 1031 | return Utils::ToLocal(obj); |
| 1032 | } |
| 1033 | |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1034 | Local<FunctionTemplate> FunctionTemplate::New( |
| 1035 | FunctionCallback callback, |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1036 | v8::Handle<Value> data, |
| 1037 | v8::Handle<Signature> signature, |
| 1038 | int length) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1039 | i::Isolate* isolate = i::Isolate::Current(); |
| 1040 | EnsureInitializedForIsolate(isolate, "v8::FunctionTemplate::New()"); |
| 1041 | LOG_API(isolate, "FunctionTemplate::New"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1042 | ENTER_V8(isolate); |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 1043 | return FunctionTemplateNew( |
| 1044 | isolate, callback, data, signature, length, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | |
| 1048 | Local<Signature> Signature::New(Handle<FunctionTemplate> receiver, |
| 1049 | int argc, Handle<FunctionTemplate> argv[]) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1050 | i::Isolate* isolate = i::Isolate::Current(); |
| 1051 | EnsureInitializedForIsolate(isolate, "v8::Signature::New()"); |
| 1052 | LOG_API(isolate, "Signature::New"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1053 | ENTER_V8(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1054 | i::Handle<i::Struct> struct_obj = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1055 | isolate->factory()->NewStruct(i::SIGNATURE_INFO_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1056 | i::Handle<i::SignatureInfo> obj = |
| 1057 | i::Handle<i::SignatureInfo>::cast(struct_obj); |
| 1058 | if (!receiver.IsEmpty()) obj->set_receiver(*Utils::OpenHandle(*receiver)); |
| 1059 | if (argc > 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1060 | i::Handle<i::FixedArray> args = isolate->factory()->NewFixedArray(argc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1061 | for (int i = 0; i < argc; i++) { |
| 1062 | if (!argv[i].IsEmpty()) |
| 1063 | args->set(i, *Utils::OpenHandle(*argv[i])); |
| 1064 | } |
| 1065 | obj->set_args(*args); |
| 1066 | } |
| 1067 | return Utils::ToLocal(obj); |
| 1068 | } |
| 1069 | |
| 1070 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1071 | Local<AccessorSignature> AccessorSignature::New( |
| 1072 | Handle<FunctionTemplate> receiver) { |
| 1073 | return Utils::AccessorSignatureToLocal(Utils::OpenHandle(*receiver)); |
| 1074 | } |
| 1075 | |
| 1076 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1077 | template<typename Operation> |
| 1078 | static Local<Operation> NewDescriptor( |
| 1079 | Isolate* isolate, |
| 1080 | const i::DeclaredAccessorDescriptorData& data, |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 1081 | Data* previous_descriptor) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1082 | i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 1083 | i::Handle<i::DeclaredAccessorDescriptor> previous = |
| 1084 | i::Handle<i::DeclaredAccessorDescriptor>(); |
| 1085 | if (previous_descriptor != NULL) { |
| 1086 | previous = Utils::OpenHandle( |
| 1087 | static_cast<DeclaredAccessorDescriptor*>(previous_descriptor)); |
| 1088 | } |
| 1089 | i::Handle<i::DeclaredAccessorDescriptor> descriptor = |
| 1090 | i::DeclaredAccessorDescriptor::Create(internal_isolate, data, previous); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 1091 | return Utils::Convert<i::DeclaredAccessorDescriptor, Operation>(descriptor); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | |
| 1095 | Local<RawOperationDescriptor> |
| 1096 | ObjectOperationDescriptor::NewInternalFieldDereference( |
| 1097 | Isolate* isolate, |
| 1098 | int internal_field) { |
| 1099 | i::DeclaredAccessorDescriptorData data; |
| 1100 | data.type = i::kDescriptorObjectDereference; |
| 1101 | data.object_dereference_descriptor.internal_field = internal_field; |
| 1102 | return NewDescriptor<RawOperationDescriptor>(isolate, data, NULL); |
| 1103 | } |
| 1104 | |
| 1105 | |
| 1106 | Local<RawOperationDescriptor> RawOperationDescriptor::NewRawShift( |
| 1107 | Isolate* isolate, |
| 1108 | int16_t byte_offset) { |
| 1109 | i::DeclaredAccessorDescriptorData data; |
| 1110 | data.type = i::kDescriptorPointerShift; |
| 1111 | data.pointer_shift_descriptor.byte_offset = byte_offset; |
| 1112 | return NewDescriptor<RawOperationDescriptor>(isolate, data, this); |
| 1113 | } |
| 1114 | |
| 1115 | |
| 1116 | Local<DeclaredAccessorDescriptor> RawOperationDescriptor::NewHandleDereference( |
| 1117 | Isolate* isolate) { |
| 1118 | i::DeclaredAccessorDescriptorData data; |
| 1119 | data.type = i::kDescriptorReturnObject; |
| 1120 | return NewDescriptor<DeclaredAccessorDescriptor>(isolate, data, this); |
| 1121 | } |
| 1122 | |
| 1123 | |
| 1124 | Local<RawOperationDescriptor> RawOperationDescriptor::NewRawDereference( |
| 1125 | Isolate* isolate) { |
| 1126 | i::DeclaredAccessorDescriptorData data; |
| 1127 | data.type = i::kDescriptorPointerDereference; |
| 1128 | return NewDescriptor<RawOperationDescriptor>(isolate, data, this); |
| 1129 | } |
| 1130 | |
| 1131 | |
| 1132 | Local<DeclaredAccessorDescriptor> RawOperationDescriptor::NewPointerCompare( |
| 1133 | Isolate* isolate, |
| 1134 | void* compare_value) { |
| 1135 | i::DeclaredAccessorDescriptorData data; |
| 1136 | data.type = i::kDescriptorPointerCompare; |
| 1137 | data.pointer_compare_descriptor.compare_value = compare_value; |
| 1138 | return NewDescriptor<DeclaredAccessorDescriptor>(isolate, data, this); |
| 1139 | } |
| 1140 | |
| 1141 | |
| 1142 | Local<DeclaredAccessorDescriptor> RawOperationDescriptor::NewPrimitiveValue( |
| 1143 | Isolate* isolate, |
| 1144 | DeclaredAccessorDescriptorDataType data_type, |
| 1145 | uint8_t bool_offset) { |
| 1146 | i::DeclaredAccessorDescriptorData data; |
| 1147 | data.type = i::kDescriptorPrimitiveValue; |
| 1148 | data.primitive_value_descriptor.data_type = data_type; |
| 1149 | data.primitive_value_descriptor.bool_offset = bool_offset; |
| 1150 | return NewDescriptor<DeclaredAccessorDescriptor>(isolate, data, this); |
| 1151 | } |
| 1152 | |
| 1153 | |
| 1154 | template<typename T> |
| 1155 | static Local<DeclaredAccessorDescriptor> NewBitmaskCompare( |
| 1156 | Isolate* isolate, |
| 1157 | T bitmask, |
| 1158 | T compare_value, |
| 1159 | RawOperationDescriptor* operation) { |
| 1160 | i::DeclaredAccessorDescriptorData data; |
| 1161 | data.type = i::kDescriptorBitmaskCompare; |
| 1162 | data.bitmask_compare_descriptor.bitmask = bitmask; |
| 1163 | data.bitmask_compare_descriptor.compare_value = compare_value; |
| 1164 | data.bitmask_compare_descriptor.size = sizeof(T); |
| 1165 | return NewDescriptor<DeclaredAccessorDescriptor>(isolate, data, operation); |
| 1166 | } |
| 1167 | |
| 1168 | |
| 1169 | Local<DeclaredAccessorDescriptor> RawOperationDescriptor::NewBitmaskCompare8( |
| 1170 | Isolate* isolate, |
| 1171 | uint8_t bitmask, |
| 1172 | uint8_t compare_value) { |
| 1173 | return NewBitmaskCompare(isolate, bitmask, compare_value, this); |
| 1174 | } |
| 1175 | |
| 1176 | |
| 1177 | Local<DeclaredAccessorDescriptor> RawOperationDescriptor::NewBitmaskCompare16( |
| 1178 | Isolate* isolate, |
| 1179 | uint16_t bitmask, |
| 1180 | uint16_t compare_value) { |
| 1181 | return NewBitmaskCompare(isolate, bitmask, compare_value, this); |
| 1182 | } |
| 1183 | |
| 1184 | |
| 1185 | Local<DeclaredAccessorDescriptor> RawOperationDescriptor::NewBitmaskCompare32( |
| 1186 | Isolate* isolate, |
| 1187 | uint32_t bitmask, |
| 1188 | uint32_t compare_value) { |
| 1189 | return NewBitmaskCompare(isolate, bitmask, compare_value, this); |
| 1190 | } |
| 1191 | |
| 1192 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1193 | Local<TypeSwitch> TypeSwitch::New(Handle<FunctionTemplate> type) { |
| 1194 | Handle<FunctionTemplate> types[1] = { type }; |
| 1195 | return TypeSwitch::New(1, types); |
| 1196 | } |
| 1197 | |
| 1198 | |
| 1199 | Local<TypeSwitch> TypeSwitch::New(int argc, Handle<FunctionTemplate> types[]) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1200 | i::Isolate* isolate = i::Isolate::Current(); |
| 1201 | EnsureInitializedForIsolate(isolate, "v8::TypeSwitch::New()"); |
| 1202 | LOG_API(isolate, "TypeSwitch::New"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1203 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1204 | i::Handle<i::FixedArray> vector = isolate->factory()->NewFixedArray(argc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1205 | for (int i = 0; i < argc; i++) |
| 1206 | vector->set(i, *Utils::OpenHandle(*types[i])); |
| 1207 | i::Handle<i::Struct> struct_obj = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1208 | isolate->factory()->NewStruct(i::TYPE_SWITCH_INFO_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1209 | i::Handle<i::TypeSwitchInfo> obj = |
| 1210 | i::Handle<i::TypeSwitchInfo>::cast(struct_obj); |
| 1211 | obj->set_types(*vector); |
| 1212 | return Utils::ToLocal(obj); |
| 1213 | } |
| 1214 | |
| 1215 | |
| 1216 | int TypeSwitch::match(v8::Handle<Value> value) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1217 | i::Isolate* isolate = i::Isolate::Current(); |
| 1218 | LOG_API(isolate, "TypeSwitch::match"); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1219 | USE(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1220 | i::Handle<i::Object> obj = Utils::OpenHandle(*value); |
| 1221 | i::Handle<i::TypeSwitchInfo> info = Utils::OpenHandle(this); |
| 1222 | i::FixedArray* types = i::FixedArray::cast(info->types()); |
| 1223 | for (int i = 0; i < types->length(); i++) { |
| 1224 | if (obj->IsInstanceOf(i::FunctionTemplateInfo::cast(types->get(i)))) |
| 1225 | return i + 1; |
| 1226 | } |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
| 1230 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1231 | #define SET_FIELD_WRAPPED(obj, setter, cdata) do { \ |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 1232 | i::Handle<i::Object> foreign = FromCData(obj->GetIsolate(), cdata); \ |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1233 | (obj)->setter(*foreign); \ |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1234 | } while (false) |
| 1235 | |
| 1236 | |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1237 | void FunctionTemplate::SetCallHandler(FunctionCallback callback, |
| 1238 | v8::Handle<Value> data) { |
| 1239 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1240 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1241 | i::HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1242 | i::Handle<i::Struct> struct_obj = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1243 | isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1244 | i::Handle<i::CallHandlerInfo> obj = |
| 1245 | i::Handle<i::CallHandlerInfo>::cast(struct_obj); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1246 | SET_FIELD_WRAPPED(obj, set_callback, callback); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1247 | if (data.IsEmpty()) { |
| 1248 | data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate)); |
| 1249 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1250 | obj->set_data(*Utils::OpenHandle(*data)); |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1251 | Utils::OpenHandle(this)->set_call_code(*obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1255 | static i::Handle<i::AccessorInfo> SetAccessorInfoProperties( |
| 1256 | i::Handle<i::AccessorInfo> obj, |
| 1257 | v8::Handle<String> name, |
| 1258 | v8::AccessControl settings, |
| 1259 | v8::PropertyAttribute attributes, |
| 1260 | v8::Handle<AccessorSignature> signature) { |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 1261 | obj->set_name(*Utils::OpenHandle(*name)); |
| 1262 | if (settings & ALL_CAN_READ) obj->set_all_can_read(true); |
| 1263 | if (settings & ALL_CAN_WRITE) obj->set_all_can_write(true); |
| 1264 | if (settings & PROHIBITS_OVERWRITING) obj->set_prohibits_overwriting(true); |
| 1265 | obj->set_property_attributes(static_cast<PropertyAttributes>(attributes)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1266 | if (!signature.IsEmpty()) { |
| 1267 | obj->set_expected_receiver_type(*Utils::OpenHandle(*signature)); |
| 1268 | } |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 1269 | return obj; |
| 1270 | } |
| 1271 | |
| 1272 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1273 | template<typename Getter, typename Setter> |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1274 | static i::Handle<i::AccessorInfo> MakeAccessorInfo( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1275 | v8::Handle<String> name, |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1276 | Getter getter, |
| 1277 | Setter setter, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1278 | v8::Handle<Value> data, |
| 1279 | v8::AccessControl settings, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1280 | v8::PropertyAttribute attributes, |
| 1281 | v8::Handle<AccessorSignature> signature) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1282 | i::Isolate* isolate = Utils::OpenHandle(*name)->GetIsolate(); |
| 1283 | i::Handle<i::ExecutableAccessorInfo> obj = |
| 1284 | isolate->factory()->NewExecutableAccessorInfo(); |
| 1285 | SET_FIELD_WRAPPED(obj, set_getter, getter); |
| 1286 | SET_FIELD_WRAPPED(obj, set_setter, setter); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1287 | if (data.IsEmpty()) { |
| 1288 | data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate)); |
| 1289 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1290 | obj->set_data(*Utils::OpenHandle(*data)); |
| 1291 | return SetAccessorInfoProperties(obj, name, settings, attributes, signature); |
| 1292 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1293 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1294 | |
| 1295 | static i::Handle<i::AccessorInfo> MakeAccessorInfo( |
| 1296 | v8::Handle<String> name, |
| 1297 | v8::Handle<v8::DeclaredAccessorDescriptor> descriptor, |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1298 | void* setter_ignored, |
| 1299 | void* data_ignored, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1300 | v8::AccessControl settings, |
| 1301 | v8::PropertyAttribute attributes, |
| 1302 | v8::Handle<AccessorSignature> signature) { |
| 1303 | i::Isolate* isolate = Utils::OpenHandle(*name)->GetIsolate(); |
| 1304 | if (descriptor.IsEmpty()) return i::Handle<i::DeclaredAccessorInfo>(); |
| 1305 | i::Handle<i::DeclaredAccessorInfo> obj = |
| 1306 | isolate->factory()->NewDeclaredAccessorInfo(); |
| 1307 | obj->set_descriptor(*Utils::OpenHandle(*descriptor)); |
| 1308 | return SetAccessorInfoProperties(obj, name, settings, attributes, signature); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | |
| 1312 | Local<ObjectTemplate> FunctionTemplate::InstanceTemplate() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1313 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
machenbach@chromium.org | ae16103 | 2013-09-24 09:12:30 +0000 | [diff] [blame] | 1314 | if (EmptyCheck("v8::FunctionTemplate::InstanceTemplate()", this)) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1315 | return Local<ObjectTemplate>(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1316 | ENTER_V8(isolate); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 1317 | i::Handle<i::FunctionTemplateInfo> handle = Utils::OpenHandle(this); |
| 1318 | if (handle->instance_template()->IsUndefined()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1319 | Local<ObjectTemplate> templ = |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 1320 | ObjectTemplate::New(ToApiHandle<FunctionTemplate>(handle)); |
| 1321 | handle->set_instance_template(*Utils::OpenHandle(*templ)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1322 | } |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 1323 | i::Handle<i::ObjectTemplateInfo> result( |
| 1324 | i::ObjectTemplateInfo::cast(handle->instance_template())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1325 | return Utils::ToLocal(result); |
| 1326 | } |
| 1327 | |
| 1328 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1329 | void FunctionTemplate::SetLength(int length) { |
| 1330 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1331 | ENTER_V8(isolate); |
| 1332 | Utils::OpenHandle(this)->set_length(length); |
| 1333 | } |
| 1334 | |
| 1335 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1336 | void FunctionTemplate::SetClassName(Handle<String> name) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1337 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1338 | ENTER_V8(isolate); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1339 | Utils::OpenHandle(this)->set_class_name(*Utils::OpenHandle(*name)); |
| 1340 | } |
| 1341 | |
| 1342 | |
| 1343 | void FunctionTemplate::SetHiddenPrototype(bool value) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1344 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1345 | ENTER_V8(isolate); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1346 | Utils::OpenHandle(this)->set_hidden_prototype(value); |
| 1347 | } |
| 1348 | |
| 1349 | |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 1350 | void FunctionTemplate::ReadOnlyPrototype() { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1351 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1352 | ENTER_V8(isolate); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 1353 | Utils::OpenHandle(this)->set_read_only_prototype(true); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1356 | |
| 1357 | void FunctionTemplate::RemovePrototype() { |
| 1358 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1359 | ENTER_V8(isolate); |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1360 | Utils::OpenHandle(this)->set_remove_prototype(true); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1364 | // --- O b j e c t T e m p l a t e --- |
| 1365 | |
| 1366 | |
| 1367 | Local<ObjectTemplate> ObjectTemplate::New() { |
| 1368 | return New(Local<FunctionTemplate>()); |
| 1369 | } |
| 1370 | |
| 1371 | |
| 1372 | Local<ObjectTemplate> ObjectTemplate::New( |
| 1373 | v8::Handle<FunctionTemplate> constructor) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1374 | i::Isolate* isolate = i::Isolate::Current(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1375 | EnsureInitializedForIsolate(isolate, "v8::ObjectTemplate::New()"); |
| 1376 | LOG_API(isolate, "ObjectTemplate::New"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1377 | ENTER_V8(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1378 | i::Handle<i::Struct> struct_obj = |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1379 | isolate->factory()->NewStruct(i::OBJECT_TEMPLATE_INFO_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1380 | i::Handle<i::ObjectTemplateInfo> obj = |
| 1381 | i::Handle<i::ObjectTemplateInfo>::cast(struct_obj); |
| 1382 | InitializeTemplate(obj, Consts::OBJECT_TEMPLATE); |
| 1383 | if (!constructor.IsEmpty()) |
| 1384 | obj->set_constructor(*Utils::OpenHandle(*constructor)); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1385 | obj->set_internal_field_count(i::Smi::FromInt(0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1386 | return Utils::ToLocal(obj); |
| 1387 | } |
| 1388 | |
| 1389 | |
| 1390 | // Ensure that the object template has a constructor. If no |
| 1391 | // constructor is available we create one. |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1392 | static i::Handle<i::FunctionTemplateInfo> EnsureConstructor( |
| 1393 | ObjectTemplate* object_template) { |
| 1394 | i::Object* obj = Utils::OpenHandle(object_template)->constructor(); |
| 1395 | if (!obj ->IsUndefined()) { |
| 1396 | i::FunctionTemplateInfo* info = i::FunctionTemplateInfo::cast(obj); |
| 1397 | return i::Handle<i::FunctionTemplateInfo>(info, info->GetIsolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1398 | } |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1399 | Local<FunctionTemplate> templ = FunctionTemplate::New(); |
| 1400 | i::Handle<i::FunctionTemplateInfo> constructor = Utils::OpenHandle(*templ); |
| 1401 | constructor->set_instance_template(*Utils::OpenHandle(object_template)); |
| 1402 | Utils::OpenHandle(object_template)->set_constructor(*constructor); |
| 1403 | return constructor; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1407 | static inline void AddPropertyToTemplate( |
| 1408 | i::Handle<i::TemplateInfo> info, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1409 | i::Handle<i::AccessorInfo> obj) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1410 | i::Handle<i::Object> list(info->property_accessors(), info->GetIsolate()); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1411 | if (list->IsUndefined()) { |
| 1412 | list = NeanderArray().value(); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1413 | info->set_property_accessors(*list); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1414 | } |
| 1415 | NeanderArray array(list); |
| 1416 | array.add(obj); |
| 1417 | } |
| 1418 | |
| 1419 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1420 | static inline i::Handle<i::TemplateInfo> GetTemplateInfo( |
| 1421 | Template* template_obj) { |
| 1422 | return Utils::OpenHandle(template_obj); |
| 1423 | } |
| 1424 | |
| 1425 | |
| 1426 | // TODO(dcarney): remove this with ObjectTemplate::SetAccessor |
| 1427 | static inline i::Handle<i::TemplateInfo> GetTemplateInfo( |
| 1428 | ObjectTemplate* object_template) { |
| 1429 | EnsureConstructor(object_template); |
| 1430 | return Utils::OpenHandle(object_template); |
| 1431 | } |
| 1432 | |
| 1433 | |
| 1434 | template<typename Setter, typename Getter, typename Data, typename Template> |
| 1435 | static bool TemplateSetAccessor( |
| 1436 | Template* template_obj, |
| 1437 | v8::Local<String> name, |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1438 | Getter getter, |
| 1439 | Setter setter, |
| 1440 | Data data, |
| 1441 | AccessControl settings, |
| 1442 | PropertyAttribute attribute, |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1443 | v8::Local<AccessorSignature> signature) { |
| 1444 | i::Isolate* isolate = Utils::OpenHandle(template_obj)->GetIsolate(); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1445 | ENTER_V8(isolate); |
| 1446 | i::HandleScope scope(isolate); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1447 | i::Handle<i::AccessorInfo> obj = MakeAccessorInfo( |
| 1448 | name, getter, setter, data, settings, attribute, signature); |
| 1449 | if (obj.is_null()) return false; |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1450 | i::Handle<i::TemplateInfo> info = GetTemplateInfo(template_obj); |
| 1451 | AddPropertyToTemplate(info, obj); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1452 | return true; |
| 1453 | } |
| 1454 | |
| 1455 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1456 | bool Template::SetDeclaredAccessor( |
| 1457 | Local<String> name, |
| 1458 | Local<DeclaredAccessorDescriptor> descriptor, |
| 1459 | PropertyAttribute attribute, |
| 1460 | Local<AccessorSignature> signature, |
| 1461 | AccessControl settings) { |
| 1462 | void* null = NULL; |
| 1463 | return TemplateSetAccessor( |
| 1464 | this, name, descriptor, null, null, settings, attribute, signature); |
| 1465 | } |
| 1466 | |
| 1467 | |
| 1468 | void Template::SetNativeDataProperty(v8::Local<String> name, |
| 1469 | AccessorGetterCallback getter, |
| 1470 | AccessorSetterCallback setter, |
| 1471 | v8::Handle<Value> data, |
| 1472 | PropertyAttribute attribute, |
| 1473 | v8::Local<AccessorSignature> signature, |
| 1474 | AccessControl settings) { |
| 1475 | TemplateSetAccessor( |
| 1476 | this, name, getter, setter, data, settings, attribute, signature); |
| 1477 | } |
| 1478 | |
| 1479 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1480 | void ObjectTemplate::SetAccessor(v8::Handle<String> name, |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1481 | AccessorGetterCallback getter, |
| 1482 | AccessorSetterCallback setter, |
| 1483 | v8::Handle<Value> data, |
| 1484 | AccessControl settings, |
| 1485 | PropertyAttribute attribute, |
| 1486 | v8::Handle<AccessorSignature> signature) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1487 | TemplateSetAccessor( |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1488 | this, name, getter, setter, data, settings, attribute, signature); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1492 | void ObjectTemplate::SetNamedPropertyHandler( |
| 1493 | NamedPropertyGetterCallback getter, |
| 1494 | NamedPropertySetterCallback setter, |
| 1495 | NamedPropertyQueryCallback query, |
| 1496 | NamedPropertyDeleterCallback remover, |
| 1497 | NamedPropertyEnumeratorCallback enumerator, |
| 1498 | Handle<Value> data) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1499 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1500 | ENTER_V8(isolate); |
| 1501 | i::HandleScope scope(isolate); |
| 1502 | EnsureConstructor(this); |
| 1503 | i::FunctionTemplateInfo* constructor = i::FunctionTemplateInfo::cast( |
| 1504 | Utils::OpenHandle(this)->constructor()); |
| 1505 | i::Handle<i::FunctionTemplateInfo> cons(constructor); |
| 1506 | i::Handle<i::Struct> struct_obj = |
| 1507 | isolate->factory()->NewStruct(i::INTERCEPTOR_INFO_TYPE); |
| 1508 | i::Handle<i::InterceptorInfo> obj = |
| 1509 | i::Handle<i::InterceptorInfo>::cast(struct_obj); |
| 1510 | |
| 1511 | if (getter != 0) SET_FIELD_WRAPPED(obj, set_getter, getter); |
| 1512 | if (setter != 0) SET_FIELD_WRAPPED(obj, set_setter, setter); |
| 1513 | if (query != 0) SET_FIELD_WRAPPED(obj, set_query, query); |
| 1514 | if (remover != 0) SET_FIELD_WRAPPED(obj, set_deleter, remover); |
| 1515 | if (enumerator != 0) SET_FIELD_WRAPPED(obj, set_enumerator, enumerator); |
| 1516 | |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1517 | if (data.IsEmpty()) { |
| 1518 | data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate)); |
| 1519 | } |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1520 | obj->set_data(*Utils::OpenHandle(*data)); |
| 1521 | cons->set_named_property_handler(*obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | |
| 1525 | void ObjectTemplate::MarkAsUndetectable() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1526 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1527 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1528 | i::HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1529 | EnsureConstructor(this); |
| 1530 | i::FunctionTemplateInfo* constructor = |
| 1531 | i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor()); |
| 1532 | i::Handle<i::FunctionTemplateInfo> cons(constructor); |
| 1533 | cons->set_undetectable(true); |
| 1534 | } |
| 1535 | |
| 1536 | |
| 1537 | void ObjectTemplate::SetAccessCheckCallbacks( |
| 1538 | NamedSecurityCallback named_callback, |
| 1539 | IndexedSecurityCallback indexed_callback, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1540 | Handle<Value> data, |
| 1541 | bool turned_on_by_default) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1542 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1543 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1544 | i::HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1545 | EnsureConstructor(this); |
| 1546 | |
| 1547 | i::Handle<i::Struct> struct_info = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1548 | isolate->factory()->NewStruct(i::ACCESS_CHECK_INFO_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1549 | i::Handle<i::AccessCheckInfo> info = |
| 1550 | i::Handle<i::AccessCheckInfo>::cast(struct_info); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1551 | |
| 1552 | SET_FIELD_WRAPPED(info, set_named_callback, named_callback); |
| 1553 | SET_FIELD_WRAPPED(info, set_indexed_callback, indexed_callback); |
| 1554 | |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1555 | if (data.IsEmpty()) { |
| 1556 | data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate)); |
| 1557 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1558 | info->set_data(*Utils::OpenHandle(*data)); |
| 1559 | |
| 1560 | i::FunctionTemplateInfo* constructor = |
| 1561 | i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor()); |
| 1562 | i::Handle<i::FunctionTemplateInfo> cons(constructor); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1563 | cons->set_access_check_info(*info); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1564 | cons->set_needs_access_check(turned_on_by_default); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1568 | void ObjectTemplate::SetIndexedPropertyHandler( |
| 1569 | IndexedPropertyGetterCallback getter, |
| 1570 | IndexedPropertySetterCallback setter, |
| 1571 | IndexedPropertyQueryCallback query, |
| 1572 | IndexedPropertyDeleterCallback remover, |
| 1573 | IndexedPropertyEnumeratorCallback enumerator, |
| 1574 | Handle<Value> data) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1575 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1576 | ENTER_V8(isolate); |
| 1577 | i::HandleScope scope(isolate); |
| 1578 | EnsureConstructor(this); |
| 1579 | i::FunctionTemplateInfo* constructor = i::FunctionTemplateInfo::cast( |
| 1580 | Utils::OpenHandle(this)->constructor()); |
| 1581 | i::Handle<i::FunctionTemplateInfo> cons(constructor); |
| 1582 | i::Handle<i::Struct> struct_obj = |
| 1583 | isolate->factory()->NewStruct(i::INTERCEPTOR_INFO_TYPE); |
| 1584 | i::Handle<i::InterceptorInfo> obj = |
| 1585 | i::Handle<i::InterceptorInfo>::cast(struct_obj); |
| 1586 | |
| 1587 | if (getter != 0) SET_FIELD_WRAPPED(obj, set_getter, getter); |
| 1588 | if (setter != 0) SET_FIELD_WRAPPED(obj, set_setter, setter); |
| 1589 | if (query != 0) SET_FIELD_WRAPPED(obj, set_query, query); |
| 1590 | if (remover != 0) SET_FIELD_WRAPPED(obj, set_deleter, remover); |
| 1591 | if (enumerator != 0) SET_FIELD_WRAPPED(obj, set_enumerator, enumerator); |
| 1592 | |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1593 | if (data.IsEmpty()) { |
| 1594 | data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate)); |
| 1595 | } |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1596 | obj->set_data(*Utils::OpenHandle(*data)); |
| 1597 | cons->set_indexed_property_handler(*obj); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1601 | void ObjectTemplate::SetCallAsFunctionHandler(FunctionCallback callback, |
| 1602 | Handle<Value> data) { |
| 1603 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1604 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1605 | i::HandleScope scope(isolate); |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1606 | EnsureConstructor(this); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1607 | i::FunctionTemplateInfo* constructor = i::FunctionTemplateInfo::cast( |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1608 | Utils::OpenHandle(this)->constructor()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1609 | i::Handle<i::FunctionTemplateInfo> cons(constructor); |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1610 | i::Handle<i::Struct> struct_obj = |
| 1611 | isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE); |
| 1612 | i::Handle<i::CallHandlerInfo> obj = |
| 1613 | i::Handle<i::CallHandlerInfo>::cast(struct_obj); |
| 1614 | SET_FIELD_WRAPPED(obj, set_callback, callback); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1615 | if (data.IsEmpty()) { |
| 1616 | data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate)); |
| 1617 | } |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1618 | obj->set_data(*Utils::OpenHandle(*data)); |
| 1619 | cons->set_instance_call_handler(*obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1623 | int ObjectTemplate::InternalFieldCount() { |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1624 | return i::Smi::cast(Utils::OpenHandle(this)->internal_field_count())->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1628 | void ObjectTemplate::SetInternalFieldCount(int value) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1629 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1630 | if (!ApiCheck(i::Smi::IsValid(value), |
| 1631 | "v8::ObjectTemplate::SetInternalFieldCount()", |
| 1632 | "Invalid internal field count")) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1633 | return; |
| 1634 | } |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1635 | ENTER_V8(isolate); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1636 | if (value > 0) { |
| 1637 | // The internal field count is set by the constructor function's |
| 1638 | // construct code, so we ensure that there is a constructor |
| 1639 | // function to do the setting. |
| 1640 | EnsureConstructor(this); |
| 1641 | } |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1642 | Utils::OpenHandle(this)->set_internal_field_count(i::Smi::FromInt(value)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 1646 | // --- S c r i p t D a t a --- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1647 | |
| 1648 | |
| 1649 | ScriptData* ScriptData::PreCompile(const char* input, int length) { |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 1650 | i::Utf8ToUtf16CharacterStream stream( |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1651 | reinterpret_cast<const unsigned char*>(input), length); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1652 | return i::PreParserApi::PreParse(i::Isolate::Current(), &stream); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1656 | ScriptData* ScriptData::PreCompile(v8::Handle<String> source) { |
| 1657 | i::Handle<i::String> str = Utils::OpenHandle(*source); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1658 | i::Isolate* isolate = str->GetIsolate(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1659 | if (str->IsExternalTwoByteString()) { |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 1660 | i::ExternalTwoByteStringUtf16CharacterStream stream( |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1661 | i::Handle<i::ExternalTwoByteString>::cast(str), 0, str->length()); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1662 | return i::PreParserApi::PreParse(isolate, &stream); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1663 | } else { |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 1664 | i::GenericStringUtf16CharacterStream stream(str, 0, str->length()); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1665 | return i::PreParserApi::PreParse(isolate, &stream); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1666 | } |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 1670 | ScriptData* ScriptData::New(const char* data, int length) { |
| 1671 | // Return an empty ScriptData if the length is obviously invalid. |
| 1672 | if (length % sizeof(unsigned) != 0) { |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 1673 | return new i::ScriptDataImpl(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | // Copy the data to ensure it is properly aligned. |
| 1677 | int deserialized_data_length = length / sizeof(unsigned); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 1678 | // If aligned, don't create a copy of the data. |
| 1679 | if (reinterpret_cast<intptr_t>(data) % sizeof(unsigned) == 0) { |
| 1680 | return new i::ScriptDataImpl(data, length); |
| 1681 | } |
| 1682 | // Copy the data to align it. |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 1683 | unsigned* deserialized_data = i::NewArray<unsigned>(deserialized_data_length); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1684 | i::CopyBytes(reinterpret_cast<char*>(deserialized_data), |
| 1685 | data, static_cast<size_t>(length)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 1686 | |
| 1687 | return new i::ScriptDataImpl( |
| 1688 | i::Vector<unsigned>(deserialized_data, deserialized_data_length)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | |
| 1692 | // --- S c r i p t --- |
| 1693 | |
| 1694 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1695 | Local<Script> Script::New(v8::Handle<String> source, |
| 1696 | v8::ScriptOrigin* origin, |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1697 | v8::ScriptData* pre_data, |
| 1698 | v8::Handle<String> script_data) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1699 | i::Isolate* isolate = i::Isolate::Current(); |
| 1700 | ON_BAILOUT(isolate, "v8::Script::New()", return Local<Script>()); |
| 1701 | LOG_API(isolate, "Script::New"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1702 | ENTER_V8(isolate); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1703 | i::SharedFunctionInfo* raw_result = NULL; |
| 1704 | { i::HandleScope scope(isolate); |
| 1705 | i::Handle<i::String> str = Utils::OpenHandle(*source); |
| 1706 | i::Handle<i::Object> name_obj; |
| 1707 | int line_offset = 0; |
| 1708 | int column_offset = 0; |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 1709 | bool is_shared_cross_origin = false; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1710 | if (origin != NULL) { |
| 1711 | if (!origin->ResourceName().IsEmpty()) { |
| 1712 | name_obj = Utils::OpenHandle(*origin->ResourceName()); |
| 1713 | } |
| 1714 | if (!origin->ResourceLineOffset().IsEmpty()) { |
| 1715 | line_offset = static_cast<int>(origin->ResourceLineOffset()->Value()); |
| 1716 | } |
| 1717 | if (!origin->ResourceColumnOffset().IsEmpty()) { |
| 1718 | column_offset = |
| 1719 | static_cast<int>(origin->ResourceColumnOffset()->Value()); |
| 1720 | } |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 1721 | if (!origin->ResourceIsSharedCrossOrigin().IsEmpty()) { |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1722 | v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 1723 | is_shared_cross_origin = |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1724 | origin->ResourceIsSharedCrossOrigin() == v8::True(v8_isolate); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 1725 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1726 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1727 | EXCEPTION_PREAMBLE(isolate); |
| 1728 | i::ScriptDataImpl* pre_data_impl = |
| 1729 | static_cast<i::ScriptDataImpl*>(pre_data); |
| 1730 | // We assert that the pre-data is sane, even though we can actually |
| 1731 | // handle it if it turns out not to be in release mode. |
| 1732 | ASSERT(pre_data_impl == NULL || pre_data_impl->SanityCheck()); |
| 1733 | // If the pre-data isn't sane we simply ignore it |
| 1734 | if (pre_data_impl != NULL && !pre_data_impl->SanityCheck()) { |
| 1735 | pre_data_impl = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1736 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1737 | i::Handle<i::SharedFunctionInfo> result = |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1738 | i::Compiler::Compile(str, |
| 1739 | name_obj, |
| 1740 | line_offset, |
| 1741 | column_offset, |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 1742 | is_shared_cross_origin, |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1743 | isolate->global_context(), |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1744 | NULL, |
| 1745 | pre_data_impl, |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 1746 | Utils::OpenHandle(*script_data, true), |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1747 | i::NOT_NATIVES_CODE); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1748 | has_pending_exception = result.is_null(); |
| 1749 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Script>()); |
| 1750 | raw_result = *result; |
| 1751 | } |
| 1752 | i::Handle<i::SharedFunctionInfo> result(raw_result, isolate); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 1753 | return ToApiHandle<Script>(result); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | |
| 1757 | Local<Script> Script::New(v8::Handle<String> source, |
| 1758 | v8::Handle<Value> file_name) { |
| 1759 | ScriptOrigin origin(file_name); |
| 1760 | return New(source, &origin); |
| 1761 | } |
| 1762 | |
| 1763 | |
| 1764 | Local<Script> Script::Compile(v8::Handle<String> source, |
| 1765 | v8::ScriptOrigin* origin, |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1766 | v8::ScriptData* pre_data, |
| 1767 | v8::Handle<String> script_data) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1768 | i::Isolate* isolate = i::Isolate::Current(); |
| 1769 | ON_BAILOUT(isolate, "v8::Script::Compile()", return Local<Script>()); |
| 1770 | LOG_API(isolate, "Script::Compile"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1771 | ENTER_V8(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1772 | Local<Script> generic = New(source, origin, pre_data, script_data); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1773 | if (generic.IsEmpty()) |
| 1774 | return generic; |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1775 | i::Handle<i::Object> obj = Utils::OpenHandle(*generic); |
| 1776 | i::Handle<i::SharedFunctionInfo> function = |
| 1777 | i::Handle<i::SharedFunctionInfo>(i::SharedFunctionInfo::cast(*obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1778 | i::Handle<i::JSFunction> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1779 | isolate->factory()->NewFunctionFromSharedFunctionInfo( |
| 1780 | function, |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1781 | isolate->global_context()); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 1782 | return ToApiHandle<Script>(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1786 | Local<Script> Script::Compile(v8::Handle<String> source, |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1787 | v8::Handle<Value> file_name, |
| 1788 | v8::Handle<String> script_data) { |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1789 | ScriptOrigin origin(file_name); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1790 | return Compile(source, &origin, 0, script_data); |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1794 | Local<Value> Script::Run() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1795 | i::Isolate* isolate = i::Isolate::Current(); |
| 1796 | ON_BAILOUT(isolate, "v8::Script::Run()", return Local<Value>()); |
| 1797 | LOG_API(isolate, "Script::Run"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1798 | ENTER_V8(isolate); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1799 | i::Logger::TimerEventScope timer_scope( |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 1800 | isolate, i::Logger::TimerEventScope::v8_execute); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1801 | i::Object* raw_result = NULL; |
| 1802 | { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1803 | i::HandleScope scope(isolate); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1804 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 1805 | i::Handle<i::JSFunction> fun; |
| 1806 | if (obj->IsSharedFunctionInfo()) { |
| 1807 | i::Handle<i::SharedFunctionInfo> |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1808 | function_info(i::SharedFunctionInfo::cast(*obj), isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1809 | fun = isolate->factory()->NewFunctionFromSharedFunctionInfo( |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1810 | function_info, isolate->global_context()); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1811 | } else { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1812 | fun = i::Handle<i::JSFunction>(i::JSFunction::cast(*obj), isolate); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1813 | } |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1814 | EXCEPTION_PREAMBLE(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1815 | i::Handle<i::Object> receiver( |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1816 | isolate->context()->global_proxy(), isolate); |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 1817 | i::Handle<i::Object> result = i::Execution::Call( |
| 1818 | isolate, fun, receiver, 0, NULL, &has_pending_exception); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 1819 | EXCEPTION_BAILOUT_CHECK_DO_CALLBACK(isolate, Local<Value>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1820 | raw_result = *result; |
| 1821 | } |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1822 | i::Handle<i::Object> result(raw_result, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1823 | return Utils::ToLocal(result); |
| 1824 | } |
| 1825 | |
| 1826 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1827 | static i::Handle<i::SharedFunctionInfo> OpenScript(Script* script) { |
| 1828 | i::Handle<i::Object> obj = Utils::OpenHandle(script); |
| 1829 | i::Handle<i::SharedFunctionInfo> result; |
| 1830 | if (obj->IsSharedFunctionInfo()) { |
| 1831 | result = |
| 1832 | i::Handle<i::SharedFunctionInfo>(i::SharedFunctionInfo::cast(*obj)); |
| 1833 | } else { |
| 1834 | result = |
| 1835 | i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared()); |
| 1836 | } |
| 1837 | return result; |
| 1838 | } |
| 1839 | |
| 1840 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1841 | Local<Value> Script::Id() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1842 | i::Isolate* isolate = i::Isolate::Current(); |
| 1843 | ON_BAILOUT(isolate, "v8::Script::Id()", return Local<Value>()); |
| 1844 | LOG_API(isolate, "Script::Id"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1845 | i::Object* raw_id = NULL; |
| 1846 | { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1847 | i::HandleScope scope(isolate); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1848 | i::Handle<i::SharedFunctionInfo> function_info = OpenScript(this); |
| 1849 | i::Handle<i::Script> script(i::Script::cast(function_info->script())); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 1850 | i::Handle<i::Object> id(script->id(), isolate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1851 | raw_id = *id; |
| 1852 | } |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 1853 | i::Handle<i::Object> id(raw_id, isolate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1854 | return Utils::ToLocal(id); |
| 1855 | } |
| 1856 | |
| 1857 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1858 | int Script::GetId() { |
| 1859 | i::Isolate* isolate = i::Isolate::Current(); |
| 1860 | ON_BAILOUT(isolate, "v8::Script::Id()", return -1); |
| 1861 | LOG_API(isolate, "Script::Id"); |
| 1862 | { |
| 1863 | i::HandleScope scope(isolate); |
| 1864 | i::Handle<i::SharedFunctionInfo> function_info = OpenScript(this); |
| 1865 | i::Handle<i::Script> script(i::Script::cast(function_info->script())); |
| 1866 | return script->id()->value(); |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1871 | int Script::GetLineNumber(int code_pos) { |
| 1872 | i::Isolate* isolate = i::Isolate::Current(); |
| 1873 | ON_BAILOUT(isolate, "v8::Script::GetLineNumber()", return -1); |
| 1874 | LOG_API(isolate, "Script::GetLineNumber"); |
| 1875 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 1876 | if (obj->IsScript()) { |
| 1877 | i::Handle<i::Script> script = i::Handle<i::Script>(i::Script::cast(*obj)); |
| 1878 | return i::GetScriptLineNumber(script, code_pos); |
| 1879 | } else { |
| 1880 | return -1; |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | |
| 1885 | Handle<Value> Script::GetScriptName() { |
| 1886 | i::Isolate* isolate = i::Isolate::Current(); |
| 1887 | ON_BAILOUT(isolate, "v8::Script::GetName()", return Handle<String>()); |
| 1888 | LOG_API(isolate, "Script::GetName"); |
| 1889 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 1890 | if (obj->IsScript()) { |
| 1891 | i::Object* name = i::Script::cast(*obj)->name(); |
| 1892 | return Utils::ToLocal(i::Handle<i::Object>(name, isolate)); |
| 1893 | } else { |
| 1894 | return Handle<String>(); |
| 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 1899 | void Script::SetData(v8::Handle<String> data) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1900 | i::Isolate* isolate = i::Isolate::Current(); |
| 1901 | ON_BAILOUT(isolate, "v8::Script::SetData()", return); |
| 1902 | LOG_API(isolate, "Script::SetData"); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1903 | { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1904 | i::HandleScope scope(isolate); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1905 | i::Handle<i::SharedFunctionInfo> function_info = OpenScript(this); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1906 | i::Handle<i::Object> raw_data = Utils::OpenHandle(*data); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1907 | i::Handle<i::Script> script(i::Script::cast(function_info->script())); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1908 | script->set_data(*raw_data); |
| 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1913 | // --- E x c e p t i o n s --- |
| 1914 | |
| 1915 | |
| 1916 | v8::TryCatch::TryCatch() |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 1917 | : isolate_(i::Isolate::Current()), |
| 1918 | next_(isolate_->try_catch_handler_address()), |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1919 | is_verbose_(false), |
ager@chromium.org | 68e7ab7 | 2009-09-23 09:40:39 +0000 | [diff] [blame] | 1920 | can_continue_(true), |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1921 | capture_message_(true), |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 1922 | rethrow_(false), |
| 1923 | has_terminated_(false) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 1924 | Reset(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 1925 | isolate_->RegisterTryCatchHandler(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | |
| 1929 | v8::TryCatch::~TryCatch() { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 1930 | ASSERT(isolate_ == i::Isolate::Current()); |
christian.plesner.hansen@gmail.com | b9ce637 | 2009-11-03 11:38:18 +0000 | [diff] [blame] | 1931 | if (rethrow_) { |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1932 | v8::Isolate* isolate = reinterpret_cast<Isolate*>(isolate_); |
| 1933 | v8::HandleScope scope(isolate); |
| 1934 | v8::Local<v8::Value> exc = v8::Local<v8::Value>::New(isolate, Exception()); |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 1935 | if (HasCaught() && capture_message_) { |
| 1936 | // If an exception was caught and rethrow_ is indicated, the saved |
| 1937 | // message, script, and location need to be restored to Isolate TLS |
| 1938 | // for reuse. capture_message_ needs to be disabled so that DoThrow() |
| 1939 | // does not create a new message. |
| 1940 | isolate_->thread_local_top()->rethrowing_message_ = true; |
| 1941 | isolate_->RestorePendingMessageFromTryCatch(this); |
| 1942 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 1943 | isolate_->UnregisterTryCatchHandler(this); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1944 | reinterpret_cast<Isolate*>(isolate_)->ThrowException(exc); |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 1945 | ASSERT(!isolate_->thread_local_top()->rethrowing_message_); |
christian.plesner.hansen@gmail.com | b9ce637 | 2009-11-03 11:38:18 +0000 | [diff] [blame] | 1946 | } else { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 1947 | isolate_->UnregisterTryCatchHandler(this); |
christian.plesner.hansen@gmail.com | b9ce637 | 2009-11-03 11:38:18 +0000 | [diff] [blame] | 1948 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1952 | bool v8::TryCatch::HasCaught() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1953 | return !reinterpret_cast<i::Object*>(exception_)->IsTheHole(); |
| 1954 | } |
| 1955 | |
| 1956 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 1957 | bool v8::TryCatch::CanContinue() const { |
| 1958 | return can_continue_; |
| 1959 | } |
| 1960 | |
| 1961 | |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 1962 | bool v8::TryCatch::HasTerminated() const { |
| 1963 | return has_terminated_; |
| 1964 | } |
| 1965 | |
| 1966 | |
christian.plesner.hansen@gmail.com | b9ce637 | 2009-11-03 11:38:18 +0000 | [diff] [blame] | 1967 | v8::Handle<v8::Value> v8::TryCatch::ReThrow() { |
| 1968 | if (!HasCaught()) return v8::Local<v8::Value>(); |
| 1969 | rethrow_ = true; |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1970 | return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate_)); |
christian.plesner.hansen@gmail.com | b9ce637 | 2009-11-03 11:38:18 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1974 | v8::Local<Value> v8::TryCatch::Exception() const { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 1975 | ASSERT(isolate_ == i::Isolate::Current()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1976 | if (HasCaught()) { |
| 1977 | // Check for out of memory exception. |
| 1978 | i::Object* exception = reinterpret_cast<i::Object*>(exception_); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 1979 | return v8::Utils::ToLocal(i::Handle<i::Object>(exception, isolate_)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1980 | } else { |
| 1981 | return v8::Local<Value>(); |
| 1982 | } |
| 1983 | } |
| 1984 | |
| 1985 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1986 | v8::Local<Value> v8::TryCatch::StackTrace() const { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 1987 | ASSERT(isolate_ == i::Isolate::Current()); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1988 | if (HasCaught()) { |
| 1989 | i::Object* raw_obj = reinterpret_cast<i::Object*>(exception_); |
| 1990 | if (!raw_obj->IsJSObject()) return v8::Local<Value>(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 1991 | i::HandleScope scope(isolate_); |
| 1992 | i::Handle<i::JSObject> obj(i::JSObject::cast(raw_obj), isolate_); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1993 | i::Handle<i::String> name = isolate_->factory()->stack_string(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1994 | if (!i::JSReceiver::HasProperty(obj, name)) return v8::Local<Value>(); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 1995 | i::Handle<i::Object> value = i::GetProperty(isolate_, obj, name); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1996 | if (value.is_null()) return v8::Local<Value>(); |
| 1997 | return v8::Utils::ToLocal(scope.CloseAndEscape(value)); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1998 | } else { |
| 1999 | return v8::Local<Value>(); |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2004 | v8::Local<v8::Message> v8::TryCatch::Message() const { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 2005 | ASSERT(isolate_ == i::Isolate::Current()); |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 2006 | i::Object* message = reinterpret_cast<i::Object*>(message_obj_); |
| 2007 | ASSERT(message->IsJSMessageObject() || message->IsTheHole()); |
| 2008 | if (HasCaught() && !message->IsTheHole()) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 2009 | return v8::Utils::MessageToLocal(i::Handle<i::Object>(message, isolate_)); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2010 | } else { |
| 2011 | return v8::Local<v8::Message>(); |
| 2012 | } |
| 2013 | } |
| 2014 | |
| 2015 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2016 | void v8::TryCatch::Reset() { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 2017 | ASSERT(isolate_ == i::Isolate::Current()); |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 2018 | i::Object* the_hole = isolate_->heap()->the_hole_value(); |
| 2019 | exception_ = the_hole; |
| 2020 | message_obj_ = the_hole; |
| 2021 | message_script_ = the_hole; |
| 2022 | message_start_pos_ = 0; |
| 2023 | message_end_pos_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | |
| 2027 | void v8::TryCatch::SetVerbose(bool value) { |
| 2028 | is_verbose_ = value; |
| 2029 | } |
| 2030 | |
| 2031 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2032 | void v8::TryCatch::SetCaptureMessage(bool value) { |
| 2033 | capture_message_ = value; |
| 2034 | } |
| 2035 | |
| 2036 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2037 | // --- M e s s a g e --- |
| 2038 | |
| 2039 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2040 | Local<String> Message::Get() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2041 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2042 | ON_BAILOUT(isolate, "v8::Message::Get()", return Local<String>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2043 | ENTER_V8(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2044 | HandleScope scope(reinterpret_cast<Isolate*>(isolate)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2045 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 2046 | i::Handle<i::String> raw_result = i::MessageHandler::GetMessage(isolate, obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2047 | Local<String> result = Utils::ToLocal(raw_result); |
| 2048 | return scope.Close(result); |
| 2049 | } |
| 2050 | |
| 2051 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2052 | v8::Handle<Value> Message::GetScriptResourceName() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2053 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2054 | ENTER_V8(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2055 | HandleScope scope(reinterpret_cast<Isolate*>(isolate)); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 2056 | i::Handle<i::JSMessageObject> message = |
| 2057 | i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2058 | // Return this.script.name. |
| 2059 | i::Handle<i::JSValue> script = |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 2060 | i::Handle<i::JSValue>::cast(i::Handle<i::Object>(message->script(), |
| 2061 | isolate)); |
| 2062 | i::Handle<i::Object> resource_name(i::Script::cast(script->value())->name(), |
| 2063 | isolate); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2064 | return scope.Close(Utils::ToLocal(resource_name)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2068 | v8::Handle<Value> Message::GetScriptData() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2069 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2070 | ENTER_V8(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2071 | HandleScope scope(reinterpret_cast<Isolate*>(isolate)); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 2072 | i::Handle<i::JSMessageObject> message = |
| 2073 | i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2074 | // Return this.script.data. |
| 2075 | i::Handle<i::JSValue> script = |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 2076 | i::Handle<i::JSValue>::cast(i::Handle<i::Object>(message->script(), |
| 2077 | isolate)); |
| 2078 | i::Handle<i::Object> data(i::Script::cast(script->value())->data(), isolate); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 2079 | return scope.Close(Utils::ToLocal(data)); |
| 2080 | } |
| 2081 | |
| 2082 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 2083 | v8::Handle<v8::StackTrace> Message::GetStackTrace() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2084 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2085 | ENTER_V8(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2086 | HandleScope scope(reinterpret_cast<Isolate*>(isolate)); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 2087 | i::Handle<i::JSMessageObject> message = |
| 2088 | i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this)); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 2089 | i::Handle<i::Object> stackFramesObj(message->stack_frames(), isolate); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 2090 | if (!stackFramesObj->IsJSArray()) return v8::Handle<v8::StackTrace>(); |
| 2091 | i::Handle<i::JSArray> stackTrace = |
| 2092 | i::Handle<i::JSArray>::cast(stackFramesObj); |
| 2093 | return scope.Close(Utils::StackTraceToLocal(stackTrace)); |
| 2094 | } |
| 2095 | |
| 2096 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2097 | static i::Handle<i::Object> CallV8HeapFunction(const char* name, |
| 2098 | i::Handle<i::Object> recv, |
| 2099 | int argc, |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 2100 | i::Handle<i::Object> argv[], |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2101 | bool* has_pending_exception) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2102 | i::Isolate* isolate = i::Isolate::Current(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2103 | i::Handle<i::String> fmt_str = |
| 2104 | isolate->factory()->InternalizeUtf8String(name); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2105 | i::Object* object_fun = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2106 | isolate->js_builtins_object()->GetPropertyNoExceptionThrown(*fmt_str); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2107 | i::Handle<i::JSFunction> fun = |
| 2108 | i::Handle<i::JSFunction>(i::JSFunction::cast(object_fun)); |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 2109 | i::Handle<i::Object> value = i::Execution::Call( |
| 2110 | isolate, fun, recv, argc, argv, has_pending_exception); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2111 | return value; |
| 2112 | } |
| 2113 | |
| 2114 | |
| 2115 | static i::Handle<i::Object> CallV8HeapFunction(const char* name, |
| 2116 | i::Handle<i::Object> data, |
| 2117 | bool* has_pending_exception) { |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 2118 | i::Handle<i::Object> argv[] = { data }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2119 | return CallV8HeapFunction(name, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2120 | i::Isolate::Current()->js_builtins_object(), |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 2121 | ARRAY_SIZE(argv), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2122 | argv, |
| 2123 | has_pending_exception); |
| 2124 | } |
| 2125 | |
| 2126 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2127 | int Message::GetLineNumber() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2128 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2129 | ON_BAILOUT(isolate, "v8::Message::GetLineNumber()", return kNoLineNumberInfo); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2130 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2131 | i::HandleScope scope(isolate); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 2132 | |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2133 | EXCEPTION_PREAMBLE(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2134 | i::Handle<i::Object> result = CallV8HeapFunction("GetLineNumber", |
| 2135 | Utils::OpenHandle(this), |
| 2136 | &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2137 | EXCEPTION_BAILOUT_CHECK(isolate, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2138 | return static_cast<int>(result->Number()); |
| 2139 | } |
| 2140 | |
| 2141 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2142 | int Message::GetStartPosition() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2143 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2144 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2145 | i::HandleScope scope(isolate); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 2146 | i::Handle<i::JSMessageObject> message = |
| 2147 | i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this)); |
| 2148 | return message->start_position(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2152 | int Message::GetEndPosition() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2153 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2154 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2155 | i::HandleScope scope(isolate); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 2156 | i::Handle<i::JSMessageObject> message = |
| 2157 | i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this)); |
| 2158 | return message->end_position(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2162 | int Message::GetStartColumn() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2163 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2164 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2165 | i::HandleScope scope(isolate); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2166 | i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2167 | EXCEPTION_PREAMBLE(isolate); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2168 | i::Handle<i::Object> start_col_obj = CallV8HeapFunction( |
| 2169 | "GetPositionInLine", |
| 2170 | data_obj, |
| 2171 | &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2172 | EXCEPTION_BAILOUT_CHECK(isolate, 0); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2173 | return static_cast<int>(start_col_obj->Number()); |
| 2174 | } |
| 2175 | |
| 2176 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2177 | int Message::GetEndColumn() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2178 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2179 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2180 | i::HandleScope scope(isolate); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2181 | i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2182 | EXCEPTION_PREAMBLE(isolate); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2183 | i::Handle<i::Object> start_col_obj = CallV8HeapFunction( |
| 2184 | "GetPositionInLine", |
| 2185 | data_obj, |
| 2186 | &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2187 | EXCEPTION_BAILOUT_CHECK(isolate, 0); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 2188 | i::Handle<i::JSMessageObject> message = |
| 2189 | i::Handle<i::JSMessageObject>::cast(data_obj); |
| 2190 | int start = message->start_position(); |
| 2191 | int end = message->end_position(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2192 | return static_cast<int>(start_col_obj->Number()) + (end - start); |
| 2193 | } |
| 2194 | |
| 2195 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 2196 | bool Message::IsSharedCrossOrigin() const { |
| 2197 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 2198 | ENTER_V8(isolate); |
| 2199 | i::HandleScope scope(isolate); |
| 2200 | i::Handle<i::JSMessageObject> message = |
| 2201 | i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this)); |
| 2202 | i::Handle<i::JSValue> script = |
| 2203 | i::Handle<i::JSValue>::cast(i::Handle<i::Object>(message->script(), |
| 2204 | isolate)); |
| 2205 | return i::Script::cast(script->value())->is_shared_cross_origin(); |
| 2206 | } |
| 2207 | |
| 2208 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2209 | Local<String> Message::GetSourceLine() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2210 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2211 | ON_BAILOUT(isolate, "v8::Message::GetSourceLine()", return Local<String>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2212 | ENTER_V8(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2213 | HandleScope scope(reinterpret_cast<Isolate*>(isolate)); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2214 | EXCEPTION_PREAMBLE(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2215 | i::Handle<i::Object> result = CallV8HeapFunction("GetSourceLine", |
| 2216 | Utils::OpenHandle(this), |
| 2217 | &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2218 | EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::String>()); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2219 | if (result->IsString()) { |
| 2220 | return scope.Close(Utils::ToLocal(i::Handle<i::String>::cast(result))); |
| 2221 | } else { |
| 2222 | return Local<String>(); |
| 2223 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
| 2226 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2227 | void Message::PrintCurrentStackTrace(FILE* out) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2228 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2229 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2230 | isolate->PrintCurrentStackTrace(out); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2234 | // --- S t a c k T r a c e --- |
| 2235 | |
| 2236 | Local<StackFrame> StackTrace::GetFrame(uint32_t index) const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2237 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2238 | ENTER_V8(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2239 | HandleScope scope(reinterpret_cast<Isolate*>(isolate)); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2240 | i::Handle<i::JSArray> self = Utils::OpenHandle(this); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 2241 | i::Object* raw_object = self->GetElementNoExceptionThrown(isolate, index); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2242 | i::Handle<i::JSObject> obj(i::JSObject::cast(raw_object)); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2243 | return scope.Close(Utils::StackFrameToLocal(obj)); |
| 2244 | } |
| 2245 | |
| 2246 | |
| 2247 | int StackTrace::GetFrameCount() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2248 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2249 | ENTER_V8(isolate); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2250 | return i::Smi::cast(Utils::OpenHandle(this)->length())->value(); |
| 2251 | } |
| 2252 | |
| 2253 | |
| 2254 | Local<Array> StackTrace::AsArray() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2255 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2256 | ENTER_V8(isolate); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2257 | return Utils::ToLocal(Utils::OpenHandle(this)); |
| 2258 | } |
| 2259 | |
| 2260 | |
| 2261 | Local<StackTrace> StackTrace::CurrentStackTrace(int frame_limit, |
| 2262 | StackTraceOptions options) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2263 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2264 | ENTER_V8(isolate); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 2265 | i::Handle<i::JSArray> stackTrace = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2266 | isolate->CaptureCurrentStackTrace(frame_limit, options); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 2267 | return Utils::StackTraceToLocal(stackTrace); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | |
| 2271 | // --- S t a c k F r a m e --- |
| 2272 | |
| 2273 | int StackFrame::GetLineNumber() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2274 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2275 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2276 | i::HandleScope scope(isolate); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2277 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2278 | i::Handle<i::Object> line = GetProperty(self, "lineNumber"); |
| 2279 | if (!line->IsSmi()) { |
| 2280 | return Message::kNoLineNumberInfo; |
| 2281 | } |
| 2282 | return i::Smi::cast(*line)->value(); |
| 2283 | } |
| 2284 | |
| 2285 | |
| 2286 | int StackFrame::GetColumn() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2287 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2288 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2289 | i::HandleScope scope(isolate); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2290 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2291 | i::Handle<i::Object> column = GetProperty(self, "column"); |
| 2292 | if (!column->IsSmi()) { |
| 2293 | return Message::kNoColumnInfo; |
| 2294 | } |
| 2295 | return i::Smi::cast(*column)->value(); |
| 2296 | } |
| 2297 | |
| 2298 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 2299 | int StackFrame::GetScriptId() const { |
| 2300 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 2301 | ENTER_V8(isolate); |
| 2302 | i::HandleScope scope(isolate); |
| 2303 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2304 | i::Handle<i::Object> scriptId = GetProperty(self, "scriptId"); |
| 2305 | if (!scriptId->IsSmi()) { |
| 2306 | return Message::kNoScriptIdInfo; |
| 2307 | } |
| 2308 | return i::Smi::cast(*scriptId)->value(); |
| 2309 | } |
| 2310 | |
| 2311 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2312 | Local<String> StackFrame::GetScriptName() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2313 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2314 | ENTER_V8(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2315 | HandleScope scope(reinterpret_cast<Isolate*>(isolate)); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2316 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2317 | i::Handle<i::Object> name = GetProperty(self, "scriptName"); |
| 2318 | if (!name->IsString()) { |
| 2319 | return Local<String>(); |
| 2320 | } |
| 2321 | return scope.Close(Local<String>::Cast(Utils::ToLocal(name))); |
| 2322 | } |
| 2323 | |
| 2324 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 2325 | Local<String> StackFrame::GetScriptNameOrSourceURL() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2326 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2327 | ENTER_V8(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2328 | HandleScope scope(reinterpret_cast<Isolate*>(isolate)); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 2329 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2330 | i::Handle<i::Object> name = GetProperty(self, "scriptNameOrSourceURL"); |
| 2331 | if (!name->IsString()) { |
| 2332 | return Local<String>(); |
| 2333 | } |
| 2334 | return scope.Close(Local<String>::Cast(Utils::ToLocal(name))); |
| 2335 | } |
| 2336 | |
| 2337 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2338 | Local<String> StackFrame::GetFunctionName() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2339 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2340 | ENTER_V8(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2341 | HandleScope scope(reinterpret_cast<Isolate*>(isolate)); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2342 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2343 | i::Handle<i::Object> name = GetProperty(self, "functionName"); |
| 2344 | if (!name->IsString()) { |
| 2345 | return Local<String>(); |
| 2346 | } |
| 2347 | return scope.Close(Local<String>::Cast(Utils::ToLocal(name))); |
| 2348 | } |
| 2349 | |
| 2350 | |
| 2351 | bool StackFrame::IsEval() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2352 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2353 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2354 | i::HandleScope scope(isolate); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2355 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2356 | i::Handle<i::Object> is_eval = GetProperty(self, "isEval"); |
| 2357 | return is_eval->IsTrue(); |
| 2358 | } |
| 2359 | |
| 2360 | |
| 2361 | bool StackFrame::IsConstructor() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2362 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2363 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2364 | i::HandleScope scope(isolate); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 2365 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 2366 | i::Handle<i::Object> is_constructor = GetProperty(self, "isConstructor"); |
| 2367 | return is_constructor->IsTrue(); |
| 2368 | } |
| 2369 | |
| 2370 | |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 2371 | // --- J S O N --- |
| 2372 | |
| 2373 | Local<Value> JSON::Parse(Local<String> json_string) { |
| 2374 | i::Isolate* isolate = i::Isolate::Current(); |
| 2375 | EnsureInitializedForIsolate(isolate, "v8::JSON::Parse"); |
| 2376 | ENTER_V8(isolate); |
| 2377 | i::HandleScope scope(isolate); |
| 2378 | i::Handle<i::String> source = i::Handle<i::String>( |
| 2379 | FlattenGetString(Utils::OpenHandle(*json_string))); |
| 2380 | EXCEPTION_PREAMBLE(isolate); |
| 2381 | i::Handle<i::Object> result; |
| 2382 | if (source->IsSeqOneByteString()) { |
| 2383 | result = i::JsonParser<true>::Parse(source); |
| 2384 | } else { |
| 2385 | result = i::JsonParser<false>::Parse(source); |
| 2386 | } |
| 2387 | has_pending_exception = result.is_null(); |
| 2388 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>()); |
| 2389 | return Utils::ToLocal( |
| 2390 | i::Handle<i::Object>::cast(scope.CloseAndEscape(result))); |
| 2391 | } |
| 2392 | |
| 2393 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2394 | // --- D a t a --- |
| 2395 | |
svenpanne@chromium.org | fb04633 | 2012-04-19 12:02:44 +0000 | [diff] [blame] | 2396 | bool Value::FullIsUndefined() const { |
svenpanne@chromium.org | fb04633 | 2012-04-19 12:02:44 +0000 | [diff] [blame] | 2397 | bool result = Utils::OpenHandle(this)->IsUndefined(); |
| 2398 | ASSERT_EQ(result, QuickIsUndefined()); |
| 2399 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | |
svenpanne@chromium.org | fb04633 | 2012-04-19 12:02:44 +0000 | [diff] [blame] | 2403 | bool Value::FullIsNull() const { |
svenpanne@chromium.org | fb04633 | 2012-04-19 12:02:44 +0000 | [diff] [blame] | 2404 | bool result = Utils::OpenHandle(this)->IsNull(); |
| 2405 | ASSERT_EQ(result, QuickIsNull()); |
| 2406 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2410 | bool Value::IsTrue() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2411 | return Utils::OpenHandle(this)->IsTrue(); |
| 2412 | } |
| 2413 | |
| 2414 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2415 | bool Value::IsFalse() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2416 | return Utils::OpenHandle(this)->IsFalse(); |
| 2417 | } |
| 2418 | |
| 2419 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2420 | bool Value::IsFunction() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2421 | return Utils::OpenHandle(this)->IsJSFunction(); |
| 2422 | } |
| 2423 | |
| 2424 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2425 | bool Value::FullIsString() const { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2426 | bool result = Utils::OpenHandle(this)->IsString(); |
| 2427 | ASSERT_EQ(result, QuickIsString()); |
| 2428 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2432 | bool Value::IsSymbol() const { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2433 | return Utils::OpenHandle(this)->IsSymbol(); |
| 2434 | } |
| 2435 | |
| 2436 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2437 | bool Value::IsArray() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2438 | return Utils::OpenHandle(this)->IsJSArray(); |
| 2439 | } |
| 2440 | |
| 2441 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2442 | bool Value::IsArrayBuffer() const { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2443 | return Utils::OpenHandle(this)->IsJSArrayBuffer(); |
| 2444 | } |
| 2445 | |
| 2446 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2447 | bool Value::IsArrayBufferView() const { |
| 2448 | return Utils::OpenHandle(this)->IsJSArrayBufferView(); |
| 2449 | } |
| 2450 | |
| 2451 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2452 | bool Value::IsTypedArray() const { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2453 | return Utils::OpenHandle(this)->IsJSTypedArray(); |
| 2454 | } |
| 2455 | |
| 2456 | |
| 2457 | #define TYPED_ARRAY_LIST(F) \ |
| 2458 | F(Uint8Array, kExternalUnsignedByteArray) \ |
| 2459 | F(Int8Array, kExternalByteArray) \ |
| 2460 | F(Uint16Array, kExternalUnsignedShortArray) \ |
| 2461 | F(Int16Array, kExternalShortArray) \ |
| 2462 | F(Uint32Array, kExternalUnsignedIntArray) \ |
| 2463 | F(Int32Array, kExternalIntArray) \ |
| 2464 | F(Float32Array, kExternalFloatArray) \ |
| 2465 | F(Float64Array, kExternalDoubleArray) \ |
| 2466 | F(Uint8ClampedArray, kExternalPixelArray) |
| 2467 | |
| 2468 | |
| 2469 | #define VALUE_IS_TYPED_ARRAY(TypedArray, type_const) \ |
| 2470 | bool Value::Is##TypedArray() const { \ |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2471 | i::Handle<i::Object> obj = Utils::OpenHandle(this); \ |
| 2472 | if (!obj->IsJSTypedArray()) return false; \ |
| 2473 | return i::JSTypedArray::cast(*obj)->type() == type_const; \ |
| 2474 | } |
| 2475 | |
| 2476 | TYPED_ARRAY_LIST(VALUE_IS_TYPED_ARRAY) |
| 2477 | |
| 2478 | #undef VALUE_IS_TYPED_ARRAY |
| 2479 | |
| 2480 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2481 | bool Value::IsDataView() const { |
| 2482 | return Utils::OpenHandle(this)->IsJSDataView(); |
| 2483 | } |
| 2484 | |
| 2485 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2486 | bool Value::IsObject() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2487 | return Utils::OpenHandle(this)->IsJSObject(); |
| 2488 | } |
| 2489 | |
| 2490 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2491 | bool Value::IsNumber() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2492 | return Utils::OpenHandle(this)->IsNumber(); |
| 2493 | } |
| 2494 | |
| 2495 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2496 | bool Value::IsBoolean() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2497 | return Utils::OpenHandle(this)->IsBoolean(); |
| 2498 | } |
| 2499 | |
| 2500 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2501 | bool Value::IsExternal() const { |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 2502 | return Utils::OpenHandle(this)->IsExternal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2506 | bool Value::IsInt32() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2507 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2508 | if (obj->IsSmi()) return true; |
| 2509 | if (obj->IsNumber()) { |
| 2510 | double value = obj->Number(); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2511 | static const i::DoubleRepresentation minus_zero(-0.0); |
| 2512 | i::DoubleRepresentation rep(value); |
| 2513 | if (rep.bits == minus_zero.bits) { |
| 2514 | return false; |
| 2515 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2516 | return i::FastI2D(i::FastD2I(value)) == value; |
| 2517 | } |
| 2518 | return false; |
| 2519 | } |
| 2520 | |
| 2521 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 2522 | bool Value::IsUint32() const { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 2523 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2524 | if (obj->IsSmi()) return i::Smi::cast(*obj)->value() >= 0; |
| 2525 | if (obj->IsNumber()) { |
| 2526 | double value = obj->Number(); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2527 | static const i::DoubleRepresentation minus_zero(-0.0); |
| 2528 | i::DoubleRepresentation rep(value); |
| 2529 | if (rep.bits == minus_zero.bits) { |
| 2530 | return false; |
| 2531 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 2532 | return i::FastUI2D(i::FastD2UI(value)) == value; |
| 2533 | } |
| 2534 | return false; |
| 2535 | } |
| 2536 | |
| 2537 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2538 | bool Value::IsDate() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2539 | i::Isolate* isolate = i::Isolate::Current(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2540 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2541 | return obj->HasSpecificClassOf(isolate->heap()->Date_string()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
| 2544 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2545 | bool Value::IsStringObject() const { |
| 2546 | i::Isolate* isolate = i::Isolate::Current(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2547 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2548 | return obj->HasSpecificClassOf(isolate->heap()->String_string()); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
| 2551 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2552 | bool Value::IsSymbolObject() const { |
| 2553 | // TODO(svenpanne): these and other test functions should be written such |
| 2554 | // that they do not use Isolate::Current(). |
| 2555 | i::Isolate* isolate = i::Isolate::Current(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2556 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2557 | return obj->HasSpecificClassOf(isolate->heap()->Symbol_string()); |
| 2558 | } |
| 2559 | |
| 2560 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2561 | bool Value::IsNumberObject() const { |
| 2562 | i::Isolate* isolate = i::Isolate::Current(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2563 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2564 | return obj->HasSpecificClassOf(isolate->heap()->Number_string()); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
| 2567 | |
| 2568 | static i::Object* LookupBuiltin(i::Isolate* isolate, |
| 2569 | const char* builtin_name) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2570 | i::Handle<i::String> string = |
| 2571 | isolate->factory()->InternalizeUtf8String(builtin_name); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2572 | i::Handle<i::JSBuiltinsObject> builtins = isolate->js_builtins_object(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2573 | return builtins->GetPropertyNoExceptionThrown(*string); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | |
| 2577 | static bool CheckConstructor(i::Isolate* isolate, |
| 2578 | i::Handle<i::JSObject> obj, |
| 2579 | const char* class_name) { |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 2580 | i::Object* constr = obj->map()->constructor(); |
| 2581 | if (!constr->IsJSFunction()) return false; |
| 2582 | i::JSFunction* func = i::JSFunction::cast(constr); |
| 2583 | return func->shared()->native() && |
| 2584 | constr == LookupBuiltin(isolate, class_name); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | |
| 2588 | bool Value::IsNativeError() const { |
| 2589 | i::Isolate* isolate = i::Isolate::Current(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2590 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2591 | if (obj->IsJSObject()) { |
| 2592 | i::Handle<i::JSObject> js_obj(i::JSObject::cast(*obj)); |
| 2593 | return CheckConstructor(isolate, js_obj, "$Error") || |
| 2594 | CheckConstructor(isolate, js_obj, "$EvalError") || |
| 2595 | CheckConstructor(isolate, js_obj, "$RangeError") || |
| 2596 | CheckConstructor(isolate, js_obj, "$ReferenceError") || |
| 2597 | CheckConstructor(isolate, js_obj, "$SyntaxError") || |
| 2598 | CheckConstructor(isolate, js_obj, "$TypeError") || |
| 2599 | CheckConstructor(isolate, js_obj, "$URIError"); |
| 2600 | } else { |
| 2601 | return false; |
| 2602 | } |
| 2603 | } |
| 2604 | |
| 2605 | |
| 2606 | bool Value::IsBooleanObject() const { |
| 2607 | i::Isolate* isolate = i::Isolate::Current(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2608 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2609 | return obj->HasSpecificClassOf(isolate->heap()->Boolean_string()); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2613 | bool Value::IsRegExp() const { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2614 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2615 | return obj->IsJSRegExp(); |
| 2616 | } |
| 2617 | |
| 2618 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2619 | Local<String> Value::ToString() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2620 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2621 | i::Handle<i::Object> str; |
| 2622 | if (obj->IsString()) { |
| 2623 | str = obj; |
| 2624 | } else { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2625 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2626 | LOG_API(isolate, "ToString"); |
| 2627 | ENTER_V8(isolate); |
| 2628 | EXCEPTION_PREAMBLE(isolate); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 2629 | str = i::Execution::ToString(isolate, obj, &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2630 | EXCEPTION_BAILOUT_CHECK(isolate, Local<String>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2631 | } |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 2632 | return ToApiHandle<String>(str); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
| 2635 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2636 | Local<String> Value::ToDetailString() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2637 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2638 | i::Handle<i::Object> str; |
| 2639 | if (obj->IsString()) { |
| 2640 | str = obj; |
| 2641 | } else { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2642 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2643 | LOG_API(isolate, "ToDetailString"); |
| 2644 | ENTER_V8(isolate); |
| 2645 | EXCEPTION_PREAMBLE(isolate); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 2646 | str = i::Execution::ToDetailString(isolate, obj, &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2647 | EXCEPTION_BAILOUT_CHECK(isolate, Local<String>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2648 | } |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 2649 | return ToApiHandle<String>(str); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2653 | Local<v8::Object> Value::ToObject() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2654 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2655 | i::Handle<i::Object> val; |
| 2656 | if (obj->IsJSObject()) { |
| 2657 | val = obj; |
| 2658 | } else { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2659 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2660 | LOG_API(isolate, "ToObject"); |
| 2661 | ENTER_V8(isolate); |
| 2662 | EXCEPTION_PREAMBLE(isolate); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 2663 | val = i::Execution::ToObject(isolate, obj, &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2664 | EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2665 | } |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 2666 | return ToApiHandle<Object>(val); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
| 2669 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2670 | Local<Boolean> Value::ToBoolean() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2671 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 2672 | if (obj->IsBoolean()) { |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 2673 | return ToApiHandle<Boolean>(obj); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 2674 | } else { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2675 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2676 | LOG_API(isolate, "ToBoolean"); |
| 2677 | ENTER_V8(isolate); |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 2678 | i::Handle<i::Object> val = |
| 2679 | isolate->factory()->ToBoolean(obj->BooleanValue()); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 2680 | return ToApiHandle<Boolean>(val); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 2681 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2685 | Local<Number> Value::ToNumber() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2686 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2687 | i::Handle<i::Object> num; |
| 2688 | if (obj->IsNumber()) { |
| 2689 | num = obj; |
| 2690 | } else { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2691 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2692 | LOG_API(isolate, "ToNumber"); |
| 2693 | ENTER_V8(isolate); |
| 2694 | EXCEPTION_PREAMBLE(isolate); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 2695 | num = i::Execution::ToNumber(isolate, obj, &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2696 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Number>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2697 | } |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 2698 | return ToApiHandle<Number>(num); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2702 | Local<Integer> Value::ToInteger() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2703 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2704 | i::Handle<i::Object> num; |
| 2705 | if (obj->IsSmi()) { |
| 2706 | num = obj; |
| 2707 | } else { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2708 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2709 | LOG_API(isolate, "ToInteger"); |
| 2710 | ENTER_V8(isolate); |
| 2711 | EXCEPTION_PREAMBLE(isolate); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 2712 | num = i::Execution::ToInteger(isolate, obj, &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2713 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Integer>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2714 | } |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 2715 | return ToApiHandle<Integer>(num); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 2719 | void i::Internals::CheckInitializedImpl(v8::Isolate* external_isolate) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2720 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 2721 | ApiCheck(isolate != NULL && isolate->IsInitialized() && !isolate->IsDead(), |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2722 | "v8::internal::Internals::CheckInitialized()", |
| 2723 | "Isolate is not initialized or V8 has died"); |
| 2724 | } |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2725 | |
| 2726 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2727 | void External::CheckCast(v8::Value* that) { |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 2728 | ApiCheck(Utils::OpenHandle(that)->IsExternal(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2729 | "v8::External::Cast()", |
| 2730 | "Could not convert to external"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2734 | void v8::Object::CheckCast(Value* that) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2735 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2736 | ApiCheck(obj->IsJSObject(), |
| 2737 | "v8::Object::Cast()", |
| 2738 | "Could not convert to object"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
| 2741 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2742 | void v8::Function::CheckCast(Value* that) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2743 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2744 | ApiCheck(obj->IsJSFunction(), |
| 2745 | "v8::Function::Cast()", |
| 2746 | "Could not convert to function"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
| 2749 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2750 | void v8::String::CheckCast(v8::Value* that) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2751 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2752 | ApiCheck(obj->IsString(), |
| 2753 | "v8::String::Cast()", |
| 2754 | "Could not convert to string"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2755 | } |
| 2756 | |
| 2757 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2758 | void v8::Symbol::CheckCast(v8::Value* that) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2759 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2760 | ApiCheck(obj->IsSymbol(), |
| 2761 | "v8::Symbol::Cast()", |
| 2762 | "Could not convert to symbol"); |
| 2763 | } |
| 2764 | |
| 2765 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2766 | void v8::Number::CheckCast(v8::Value* that) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2767 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2768 | ApiCheck(obj->IsNumber(), |
| 2769 | "v8::Number::Cast()", |
| 2770 | "Could not convert to number"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2774 | void v8::Integer::CheckCast(v8::Value* that) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2775 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2776 | ApiCheck(obj->IsNumber(), |
| 2777 | "v8::Integer::Cast()", |
| 2778 | "Could not convert to number"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
| 2781 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2782 | void v8::Array::CheckCast(Value* that) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2783 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2784 | ApiCheck(obj->IsJSArray(), |
| 2785 | "v8::Array::Cast()", |
| 2786 | "Could not convert to array"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2790 | void v8::ArrayBuffer::CheckCast(Value* that) { |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2791 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2792 | ApiCheck(obj->IsJSArrayBuffer(), |
| 2793 | "v8::ArrayBuffer::Cast()", |
| 2794 | "Could not convert to ArrayBuffer"); |
| 2795 | } |
| 2796 | |
| 2797 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2798 | void v8::ArrayBufferView::CheckCast(Value* that) { |
| 2799 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2800 | ApiCheck(obj->IsJSArrayBufferView(), |
| 2801 | "v8::ArrayBufferView::Cast()", |
| 2802 | "Could not convert to ArrayBufferView"); |
| 2803 | } |
| 2804 | |
| 2805 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2806 | void v8::TypedArray::CheckCast(Value* that) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2807 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2808 | ApiCheck(obj->IsJSTypedArray(), |
| 2809 | "v8::TypedArray::Cast()", |
| 2810 | "Could not convert to TypedArray"); |
| 2811 | } |
| 2812 | |
| 2813 | |
| 2814 | #define CHECK_TYPED_ARRAY_CAST(ApiClass, typeConst) \ |
| 2815 | void v8::ApiClass::CheckCast(Value* that) { \ |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2816 | i::Handle<i::Object> obj = Utils::OpenHandle(that); \ |
| 2817 | ApiCheck(obj->IsJSTypedArray() && \ |
| 2818 | i::JSTypedArray::cast(*obj)->type() == typeConst, \ |
| 2819 | "v8::" #ApiClass "::Cast()", \ |
| 2820 | "Could not convert to " #ApiClass); \ |
| 2821 | } |
| 2822 | |
| 2823 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2824 | TYPED_ARRAY_LIST(CHECK_TYPED_ARRAY_CAST) |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2825 | |
| 2826 | #undef CHECK_TYPED_ARRAY_CAST |
| 2827 | |
| 2828 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2829 | void v8::DataView::CheckCast(Value* that) { |
| 2830 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2831 | ApiCheck(obj->IsJSDataView(), |
| 2832 | "v8::DataView::Cast()", |
| 2833 | "Could not convert to DataView"); |
| 2834 | } |
| 2835 | |
| 2836 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2837 | void v8::Date::CheckCast(v8::Value* that) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2838 | i::Isolate* isolate = i::Isolate::Current(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2839 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2840 | ApiCheck(obj->HasSpecificClassOf(isolate->heap()->Date_string()), |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2841 | "v8::Date::Cast()", |
| 2842 | "Could not convert to date"); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
| 2845 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2846 | void v8::StringObject::CheckCast(v8::Value* that) { |
| 2847 | i::Isolate* isolate = i::Isolate::Current(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2848 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2849 | ApiCheck(obj->HasSpecificClassOf(isolate->heap()->String_string()), |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2850 | "v8::StringObject::Cast()", |
| 2851 | "Could not convert to StringObject"); |
| 2852 | } |
| 2853 | |
| 2854 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2855 | void v8::SymbolObject::CheckCast(v8::Value* that) { |
| 2856 | i::Isolate* isolate = i::Isolate::Current(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2857 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2858 | ApiCheck(obj->HasSpecificClassOf(isolate->heap()->Symbol_string()), |
| 2859 | "v8::SymbolObject::Cast()", |
| 2860 | "Could not convert to SymbolObject"); |
| 2861 | } |
| 2862 | |
| 2863 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2864 | void v8::NumberObject::CheckCast(v8::Value* that) { |
| 2865 | i::Isolate* isolate = i::Isolate::Current(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2866 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2867 | ApiCheck(obj->HasSpecificClassOf(isolate->heap()->Number_string()), |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2868 | "v8::NumberObject::Cast()", |
| 2869 | "Could not convert to NumberObject"); |
| 2870 | } |
| 2871 | |
| 2872 | |
| 2873 | void v8::BooleanObject::CheckCast(v8::Value* that) { |
| 2874 | i::Isolate* isolate = i::Isolate::Current(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2875 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2876 | ApiCheck(obj->HasSpecificClassOf(isolate->heap()->Boolean_string()), |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 2877 | "v8::BooleanObject::Cast()", |
| 2878 | "Could not convert to BooleanObject"); |
| 2879 | } |
| 2880 | |
| 2881 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 2882 | void v8::RegExp::CheckCast(v8::Value* that) { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 2883 | i::Handle<i::Object> obj = Utils::OpenHandle(that); |
| 2884 | ApiCheck(obj->IsJSRegExp(), |
| 2885 | "v8::RegExp::Cast()", |
| 2886 | "Could not convert to regular expression"); |
| 2887 | } |
| 2888 | |
| 2889 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2890 | bool Value::BooleanValue() const { |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 2891 | return Utils::OpenHandle(this)->BooleanValue(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2895 | double Value::NumberValue() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2896 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2897 | i::Handle<i::Object> num; |
| 2898 | if (obj->IsNumber()) { |
| 2899 | num = obj; |
| 2900 | } else { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2901 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2902 | LOG_API(isolate, "NumberValue"); |
| 2903 | ENTER_V8(isolate); |
| 2904 | EXCEPTION_PREAMBLE(isolate); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 2905 | num = i::Execution::ToNumber(isolate, obj, &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2906 | EXCEPTION_BAILOUT_CHECK(isolate, i::OS::nan_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2907 | } |
| 2908 | return num->Number(); |
| 2909 | } |
| 2910 | |
| 2911 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2912 | int64_t Value::IntegerValue() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2913 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2914 | i::Handle<i::Object> num; |
| 2915 | if (obj->IsNumber()) { |
| 2916 | num = obj; |
| 2917 | } else { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2918 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2919 | LOG_API(isolate, "IntegerValue"); |
| 2920 | ENTER_V8(isolate); |
| 2921 | EXCEPTION_PREAMBLE(isolate); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 2922 | num = i::Execution::ToInteger(isolate, obj, &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2923 | EXCEPTION_BAILOUT_CHECK(isolate, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2924 | } |
| 2925 | if (num->IsSmi()) { |
| 2926 | return i::Smi::cast(*num)->value(); |
| 2927 | } else { |
| 2928 | return static_cast<int64_t>(num->Number()); |
| 2929 | } |
| 2930 | } |
| 2931 | |
| 2932 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2933 | Local<Int32> Value::ToInt32() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2934 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2935 | i::Handle<i::Object> num; |
| 2936 | if (obj->IsSmi()) { |
| 2937 | num = obj; |
| 2938 | } else { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2939 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2940 | LOG_API(isolate, "ToInt32"); |
| 2941 | ENTER_V8(isolate); |
| 2942 | EXCEPTION_PREAMBLE(isolate); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 2943 | num = i::Execution::ToInt32(isolate, obj, &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2944 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Int32>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2945 | } |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 2946 | return ToApiHandle<Int32>(num); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2950 | Local<Uint32> Value::ToUint32() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2951 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2952 | i::Handle<i::Object> num; |
| 2953 | if (obj->IsSmi()) { |
| 2954 | num = obj; |
| 2955 | } else { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2956 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2957 | LOG_API(isolate, "ToUInt32"); |
| 2958 | ENTER_V8(isolate); |
| 2959 | EXCEPTION_PREAMBLE(isolate); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 2960 | num = i::Execution::ToUint32(isolate, obj, &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2961 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Uint32>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2962 | } |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 2963 | return ToApiHandle<Uint32>(num); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2967 | Local<Uint32> Value::ToArrayIndex() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2968 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2969 | if (obj->IsSmi()) { |
| 2970 | if (i::Smi::cast(*obj)->value() >= 0) return Utils::Uint32ToLocal(obj); |
| 2971 | return Local<Uint32>(); |
| 2972 | } |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2973 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2974 | LOG_API(isolate, "ToArrayIndex"); |
| 2975 | ENTER_V8(isolate); |
| 2976 | EXCEPTION_PREAMBLE(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2977 | i::Handle<i::Object> string_obj = |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 2978 | i::Execution::ToString(isolate, obj, &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2979 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Uint32>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2980 | i::Handle<i::String> str = i::Handle<i::String>::cast(string_obj); |
| 2981 | uint32_t index; |
| 2982 | if (str->AsArrayIndex(&index)) { |
| 2983 | i::Handle<i::Object> value; |
| 2984 | if (index <= static_cast<uint32_t>(i::Smi::kMaxValue)) { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 2985 | value = i::Handle<i::Object>(i::Smi::FromInt(index), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2986 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2987 | value = isolate->factory()->NewNumber(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2988 | } |
| 2989 | return Utils::Uint32ToLocal(value); |
| 2990 | } |
| 2991 | return Local<Uint32>(); |
| 2992 | } |
| 2993 | |
| 2994 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2995 | int32_t Value::Int32Value() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2996 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 2997 | if (obj->IsSmi()) { |
| 2998 | return i::Smi::cast(*obj)->value(); |
| 2999 | } else { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3000 | i::Isolate* isolate = i::Isolate::Current(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3001 | LOG_API(isolate, "Int32Value (slow)"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3002 | ENTER_V8(isolate); |
| 3003 | EXCEPTION_PREAMBLE(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3004 | i::Handle<i::Object> num = |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 3005 | i::Execution::ToInt32(isolate, obj, &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3006 | EXCEPTION_BAILOUT_CHECK(isolate, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3007 | if (num->IsSmi()) { |
| 3008 | return i::Smi::cast(*num)->value(); |
| 3009 | } else { |
| 3010 | return static_cast<int32_t>(num->Number()); |
| 3011 | } |
| 3012 | } |
| 3013 | } |
| 3014 | |
| 3015 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3016 | bool Value::Equals(Handle<Value> that) const { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3017 | i::Isolate* isolate = i::Isolate::Current(); |
machenbach@chromium.org | ae16103 | 2013-09-24 09:12:30 +0000 | [diff] [blame] | 3018 | if (EmptyCheck("v8::Value::Equals()", this) || |
| 3019 | EmptyCheck("v8::Value::Equals()", that)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3020 | return false; |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3021 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3022 | LOG_API(isolate, "Equals"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3023 | ENTER_V8(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3024 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 3025 | i::Handle<i::Object> other = Utils::OpenHandle(*that); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 3026 | // If both obj and other are JSObjects, we'd better compare by identity |
| 3027 | // immediately when going into JS builtin. The reason is Invoke |
| 3028 | // would overwrite global object receiver with global proxy. |
| 3029 | if (obj->IsJSObject() && other->IsJSObject()) { |
| 3030 | return *obj == *other; |
| 3031 | } |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 3032 | i::Handle<i::Object> args[] = { other }; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3033 | EXCEPTION_PREAMBLE(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3034 | i::Handle<i::Object> result = |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 3035 | CallV8HeapFunction("EQUALS", obj, ARRAY_SIZE(args), args, |
| 3036 | &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3037 | EXCEPTION_BAILOUT_CHECK(isolate, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3038 | return *result == i::Smi::FromInt(i::EQUAL); |
| 3039 | } |
| 3040 | |
| 3041 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3042 | bool Value::StrictEquals(Handle<Value> that) const { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3043 | i::Isolate* isolate = i::Isolate::Current(); |
machenbach@chromium.org | ae16103 | 2013-09-24 09:12:30 +0000 | [diff] [blame] | 3044 | if (EmptyCheck("v8::Value::StrictEquals()", this) || |
| 3045 | EmptyCheck("v8::Value::StrictEquals()", that)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3046 | return false; |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 3047 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3048 | LOG_API(isolate, "StrictEquals"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3049 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 3050 | i::Handle<i::Object> other = Utils::OpenHandle(*that); |
| 3051 | // Must check HeapNumber first, since NaN !== NaN. |
| 3052 | if (obj->IsHeapNumber()) { |
| 3053 | if (!other->IsNumber()) return false; |
| 3054 | double x = obj->Number(); |
| 3055 | double y = other->Number(); |
| 3056 | // Must check explicitly for NaN:s on Windows, but -0 works fine. |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 3057 | return x == y && !std::isnan(x) && !std::isnan(y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3058 | } else if (*obj == *other) { // Also covers Booleans. |
| 3059 | return true; |
| 3060 | } else if (obj->IsSmi()) { |
| 3061 | return other->IsNumber() && obj->Number() == other->Number(); |
| 3062 | } else if (obj->IsString()) { |
| 3063 | return other->IsString() && |
| 3064 | i::String::cast(*obj)->Equals(i::String::cast(*other)); |
| 3065 | } else if (obj->IsUndefined() || obj->IsUndetectableObject()) { |
| 3066 | return other->IsUndefined() || other->IsUndetectableObject(); |
| 3067 | } else { |
| 3068 | return false; |
| 3069 | } |
| 3070 | } |
| 3071 | |
| 3072 | |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 3073 | bool Value::SameValue(Handle<Value> that) const { |
| 3074 | i::Isolate* isolate = i::Isolate::Current(); |
| 3075 | if (EmptyCheck("v8::Value::SameValue()", this) || |
| 3076 | EmptyCheck("v8::Value::SameValue()", that)) { |
| 3077 | return false; |
| 3078 | } |
| 3079 | LOG_API(isolate, "SameValue"); |
| 3080 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 3081 | i::Handle<i::Object> other = Utils::OpenHandle(*that); |
| 3082 | return obj->SameValue(*other); |
| 3083 | } |
| 3084 | |
| 3085 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3086 | uint32_t Value::Uint32Value() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3087 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 3088 | if (obj->IsSmi()) { |
| 3089 | return i::Smi::cast(*obj)->value(); |
| 3090 | } else { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3091 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3092 | LOG_API(isolate, "Uint32Value"); |
| 3093 | ENTER_V8(isolate); |
| 3094 | EXCEPTION_PREAMBLE(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3095 | i::Handle<i::Object> num = |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 3096 | i::Execution::ToUint32(isolate, obj, &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3097 | EXCEPTION_BAILOUT_CHECK(isolate, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3098 | if (num->IsSmi()) { |
| 3099 | return i::Smi::cast(*num)->value(); |
| 3100 | } else { |
| 3101 | return static_cast<uint32_t>(num->Number()); |
| 3102 | } |
| 3103 | } |
| 3104 | } |
| 3105 | |
| 3106 | |
| 3107 | bool v8::Object::Set(v8::Handle<Value> key, v8::Handle<Value> value, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3108 | v8::PropertyAttribute attribs) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3109 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3110 | ON_BAILOUT(isolate, "v8::Object::Set()", return false); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3111 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3112 | i::HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3113 | i::Handle<i::Object> self = Utils::OpenHandle(this); |
| 3114 | i::Handle<i::Object> key_obj = Utils::OpenHandle(*key); |
| 3115 | i::Handle<i::Object> value_obj = Utils::OpenHandle(*value); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3116 | EXCEPTION_PREAMBLE(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3117 | i::Handle<i::Object> obj = i::SetProperty( |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 3118 | isolate, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3119 | self, |
| 3120 | key_obj, |
| 3121 | value_obj, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3122 | static_cast<PropertyAttributes>(attribs), |
| 3123 | i::kNonStrictMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3124 | has_pending_exception = obj.is_null(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3125 | EXCEPTION_BAILOUT_CHECK(isolate, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3126 | return true; |
| 3127 | } |
| 3128 | |
| 3129 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3130 | bool v8::Object::Set(uint32_t index, v8::Handle<Value> value) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3131 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3132 | ON_BAILOUT(isolate, "v8::Object::Set()", return false); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3133 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3134 | i::HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3135 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3136 | i::Handle<i::Object> value_obj = Utils::OpenHandle(*value); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3137 | EXCEPTION_PREAMBLE(isolate); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 3138 | i::Handle<i::Object> obj = i::JSObject::SetElement( |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3139 | self, |
| 3140 | index, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3141 | value_obj, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 3142 | NONE, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3143 | i::kNonStrictMode); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3144 | has_pending_exception = obj.is_null(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3145 | EXCEPTION_BAILOUT_CHECK(isolate, false); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3146 | return true; |
| 3147 | } |
| 3148 | |
| 3149 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3150 | bool v8::Object::ForceSet(v8::Handle<Value> key, |
| 3151 | v8::Handle<Value> value, |
| 3152 | v8::PropertyAttribute attribs) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3153 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3154 | ON_BAILOUT(isolate, "v8::Object::ForceSet()", return false); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3155 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3156 | i::HandleScope scope(isolate); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3157 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3158 | i::Handle<i::Object> key_obj = Utils::OpenHandle(*key); |
| 3159 | i::Handle<i::Object> value_obj = Utils::OpenHandle(*value); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3160 | EXCEPTION_PREAMBLE(isolate); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3161 | i::Handle<i::Object> obj = i::ForceSetProperty( |
| 3162 | self, |
| 3163 | key_obj, |
| 3164 | value_obj, |
| 3165 | static_cast<PropertyAttributes>(attribs)); |
| 3166 | has_pending_exception = obj.is_null(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3167 | EXCEPTION_BAILOUT_CHECK(isolate, false); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3168 | return true; |
| 3169 | } |
| 3170 | |
| 3171 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 3172 | bool v8::Object::ForceDelete(v8::Handle<Value> key) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3173 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3174 | ON_BAILOUT(isolate, "v8::Object::ForceDelete()", return false); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3175 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3176 | i::HandleScope scope(isolate); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 3177 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3178 | i::Handle<i::Object> key_obj = Utils::OpenHandle(*key); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3179 | |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 3180 | // When deleting a property on the global object using ForceDelete |
| 3181 | // deoptimize all functions as optimized code does not check for the hole |
| 3182 | // value with DontDelete properties. We have to deoptimize all contexts |
| 3183 | // because of possible cross-context inlined functions. |
| 3184 | if (self->IsJSGlobalProxy() || self->IsGlobalObject()) { |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 3185 | i::Deoptimizer::DeoptimizeAll(isolate); |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 3186 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3187 | |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3188 | EXCEPTION_PREAMBLE(isolate); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 3189 | i::Handle<i::Object> obj = i::ForceDeleteProperty(self, key_obj); |
| 3190 | has_pending_exception = obj.is_null(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3191 | EXCEPTION_BAILOUT_CHECK(isolate, false); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 3192 | return obj->IsTrue(); |
| 3193 | } |
| 3194 | |
| 3195 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3196 | Local<Value> v8::Object::Get(v8::Handle<Value> key) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3197 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3198 | ON_BAILOUT(isolate, "v8::Object::Get()", return Local<v8::Value>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3199 | ENTER_V8(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3200 | i::Handle<i::Object> self = Utils::OpenHandle(this); |
| 3201 | i::Handle<i::Object> key_obj = Utils::OpenHandle(*key); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3202 | EXCEPTION_PREAMBLE(isolate); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3203 | i::Handle<i::Object> result = i::GetProperty(isolate, self, key_obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3204 | has_pending_exception = result.is_null(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3205 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3206 | return Utils::ToLocal(result); |
| 3207 | } |
| 3208 | |
| 3209 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3210 | Local<Value> v8::Object::Get(uint32_t index) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3211 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3212 | ON_BAILOUT(isolate, "v8::Object::Get()", return Local<v8::Value>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3213 | ENTER_V8(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3214 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3215 | EXCEPTION_PREAMBLE(isolate); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3216 | i::Handle<i::Object> result = i::Object::GetElement(isolate, self, index); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3217 | has_pending_exception = result.is_null(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3218 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3219 | return Utils::ToLocal(result); |
| 3220 | } |
| 3221 | |
| 3222 | |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3223 | PropertyAttribute v8::Object::GetPropertyAttributes(v8::Handle<Value> key) { |
| 3224 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3225 | ON_BAILOUT(isolate, "v8::Object::GetPropertyAttribute()", |
| 3226 | return static_cast<PropertyAttribute>(NONE)); |
| 3227 | ENTER_V8(isolate); |
| 3228 | i::HandleScope scope(isolate); |
| 3229 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3230 | i::Handle<i::Object> key_obj = Utils::OpenHandle(*key); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3231 | if (!key_obj->IsName()) { |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3232 | EXCEPTION_PREAMBLE(isolate); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 3233 | key_obj = i::Execution::ToString(isolate, key_obj, &has_pending_exception); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3234 | EXCEPTION_BAILOUT_CHECK(isolate, static_cast<PropertyAttribute>(NONE)); |
| 3235 | } |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3236 | i::Handle<i::Name> key_name = i::Handle<i::Name>::cast(key_obj); |
| 3237 | PropertyAttributes result = self->GetPropertyAttribute(*key_name); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3238 | if (result == ABSENT) return static_cast<PropertyAttribute>(NONE); |
| 3239 | return static_cast<PropertyAttribute>(result); |
| 3240 | } |
| 3241 | |
| 3242 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3243 | Local<Value> v8::Object::GetPrototype() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3244 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3245 | ON_BAILOUT(isolate, "v8::Object::GetPrototype()", |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3246 | return Local<v8::Value>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3247 | ENTER_V8(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3248 | i::Handle<i::Object> self = Utils::OpenHandle(this); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 3249 | i::Handle<i::Object> result(self->GetPrototype(isolate), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3250 | return Utils::ToLocal(result); |
| 3251 | } |
| 3252 | |
| 3253 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3254 | bool v8::Object::SetPrototype(Handle<Value> value) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3255 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3256 | ON_BAILOUT(isolate, "v8::Object::SetPrototype()", return false); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3257 | ENTER_V8(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3258 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3259 | i::Handle<i::Object> value_obj = Utils::OpenHandle(*value); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 3260 | // We do not allow exceptions thrown while setting the prototype |
| 3261 | // to propagate outside. |
| 3262 | TryCatch try_catch; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3263 | EXCEPTION_PREAMBLE(isolate); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 3264 | i::Handle<i::Object> result = i::JSObject::SetPrototype(self, value_obj); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3265 | has_pending_exception = result.is_null(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3266 | EXCEPTION_BAILOUT_CHECK(isolate, false); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3267 | return true; |
| 3268 | } |
| 3269 | |
| 3270 | |
sgjesse@chromium.org | 900d3b7 | 2009-08-07 11:24:25 +0000 | [diff] [blame] | 3271 | Local<Object> v8::Object::FindInstanceInPrototypeChain( |
| 3272 | v8::Handle<FunctionTemplate> tmpl) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3273 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3274 | ON_BAILOUT(isolate, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3275 | "v8::Object::FindInstanceInPrototypeChain()", |
sgjesse@chromium.org | 900d3b7 | 2009-08-07 11:24:25 +0000 | [diff] [blame] | 3276 | return Local<v8::Object>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3277 | ENTER_V8(isolate); |
sgjesse@chromium.org | 900d3b7 | 2009-08-07 11:24:25 +0000 | [diff] [blame] | 3278 | i::JSObject* object = *Utils::OpenHandle(this); |
| 3279 | i::FunctionTemplateInfo* tmpl_info = *Utils::OpenHandle(*tmpl); |
| 3280 | while (!object->IsInstanceOf(tmpl_info)) { |
| 3281 | i::Object* prototype = object->GetPrototype(); |
| 3282 | if (!prototype->IsJSObject()) return Local<Object>(); |
| 3283 | object = i::JSObject::cast(prototype); |
| 3284 | } |
| 3285 | return Utils::ToLocal(i::Handle<i::JSObject>(object)); |
| 3286 | } |
| 3287 | |
| 3288 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3289 | Local<Array> v8::Object::GetPropertyNames() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3290 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3291 | ON_BAILOUT(isolate, "v8::Object::GetPropertyNames()", |
| 3292 | return Local<v8::Array>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3293 | ENTER_V8(isolate); |
| 3294 | i::HandleScope scope(isolate); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3295 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3296 | bool threw = false; |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 3297 | i::Handle<i::FixedArray> value = |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3298 | i::GetKeysInFixedArrayFor(self, i::INCLUDE_PROTOS, &threw); |
| 3299 | if (threw) return Local<v8::Array>(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3300 | // Because we use caching to speed up enumeration it is important |
| 3301 | // to never change the result of the basic enumeration function so |
| 3302 | // we clone the result. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3303 | i::Handle<i::FixedArray> elms = isolate->factory()->CopyFixedArray(value); |
| 3304 | i::Handle<i::JSArray> result = |
| 3305 | isolate->factory()->NewJSArrayWithElements(elms); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3306 | return Utils::ToLocal(scope.CloseAndEscape(result)); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3310 | Local<Array> v8::Object::GetOwnPropertyNames() { |
| 3311 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3312 | ON_BAILOUT(isolate, "v8::Object::GetOwnPropertyNames()", |
| 3313 | return Local<v8::Array>()); |
| 3314 | ENTER_V8(isolate); |
| 3315 | i::HandleScope scope(isolate); |
| 3316 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3317 | bool threw = false; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3318 | i::Handle<i::FixedArray> value = |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3319 | i::GetKeysInFixedArrayFor(self, i::LOCAL_ONLY, &threw); |
| 3320 | if (threw) return Local<v8::Array>(); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3321 | // Because we use caching to speed up enumeration it is important |
| 3322 | // to never change the result of the basic enumeration function so |
| 3323 | // we clone the result. |
| 3324 | i::Handle<i::FixedArray> elms = isolate->factory()->CopyFixedArray(value); |
| 3325 | i::Handle<i::JSArray> result = |
| 3326 | isolate->factory()->NewJSArrayWithElements(elms); |
| 3327 | return Utils::ToLocal(scope.CloseAndEscape(result)); |
| 3328 | } |
| 3329 | |
| 3330 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3331 | Local<String> v8::Object::ObjectProtoToString() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3332 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3333 | ON_BAILOUT(isolate, "v8::Object::ObjectProtoToString()", |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3334 | return Local<v8::String>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3335 | ENTER_V8(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3336 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3337 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3338 | i::Handle<i::Object> name(self->class_name(), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3339 | |
| 3340 | // Native implementation of Object.prototype.toString (v8natives.js): |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3341 | // var c = %_ClassOf(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3342 | // if (c === 'Arguments') c = 'Object'; |
| 3343 | // return "[object " + c + "]"; |
| 3344 | |
| 3345 | if (!name->IsString()) { |
| 3346 | return v8::String::New("[object ]"); |
| 3347 | |
| 3348 | } else { |
| 3349 | i::Handle<i::String> class_name = i::Handle<i::String>::cast(name); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 3350 | if (class_name->IsOneByteEqualTo(STATIC_ASCII_VECTOR("Arguments"))) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3351 | return v8::String::New("[object Object]"); |
| 3352 | |
| 3353 | } else { |
| 3354 | const char* prefix = "[object "; |
| 3355 | Local<String> str = Utils::ToLocal(class_name); |
| 3356 | const char* postfix = "]"; |
| 3357 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3358 | int prefix_len = i::StrLength(prefix); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 3359 | int str_len = str->Utf8Length(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3360 | int postfix_len = i::StrLength(postfix); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3361 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3362 | int buf_len = prefix_len + str_len + postfix_len; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 3363 | i::ScopedVector<char> buf(buf_len); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3364 | |
| 3365 | // Write prefix. |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 3366 | char* ptr = buf.start(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3367 | i::OS::MemCopy(ptr, prefix, prefix_len * v8::internal::kCharSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3368 | ptr += prefix_len; |
| 3369 | |
| 3370 | // Write real content. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 3371 | str->WriteUtf8(ptr, str_len); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3372 | ptr += str_len; |
| 3373 | |
| 3374 | // Write postfix. |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3375 | i::OS::MemCopy(ptr, postfix, postfix_len * v8::internal::kCharSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3376 | |
| 3377 | // Copy the buffer into a heap-allocated string and return it. |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 3378 | Local<String> result = v8::String::New(buf.start(), buf_len); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3379 | return result; |
| 3380 | } |
| 3381 | } |
| 3382 | } |
| 3383 | |
| 3384 | |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 3385 | Local<Value> v8::Object::GetConstructor() { |
| 3386 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3387 | ON_BAILOUT(isolate, "v8::Object::GetConstructor()", |
| 3388 | return Local<v8::Function>()); |
| 3389 | ENTER_V8(isolate); |
| 3390 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3391 | i::Handle<i::Object> constructor(self->GetConstructor(), isolate); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 3392 | return Utils::ToLocal(constructor); |
| 3393 | } |
| 3394 | |
| 3395 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 3396 | Local<String> v8::Object::GetConstructorName() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3397 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3398 | ON_BAILOUT(isolate, "v8::Object::GetConstructorName()", |
| 3399 | return Local<v8::String>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3400 | ENTER_V8(isolate); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 3401 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3402 | i::Handle<i::String> name(self->constructor_name()); |
| 3403 | return Utils::ToLocal(name); |
| 3404 | } |
| 3405 | |
| 3406 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3407 | bool v8::Object::Delete(v8::Handle<Value> key) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3408 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3409 | ON_BAILOUT(isolate, "v8::Object::Delete()", return false); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3410 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3411 | i::HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3412 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3413 | i::Handle<i::Object> key_obj = Utils::OpenHandle(*key); |
| 3414 | EXCEPTION_PREAMBLE(isolate); |
| 3415 | i::Handle<i::Object> obj = i::DeleteProperty(self, key_obj); |
| 3416 | has_pending_exception = obj.is_null(); |
| 3417 | EXCEPTION_BAILOUT_CHECK(isolate, false); |
| 3418 | return obj->IsTrue(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
| 3421 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3422 | bool v8::Object::Has(v8::Handle<Value> key) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3423 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3424 | ON_BAILOUT(isolate, "v8::Object::Has()", return false); |
| 3425 | ENTER_V8(isolate); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3426 | i::Handle<i::JSReceiver> self = Utils::OpenHandle(this); |
| 3427 | i::Handle<i::Object> key_obj = Utils::OpenHandle(*key); |
| 3428 | EXCEPTION_PREAMBLE(isolate); |
| 3429 | i::Handle<i::Object> obj = i::HasProperty(self, key_obj); |
| 3430 | has_pending_exception = obj.is_null(); |
| 3431 | EXCEPTION_BAILOUT_CHECK(isolate, false); |
| 3432 | return obj->IsTrue(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3433 | } |
| 3434 | |
| 3435 | |
| 3436 | bool v8::Object::Delete(uint32_t index) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3437 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3438 | ON_BAILOUT(isolate, "v8::Object::DeleteProperty()", |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3439 | return false); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3440 | ENTER_V8(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3441 | HandleScope scope(reinterpret_cast<Isolate*>(isolate)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3442 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 3443 | return i::JSReceiver::DeleteElement(self, index)->IsTrue(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3444 | } |
| 3445 | |
| 3446 | |
| 3447 | bool v8::Object::Has(uint32_t index) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3448 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3449 | ON_BAILOUT(isolate, "v8::Object::HasProperty()", return false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3450 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3451 | return i::JSReceiver::HasElement(self, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3452 | } |
| 3453 | |
| 3454 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 3455 | template<typename Setter, typename Getter, typename Data> |
| 3456 | static inline bool ObjectSetAccessor(Object* obj, |
| 3457 | Handle<String> name, |
| 3458 | Setter getter, |
| 3459 | Getter setter, |
| 3460 | Data data, |
| 3461 | AccessControl settings, |
| 3462 | PropertyAttribute attributes) { |
| 3463 | i::Isolate* isolate = Utils::OpenHandle(obj)->GetIsolate(); |
| 3464 | ON_BAILOUT(isolate, "v8::Object::SetAccessor()", return false); |
| 3465 | ENTER_V8(isolate); |
| 3466 | i::HandleScope scope(isolate); |
| 3467 | v8::Handle<AccessorSignature> signature; |
| 3468 | i::Handle<i::AccessorInfo> info = MakeAccessorInfo( |
| 3469 | name, getter, setter, data, settings, attributes, signature); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3470 | if (info.is_null()) return false; |
| 3471 | bool fast = Utils::OpenHandle(obj)->HasFastProperties(); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3472 | i::Handle<i::Object> result = |
| 3473 | i::JSObject::SetAccessor(Utils::OpenHandle(obj), info); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3474 | if (result.is_null() || result->IsUndefined()) return false; |
| 3475 | if (fast) i::JSObject::TransformToFastProperties(Utils::OpenHandle(obj), 0); |
| 3476 | return true; |
| 3477 | } |
| 3478 | |
| 3479 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 3480 | bool Object::SetAccessor(Handle<String> name, |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 3481 | AccessorGetterCallback getter, |
| 3482 | AccessorSetterCallback setter, |
| 3483 | v8::Handle<Value> data, |
| 3484 | AccessControl settings, |
| 3485 | PropertyAttribute attributes) { |
| 3486 | return ObjectSetAccessor( |
| 3487 | this, name, getter, setter, data, settings, attributes); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3491 | bool Object::SetDeclaredAccessor(Local<String> name, |
| 3492 | Local<DeclaredAccessorDescriptor> descriptor, |
| 3493 | PropertyAttribute attributes, |
| 3494 | AccessControl settings) { |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 3495 | void* null = NULL; |
| 3496 | return ObjectSetAccessor( |
| 3497 | this, name, descriptor, null, null, settings, attributes); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
| 3500 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 3501 | bool v8::Object::HasOwnProperty(Handle<String> key) { |
| 3502 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3503 | ON_BAILOUT(isolate, "v8::Object::HasOwnProperty()", |
| 3504 | return false); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3505 | return i::JSReceiver::HasLocalProperty( |
| 3506 | Utils::OpenHandle(this), Utils::OpenHandle(*key)); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3510 | bool v8::Object::HasRealNamedProperty(Handle<String> key) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3511 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3512 | ON_BAILOUT(isolate, "v8::Object::HasRealNamedProperty()", |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3513 | return false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3514 | return Utils::OpenHandle(this)->HasRealNamedProperty( |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3515 | isolate, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3516 | *Utils::OpenHandle(*key)); |
| 3517 | } |
| 3518 | |
| 3519 | |
| 3520 | bool v8::Object::HasRealIndexedProperty(uint32_t index) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3521 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3522 | ON_BAILOUT(isolate, "v8::Object::HasRealIndexedProperty()", |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3523 | return false); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3524 | return Utils::OpenHandle(this)->HasRealElementProperty(isolate, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | |
| 3528 | bool v8::Object::HasRealNamedCallbackProperty(Handle<String> key) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3529 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3530 | ON_BAILOUT(isolate, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3531 | "v8::Object::HasRealNamedCallbackProperty()", |
| 3532 | return false); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3533 | ENTER_V8(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3534 | return Utils::OpenHandle(this)->HasRealNamedCallbackProperty( |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3535 | isolate, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3536 | *Utils::OpenHandle(*key)); |
| 3537 | } |
| 3538 | |
| 3539 | |
| 3540 | bool v8::Object::HasNamedLookupInterceptor() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3541 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3542 | ON_BAILOUT(isolate, "v8::Object::HasNamedLookupInterceptor()", |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3543 | return false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3544 | return Utils::OpenHandle(this)->HasNamedInterceptor(); |
| 3545 | } |
| 3546 | |
| 3547 | |
| 3548 | bool v8::Object::HasIndexedLookupInterceptor() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3549 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3550 | ON_BAILOUT(isolate, "v8::Object::HasIndexedLookupInterceptor()", |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3551 | return false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3552 | return Utils::OpenHandle(this)->HasIndexedInterceptor(); |
| 3553 | } |
| 3554 | |
| 3555 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 3556 | static Local<Value> GetPropertyByLookup(i::Isolate* isolate, |
| 3557 | i::Handle<i::JSObject> receiver, |
| 3558 | i::Handle<i::String> name, |
| 3559 | i::LookupResult* lookup) { |
| 3560 | if (!lookup->IsProperty()) { |
| 3561 | // No real property was found. |
| 3562 | return Local<Value>(); |
| 3563 | } |
| 3564 | |
| 3565 | // If the property being looked up is a callback, it can throw |
| 3566 | // an exception. |
| 3567 | EXCEPTION_PREAMBLE(isolate); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3568 | PropertyAttributes ignored; |
| 3569 | i::Handle<i::Object> result = |
| 3570 | i::Object::GetProperty(receiver, receiver, lookup, name, |
| 3571 | &ignored); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 3572 | has_pending_exception = result.is_null(); |
| 3573 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>()); |
| 3574 | |
| 3575 | return Utils::ToLocal(result); |
| 3576 | } |
| 3577 | |
| 3578 | |
sgjesse@chromium.org | 98aff2f | 2009-09-30 08:27:10 +0000 | [diff] [blame] | 3579 | Local<Value> v8::Object::GetRealNamedPropertyInPrototypeChain( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3580 | Handle<String> key) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3581 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3582 | ON_BAILOUT(isolate, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3583 | "v8::Object::GetRealNamedPropertyInPrototypeChain()", |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3584 | return Local<Value>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3585 | ENTER_V8(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3586 | i::Handle<i::JSObject> self_obj = Utils::OpenHandle(this); |
| 3587 | i::Handle<i::String> key_obj = Utils::OpenHandle(*key); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3588 | i::LookupResult lookup(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3589 | self_obj->LookupRealNamedPropertyInPrototypes(*key_obj, &lookup); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 3590 | return GetPropertyByLookup(isolate, self_obj, key_obj, &lookup); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | |
sgjesse@chromium.org | 98aff2f | 2009-09-30 08:27:10 +0000 | [diff] [blame] | 3594 | Local<Value> v8::Object::GetRealNamedProperty(Handle<String> key) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3595 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3596 | ON_BAILOUT(isolate, "v8::Object::GetRealNamedProperty()", |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3597 | return Local<Value>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3598 | ENTER_V8(isolate); |
sgjesse@chromium.org | 98aff2f | 2009-09-30 08:27:10 +0000 | [diff] [blame] | 3599 | i::Handle<i::JSObject> self_obj = Utils::OpenHandle(this); |
| 3600 | i::Handle<i::String> key_obj = Utils::OpenHandle(*key); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3601 | i::LookupResult lookup(isolate); |
sgjesse@chromium.org | 98aff2f | 2009-09-30 08:27:10 +0000 | [diff] [blame] | 3602 | self_obj->LookupRealNamedProperty(*key_obj, &lookup); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 3603 | return GetPropertyByLookup(isolate, self_obj, key_obj, &lookup); |
sgjesse@chromium.org | 98aff2f | 2009-09-30 08:27:10 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
| 3606 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3607 | // Turns on access checks by copying the map and setting the check flag. |
| 3608 | // Because the object gets a new map, existing inline cache caching |
| 3609 | // the old map of this object will fail. |
| 3610 | void v8::Object::TurnOnAccessCheck() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3611 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3612 | ON_BAILOUT(isolate, "v8::Object::TurnOnAccessCheck()", return); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3613 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3614 | i::HandleScope scope(isolate); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3615 | i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
| 3616 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3617 | // When turning on access checks for a global object deoptimize all functions |
| 3618 | // as optimized code does not always handle access checks. |
| 3619 | i::Deoptimizer::DeoptimizeGlobalObject(*obj); |
| 3620 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3621 | i::Handle<i::Map> new_map = |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3622 | isolate->factory()->CopyMap(i::Handle<i::Map>(obj->map())); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 3623 | new_map->set_is_access_check_needed(true); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3624 | obj->set_map(*new_map); |
| 3625 | } |
| 3626 | |
| 3627 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 3628 | bool v8::Object::IsDirty() { |
| 3629 | return Utils::OpenHandle(this)->IsDirty(); |
| 3630 | } |
| 3631 | |
| 3632 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3633 | Local<v8::Object> v8::Object::Clone() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3634 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3635 | ON_BAILOUT(isolate, "v8::Object::Clone()", return Local<Object>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3636 | ENTER_V8(isolate); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3637 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3638 | EXCEPTION_PREAMBLE(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3639 | i::Handle<i::JSObject> result = i::JSObject::Copy(self); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3640 | has_pending_exception = result.is_null(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3641 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>()); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3642 | return Utils::ToLocal(result); |
| 3643 | } |
| 3644 | |
| 3645 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3646 | static i::Context* GetCreationContext(i::JSObject* object) { |
| 3647 | i::Object* constructor = object->map()->constructor(); |
| 3648 | i::JSFunction* function; |
| 3649 | if (!constructor->IsJSFunction()) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 3650 | // Functions have null as a constructor, |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3651 | // but any JSFunction knows its context immediately. |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 3652 | ASSERT(object->IsJSFunction()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3653 | function = i::JSFunction::cast(object); |
| 3654 | } else { |
| 3655 | function = i::JSFunction::cast(constructor); |
| 3656 | } |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 3657 | return function->context()->native_context(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3658 | } |
| 3659 | |
| 3660 | |
| 3661 | Local<v8::Context> v8::Object::CreationContext() { |
| 3662 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3663 | ON_BAILOUT(isolate, |
| 3664 | "v8::Object::CreationContext()", return Local<v8::Context>()); |
| 3665 | ENTER_V8(isolate); |
| 3666 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3667 | i::Context* context = GetCreationContext(*self); |
| 3668 | return Utils::ToLocal(i::Handle<i::Context>(context)); |
| 3669 | } |
| 3670 | |
| 3671 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3672 | int v8::Object::GetIdentityHash() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3673 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3674 | ON_BAILOUT(isolate, "v8::Object::GetIdentityHash()", return 0); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3675 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3676 | i::HandleScope scope(isolate); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3677 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 3678 | return i::JSObject::GetIdentityHash(self); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
| 3681 | |
| 3682 | bool v8::Object::SetHiddenValue(v8::Handle<v8::String> key, |
| 3683 | v8::Handle<v8::Value> value) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3684 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3685 | ON_BAILOUT(isolate, "v8::Object::SetHiddenValue()", return false); |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 3686 | if (value.IsEmpty()) return DeleteHiddenValue(key); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3687 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3688 | i::HandleScope scope(isolate); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3689 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3690 | i::Handle<i::String> key_obj = Utils::OpenHandle(*key); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3691 | i::Handle<i::String> key_string = |
| 3692 | isolate->factory()->InternalizeString(key_obj); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3693 | i::Handle<i::Object> value_obj = Utils::OpenHandle(*value); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 3694 | i::Handle<i::Object> result = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3695 | i::JSObject::SetHiddenProperty(self, key_string, value_obj); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3696 | return *result == *self; |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3697 | } |
| 3698 | |
| 3699 | |
| 3700 | v8::Local<v8::Value> v8::Object::GetHiddenValue(v8::Handle<v8::String> key) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3701 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3702 | ON_BAILOUT(isolate, "v8::Object::GetHiddenValue()", |
| 3703 | return Local<v8::Value>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3704 | ENTER_V8(isolate); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3705 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3706 | i::Handle<i::String> key_obj = Utils::OpenHandle(*key); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 3707 | i::Handle<i::String> key_string = |
| 3708 | isolate->factory()->InternalizeString(key_obj); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3709 | i::Handle<i::Object> result(self->GetHiddenProperty(*key_string), isolate); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 3710 | if (result->IsTheHole()) return v8::Local<v8::Value>(); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3711 | return Utils::ToLocal(result); |
| 3712 | } |
| 3713 | |
| 3714 | |
| 3715 | bool v8::Object::DeleteHiddenValue(v8::Handle<v8::String> key) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3716 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3717 | ON_BAILOUT(isolate, "v8::DeleteHiddenValue()", return false); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3718 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3719 | i::HandleScope scope(isolate); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3720 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3721 | i::Handle<i::String> key_obj = Utils::OpenHandle(*key); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 3722 | i::Handle<i::String> key_string = |
| 3723 | isolate->factory()->InternalizeString(key_obj); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3724 | i::JSObject::DeleteHiddenProperty(self, key_string); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3725 | return true; |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3729 | namespace { |
| 3730 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 3731 | static i::ElementsKind GetElementsKindFromExternalArrayType( |
| 3732 | ExternalArrayType array_type) { |
| 3733 | switch (array_type) { |
| 3734 | case kExternalByteArray: |
| 3735 | return i::EXTERNAL_BYTE_ELEMENTS; |
| 3736 | break; |
| 3737 | case kExternalUnsignedByteArray: |
| 3738 | return i::EXTERNAL_UNSIGNED_BYTE_ELEMENTS; |
| 3739 | break; |
| 3740 | case kExternalShortArray: |
| 3741 | return i::EXTERNAL_SHORT_ELEMENTS; |
| 3742 | break; |
| 3743 | case kExternalUnsignedShortArray: |
| 3744 | return i::EXTERNAL_UNSIGNED_SHORT_ELEMENTS; |
| 3745 | break; |
| 3746 | case kExternalIntArray: |
| 3747 | return i::EXTERNAL_INT_ELEMENTS; |
| 3748 | break; |
| 3749 | case kExternalUnsignedIntArray: |
| 3750 | return i::EXTERNAL_UNSIGNED_INT_ELEMENTS; |
| 3751 | break; |
| 3752 | case kExternalFloatArray: |
| 3753 | return i::EXTERNAL_FLOAT_ELEMENTS; |
| 3754 | break; |
| 3755 | case kExternalDoubleArray: |
| 3756 | return i::EXTERNAL_DOUBLE_ELEMENTS; |
| 3757 | break; |
| 3758 | case kExternalPixelArray: |
| 3759 | return i::EXTERNAL_PIXEL_ELEMENTS; |
| 3760 | break; |
| 3761 | } |
| 3762 | UNREACHABLE(); |
| 3763 | return i::DICTIONARY_ELEMENTS; |
| 3764 | } |
| 3765 | |
| 3766 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3767 | void PrepareExternalArrayElements(i::Handle<i::JSObject> object, |
| 3768 | void* data, |
| 3769 | ExternalArrayType array_type, |
| 3770 | int length) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3771 | i::Isolate* isolate = object->GetIsolate(); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3772 | i::Handle<i::ExternalArray> array = |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3773 | isolate->factory()->NewExternalArray(length, array_type, data); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3774 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3775 | i::Handle<i::Map> external_array_map = |
| 3776 | isolate->factory()->GetElementsTransitionMap( |
| 3777 | object, |
| 3778 | GetElementsKindFromExternalArrayType(array_type)); |
| 3779 | |
| 3780 | object->set_map(*external_array_map); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3781 | object->set_elements(*array); |
| 3782 | } |
| 3783 | |
| 3784 | } // namespace |
| 3785 | |
| 3786 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3787 | void v8::Object::SetIndexedPropertiesToPixelData(uint8_t* data, int length) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3788 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3789 | ON_BAILOUT(isolate, "v8::SetElementsToPixelData()", return); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3790 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3791 | i::HandleScope scope(isolate); |
danno@chromium.org | 412fa51 | 2012-09-14 13:28:26 +0000 | [diff] [blame] | 3792 | if (!ApiCheck(length >= 0 && length <= i::ExternalPixelArray::kMaxLength, |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3793 | "v8::Object::SetIndexedPropertiesToPixelData()", |
| 3794 | "length exceeds max acceptable value")) { |
| 3795 | return; |
| 3796 | } |
| 3797 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3798 | if (!ApiCheck(!self->IsJSArray(), |
| 3799 | "v8::Object::SetIndexedPropertiesToPixelData()", |
| 3800 | "JSArray is not supported")) { |
| 3801 | return; |
| 3802 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3803 | PrepareExternalArrayElements(self, data, kExternalPixelArray, length); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
| 3806 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3807 | bool v8::Object::HasIndexedPropertiesInPixelData() { |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3808 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3809 | ON_BAILOUT(self->GetIsolate(), "v8::HasIndexedPropertiesInPixelData()", |
| 3810 | return false); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3811 | return self->HasExternalPixelElements(); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3812 | } |
| 3813 | |
| 3814 | |
| 3815 | uint8_t* v8::Object::GetIndexedPropertiesPixelData() { |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3816 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3817 | ON_BAILOUT(self->GetIsolate(), "v8::GetIndexedPropertiesPixelData()", |
| 3818 | return NULL); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3819 | if (self->HasExternalPixelElements()) { |
| 3820 | return i::ExternalPixelArray::cast(self->elements())-> |
| 3821 | external_pixel_pointer(); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3822 | } else { |
| 3823 | return NULL; |
| 3824 | } |
| 3825 | } |
| 3826 | |
| 3827 | |
| 3828 | int v8::Object::GetIndexedPropertiesPixelDataLength() { |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3829 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3830 | ON_BAILOUT(self->GetIsolate(), "v8::GetIndexedPropertiesPixelDataLength()", |
| 3831 | return -1); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3832 | if (self->HasExternalPixelElements()) { |
| 3833 | return i::ExternalPixelArray::cast(self->elements())->length(); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3834 | } else { |
| 3835 | return -1; |
| 3836 | } |
| 3837 | } |
| 3838 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 3839 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3840 | void v8::Object::SetIndexedPropertiesToExternalArrayData( |
| 3841 | void* data, |
| 3842 | ExternalArrayType array_type, |
| 3843 | int length) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3844 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3845 | ON_BAILOUT(isolate, "v8::SetIndexedPropertiesToExternalArrayData()", return); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3846 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3847 | i::HandleScope scope(isolate); |
danno@chromium.org | 412fa51 | 2012-09-14 13:28:26 +0000 | [diff] [blame] | 3848 | if (!ApiCheck(length >= 0 && length <= i::ExternalArray::kMaxLength, |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3849 | "v8::Object::SetIndexedPropertiesToExternalArrayData()", |
| 3850 | "length exceeds max acceptable value")) { |
| 3851 | return; |
| 3852 | } |
| 3853 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3854 | if (!ApiCheck(!self->IsJSArray(), |
| 3855 | "v8::Object::SetIndexedPropertiesToExternalArrayData()", |
| 3856 | "JSArray is not supported")) { |
| 3857 | return; |
| 3858 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3859 | PrepareExternalArrayElements(self, data, array_type, length); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3863 | bool v8::Object::HasIndexedPropertiesInExternalArrayData() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3864 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3865 | ON_BAILOUT(self->GetIsolate(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3866 | "v8::HasIndexedPropertiesInExternalArrayData()", |
| 3867 | return false); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3868 | return self->HasExternalArrayElements(); |
| 3869 | } |
| 3870 | |
| 3871 | |
| 3872 | void* v8::Object::GetIndexedPropertiesExternalArrayData() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3873 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3874 | ON_BAILOUT(self->GetIsolate(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3875 | "v8::GetIndexedPropertiesExternalArrayData()", |
| 3876 | return NULL); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3877 | if (self->HasExternalArrayElements()) { |
| 3878 | return i::ExternalArray::cast(self->elements())->external_pointer(); |
| 3879 | } else { |
| 3880 | return NULL; |
| 3881 | } |
| 3882 | } |
| 3883 | |
| 3884 | |
| 3885 | ExternalArrayType v8::Object::GetIndexedPropertiesExternalArrayDataType() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3886 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3887 | ON_BAILOUT(self->GetIsolate(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3888 | "v8::GetIndexedPropertiesExternalArrayDataType()", |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3889 | return static_cast<ExternalArrayType>(-1)); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3890 | switch (self->elements()->map()->instance_type()) { |
| 3891 | case i::EXTERNAL_BYTE_ARRAY_TYPE: |
| 3892 | return kExternalByteArray; |
| 3893 | case i::EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 3894 | return kExternalUnsignedByteArray; |
| 3895 | case i::EXTERNAL_SHORT_ARRAY_TYPE: |
| 3896 | return kExternalShortArray; |
| 3897 | case i::EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 3898 | return kExternalUnsignedShortArray; |
| 3899 | case i::EXTERNAL_INT_ARRAY_TYPE: |
| 3900 | return kExternalIntArray; |
| 3901 | case i::EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 3902 | return kExternalUnsignedIntArray; |
| 3903 | case i::EXTERNAL_FLOAT_ARRAY_TYPE: |
| 3904 | return kExternalFloatArray; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 3905 | case i::EXTERNAL_DOUBLE_ARRAY_TYPE: |
| 3906 | return kExternalDoubleArray; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 3907 | case i::EXTERNAL_PIXEL_ARRAY_TYPE: |
| 3908 | return kExternalPixelArray; |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3909 | default: |
| 3910 | return static_cast<ExternalArrayType>(-1); |
| 3911 | } |
| 3912 | } |
| 3913 | |
| 3914 | |
| 3915 | int v8::Object::GetIndexedPropertiesExternalArrayDataLength() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3916 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
| 3917 | ON_BAILOUT(self->GetIsolate(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3918 | "v8::GetIndexedPropertiesExternalArrayDataLength()", |
| 3919 | return 0); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3920 | if (self->HasExternalArrayElements()) { |
| 3921 | return i::ExternalArray::cast(self->elements())->length(); |
| 3922 | } else { |
| 3923 | return -1; |
| 3924 | } |
| 3925 | } |
| 3926 | |
| 3927 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 3928 | bool v8::Object::IsCallable() { |
| 3929 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3930 | ON_BAILOUT(isolate, "v8::Object::IsCallable()", return false); |
| 3931 | ENTER_V8(isolate); |
| 3932 | i::HandleScope scope(isolate); |
| 3933 | i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
| 3934 | if (obj->IsJSFunction()) return true; |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 3935 | return i::Execution::GetFunctionDelegate(isolate, obj)->IsJSFunction(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 3936 | } |
| 3937 | |
| 3938 | |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame^] | 3939 | Local<v8::Value> Object::CallAsFunction(v8::Handle<v8::Value> recv, |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 3940 | int argc, |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 3941 | v8::Handle<v8::Value> argv[]) { |
| 3942 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3943 | ON_BAILOUT(isolate, "v8::Object::CallAsFunction()", |
| 3944 | return Local<v8::Value>()); |
| 3945 | LOG_API(isolate, "Object::CallAsFunction"); |
| 3946 | ENTER_V8(isolate); |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 3947 | i::Logger::TimerEventScope timer_scope( |
| 3948 | isolate, i::Logger::TimerEventScope::v8_execute); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 3949 | i::HandleScope scope(isolate); |
| 3950 | i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
| 3951 | i::Handle<i::Object> recv_obj = Utils::OpenHandle(*recv); |
| 3952 | STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**)); |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 3953 | i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 3954 | i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>(); |
| 3955 | if (obj->IsJSFunction()) { |
| 3956 | fun = i::Handle<i::JSFunction>::cast(obj); |
| 3957 | } else { |
| 3958 | EXCEPTION_PREAMBLE(isolate); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 3959 | i::Handle<i::Object> delegate = i::Execution::TryGetFunctionDelegate( |
| 3960 | isolate, obj, &has_pending_exception); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 3961 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>()); |
| 3962 | fun = i::Handle<i::JSFunction>::cast(delegate); |
| 3963 | recv_obj = obj; |
| 3964 | } |
| 3965 | EXCEPTION_PREAMBLE(isolate); |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 3966 | i::Handle<i::Object> returned = i::Execution::Call( |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame^] | 3967 | isolate, fun, recv_obj, argc, args, &has_pending_exception, true); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 3968 | EXCEPTION_BAILOUT_CHECK_DO_CALLBACK(isolate, Local<Value>()); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 3969 | return Utils::ToLocal(scope.CloseAndEscape(returned)); |
| 3970 | } |
| 3971 | |
| 3972 | |
| 3973 | Local<v8::Value> Object::CallAsConstructor(int argc, |
| 3974 | v8::Handle<v8::Value> argv[]) { |
| 3975 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 3976 | ON_BAILOUT(isolate, "v8::Object::CallAsConstructor()", |
| 3977 | return Local<v8::Object>()); |
| 3978 | LOG_API(isolate, "Object::CallAsConstructor"); |
| 3979 | ENTER_V8(isolate); |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 3980 | i::Logger::TimerEventScope timer_scope( |
| 3981 | isolate, i::Logger::TimerEventScope::v8_execute); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 3982 | i::HandleScope scope(isolate); |
| 3983 | i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
| 3984 | STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**)); |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 3985 | i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 3986 | if (obj->IsJSFunction()) { |
| 3987 | i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(obj); |
| 3988 | EXCEPTION_PREAMBLE(isolate); |
| 3989 | i::Handle<i::Object> returned = |
| 3990 | i::Execution::New(fun, argc, args, &has_pending_exception); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 3991 | EXCEPTION_BAILOUT_CHECK_DO_CALLBACK(isolate, Local<v8::Object>()); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 3992 | return Utils::ToLocal(scope.CloseAndEscape( |
| 3993 | i::Handle<i::JSObject>::cast(returned))); |
| 3994 | } |
| 3995 | EXCEPTION_PREAMBLE(isolate); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 3996 | i::Handle<i::Object> delegate = i::Execution::TryGetConstructorDelegate( |
| 3997 | isolate, obj, &has_pending_exception); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 3998 | EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>()); |
| 3999 | if (!delegate->IsUndefined()) { |
| 4000 | i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(delegate); |
| 4001 | EXCEPTION_PREAMBLE(isolate); |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 4002 | i::Handle<i::Object> returned = i::Execution::Call( |
| 4003 | isolate, fun, obj, argc, args, &has_pending_exception); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 4004 | EXCEPTION_BAILOUT_CHECK_DO_CALLBACK(isolate, Local<v8::Object>()); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 4005 | ASSERT(!delegate->IsUndefined()); |
| 4006 | return Utils::ToLocal(scope.CloseAndEscape(returned)); |
| 4007 | } |
| 4008 | return Local<v8::Object>(); |
| 4009 | } |
| 4010 | |
| 4011 | |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 4012 | Local<Function> Function::New(Isolate* v8_isolate, |
| 4013 | FunctionCallback callback, |
| 4014 | Local<Value> data, |
| 4015 | int length) { |
| 4016 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); |
| 4017 | LOG_API(isolate, "Function::New"); |
| 4018 | ENTER_V8(isolate); |
| 4019 | return FunctionTemplateNew( |
| 4020 | isolate, callback, data, Local<Signature>(), length, true)-> |
| 4021 | GetFunction(); |
| 4022 | } |
| 4023 | |
| 4024 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4025 | Local<v8::Object> Function::NewInstance() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4026 | return NewInstance(0, NULL); |
| 4027 | } |
| 4028 | |
| 4029 | |
| 4030 | Local<v8::Object> Function::NewInstance(int argc, |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4031 | v8::Handle<v8::Value> argv[]) const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4032 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4033 | ON_BAILOUT(isolate, "v8::Function::NewInstance()", |
| 4034 | return Local<v8::Object>()); |
| 4035 | LOG_API(isolate, "Function::NewInstance"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4036 | ENTER_V8(isolate); |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 4037 | i::Logger::TimerEventScope timer_scope( |
| 4038 | isolate, i::Logger::TimerEventScope::v8_execute); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4039 | HandleScope scope(reinterpret_cast<Isolate*>(isolate)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4040 | i::Handle<i::JSFunction> function = Utils::OpenHandle(this); |
| 4041 | STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**)); |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 4042 | i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4043 | EXCEPTION_PREAMBLE(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4044 | i::Handle<i::Object> returned = |
| 4045 | i::Execution::New(function, argc, args, &has_pending_exception); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 4046 | EXCEPTION_BAILOUT_CHECK_DO_CALLBACK(isolate, Local<v8::Object>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4047 | return scope.Close(Utils::ToLocal(i::Handle<i::JSObject>::cast(returned))); |
| 4048 | } |
| 4049 | |
| 4050 | |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame^] | 4051 | Local<v8::Value> Function::Call(v8::Handle<v8::Value> recv, int argc, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4052 | v8::Handle<v8::Value> argv[]) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4053 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4054 | ON_BAILOUT(isolate, "v8::Function::Call()", return Local<v8::Value>()); |
| 4055 | LOG_API(isolate, "Function::Call"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4056 | ENTER_V8(isolate); |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 4057 | i::Logger::TimerEventScope timer_scope( |
| 4058 | isolate, i::Logger::TimerEventScope::v8_execute); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4059 | i::Object* raw_result = NULL; |
| 4060 | { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4061 | i::HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4062 | i::Handle<i::JSFunction> fun = Utils::OpenHandle(this); |
| 4063 | i::Handle<i::Object> recv_obj = Utils::OpenHandle(*recv); |
| 4064 | STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**)); |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 4065 | i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4066 | EXCEPTION_PREAMBLE(isolate); |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 4067 | i::Handle<i::Object> returned = i::Execution::Call( |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame^] | 4068 | isolate, fun, recv_obj, argc, args, &has_pending_exception, true); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 4069 | EXCEPTION_BAILOUT_CHECK_DO_CALLBACK(isolate, Local<Object>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4070 | raw_result = *returned; |
| 4071 | } |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 4072 | i::Handle<i::Object> result(raw_result, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4073 | return Utils::ToLocal(result); |
| 4074 | } |
| 4075 | |
| 4076 | |
| 4077 | void Function::SetName(v8::Handle<v8::String> name) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4078 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 4079 | ENTER_V8(isolate); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4080 | USE(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4081 | i::Handle<i::JSFunction> func = Utils::OpenHandle(this); |
| 4082 | func->shared()->set_name(*Utils::OpenHandle(*name)); |
| 4083 | } |
| 4084 | |
| 4085 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4086 | Handle<Value> Function::GetName() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4087 | i::Handle<i::JSFunction> func = Utils::OpenHandle(this); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 4088 | return Utils::ToLocal(i::Handle<i::Object>(func->shared()->name(), |
| 4089 | func->GetIsolate())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4090 | } |
| 4091 | |
| 4092 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 4093 | Handle<Value> Function::GetInferredName() const { |
| 4094 | i::Handle<i::JSFunction> func = Utils::OpenHandle(this); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 4095 | return Utils::ToLocal(i::Handle<i::Object>(func->shared()->inferred_name(), |
| 4096 | func->GetIsolate())); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
| 4099 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4100 | ScriptOrigin Function::GetScriptOrigin() const { |
| 4101 | i::Handle<i::JSFunction> func = Utils::OpenHandle(this); |
| 4102 | if (func->shared()->script()->IsScript()) { |
| 4103 | i::Handle<i::Script> script(i::Script::cast(func->shared()->script())); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4104 | i::Handle<i::Object> scriptName = GetScriptNameOrSourceURL(script); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4105 | v8::ScriptOrigin origin( |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4106 | Utils::ToLocal(scriptName), |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4107 | v8::Integer::New(script->line_offset()->value()), |
| 4108 | v8::Integer::New(script->column_offset()->value())); |
| 4109 | return origin; |
| 4110 | } |
| 4111 | return v8::ScriptOrigin(Handle<Value>()); |
| 4112 | } |
| 4113 | |
| 4114 | |
| 4115 | const int Function::kLineOffsetNotFound = -1; |
| 4116 | |
| 4117 | |
| 4118 | int Function::GetScriptLineNumber() const { |
| 4119 | i::Handle<i::JSFunction> func = Utils::OpenHandle(this); |
| 4120 | if (func->shared()->script()->IsScript()) { |
| 4121 | i::Handle<i::Script> script(i::Script::cast(func->shared()->script())); |
| 4122 | return i::GetScriptLineNumber(script, func->shared()->start_position()); |
| 4123 | } |
| 4124 | return kLineOffsetNotFound; |
| 4125 | } |
| 4126 | |
| 4127 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4128 | int Function::GetScriptColumnNumber() const { |
| 4129 | i::Handle<i::JSFunction> func = Utils::OpenHandle(this); |
| 4130 | if (func->shared()->script()->IsScript()) { |
| 4131 | i::Handle<i::Script> script(i::Script::cast(func->shared()->script())); |
| 4132 | return i::GetScriptColumnNumber(script, func->shared()->start_position()); |
| 4133 | } |
| 4134 | return kLineOffsetNotFound; |
| 4135 | } |
| 4136 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4137 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4138 | Handle<Value> Function::GetScriptId() const { |
| 4139 | i::Handle<i::JSFunction> func = Utils::OpenHandle(this); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 4140 | i::Isolate* isolate = func->GetIsolate(); |
| 4141 | if (!func->shared()->script()->IsScript()) { |
| 4142 | return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate)); |
| 4143 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4144 | i::Handle<i::Script> script(i::Script::cast(func->shared()->script())); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 4145 | return Utils::ToLocal(i::Handle<i::Object>(script->id(), isolate)); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4148 | |
| 4149 | int Function::ScriptId() const { |
| 4150 | i::Handle<i::JSFunction> func = Utils::OpenHandle(this); |
| 4151 | if (!func->shared()->script()->IsScript()) return v8::Script::kNoScriptId; |
| 4152 | i::Handle<i::Script> script(i::Script::cast(func->shared()->script())); |
| 4153 | return script->id()->value(); |
| 4154 | } |
| 4155 | |
| 4156 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4157 | int String::Length() const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4158 | i::Handle<i::String> str = Utils::OpenHandle(this); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4159 | return str->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 4162 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 4163 | bool String::IsOneByte() const { |
| 4164 | i::Handle<i::String> str = Utils::OpenHandle(this); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 4165 | return str->HasOnlyOneByteChars(); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 4166 | } |
| 4167 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 4168 | |
verwaest@chromium.org | 8a00e82 | 2013-06-10 15:11:22 +0000 | [diff] [blame] | 4169 | // Helpers for ContainsOnlyOneByteHelper |
| 4170 | template<size_t size> struct OneByteMask; |
| 4171 | template<> struct OneByteMask<4> { |
| 4172 | static const uint32_t value = 0xFF00FF00; |
| 4173 | }; |
| 4174 | template<> struct OneByteMask<8> { |
| 4175 | static const uint64_t value = V8_2PART_UINT64_C(0xFF00FF00, FF00FF00); |
| 4176 | }; |
| 4177 | static const uintptr_t kOneByteMask = OneByteMask<sizeof(uintptr_t)>::value; |
| 4178 | static const uintptr_t kAlignmentMask = sizeof(uintptr_t) - 1; |
| 4179 | static inline bool Unaligned(const uint16_t* chars) { |
| 4180 | return reinterpret_cast<const uintptr_t>(chars) & kAlignmentMask; |
| 4181 | } |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 4182 | |
| 4183 | |
verwaest@chromium.org | 8a00e82 | 2013-06-10 15:11:22 +0000 | [diff] [blame] | 4184 | static inline const uint16_t* Align(const uint16_t* chars) { |
| 4185 | return reinterpret_cast<uint16_t*>( |
| 4186 | reinterpret_cast<uintptr_t>(chars) & ~kAlignmentMask); |
| 4187 | } |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 4188 | |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 4189 | class ContainsOnlyOneByteHelper { |
| 4190 | public: |
| 4191 | ContainsOnlyOneByteHelper() : is_one_byte_(true) {} |
| 4192 | bool Check(i::String* string) { |
| 4193 | i::ConsString* cons_string = i::String::VisitFlat(this, string, 0); |
| 4194 | if (cons_string == NULL) return is_one_byte_; |
| 4195 | return CheckCons(cons_string); |
| 4196 | } |
| 4197 | void VisitOneByteString(const uint8_t* chars, int length) { |
| 4198 | // Nothing to do. |
| 4199 | } |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 4200 | void VisitTwoByteString(const uint16_t* chars, int length) { |
verwaest@chromium.org | 8a00e82 | 2013-06-10 15:11:22 +0000 | [diff] [blame] | 4201 | // Accumulated bits. |
| 4202 | uintptr_t acc = 0; |
| 4203 | // Align to uintptr_t. |
| 4204 | const uint16_t* end = chars + length; |
| 4205 | while (Unaligned(chars) && chars != end) { |
| 4206 | acc |= *chars++; |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 4207 | } |
verwaest@chromium.org | 8a00e82 | 2013-06-10 15:11:22 +0000 | [diff] [blame] | 4208 | // Read word aligned in blocks, |
| 4209 | // checking the return value at the end of each block. |
| 4210 | const uint16_t* aligned_end = Align(end); |
| 4211 | const int increment = sizeof(uintptr_t)/sizeof(uint16_t); |
| 4212 | const int inner_loops = 16; |
| 4213 | while (chars + inner_loops*increment < aligned_end) { |
| 4214 | for (int i = 0; i < inner_loops; i++) { |
| 4215 | acc |= *reinterpret_cast<const uintptr_t*>(chars); |
| 4216 | chars += increment; |
| 4217 | } |
| 4218 | // Check for early return. |
| 4219 | if ((acc & kOneByteMask) != 0) { |
| 4220 | is_one_byte_ = false; |
| 4221 | return; |
| 4222 | } |
| 4223 | } |
| 4224 | // Read the rest. |
| 4225 | while (chars != end) { |
| 4226 | acc |= *chars++; |
| 4227 | } |
| 4228 | // Check result. |
| 4229 | if ((acc & kOneByteMask) != 0) is_one_byte_ = false; |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 4230 | } |
| 4231 | |
| 4232 | private: |
| 4233 | bool CheckCons(i::ConsString* cons_string) { |
| 4234 | while (true) { |
| 4235 | // Check left side if flat. |
| 4236 | i::String* left = cons_string->first(); |
| 4237 | i::ConsString* left_as_cons = |
| 4238 | i::String::VisitFlat(this, left, 0); |
| 4239 | if (!is_one_byte_) return false; |
| 4240 | // Check right side if flat. |
| 4241 | i::String* right = cons_string->second(); |
| 4242 | i::ConsString* right_as_cons = |
| 4243 | i::String::VisitFlat(this, right, 0); |
| 4244 | if (!is_one_byte_) return false; |
| 4245 | // Standard recurse/iterate trick. |
| 4246 | if (left_as_cons != NULL && right_as_cons != NULL) { |
| 4247 | if (left->length() < right->length()) { |
| 4248 | CheckCons(left_as_cons); |
| 4249 | cons_string = right_as_cons; |
| 4250 | } else { |
| 4251 | CheckCons(right_as_cons); |
| 4252 | cons_string = left_as_cons; |
| 4253 | } |
| 4254 | // Check fast return. |
| 4255 | if (!is_one_byte_) return false; |
| 4256 | continue; |
| 4257 | } |
| 4258 | // Descend left in place. |
| 4259 | if (left_as_cons != NULL) { |
| 4260 | cons_string = left_as_cons; |
| 4261 | continue; |
| 4262 | } |
| 4263 | // Descend right in place. |
| 4264 | if (right_as_cons != NULL) { |
| 4265 | cons_string = right_as_cons; |
| 4266 | continue; |
| 4267 | } |
| 4268 | // Terminate. |
| 4269 | break; |
| 4270 | } |
| 4271 | return is_one_byte_; |
| 4272 | } |
| 4273 | bool is_one_byte_; |
| 4274 | DISALLOW_COPY_AND_ASSIGN(ContainsOnlyOneByteHelper); |
| 4275 | }; |
| 4276 | |
| 4277 | |
| 4278 | bool String::ContainsOnlyOneByte() const { |
| 4279 | i::Handle<i::String> str = Utils::OpenHandle(this); |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 4280 | if (str->HasOnlyOneByteChars()) return true; |
| 4281 | ContainsOnlyOneByteHelper helper; |
| 4282 | return helper.Check(*str); |
| 4283 | } |
| 4284 | |
| 4285 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4286 | class Utf8LengthHelper : public i::AllStatic { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4287 | public: |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4288 | enum State { |
| 4289 | kEndsWithLeadingSurrogate = 1 << 0, |
| 4290 | kStartsWithTrailingSurrogate = 1 << 1, |
| 4291 | kLeftmostEdgeIsCalculated = 1 << 2, |
| 4292 | kRightmostEdgeIsCalculated = 1 << 3, |
| 4293 | kLeftmostEdgeIsSurrogate = 1 << 4, |
| 4294 | kRightmostEdgeIsSurrogate = 1 << 5 |
| 4295 | }; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4296 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4297 | static const uint8_t kInitialState = 0; |
| 4298 | |
| 4299 | static inline bool EndsWithSurrogate(uint8_t state) { |
| 4300 | return state & kEndsWithLeadingSurrogate; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4303 | static inline bool StartsWithSurrogate(uint8_t state) { |
| 4304 | return state & kStartsWithTrailingSurrogate; |
| 4305 | } |
| 4306 | |
| 4307 | class Visitor { |
| 4308 | public: |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4309 | inline explicit Visitor() |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4310 | : utf8_length_(0), |
| 4311 | state_(kInitialState) {} |
| 4312 | |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4313 | void VisitOneByteString(const uint8_t* chars, int length) { |
| 4314 | int utf8_length = 0; |
| 4315 | // Add in length 1 for each non-ASCII character. |
| 4316 | for (int i = 0; i < length; i++) { |
| 4317 | utf8_length += *chars++ >> 7; |
| 4318 | } |
| 4319 | // Add in length 1 for each character. |
| 4320 | utf8_length_ = utf8_length + length; |
| 4321 | state_ = kInitialState; |
| 4322 | } |
| 4323 | |
| 4324 | void VisitTwoByteString(const uint16_t* chars, int length) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4325 | int utf8_length = 0; |
| 4326 | int last_character = unibrow::Utf16::kNoPreviousCharacter; |
| 4327 | for (int i = 0; i < length; i++) { |
| 4328 | uint16_t c = chars[i]; |
| 4329 | utf8_length += unibrow::Utf8::Length(c, last_character); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4330 | last_character = c; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4331 | } |
| 4332 | utf8_length_ = utf8_length; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4333 | uint8_t state = 0; |
| 4334 | if (unibrow::Utf16::IsTrailSurrogate(chars[0])) { |
| 4335 | state |= kStartsWithTrailingSurrogate; |
| 4336 | } |
| 4337 | if (unibrow::Utf16::IsLeadSurrogate(chars[length-1])) { |
| 4338 | state |= kEndsWithLeadingSurrogate; |
| 4339 | } |
| 4340 | state_ = state; |
| 4341 | } |
| 4342 | |
| 4343 | static i::ConsString* VisitFlat(i::String* string, |
| 4344 | int* length, |
| 4345 | uint8_t* state) { |
| 4346 | Visitor visitor; |
| 4347 | i::ConsString* cons_string = i::String::VisitFlat(&visitor, string); |
| 4348 | *length = visitor.utf8_length_; |
| 4349 | *state = visitor.state_; |
| 4350 | return cons_string; |
| 4351 | } |
| 4352 | |
| 4353 | private: |
| 4354 | int utf8_length_; |
| 4355 | uint8_t state_; |
| 4356 | DISALLOW_COPY_AND_ASSIGN(Visitor); |
| 4357 | }; |
| 4358 | |
| 4359 | static inline void MergeLeafLeft(int* length, |
| 4360 | uint8_t* state, |
| 4361 | uint8_t leaf_state) { |
| 4362 | bool edge_surrogate = StartsWithSurrogate(leaf_state); |
| 4363 | if (!(*state & kLeftmostEdgeIsCalculated)) { |
| 4364 | ASSERT(!(*state & kLeftmostEdgeIsSurrogate)); |
| 4365 | *state |= kLeftmostEdgeIsCalculated |
| 4366 | | (edge_surrogate ? kLeftmostEdgeIsSurrogate : 0); |
| 4367 | } else if (EndsWithSurrogate(*state) && edge_surrogate) { |
| 4368 | *length -= unibrow::Utf8::kBytesSavedByCombiningSurrogates; |
| 4369 | } |
| 4370 | if (EndsWithSurrogate(leaf_state)) { |
| 4371 | *state |= kEndsWithLeadingSurrogate; |
| 4372 | } else { |
| 4373 | *state &= ~kEndsWithLeadingSurrogate; |
| 4374 | } |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4375 | } |
| 4376 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4377 | static inline void MergeLeafRight(int* length, |
| 4378 | uint8_t* state, |
| 4379 | uint8_t leaf_state) { |
| 4380 | bool edge_surrogate = EndsWithSurrogate(leaf_state); |
| 4381 | if (!(*state & kRightmostEdgeIsCalculated)) { |
| 4382 | ASSERT(!(*state & kRightmostEdgeIsSurrogate)); |
| 4383 | *state |= (kRightmostEdgeIsCalculated |
| 4384 | | (edge_surrogate ? kRightmostEdgeIsSurrogate : 0)); |
| 4385 | } else if (edge_surrogate && StartsWithSurrogate(*state)) { |
| 4386 | *length -= unibrow::Utf8::kBytesSavedByCombiningSurrogates; |
| 4387 | } |
| 4388 | if (StartsWithSurrogate(leaf_state)) { |
| 4389 | *state |= kStartsWithTrailingSurrogate; |
| 4390 | } else { |
| 4391 | *state &= ~kStartsWithTrailingSurrogate; |
| 4392 | } |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4393 | } |
| 4394 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4395 | static inline void MergeTerminal(int* length, |
| 4396 | uint8_t state, |
| 4397 | uint8_t* state_out) { |
| 4398 | ASSERT((state & kLeftmostEdgeIsCalculated) && |
| 4399 | (state & kRightmostEdgeIsCalculated)); |
| 4400 | if (EndsWithSurrogate(state) && StartsWithSurrogate(state)) { |
| 4401 | *length -= unibrow::Utf8::kBytesSavedByCombiningSurrogates; |
| 4402 | } |
| 4403 | *state_out = kInitialState | |
| 4404 | (state & kLeftmostEdgeIsSurrogate ? kStartsWithTrailingSurrogate : 0) | |
| 4405 | (state & kRightmostEdgeIsSurrogate ? kEndsWithLeadingSurrogate : 0); |
| 4406 | } |
| 4407 | |
| 4408 | static int Calculate(i::ConsString* current, uint8_t* state_out) { |
| 4409 | using namespace internal; |
| 4410 | int total_length = 0; |
| 4411 | uint8_t state = kInitialState; |
| 4412 | while (true) { |
| 4413 | i::String* left = current->first(); |
| 4414 | i::String* right = current->second(); |
| 4415 | uint8_t right_leaf_state; |
| 4416 | uint8_t left_leaf_state; |
| 4417 | int leaf_length; |
| 4418 | ConsString* left_as_cons = |
| 4419 | Visitor::VisitFlat(left, &leaf_length, &left_leaf_state); |
| 4420 | if (left_as_cons == NULL) { |
| 4421 | total_length += leaf_length; |
| 4422 | MergeLeafLeft(&total_length, &state, left_leaf_state); |
| 4423 | } |
| 4424 | ConsString* right_as_cons = |
| 4425 | Visitor::VisitFlat(right, &leaf_length, &right_leaf_state); |
| 4426 | if (right_as_cons == NULL) { |
| 4427 | total_length += leaf_length; |
| 4428 | MergeLeafRight(&total_length, &state, right_leaf_state); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4429 | if (left_as_cons != NULL) { |
| 4430 | // 1 Leaf node. Descend in place. |
| 4431 | current = left_as_cons; |
| 4432 | continue; |
| 4433 | } else { |
| 4434 | // Terminal node. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4435 | MergeTerminal(&total_length, state, state_out); |
| 4436 | return total_length; |
| 4437 | } |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4438 | } else if (left_as_cons == NULL) { |
| 4439 | // 1 Leaf node. Descend in place. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4440 | current = right_as_cons; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4441 | continue; |
| 4442 | } |
| 4443 | // Both strings are ConsStrings. |
| 4444 | // Recurse on smallest. |
| 4445 | if (left->length() < right->length()) { |
| 4446 | total_length += Calculate(left_as_cons, &left_leaf_state); |
| 4447 | MergeLeafLeft(&total_length, &state, left_leaf_state); |
| 4448 | current = right_as_cons; |
| 4449 | } else { |
| 4450 | total_length += Calculate(right_as_cons, &right_leaf_state); |
| 4451 | MergeLeafRight(&total_length, &state, right_leaf_state); |
| 4452 | current = left_as_cons; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4453 | } |
| 4454 | } |
| 4455 | UNREACHABLE(); |
| 4456 | return 0; |
| 4457 | } |
| 4458 | |
| 4459 | static inline int Calculate(i::ConsString* current) { |
| 4460 | uint8_t state = kInitialState; |
| 4461 | return Calculate(current, &state); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4462 | } |
| 4463 | |
| 4464 | private: |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4465 | DISALLOW_IMPLICIT_CONSTRUCTORS(Utf8LengthHelper); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4466 | }; |
| 4467 | |
| 4468 | |
| 4469 | static int Utf8Length(i::String* str, i::Isolate* isolate) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4470 | int length = str->length(); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4471 | if (length == 0) return 0; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4472 | uint8_t state; |
| 4473 | i::ConsString* cons_string = |
| 4474 | Utf8LengthHelper::Visitor::VisitFlat(str, &length, &state); |
| 4475 | if (cons_string == NULL) return length; |
| 4476 | return Utf8LengthHelper::Calculate(cons_string); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
| 4479 | |
| 4480 | int String::Utf8Length() const { |
| 4481 | i::Handle<i::String> str = Utils::OpenHandle(this); |
| 4482 | i::Isolate* isolate = str->GetIsolate(); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4483 | return v8::Utf8Length(*str, isolate); |
| 4484 | } |
| 4485 | |
| 4486 | |
| 4487 | class Utf8WriterVisitor { |
| 4488 | public: |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4489 | Utf8WriterVisitor( |
| 4490 | char* buffer, int capacity, bool skip_capacity_check) |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4491 | : early_termination_(false), |
| 4492 | last_character_(unibrow::Utf16::kNoPreviousCharacter), |
| 4493 | buffer_(buffer), |
| 4494 | start_(buffer), |
| 4495 | capacity_(capacity), |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4496 | skip_capacity_check_(capacity == -1 || skip_capacity_check), |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4497 | utf16_chars_read_(0) { |
| 4498 | } |
| 4499 | |
| 4500 | static int WriteEndCharacter(uint16_t character, |
| 4501 | int last_character, |
| 4502 | int remaining, |
| 4503 | char* const buffer) { |
| 4504 | using namespace unibrow; |
| 4505 | ASSERT(remaining > 0); |
| 4506 | // We can't use a local buffer here because Encode needs to modify |
| 4507 | // previous characters in the stream. We know, however, that |
| 4508 | // exactly one character will be advanced. |
| 4509 | if (Utf16::IsTrailSurrogate(character) && |
| 4510 | Utf16::IsLeadSurrogate(last_character)) { |
| 4511 | int written = Utf8::Encode(buffer, character, last_character); |
| 4512 | ASSERT(written == 1); |
| 4513 | return written; |
| 4514 | } |
| 4515 | // Use a scratch buffer to check the required characters. |
| 4516 | char temp_buffer[Utf8::kMaxEncodedSize]; |
| 4517 | // Can't encode using last_character as gcc has array bounds issues. |
| 4518 | int written = Utf8::Encode(temp_buffer, |
| 4519 | character, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4520 | Utf16::kNoPreviousCharacter); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4521 | // Won't fit. |
| 4522 | if (written > remaining) return 0; |
| 4523 | // Copy over the character from temp_buffer. |
| 4524 | for (int j = 0; j < written; j++) { |
| 4525 | buffer[j] = temp_buffer[j]; |
| 4526 | } |
| 4527 | return written; |
| 4528 | } |
| 4529 | |
| 4530 | template<typename Char> |
| 4531 | void Visit(const Char* chars, const int length) { |
| 4532 | using namespace unibrow; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4533 | ASSERT(!early_termination_); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4534 | if (length == 0) return; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4535 | // Copy state to stack. |
| 4536 | char* buffer = buffer_; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4537 | int last_character = |
| 4538 | sizeof(Char) == 1 ? Utf16::kNoPreviousCharacter : last_character_; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4539 | int i = 0; |
| 4540 | // Do a fast loop where there is no exit capacity check. |
| 4541 | while (true) { |
| 4542 | int fast_length; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4543 | if (skip_capacity_check_) { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4544 | fast_length = length; |
| 4545 | } else { |
| 4546 | int remaining_capacity = capacity_ - static_cast<int>(buffer - start_); |
| 4547 | // Need enough space to write everything but one character. |
| 4548 | STATIC_ASSERT(Utf16::kMaxExtraUtf8BytesForOneUtf16CodeUnit == 3); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4549 | int max_size_per_char = sizeof(Char) == 1 ? 2 : 3; |
| 4550 | int writable_length = |
| 4551 | (remaining_capacity - max_size_per_char)/max_size_per_char; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4552 | // Need to drop into slow loop. |
| 4553 | if (writable_length <= 0) break; |
| 4554 | fast_length = i + writable_length; |
| 4555 | if (fast_length > length) fast_length = length; |
| 4556 | } |
| 4557 | // Write the characters to the stream. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4558 | if (sizeof(Char) == 1) { |
| 4559 | for (; i < fast_length; i++) { |
| 4560 | buffer += |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4561 | Utf8::EncodeOneByte(buffer, static_cast<uint8_t>(*chars++)); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4562 | ASSERT(capacity_ == -1 || (buffer - start_) <= capacity_); |
| 4563 | } |
| 4564 | } else { |
| 4565 | for (; i < fast_length; i++) { |
| 4566 | uint16_t character = *chars++; |
| 4567 | buffer += Utf8::Encode(buffer, character, last_character); |
| 4568 | last_character = character; |
| 4569 | ASSERT(capacity_ == -1 || (buffer - start_) <= capacity_); |
| 4570 | } |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4571 | } |
| 4572 | // Array is fully written. Exit. |
| 4573 | if (fast_length == length) { |
| 4574 | // Write state back out to object. |
| 4575 | last_character_ = last_character; |
| 4576 | buffer_ = buffer; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4577 | utf16_chars_read_ += length; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4578 | return; |
| 4579 | } |
| 4580 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4581 | ASSERT(!skip_capacity_check_); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4582 | // Slow loop. Must check capacity on each iteration. |
| 4583 | int remaining_capacity = capacity_ - static_cast<int>(buffer - start_); |
| 4584 | ASSERT(remaining_capacity >= 0); |
| 4585 | for (; i < length && remaining_capacity > 0; i++) { |
| 4586 | uint16_t character = *chars++; |
| 4587 | int written = WriteEndCharacter(character, |
| 4588 | last_character, |
| 4589 | remaining_capacity, |
| 4590 | buffer); |
| 4591 | if (written == 0) { |
| 4592 | early_termination_ = true; |
| 4593 | break; |
| 4594 | } |
| 4595 | buffer += written; |
| 4596 | remaining_capacity -= written; |
| 4597 | last_character = character; |
| 4598 | } |
| 4599 | // Write state back out to object. |
| 4600 | last_character_ = last_character; |
| 4601 | buffer_ = buffer; |
| 4602 | utf16_chars_read_ += i; |
| 4603 | } |
| 4604 | |
| 4605 | inline bool IsDone() { |
| 4606 | return early_termination_; |
| 4607 | } |
| 4608 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4609 | inline void VisitOneByteString(const uint8_t* chars, int length) { |
| 4610 | Visit(chars, length); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4611 | } |
| 4612 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4613 | inline void VisitTwoByteString(const uint16_t* chars, int length) { |
| 4614 | Visit(chars, length); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4615 | } |
| 4616 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4617 | int CompleteWrite(bool write_null, int* utf16_chars_read_out) { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4618 | // Write out number of utf16 characters written to the stream. |
| 4619 | if (utf16_chars_read_out != NULL) { |
| 4620 | *utf16_chars_read_out = utf16_chars_read_; |
| 4621 | } |
| 4622 | // Only null terminate if all of the string was written and there's space. |
| 4623 | if (write_null && |
| 4624 | !early_termination_ && |
| 4625 | (capacity_ == -1 || (buffer_ - start_) < capacity_)) { |
| 4626 | *buffer_++ = '\0'; |
| 4627 | } |
| 4628 | return static_cast<int>(buffer_ - start_); |
| 4629 | } |
| 4630 | |
| 4631 | private: |
| 4632 | bool early_termination_; |
| 4633 | int last_character_; |
| 4634 | char* buffer_; |
| 4635 | char* const start_; |
| 4636 | int capacity_; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4637 | bool const skip_capacity_check_; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 4638 | int utf16_chars_read_; |
| 4639 | DISALLOW_IMPLICIT_CONSTRUCTORS(Utf8WriterVisitor); |
| 4640 | }; |
| 4641 | |
| 4642 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4643 | static bool RecursivelySerializeToUtf8(i::String* current, |
| 4644 | Utf8WriterVisitor* writer, |
| 4645 | int recursion_budget) { |
| 4646 | while (!writer->IsDone()) { |
| 4647 | i::ConsString* cons_string = i::String::VisitFlat(writer, current); |
| 4648 | if (cons_string == NULL) return true; // Leaf node. |
| 4649 | if (recursion_budget <= 0) return false; |
| 4650 | // Must write the left branch first. |
| 4651 | i::String* first = cons_string->first(); |
| 4652 | bool success = RecursivelySerializeToUtf8(first, |
| 4653 | writer, |
| 4654 | recursion_budget - 1); |
| 4655 | if (!success) return false; |
| 4656 | // Inline tail recurse for right branch. |
| 4657 | current = cons_string->second(); |
| 4658 | } |
| 4659 | return true; |
| 4660 | } |
| 4661 | |
| 4662 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 4663 | int String::WriteUtf8(char* buffer, |
| 4664 | int capacity, |
| 4665 | int* nchars_ref, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 4666 | int options) const { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4667 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4668 | LOG_API(isolate, "String::WriteUtf8"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4669 | ENTER_V8(isolate); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4670 | i::Handle<i::String> str = Utils::OpenHandle(this); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 4671 | if (options & HINT_MANY_WRITES_EXPECTED) { |
| 4672 | FlattenString(str); // Flatten the string for efficiency. |
| 4673 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4674 | const int string_length = str->length(); |
| 4675 | bool write_null = !(options & NO_NULL_TERMINATION); |
| 4676 | // First check if we can just write the string without checking capacity. |
| 4677 | if (capacity == -1 || capacity / 3 >= string_length) { |
| 4678 | Utf8WriterVisitor writer(buffer, capacity, true); |
| 4679 | const int kMaxRecursion = 100; |
| 4680 | bool success = RecursivelySerializeToUtf8(*str, &writer, kMaxRecursion); |
| 4681 | if (success) return writer.CompleteWrite(write_null, nchars_ref); |
| 4682 | } else if (capacity >= string_length) { |
| 4683 | // First check that the buffer is large enough. |
| 4684 | int utf8_bytes = v8::Utf8Length(*str, str->GetIsolate()); |
| 4685 | if (utf8_bytes <= capacity) { |
| 4686 | // ASCII fast path. |
| 4687 | if (utf8_bytes == string_length) { |
| 4688 | WriteOneByte(reinterpret_cast<uint8_t*>(buffer), 0, capacity, options); |
| 4689 | if (nchars_ref != NULL) *nchars_ref = string_length; |
| 4690 | if (write_null && (utf8_bytes+1 <= capacity)) { |
| 4691 | return string_length + 1; |
| 4692 | } |
| 4693 | return string_length; |
| 4694 | } |
| 4695 | if (write_null && (utf8_bytes+1 > capacity)) { |
| 4696 | options |= NO_NULL_TERMINATION; |
| 4697 | } |
| 4698 | // Recurse once without a capacity limit. |
| 4699 | // This will get into the first branch above. |
| 4700 | // TODO(dcarney) Check max left rec. in Utf8Length and fall through. |
| 4701 | return WriteUtf8(buffer, -1, nchars_ref, options); |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 4702 | } |
| 4703 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4704 | // Recursive slow path can potentially be unreasonable slow. Flatten. |
| 4705 | str = FlattenGetString(str); |
| 4706 | Utf8WriterVisitor writer(buffer, capacity, false); |
| 4707 | i::String::VisitFlat(&writer, *str); |
| 4708 | return writer.CompleteWrite(write_null, nchars_ref); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
| 4711 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 4712 | template<typename CharType> |
| 4713 | static inline int WriteHelper(const String* string, |
| 4714 | CharType* buffer, |
| 4715 | int start, |
| 4716 | int length, |
| 4717 | int options) { |
| 4718 | i::Isolate* isolate = Utils::OpenHandle(string)->GetIsolate(); |
yangguo@chromium.org | e19986e | 2013-01-16 09:48:20 +0000 | [diff] [blame] | 4719 | LOG_API(isolate, "String::Write"); |
| 4720 | ENTER_V8(isolate); |
| 4721 | ASSERT(start >= 0 && length >= -1); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 4722 | i::Handle<i::String> str = Utils::OpenHandle(string); |
yangguo@chromium.org | e19986e | 2013-01-16 09:48:20 +0000 | [diff] [blame] | 4723 | isolate->string_tracker()->RecordWrite(str); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 4724 | if (options & String::HINT_MANY_WRITES_EXPECTED) { |
yangguo@chromium.org | e19986e | 2013-01-16 09:48:20 +0000 | [diff] [blame] | 4725 | // Flatten the string for efficiency. This applies whether we are |
| 4726 | // using StringCharacterStream or Get(i) to access the characters. |
| 4727 | FlattenString(str); |
| 4728 | } |
| 4729 | int end = start + length; |
| 4730 | if ((length == -1) || (length > str->length() - start) ) |
| 4731 | end = str->length(); |
| 4732 | if (end < 0) return 0; |
| 4733 | i::String::WriteToFlat(*str, buffer, start, end); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 4734 | if (!(options & String::NO_NULL_TERMINATION) && |
yangguo@chromium.org | e19986e | 2013-01-16 09:48:20 +0000 | [diff] [blame] | 4735 | (length == -1 || end - start < length)) { |
| 4736 | buffer[end - start] = '\0'; |
| 4737 | } |
| 4738 | return end - start; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4739 | } |
| 4740 | |
| 4741 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 4742 | int String::WriteOneByte(uint8_t* buffer, |
| 4743 | int start, |
| 4744 | int length, |
| 4745 | int options) const { |
| 4746 | return WriteHelper(this, buffer, start, length, options); |
| 4747 | } |
| 4748 | |
| 4749 | |
| 4750 | int String::Write(uint16_t* buffer, |
| 4751 | int start, |
| 4752 | int length, |
| 4753 | int options) const { |
| 4754 | return WriteHelper(this, buffer, start, length, options); |
| 4755 | } |
| 4756 | |
| 4757 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4758 | bool v8::String::IsExternal() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4759 | i::Handle<i::String> str = Utils::OpenHandle(this); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4760 | EnsureInitializedForIsolate(str->GetIsolate(), "v8::String::IsExternal()"); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4761 | return i::StringShape(*str).IsExternalTwoByte(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4762 | } |
| 4763 | |
| 4764 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4765 | bool v8::String::IsExternalAscii() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4766 | i::Handle<i::String> str = Utils::OpenHandle(this); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4767 | return i::StringShape(*str).IsExternalAscii(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4768 | } |
| 4769 | |
| 4770 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 4771 | void v8::String::VerifyExternalStringResource( |
| 4772 | v8::String::ExternalStringResource* value) const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4773 | i::Handle<i::String> str = Utils::OpenHandle(this); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4774 | const v8::String::ExternalStringResource* expected; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4775 | if (i::StringShape(*str).IsExternalTwoByte()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4776 | const void* resource = |
| 4777 | i::Handle<i::ExternalTwoByteString>::cast(str)->resource(); |
| 4778 | expected = reinterpret_cast<const ExternalStringResource*>(resource); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4779 | } else { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 4780 | expected = NULL; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4781 | } |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 4782 | CHECK_EQ(expected, value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4783 | } |
| 4784 | |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 4785 | void v8::String::VerifyExternalStringResourceBase( |
| 4786 | v8::String::ExternalStringResourceBase* value, Encoding encoding) const { |
| 4787 | i::Handle<i::String> str = Utils::OpenHandle(this); |
| 4788 | const v8::String::ExternalStringResourceBase* expected; |
| 4789 | Encoding expectedEncoding; |
| 4790 | if (i::StringShape(*str).IsExternalAscii()) { |
| 4791 | const void* resource = |
| 4792 | i::Handle<i::ExternalAsciiString>::cast(str)->resource(); |
| 4793 | expected = reinterpret_cast<const ExternalStringResourceBase*>(resource); |
| 4794 | expectedEncoding = ASCII_ENCODING; |
| 4795 | } else if (i::StringShape(*str).IsExternalTwoByte()) { |
| 4796 | const void* resource = |
| 4797 | i::Handle<i::ExternalTwoByteString>::cast(str)->resource(); |
| 4798 | expected = reinterpret_cast<const ExternalStringResourceBase*>(resource); |
| 4799 | expectedEncoding = TWO_BYTE_ENCODING; |
| 4800 | } else { |
| 4801 | expected = NULL; |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 4802 | expectedEncoding = str->IsOneByteRepresentation() ? ASCII_ENCODING |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 4803 | : TWO_BYTE_ENCODING; |
| 4804 | } |
| 4805 | CHECK_EQ(expected, value); |
| 4806 | CHECK_EQ(expectedEncoding, encoding); |
| 4807 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4808 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4809 | const v8::String::ExternalAsciiStringResource* |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4810 | v8::String::GetExternalAsciiStringResource() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4811 | i::Handle<i::String> str = Utils::OpenHandle(this); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4812 | if (i::StringShape(*str).IsExternalAscii()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4813 | const void* resource = |
| 4814 | i::Handle<i::ExternalAsciiString>::cast(str)->resource(); |
| 4815 | return reinterpret_cast<const ExternalAsciiStringResource*>(resource); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4816 | } else { |
| 4817 | return NULL; |
| 4818 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4819 | } |
| 4820 | |
| 4821 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 4822 | Local<Value> Symbol::Name() const { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 4823 | i::Handle<i::Symbol> sym = Utils::OpenHandle(this); |
| 4824 | i::Handle<i::Object> name(sym->name(), sym->GetIsolate()); |
| 4825 | return Utils::ToLocal(name); |
| 4826 | } |
| 4827 | |
| 4828 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4829 | double Number::Value() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4830 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 4831 | return obj->Number(); |
| 4832 | } |
| 4833 | |
| 4834 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4835 | bool Boolean::Value() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4836 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 4837 | return obj->IsTrue(); |
| 4838 | } |
| 4839 | |
| 4840 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4841 | int64_t Integer::Value() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4842 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 4843 | if (obj->IsSmi()) { |
| 4844 | return i::Smi::cast(*obj)->value(); |
| 4845 | } else { |
| 4846 | return static_cast<int64_t>(obj->Number()); |
| 4847 | } |
| 4848 | } |
| 4849 | |
| 4850 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4851 | int32_t Int32::Value() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4852 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 4853 | if (obj->IsSmi()) { |
| 4854 | return i::Smi::cast(*obj)->value(); |
| 4855 | } else { |
| 4856 | return static_cast<int32_t>(obj->Number()); |
| 4857 | } |
| 4858 | } |
| 4859 | |
| 4860 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4861 | uint32_t Uint32::Value() const { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4862 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 4863 | if (obj->IsSmi()) { |
| 4864 | return i::Smi::cast(*obj)->value(); |
| 4865 | } else { |
| 4866 | return static_cast<uint32_t>(obj->Number()); |
| 4867 | } |
| 4868 | } |
| 4869 | |
| 4870 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4871 | int v8::Object::InternalFieldCount() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4872 | i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4873 | return obj->GetInternalFieldCount(); |
| 4874 | } |
| 4875 | |
| 4876 | |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 4877 | static bool InternalFieldOK(i::Handle<i::JSObject> obj, |
| 4878 | int index, |
| 4879 | const char* location) { |
machenbach@chromium.org | ae16103 | 2013-09-24 09:12:30 +0000 | [diff] [blame] | 4880 | return ApiCheck(index < obj->GetInternalFieldCount(), |
| 4881 | location, |
| 4882 | "Internal field out of bounds"); |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 4883 | } |
| 4884 | |
| 4885 | |
| 4886 | Local<Value> v8::Object::SlowGetInternalField(int index) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4887 | i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 4888 | const char* location = "v8::Object::GetInternalField()"; |
| 4889 | if (!InternalFieldOK(obj, index, location)) return Local<Value>(); |
| 4890 | i::Handle<i::Object> value(obj->GetInternalField(index), obj->GetIsolate()); |
| 4891 | return Utils::ToLocal(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
| 4894 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 4895 | void v8::Object::SetInternalField(int index, v8::Handle<Value> value) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4896 | i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 4897 | const char* location = "v8::Object::SetInternalField()"; |
| 4898 | if (!InternalFieldOK(obj, index, location)) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4899 | i::Handle<i::Object> val = Utils::OpenHandle(*value); |
| 4900 | obj->SetInternalField(index, *val); |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 4901 | ASSERT_EQ(value, GetInternalField(index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4902 | } |
| 4903 | |
| 4904 | |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 4905 | void* v8::Object::SlowGetAlignedPointerFromInternalField(int index) { |
| 4906 | i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
| 4907 | const char* location = "v8::Object::GetAlignedPointerFromInternalField()"; |
| 4908 | if (!InternalFieldOK(obj, index, location)) return NULL; |
| 4909 | return DecodeSmiToAligned(obj->GetInternalField(index), location); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 4910 | } |
| 4911 | |
| 4912 | |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 4913 | void v8::Object::SetAlignedPointerInInternalField(int index, void* value) { |
| 4914 | i::Handle<i::JSObject> obj = Utils::OpenHandle(this); |
| 4915 | const char* location = "v8::Object::SetAlignedPointerInInternalField()"; |
| 4916 | if (!InternalFieldOK(obj, index, location)) return; |
| 4917 | obj->SetInternalField(index, EncodeAlignedAsSmi(value, location)); |
| 4918 | ASSERT_EQ(value, GetAlignedPointerFromInternalField(index)); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 4919 | } |
| 4920 | |
| 4921 | |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 4922 | static void* ExternalValue(i::Object* obj) { |
| 4923 | // Obscure semantics for undefined, but somehow checked in our unit tests... |
| 4924 | if (obj->IsUndefined()) return NULL; |
| 4925 | i::Object* foreign = i::JSObject::cast(obj)->GetInternalField(0); |
| 4926 | return i::Foreign::cast(foreign)->foreign_address(); |
| 4927 | } |
| 4928 | |
| 4929 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4930 | // --- E n v i r o n m e n t --- |
| 4931 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4932 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4933 | bool v8::V8::Initialize() { |
danno@chromium.org | 8c0a43f | 2012-04-03 08:37:53 +0000 | [diff] [blame] | 4934 | i::Isolate* isolate = i::Isolate::UncheckedCurrent(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4935 | if (isolate != NULL && isolate->IsInitialized()) { |
| 4936 | return true; |
| 4937 | } |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4938 | return InitializeHelper(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4939 | } |
| 4940 | |
| 4941 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 4942 | void v8::V8::SetEntropySource(EntropySource entropy_source) { |
| 4943 | i::RandomNumberGenerator::SetEntropySource(entropy_source); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4944 | } |
| 4945 | |
| 4946 | |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 4947 | void v8::V8::SetReturnAddressLocationResolver( |
| 4948 | ReturnAddressLocationResolver return_address_resolver) { |
| 4949 | i::V8::SetReturnAddressLocationResolver(return_address_resolver); |
| 4950 | } |
| 4951 | |
| 4952 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4953 | bool v8::V8::SetFunctionEntryHook(Isolate* ext_isolate, |
| 4954 | FunctionEntryHook entry_hook) { |
| 4955 | ASSERT(ext_isolate != NULL); |
| 4956 | ASSERT(entry_hook != NULL); |
| 4957 | |
| 4958 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(ext_isolate); |
| 4959 | |
| 4960 | // The entry hook can only be set before the Isolate is initialized, as |
| 4961 | // otherwise the Isolate's code stubs generated at initialization won't |
| 4962 | // contain entry hooks. |
| 4963 | if (isolate->IsInitialized()) |
| 4964 | return false; |
| 4965 | |
| 4966 | // Setting an entry hook is a one-way operation, once set, it cannot be |
| 4967 | // changed or unset. |
| 4968 | if (isolate->function_entry_hook() != NULL) |
| 4969 | return false; |
| 4970 | |
| 4971 | isolate->set_function_entry_hook(entry_hook); |
| 4972 | return true; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 4973 | } |
| 4974 | |
| 4975 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 4976 | void v8::V8::SetJitCodeEventHandler( |
| 4977 | JitCodeEventOptions options, JitCodeEventHandler event_handler) { |
| 4978 | i::Isolate* isolate = i::Isolate::Current(); |
| 4979 | // Ensure that logging is initialized for our isolate. |
| 4980 | isolate->InitializeLoggingAndCounters(); |
| 4981 | isolate->logger()->SetCodeEventHandler(options, event_handler); |
| 4982 | } |
| 4983 | |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 4984 | void v8::V8::SetArrayBufferAllocator( |
| 4985 | ArrayBuffer::Allocator* allocator) { |
| 4986 | if (!ApiCheck(i::V8::ArrayBufferAllocator() == NULL, |
| 4987 | "v8::V8::SetArrayBufferAllocator", |
| 4988 | "ArrayBufferAllocator might only be set once")) |
| 4989 | return; |
| 4990 | i::V8::SetArrayBufferAllocator(allocator); |
| 4991 | } |
| 4992 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 4993 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 4994 | bool v8::V8::Dispose() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4995 | i::Isolate* isolate = i::Isolate::Current(); |
| 4996 | if (!ApiCheck(isolate != NULL && isolate->IsDefaultIsolate(), |
| 4997 | "v8::V8::Dispose()", |
| 4998 | "Use v8::Isolate::Dispose() for a non-default isolate.")) { |
| 4999 | return false; |
| 5000 | } |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 5001 | i::V8::TearDown(); |
| 5002 | return true; |
| 5003 | } |
| 5004 | |
| 5005 | |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 5006 | HeapStatistics::HeapStatistics(): total_heap_size_(0), |
| 5007 | total_heap_size_executable_(0), |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 5008 | total_physical_size_(0), |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5009 | used_heap_size_(0), |
| 5010 | heap_size_limit_(0) { } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 5011 | |
| 5012 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5013 | void v8::V8::VisitExternalResources(ExternalResourceVisitor* visitor) { |
| 5014 | i::Isolate* isolate = i::Isolate::Current(); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5015 | isolate->heap()->VisitExternalResources(visitor); |
| 5016 | } |
| 5017 | |
| 5018 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 5019 | class VisitorAdapter : public i::ObjectVisitor { |
| 5020 | public: |
| 5021 | explicit VisitorAdapter(PersistentHandleVisitor* visitor) |
| 5022 | : visitor_(visitor) {} |
| 5023 | virtual void VisitPointers(i::Object** start, i::Object** end) { |
| 5024 | UNREACHABLE(); |
| 5025 | } |
| 5026 | virtual void VisitEmbedderReference(i::Object** p, uint16_t class_id) { |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 5027 | Value* value = ToApi<Value>(i::Handle<i::Object>(p)); |
| 5028 | visitor_->VisitPersistentHandle( |
| 5029 | reinterpret_cast<Persistent<Value>*>(&value), class_id); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 5030 | } |
| 5031 | private: |
| 5032 | PersistentHandleVisitor* visitor_; |
| 5033 | }; |
| 5034 | |
| 5035 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 5036 | void v8::V8::VisitHandlesWithClassIds(PersistentHandleVisitor* visitor) { |
| 5037 | i::Isolate* isolate = i::Isolate::Current(); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 5038 | i::DisallowHeapAllocation no_allocation; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 5039 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 5040 | VisitorAdapter visitor_adapter(visitor); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 5041 | isolate->global_handles()->IterateAllRootsWithClassIds(&visitor_adapter); |
| 5042 | } |
| 5043 | |
| 5044 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 5045 | void v8::V8::VisitHandlesForPartialDependence( |
| 5046 | Isolate* exported_isolate, PersistentHandleVisitor* visitor) { |
| 5047 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(exported_isolate); |
| 5048 | ASSERT(isolate == i::Isolate::Current()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 5049 | i::DisallowHeapAllocation no_allocation; |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 5050 | |
| 5051 | VisitorAdapter visitor_adapter(visitor); |
| 5052 | isolate->global_handles()->IterateAllRootsInNewSpaceWithClassIds( |
| 5053 | &visitor_adapter); |
| 5054 | } |
| 5055 | |
| 5056 | |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 5057 | bool v8::V8::IdleNotification(int hint) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5058 | // Returning true tells the caller that it need not |
| 5059 | // continue to call IdleNotification. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5060 | i::Isolate* isolate = i::Isolate::Current(); |
| 5061 | if (isolate == NULL || !isolate->IsInitialized()) return true; |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 5062 | if (!i::FLAG_use_idle_notification) return true; |
| 5063 | return isolate->heap()->IdleNotification(hint); |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 5064 | } |
| 5065 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5066 | |
| 5067 | void v8::V8::LowMemoryNotification() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5068 | i::Isolate* isolate = i::Isolate::Current(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5069 | if (isolate == NULL || !isolate->IsInitialized()) return; |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 5070 | isolate->heap()->CollectAllAvailableGarbage("low memory notification"); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 5071 | } |
| 5072 | |
| 5073 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5074 | int v8::V8::ContextDisposedNotification() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5075 | i::Isolate* isolate = i::Isolate::Current(); |
| 5076 | if (!isolate->IsInitialized()) return 0; |
| 5077 | return isolate->heap()->NotifyContextDisposed(); |
kasperl@chromium.org | 8b2bb26 | 2010-03-01 09:46:28 +0000 | [diff] [blame] | 5078 | } |
| 5079 | |
| 5080 | |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5081 | bool v8::V8::InitializeICU() { |
| 5082 | return i::InitializeICU(); |
| 5083 | } |
| 5084 | |
| 5085 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 5086 | const char* v8::V8::GetVersion() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5087 | return i::Version::GetVersion(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 5088 | } |
| 5089 | |
| 5090 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 5091 | static i::Handle<i::Context> CreateEnvironment( |
| 5092 | i::Isolate* isolate, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5093 | v8::ExtensionConfiguration* extensions, |
| 5094 | v8::Handle<ObjectTemplate> global_template, |
| 5095 | v8::Handle<Value> global_object) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 5096 | i::Handle<i::Context> env; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5097 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 5098 | // Enter V8 via an ENTER_V8 scope. |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 5099 | { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5100 | ENTER_V8(isolate); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 5101 | v8::Handle<ObjectTemplate> proxy_template = global_template; |
| 5102 | i::Handle<i::FunctionTemplateInfo> proxy_constructor; |
| 5103 | i::Handle<i::FunctionTemplateInfo> global_constructor; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 5104 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 5105 | if (!global_template.IsEmpty()) { |
| 5106 | // Make sure that the global_template has a constructor. |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 5107 | global_constructor = EnsureConstructor(*global_template); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5108 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 5109 | // Create a fresh template for the global proxy object. |
| 5110 | proxy_template = ObjectTemplate::New(); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 5111 | proxy_constructor = EnsureConstructor(*proxy_template); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5112 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 5113 | // Set the global template to be the prototype template of |
| 5114 | // global proxy template. |
| 5115 | proxy_constructor->set_prototype_template( |
| 5116 | *Utils::OpenHandle(*global_template)); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5117 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 5118 | // Migrate security handlers from global_template to |
| 5119 | // proxy_template. Temporarily removing access check |
| 5120 | // information from the global template. |
| 5121 | if (!global_constructor->access_check_info()->IsUndefined()) { |
| 5122 | proxy_constructor->set_access_check_info( |
| 5123 | global_constructor->access_check_info()); |
| 5124 | proxy_constructor->set_needs_access_check( |
| 5125 | global_constructor->needs_access_check()); |
| 5126 | global_constructor->set_needs_access_check(false); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5127 | global_constructor->set_access_check_info( |
| 5128 | isolate->heap()->undefined_value()); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 5129 | } |
| 5130 | } |
| 5131 | |
| 5132 | // Create the environment. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5133 | env = isolate->bootstrapper()->CreateEnvironment( |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 5134 | Utils::OpenHandle(*global_object, true), |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 5135 | proxy_template, |
| 5136 | extensions); |
| 5137 | |
| 5138 | // Restore the access check info on the global template. |
| 5139 | if (!global_template.IsEmpty()) { |
| 5140 | ASSERT(!global_constructor.is_null()); |
| 5141 | ASSERT(!proxy_constructor.is_null()); |
| 5142 | global_constructor->set_access_check_info( |
| 5143 | proxy_constructor->access_check_info()); |
| 5144 | global_constructor->set_needs_access_check( |
| 5145 | proxy_constructor->needs_access_check()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5146 | } |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5147 | isolate->runtime_profiler()->Reset(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5148 | } |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 5149 | // Leave V8. |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 5150 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 5151 | return env; |
| 5152 | } |
| 5153 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 5154 | Local<Context> v8::Context::New( |
| 5155 | v8::Isolate* external_isolate, |
| 5156 | v8::ExtensionConfiguration* extensions, |
| 5157 | v8::Handle<ObjectTemplate> global_template, |
| 5158 | v8::Handle<Value> global_object) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 5159 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate); |
| 5160 | EnsureInitializedForIsolate(isolate, "v8::Context::New()"); |
| 5161 | LOG_API(isolate, "Context::New"); |
| 5162 | ON_BAILOUT(isolate, "v8::Context::New()", return Local<Context>()); |
| 5163 | i::HandleScope scope(isolate); |
| 5164 | i::Handle<i::Context> env = |
| 5165 | CreateEnvironment(isolate, extensions, global_template, global_object); |
| 5166 | if (env.is_null()) return Local<Context>(); |
| 5167 | return Utils::ToLocal(scope.CloseAndEscape(env)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5168 | } |
| 5169 | |
| 5170 | |
| 5171 | void v8::Context::SetSecurityToken(Handle<Value> token) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5172 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5173 | ENTER_V8(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5174 | i::Handle<i::Context> env = Utils::OpenHandle(this); |
| 5175 | i::Handle<i::Object> token_handle = Utils::OpenHandle(*token); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5176 | env->set_security_token(*token_handle); |
| 5177 | } |
| 5178 | |
| 5179 | |
| 5180 | void v8::Context::UseDefaultSecurityToken() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5181 | i::Isolate* isolate = i::Isolate::Current(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5182 | ENTER_V8(isolate); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5183 | i::Handle<i::Context> env = Utils::OpenHandle(this); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5184 | env->set_security_token(env->global_object()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5185 | } |
| 5186 | |
| 5187 | |
| 5188 | Handle<Value> v8::Context::GetSecurityToken() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5189 | i::Isolate* isolate = i::Isolate::Current(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5190 | i::Handle<i::Context> env = Utils::OpenHandle(this); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5191 | i::Object* security_token = env->security_token(); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 5192 | i::Handle<i::Object> token_handle(security_token, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5193 | return Utils::ToLocal(token_handle); |
| 5194 | } |
| 5195 | |
| 5196 | |
| 5197 | bool Context::HasOutOfMemoryException() { |
| 5198 | i::Handle<i::Context> env = Utils::OpenHandle(this); |
| 5199 | return env->has_out_of_memory(); |
| 5200 | } |
| 5201 | |
| 5202 | |
| 5203 | bool Context::InContext() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5204 | return i::Isolate::Current()->context() != NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5205 | } |
| 5206 | |
| 5207 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 5208 | v8::Isolate* Context::GetIsolate() { |
| 5209 | i::Handle<i::Context> env = Utils::OpenHandle(this); |
| 5210 | return reinterpret_cast<Isolate*>(env->GetIsolate()); |
| 5211 | } |
| 5212 | |
| 5213 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 5214 | v8::Local<v8::Context> Context::GetEntered() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5215 | i::Isolate* isolate = i::Isolate::Current(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5216 | if (!EnsureInitializedForIsolate(isolate, "v8::Context::GetEntered()")) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5217 | return Local<Context>(); |
| 5218 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5219 | return reinterpret_cast<Isolate*>(isolate)->GetEnteredContext(); |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 5220 | } |
| 5221 | |
| 5222 | |
| 5223 | v8::Local<v8::Context> Context::GetCurrent() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5224 | i::Isolate* isolate = i::Isolate::Current(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 5225 | return reinterpret_cast<Isolate*>(isolate)->GetCurrentContext(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5226 | } |
| 5227 | |
| 5228 | |
ager@chromium.org | 1bf0cd0 | 2009-05-20 11:34:19 +0000 | [diff] [blame] | 5229 | v8::Local<v8::Context> Context::GetCalling() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5230 | i::Isolate* isolate = i::Isolate::Current(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5231 | return reinterpret_cast<Isolate*>(isolate)->GetCallingContext(); |
ager@chromium.org | 1bf0cd0 | 2009-05-20 11:34:19 +0000 | [diff] [blame] | 5232 | } |
| 5233 | |
| 5234 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5235 | v8::Local<v8::Object> Context::Global() { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5236 | i::Handle<i::Context> context = Utils::OpenHandle(this); |
| 5237 | i::Isolate* isolate = context->GetIsolate(); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 5238 | i::Handle<i::Object> global(context->global_proxy(), isolate); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5239 | return Utils::ToLocal(i::Handle<i::JSObject>::cast(global)); |
| 5240 | } |
| 5241 | |
| 5242 | |
| 5243 | void Context::DetachGlobal() { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5244 | i::Handle<i::Context> context = Utils::OpenHandle(this); |
| 5245 | i::Isolate* isolate = context->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5246 | ENTER_V8(isolate); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5247 | isolate->bootstrapper()->DetachGlobal(context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5248 | } |
| 5249 | |
| 5250 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 5251 | void Context::ReattachGlobal(Handle<Object> global_object) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5252 | i::Handle<i::Context> context = Utils::OpenHandle(this); |
| 5253 | i::Isolate* isolate = context->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5254 | ENTER_V8(isolate); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 5255 | i::Handle<i::JSGlobalProxy> global_proxy = |
| 5256 | i::Handle<i::JSGlobalProxy>::cast(Utils::OpenHandle(*global_object)); |
| 5257 | isolate->bootstrapper()->ReattachGlobal(context, global_proxy); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 5258 | } |
| 5259 | |
| 5260 | |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 5261 | void Context::AllowCodeGenerationFromStrings(bool allow) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5262 | i::Handle<i::Context> context = Utils::OpenHandle(this); |
| 5263 | i::Isolate* isolate = context->GetIsolate(); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 5264 | ENTER_V8(isolate); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 5265 | context->set_allow_code_gen_from_strings( |
| 5266 | allow ? isolate->heap()->true_value() : isolate->heap()->false_value()); |
| 5267 | } |
| 5268 | |
| 5269 | |
jkummerow@chromium.org | 1145ef8 | 2012-02-02 16:21:15 +0000 | [diff] [blame] | 5270 | bool Context::IsCodeGenerationFromStringsAllowed() { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5271 | i::Handle<i::Context> context = Utils::OpenHandle(this); |
jkummerow@chromium.org | 1145ef8 | 2012-02-02 16:21:15 +0000 | [diff] [blame] | 5272 | return !context->allow_code_gen_from_strings()->IsFalse(); |
| 5273 | } |
| 5274 | |
| 5275 | |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 5276 | void Context::SetErrorMessageForCodeGenerationFromStrings( |
| 5277 | Handle<String> error) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5278 | i::Handle<i::Context> context = Utils::OpenHandle(this); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 5279 | i::Handle<i::String> error_handle = Utils::OpenHandle(*error); |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 5280 | context->set_error_message_for_code_gen_from_strings(*error_handle); |
| 5281 | } |
| 5282 | |
| 5283 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5284 | Local<v8::Object> ObjectTemplate::NewInstance() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5285 | i::Isolate* isolate = i::Isolate::Current(); |
| 5286 | ON_BAILOUT(isolate, "v8::ObjectTemplate::NewInstance()", |
| 5287 | return Local<v8::Object>()); |
| 5288 | LOG_API(isolate, "ObjectTemplate::NewInstance"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5289 | ENTER_V8(isolate); |
| 5290 | EXCEPTION_PREAMBLE(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5291 | i::Handle<i::Object> obj = |
| 5292 | i::Execution::InstantiateObject(Utils::OpenHandle(this), |
| 5293 | &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5294 | EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5295 | return Utils::ToLocal(i::Handle<i::JSObject>::cast(obj)); |
| 5296 | } |
| 5297 | |
| 5298 | |
| 5299 | Local<v8::Function> FunctionTemplate::GetFunction() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5300 | i::Isolate* isolate = i::Isolate::Current(); |
| 5301 | ON_BAILOUT(isolate, "v8::FunctionTemplate::GetFunction()", |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5302 | return Local<v8::Function>()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5303 | LOG_API(isolate, "FunctionTemplate::GetFunction"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5304 | ENTER_V8(isolate); |
| 5305 | EXCEPTION_PREAMBLE(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5306 | i::Handle<i::Object> obj = |
| 5307 | i::Execution::InstantiateFunction(Utils::OpenHandle(this), |
| 5308 | &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5309 | EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Function>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5310 | return Utils::ToLocal(i::Handle<i::JSFunction>::cast(obj)); |
| 5311 | } |
| 5312 | |
| 5313 | |
| 5314 | bool FunctionTemplate::HasInstance(v8::Handle<v8::Value> value) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5315 | ON_BAILOUT(i::Isolate::Current(), "v8::FunctionTemplate::HasInstanceOf()", |
| 5316 | return false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5317 | i::Object* obj = *Utils::OpenHandle(*value); |
| 5318 | return obj->IsInstanceOf(*Utils::OpenHandle(this)); |
| 5319 | } |
| 5320 | |
| 5321 | |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 5322 | Local<External> v8::External::New(void* value) { |
| 5323 | STATIC_ASSERT(sizeof(value) == sizeof(i::Address)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5324 | i::Isolate* isolate = i::Isolate::Current(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5325 | EnsureInitializedForIsolate(isolate, "v8::External::New()"); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 5326 | LOG_API(isolate, "External::New"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5327 | ENTER_V8(isolate); |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 5328 | i::Handle<i::JSObject> external = isolate->factory()->NewExternal(value); |
| 5329 | return Utils::ExternalToLocal(external); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5330 | } |
| 5331 | |
| 5332 | |
| 5333 | void* External::Value() const { |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 5334 | return ExternalValue(*Utils::OpenHandle(this)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5335 | } |
| 5336 | |
| 5337 | |
ager@chromium.org | 563b8dc | 2009-03-20 14:23:52 +0000 | [diff] [blame] | 5338 | Local<String> v8::String::Empty() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5339 | i::Isolate* isolate = i::Isolate::Current(); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 5340 | if (!EnsureInitializedForIsolate(isolate, "v8::String::Empty()")) { |
| 5341 | return v8::Local<String>(); |
| 5342 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5343 | LOG_API(isolate, "String::Empty()"); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 5344 | return Utils::ToLocal(isolate->factory()->empty_string()); |
ager@chromium.org | 563b8dc | 2009-03-20 14:23:52 +0000 | [diff] [blame] | 5345 | } |
| 5346 | |
| 5347 | |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 5348 | // anonymous namespace for string creation helper functions |
| 5349 | namespace { |
| 5350 | |
| 5351 | inline int StringLength(const char* string) { |
| 5352 | return i::StrLength(string); |
| 5353 | } |
| 5354 | |
| 5355 | |
| 5356 | inline int StringLength(const uint8_t* string) { |
| 5357 | return i::StrLength(reinterpret_cast<const char*>(string)); |
| 5358 | } |
| 5359 | |
| 5360 | |
| 5361 | inline int StringLength(const uint16_t* string) { |
| 5362 | int length = 0; |
| 5363 | while (string[length] != '\0') |
| 5364 | length++; |
| 5365 | return length; |
| 5366 | } |
| 5367 | |
| 5368 | |
| 5369 | inline i::Handle<i::String> NewString(i::Factory* factory, |
| 5370 | String::NewStringType type, |
| 5371 | i::Vector<const char> string) { |
| 5372 | if (type ==String::kInternalizedString) { |
| 5373 | return factory->InternalizeUtf8String(string); |
| 5374 | } |
| 5375 | return factory->NewStringFromUtf8(string); |
| 5376 | } |
| 5377 | |
| 5378 | |
| 5379 | inline i::Handle<i::String> NewString(i::Factory* factory, |
| 5380 | String::NewStringType type, |
| 5381 | i::Vector<const uint8_t> string) { |
| 5382 | if (type == String::kInternalizedString) { |
| 5383 | return factory->InternalizeOneByteString(string); |
| 5384 | } |
| 5385 | return factory->NewStringFromOneByte(string); |
| 5386 | } |
| 5387 | |
| 5388 | |
| 5389 | inline i::Handle<i::String> NewString(i::Factory* factory, |
| 5390 | String::NewStringType type, |
| 5391 | i::Vector<const uint16_t> string) { |
| 5392 | if (type == String::kInternalizedString) { |
| 5393 | return factory->InternalizeTwoByteString(string); |
| 5394 | } |
| 5395 | return factory->NewStringFromTwoByte(string); |
| 5396 | } |
| 5397 | |
| 5398 | |
| 5399 | template<typename Char> |
| 5400 | inline Local<String> NewString(Isolate* v8_isolate, |
| 5401 | const char* location, |
| 5402 | const char* env, |
| 5403 | const Char* data, |
| 5404 | String::NewStringType type, |
| 5405 | int length) { |
| 5406 | i::Isolate* isolate = reinterpret_cast<internal::Isolate*>(v8_isolate); |
| 5407 | EnsureInitializedForIsolate(isolate, location); |
| 5408 | LOG_API(isolate, env); |
| 5409 | if (length == 0 && type != String::kUndetectableString) { |
| 5410 | return String::Empty(); |
| 5411 | } |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5412 | ENTER_V8(isolate); |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 5413 | if (length == -1) length = StringLength(data); |
| 5414 | i::Handle<i::String> result = NewString( |
| 5415 | isolate->factory(), type, i::Vector<const Char>(data, length)); |
| 5416 | if (type == String::kUndetectableString) { |
| 5417 | result->MarkAsUndetectable(); |
| 5418 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5419 | return Utils::ToLocal(result); |
| 5420 | } |
| 5421 | |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 5422 | } // anonymous namespace |
| 5423 | |
| 5424 | |
| 5425 | Local<String> String::NewFromUtf8(Isolate* isolate, |
| 5426 | const char* data, |
| 5427 | NewStringType type, |
| 5428 | int length) { |
| 5429 | return NewString(isolate, |
| 5430 | "v8::String::NewFromUtf8()", |
| 5431 | "String::NewFromUtf8", |
| 5432 | data, |
| 5433 | type, |
| 5434 | length); |
| 5435 | } |
| 5436 | |
| 5437 | |
| 5438 | Local<String> String::NewFromOneByte(Isolate* isolate, |
| 5439 | const uint8_t* data, |
| 5440 | NewStringType type, |
| 5441 | int length) { |
| 5442 | return NewString(isolate, |
| 5443 | "v8::String::NewFromOneByte()", |
| 5444 | "String::NewFromOneByte", |
| 5445 | data, |
| 5446 | type, |
| 5447 | length); |
| 5448 | } |
| 5449 | |
| 5450 | |
| 5451 | Local<String> String::NewFromTwoByte(Isolate* isolate, |
| 5452 | const uint16_t* data, |
| 5453 | NewStringType type, |
| 5454 | int length) { |
| 5455 | return NewString(isolate, |
| 5456 | "v8::String::NewFromTwoByte()", |
| 5457 | "String::NewFromTwoByte", |
| 5458 | data, |
| 5459 | type, |
| 5460 | length); |
| 5461 | } |
| 5462 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5463 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5464 | Local<String> v8::String::Concat(Handle<String> left, Handle<String> right) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5465 | i::Handle<i::String> left_string = Utils::OpenHandle(*left); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5466 | i::Isolate* isolate = left_string->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5467 | EnsureInitializedForIsolate(isolate, "v8::String::New()"); |
| 5468 | LOG_API(isolate, "String::New(char)"); |
| 5469 | ENTER_V8(isolate); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5470 | i::Handle<i::String> right_string = Utils::OpenHandle(*right); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5471 | i::Handle<i::String> result = isolate->factory()->NewConsString(left_string, |
| 5472 | right_string); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5473 | return Utils::ToLocal(result); |
| 5474 | } |
| 5475 | |
| 5476 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5477 | i::Handle<i::String> NewExternalStringHandle(i::Isolate* isolate, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5478 | v8::String::ExternalStringResource* resource) { |
| 5479 | i::Handle<i::String> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5480 | isolate->factory()->NewExternalStringFromTwoByte(resource); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5481 | return result; |
| 5482 | } |
| 5483 | |
| 5484 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5485 | i::Handle<i::String> NewExternalAsciiStringHandle(i::Isolate* isolate, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5486 | v8::String::ExternalAsciiStringResource* resource) { |
| 5487 | i::Handle<i::String> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5488 | isolate->factory()->NewExternalStringFromAscii(resource); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5489 | return result; |
| 5490 | } |
| 5491 | |
| 5492 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 5493 | bool RedirectToExternalString(i::Isolate* isolate, |
| 5494 | i::Handle<i::String> parent, |
| 5495 | i::Handle<i::String> external) { |
| 5496 | if (parent->IsConsString()) { |
| 5497 | i::Handle<i::ConsString> cons = i::Handle<i::ConsString>::cast(parent); |
| 5498 | cons->set_first(*external); |
| 5499 | cons->set_second(isolate->heap()->empty_string()); |
| 5500 | } else { |
| 5501 | ASSERT(parent->IsSlicedString()); |
| 5502 | i::Handle<i::SlicedString> slice = i::Handle<i::SlicedString>::cast(parent); |
| 5503 | slice->set_parent(*external); |
| 5504 | slice->set_offset(0); |
| 5505 | } |
| 5506 | return true; |
| 5507 | } |
| 5508 | |
| 5509 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5510 | Local<String> v8::String::NewExternal( |
| 5511 | v8::String::ExternalStringResource* resource) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5512 | i::Isolate* isolate = i::Isolate::Current(); |
| 5513 | EnsureInitializedForIsolate(isolate, "v8::String::NewExternal()"); |
| 5514 | LOG_API(isolate, "String::NewExternal"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5515 | ENTER_V8(isolate); |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 5516 | CHECK(resource && resource->data()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5517 | i::Handle<i::String> result = NewExternalStringHandle(isolate, resource); |
| 5518 | isolate->heap()->external_string_table()->AddString(*result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5519 | return Utils::ToLocal(result); |
| 5520 | } |
| 5521 | |
| 5522 | |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 5523 | bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5524 | i::Handle<i::String> obj = Utils::OpenHandle(this); |
| 5525 | i::Isolate* isolate = obj->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5526 | if (i::StringShape(*obj).IsExternalTwoByte()) { |
| 5527 | return false; // Already an external string. |
| 5528 | } |
| 5529 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5530 | if (isolate->string_tracker()->IsFreshUnusedString(obj)) { |
| 5531 | return false; |
| 5532 | } |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 5533 | if (isolate->heap()->IsInGCPostProcessing()) { |
| 5534 | return false; |
| 5535 | } |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 5536 | CHECK(resource && resource->data()); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 5537 | |
| 5538 | bool result; |
| 5539 | i::Handle<i::String> external; |
| 5540 | if (isolate->heap()->old_pointer_space()->Contains(*obj)) { |
| 5541 | // We do not allow external strings in the old pointer space. Instead of |
| 5542 | // converting the string in-place, we keep the cons/sliced string and |
| 5543 | // point it to a newly-allocated external string. |
| 5544 | external = NewExternalStringHandle(isolate, resource); |
| 5545 | result = RedirectToExternalString(isolate, obj, external); |
| 5546 | } else { |
| 5547 | result = obj->MakeExternal(resource); |
| 5548 | external = obj; |
| 5549 | } |
| 5550 | |
| 5551 | ASSERT(external->IsExternalString()); |
| 5552 | if (result && !external->IsInternalizedString()) { |
| 5553 | isolate->heap()->external_string_table()->AddString(*external); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 5554 | } |
| 5555 | return result; |
| 5556 | } |
| 5557 | |
| 5558 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5559 | Local<String> v8::String::NewExternal( |
| 5560 | v8::String::ExternalAsciiStringResource* resource) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5561 | i::Isolate* isolate = i::Isolate::Current(); |
| 5562 | EnsureInitializedForIsolate(isolate, "v8::String::NewExternal()"); |
| 5563 | LOG_API(isolate, "String::NewExternal"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5564 | ENTER_V8(isolate); |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 5565 | CHECK(resource && resource->data()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5566 | i::Handle<i::String> result = NewExternalAsciiStringHandle(isolate, resource); |
| 5567 | isolate->heap()->external_string_table()->AddString(*result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5568 | return Utils::ToLocal(result); |
| 5569 | } |
| 5570 | |
| 5571 | |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 5572 | bool v8::String::MakeExternal( |
| 5573 | v8::String::ExternalAsciiStringResource* resource) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5574 | i::Handle<i::String> obj = Utils::OpenHandle(this); |
| 5575 | i::Isolate* isolate = obj->GetIsolate(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5576 | if (i::StringShape(*obj).IsExternalTwoByte()) { |
| 5577 | return false; // Already an external string. |
| 5578 | } |
| 5579 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5580 | if (isolate->string_tracker()->IsFreshUnusedString(obj)) { |
| 5581 | return false; |
| 5582 | } |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 5583 | if (isolate->heap()->IsInGCPostProcessing()) { |
| 5584 | return false; |
| 5585 | } |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 5586 | CHECK(resource && resource->data()); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 5587 | |
| 5588 | bool result; |
| 5589 | i::Handle<i::String> external; |
| 5590 | if (isolate->heap()->old_pointer_space()->Contains(*obj)) { |
| 5591 | // We do not allow external strings in the old pointer space. Instead of |
| 5592 | // converting the string in-place, we keep the cons/sliced string and |
| 5593 | // point it to a newly-allocated external string. |
| 5594 | external = NewExternalAsciiStringHandle(isolate, resource); |
| 5595 | result = RedirectToExternalString(isolate, obj, external); |
| 5596 | } else { |
| 5597 | result = obj->MakeExternal(resource); |
| 5598 | external = obj; |
| 5599 | } |
| 5600 | |
| 5601 | ASSERT(external->IsExternalString()); |
| 5602 | if (result && !external->IsInternalizedString()) { |
| 5603 | isolate->heap()->external_string_table()->AddString(*external); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 5604 | } |
| 5605 | return result; |
| 5606 | } |
| 5607 | |
| 5608 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 5609 | bool v8::String::CanMakeExternal() { |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 5610 | if (!internal::FLAG_clever_optimizations) return false; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 5611 | i::Handle<i::String> obj = Utils::OpenHandle(this); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5612 | i::Isolate* isolate = obj->GetIsolate(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5613 | if (isolate->string_tracker()->IsFreshUnusedString(obj)) return false; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 5614 | int size = obj->Size(); // Byte size of the original string. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5615 | if (size < i::ExternalString::kShortSize) return false; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 5616 | i::StringShape shape(*obj); |
| 5617 | return !shape.IsExternal(); |
| 5618 | } |
| 5619 | |
| 5620 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5621 | Local<v8::Object> v8::Object::New() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5622 | i::Isolate* isolate = i::Isolate::Current(); |
| 5623 | EnsureInitializedForIsolate(isolate, "v8::Object::New()"); |
| 5624 | LOG_API(isolate, "Object::New"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5625 | ENTER_V8(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5626 | i::Handle<i::JSObject> obj = |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5627 | isolate->factory()->NewJSObject(isolate->object_function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5628 | return Utils::ToLocal(obj); |
| 5629 | } |
| 5630 | |
| 5631 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 5632 | Local<v8::Value> v8::NumberObject::New(double value) { |
| 5633 | i::Isolate* isolate = i::Isolate::Current(); |
| 5634 | EnsureInitializedForIsolate(isolate, "v8::NumberObject::New()"); |
| 5635 | LOG_API(isolate, "NumberObject::New"); |
| 5636 | ENTER_V8(isolate); |
| 5637 | i::Handle<i::Object> number = isolate->factory()->NewNumber(value); |
| 5638 | i::Handle<i::Object> obj = isolate->factory()->ToObject(number); |
| 5639 | return Utils::ToLocal(obj); |
| 5640 | } |
| 5641 | |
| 5642 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 5643 | double v8::NumberObject::ValueOf() const { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 5644 | i::Isolate* isolate = i::Isolate::Current(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 5645 | LOG_API(isolate, "NumberObject::NumberValue"); |
| 5646 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 5647 | i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj); |
| 5648 | return jsvalue->value()->Number(); |
| 5649 | } |
| 5650 | |
| 5651 | |
| 5652 | Local<v8::Value> v8::BooleanObject::New(bool value) { |
| 5653 | i::Isolate* isolate = i::Isolate::Current(); |
| 5654 | EnsureInitializedForIsolate(isolate, "v8::BooleanObject::New()"); |
| 5655 | LOG_API(isolate, "BooleanObject::New"); |
| 5656 | ENTER_V8(isolate); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 5657 | i::Handle<i::Object> boolean(value |
| 5658 | ? isolate->heap()->true_value() |
| 5659 | : isolate->heap()->false_value(), |
| 5660 | isolate); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 5661 | i::Handle<i::Object> obj = isolate->factory()->ToObject(boolean); |
| 5662 | return Utils::ToLocal(obj); |
| 5663 | } |
| 5664 | |
| 5665 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 5666 | bool v8::BooleanObject::ValueOf() const { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 5667 | i::Isolate* isolate = i::Isolate::Current(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 5668 | LOG_API(isolate, "BooleanObject::BooleanValue"); |
| 5669 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 5670 | i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj); |
| 5671 | return jsvalue->value()->IsTrue(); |
| 5672 | } |
| 5673 | |
| 5674 | |
| 5675 | Local<v8::Value> v8::StringObject::New(Handle<String> value) { |
| 5676 | i::Isolate* isolate = i::Isolate::Current(); |
| 5677 | EnsureInitializedForIsolate(isolate, "v8::StringObject::New()"); |
| 5678 | LOG_API(isolate, "StringObject::New"); |
| 5679 | ENTER_V8(isolate); |
| 5680 | i::Handle<i::Object> obj = |
| 5681 | isolate->factory()->ToObject(Utils::OpenHandle(*value)); |
| 5682 | return Utils::ToLocal(obj); |
| 5683 | } |
| 5684 | |
| 5685 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 5686 | Local<v8::String> v8::StringObject::ValueOf() const { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 5687 | i::Isolate* isolate = i::Isolate::Current(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 5688 | LOG_API(isolate, "StringObject::StringValue"); |
| 5689 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 5690 | i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj); |
| 5691 | return Utils::ToLocal( |
| 5692 | i::Handle<i::String>(i::String::cast(jsvalue->value()))); |
| 5693 | } |
| 5694 | |
| 5695 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 5696 | Local<v8::Value> v8::SymbolObject::New(Isolate* isolate, Handle<Symbol> value) { |
| 5697 | i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 5698 | EnsureInitializedForIsolate(i_isolate, "v8::SymbolObject::New()"); |
| 5699 | LOG_API(i_isolate, "SymbolObject::New"); |
| 5700 | ENTER_V8(i_isolate); |
| 5701 | i::Handle<i::Object> obj = |
| 5702 | i_isolate->factory()->ToObject(Utils::OpenHandle(*value)); |
| 5703 | return Utils::ToLocal(obj); |
| 5704 | } |
| 5705 | |
| 5706 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 5707 | Local<v8::Symbol> v8::SymbolObject::ValueOf() const { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 5708 | i::Isolate* isolate = i::Isolate::Current(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 5709 | LOG_API(isolate, "SymbolObject::SymbolValue"); |
| 5710 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
| 5711 | i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj); |
| 5712 | return Utils::ToLocal( |
| 5713 | i::Handle<i::Symbol>(i::Symbol::cast(jsvalue->value()))); |
| 5714 | } |
| 5715 | |
| 5716 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5717 | Local<v8::Value> v8::Date::New(double time) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5718 | i::Isolate* isolate = i::Isolate::Current(); |
| 5719 | EnsureInitializedForIsolate(isolate, "v8::Date::New()"); |
| 5720 | LOG_API(isolate, "Date::New"); |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 5721 | if (std::isnan(time)) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 5722 | // Introduce only canonical NaN value into the VM, to avoid signaling NaNs. |
| 5723 | time = i::OS::nan_value(); |
| 5724 | } |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5725 | ENTER_V8(isolate); |
| 5726 | EXCEPTION_PREAMBLE(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5727 | i::Handle<i::Object> obj = |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 5728 | i::Execution::NewDate(isolate, time, &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5729 | EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Value>()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5730 | return Utils::ToLocal(obj); |
| 5731 | } |
| 5732 | |
| 5733 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 5734 | double v8::Date::ValueOf() const { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5735 | i::Isolate* isolate = i::Isolate::Current(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5736 | LOG_API(isolate, "Date::NumberValue"); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5737 | i::Handle<i::Object> obj = Utils::OpenHandle(this); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 5738 | i::Handle<i::JSDate> jsdate = i::Handle<i::JSDate>::cast(obj); |
| 5739 | return jsdate->value()->Number(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5740 | } |
| 5741 | |
| 5742 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 5743 | void v8::Date::DateTimeConfigurationChangeNotification() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5744 | i::Isolate* isolate = i::Isolate::Current(); |
| 5745 | ON_BAILOUT(isolate, "v8::Date::DateTimeConfigurationChangeNotification()", |
| 5746 | return); |
| 5747 | LOG_API(isolate, "Date::DateTimeConfigurationChangeNotification"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5748 | ENTER_V8(isolate); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 5749 | |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 5750 | isolate->date_cache()->ResetDateCache(); |
| 5751 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5752 | i::HandleScope scope(isolate); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 5753 | // Get the function ResetDateCache (defined in date.js). |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 5754 | i::Handle<i::String> func_name_str = |
| 5755 | isolate->factory()->InternalizeOneByteString( |
| 5756 | STATIC_ASCII_VECTOR("ResetDateCache")); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5757 | i::MaybeObject* result = |
| 5758 | isolate->js_builtins_object()->GetProperty(*func_name_str); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 5759 | i::Object* object_func; |
| 5760 | if (!result->ToObject(&object_func)) { |
| 5761 | return; |
| 5762 | } |
| 5763 | |
| 5764 | if (object_func->IsJSFunction()) { |
| 5765 | i::Handle<i::JSFunction> func = |
| 5766 | i::Handle<i::JSFunction>(i::JSFunction::cast(object_func)); |
| 5767 | |
| 5768 | // Call ResetDateCache(0 but expect no exceptions: |
| 5769 | bool caught_exception = false; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5770 | i::Execution::TryCall(func, |
| 5771 | isolate->js_builtins_object(), |
| 5772 | 0, |
| 5773 | NULL, |
| 5774 | &caught_exception); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 5775 | } |
| 5776 | } |
| 5777 | |
| 5778 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5779 | static i::Handle<i::String> RegExpFlagsToString(RegExp::Flags flags) { |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 5780 | i::Isolate* isolate = i::Isolate::Current(); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 5781 | uint8_t flags_buf[3]; |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5782 | int num_flags = 0; |
| 5783 | if ((flags & RegExp::kGlobal) != 0) flags_buf[num_flags++] = 'g'; |
| 5784 | if ((flags & RegExp::kMultiline) != 0) flags_buf[num_flags++] = 'm'; |
| 5785 | if ((flags & RegExp::kIgnoreCase) != 0) flags_buf[num_flags++] = 'i'; |
| 5786 | ASSERT(num_flags <= static_cast<int>(ARRAY_SIZE(flags_buf))); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 5787 | return isolate->factory()->InternalizeOneByteString( |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 5788 | i::Vector<const uint8_t>(flags_buf, num_flags)); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5789 | } |
| 5790 | |
| 5791 | |
| 5792 | Local<v8::RegExp> v8::RegExp::New(Handle<String> pattern, |
| 5793 | Flags flags) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5794 | i::Isolate* isolate = Utils::OpenHandle(*pattern)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5795 | EnsureInitializedForIsolate(isolate, "v8::RegExp::New()"); |
| 5796 | LOG_API(isolate, "RegExp::New"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5797 | ENTER_V8(isolate); |
| 5798 | EXCEPTION_PREAMBLE(isolate); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5799 | i::Handle<i::JSRegExp> obj = i::Execution::NewJSRegExp( |
| 5800 | Utils::OpenHandle(*pattern), |
| 5801 | RegExpFlagsToString(flags), |
| 5802 | &has_pending_exception); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5803 | EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::RegExp>()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5804 | return Utils::ToLocal(i::Handle<i::JSRegExp>::cast(obj)); |
| 5805 | } |
| 5806 | |
| 5807 | |
| 5808 | Local<v8::String> v8::RegExp::GetSource() const { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5809 | i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this); |
| 5810 | return Utils::ToLocal(i::Handle<i::String>(obj->Pattern())); |
| 5811 | } |
| 5812 | |
| 5813 | |
| 5814 | // Assert that the static flags cast in GetFlags is valid. |
| 5815 | #define REGEXP_FLAG_ASSERT_EQ(api_flag, internal_flag) \ |
| 5816 | STATIC_ASSERT(static_cast<int>(v8::RegExp::api_flag) == \ |
| 5817 | static_cast<int>(i::JSRegExp::internal_flag)) |
| 5818 | REGEXP_FLAG_ASSERT_EQ(kNone, NONE); |
| 5819 | REGEXP_FLAG_ASSERT_EQ(kGlobal, GLOBAL); |
| 5820 | REGEXP_FLAG_ASSERT_EQ(kIgnoreCase, IGNORE_CASE); |
| 5821 | REGEXP_FLAG_ASSERT_EQ(kMultiline, MULTILINE); |
| 5822 | #undef REGEXP_FLAG_ASSERT_EQ |
| 5823 | |
| 5824 | v8::RegExp::Flags v8::RegExp::GetFlags() const { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 5825 | i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this); |
| 5826 | return static_cast<RegExp::Flags>(obj->GetFlags().value()); |
| 5827 | } |
| 5828 | |
| 5829 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5830 | Local<v8::Array> v8::Array::New(int length) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5831 | i::Isolate* isolate = i::Isolate::Current(); |
| 5832 | EnsureInitializedForIsolate(isolate, "v8::Array::New()"); |
| 5833 | LOG_API(isolate, "Array::New"); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5834 | ENTER_V8(isolate); |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 5835 | int real_length = length > 0 ? length : 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5836 | i::Handle<i::JSArray> obj = isolate->factory()->NewJSArray(real_length); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5837 | i::Handle<i::Object> length_obj = |
| 5838 | isolate->factory()->NewNumberFromInt(real_length); |
| 5839 | obj->set_length(*length_obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5840 | return Utils::ToLocal(obj); |
| 5841 | } |
| 5842 | |
| 5843 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 5844 | uint32_t v8::Array::Length() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5845 | i::Handle<i::JSArray> obj = Utils::OpenHandle(this); |
| 5846 | i::Object* length = obj->length(); |
| 5847 | if (length->IsSmi()) { |
| 5848 | return i::Smi::cast(length)->value(); |
| 5849 | } else { |
| 5850 | return static_cast<uint32_t>(length->Number()); |
| 5851 | } |
| 5852 | } |
| 5853 | |
| 5854 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 5855 | Local<Object> Array::CloneElementAt(uint32_t index) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5856 | i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5857 | ON_BAILOUT(isolate, "v8::Array::CloneElementAt()", return Local<Object>()); |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 5858 | i::Handle<i::JSObject> self = Utils::OpenHandle(this); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5859 | if (!self->HasFastObjectElements()) { |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 5860 | return Local<Object>(); |
| 5861 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 5862 | i::FixedArray* elms = i::FixedArray::cast(self->elements()); |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 5863 | i::Object* paragon = elms->get(index); |
| 5864 | if (!paragon->IsJSObject()) { |
| 5865 | return Local<Object>(); |
| 5866 | } |
| 5867 | i::Handle<i::JSObject> paragon_handle(i::JSObject::cast(paragon)); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5868 | EXCEPTION_PREAMBLE(isolate); |
| 5869 | ENTER_V8(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5870 | i::Handle<i::JSObject> result = i::JSObject::Copy(paragon_handle); |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 5871 | has_pending_exception = result.is_null(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5872 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>()); |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 5873 | return Utils::ToLocal(result); |
| 5874 | } |
| 5875 | |
| 5876 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5877 | bool v8::ArrayBuffer::IsExternal() const { |
| 5878 | return Utils::OpenHandle(this)->is_external(); |
| 5879 | } |
| 5880 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 5881 | |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 5882 | v8::ArrayBuffer::Contents v8::ArrayBuffer::Externalize() { |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5883 | i::Handle<i::JSArrayBuffer> obj = Utils::OpenHandle(this); |
| 5884 | ApiCheck(!obj->is_external(), |
| 5885 | "v8::ArrayBuffer::Externalize", |
| 5886 | "ArrayBuffer already externalized"); |
| 5887 | obj->set_is_external(true); |
| 5888 | size_t byte_length = static_cast<size_t>(obj->byte_length()->Number()); |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 5889 | Contents contents; |
| 5890 | contents.data_ = obj->backing_store(); |
| 5891 | contents.byte_length_ = byte_length; |
| 5892 | return contents; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5893 | } |
| 5894 | |
| 5895 | |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 5896 | void v8::ArrayBuffer::Neuter() { |
| 5897 | i::Handle<i::JSArrayBuffer> obj = Utils::OpenHandle(this); |
| 5898 | i::Isolate* isolate = obj->GetIsolate(); |
| 5899 | ApiCheck(obj->is_external(), |
| 5900 | "v8::ArrayBuffer::Neuter", |
| 5901 | "Only externalized ArrayBuffers can be neutered"); |
| 5902 | LOG_API(obj->GetIsolate(), "v8::ArrayBuffer::Neuter()"); |
| 5903 | ENTER_V8(isolate); |
| 5904 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5905 | for (i::Handle<i::Object> view_obj(obj->weak_first_view(), isolate); |
| 5906 | !view_obj->IsUndefined();) { |
| 5907 | i::Handle<i::JSArrayBufferView> view(i::JSArrayBufferView::cast(*view_obj)); |
| 5908 | if (view->IsJSTypedArray()) { |
| 5909 | i::JSTypedArray::cast(*view)->Neuter(); |
| 5910 | } else if (view->IsJSDataView()) { |
| 5911 | i::JSDataView::cast(*view)->Neuter(); |
| 5912 | } else { |
| 5913 | UNREACHABLE(); |
| 5914 | } |
| 5915 | view_obj = i::handle(view->weak_next(), isolate); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 5916 | } |
| 5917 | obj->Neuter(); |
| 5918 | } |
| 5919 | |
| 5920 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 5921 | size_t v8::ArrayBuffer::ByteLength() const { |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 5922 | i::Handle<i::JSArrayBuffer> obj = Utils::OpenHandle(this); |
| 5923 | return static_cast<size_t>(obj->byte_length()->Number()); |
| 5924 | } |
| 5925 | |
| 5926 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 5927 | Local<ArrayBuffer> v8::ArrayBuffer::New(size_t byte_length) { |
| 5928 | i::Isolate* isolate = i::Isolate::Current(); |
| 5929 | EnsureInitializedForIsolate(isolate, "v8::ArrayBuffer::New(size_t)"); |
| 5930 | LOG_API(isolate, "v8::ArrayBuffer::New(size_t)"); |
| 5931 | ENTER_V8(isolate); |
| 5932 | i::Handle<i::JSArrayBuffer> obj = |
| 5933 | isolate->factory()->NewJSArrayBuffer(); |
| 5934 | i::Runtime::SetupArrayBufferAllocatingData(isolate, obj, byte_length); |
| 5935 | return Utils::ToLocal(obj); |
| 5936 | } |
| 5937 | |
| 5938 | |
| 5939 | Local<ArrayBuffer> v8::ArrayBuffer::New(void* data, size_t byte_length) { |
| 5940 | i::Isolate* isolate = i::Isolate::Current(); |
| 5941 | EnsureInitializedForIsolate(isolate, "v8::ArrayBuffer::New(void*, size_t)"); |
| 5942 | LOG_API(isolate, "v8::ArrayBuffer::New(void*, size_t)"); |
| 5943 | ENTER_V8(isolate); |
| 5944 | i::Handle<i::JSArrayBuffer> obj = |
| 5945 | isolate->factory()->NewJSArrayBuffer(); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5946 | i::Runtime::SetupArrayBuffer(isolate, obj, true, data, byte_length); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 5947 | return Utils::ToLocal(obj); |
| 5948 | } |
| 5949 | |
| 5950 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5951 | Local<ArrayBuffer> v8::ArrayBufferView::Buffer() { |
| 5952 | i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 5953 | ASSERT(obj->buffer()->IsJSArrayBuffer()); |
| 5954 | i::Handle<i::JSArrayBuffer> buffer(i::JSArrayBuffer::cast(obj->buffer())); |
| 5955 | return Utils::ToLocal(buffer); |
| 5956 | } |
| 5957 | |
| 5958 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5959 | size_t v8::ArrayBufferView::ByteOffset() { |
| 5960 | i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 5961 | return static_cast<size_t>(obj->byte_offset()->Number()); |
| 5962 | } |
| 5963 | |
| 5964 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5965 | size_t v8::ArrayBufferView::ByteLength() { |
| 5966 | i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 5967 | return static_cast<size_t>(obj->byte_length()->Number()); |
| 5968 | } |
| 5969 | |
| 5970 | |
| 5971 | size_t v8::TypedArray::Length() { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 5972 | i::Handle<i::JSTypedArray> obj = Utils::OpenHandle(this); |
| 5973 | return static_cast<size_t>(obj->length()->Number()); |
| 5974 | } |
| 5975 | |
| 5976 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5977 | static inline void SetupArrayBufferView( |
| 5978 | i::Isolate* isolate, |
| 5979 | i::Handle<i::JSArrayBufferView> obj, |
| 5980 | i::Handle<i::JSArrayBuffer> buffer, |
| 5981 | size_t byte_offset, |
| 5982 | size_t byte_length) { |
| 5983 | ASSERT(byte_offset + byte_length <= |
| 5984 | static_cast<size_t>(buffer->byte_length()->Number())); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 5985 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5986 | obj->set_buffer(*buffer); |
| 5987 | |
| 5988 | obj->set_weak_next(buffer->weak_first_view()); |
| 5989 | buffer->set_weak_first_view(*obj); |
| 5990 | |
| 5991 | i::Handle<i::Object> byte_offset_object = |
| 5992 | isolate->factory()->NewNumberFromSize(byte_offset); |
| 5993 | obj->set_byte_offset(*byte_offset_object); |
| 5994 | |
| 5995 | i::Handle<i::Object> byte_length_object = |
| 5996 | isolate->factory()->NewNumberFromSize(byte_length); |
| 5997 | obj->set_byte_length(*byte_length_object); |
| 5998 | } |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 5999 | |
| 6000 | template<typename ElementType, |
| 6001 | ExternalArrayType array_type, |
| 6002 | i::ElementsKind elements_kind> |
| 6003 | i::Handle<i::JSTypedArray> NewTypedArray( |
| 6004 | i::Isolate* isolate, |
| 6005 | Handle<ArrayBuffer> array_buffer, size_t byte_offset, size_t length) { |
| 6006 | i::Handle<i::JSTypedArray> obj = |
| 6007 | isolate->factory()->NewJSTypedArray(array_type); |
| 6008 | i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*array_buffer); |
| 6009 | |
| 6010 | ASSERT(byte_offset % sizeof(ElementType) == 0); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6011 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6012 | SetupArrayBufferView( |
| 6013 | isolate, obj, buffer, byte_offset, length * sizeof(ElementType)); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6014 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6015 | i::Handle<i::Object> length_object = |
| 6016 | isolate->factory()->NewNumberFromSize(length); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6017 | obj->set_length(*length_object); |
| 6018 | |
| 6019 | i::Handle<i::ExternalArray> elements = |
| 6020 | isolate->factory()->NewExternalArray( |
| 6021 | static_cast<int>(length), array_type, |
| 6022 | static_cast<uint8_t*>(buffer->backing_store()) + byte_offset); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6023 | obj->set_elements(*elements); |
| 6024 | return obj; |
| 6025 | } |
| 6026 | |
| 6027 | |
| 6028 | #define TYPED_ARRAY_NEW(TypedArray, element_type, array_type, elements_kind) \ |
| 6029 | Local<TypedArray> TypedArray::New(Handle<ArrayBuffer> array_buffer, \ |
| 6030 | size_t byte_offset, size_t length) { \ |
| 6031 | i::Isolate* isolate = i::Isolate::Current(); \ |
| 6032 | EnsureInitializedForIsolate(isolate, \ |
| 6033 | "v8::" #TypedArray "::New(Handle<ArrayBuffer>, size_t, size_t)"); \ |
| 6034 | LOG_API(isolate, \ |
| 6035 | "v8::" #TypedArray "::New(Handle<ArrayBuffer>, size_t, size_t)"); \ |
| 6036 | ENTER_V8(isolate); \ |
| 6037 | i::Handle<i::JSTypedArray> obj = \ |
| 6038 | NewTypedArray<element_type, array_type, elements_kind>( \ |
| 6039 | isolate, array_buffer, byte_offset, length); \ |
| 6040 | return Utils::ToLocal##TypedArray(obj); \ |
| 6041 | } |
| 6042 | |
| 6043 | |
| 6044 | TYPED_ARRAY_NEW(Uint8Array, uint8_t, kExternalUnsignedByteArray, |
| 6045 | i::EXTERNAL_UNSIGNED_BYTE_ELEMENTS) |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6046 | TYPED_ARRAY_NEW(Uint8ClampedArray, uint8_t, kExternalPixelArray, |
| 6047 | i::EXTERNAL_PIXEL_ELEMENTS) |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6048 | TYPED_ARRAY_NEW(Int8Array, int8_t, kExternalByteArray, |
| 6049 | i::EXTERNAL_BYTE_ELEMENTS) |
| 6050 | TYPED_ARRAY_NEW(Uint16Array, uint16_t, kExternalUnsignedShortArray, |
| 6051 | i::EXTERNAL_UNSIGNED_SHORT_ELEMENTS) |
| 6052 | TYPED_ARRAY_NEW(Int16Array, int16_t, kExternalShortArray, |
| 6053 | i::EXTERNAL_SHORT_ELEMENTS) |
| 6054 | TYPED_ARRAY_NEW(Uint32Array, uint32_t, kExternalUnsignedIntArray, |
| 6055 | i::EXTERNAL_UNSIGNED_INT_ELEMENTS) |
| 6056 | TYPED_ARRAY_NEW(Int32Array, int32_t, kExternalIntArray, |
| 6057 | i::EXTERNAL_INT_ELEMENTS) |
| 6058 | TYPED_ARRAY_NEW(Float32Array, float, kExternalFloatArray, |
| 6059 | i::EXTERNAL_FLOAT_ELEMENTS) |
| 6060 | TYPED_ARRAY_NEW(Float64Array, double, kExternalDoubleArray, |
| 6061 | i::EXTERNAL_DOUBLE_ELEMENTS) |
| 6062 | |
| 6063 | #undef TYPED_ARRAY_NEW |
| 6064 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6065 | Local<DataView> DataView::New(Handle<ArrayBuffer> array_buffer, |
| 6066 | size_t byte_offset, size_t byte_length) { |
| 6067 | i::Isolate* isolate = i::Isolate::Current(); |
| 6068 | EnsureInitializedForIsolate( |
| 6069 | isolate, "v8::DataView::New(void*, size_t, size_t)"); |
| 6070 | LOG_API(isolate, "v8::DataView::New(void*, size_t, size_t)"); |
| 6071 | ENTER_V8(isolate); |
| 6072 | i::Handle<i::JSDataView> obj = isolate->factory()->NewJSDataView(); |
| 6073 | i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*array_buffer); |
| 6074 | SetupArrayBufferView( |
| 6075 | isolate, obj, buffer, byte_offset, byte_length); |
| 6076 | return Utils::ToLocal(obj); |
| 6077 | } |
| 6078 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6079 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 6080 | Local<Symbol> v8::Symbol::New(Isolate* isolate) { |
| 6081 | i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 6082 | EnsureInitializedForIsolate(i_isolate, "v8::Symbol::New()"); |
| 6083 | LOG_API(i_isolate, "Symbol::New()"); |
| 6084 | ENTER_V8(i_isolate); |
| 6085 | i::Handle<i::Symbol> result = i_isolate->factory()->NewSymbol(); |
| 6086 | return Utils::ToLocal(result); |
| 6087 | } |
| 6088 | |
| 6089 | |
| 6090 | Local<Symbol> v8::Symbol::New(Isolate* isolate, const char* data, int length) { |
| 6091 | i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 6092 | EnsureInitializedForIsolate(i_isolate, "v8::Symbol::New()"); |
| 6093 | LOG_API(i_isolate, "Symbol::New(char)"); |
| 6094 | ENTER_V8(i_isolate); |
| 6095 | if (length == -1) length = i::StrLength(data); |
| 6096 | i::Handle<i::String> name = i_isolate->factory()->NewStringFromUtf8( |
| 6097 | i::Vector<const char>(data, length)); |
| 6098 | i::Handle<i::Symbol> result = i_isolate->factory()->NewSymbol(); |
| 6099 | result->set_name(*name); |
| 6100 | return Utils::ToLocal(result); |
| 6101 | } |
| 6102 | |
| 6103 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6104 | Local<Number> v8::Number::New(double value) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6105 | i::Isolate* isolate = i::Isolate::Current(); |
| 6106 | EnsureInitializedForIsolate(isolate, "v8::Number::New()"); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 6107 | return Number::New(reinterpret_cast<Isolate*>(isolate), value); |
| 6108 | } |
| 6109 | |
| 6110 | |
| 6111 | Local<Number> v8::Number::New(Isolate* isolate, double value) { |
| 6112 | i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 6113 | ASSERT(internal_isolate->IsInitialized()); |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 6114 | if (std::isnan(value)) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 6115 | // Introduce only canonical NaN value into the VM, to avoid signaling NaNs. |
| 6116 | value = i::OS::nan_value(); |
| 6117 | } |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 6118 | ENTER_V8(internal_isolate); |
| 6119 | i::Handle<i::Object> result = internal_isolate->factory()->NewNumber(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6120 | return Utils::NumberToLocal(result); |
| 6121 | } |
| 6122 | |
| 6123 | |
| 6124 | Local<Integer> v8::Integer::New(int32_t value) { |
danno@chromium.org | 8c0a43f | 2012-04-03 08:37:53 +0000 | [diff] [blame] | 6125 | i::Isolate* isolate = i::Isolate::UncheckedCurrent(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6126 | EnsureInitializedForIsolate(isolate, "v8::Integer::New()"); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6127 | return v8::Integer::New(value, reinterpret_cast<Isolate*>(isolate)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6128 | } |
| 6129 | |
| 6130 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 6131 | Local<Integer> Integer::NewFromUnsigned(uint32_t value) { |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6132 | i::Isolate* isolate = i::Isolate::Current(); |
| 6133 | EnsureInitializedForIsolate(isolate, "v8::Integer::NewFromUnsigned()"); |
| 6134 | return Integer::NewFromUnsigned(value, reinterpret_cast<Isolate*>(isolate)); |
| 6135 | } |
| 6136 | |
| 6137 | |
| 6138 | Local<Integer> v8::Integer::New(int32_t value, Isolate* isolate) { |
| 6139 | i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 6140 | ASSERT(internal_isolate->IsInitialized()); |
| 6141 | if (i::Smi::IsValid(value)) { |
| 6142 | return Utils::IntegerToLocal(i::Handle<i::Object>(i::Smi::FromInt(value), |
| 6143 | internal_isolate)); |
| 6144 | } |
| 6145 | ENTER_V8(internal_isolate); |
| 6146 | i::Handle<i::Object> result = internal_isolate->factory()->NewNumber(value); |
| 6147 | return Utils::IntegerToLocal(result); |
| 6148 | } |
| 6149 | |
| 6150 | |
| 6151 | Local<Integer> v8::Integer::NewFromUnsigned(uint32_t value, Isolate* isolate) { |
| 6152 | i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 6153 | ASSERT(internal_isolate->IsInitialized()); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 6154 | bool fits_into_int32_t = (value & (1 << 31)) == 0; |
| 6155 | if (fits_into_int32_t) { |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6156 | return Integer::New(static_cast<int32_t>(value), isolate); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 6157 | } |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6158 | ENTER_V8(internal_isolate); |
| 6159 | i::Handle<i::Object> result = internal_isolate->factory()->NewNumber(value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 6160 | return Utils::IntegerToLocal(result); |
| 6161 | } |
| 6162 | |
| 6163 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6164 | #ifdef DEBUG |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 6165 | v8::AssertNoGCScope::AssertNoGCScope(v8::Isolate* isolate) { |
| 6166 | disallow_heap_allocation_ = new i::DisallowHeapAllocation(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6167 | } |
| 6168 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 6169 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6170 | v8::AssertNoGCScope::~AssertNoGCScope() { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 6171 | delete static_cast<i::DisallowHeapAllocation*>(disallow_heap_allocation_); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6172 | } |
| 6173 | #endif |
| 6174 | |
| 6175 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6176 | void V8::IgnoreOutOfMemoryException() { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 6177 | EnterIsolateIfNeeded()->set_ignore_out_of_memory(true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6178 | } |
| 6179 | |
| 6180 | |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 6181 | bool V8::AddMessageListener(MessageCallback that, Handle<Value> data) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6182 | i::Isolate* isolate = i::Isolate::Current(); |
| 6183 | EnsureInitializedForIsolate(isolate, "v8::V8::AddMessageListener()"); |
| 6184 | ON_BAILOUT(isolate, "v8::V8::AddMessageListener()", return false); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6185 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6186 | i::HandleScope scope(isolate); |
| 6187 | NeanderArray listeners(isolate->factory()->message_listeners()); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 6188 | NeanderObject obj(2); |
| 6189 | obj.set(0, *isolate->factory()->NewForeign(FUNCTION_ADDR(that))); |
| 6190 | obj.set(1, data.IsEmpty() ? isolate->heap()->undefined_value() |
| 6191 | : *Utils::OpenHandle(*data)); |
| 6192 | listeners.add(obj.value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6193 | return true; |
| 6194 | } |
| 6195 | |
| 6196 | |
| 6197 | void V8::RemoveMessageListeners(MessageCallback that) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6198 | i::Isolate* isolate = i::Isolate::Current(); |
| 6199 | EnsureInitializedForIsolate(isolate, "v8::V8::RemoveMessageListener()"); |
| 6200 | ON_BAILOUT(isolate, "v8::V8::RemoveMessageListeners()", return); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6201 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6202 | i::HandleScope scope(isolate); |
| 6203 | NeanderArray listeners(isolate->factory()->message_listeners()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6204 | for (int i = 0; i < listeners.length(); i++) { |
| 6205 | if (listeners.get(i)->IsUndefined()) continue; // skip deleted ones |
| 6206 | |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 6207 | NeanderObject listener(i::JSObject::cast(listeners.get(i))); |
| 6208 | i::Handle<i::Foreign> callback_obj(i::Foreign::cast(listener.get(0))); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 6209 | if (callback_obj->foreign_address() == FUNCTION_ADDR(that)) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6210 | listeners.set(i, isolate->heap()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6211 | } |
| 6212 | } |
| 6213 | } |
| 6214 | |
| 6215 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 6216 | void V8::SetCaptureStackTraceForUncaughtExceptions( |
| 6217 | bool capture, |
| 6218 | int frame_limit, |
| 6219 | StackTrace::StackTraceOptions options) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6220 | i::Isolate::Current()->SetCaptureStackTraceForUncaughtExceptions( |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 6221 | capture, |
| 6222 | frame_limit, |
| 6223 | options); |
| 6224 | } |
| 6225 | |
| 6226 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6227 | void V8::SetCounterFunction(CounterLookupCallback callback) { |
sgjesse@chromium.org | e059905 | 2011-03-25 07:34:35 +0000 | [diff] [blame] | 6228 | i::Isolate* isolate = EnterIsolateIfNeeded(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6229 | isolate->stats_table()->SetCounterFunction(callback); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6230 | } |
| 6231 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 6232 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6233 | void V8::SetCreateHistogramFunction(CreateHistogramCallback callback) { |
sgjesse@chromium.org | e059905 | 2011-03-25 07:34:35 +0000 | [diff] [blame] | 6234 | i::Isolate* isolate = EnterIsolateIfNeeded(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6235 | isolate->stats_table()->SetCreateHistogramFunction(callback); |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 6236 | isolate->InitializeLoggingAndCounters(); |
| 6237 | isolate->counters()->ResetHistograms(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6238 | } |
| 6239 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 6240 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6241 | void V8::SetAddHistogramSampleFunction(AddHistogramSampleCallback callback) { |
sgjesse@chromium.org | e059905 | 2011-03-25 07:34:35 +0000 | [diff] [blame] | 6242 | i::Isolate* isolate = EnterIsolateIfNeeded(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6243 | isolate->stats_table()-> |
| 6244 | SetAddHistogramSampleFunction(callback); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6245 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6246 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6247 | void V8::SetFailedAccessCheckCallbackFunction( |
| 6248 | FailedAccessCheckCallback callback) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6249 | i::Isolate* isolate = i::Isolate::Current(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6250 | isolate->SetFailedAccessCheckCallback(callback); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6251 | } |
| 6252 | |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 6253 | |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6254 | intptr_t Isolate::AdjustAmountOfExternalAllocatedMemory( |
| 6255 | intptr_t change_in_bytes) { |
| 6256 | i::Heap* heap = reinterpret_cast<i::Isolate*>(this)->heap(); |
| 6257 | return heap->AdjustAmountOfExternalAllocatedMemory(change_in_bytes); |
| 6258 | } |
| 6259 | |
| 6260 | |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6261 | intptr_t V8::AdjustAmountOfExternalAllocatedMemory(intptr_t change_in_bytes) { |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 6262 | i::Isolate* isolate = i::Isolate::UncheckedCurrent(); |
machenbach@chromium.org | ae16103 | 2013-09-24 09:12:30 +0000 | [diff] [blame] | 6263 | if (isolate == NULL || !isolate->IsInitialized()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6264 | return 0; |
| 6265 | } |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6266 | Isolate* isolate_ext = reinterpret_cast<Isolate*>(isolate); |
| 6267 | return isolate_ext->AdjustAmountOfExternalAllocatedMemory(change_in_bytes); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 6268 | } |
| 6269 | |
| 6270 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 6271 | HeapProfiler* Isolate::GetHeapProfiler() { |
| 6272 | i::HeapProfiler* heap_profiler = |
| 6273 | reinterpret_cast<i::Isolate*>(this)->heap_profiler(); |
| 6274 | return reinterpret_cast<HeapProfiler*>(heap_profiler); |
| 6275 | } |
| 6276 | |
| 6277 | |
| 6278 | CpuProfiler* Isolate::GetCpuProfiler() { |
| 6279 | i::CpuProfiler* cpu_profiler = |
| 6280 | reinterpret_cast<i::Isolate*>(this)->cpu_profiler(); |
| 6281 | return reinterpret_cast<CpuProfiler*>(cpu_profiler); |
| 6282 | } |
| 6283 | |
| 6284 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6285 | bool Isolate::InContext() { |
| 6286 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 6287 | return isolate->context() != NULL; |
| 6288 | } |
| 6289 | |
| 6290 | |
mvstanton@chromium.org | 40ce96b | 2013-04-09 09:52:22 +0000 | [diff] [blame] | 6291 | v8::Local<v8::Context> Isolate::GetCurrentContext() { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6292 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 6293 | i::Context* context = isolate->context(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6294 | if (context == NULL) return Local<Context>(); |
| 6295 | i::Context* native_context = context->global_object()->native_context(); |
| 6296 | if (native_context == NULL) return Local<Context>(); |
| 6297 | return Utils::ToLocal(i::Handle<i::Context>(native_context)); |
mvstanton@chromium.org | 40ce96b | 2013-04-09 09:52:22 +0000 | [diff] [blame] | 6298 | } |
| 6299 | |
| 6300 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6301 | v8::Local<v8::Context> Isolate::GetCallingContext() { |
| 6302 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 6303 | i::Handle<i::Object> calling = isolate->GetCallingNativeContext(); |
| 6304 | if (calling.is_null()) return Local<Context>(); |
| 6305 | return Utils::ToLocal(i::Handle<i::Context>::cast(calling)); |
| 6306 | } |
| 6307 | |
| 6308 | |
| 6309 | v8::Local<v8::Context> Isolate::GetEnteredContext() { |
| 6310 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 6311 | i::Handle<i::Object> last = |
| 6312 | isolate->handle_scope_implementer()->LastEnteredContext(); |
| 6313 | if (last.is_null()) return Local<Context>(); |
| 6314 | return Utils::ToLocal(i::Handle<i::Context>::cast(last)); |
| 6315 | } |
| 6316 | |
| 6317 | |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 6318 | v8::Local<Value> Isolate::ThrowException(v8::Local<v8::Value> value) { |
| 6319 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 6320 | ENTER_V8(isolate); |
| 6321 | // If we're passed an empty handle, we throw an undefined exception |
| 6322 | // to deal more gracefully with out of memory situations. |
| 6323 | if (value.IsEmpty()) { |
| 6324 | isolate->ScheduleThrow(isolate->heap()->undefined_value()); |
| 6325 | } else { |
| 6326 | isolate->ScheduleThrow(*Utils::OpenHandle(*value)); |
| 6327 | } |
| 6328 | return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate)); |
| 6329 | } |
| 6330 | |
| 6331 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 6332 | void Isolate::SetObjectGroupId(const Persistent<Value>& object, |
| 6333 | UniqueId id) { |
| 6334 | i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(this); |
| 6335 | internal_isolate->global_handles()->SetObjectGroupId( |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 6336 | Utils::OpenPersistent(object).location(), |
| 6337 | id); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 6338 | } |
| 6339 | |
| 6340 | |
| 6341 | void Isolate::SetReferenceFromGroup(UniqueId id, |
| 6342 | const Persistent<Value>& object) { |
| 6343 | i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(this); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 6344 | internal_isolate->global_handles()->SetReferenceFromGroup( |
| 6345 | id, |
| 6346 | Utils::OpenPersistent(object).location()); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 6347 | } |
| 6348 | |
| 6349 | |
| 6350 | void Isolate::SetReference(const Persistent<Object>& parent, |
| 6351 | const Persistent<Value>& child) { |
| 6352 | i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(this); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 6353 | i::Object** parent_location = Utils::OpenPersistent(parent).location(); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 6354 | internal_isolate->global_handles()->SetReference( |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 6355 | reinterpret_cast<i::HeapObject**>(parent_location), |
| 6356 | Utils::OpenPersistent(child).location()); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 6357 | } |
| 6358 | |
| 6359 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6360 | void Isolate::AddGCPrologueCallback(GCPrologueCallback callback, |
| 6361 | GCType gc_type) { |
| 6362 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 6363 | isolate->heap()->AddGCPrologueCallback(callback, gc_type); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6364 | } |
| 6365 | |
| 6366 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6367 | void Isolate::RemoveGCPrologueCallback(GCPrologueCallback callback) { |
| 6368 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 6369 | isolate->heap()->RemoveGCPrologueCallback(callback); |
| 6370 | } |
| 6371 | |
| 6372 | |
| 6373 | void Isolate::AddGCEpilogueCallback(GCEpilogueCallback callback, |
| 6374 | GCType gc_type) { |
| 6375 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 6376 | isolate->heap()->AddGCEpilogueCallback(callback, gc_type); |
| 6377 | } |
| 6378 | |
| 6379 | |
| 6380 | void Isolate::RemoveGCEpilogueCallback(GCEpilogueCallback callback) { |
| 6381 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 6382 | isolate->heap()->RemoveGCEpilogueCallback(callback); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6383 | } |
| 6384 | |
| 6385 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 6386 | void V8::AddGCPrologueCallback(GCPrologueCallback callback, GCType gc_type) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6387 | i::Isolate* isolate = i::Isolate::Current(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6388 | isolate->heap()->AddGCPrologueCallback( |
| 6389 | reinterpret_cast<v8::Isolate::GCPrologueCallback>(callback), |
| 6390 | gc_type, |
| 6391 | false); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 6392 | } |
| 6393 | |
| 6394 | |
| 6395 | void V8::RemoveGCPrologueCallback(GCPrologueCallback callback) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6396 | i::Isolate* isolate = i::Isolate::Current(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6397 | isolate->heap()->RemoveGCPrologueCallback( |
| 6398 | reinterpret_cast<v8::Isolate::GCPrologueCallback>(callback)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 6399 | } |
| 6400 | |
| 6401 | |
| 6402 | void V8::AddGCEpilogueCallback(GCEpilogueCallback callback, GCType gc_type) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6403 | i::Isolate* isolate = i::Isolate::Current(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6404 | isolate->heap()->AddGCEpilogueCallback( |
| 6405 | reinterpret_cast<v8::Isolate::GCEpilogueCallback>(callback), |
| 6406 | gc_type, |
| 6407 | false); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 6408 | } |
| 6409 | |
| 6410 | |
| 6411 | void V8::RemoveGCEpilogueCallback(GCEpilogueCallback callback) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6412 | i::Isolate* isolate = i::Isolate::Current(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6413 | isolate->heap()->RemoveGCEpilogueCallback( |
| 6414 | reinterpret_cast<v8::Isolate::GCEpilogueCallback>(callback)); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 6415 | } |
| 6416 | |
| 6417 | |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 6418 | void V8::AddMemoryAllocationCallback(MemoryAllocationCallback callback, |
| 6419 | ObjectSpace space, |
| 6420 | AllocationAction action) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6421 | i::Isolate* isolate = i::Isolate::Current(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6422 | isolate->memory_allocator()->AddMemoryAllocationCallback( |
| 6423 | callback, space, action); |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 6424 | } |
| 6425 | |
| 6426 | |
| 6427 | void V8::RemoveMemoryAllocationCallback(MemoryAllocationCallback callback) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6428 | i::Isolate* isolate = i::Isolate::Current(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6429 | isolate->memory_allocator()->RemoveMemoryAllocationCallback( |
| 6430 | callback); |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 6431 | } |
| 6432 | |
| 6433 | |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 6434 | void V8::AddCallCompletedCallback(CallCompletedCallback callback) { |
| 6435 | if (callback == NULL) return; |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 6436 | i::V8::AddCallCompletedCallback(callback); |
| 6437 | } |
| 6438 | |
| 6439 | |
| 6440 | void V8::RemoveCallCompletedCallback(CallCompletedCallback callback) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 6441 | i::V8::RemoveCallCompletedCallback(callback); |
| 6442 | } |
| 6443 | |
| 6444 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6445 | void V8::TerminateExecution(Isolate* isolate) { |
| 6446 | // If no isolate is supplied, use the default isolate. |
| 6447 | if (isolate != NULL) { |
| 6448 | reinterpret_cast<i::Isolate*>(isolate)->stack_guard()->TerminateExecution(); |
| 6449 | } else { |
| 6450 | i::Isolate::GetDefaultIsolateStackGuard()->TerminateExecution(); |
| 6451 | } |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6452 | } |
| 6453 | |
| 6454 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6455 | bool V8::IsExecutionTerminating(Isolate* isolate) { |
| 6456 | i::Isolate* i_isolate = isolate != NULL ? |
| 6457 | reinterpret_cast<i::Isolate*>(isolate) : i::Isolate::Current(); |
| 6458 | return IsExecutionTerminatingCheck(i_isolate); |
sgjesse@chromium.org | 2ab9952 | 2010-03-10 09:03:43 +0000 | [diff] [blame] | 6459 | } |
| 6460 | |
| 6461 | |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 6462 | void V8::CancelTerminateExecution(Isolate* isolate) { |
| 6463 | i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 6464 | i_isolate->stack_guard()->CancelTerminateExecution(); |
| 6465 | } |
| 6466 | |
| 6467 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6468 | Isolate* Isolate::GetCurrent() { |
danno@chromium.org | 8c0a43f | 2012-04-03 08:37:53 +0000 | [diff] [blame] | 6469 | i::Isolate* isolate = i::Isolate::UncheckedCurrent(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6470 | return reinterpret_cast<Isolate*>(isolate); |
| 6471 | } |
| 6472 | |
| 6473 | |
| 6474 | Isolate* Isolate::New() { |
| 6475 | i::Isolate* isolate = new i::Isolate(); |
| 6476 | return reinterpret_cast<Isolate*>(isolate); |
| 6477 | } |
| 6478 | |
| 6479 | |
| 6480 | void Isolate::Dispose() { |
| 6481 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 6482 | if (!ApiCheck(!isolate->IsInUse(), |
| 6483 | "v8::Isolate::Dispose()", |
| 6484 | "Disposing the isolate that is entered by a thread.")) { |
| 6485 | return; |
| 6486 | } |
| 6487 | isolate->TearDown(); |
| 6488 | } |
| 6489 | |
| 6490 | |
| 6491 | void Isolate::Enter() { |
| 6492 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 6493 | isolate->Enter(); |
| 6494 | } |
| 6495 | |
| 6496 | |
| 6497 | void Isolate::Exit() { |
| 6498 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 6499 | isolate->Exit(); |
| 6500 | } |
| 6501 | |
| 6502 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 6503 | void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) { |
| 6504 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 6505 | if (!isolate->IsInitialized()) { |
| 6506 | heap_statistics->total_heap_size_ = 0; |
| 6507 | heap_statistics->total_heap_size_executable_ = 0; |
| 6508 | heap_statistics->total_physical_size_ = 0; |
| 6509 | heap_statistics->used_heap_size_ = 0; |
| 6510 | heap_statistics->heap_size_limit_ = 0; |
| 6511 | return; |
| 6512 | } |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 6513 | i::Heap* heap = isolate->heap(); |
| 6514 | heap_statistics->total_heap_size_ = heap->CommittedMemory(); |
| 6515 | heap_statistics->total_heap_size_executable_ = |
| 6516 | heap->CommittedMemoryExecutable(); |
| 6517 | heap_statistics->total_physical_size_ = heap->CommittedPhysicalMemory(); |
| 6518 | heap_statistics->used_heap_size_ = heap->SizeOfObjects(); |
| 6519 | heap_statistics->heap_size_limit_ = heap->MaxReserved(); |
| 6520 | } |
| 6521 | |
| 6522 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6523 | String::Utf8Value::Utf8Value(v8::Handle<v8::Value> obj) |
| 6524 | : str_(NULL), length_(0) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6525 | i::Isolate* isolate = i::Isolate::Current(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6526 | if (obj.IsEmpty()) return; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6527 | ENTER_V8(isolate); |
| 6528 | i::HandleScope scope(isolate); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6529 | TryCatch try_catch; |
| 6530 | Handle<String> str = obj->ToString(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6531 | if (str.IsEmpty()) return; |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 6532 | i::Handle<i::String> i_str = Utils::OpenHandle(*str); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 6533 | length_ = v8::Utf8Length(*i_str, isolate); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6534 | str_ = i::NewArray<char>(length_ + 1); |
| 6535 | str->WriteUtf8(str_); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6536 | } |
| 6537 | |
| 6538 | |
| 6539 | String::Utf8Value::~Utf8Value() { |
| 6540 | i::DeleteArray(str_); |
| 6541 | } |
| 6542 | |
| 6543 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6544 | String::AsciiValue::AsciiValue(v8::Handle<v8::Value> obj) |
| 6545 | : str_(NULL), length_(0) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6546 | i::Isolate* isolate = i::Isolate::Current(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6547 | if (obj.IsEmpty()) return; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6548 | ENTER_V8(isolate); |
| 6549 | i::HandleScope scope(isolate); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6550 | TryCatch try_catch; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6551 | Handle<String> str = obj->ToString(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6552 | if (str.IsEmpty()) return; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6553 | length_ = str->Utf8Length(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6554 | str_ = i::NewArray<char>(length_ + 1); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6555 | str->WriteUtf8(str_); |
| 6556 | ASSERT(i::String::NonAsciiStart(str_, length_) >= length_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6557 | } |
| 6558 | |
| 6559 | |
| 6560 | String::AsciiValue::~AsciiValue() { |
| 6561 | i::DeleteArray(str_); |
| 6562 | } |
| 6563 | |
| 6564 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6565 | String::Value::Value(v8::Handle<v8::Value> obj) |
| 6566 | : str_(NULL), length_(0) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6567 | i::Isolate* isolate = i::Isolate::Current(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6568 | if (obj.IsEmpty()) return; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6569 | ENTER_V8(isolate); |
| 6570 | i::HandleScope scope(isolate); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6571 | TryCatch try_catch; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6572 | Handle<String> str = obj->ToString(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 6573 | if (str.IsEmpty()) return; |
| 6574 | length_ = str->Length(); |
| 6575 | str_ = i::NewArray<uint16_t>(length_ + 1); |
| 6576 | str->Write(str_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6577 | } |
| 6578 | |
| 6579 | |
| 6580 | String::Value::~Value() { |
| 6581 | i::DeleteArray(str_); |
| 6582 | } |
| 6583 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 6584 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6585 | Local<Value> Exception::RangeError(v8::Handle<v8::String> raw_message) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6586 | i::Isolate* isolate = i::Isolate::Current(); |
| 6587 | LOG_API(isolate, "RangeError"); |
| 6588 | ON_BAILOUT(isolate, "v8::Exception::RangeError()", return Local<Value>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6589 | ENTER_V8(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6590 | i::Object* error; |
| 6591 | { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6592 | i::HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6593 | i::Handle<i::String> message = Utils::OpenHandle(*raw_message); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6594 | i::Handle<i::Object> result = isolate->factory()->NewRangeError(message); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6595 | error = *result; |
| 6596 | } |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 6597 | i::Handle<i::Object> result(error, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6598 | return Utils::ToLocal(result); |
| 6599 | } |
| 6600 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 6601 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6602 | Local<Value> Exception::ReferenceError(v8::Handle<v8::String> raw_message) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6603 | i::Isolate* isolate = i::Isolate::Current(); |
| 6604 | LOG_API(isolate, "ReferenceError"); |
| 6605 | ON_BAILOUT(isolate, "v8::Exception::ReferenceError()", return Local<Value>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6606 | ENTER_V8(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6607 | i::Object* error; |
| 6608 | { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6609 | i::HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6610 | i::Handle<i::String> message = Utils::OpenHandle(*raw_message); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6611 | i::Handle<i::Object> result = |
| 6612 | isolate->factory()->NewReferenceError(message); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6613 | error = *result; |
| 6614 | } |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 6615 | i::Handle<i::Object> result(error, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6616 | return Utils::ToLocal(result); |
| 6617 | } |
| 6618 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 6619 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6620 | Local<Value> Exception::SyntaxError(v8::Handle<v8::String> raw_message) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6621 | i::Isolate* isolate = i::Isolate::Current(); |
| 6622 | LOG_API(isolate, "SyntaxError"); |
| 6623 | ON_BAILOUT(isolate, "v8::Exception::SyntaxError()", return Local<Value>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6624 | ENTER_V8(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6625 | i::Object* error; |
| 6626 | { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6627 | i::HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6628 | i::Handle<i::String> message = Utils::OpenHandle(*raw_message); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6629 | i::Handle<i::Object> result = isolate->factory()->NewSyntaxError(message); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6630 | error = *result; |
| 6631 | } |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 6632 | i::Handle<i::Object> result(error, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6633 | return Utils::ToLocal(result); |
| 6634 | } |
| 6635 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 6636 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6637 | Local<Value> Exception::TypeError(v8::Handle<v8::String> raw_message) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6638 | i::Isolate* isolate = i::Isolate::Current(); |
| 6639 | LOG_API(isolate, "TypeError"); |
| 6640 | ON_BAILOUT(isolate, "v8::Exception::TypeError()", return Local<Value>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6641 | ENTER_V8(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6642 | i::Object* error; |
| 6643 | { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6644 | i::HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6645 | i::Handle<i::String> message = Utils::OpenHandle(*raw_message); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6646 | i::Handle<i::Object> result = isolate->factory()->NewTypeError(message); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6647 | error = *result; |
| 6648 | } |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 6649 | i::Handle<i::Object> result(error, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6650 | return Utils::ToLocal(result); |
| 6651 | } |
| 6652 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 6653 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6654 | Local<Value> Exception::Error(v8::Handle<v8::String> raw_message) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6655 | i::Isolate* isolate = i::Isolate::Current(); |
| 6656 | LOG_API(isolate, "Error"); |
| 6657 | ON_BAILOUT(isolate, "v8::Exception::Error()", return Local<Value>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6658 | ENTER_V8(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6659 | i::Object* error; |
| 6660 | { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6661 | i::HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6662 | i::Handle<i::String> message = Utils::OpenHandle(*raw_message); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6663 | i::Handle<i::Object> result = isolate->factory()->NewError(message); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6664 | error = *result; |
| 6665 | } |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 6666 | i::Handle<i::Object> result(error, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6667 | return Utils::ToLocal(result); |
| 6668 | } |
| 6669 | |
| 6670 | |
| 6671 | // --- D e b u g S u p p o r t --- |
| 6672 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6673 | #ifdef ENABLE_DEBUGGER_SUPPORT |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6674 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 6675 | bool Debug::SetDebugEventListener2(EventCallback2 that, Handle<Value> data) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6676 | i::Isolate* isolate = i::Isolate::Current(); |
| 6677 | EnsureInitializedForIsolate(isolate, "v8::Debug::SetDebugEventListener2()"); |
| 6678 | ON_BAILOUT(isolate, "v8::Debug::SetDebugEventListener2()", return false); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6679 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6680 | i::HandleScope scope(isolate); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6681 | i::Handle<i::Object> foreign = isolate->factory()->undefined_value(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 6682 | if (that != NULL) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 6683 | foreign = isolate->factory()->NewForeign(FUNCTION_ADDR(that)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 6684 | } |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 6685 | isolate->debugger()->SetEventListener(foreign, |
| 6686 | Utils::OpenHandle(*data, true)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6687 | return true; |
| 6688 | } |
| 6689 | |
| 6690 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 6691 | bool Debug::SetDebugEventListener(v8::Handle<v8::Object> that, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6692 | Handle<Value> data) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6693 | i::Isolate* isolate = i::Isolate::Current(); |
| 6694 | ON_BAILOUT(isolate, "v8::Debug::SetDebugEventListener()", return false); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6695 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6696 | isolate->debugger()->SetEventListener(Utils::OpenHandle(*that), |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 6697 | Utils::OpenHandle(*data, true)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6698 | return true; |
| 6699 | } |
| 6700 | |
| 6701 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6702 | void Debug::DebugBreak(Isolate* isolate) { |
| 6703 | // If no isolate is supplied, use the default isolate. |
| 6704 | if (isolate != NULL) { |
| 6705 | reinterpret_cast<i::Isolate*>(isolate)->stack_guard()->DebugBreak(); |
| 6706 | } else { |
| 6707 | i::Isolate::GetDefaultIsolateStackGuard()->DebugBreak(); |
| 6708 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6709 | } |
| 6710 | |
| 6711 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6712 | void Debug::CancelDebugBreak(Isolate* isolate) { |
| 6713 | // If no isolate is supplied, use the default isolate. |
| 6714 | if (isolate != NULL) { |
| 6715 | i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 6716 | internal_isolate->stack_guard()->Continue(i::DEBUGBREAK); |
| 6717 | } else { |
| 6718 | i::Isolate::GetDefaultIsolateStackGuard()->Continue(i::DEBUGBREAK); |
| 6719 | } |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 6720 | } |
| 6721 | |
| 6722 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6723 | void Debug::DebugBreakForCommand(ClientData* data, Isolate* isolate) { |
| 6724 | // If no isolate is supplied, use the default isolate. |
| 6725 | if (isolate != NULL) { |
| 6726 | i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 6727 | internal_isolate->debugger()->EnqueueDebugCommand(data); |
| 6728 | } else { |
| 6729 | i::Isolate::GetDefaultIsolateDebugger()->EnqueueDebugCommand(data); |
| 6730 | } |
mikhail.naganov@gmail.com | 2276287 | 2010-07-14 09:29:05 +0000 | [diff] [blame] | 6731 | } |
| 6732 | |
| 6733 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 6734 | void Debug::SetMessageHandler2(v8::Debug::MessageHandler2 handler) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6735 | i::Isolate* isolate = i::Isolate::Current(); |
| 6736 | EnsureInitializedForIsolate(isolate, "v8::Debug::SetMessageHandler"); |
| 6737 | ENTER_V8(isolate); |
| 6738 | isolate->debugger()->SetMessageHandler(handler); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6739 | } |
| 6740 | |
| 6741 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6742 | void Debug::SendCommand(Isolate* isolate, |
| 6743 | const uint16_t* command, |
| 6744 | int length, |
| 6745 | ClientData* client_data) { |
| 6746 | i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 6747 | internal_isolate->debugger()->ProcessCommand( |
| 6748 | i::Vector<const uint16_t>(command, length), client_data); |
| 6749 | } |
| 6750 | |
| 6751 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6752 | void Debug::SendCommand(const uint16_t* command, int length, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6753 | ClientData* client_data, |
| 6754 | Isolate* isolate) { |
| 6755 | // If no isolate is supplied, use the default isolate. |
| 6756 | if (isolate != NULL) { |
| 6757 | i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 6758 | internal_isolate->debugger()->ProcessCommand( |
| 6759 | i::Vector<const uint16_t>(command, length), client_data); |
| 6760 | } else { |
| 6761 | i::Isolate::GetDefaultIsolateDebugger()->ProcessCommand( |
| 6762 | i::Vector<const uint16_t>(command, length), client_data); |
| 6763 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6764 | } |
| 6765 | |
| 6766 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6767 | void Debug::SetHostDispatchHandler(HostDispatchHandler handler, |
| 6768 | int period) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6769 | i::Isolate* isolate = i::Isolate::Current(); |
| 6770 | EnsureInitializedForIsolate(isolate, "v8::Debug::SetHostDispatchHandler"); |
| 6771 | ENTER_V8(isolate); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 6772 | isolate->debugger()->SetHostDispatchHandler( |
| 6773 | handler, i::TimeDelta::FromMilliseconds(period)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 6774 | } |
| 6775 | |
| 6776 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6777 | void Debug::SetDebugMessageDispatchHandler( |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6778 | DebugMessageDispatchHandler handler, bool provide_locker) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6779 | i::Isolate* isolate = i::Isolate::Current(); |
| 6780 | EnsureInitializedForIsolate(isolate, |
| 6781 | "v8::Debug::SetDebugMessageDispatchHandler"); |
| 6782 | ENTER_V8(isolate); |
| 6783 | isolate->debugger()->SetDebugMessageDispatchHandler( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6784 | handler, provide_locker); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6785 | } |
| 6786 | |
| 6787 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6788 | Local<Value> Debug::Call(v8::Handle<v8::Function> fun, |
| 6789 | v8::Handle<v8::Value> data) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6790 | i::Isolate* isolate = i::Isolate::Current(); |
| 6791 | if (!isolate->IsInitialized()) return Local<Value>(); |
| 6792 | ON_BAILOUT(isolate, "v8::Debug::Call()", return Local<Value>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6793 | ENTER_V8(isolate); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 6794 | i::Handle<i::Object> result; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6795 | EXCEPTION_PREAMBLE(isolate); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 6796 | if (data.IsEmpty()) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6797 | result = isolate->debugger()->Call(Utils::OpenHandle(*fun), |
| 6798 | isolate->factory()->undefined_value(), |
| 6799 | &has_pending_exception); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 6800 | } else { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6801 | result = isolate->debugger()->Call(Utils::OpenHandle(*fun), |
| 6802 | Utils::OpenHandle(*data), |
| 6803 | &has_pending_exception); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 6804 | } |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6805 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 6806 | return Utils::ToLocal(result); |
| 6807 | } |
| 6808 | |
| 6809 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6810 | Local<Value> Debug::GetMirror(v8::Handle<v8::Value> obj) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6811 | i::Isolate* isolate = i::Isolate::Current(); |
| 6812 | if (!isolate->IsInitialized()) return Local<Value>(); |
| 6813 | ON_BAILOUT(isolate, "v8::Debug::GetMirror()", return Local<Value>()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6814 | ENTER_V8(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6815 | v8::HandleScope scope(reinterpret_cast<Isolate*>(isolate)); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6816 | i::Debug* isolate_debug = isolate->debug(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6817 | isolate_debug->Load(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 6818 | i::Handle<i::JSObject> debug(isolate_debug->debug_context()->global_object()); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 6819 | i::Handle<i::String> name = isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 6820 | STATIC_ASCII_VECTOR("MakeMirror")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 6821 | i::Handle<i::Object> fun_obj = i::GetProperty(isolate, debug, name); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6822 | i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(fun_obj); |
| 6823 | v8::Handle<v8::Function> v8_fun = Utils::ToLocal(fun); |
| 6824 | const int kArgc = 1; |
| 6825 | v8::Handle<v8::Value> argv[kArgc] = { obj }; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6826 | EXCEPTION_PREAMBLE(isolate); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6827 | v8::Handle<v8::Value> result = v8_fun->Call(Utils::ToLocal(debug), |
| 6828 | kArgc, |
| 6829 | argv); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6830 | EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>()); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 6831 | return scope.Close(result); |
| 6832 | } |
| 6833 | |
| 6834 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6835 | bool Debug::EnableAgent(const char* name, int port, bool wait_for_connection) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6836 | return i::Isolate::Current()->debugger()->StartAgent(name, port, |
| 6837 | wait_for_connection); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6838 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6839 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6840 | |
| 6841 | void Debug::DisableAgent() { |
| 6842 | return i::Isolate::Current()->debugger()->StopAgent(); |
| 6843 | } |
| 6844 | |
| 6845 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6846 | void Debug::ProcessDebugMessages() { |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 6847 | i::Execution::ProcessDebugMessages(i::Isolate::Current(), true); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6848 | } |
| 6849 | |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 6850 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 6851 | Local<Context> Debug::GetDebugContext() { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6852 | i::Isolate* isolate = i::Isolate::Current(); |
| 6853 | EnsureInitializedForIsolate(isolate, "v8::Debug::GetDebugContext()"); |
| 6854 | ENTER_V8(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6855 | return Utils::ToLocal(i::Isolate::Current()->debugger()->GetDebugContext()); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 6856 | } |
| 6857 | |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 6858 | |
| 6859 | void Debug::SetLiveEditEnabled(bool enable, Isolate* isolate) { |
| 6860 | // If no isolate is supplied, use the default isolate. |
| 6861 | i::Debugger* debugger; |
| 6862 | if (isolate != NULL) { |
| 6863 | i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 6864 | debugger = internal_isolate->debugger(); |
| 6865 | } else { |
| 6866 | debugger = i::Isolate::GetDefaultIsolateDebugger(); |
| 6867 | } |
| 6868 | debugger->set_live_edit_enabled(enable); |
| 6869 | } |
| 6870 | |
| 6871 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6872 | #endif // ENABLE_DEBUGGER_SUPPORT |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 6873 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6874 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6875 | Handle<String> CpuProfileNode::GetFunctionName() const { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6876 | i::Isolate* isolate = i::Isolate::Current(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6877 | const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this); |
| 6878 | const i::CodeEntry* entry = node->entry(); |
| 6879 | if (!entry->has_name_prefix()) { |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 6880 | return ToApiHandle<String>( |
| 6881 | isolate->factory()->InternalizeUtf8String(entry->name())); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6882 | } else { |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 6883 | return ToApiHandle<String>(isolate->factory()->NewConsString( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 6884 | isolate->factory()->InternalizeUtf8String(entry->name_prefix()), |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 6885 | isolate->factory()->InternalizeUtf8String(entry->name()))); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6886 | } |
| 6887 | } |
| 6888 | |
| 6889 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 6890 | int CpuProfileNode::GetScriptId() const { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 6891 | const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this); |
| 6892 | const i::CodeEntry* entry = node->entry(); |
| 6893 | return entry->script_id(); |
| 6894 | } |
| 6895 | |
| 6896 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6897 | Handle<String> CpuProfileNode::GetScriptResourceName() const { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6898 | i::Isolate* isolate = i::Isolate::Current(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6899 | const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 6900 | return ToApiHandle<String>(isolate->factory()->InternalizeUtf8String( |
| 6901 | node->entry()->resource_name())); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6902 | } |
| 6903 | |
| 6904 | |
| 6905 | int CpuProfileNode::GetLineNumber() const { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6906 | return reinterpret_cast<const i::ProfileNode*>(this)->entry()->line_number(); |
| 6907 | } |
| 6908 | |
| 6909 | |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 6910 | const char* CpuProfileNode::GetBailoutReason() const { |
| 6911 | const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this); |
| 6912 | return node->entry()->bailout_reason(); |
| 6913 | } |
| 6914 | |
| 6915 | |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 6916 | unsigned CpuProfileNode::GetHitCount() const { |
| 6917 | return reinterpret_cast<const i::ProfileNode*>(this)->self_ticks(); |
| 6918 | } |
| 6919 | |
| 6920 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6921 | unsigned CpuProfileNode::GetCallUid() const { |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 6922 | return reinterpret_cast<const i::ProfileNode*>(this)->entry()->GetCallUid(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6923 | } |
| 6924 | |
| 6925 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 6926 | unsigned CpuProfileNode::GetNodeId() const { |
| 6927 | return reinterpret_cast<const i::ProfileNode*>(this)->id(); |
| 6928 | } |
| 6929 | |
| 6930 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6931 | int CpuProfileNode::GetChildrenCount() const { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6932 | return reinterpret_cast<const i::ProfileNode*>(this)->children()->length(); |
| 6933 | } |
| 6934 | |
| 6935 | |
| 6936 | const CpuProfileNode* CpuProfileNode::GetChild(int index) const { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6937 | const i::ProfileNode* child = |
| 6938 | reinterpret_cast<const i::ProfileNode*>(this)->children()->at(index); |
| 6939 | return reinterpret_cast<const CpuProfileNode*>(child); |
| 6940 | } |
| 6941 | |
| 6942 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6943 | void CpuProfile::Delete() { |
| 6944 | i::Isolate* isolate = i::Isolate::Current(); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 6945 | i::CpuProfiler* profiler = isolate->cpu_profiler(); |
| 6946 | ASSERT(profiler != NULL); |
| 6947 | profiler->DeleteProfile(reinterpret_cast<i::CpuProfile*>(this)); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 6948 | if (profiler->GetProfilesCount() == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6949 | // If this was the last profile, clean up all accessory data as well. |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 6950 | profiler->DeleteAllProfiles(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6951 | } |
| 6952 | } |
| 6953 | |
| 6954 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6955 | unsigned CpuProfile::GetUid() const { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6956 | return reinterpret_cast<const i::CpuProfile*>(this)->uid(); |
| 6957 | } |
| 6958 | |
| 6959 | |
| 6960 | Handle<String> CpuProfile::GetTitle() const { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6961 | i::Isolate* isolate = i::Isolate::Current(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6962 | const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 6963 | return ToApiHandle<String>(isolate->factory()->InternalizeUtf8String( |
| 6964 | profile->title())); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6965 | } |
| 6966 | |
| 6967 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6968 | const CpuProfileNode* CpuProfile::GetTopDownRoot() const { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6969 | const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this); |
| 6970 | return reinterpret_cast<const CpuProfileNode*>(profile->top_down()->root()); |
| 6971 | } |
| 6972 | |
| 6973 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 6974 | const CpuProfileNode* CpuProfile::GetSample(int index) const { |
| 6975 | const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this); |
| 6976 | return reinterpret_cast<const CpuProfileNode*>(profile->sample(index)); |
| 6977 | } |
| 6978 | |
| 6979 | |
danno@chromium.org | 89acc0b | 2013-08-09 20:55:58 +0000 | [diff] [blame] | 6980 | int64_t CpuProfile::GetStartTime() const { |
| 6981 | const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 6982 | return (profile->start_time() - i::Time::UnixEpoch()).InMicroseconds(); |
danno@chromium.org | 89acc0b | 2013-08-09 20:55:58 +0000 | [diff] [blame] | 6983 | } |
| 6984 | |
| 6985 | |
| 6986 | int64_t CpuProfile::GetEndTime() const { |
| 6987 | const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 6988 | return (profile->end_time() - i::Time::UnixEpoch()).InMicroseconds(); |
danno@chromium.org | 89acc0b | 2013-08-09 20:55:58 +0000 | [diff] [blame] | 6989 | } |
| 6990 | |
| 6991 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 6992 | int CpuProfile::GetSamplesCount() const { |
| 6993 | return reinterpret_cast<const i::CpuProfile*>(this)->samples_count(); |
| 6994 | } |
| 6995 | |
| 6996 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 6997 | int CpuProfiler::GetProfileCount() { |
| 6998 | return reinterpret_cast<i::CpuProfiler*>(this)->GetProfilesCount(); |
jkummerow@chromium.org | 7bd87f0 | 2013-03-20 18:06:29 +0000 | [diff] [blame] | 6999 | } |
| 7000 | |
| 7001 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 7002 | void CpuProfiler::SetSamplingInterval(int us) { |
| 7003 | ASSERT(us >= 0); |
| 7004 | return reinterpret_cast<i::CpuProfiler*>(this)->set_sampling_interval( |
| 7005 | i::TimeDelta::FromMicroseconds(us)); |
| 7006 | } |
| 7007 | |
| 7008 | |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 7009 | const CpuProfile* CpuProfiler::GetCpuProfile(int index) { |
| 7010 | return reinterpret_cast<const CpuProfile*>( |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 7011 | reinterpret_cast<i::CpuProfiler*>(this)->GetProfile(index)); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 7012 | } |
| 7013 | |
| 7014 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 7015 | void CpuProfiler::StartCpuProfiling(Handle<String> title, bool record_samples) { |
| 7016 | reinterpret_cast<i::CpuProfiler*>(this)->StartProfiling( |
| 7017 | *Utils::OpenHandle(*title), record_samples); |
jkummerow@chromium.org | 7bd87f0 | 2013-03-20 18:06:29 +0000 | [diff] [blame] | 7018 | } |
| 7019 | |
| 7020 | |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 7021 | const CpuProfile* CpuProfiler::StopCpuProfiling(Handle<String> title) { |
| 7022 | return reinterpret_cast<const CpuProfile*>( |
| 7023 | reinterpret_cast<i::CpuProfiler*>(this)->StopProfiling( |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 7024 | *Utils::OpenHandle(*title))); |
| 7025 | } |
| 7026 | |
| 7027 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 7028 | void CpuProfiler::DeleteAllCpuProfiles() { |
| 7029 | reinterpret_cast<i::CpuProfiler*>(this)->DeleteAllProfiles(); |
jkummerow@chromium.org | 7bd87f0 | 2013-03-20 18:06:29 +0000 | [diff] [blame] | 7030 | } |
| 7031 | |
| 7032 | |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 7033 | void CpuProfiler::SetIdle(bool is_idle) { |
| 7034 | i::Isolate* isolate = reinterpret_cast<i::CpuProfiler*>(this)->isolate(); |
| 7035 | i::StateTag state = isolate->current_vm_state(); |
| 7036 | ASSERT(state == i::EXTERNAL || state == i::IDLE); |
| 7037 | if (isolate->js_entry_sp() != NULL) return; |
| 7038 | if (is_idle) { |
| 7039 | isolate->set_current_vm_state(i::IDLE); |
| 7040 | } else if (state == i::IDLE) { |
| 7041 | isolate->set_current_vm_state(i::EXTERNAL); |
| 7042 | } |
| 7043 | } |
| 7044 | |
| 7045 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7046 | static i::HeapGraphEdge* ToInternal(const HeapGraphEdge* edge) { |
| 7047 | return const_cast<i::HeapGraphEdge*>( |
| 7048 | reinterpret_cast<const i::HeapGraphEdge*>(edge)); |
| 7049 | } |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 7050 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7051 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7052 | HeapGraphEdge::Type HeapGraphEdge::GetType() const { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7053 | return static_cast<HeapGraphEdge::Type>(ToInternal(this)->type()); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7054 | } |
| 7055 | |
| 7056 | |
| 7057 | Handle<Value> HeapGraphEdge::GetName() const { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7058 | i::Isolate* isolate = i::Isolate::Current(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7059 | i::HeapGraphEdge* edge = ToInternal(this); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7060 | switch (edge->type()) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7061 | case i::HeapGraphEdge::kContextVariable: |
| 7062 | case i::HeapGraphEdge::kInternal: |
| 7063 | case i::HeapGraphEdge::kProperty: |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7064 | case i::HeapGraphEdge::kShortcut: |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 7065 | return ToApiHandle<String>( |
| 7066 | isolate->factory()->InternalizeUtf8String(edge->name())); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7067 | case i::HeapGraphEdge::kElement: |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7068 | case i::HeapGraphEdge::kHidden: |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 7069 | case i::HeapGraphEdge::kWeak: |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 7070 | return ToApiHandle<Number>( |
| 7071 | isolate->factory()->NewNumberFromInt(edge->index())); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7072 | default: UNREACHABLE(); |
| 7073 | } |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 7074 | return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate)); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7075 | } |
| 7076 | |
| 7077 | |
| 7078 | const HeapGraphNode* HeapGraphEdge::GetFromNode() const { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 7079 | const i::HeapEntry* from = ToInternal(this)->from(); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7080 | return reinterpret_cast<const HeapGraphNode*>(from); |
| 7081 | } |
| 7082 | |
| 7083 | |
| 7084 | const HeapGraphNode* HeapGraphEdge::GetToNode() const { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7085 | const i::HeapEntry* to = ToInternal(this)->to(); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7086 | return reinterpret_cast<const HeapGraphNode*>(to); |
| 7087 | } |
| 7088 | |
| 7089 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7090 | static i::HeapEntry* ToInternal(const HeapGraphNode* entry) { |
| 7091 | return const_cast<i::HeapEntry*>( |
| 7092 | reinterpret_cast<const i::HeapEntry*>(entry)); |
| 7093 | } |
| 7094 | |
| 7095 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7096 | HeapGraphNode::Type HeapGraphNode::GetType() const { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7097 | return static_cast<HeapGraphNode::Type>(ToInternal(this)->type()); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7098 | } |
| 7099 | |
| 7100 | |
| 7101 | Handle<String> HeapGraphNode::GetName() const { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7102 | i::Isolate* isolate = i::Isolate::Current(); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 7103 | return ToApiHandle<String>( |
| 7104 | isolate->factory()->InternalizeUtf8String(ToInternal(this)->name())); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7105 | } |
| 7106 | |
| 7107 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 7108 | SnapshotObjectId HeapGraphNode::GetId() const { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7109 | return ToInternal(this)->id(); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 7110 | } |
| 7111 | |
| 7112 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7113 | int HeapGraphNode::GetSelfSize() const { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7114 | return ToInternal(this)->self_size(); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7115 | } |
| 7116 | |
| 7117 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7118 | int HeapGraphNode::GetChildrenCount() const { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7119 | return ToInternal(this)->children().length(); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7120 | } |
| 7121 | |
| 7122 | |
| 7123 | const HeapGraphEdge* HeapGraphNode::GetChild(int index) const { |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7124 | return reinterpret_cast<const HeapGraphEdge*>( |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 7125 | ToInternal(this)->children()[index]); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7126 | } |
| 7127 | |
| 7128 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 7129 | v8::Handle<v8::Value> HeapGraphNode::GetHeapValue() const { |
| 7130 | i::Isolate* isolate = i::Isolate::Current(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 7131 | i::Handle<i::HeapObject> object = ToInternal(this)->GetHeapObject(); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 7132 | return !object.is_null() ? |
| 7133 | ToApiHandle<Value>(object) : |
| 7134 | ToApiHandle<Value>(isolate->factory()->undefined_value()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 7135 | } |
| 7136 | |
| 7137 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7138 | static i::HeapSnapshot* ToInternal(const HeapSnapshot* snapshot) { |
| 7139 | return const_cast<i::HeapSnapshot*>( |
| 7140 | reinterpret_cast<const i::HeapSnapshot*>(snapshot)); |
| 7141 | } |
| 7142 | |
| 7143 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7144 | void HeapSnapshot::Delete() { |
| 7145 | i::Isolate* isolate = i::Isolate::Current(); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 7146 | if (isolate->heap_profiler()->GetSnapshotsCount() > 1) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7147 | ToInternal(this)->Delete(); |
| 7148 | } else { |
| 7149 | // If this is the last snapshot, clean up all accessory data as well. |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 7150 | isolate->heap_profiler()->DeleteAllSnapshots(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7151 | } |
| 7152 | } |
| 7153 | |
| 7154 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7155 | unsigned HeapSnapshot::GetUid() const { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7156 | return ToInternal(this)->uid(); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7157 | } |
| 7158 | |
| 7159 | |
| 7160 | Handle<String> HeapSnapshot::GetTitle() const { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7161 | i::Isolate* isolate = i::Isolate::Current(); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 7162 | return ToApiHandle<String>( |
| 7163 | isolate->factory()->InternalizeUtf8String(ToInternal(this)->title())); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7164 | } |
| 7165 | |
| 7166 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 7167 | const HeapGraphNode* HeapSnapshot::GetRoot() const { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7168 | return reinterpret_cast<const HeapGraphNode*>(ToInternal(this)->root()); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7169 | } |
| 7170 | |
| 7171 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 7172 | const HeapGraphNode* HeapSnapshot::GetNodeById(SnapshotObjectId id) const { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7173 | return reinterpret_cast<const HeapGraphNode*>( |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 7174 | ToInternal(this)->GetEntryById(id)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7175 | } |
| 7176 | |
| 7177 | |
lrn@chromium.org | ac2828d | 2011-06-23 06:29:21 +0000 | [diff] [blame] | 7178 | int HeapSnapshot::GetNodesCount() const { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 7179 | return ToInternal(this)->entries().length(); |
lrn@chromium.org | ac2828d | 2011-06-23 06:29:21 +0000 | [diff] [blame] | 7180 | } |
| 7181 | |
| 7182 | |
| 7183 | const HeapGraphNode* HeapSnapshot::GetNode(int index) const { |
lrn@chromium.org | ac2828d | 2011-06-23 06:29:21 +0000 | [diff] [blame] | 7184 | return reinterpret_cast<const HeapGraphNode*>( |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 7185 | &ToInternal(this)->entries().at(index)); |
lrn@chromium.org | ac2828d | 2011-06-23 06:29:21 +0000 | [diff] [blame] | 7186 | } |
| 7187 | |
| 7188 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 7189 | SnapshotObjectId HeapSnapshot::GetMaxSnapshotJSObjectId() const { |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 7190 | return ToInternal(this)->max_snapshot_js_object_id(); |
| 7191 | } |
| 7192 | |
| 7193 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 7194 | void HeapSnapshot::Serialize(OutputStream* stream, |
| 7195 | HeapSnapshot::SerializationFormat format) const { |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 7196 | ApiCheck(format == kJSON, |
| 7197 | "v8::HeapSnapshot::Serialize", |
| 7198 | "Unknown serialization format"); |
| 7199 | ApiCheck(stream->GetOutputEncoding() == OutputStream::kAscii, |
| 7200 | "v8::HeapSnapshot::Serialize", |
| 7201 | "Unsupported output encoding"); |
| 7202 | ApiCheck(stream->GetChunkSize() > 0, |
| 7203 | "v8::HeapSnapshot::Serialize", |
| 7204 | "Invalid stream chunk size"); |
| 7205 | i::HeapSnapshotJSONSerializer serializer(ToInternal(this)); |
| 7206 | serializer.Serialize(stream); |
| 7207 | } |
| 7208 | |
| 7209 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 7210 | int HeapProfiler::GetSnapshotCount() { |
| 7211 | return reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshotsCount(); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7212 | } |
| 7213 | |
| 7214 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 7215 | const HeapSnapshot* HeapProfiler::GetHeapSnapshot(int index) { |
| 7216 | return reinterpret_cast<const HeapSnapshot*>( |
| 7217 | reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshot(index)); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7218 | } |
| 7219 | |
| 7220 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 7221 | SnapshotObjectId HeapProfiler::GetObjectId(Handle<Value> value) { |
| 7222 | i::Handle<i::Object> obj = Utils::OpenHandle(*value); |
| 7223 | return reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshotObjectId(obj); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 7224 | } |
| 7225 | |
| 7226 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 7227 | const HeapSnapshot* HeapProfiler::TakeHeapSnapshot( |
| 7228 | Handle<String> title, |
| 7229 | ActivityControl* control, |
| 7230 | ObjectNameResolver* resolver) { |
| 7231 | return reinterpret_cast<const HeapSnapshot*>( |
| 7232 | reinterpret_cast<i::HeapProfiler*>(this)->TakeSnapshot( |
| 7233 | *Utils::OpenHandle(*title), control, resolver)); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 7234 | } |
| 7235 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 7236 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 7237 | void HeapProfiler::StartTrackingHeapObjects() { |
| 7238 | reinterpret_cast<i::HeapProfiler*>(this)->StartHeapObjectsTracking(); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 7239 | } |
| 7240 | |
| 7241 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 7242 | void HeapProfiler::StopTrackingHeapObjects() { |
| 7243 | reinterpret_cast<i::HeapProfiler*>(this)->StopHeapObjectsTracking(); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 7244 | } |
| 7245 | |
| 7246 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 7247 | SnapshotObjectId HeapProfiler::GetHeapStats(OutputStream* stream) { |
| 7248 | return reinterpret_cast<i::HeapProfiler*>(this)->PushHeapObjectsStats(stream); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 7249 | } |
| 7250 | |
| 7251 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 7252 | void HeapProfiler::DeleteAllHeapSnapshots() { |
| 7253 | reinterpret_cast<i::HeapProfiler*>(this)->DeleteAllSnapshots(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7254 | } |
| 7255 | |
| 7256 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 7257 | void HeapProfiler::SetWrapperClassInfoProvider(uint16_t class_id, |
| 7258 | WrapperInfoCallback callback) { |
| 7259 | reinterpret_cast<i::HeapProfiler*>(this)->DefineWrapperClass(class_id, |
| 7260 | callback); |
| 7261 | } |
| 7262 | |
| 7263 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 7264 | size_t HeapProfiler::GetProfilerMemorySize() { |
| 7265 | return reinterpret_cast<i::HeapProfiler*>(this)-> |
| 7266 | GetMemorySizeUsedByProfiler(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 7267 | } |
| 7268 | |
| 7269 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 7270 | void HeapProfiler::SetRetainedObjectInfo(UniqueId id, |
| 7271 | RetainedObjectInfo* info) { |
| 7272 | reinterpret_cast<i::HeapProfiler*>(this)->SetRetainedObjectInfo(id, info); |
| 7273 | } |
| 7274 | |
| 7275 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7276 | v8::Testing::StressType internal::Testing::stress_type_ = |
| 7277 | v8::Testing::kStressTypeOpt; |
| 7278 | |
| 7279 | |
| 7280 | void Testing::SetStressRunType(Testing::StressType type) { |
| 7281 | internal::Testing::set_stress_type(type); |
| 7282 | } |
| 7283 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 7284 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7285 | int Testing::GetStressRuns() { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7286 | if (internal::FLAG_stress_runs != 0) return internal::FLAG_stress_runs; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7287 | #ifdef DEBUG |
| 7288 | // In debug mode the code runs much slower so stressing will only make two |
| 7289 | // runs. |
| 7290 | return 2; |
| 7291 | #else |
| 7292 | return 5; |
| 7293 | #endif |
| 7294 | } |
| 7295 | |
| 7296 | |
| 7297 | static void SetFlagsFromString(const char* flags) { |
| 7298 | V8::SetFlagsFromString(flags, i::StrLength(flags)); |
| 7299 | } |
| 7300 | |
| 7301 | |
| 7302 | void Testing::PrepareStressRun(int run) { |
| 7303 | static const char* kLazyOptimizations = |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 7304 | "--prepare-always-opt " |
| 7305 | "--max-inlined-source-size=999999 " |
| 7306 | "--max-inlined-nodes=999999 " |
| 7307 | "--max-inlined-nodes-cumulative=999999 " |
| 7308 | "--noalways-opt"; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7309 | static const char* kForcedOptimizations = "--always-opt"; |
| 7310 | |
| 7311 | // If deoptimization stressed turn on frequent deoptimization. If no value |
| 7312 | // is spefified through --deopt-every-n-times use a default default value. |
| 7313 | static const char* kDeoptEvery13Times = "--deopt-every-n-times=13"; |
| 7314 | if (internal::Testing::stress_type() == Testing::kStressTypeDeopt && |
| 7315 | internal::FLAG_deopt_every_n_times == 0) { |
| 7316 | SetFlagsFromString(kDeoptEvery13Times); |
| 7317 | } |
| 7318 | |
| 7319 | #ifdef DEBUG |
| 7320 | // As stressing in debug mode only make two runs skip the deopt stressing |
| 7321 | // here. |
| 7322 | if (run == GetStressRuns() - 1) { |
| 7323 | SetFlagsFromString(kForcedOptimizations); |
| 7324 | } else { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7325 | SetFlagsFromString(kLazyOptimizations); |
| 7326 | } |
| 7327 | #else |
| 7328 | if (run == GetStressRuns() - 1) { |
| 7329 | SetFlagsFromString(kForcedOptimizations); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 7330 | } else if (run != GetStressRuns() - 2) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7331 | SetFlagsFromString(kLazyOptimizations); |
| 7332 | } |
| 7333 | #endif |
| 7334 | } |
| 7335 | |
| 7336 | |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 7337 | // TODO(svenpanne) Deprecate this. |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 7338 | void Testing::DeoptimizeAll() { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 7339 | i::Isolate* isolate = i::Isolate::Current(); |
| 7340 | i::HandleScope scope(isolate); |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 7341 | internal::Deoptimizer::DeoptimizeAll(isolate); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 7342 | } |
| 7343 | |
| 7344 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7345 | namespace internal { |
| 7346 | |
| 7347 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 7348 | void HandleScopeImplementer::FreeThreadResources() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7349 | Free(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 7350 | } |
| 7351 | |
| 7352 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7353 | char* HandleScopeImplementer::ArchiveThread(char* storage) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 7354 | v8::ImplementationUtilities::HandleScopeData* current = |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 7355 | isolate_->handle_scope_data(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7356 | handle_scope_data_ = *current; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 7357 | OS::MemCopy(storage, this, sizeof(*this)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7358 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 7359 | ResetAfterArchive(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7360 | current->Initialize(); |
| 7361 | |
| 7362 | return storage + ArchiveSpacePerThread(); |
| 7363 | } |
| 7364 | |
| 7365 | |
| 7366 | int HandleScopeImplementer::ArchiveSpacePerThread() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7367 | return sizeof(HandleScopeImplementer); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7368 | } |
| 7369 | |
| 7370 | |
| 7371 | char* HandleScopeImplementer::RestoreThread(char* storage) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 7372 | OS::MemCopy(this, storage, sizeof(*this)); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 7373 | *isolate_->handle_scope_data() = handle_scope_data_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7374 | return storage + ArchiveSpacePerThread(); |
| 7375 | } |
| 7376 | |
| 7377 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 7378 | void HandleScopeImplementer::IterateThis(ObjectVisitor* v) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7379 | #ifdef DEBUG |
| 7380 | bool found_block_before_deferred = false; |
| 7381 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7382 | // Iterate over all handles in the blocks except for the last. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 7383 | for (int i = blocks()->length() - 2; i >= 0; --i) { |
| 7384 | Object** block = blocks()->at(i); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7385 | if (last_handle_before_deferred_block_ != NULL && |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 7386 | (last_handle_before_deferred_block_ <= &block[kHandleBlockSize]) && |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7387 | (last_handle_before_deferred_block_ >= block)) { |
| 7388 | v->VisitPointers(block, last_handle_before_deferred_block_); |
| 7389 | ASSERT(!found_block_before_deferred); |
| 7390 | #ifdef DEBUG |
| 7391 | found_block_before_deferred = true; |
| 7392 | #endif |
| 7393 | } else { |
| 7394 | v->VisitPointers(block, &block[kHandleBlockSize]); |
| 7395 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7396 | } |
| 7397 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7398 | ASSERT(last_handle_before_deferred_block_ == NULL || |
| 7399 | found_block_before_deferred); |
| 7400 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7401 | // Iterate over live handles in the last block (if any). |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 7402 | if (!blocks()->is_empty()) { |
| 7403 | v->VisitPointers(blocks()->last(), handle_scope_data_.next); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 7404 | } |
| 7405 | |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 7406 | List<Context*>* context_lists[2] = { &saved_contexts_, &entered_contexts_}; |
| 7407 | for (unsigned i = 0; i < ARRAY_SIZE(context_lists); i++) { |
| 7408 | if (context_lists[i]->is_empty()) continue; |
| 7409 | Object** start = reinterpret_cast<Object**>(&context_lists[i]->first()); |
| 7410 | v->VisitPointers(start, start + context_lists[i]->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7411 | } |
| 7412 | } |
| 7413 | |
| 7414 | |
| 7415 | void HandleScopeImplementer::Iterate(ObjectVisitor* v) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 7416 | v8::ImplementationUtilities::HandleScopeData* current = |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 7417 | isolate_->handle_scope_data(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7418 | handle_scope_data_ = *current; |
| 7419 | IterateThis(v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7420 | } |
| 7421 | |
| 7422 | |
| 7423 | char* HandleScopeImplementer::Iterate(ObjectVisitor* v, char* storage) { |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 7424 | HandleScopeImplementer* scope_implementer = |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7425 | reinterpret_cast<HandleScopeImplementer*>(storage); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 7426 | scope_implementer->IterateThis(v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7427 | return storage + ArchiveSpacePerThread(); |
| 7428 | } |
| 7429 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7430 | |
| 7431 | DeferredHandles* HandleScopeImplementer::Detach(Object** prev_limit) { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7432 | DeferredHandles* deferred = |
| 7433 | new DeferredHandles(isolate()->handle_scope_data()->next, isolate()); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7434 | |
| 7435 | while (!blocks_.is_empty()) { |
| 7436 | Object** block_start = blocks_.last(); |
| 7437 | Object** block_limit = &block_start[kHandleBlockSize]; |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 7438 | // We should not need to check for SealHandleScope here. Assert this. |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7439 | ASSERT(prev_limit == block_limit || |
| 7440 | !(block_start <= prev_limit && prev_limit <= block_limit)); |
| 7441 | if (prev_limit == block_limit) break; |
| 7442 | deferred->blocks_.Add(blocks_.last()); |
| 7443 | blocks_.RemoveLast(); |
| 7444 | } |
| 7445 | |
| 7446 | // deferred->blocks_ now contains the blocks installed on the |
| 7447 | // HandleScope stack since BeginDeferredScope was called, but in |
| 7448 | // reverse order. |
| 7449 | |
| 7450 | ASSERT(prev_limit == NULL || !blocks_.is_empty()); |
| 7451 | |
| 7452 | ASSERT(!blocks_.is_empty() && prev_limit != NULL); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7453 | ASSERT(last_handle_before_deferred_block_ != NULL); |
| 7454 | last_handle_before_deferred_block_ = NULL; |
| 7455 | return deferred; |
| 7456 | } |
| 7457 | |
| 7458 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7459 | void HandleScopeImplementer::BeginDeferredScope() { |
| 7460 | ASSERT(last_handle_before_deferred_block_ == NULL); |
| 7461 | last_handle_before_deferred_block_ = isolate()->handle_scope_data()->next; |
| 7462 | } |
| 7463 | |
| 7464 | |
| 7465 | DeferredHandles::~DeferredHandles() { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7466 | isolate_->UnlinkDeferredHandles(this); |
| 7467 | |
| 7468 | for (int i = 0; i < blocks_.length(); i++) { |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame^] | 7469 | #ifdef ENABLE_HANDLE_ZAPPING |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7470 | HandleScope::ZapRange(blocks_[i], &blocks_[i][kHandleBlockSize]); |
| 7471 | #endif |
| 7472 | isolate_->handle_scope_implementer()->ReturnBlock(blocks_[i]); |
| 7473 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7474 | } |
| 7475 | |
| 7476 | |
| 7477 | void DeferredHandles::Iterate(ObjectVisitor* v) { |
| 7478 | ASSERT(!blocks_.is_empty()); |
| 7479 | |
| 7480 | ASSERT((first_block_limit_ >= blocks_.first()) && |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7481 | (first_block_limit_ <= &(blocks_.first())[kHandleBlockSize])); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7482 | |
| 7483 | v->VisitPointers(blocks_.first(), first_block_limit_); |
| 7484 | |
| 7485 | for (int i = 1; i < blocks_.length(); i++) { |
| 7486 | v->VisitPointers(blocks_[i], &blocks_[i][kHandleBlockSize]); |
| 7487 | } |
| 7488 | } |
| 7489 | |
| 7490 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 7491 | void InvokeAccessorGetterCallback( |
| 7492 | v8::Local<v8::String> property, |
| 7493 | const v8::PropertyCallbackInfo<v8::Value>& info, |
| 7494 | v8::AccessorGetterCallback getter) { |
| 7495 | // Leaving JavaScript. |
| 7496 | Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate()); |
| 7497 | Address getter_address = reinterpret_cast<Address>(reinterpret_cast<intptr_t>( |
| 7498 | getter)); |
| 7499 | VMState<EXTERNAL> state(isolate); |
| 7500 | ExternalCallbackScope call_scope(isolate, getter_address); |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 7501 | getter(property, info); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 7502 | } |
| 7503 | |
| 7504 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 7505 | void InvokeFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info, |
| 7506 | v8::FunctionCallback callback) { |
| 7507 | Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate()); |
| 7508 | Address callback_address = |
| 7509 | reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); |
| 7510 | VMState<EXTERNAL> state(isolate); |
| 7511 | ExternalCallbackScope call_scope(isolate, callback_address); |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 7512 | callback(info); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 7513 | } |
| 7514 | |
| 7515 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7516 | } } // namespace v8::internal |