blob: 39c0d02b61d845681638a875af9cf18ee5695db7 [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "api.h"
Ben Murdochf87a2032010-10-22 12:50:53 +010031
Steve Blocka7e24c12009-10-30 11:49:00 +000032#include "arguments.h"
33#include "bootstrapper.h"
34#include "compiler.h"
35#include "debug.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010036#include "deoptimizer.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000037#include "execution.h"
Ben Murdoch69a99ed2011-11-30 16:03:39 +000038#include "flags.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000039#include "global-handles.h"
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010040#include "heap-profiler.h"
Steve Block6ded16b2010-05-10 14:33:55 +010041#include "messages.h"
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000042#include "natives.h"
Ben Murdochf87a2032010-10-22 12:50:53 +010043#include "parser.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000044#include "platform.h"
Steve Block6ded16b2010-05-10 14:33:55 +010045#include "profile-generator-inl.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010046#include "runtime-profiler.h"
Ben Murdoch589d6972011-11-30 16:04:58 +000047#include "scanner-character-streams.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000048#include "serialize.h"
49#include "snapshot.h"
50#include "v8threads.h"
51#include "version.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010052#include "vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000053
Steve Block6ded16b2010-05-10 14:33:55 +010054#include "../include/v8-profiler.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010055#include "../include/v8-testing.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000056
Steve Block44f0eee2011-05-26 01:26:41 +010057#define LOG_API(isolate, expr) LOG(isolate, ApiEntryCall(expr))
Steve Blocka7e24c12009-10-30 11:49:00 +000058
Steve Block44f0eee2011-05-26 01:26:41 +010059#define ENTER_V8(isolate) \
60 ASSERT((isolate)->IsInitialized()); \
61 i::VMState __state__((isolate), i::OTHER)
62#define LEAVE_V8(isolate) \
63 i::VMState __state__((isolate), i::EXTERNAL)
Steve Blocka7e24c12009-10-30 11:49:00 +000064
65namespace v8 {
66
Steve Block44f0eee2011-05-26 01:26:41 +010067#define ON_BAILOUT(isolate, location, code) \
68 if (IsDeadCheck(isolate, location) || \
69 IsExecutionTerminatingCheck(isolate)) { \
Leon Clarkef7060e22010-06-03 12:02:55 +010070 code; \
71 UNREACHABLE(); \
Steve Blocka7e24c12009-10-30 11:49:00 +000072 }
73
74
Steve Block44f0eee2011-05-26 01:26:41 +010075#define EXCEPTION_PREAMBLE(isolate) \
76 (isolate)->handle_scope_implementer()->IncrementCallDepth(); \
77 ASSERT(!(isolate)->external_caught_exception()); \
Steve Blocka7e24c12009-10-30 11:49:00 +000078 bool has_pending_exception = false
79
80
Steve Block44f0eee2011-05-26 01:26:41 +010081#define EXCEPTION_BAILOUT_CHECK(isolate, value) \
Steve Blocka7e24c12009-10-30 11:49:00 +000082 do { \
Steve Block44f0eee2011-05-26 01:26:41 +010083 i::HandleScopeImplementer* handle_scope_implementer = \
84 (isolate)->handle_scope_implementer(); \
85 handle_scope_implementer->DecrementCallDepth(); \
Steve Blocka7e24c12009-10-30 11:49:00 +000086 if (has_pending_exception) { \
Steve Block44f0eee2011-05-26 01:26:41 +010087 if (handle_scope_implementer->CallDepthIsZero() && \
88 (isolate)->is_out_of_memory()) { \
Ben Murdoch69a99ed2011-11-30 16:03:39 +000089 if (!(isolate)->ignore_out_of_memory()) \
Steve Blocka7e24c12009-10-30 11:49:00 +000090 i::V8::FatalProcessOutOfMemory(NULL); \
91 } \
Steve Block44f0eee2011-05-26 01:26:41 +010092 bool call_depth_is_zero = handle_scope_implementer->CallDepthIsZero(); \
93 (isolate)->OptionalRescheduleException(call_depth_is_zero); \
Steve Blocka7e24c12009-10-30 11:49:00 +000094 return value; \
95 } \
96 } while (false)
97
98
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000099#define API_ENTRY_CHECK(isolate, msg) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000100 do { \
101 if (v8::Locker::IsActive()) { \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000102 ApiCheck(isolate->thread_manager()->IsLockedByCurrentThread(), \
Steve Blocka7e24c12009-10-30 11:49:00 +0000103 msg, \
104 "Entering the V8 API without proper locking in place"); \
105 } \
106 } while (false)
107
Ben Murdochb0fe1622011-05-05 13:52:32 +0100108
Steve Blocka7e24c12009-10-30 11:49:00 +0000109// --- E x c e p t i o n B e h a v i o r ---
110
111
Steve Blocka7e24c12009-10-30 11:49:00 +0000112static void DefaultFatalErrorHandler(const char* location,
113 const char* message) {
Steve Block44f0eee2011-05-26 01:26:41 +0100114 i::VMState __state__(i::Isolate::Current(), i::OTHER);
Steve Blocka7e24c12009-10-30 11:49:00 +0000115 API_Fatal(location, message);
116}
117
118
Steve Block44f0eee2011-05-26 01:26:41 +0100119static FatalErrorCallback GetFatalErrorHandler() {
120 i::Isolate* isolate = i::Isolate::Current();
121 if (isolate->exception_behavior() == NULL) {
122 isolate->set_exception_behavior(DefaultFatalErrorHandler);
Steve Blocka7e24c12009-10-30 11:49:00 +0000123 }
Steve Block44f0eee2011-05-26 01:26:41 +0100124 return isolate->exception_behavior();
Steve Blocka7e24c12009-10-30 11:49:00 +0000125}
126
127
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800128void i::FatalProcessOutOfMemory(const char* location) {
129 i::V8::FatalProcessOutOfMemory(location, false);
130}
131
Steve Blocka7e24c12009-10-30 11:49:00 +0000132
133// When V8 cannot allocated memory FatalProcessOutOfMemory is called.
134// The default fatal error handler is called and execution is stopped.
Ben Murdochbb769b22010-08-11 14:56:33 +0100135void i::V8::FatalProcessOutOfMemory(const char* location, bool take_snapshot) {
Steve Blockd0582a62009-12-15 09:54:21 +0000136 i::HeapStats heap_stats;
137 int start_marker;
138 heap_stats.start_marker = &start_marker;
139 int new_space_size;
140 heap_stats.new_space_size = &new_space_size;
141 int new_space_capacity;
142 heap_stats.new_space_capacity = &new_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100143 intptr_t old_pointer_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000144 heap_stats.old_pointer_space_size = &old_pointer_space_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100145 intptr_t old_pointer_space_capacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000146 heap_stats.old_pointer_space_capacity = &old_pointer_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100147 intptr_t old_data_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000148 heap_stats.old_data_space_size = &old_data_space_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100149 intptr_t old_data_space_capacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000150 heap_stats.old_data_space_capacity = &old_data_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100151 intptr_t code_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000152 heap_stats.code_space_size = &code_space_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100153 intptr_t code_space_capacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000154 heap_stats.code_space_capacity = &code_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100155 intptr_t map_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000156 heap_stats.map_space_size = &map_space_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100157 intptr_t map_space_capacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000158 heap_stats.map_space_capacity = &map_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100159 intptr_t cell_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000160 heap_stats.cell_space_size = &cell_space_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100161 intptr_t cell_space_capacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000162 heap_stats.cell_space_capacity = &cell_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100163 intptr_t lo_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000164 heap_stats.lo_space_size = &lo_space_size;
165 int global_handle_count;
166 heap_stats.global_handle_count = &global_handle_count;
167 int weak_global_handle_count;
168 heap_stats.weak_global_handle_count = &weak_global_handle_count;
169 int pending_global_handle_count;
170 heap_stats.pending_global_handle_count = &pending_global_handle_count;
171 int near_death_global_handle_count;
172 heap_stats.near_death_global_handle_count = &near_death_global_handle_count;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000173 int free_global_handle_count;
174 heap_stats.free_global_handle_count = &free_global_handle_count;
Ben Murdochf87a2032010-10-22 12:50:53 +0100175 intptr_t memory_allocator_size;
Ben Murdochbb769b22010-08-11 14:56:33 +0100176 heap_stats.memory_allocator_size = &memory_allocator_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100177 intptr_t memory_allocator_capacity;
Ben Murdochbb769b22010-08-11 14:56:33 +0100178 heap_stats.memory_allocator_capacity = &memory_allocator_capacity;
179 int objects_per_type[LAST_TYPE + 1] = {0};
180 heap_stats.objects_per_type = objects_per_type;
181 int size_per_type[LAST_TYPE + 1] = {0};
182 heap_stats.size_per_type = size_per_type;
Iain Merrick75681382010-08-19 15:07:18 +0100183 int os_error;
184 heap_stats.os_error = &os_error;
Steve Blockd0582a62009-12-15 09:54:21 +0000185 int end_marker;
186 heap_stats.end_marker = &end_marker;
Steve Block44f0eee2011-05-26 01:26:41 +0100187 i::Isolate* isolate = i::Isolate::Current();
188 isolate->heap()->RecordStats(&heap_stats, take_snapshot);
Steve Blocka7e24c12009-10-30 11:49:00 +0000189 i::V8::SetFatalError();
190 FatalErrorCallback callback = GetFatalErrorHandler();
191 {
Steve Block44f0eee2011-05-26 01:26:41 +0100192 LEAVE_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000193 callback(location, "Allocation failed - process out of memory");
194 }
195 // If the callback returns, we stop execution.
196 UNREACHABLE();
197}
198
199
Steve Blocka7e24c12009-10-30 11:49:00 +0000200bool Utils::ReportApiFailure(const char* location, const char* message) {
201 FatalErrorCallback callback = GetFatalErrorHandler();
202 callback(location, message);
203 i::V8::SetFatalError();
204 return false;
205}
206
207
208bool V8::IsDead() {
209 return i::V8::IsDead();
210}
211
212
213static inline bool ApiCheck(bool condition,
214 const char* location,
215 const char* message) {
216 return condition ? true : Utils::ReportApiFailure(location, message);
217}
218
219
220static bool ReportV8Dead(const char* location) {
221 FatalErrorCallback callback = GetFatalErrorHandler();
222 callback(location, "V8 is no longer usable");
223 return true;
224}
225
226
227static bool ReportEmptyHandle(const char* location) {
228 FatalErrorCallback callback = GetFatalErrorHandler();
229 callback(location, "Reading from empty handle");
230 return true;
231}
232
233
234/**
235 * IsDeadCheck checks that the vm is usable. If, for instance, the vm has been
236 * out of memory at some point this check will fail. It should be called on
237 * entry to all methods that touch anything in the heap, except destructors
238 * which you sometimes can't avoid calling after the vm has crashed. Functions
239 * that call EnsureInitialized or ON_BAILOUT don't have to also call
240 * IsDeadCheck. ON_BAILOUT has the advantage over EnsureInitialized that you
241 * can arrange to return if the VM is dead. This is needed to ensure that no VM
242 * heap allocations are attempted on a dead VM. EnsureInitialized has the
243 * advantage over ON_BAILOUT that it actually initializes the VM if this has not
244 * yet been done.
245 */
Steve Block44f0eee2011-05-26 01:26:41 +0100246static inline bool IsDeadCheck(i::Isolate* isolate, const char* location) {
247 return !isolate->IsInitialized()
Steve Blocka7e24c12009-10-30 11:49:00 +0000248 && i::V8::IsDead() ? ReportV8Dead(location) : false;
249}
250
251
Steve Block44f0eee2011-05-26 01:26:41 +0100252static inline bool IsExecutionTerminatingCheck(i::Isolate* isolate) {
253 if (!isolate->IsInitialized()) return false;
254 if (isolate->has_scheduled_exception()) {
255 return isolate->scheduled_exception() ==
256 isolate->heap()->termination_exception();
257 }
258 return false;
259}
260
261
Steve Blocka7e24c12009-10-30 11:49:00 +0000262static inline bool EmptyCheck(const char* location, v8::Handle<v8::Data> obj) {
263 return obj.IsEmpty() ? ReportEmptyHandle(location) : false;
264}
265
266
267static inline bool EmptyCheck(const char* location, const v8::Data* obj) {
268 return (obj == 0) ? ReportEmptyHandle(location) : false;
269}
270
271// --- S t a t i c s ---
272
273
Steve Block44f0eee2011-05-26 01:26:41 +0100274static bool InitializeHelper() {
275 if (i::Snapshot::Initialize()) return true;
276 return i::V8::Initialize(NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000277}
278
279
Steve Block44f0eee2011-05-26 01:26:41 +0100280static inline bool EnsureInitializedForIsolate(i::Isolate* isolate,
281 const char* location) {
282 if (IsDeadCheck(isolate, location)) return false;
283 if (isolate != NULL) {
284 if (isolate->IsInitialized()) return true;
285 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000286 ASSERT(isolate == i::Isolate::Current());
Steve Block44f0eee2011-05-26 01:26:41 +0100287 return ApiCheck(InitializeHelper(), location, "Error initializing V8");
288}
289
290// Some initializing API functions are called early and may be
291// called on a thread different from static initializer thread.
292// If Isolate API is used, Isolate::Enter() will initialize TLS so
293// Isolate::Current() works. If it's a legacy case, then the thread
294// may not have TLS initialized yet. However, in initializing APIs it
295// may be too early to call EnsureInitialized() - some pre-init
296// parameters still have to be configured.
297static inline i::Isolate* EnterIsolateIfNeeded() {
298 i::Isolate* isolate = i::Isolate::UncheckedCurrent();
299 if (isolate != NULL)
300 return isolate;
301
302 i::Isolate::EnterDefaultIsolate();
303 isolate = i::Isolate::Current();
304 return isolate;
305}
306
307
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000308StartupDataDecompressor::StartupDataDecompressor()
309 : raw_data(i::NewArray<char*>(V8::GetCompressedStartupDataCount())) {
310 for (int i = 0; i < V8::GetCompressedStartupDataCount(); ++i) {
311 raw_data[i] = NULL;
312 }
313}
314
315
316StartupDataDecompressor::~StartupDataDecompressor() {
317 for (int i = 0; i < V8::GetCompressedStartupDataCount(); ++i) {
318 i::DeleteArray(raw_data[i]);
319 }
320 i::DeleteArray(raw_data);
321}
322
323
324int StartupDataDecompressor::Decompress() {
325 int compressed_data_count = V8::GetCompressedStartupDataCount();
326 StartupData* compressed_data =
327 i::NewArray<StartupData>(compressed_data_count);
328 V8::GetCompressedStartupData(compressed_data);
329 for (int i = 0; i < compressed_data_count; ++i) {
330 char* decompressed = raw_data[i] =
331 i::NewArray<char>(compressed_data[i].raw_size);
332 if (compressed_data[i].compressed_size != 0) {
333 int result = DecompressData(decompressed,
334 &compressed_data[i].raw_size,
335 compressed_data[i].data,
336 compressed_data[i].compressed_size);
337 if (result != 0) return result;
338 } else {
339 ASSERT_EQ(0, compressed_data[i].raw_size);
340 }
341 compressed_data[i].data = decompressed;
342 }
343 V8::SetDecompressedStartupData(compressed_data);
344 return 0;
345}
346
347
Ben Murdoch257744e2011-11-30 15:57:28 +0000348StartupData::CompressionAlgorithm V8::GetCompressedStartupDataAlgorithm() {
349#ifdef COMPRESS_STARTUP_DATA_BZ2
350 return StartupData::kBZip2;
351#else
352 return StartupData::kUncompressed;
353#endif
354}
355
356
357enum CompressedStartupDataItems {
358 kSnapshot = 0,
359 kSnapshotContext,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000360 kLibraries,
361 kExperimentalLibraries,
Ben Murdoch257744e2011-11-30 15:57:28 +0000362 kCompressedStartupDataCount
363};
364
365int V8::GetCompressedStartupDataCount() {
366#ifdef COMPRESS_STARTUP_DATA_BZ2
367 return kCompressedStartupDataCount;
368#else
369 return 0;
370#endif
371}
372
373
374void V8::GetCompressedStartupData(StartupData* compressed_data) {
375#ifdef COMPRESS_STARTUP_DATA_BZ2
376 compressed_data[kSnapshot].data =
377 reinterpret_cast<const char*>(i::Snapshot::data());
378 compressed_data[kSnapshot].compressed_size = i::Snapshot::size();
379 compressed_data[kSnapshot].raw_size = i::Snapshot::raw_size();
380
381 compressed_data[kSnapshotContext].data =
382 reinterpret_cast<const char*>(i::Snapshot::context_data());
383 compressed_data[kSnapshotContext].compressed_size =
384 i::Snapshot::context_size();
385 compressed_data[kSnapshotContext].raw_size = i::Snapshot::context_raw_size();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000386
387 i::Vector<const i::byte> libraries_source = i::Natives::GetScriptsSource();
388 compressed_data[kLibraries].data =
389 reinterpret_cast<const char*>(libraries_source.start());
390 compressed_data[kLibraries].compressed_size = libraries_source.length();
391 compressed_data[kLibraries].raw_size = i::Natives::GetRawScriptsSize();
392
393 i::Vector<const i::byte> exp_libraries_source =
394 i::ExperimentalNatives::GetScriptsSource();
395 compressed_data[kExperimentalLibraries].data =
396 reinterpret_cast<const char*>(exp_libraries_source.start());
397 compressed_data[kExperimentalLibraries].compressed_size =
398 exp_libraries_source.length();
399 compressed_data[kExperimentalLibraries].raw_size =
400 i::ExperimentalNatives::GetRawScriptsSize();
Ben Murdoch257744e2011-11-30 15:57:28 +0000401#endif
402}
403
404
405void V8::SetDecompressedStartupData(StartupData* decompressed_data) {
406#ifdef COMPRESS_STARTUP_DATA_BZ2
407 ASSERT_EQ(i::Snapshot::raw_size(), decompressed_data[kSnapshot].raw_size);
408 i::Snapshot::set_raw_data(
409 reinterpret_cast<const i::byte*>(decompressed_data[kSnapshot].data));
410
411 ASSERT_EQ(i::Snapshot::context_raw_size(),
412 decompressed_data[kSnapshotContext].raw_size);
413 i::Snapshot::set_context_raw_data(
414 reinterpret_cast<const i::byte*>(
415 decompressed_data[kSnapshotContext].data));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000416
417 ASSERT_EQ(i::Natives::GetRawScriptsSize(),
418 decompressed_data[kLibraries].raw_size);
419 i::Vector<const char> libraries_source(
420 decompressed_data[kLibraries].data,
421 decompressed_data[kLibraries].raw_size);
422 i::Natives::SetRawScriptsSource(libraries_source);
423
424 ASSERT_EQ(i::ExperimentalNatives::GetRawScriptsSize(),
425 decompressed_data[kExperimentalLibraries].raw_size);
426 i::Vector<const char> exp_libraries_source(
427 decompressed_data[kExperimentalLibraries].data,
428 decompressed_data[kExperimentalLibraries].raw_size);
429 i::ExperimentalNatives::SetRawScriptsSource(exp_libraries_source);
Ben Murdoch257744e2011-11-30 15:57:28 +0000430#endif
431}
432
433
Steve Block44f0eee2011-05-26 01:26:41 +0100434void V8::SetFatalErrorHandler(FatalErrorCallback that) {
435 i::Isolate* isolate = EnterIsolateIfNeeded();
436 isolate->set_exception_behavior(that);
Steve Blocka7e24c12009-10-30 11:49:00 +0000437}
438
439
Ben Murdoch257744e2011-11-30 15:57:28 +0000440void V8::SetAllowCodeGenerationFromStringsCallback(
441 AllowCodeGenerationFromStringsCallback callback) {
442 i::Isolate* isolate = EnterIsolateIfNeeded();
443 isolate->set_allow_code_gen_callback(callback);
444}
445
446
Steve Blocka7e24c12009-10-30 11:49:00 +0000447#ifdef DEBUG
448void ImplementationUtilities::ZapHandleRange(i::Object** begin,
449 i::Object** end) {
450 i::HandleScope::ZapRange(begin, end);
451}
452#endif
453
454
Steve Blocka7e24c12009-10-30 11:49:00 +0000455void V8::SetFlagsFromString(const char* str, int length) {
456 i::FlagList::SetFlagsFromString(str, length);
457}
458
459
460void V8::SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags) {
461 i::FlagList::SetFlagsFromCommandLine(argc, argv, remove_flags);
462}
463
464
465v8::Handle<Value> ThrowException(v8::Handle<v8::Value> value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100466 i::Isolate* isolate = i::Isolate::Current();
467 if (IsDeadCheck(isolate, "v8::ThrowException()")) {
468 return v8::Handle<Value>();
469 }
470 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000471 // If we're passed an empty handle, we throw an undefined exception
472 // to deal more gracefully with out of memory situations.
473 if (value.IsEmpty()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100474 isolate->ScheduleThrow(isolate->heap()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100476 isolate->ScheduleThrow(*Utils::OpenHandle(*value));
Steve Blocka7e24c12009-10-30 11:49:00 +0000477 }
478 return v8::Undefined();
479}
480
481
482RegisteredExtension* RegisteredExtension::first_extension_ = NULL;
483
484
485RegisteredExtension::RegisteredExtension(Extension* extension)
486 : extension_(extension), state_(UNVISITED) { }
487
488
489void RegisteredExtension::Register(RegisteredExtension* that) {
Steve Block44f0eee2011-05-26 01:26:41 +0100490 that->next_ = first_extension_;
491 first_extension_ = that;
Steve Blocka7e24c12009-10-30 11:49:00 +0000492}
493
494
495void RegisterExtension(Extension* that) {
496 RegisteredExtension* extension = new RegisteredExtension(that);
497 RegisteredExtension::Register(extension);
498}
499
500
501Extension::Extension(const char* name,
502 const char* source,
503 int dep_count,
504 const char** deps)
505 : name_(name),
506 source_(source),
507 dep_count_(dep_count),
508 deps_(deps),
509 auto_enable_(false) { }
510
511
512v8::Handle<Primitive> Undefined() {
Steve Block44f0eee2011-05-26 01:26:41 +0100513 i::Isolate* isolate = i::Isolate::Current();
514 if (!EnsureInitializedForIsolate(isolate, "v8::Undefined()")) {
515 return v8::Handle<v8::Primitive>();
516 }
517 return v8::Handle<Primitive>(ToApi<Primitive>(
518 isolate->factory()->undefined_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000519}
520
521
522v8::Handle<Primitive> Null() {
Steve Block44f0eee2011-05-26 01:26:41 +0100523 i::Isolate* isolate = i::Isolate::Current();
524 if (!EnsureInitializedForIsolate(isolate, "v8::Null()")) {
525 return v8::Handle<v8::Primitive>();
526 }
527 return v8::Handle<Primitive>(
528 ToApi<Primitive>(isolate->factory()->null_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000529}
530
531
532v8::Handle<Boolean> True() {
Steve Block44f0eee2011-05-26 01:26:41 +0100533 i::Isolate* isolate = i::Isolate::Current();
534 if (!EnsureInitializedForIsolate(isolate, "v8::True()")) {
535 return v8::Handle<Boolean>();
536 }
537 return v8::Handle<Boolean>(
538 ToApi<Boolean>(isolate->factory()->true_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000539}
540
541
542v8::Handle<Boolean> False() {
Steve Block44f0eee2011-05-26 01:26:41 +0100543 i::Isolate* isolate = i::Isolate::Current();
544 if (!EnsureInitializedForIsolate(isolate, "v8::False()")) {
545 return v8::Handle<Boolean>();
546 }
547 return v8::Handle<Boolean>(
548 ToApi<Boolean>(isolate->factory()->false_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000549}
550
551
552ResourceConstraints::ResourceConstraints()
553 : max_young_space_size_(0),
554 max_old_space_size_(0),
Russell Brenner90bac252010-11-18 13:33:46 -0800555 max_executable_size_(0),
Steve Blocka7e24c12009-10-30 11:49:00 +0000556 stack_limit_(NULL) { }
557
558
559bool SetResourceConstraints(ResourceConstraints* constraints) {
Steve Block44f0eee2011-05-26 01:26:41 +0100560 i::Isolate* isolate = EnterIsolateIfNeeded();
561
Steve Block3ce2e202009-11-05 08:53:23 +0000562 int young_space_size = constraints->max_young_space_size();
Steve Blocka7e24c12009-10-30 11:49:00 +0000563 int old_gen_size = constraints->max_old_space_size();
Russell Brenner90bac252010-11-18 13:33:46 -0800564 int max_executable_size = constraints->max_executable_size();
565 if (young_space_size != 0 || old_gen_size != 0 || max_executable_size != 0) {
Steve Block44f0eee2011-05-26 01:26:41 +0100566 // After initialization it's too late to change Heap constraints.
567 ASSERT(!isolate->IsInitialized());
568 bool result = isolate->heap()->ConfigureHeap(young_space_size / 2,
569 old_gen_size,
570 max_executable_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000571 if (!result) return false;
572 }
573 if (constraints->stack_limit() != NULL) {
574 uintptr_t limit = reinterpret_cast<uintptr_t>(constraints->stack_limit());
Steve Block44f0eee2011-05-26 01:26:41 +0100575 isolate->stack_guard()->SetStackLimit(limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000576 }
577 return true;
578}
579
580
581i::Object** V8::GlobalizeReference(i::Object** obj) {
Steve Block44f0eee2011-05-26 01:26:41 +0100582 i::Isolate* isolate = i::Isolate::Current();
583 if (IsDeadCheck(isolate, "V8::Persistent::New")) return NULL;
584 LOG_API(isolate, "Persistent::New");
Steve Blocka7e24c12009-10-30 11:49:00 +0000585 i::Handle<i::Object> result =
Steve Block44f0eee2011-05-26 01:26:41 +0100586 isolate->global_handles()->Create(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +0000587 return result.location();
588}
589
590
591void V8::MakeWeak(i::Object** object, void* parameters,
592 WeakReferenceCallback callback) {
Steve Block44f0eee2011-05-26 01:26:41 +0100593 i::Isolate* isolate = i::Isolate::Current();
594 LOG_API(isolate, "MakeWeak");
595 isolate->global_handles()->MakeWeak(object, parameters,
596 callback);
Steve Blocka7e24c12009-10-30 11:49:00 +0000597}
598
599
600void V8::ClearWeak(i::Object** obj) {
Steve Block44f0eee2011-05-26 01:26:41 +0100601 i::Isolate* isolate = i::Isolate::Current();
602 LOG_API(isolate, "ClearWeak");
603 isolate->global_handles()->ClearWeakness(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +0000604}
605
606
Ben Murdoch257744e2011-11-30 15:57:28 +0000607void V8::MarkIndependent(i::Object** object) {
608 i::Isolate* isolate = i::Isolate::Current();
609 LOG_API(isolate, "MakeIndependent");
610 isolate->global_handles()->MarkIndependent(object);
611}
612
613
Steve Blocka7e24c12009-10-30 11:49:00 +0000614bool V8::IsGlobalNearDeath(i::Object** obj) {
Steve Block44f0eee2011-05-26 01:26:41 +0100615 i::Isolate* isolate = i::Isolate::Current();
616 LOG_API(isolate, "IsGlobalNearDeath");
617 if (!isolate->IsInitialized()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000618 return i::GlobalHandles::IsNearDeath(obj);
619}
620
621
622bool V8::IsGlobalWeak(i::Object** obj) {
Steve Block44f0eee2011-05-26 01:26:41 +0100623 i::Isolate* isolate = i::Isolate::Current();
624 LOG_API(isolate, "IsGlobalWeak");
625 if (!isolate->IsInitialized()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000626 return i::GlobalHandles::IsWeak(obj);
627}
628
629
630void V8::DisposeGlobal(i::Object** obj) {
Steve Block44f0eee2011-05-26 01:26:41 +0100631 i::Isolate* isolate = i::Isolate::Current();
632 LOG_API(isolate, "DisposeGlobal");
633 if (!isolate->IsInitialized()) return;
634 isolate->global_handles()->Destroy(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +0000635}
636
637// --- H a n d l e s ---
638
639
Steve Block44f0eee2011-05-26 01:26:41 +0100640HandleScope::HandleScope() {
Steve Block44f0eee2011-05-26 01:26:41 +0100641 i::Isolate* isolate = i::Isolate::Current();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000642 API_ENTRY_CHECK(isolate, "HandleScope::HandleScope");
Steve Block44f0eee2011-05-26 01:26:41 +0100643 v8::ImplementationUtilities::HandleScopeData* current =
644 isolate->handle_scope_data();
645 isolate_ = isolate;
646 prev_next_ = current->next;
647 prev_limit_ = current->limit;
648 is_closed_ = false;
649 current->level++;
Steve Blocka7e24c12009-10-30 11:49:00 +0000650}
651
652
653HandleScope::~HandleScope() {
654 if (!is_closed_) {
John Reck59135872010-11-02 12:39:01 -0700655 Leave();
Steve Blocka7e24c12009-10-30 11:49:00 +0000656 }
657}
658
659
John Reck59135872010-11-02 12:39:01 -0700660void HandleScope::Leave() {
Steve Block44f0eee2011-05-26 01:26:41 +0100661 ASSERT(isolate_ == i::Isolate::Current());
662 v8::ImplementationUtilities::HandleScopeData* current =
663 isolate_->handle_scope_data();
664 current->level--;
665 ASSERT(current->level >= 0);
666 current->next = prev_next_;
667 if (current->limit != prev_limit_) {
668 current->limit = prev_limit_;
669 i::HandleScope::DeleteExtensions(isolate_);
John Reck59135872010-11-02 12:39:01 -0700670 }
671
672#ifdef DEBUG
673 i::HandleScope::ZapRange(prev_next_, prev_limit_);
674#endif
675}
676
677
Steve Blocka7e24c12009-10-30 11:49:00 +0000678int HandleScope::NumberOfHandles() {
Steve Block44f0eee2011-05-26 01:26:41 +0100679 EnsureInitializedForIsolate(
680 i::Isolate::Current(), "HandleScope::NumberOfHandles");
Steve Blocka7e24c12009-10-30 11:49:00 +0000681 return i::HandleScope::NumberOfHandles();
682}
683
684
Steve Block44f0eee2011-05-26 01:26:41 +0100685i::Object** HandleScope::CreateHandle(i::Object* value) {
686 return i::HandleScope::CreateHandle(value, i::Isolate::Current());
687}
688
689
690i::Object** HandleScope::CreateHandle(i::HeapObject* value) {
691 ASSERT(value->IsHeapObject());
692 return reinterpret_cast<i::Object**>(
693 i::HandleScope::CreateHandle(value, value->GetIsolate()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000694}
695
696
697void Context::Enter() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000698 i::Handle<i::Context> env = Utils::OpenHandle(this);
699 i::Isolate* isolate = env->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100700 if (IsDeadCheck(isolate, "v8::Context::Enter()")) return;
701 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000702
Steve Block44f0eee2011-05-26 01:26:41 +0100703 isolate->handle_scope_implementer()->EnterContext(env);
704
705 isolate->handle_scope_implementer()->SaveContext(isolate->context());
706 isolate->set_context(*env);
Steve Blocka7e24c12009-10-30 11:49:00 +0000707}
708
709
710void Context::Exit() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000711 // Exit is essentially a static function and doesn't use the
712 // receiver, so we have to get the current isolate from the thread
713 // local.
Steve Block44f0eee2011-05-26 01:26:41 +0100714 i::Isolate* isolate = i::Isolate::Current();
715 if (!isolate->IsInitialized()) return;
716
717 if (!ApiCheck(isolate->handle_scope_implementer()->LeaveLastContext(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000718 "v8::Context::Exit()",
719 "Cannot exit non-entered context")) {
720 return;
721 }
722
723 // Content of 'last_context' could be NULL.
Steve Block44f0eee2011-05-26 01:26:41 +0100724 i::Context* last_context =
725 isolate->handle_scope_implementer()->RestoreContext();
726 isolate->set_context(last_context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000727}
728
729
Steve Blockd0582a62009-12-15 09:54:21 +0000730void Context::SetData(v8::Handle<String> data) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000731 i::Handle<i::Context> env = Utils::OpenHandle(this);
732 i::Isolate* isolate = env->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100733 if (IsDeadCheck(isolate, "v8::Context::SetData()")) return;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000734 i::Handle<i::Object> raw_data = Utils::OpenHandle(*data);
735 ASSERT(env->IsGlobalContext());
736 if (env->IsGlobalContext()) {
737 env->set_data(*raw_data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000738 }
739}
740
741
742v8::Local<v8::Value> Context::GetData() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000743 i::Handle<i::Context> env = Utils::OpenHandle(this);
744 i::Isolate* isolate = env->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100745 if (IsDeadCheck(isolate, "v8::Context::GetData()")) {
746 return v8::Local<Value>();
747 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000748 i::Object* raw_result = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000749 ASSERT(env->IsGlobalContext());
750 if (env->IsGlobalContext()) {
751 raw_result = env->data();
752 } else {
753 return Local<Value>();
Steve Blocka7e24c12009-10-30 11:49:00 +0000754 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000755 i::Handle<i::Object> result(raw_result, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000756 return Utils::ToLocal(result);
757}
758
759
760i::Object** v8::HandleScope::RawClose(i::Object** value) {
761 if (!ApiCheck(!is_closed_,
762 "v8::HandleScope::Close()",
763 "Local scope has already been closed")) {
764 return 0;
765 }
Steve Block44f0eee2011-05-26 01:26:41 +0100766 LOG_API(isolate_, "CloseHandleScope");
Steve Blocka7e24c12009-10-30 11:49:00 +0000767
768 // Read the result before popping the handle block.
Steve Block6ded16b2010-05-10 14:33:55 +0100769 i::Object* result = NULL;
770 if (value != NULL) {
771 result = *value;
772 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000773 is_closed_ = true;
John Reck59135872010-11-02 12:39:01 -0700774 Leave();
Steve Blocka7e24c12009-10-30 11:49:00 +0000775
Steve Block6ded16b2010-05-10 14:33:55 +0100776 if (value == NULL) {
777 return NULL;
778 }
779
Steve Blocka7e24c12009-10-30 11:49:00 +0000780 // Allocate a new handle on the previous handle block.
781 i::Handle<i::Object> handle(result);
782 return handle.location();
783}
784
785
786// --- N e a n d e r ---
787
788
789// A constructor cannot easily return an error value, therefore it is necessary
790// to check for a dead VM with ON_BAILOUT before constructing any Neander
791// objects. To remind you about this there is no HandleScope in the
792// NeanderObject constructor. When you add one to the site calling the
793// constructor you should check that you ensured the VM was not dead first.
794NeanderObject::NeanderObject(int size) {
Steve Block44f0eee2011-05-26 01:26:41 +0100795 i::Isolate* isolate = i::Isolate::Current();
796 EnsureInitializedForIsolate(isolate, "v8::Nowhere");
797 ENTER_V8(isolate);
798 value_ = isolate->factory()->NewNeanderObject();
799 i::Handle<i::FixedArray> elements = isolate->factory()->NewFixedArray(size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000800 value_->set_elements(*elements);
801}
802
803
804int NeanderObject::size() {
805 return i::FixedArray::cast(value_->elements())->length();
806}
807
808
809NeanderArray::NeanderArray() : obj_(2) {
810 obj_.set(0, i::Smi::FromInt(0));
811}
812
813
814int NeanderArray::length() {
815 return i::Smi::cast(obj_.get(0))->value();
816}
817
818
819i::Object* NeanderArray::get(int offset) {
820 ASSERT(0 <= offset);
821 ASSERT(offset < length());
822 return obj_.get(offset + 1);
823}
824
825
826// This method cannot easily return an error value, therefore it is necessary
827// to check for a dead VM with ON_BAILOUT before calling it. To remind you
828// about this there is no HandleScope in this method. When you add one to the
829// site calling this method you should check that you ensured the VM was not
830// dead first.
831void NeanderArray::add(i::Handle<i::Object> value) {
832 int length = this->length();
833 int size = obj_.size();
834 if (length == size - 1) {
Steve Block44f0eee2011-05-26 01:26:41 +0100835 i::Handle<i::FixedArray> new_elms = FACTORY->NewFixedArray(2 * size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000836 for (int i = 0; i < length; i++)
837 new_elms->set(i + 1, get(i));
838 obj_.value()->set_elements(*new_elms);
839 }
840 obj_.set(length + 1, *value);
841 obj_.set(0, i::Smi::FromInt(length + 1));
842}
843
844
845void NeanderArray::set(int index, i::Object* value) {
846 if (index < 0 || index >= this->length()) return;
847 obj_.set(index + 1, value);
848}
849
850
851// --- T e m p l a t e ---
852
853
854static void InitializeTemplate(i::Handle<i::TemplateInfo> that, int type) {
855 that->set_tag(i::Smi::FromInt(type));
856}
857
858
859void Template::Set(v8::Handle<String> name, v8::Handle<Data> value,
860 v8::PropertyAttribute attribute) {
Steve Block44f0eee2011-05-26 01:26:41 +0100861 i::Isolate* isolate = i::Isolate::Current();
862 if (IsDeadCheck(isolate, "v8::Template::Set()")) return;
863 ENTER_V8(isolate);
864 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000865 i::Handle<i::Object> list(Utils::OpenHandle(this)->property_list());
866 if (list->IsUndefined()) {
867 list = NeanderArray().value();
868 Utils::OpenHandle(this)->set_property_list(*list);
869 }
870 NeanderArray array(list);
871 array.add(Utils::OpenHandle(*name));
872 array.add(Utils::OpenHandle(*value));
873 array.add(Utils::OpenHandle(*v8::Integer::New(attribute)));
874}
875
876
877// --- F u n c t i o n T e m p l a t e ---
878static void InitializeFunctionTemplate(
879 i::Handle<i::FunctionTemplateInfo> info) {
880 info->set_tag(i::Smi::FromInt(Consts::FUNCTION_TEMPLATE));
881 info->set_flag(0);
882}
883
884
885Local<ObjectTemplate> FunctionTemplate::PrototypeTemplate() {
Steve Block44f0eee2011-05-26 01:26:41 +0100886 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
887 if (IsDeadCheck(isolate, "v8::FunctionTemplate::PrototypeTemplate()")) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000888 return Local<ObjectTemplate>();
889 }
Steve Block44f0eee2011-05-26 01:26:41 +0100890 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000891 i::Handle<i::Object> result(Utils::OpenHandle(this)->prototype_template());
892 if (result->IsUndefined()) {
893 result = Utils::OpenHandle(*ObjectTemplate::New());
894 Utils::OpenHandle(this)->set_prototype_template(*result);
895 }
896 return Local<ObjectTemplate>(ToApi<ObjectTemplate>(result));
897}
898
899
900void FunctionTemplate::Inherit(v8::Handle<FunctionTemplate> value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100901 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
902 if (IsDeadCheck(isolate, "v8::FunctionTemplate::Inherit()")) return;
903 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000904 Utils::OpenHandle(this)->set_parent_template(*Utils::OpenHandle(*value));
905}
906
907
Steve Blocka7e24c12009-10-30 11:49:00 +0000908Local<FunctionTemplate> FunctionTemplate::New(InvocationCallback callback,
909 v8::Handle<Value> data, v8::Handle<Signature> signature) {
Steve Block44f0eee2011-05-26 01:26:41 +0100910 i::Isolate* isolate = i::Isolate::Current();
911 EnsureInitializedForIsolate(isolate, "v8::FunctionTemplate::New()");
912 LOG_API(isolate, "FunctionTemplate::New");
913 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000914 i::Handle<i::Struct> struct_obj =
Steve Block44f0eee2011-05-26 01:26:41 +0100915 isolate->factory()->NewStruct(i::FUNCTION_TEMPLATE_INFO_TYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000916 i::Handle<i::FunctionTemplateInfo> obj =
917 i::Handle<i::FunctionTemplateInfo>::cast(struct_obj);
918 InitializeFunctionTemplate(obj);
Steve Block44f0eee2011-05-26 01:26:41 +0100919 int next_serial_number = isolate->next_serial_number();
920 isolate->set_next_serial_number(next_serial_number + 1);
921 obj->set_serial_number(i::Smi::FromInt(next_serial_number));
Steve Blocka7e24c12009-10-30 11:49:00 +0000922 if (callback != 0) {
923 if (data.IsEmpty()) data = v8::Undefined();
924 Utils::ToLocal(obj)->SetCallHandler(callback, data);
925 }
926 obj->set_undetectable(false);
927 obj->set_needs_access_check(false);
928
929 if (!signature.IsEmpty())
930 obj->set_signature(*Utils::OpenHandle(*signature));
931 return Utils::ToLocal(obj);
932}
933
934
935Local<Signature> Signature::New(Handle<FunctionTemplate> receiver,
936 int argc, Handle<FunctionTemplate> argv[]) {
Steve Block44f0eee2011-05-26 01:26:41 +0100937 i::Isolate* isolate = i::Isolate::Current();
938 EnsureInitializedForIsolate(isolate, "v8::Signature::New()");
939 LOG_API(isolate, "Signature::New");
940 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000941 i::Handle<i::Struct> struct_obj =
Steve Block44f0eee2011-05-26 01:26:41 +0100942 isolate->factory()->NewStruct(i::SIGNATURE_INFO_TYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000943 i::Handle<i::SignatureInfo> obj =
944 i::Handle<i::SignatureInfo>::cast(struct_obj);
945 if (!receiver.IsEmpty()) obj->set_receiver(*Utils::OpenHandle(*receiver));
946 if (argc > 0) {
Steve Block44f0eee2011-05-26 01:26:41 +0100947 i::Handle<i::FixedArray> args = isolate->factory()->NewFixedArray(argc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000948 for (int i = 0; i < argc; i++) {
949 if (!argv[i].IsEmpty())
950 args->set(i, *Utils::OpenHandle(*argv[i]));
951 }
952 obj->set_args(*args);
953 }
954 return Utils::ToLocal(obj);
955}
956
957
958Local<TypeSwitch> TypeSwitch::New(Handle<FunctionTemplate> type) {
959 Handle<FunctionTemplate> types[1] = { type };
960 return TypeSwitch::New(1, types);
961}
962
963
964Local<TypeSwitch> TypeSwitch::New(int argc, Handle<FunctionTemplate> types[]) {
Steve Block44f0eee2011-05-26 01:26:41 +0100965 i::Isolate* isolate = i::Isolate::Current();
966 EnsureInitializedForIsolate(isolate, "v8::TypeSwitch::New()");
967 LOG_API(isolate, "TypeSwitch::New");
968 ENTER_V8(isolate);
969 i::Handle<i::FixedArray> vector = isolate->factory()->NewFixedArray(argc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000970 for (int i = 0; i < argc; i++)
971 vector->set(i, *Utils::OpenHandle(*types[i]));
972 i::Handle<i::Struct> struct_obj =
Steve Block44f0eee2011-05-26 01:26:41 +0100973 isolate->factory()->NewStruct(i::TYPE_SWITCH_INFO_TYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000974 i::Handle<i::TypeSwitchInfo> obj =
975 i::Handle<i::TypeSwitchInfo>::cast(struct_obj);
976 obj->set_types(*vector);
977 return Utils::ToLocal(obj);
978}
979
980
981int TypeSwitch::match(v8::Handle<Value> value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100982 i::Isolate* isolate = i::Isolate::Current();
983 LOG_API(isolate, "TypeSwitch::match");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000984 USE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000985 i::Handle<i::Object> obj = Utils::OpenHandle(*value);
986 i::Handle<i::TypeSwitchInfo> info = Utils::OpenHandle(this);
987 i::FixedArray* types = i::FixedArray::cast(info->types());
988 for (int i = 0; i < types->length(); i++) {
989 if (obj->IsInstanceOf(i::FunctionTemplateInfo::cast(types->get(i))))
990 return i + 1;
991 }
992 return 0;
993}
994
995
Ben Murdoch257744e2011-11-30 15:57:28 +0000996#define SET_FIELD_WRAPPED(obj, setter, cdata) do { \
997 i::Handle<i::Object> foreign = FromCData(cdata); \
998 (obj)->setter(*foreign); \
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100999 } while (false)
1000
1001
Steve Blocka7e24c12009-10-30 11:49:00 +00001002void FunctionTemplate::SetCallHandler(InvocationCallback callback,
1003 v8::Handle<Value> data) {
Steve Block44f0eee2011-05-26 01:26:41 +01001004 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1005 if (IsDeadCheck(isolate, "v8::FunctionTemplate::SetCallHandler()")) return;
1006 ENTER_V8(isolate);
1007 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001008 i::Handle<i::Struct> struct_obj =
Steve Block44f0eee2011-05-26 01:26:41 +01001009 isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001010 i::Handle<i::CallHandlerInfo> obj =
1011 i::Handle<i::CallHandlerInfo>::cast(struct_obj);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001012 SET_FIELD_WRAPPED(obj, set_callback, callback);
Steve Blocka7e24c12009-10-30 11:49:00 +00001013 if (data.IsEmpty()) data = v8::Undefined();
1014 obj->set_data(*Utils::OpenHandle(*data));
1015 Utils::OpenHandle(this)->set_call_code(*obj);
1016}
1017
1018
Leon Clarkef7060e22010-06-03 12:02:55 +01001019static i::Handle<i::AccessorInfo> MakeAccessorInfo(
1020 v8::Handle<String> name,
1021 AccessorGetter getter,
1022 AccessorSetter setter,
1023 v8::Handle<Value> data,
1024 v8::AccessControl settings,
1025 v8::PropertyAttribute attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +01001026 i::Handle<i::AccessorInfo> obj = FACTORY->NewAccessorInfo();
Leon Clarkef7060e22010-06-03 12:02:55 +01001027 ASSERT(getter != NULL);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001028 SET_FIELD_WRAPPED(obj, set_getter, getter);
1029 SET_FIELD_WRAPPED(obj, set_setter, setter);
Leon Clarkef7060e22010-06-03 12:02:55 +01001030 if (data.IsEmpty()) data = v8::Undefined();
1031 obj->set_data(*Utils::OpenHandle(*data));
1032 obj->set_name(*Utils::OpenHandle(*name));
1033 if (settings & ALL_CAN_READ) obj->set_all_can_read(true);
1034 if (settings & ALL_CAN_WRITE) obj->set_all_can_write(true);
1035 if (settings & PROHIBITS_OVERWRITING) obj->set_prohibits_overwriting(true);
1036 obj->set_property_attributes(static_cast<PropertyAttributes>(attributes));
1037 return obj;
1038}
1039
1040
Steve Blocka7e24c12009-10-30 11:49:00 +00001041void FunctionTemplate::AddInstancePropertyAccessor(
1042 v8::Handle<String> name,
1043 AccessorGetter getter,
1044 AccessorSetter setter,
1045 v8::Handle<Value> data,
1046 v8::AccessControl settings,
1047 v8::PropertyAttribute attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +01001048 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1049 if (IsDeadCheck(isolate,
1050 "v8::FunctionTemplate::AddInstancePropertyAccessor()")) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001051 return;
1052 }
Steve Block44f0eee2011-05-26 01:26:41 +01001053 ENTER_V8(isolate);
1054 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001055
Leon Clarkef7060e22010-06-03 12:02:55 +01001056 i::Handle<i::AccessorInfo> obj = MakeAccessorInfo(name,
1057 getter, setter, data,
1058 settings, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001059 i::Handle<i::Object> list(Utils::OpenHandle(this)->property_accessors());
1060 if (list->IsUndefined()) {
1061 list = NeanderArray().value();
1062 Utils::OpenHandle(this)->set_property_accessors(*list);
1063 }
1064 NeanderArray array(list);
1065 array.add(obj);
1066}
1067
1068
1069Local<ObjectTemplate> FunctionTemplate::InstanceTemplate() {
Steve Block44f0eee2011-05-26 01:26:41 +01001070 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1071 if (IsDeadCheck(isolate, "v8::FunctionTemplate::InstanceTemplate()")
Steve Blocka7e24c12009-10-30 11:49:00 +00001072 || EmptyCheck("v8::FunctionTemplate::InstanceTemplate()", this))
1073 return Local<ObjectTemplate>();
Steve Block44f0eee2011-05-26 01:26:41 +01001074 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001075 if (Utils::OpenHandle(this)->instance_template()->IsUndefined()) {
1076 Local<ObjectTemplate> templ =
1077 ObjectTemplate::New(v8::Handle<FunctionTemplate>(this));
1078 Utils::OpenHandle(this)->set_instance_template(*Utils::OpenHandle(*templ));
1079 }
1080 i::Handle<i::ObjectTemplateInfo> result(i::ObjectTemplateInfo::cast(
1081 Utils::OpenHandle(this)->instance_template()));
1082 return Utils::ToLocal(result);
1083}
1084
1085
1086void FunctionTemplate::SetClassName(Handle<String> name) {
Steve Block44f0eee2011-05-26 01:26:41 +01001087 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1088 if (IsDeadCheck(isolate, "v8::FunctionTemplate::SetClassName()")) return;
1089 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001090 Utils::OpenHandle(this)->set_class_name(*Utils::OpenHandle(*name));
1091}
1092
1093
1094void FunctionTemplate::SetHiddenPrototype(bool value) {
Steve Block44f0eee2011-05-26 01:26:41 +01001095 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1096 if (IsDeadCheck(isolate, "v8::FunctionTemplate::SetHiddenPrototype()")) {
1097 return;
1098 }
1099 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001100 Utils::OpenHandle(this)->set_hidden_prototype(value);
1101}
1102
1103
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001104void FunctionTemplate::ReadOnlyPrototype() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001105 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1106 if (IsDeadCheck(isolate, "v8::FunctionTemplate::SetPrototypeAttributes()")) {
1107 return;
1108 }
1109 ENTER_V8(isolate);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001110 Utils::OpenHandle(this)->set_read_only_prototype(true);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001111}
1112
1113
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001114void FunctionTemplate::SetNamedInstancePropertyHandler(
Steve Blocka7e24c12009-10-30 11:49:00 +00001115 NamedPropertyGetter getter,
1116 NamedPropertySetter setter,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001117 NamedPropertyQuery query,
Steve Blocka7e24c12009-10-30 11:49:00 +00001118 NamedPropertyDeleter remover,
1119 NamedPropertyEnumerator enumerator,
1120 Handle<Value> data) {
Steve Block44f0eee2011-05-26 01:26:41 +01001121 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1122 if (IsDeadCheck(isolate,
1123 "v8::FunctionTemplate::SetNamedInstancePropertyHandler()")) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001124 return;
1125 }
Steve Block44f0eee2011-05-26 01:26:41 +01001126 ENTER_V8(isolate);
1127 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001128 i::Handle<i::Struct> struct_obj =
Steve Block44f0eee2011-05-26 01:26:41 +01001129 isolate->factory()->NewStruct(i::INTERCEPTOR_INFO_TYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001130 i::Handle<i::InterceptorInfo> obj =
1131 i::Handle<i::InterceptorInfo>::cast(struct_obj);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001132
1133 if (getter != 0) SET_FIELD_WRAPPED(obj, set_getter, getter);
1134 if (setter != 0) SET_FIELD_WRAPPED(obj, set_setter, setter);
1135 if (query != 0) SET_FIELD_WRAPPED(obj, set_query, query);
1136 if (remover != 0) SET_FIELD_WRAPPED(obj, set_deleter, remover);
1137 if (enumerator != 0) SET_FIELD_WRAPPED(obj, set_enumerator, enumerator);
1138
Steve Blocka7e24c12009-10-30 11:49:00 +00001139 if (data.IsEmpty()) data = v8::Undefined();
1140 obj->set_data(*Utils::OpenHandle(*data));
1141 Utils::OpenHandle(this)->set_named_property_handler(*obj);
1142}
1143
1144
1145void FunctionTemplate::SetIndexedInstancePropertyHandler(
1146 IndexedPropertyGetter getter,
1147 IndexedPropertySetter setter,
1148 IndexedPropertyQuery query,
1149 IndexedPropertyDeleter remover,
1150 IndexedPropertyEnumerator enumerator,
1151 Handle<Value> data) {
Steve Block44f0eee2011-05-26 01:26:41 +01001152 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1153 if (IsDeadCheck(isolate,
Steve Blocka7e24c12009-10-30 11:49:00 +00001154 "v8::FunctionTemplate::SetIndexedInstancePropertyHandler()")) {
1155 return;
1156 }
Steve Block44f0eee2011-05-26 01:26:41 +01001157 ENTER_V8(isolate);
1158 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001159 i::Handle<i::Struct> struct_obj =
Steve Block44f0eee2011-05-26 01:26:41 +01001160 isolate->factory()->NewStruct(i::INTERCEPTOR_INFO_TYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001161 i::Handle<i::InterceptorInfo> obj =
1162 i::Handle<i::InterceptorInfo>::cast(struct_obj);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001163
1164 if (getter != 0) SET_FIELD_WRAPPED(obj, set_getter, getter);
1165 if (setter != 0) SET_FIELD_WRAPPED(obj, set_setter, setter);
1166 if (query != 0) SET_FIELD_WRAPPED(obj, set_query, query);
1167 if (remover != 0) SET_FIELD_WRAPPED(obj, set_deleter, remover);
1168 if (enumerator != 0) SET_FIELD_WRAPPED(obj, set_enumerator, enumerator);
1169
Steve Blocka7e24c12009-10-30 11:49:00 +00001170 if (data.IsEmpty()) data = v8::Undefined();
1171 obj->set_data(*Utils::OpenHandle(*data));
1172 Utils::OpenHandle(this)->set_indexed_property_handler(*obj);
1173}
1174
1175
1176void FunctionTemplate::SetInstanceCallAsFunctionHandler(
1177 InvocationCallback callback,
1178 Handle<Value> data) {
Steve Block44f0eee2011-05-26 01:26:41 +01001179 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1180 if (IsDeadCheck(isolate,
1181 "v8::FunctionTemplate::SetInstanceCallAsFunctionHandler()")) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001182 return;
1183 }
Steve Block44f0eee2011-05-26 01:26:41 +01001184 ENTER_V8(isolate);
1185 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001186 i::Handle<i::Struct> struct_obj =
Steve Block44f0eee2011-05-26 01:26:41 +01001187 isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001188 i::Handle<i::CallHandlerInfo> obj =
1189 i::Handle<i::CallHandlerInfo>::cast(struct_obj);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001190 SET_FIELD_WRAPPED(obj, set_callback, callback);
Steve Blocka7e24c12009-10-30 11:49:00 +00001191 if (data.IsEmpty()) data = v8::Undefined();
1192 obj->set_data(*Utils::OpenHandle(*data));
1193 Utils::OpenHandle(this)->set_instance_call_handler(*obj);
1194}
1195
1196
1197// --- O b j e c t T e m p l a t e ---
1198
1199
1200Local<ObjectTemplate> ObjectTemplate::New() {
1201 return New(Local<FunctionTemplate>());
1202}
1203
1204
1205Local<ObjectTemplate> ObjectTemplate::New(
1206 v8::Handle<FunctionTemplate> constructor) {
Steve Block44f0eee2011-05-26 01:26:41 +01001207 i::Isolate* isolate = i::Isolate::Current();
1208 if (IsDeadCheck(isolate, "v8::ObjectTemplate::New()")) {
1209 return Local<ObjectTemplate>();
1210 }
1211 EnsureInitializedForIsolate(isolate, "v8::ObjectTemplate::New()");
1212 LOG_API(isolate, "ObjectTemplate::New");
1213 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001214 i::Handle<i::Struct> struct_obj =
Steve Block44f0eee2011-05-26 01:26:41 +01001215 isolate->factory()->NewStruct(i::OBJECT_TEMPLATE_INFO_TYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001216 i::Handle<i::ObjectTemplateInfo> obj =
1217 i::Handle<i::ObjectTemplateInfo>::cast(struct_obj);
1218 InitializeTemplate(obj, Consts::OBJECT_TEMPLATE);
1219 if (!constructor.IsEmpty())
1220 obj->set_constructor(*Utils::OpenHandle(*constructor));
1221 obj->set_internal_field_count(i::Smi::FromInt(0));
1222 return Utils::ToLocal(obj);
1223}
1224
1225
1226// Ensure that the object template has a constructor. If no
1227// constructor is available we create one.
1228static void EnsureConstructor(ObjectTemplate* object_template) {
1229 if (Utils::OpenHandle(object_template)->constructor()->IsUndefined()) {
1230 Local<FunctionTemplate> templ = FunctionTemplate::New();
1231 i::Handle<i::FunctionTemplateInfo> constructor = Utils::OpenHandle(*templ);
1232 constructor->set_instance_template(*Utils::OpenHandle(object_template));
1233 Utils::OpenHandle(object_template)->set_constructor(*constructor);
1234 }
1235}
1236
1237
1238void ObjectTemplate::SetAccessor(v8::Handle<String> name,
1239 AccessorGetter getter,
1240 AccessorSetter setter,
1241 v8::Handle<Value> data,
1242 AccessControl settings,
1243 PropertyAttribute attribute) {
Steve Block44f0eee2011-05-26 01:26:41 +01001244 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1245 if (IsDeadCheck(isolate, "v8::ObjectTemplate::SetAccessor()")) return;
1246 ENTER_V8(isolate);
1247 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001248 EnsureConstructor(this);
1249 i::FunctionTemplateInfo* constructor =
1250 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1251 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1252 Utils::ToLocal(cons)->AddInstancePropertyAccessor(name,
1253 getter,
1254 setter,
1255 data,
1256 settings,
1257 attribute);
1258}
1259
1260
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001261void ObjectTemplate::SetNamedPropertyHandler(NamedPropertyGetter getter,
1262 NamedPropertySetter setter,
1263 NamedPropertyQuery query,
1264 NamedPropertyDeleter remover,
1265 NamedPropertyEnumerator enumerator,
1266 Handle<Value> data) {
Steve Block44f0eee2011-05-26 01:26:41 +01001267 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1268 if (IsDeadCheck(isolate, "v8::ObjectTemplate::SetNamedPropertyHandler()")) {
1269 return;
1270 }
1271 ENTER_V8(isolate);
1272 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001273 EnsureConstructor(this);
1274 i::FunctionTemplateInfo* constructor =
1275 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1276 i::Handle<i::FunctionTemplateInfo> cons(constructor);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001277 Utils::ToLocal(cons)->SetNamedInstancePropertyHandler(getter,
1278 setter,
1279 query,
1280 remover,
1281 enumerator,
1282 data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001283}
1284
1285
1286void ObjectTemplate::MarkAsUndetectable() {
Steve Block44f0eee2011-05-26 01:26:41 +01001287 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1288 if (IsDeadCheck(isolate, "v8::ObjectTemplate::MarkAsUndetectable()")) return;
1289 ENTER_V8(isolate);
1290 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001291 EnsureConstructor(this);
1292 i::FunctionTemplateInfo* constructor =
1293 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1294 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1295 cons->set_undetectable(true);
1296}
1297
1298
1299void ObjectTemplate::SetAccessCheckCallbacks(
1300 NamedSecurityCallback named_callback,
1301 IndexedSecurityCallback indexed_callback,
1302 Handle<Value> data,
1303 bool turned_on_by_default) {
Steve Block44f0eee2011-05-26 01:26:41 +01001304 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1305 if (IsDeadCheck(isolate, "v8::ObjectTemplate::SetAccessCheckCallbacks()")) {
1306 return;
1307 }
1308 ENTER_V8(isolate);
1309 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001310 EnsureConstructor(this);
1311
1312 i::Handle<i::Struct> struct_info =
Steve Block44f0eee2011-05-26 01:26:41 +01001313 isolate->factory()->NewStruct(i::ACCESS_CHECK_INFO_TYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001314 i::Handle<i::AccessCheckInfo> info =
1315 i::Handle<i::AccessCheckInfo>::cast(struct_info);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001316
1317 SET_FIELD_WRAPPED(info, set_named_callback, named_callback);
1318 SET_FIELD_WRAPPED(info, set_indexed_callback, indexed_callback);
1319
Steve Blocka7e24c12009-10-30 11:49:00 +00001320 if (data.IsEmpty()) data = v8::Undefined();
1321 info->set_data(*Utils::OpenHandle(*data));
1322
1323 i::FunctionTemplateInfo* constructor =
1324 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1325 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1326 cons->set_access_check_info(*info);
1327 cons->set_needs_access_check(turned_on_by_default);
1328}
1329
1330
1331void ObjectTemplate::SetIndexedPropertyHandler(
1332 IndexedPropertyGetter getter,
1333 IndexedPropertySetter setter,
1334 IndexedPropertyQuery query,
1335 IndexedPropertyDeleter remover,
1336 IndexedPropertyEnumerator enumerator,
1337 Handle<Value> data) {
Steve Block44f0eee2011-05-26 01:26:41 +01001338 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1339 if (IsDeadCheck(isolate, "v8::ObjectTemplate::SetIndexedPropertyHandler()")) {
1340 return;
1341 }
1342 ENTER_V8(isolate);
1343 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001344 EnsureConstructor(this);
1345 i::FunctionTemplateInfo* constructor =
1346 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1347 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1348 Utils::ToLocal(cons)->SetIndexedInstancePropertyHandler(getter,
1349 setter,
1350 query,
1351 remover,
1352 enumerator,
1353 data);
1354}
1355
1356
1357void ObjectTemplate::SetCallAsFunctionHandler(InvocationCallback callback,
1358 Handle<Value> data) {
Steve Block44f0eee2011-05-26 01:26:41 +01001359 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1360 if (IsDeadCheck(isolate,
1361 "v8::ObjectTemplate::SetCallAsFunctionHandler()")) {
1362 return;
1363 }
1364 ENTER_V8(isolate);
1365 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001366 EnsureConstructor(this);
1367 i::FunctionTemplateInfo* constructor =
1368 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1369 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1370 Utils::ToLocal(cons)->SetInstanceCallAsFunctionHandler(callback, data);
1371}
1372
1373
1374int ObjectTemplate::InternalFieldCount() {
Steve Block44f0eee2011-05-26 01:26:41 +01001375 if (IsDeadCheck(Utils::OpenHandle(this)->GetIsolate(),
1376 "v8::ObjectTemplate::InternalFieldCount()")) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001377 return 0;
1378 }
1379 return i::Smi::cast(Utils::OpenHandle(this)->internal_field_count())->value();
1380}
1381
1382
1383void ObjectTemplate::SetInternalFieldCount(int value) {
Steve Block44f0eee2011-05-26 01:26:41 +01001384 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1385 if (IsDeadCheck(isolate, "v8::ObjectTemplate::SetInternalFieldCount()")) {
1386 return;
1387 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001388 if (!ApiCheck(i::Smi::IsValid(value),
1389 "v8::ObjectTemplate::SetInternalFieldCount()",
1390 "Invalid internal field count")) {
1391 return;
1392 }
Steve Block44f0eee2011-05-26 01:26:41 +01001393 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001394 if (value > 0) {
1395 // The internal field count is set by the constructor function's
1396 // construct code, so we ensure that there is a constructor
1397 // function to do the setting.
1398 EnsureConstructor(this);
1399 }
1400 Utils::OpenHandle(this)->set_internal_field_count(i::Smi::FromInt(value));
1401}
1402
1403
1404// --- S c r i p t D a t a ---
1405
1406
1407ScriptData* ScriptData::PreCompile(const char* input, int length) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001408 i::Utf8ToUC16CharacterStream stream(
1409 reinterpret_cast<const unsigned char*>(input), length);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001410 return i::ParserApi::PreParse(&stream, NULL, i::FLAG_harmony_block_scoping);
Steve Blocka7e24c12009-10-30 11:49:00 +00001411}
1412
1413
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001414ScriptData* ScriptData::PreCompile(v8::Handle<String> source) {
1415 i::Handle<i::String> str = Utils::OpenHandle(*source);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001416 if (str->IsExternalTwoByteString()) {
1417 i::ExternalTwoByteStringUC16CharacterStream stream(
1418 i::Handle<i::ExternalTwoByteString>::cast(str), 0, str->length());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001419 return i::ParserApi::PreParse(&stream, NULL, i::FLAG_harmony_block_scoping);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001420 } else {
1421 i::GenericStringUC16CharacterStream stream(str, 0, str->length());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001422 return i::ParserApi::PreParse(&stream, NULL, i::FLAG_harmony_block_scoping);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001423 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001424}
1425
1426
Leon Clarkef7060e22010-06-03 12:02:55 +01001427ScriptData* ScriptData::New(const char* data, int length) {
1428 // Return an empty ScriptData if the length is obviously invalid.
1429 if (length % sizeof(unsigned) != 0) {
Iain Merrick9ac36c92010-09-13 15:29:50 +01001430 return new i::ScriptDataImpl();
Leon Clarkef7060e22010-06-03 12:02:55 +01001431 }
1432
1433 // Copy the data to ensure it is properly aligned.
1434 int deserialized_data_length = length / sizeof(unsigned);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001435 // If aligned, don't create a copy of the data.
1436 if (reinterpret_cast<intptr_t>(data) % sizeof(unsigned) == 0) {
1437 return new i::ScriptDataImpl(data, length);
1438 }
1439 // Copy the data to align it.
Leon Clarkef7060e22010-06-03 12:02:55 +01001440 unsigned* deserialized_data = i::NewArray<unsigned>(deserialized_data_length);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001441 i::OS::MemCopy(deserialized_data, data, length);
Leon Clarkef7060e22010-06-03 12:02:55 +01001442
1443 return new i::ScriptDataImpl(
1444 i::Vector<unsigned>(deserialized_data, deserialized_data_length));
Steve Blocka7e24c12009-10-30 11:49:00 +00001445}
1446
1447
1448// --- S c r i p t ---
1449
1450
1451Local<Script> Script::New(v8::Handle<String> source,
1452 v8::ScriptOrigin* origin,
Andrei Popescu402d9372010-02-26 13:31:12 +00001453 v8::ScriptData* pre_data,
1454 v8::Handle<String> script_data) {
Steve Block44f0eee2011-05-26 01:26:41 +01001455 i::Isolate* isolate = i::Isolate::Current();
1456 ON_BAILOUT(isolate, "v8::Script::New()", return Local<Script>());
1457 LOG_API(isolate, "Script::New");
1458 ENTER_V8(isolate);
Ben Murdoch692be652012-01-10 18:47:50 +00001459 i::SharedFunctionInfo* raw_result = NULL;
1460 { i::HandleScope scope(isolate);
1461 i::Handle<i::String> str = Utils::OpenHandle(*source);
1462 i::Handle<i::Object> name_obj;
1463 int line_offset = 0;
1464 int column_offset = 0;
1465 if (origin != NULL) {
1466 if (!origin->ResourceName().IsEmpty()) {
1467 name_obj = Utils::OpenHandle(*origin->ResourceName());
1468 }
1469 if (!origin->ResourceLineOffset().IsEmpty()) {
1470 line_offset = static_cast<int>(origin->ResourceLineOffset()->Value());
1471 }
1472 if (!origin->ResourceColumnOffset().IsEmpty()) {
1473 column_offset =
1474 static_cast<int>(origin->ResourceColumnOffset()->Value());
1475 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001476 }
Ben Murdoch692be652012-01-10 18:47:50 +00001477 EXCEPTION_PREAMBLE(isolate);
1478 i::ScriptDataImpl* pre_data_impl =
1479 static_cast<i::ScriptDataImpl*>(pre_data);
1480 // We assert that the pre-data is sane, even though we can actually
1481 // handle it if it turns out not to be in release mode.
1482 ASSERT(pre_data_impl == NULL || pre_data_impl->SanityCheck());
1483 // If the pre-data isn't sane we simply ignore it
1484 if (pre_data_impl != NULL && !pre_data_impl->SanityCheck()) {
1485 pre_data_impl = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00001486 }
Ben Murdoch692be652012-01-10 18:47:50 +00001487 i::Handle<i::SharedFunctionInfo> result =
Andrei Popescu31002712010-02-23 13:46:05 +00001488 i::Compiler::Compile(str,
1489 name_obj,
1490 line_offset,
1491 column_offset,
1492 NULL,
Andrei Popescu402d9372010-02-26 13:31:12 +00001493 pre_data_impl,
1494 Utils::OpenHandle(*script_data),
Andrei Popescu31002712010-02-23 13:46:05 +00001495 i::NOT_NATIVES_CODE);
Ben Murdoch692be652012-01-10 18:47:50 +00001496 has_pending_exception = result.is_null();
1497 EXCEPTION_BAILOUT_CHECK(isolate, Local<Script>());
1498 raw_result = *result;
1499 }
1500 i::Handle<i::SharedFunctionInfo> result(raw_result, isolate);
Steve Block6ded16b2010-05-10 14:33:55 +01001501 return Local<Script>(ToApi<Script>(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00001502}
1503
1504
1505Local<Script> Script::New(v8::Handle<String> source,
1506 v8::Handle<Value> file_name) {
1507 ScriptOrigin origin(file_name);
1508 return New(source, &origin);
1509}
1510
1511
1512Local<Script> Script::Compile(v8::Handle<String> source,
1513 v8::ScriptOrigin* origin,
Andrei Popescu402d9372010-02-26 13:31:12 +00001514 v8::ScriptData* pre_data,
1515 v8::Handle<String> script_data) {
Steve Block44f0eee2011-05-26 01:26:41 +01001516 i::Isolate* isolate = i::Isolate::Current();
1517 ON_BAILOUT(isolate, "v8::Script::Compile()", return Local<Script>());
1518 LOG_API(isolate, "Script::Compile");
1519 ENTER_V8(isolate);
Andrei Popescu402d9372010-02-26 13:31:12 +00001520 Local<Script> generic = New(source, origin, pre_data, script_data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001521 if (generic.IsEmpty())
1522 return generic;
Steve Block6ded16b2010-05-10 14:33:55 +01001523 i::Handle<i::Object> obj = Utils::OpenHandle(*generic);
1524 i::Handle<i::SharedFunctionInfo> function =
1525 i::Handle<i::SharedFunctionInfo>(i::SharedFunctionInfo::cast(*obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00001526 i::Handle<i::JSFunction> result =
Steve Block44f0eee2011-05-26 01:26:41 +01001527 isolate->factory()->NewFunctionFromSharedFunctionInfo(
1528 function,
1529 isolate->global_context());
Steve Blocka7e24c12009-10-30 11:49:00 +00001530 return Local<Script>(ToApi<Script>(result));
1531}
1532
1533
1534Local<Script> Script::Compile(v8::Handle<String> source,
Andrei Popescu402d9372010-02-26 13:31:12 +00001535 v8::Handle<Value> file_name,
1536 v8::Handle<String> script_data) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001537 ScriptOrigin origin(file_name);
Andrei Popescu402d9372010-02-26 13:31:12 +00001538 return Compile(source, &origin, 0, script_data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001539}
1540
1541
1542Local<Value> Script::Run() {
Steve Block44f0eee2011-05-26 01:26:41 +01001543 i::Isolate* isolate = i::Isolate::Current();
1544 ON_BAILOUT(isolate, "v8::Script::Run()", return Local<Value>());
1545 LOG_API(isolate, "Script::Run");
1546 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001547 i::Object* raw_result = NULL;
1548 {
Steve Block44f0eee2011-05-26 01:26:41 +01001549 i::HandleScope scope(isolate);
Steve Block6ded16b2010-05-10 14:33:55 +01001550 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1551 i::Handle<i::JSFunction> fun;
1552 if (obj->IsSharedFunctionInfo()) {
1553 i::Handle<i::SharedFunctionInfo>
Steve Block44f0eee2011-05-26 01:26:41 +01001554 function_info(i::SharedFunctionInfo::cast(*obj), isolate);
1555 fun = isolate->factory()->NewFunctionFromSharedFunctionInfo(
1556 function_info, isolate->global_context());
Steve Block6ded16b2010-05-10 14:33:55 +01001557 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001558 fun = i::Handle<i::JSFunction>(i::JSFunction::cast(*obj), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001559 }
Steve Block44f0eee2011-05-26 01:26:41 +01001560 EXCEPTION_PREAMBLE(isolate);
1561 i::Handle<i::Object> receiver(
1562 isolate->context()->global_proxy(), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001563 i::Handle<i::Object> result =
1564 i::Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01001565 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +00001566 raw_result = *result;
1567 }
Steve Block44f0eee2011-05-26 01:26:41 +01001568 i::Handle<i::Object> result(raw_result, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001569 return Utils::ToLocal(result);
1570}
1571
1572
Steve Block6ded16b2010-05-10 14:33:55 +01001573static i::Handle<i::SharedFunctionInfo> OpenScript(Script* script) {
1574 i::Handle<i::Object> obj = Utils::OpenHandle(script);
1575 i::Handle<i::SharedFunctionInfo> result;
1576 if (obj->IsSharedFunctionInfo()) {
1577 result =
1578 i::Handle<i::SharedFunctionInfo>(i::SharedFunctionInfo::cast(*obj));
1579 } else {
1580 result =
1581 i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared());
1582 }
1583 return result;
1584}
1585
1586
Steve Blocka7e24c12009-10-30 11:49:00 +00001587Local<Value> Script::Id() {
Steve Block44f0eee2011-05-26 01:26:41 +01001588 i::Isolate* isolate = i::Isolate::Current();
1589 ON_BAILOUT(isolate, "v8::Script::Id()", return Local<Value>());
1590 LOG_API(isolate, "Script::Id");
Steve Blocka7e24c12009-10-30 11:49:00 +00001591 i::Object* raw_id = NULL;
1592 {
Steve Block44f0eee2011-05-26 01:26:41 +01001593 i::HandleScope scope(isolate);
Steve Block6ded16b2010-05-10 14:33:55 +01001594 i::Handle<i::SharedFunctionInfo> function_info = OpenScript(this);
1595 i::Handle<i::Script> script(i::Script::cast(function_info->script()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001596 i::Handle<i::Object> id(script->id());
1597 raw_id = *id;
1598 }
1599 i::Handle<i::Object> id(raw_id);
1600 return Utils::ToLocal(id);
1601}
1602
1603
Steve Blockd0582a62009-12-15 09:54:21 +00001604void Script::SetData(v8::Handle<String> data) {
Steve Block44f0eee2011-05-26 01:26:41 +01001605 i::Isolate* isolate = i::Isolate::Current();
1606 ON_BAILOUT(isolate, "v8::Script::SetData()", return);
1607 LOG_API(isolate, "Script::SetData");
Steve Blocka7e24c12009-10-30 11:49:00 +00001608 {
Steve Block44f0eee2011-05-26 01:26:41 +01001609 i::HandleScope scope(isolate);
Steve Block6ded16b2010-05-10 14:33:55 +01001610 i::Handle<i::SharedFunctionInfo> function_info = OpenScript(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001611 i::Handle<i::Object> raw_data = Utils::OpenHandle(*data);
Steve Block6ded16b2010-05-10 14:33:55 +01001612 i::Handle<i::Script> script(i::Script::cast(function_info->script()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001613 script->set_data(*raw_data);
1614 }
1615}
1616
1617
1618// --- E x c e p t i o n s ---
1619
1620
1621v8::TryCatch::TryCatch()
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001622 : isolate_(i::Isolate::Current()),
1623 next_(isolate_->try_catch_handler_address()),
1624 exception_(isolate_->heap()->the_hole_value()),
Steve Blocka7e24c12009-10-30 11:49:00 +00001625 message_(i::Smi::FromInt(0)),
1626 is_verbose_(false),
1627 can_continue_(true),
1628 capture_message_(true),
Steve Blockd0582a62009-12-15 09:54:21 +00001629 rethrow_(false) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001630 isolate_->RegisterTryCatchHandler(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001631}
1632
1633
1634v8::TryCatch::~TryCatch() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001635 ASSERT(isolate_ == i::Isolate::Current());
Steve Blockd0582a62009-12-15 09:54:21 +00001636 if (rethrow_) {
1637 v8::HandleScope scope;
1638 v8::Local<v8::Value> exc = v8::Local<v8::Value>::New(Exception());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001639 isolate_->UnregisterTryCatchHandler(this);
Steve Blockd0582a62009-12-15 09:54:21 +00001640 v8::ThrowException(exc);
1641 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001642 isolate_->UnregisterTryCatchHandler(this);
Steve Blockd0582a62009-12-15 09:54:21 +00001643 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001644}
1645
1646
1647bool v8::TryCatch::HasCaught() const {
1648 return !reinterpret_cast<i::Object*>(exception_)->IsTheHole();
1649}
1650
1651
1652bool v8::TryCatch::CanContinue() const {
1653 return can_continue_;
1654}
1655
1656
Steve Blockd0582a62009-12-15 09:54:21 +00001657v8::Handle<v8::Value> v8::TryCatch::ReThrow() {
1658 if (!HasCaught()) return v8::Local<v8::Value>();
1659 rethrow_ = true;
1660 return v8::Undefined();
1661}
1662
1663
Steve Blocka7e24c12009-10-30 11:49:00 +00001664v8::Local<Value> v8::TryCatch::Exception() const {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001665 ASSERT(isolate_ == i::Isolate::Current());
Steve Blocka7e24c12009-10-30 11:49:00 +00001666 if (HasCaught()) {
1667 // Check for out of memory exception.
1668 i::Object* exception = reinterpret_cast<i::Object*>(exception_);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001669 return v8::Utils::ToLocal(i::Handle<i::Object>(exception, isolate_));
Steve Blocka7e24c12009-10-30 11:49:00 +00001670 } else {
1671 return v8::Local<Value>();
1672 }
1673}
1674
1675
1676v8::Local<Value> v8::TryCatch::StackTrace() const {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001677 ASSERT(isolate_ == i::Isolate::Current());
Steve Blocka7e24c12009-10-30 11:49:00 +00001678 if (HasCaught()) {
1679 i::Object* raw_obj = reinterpret_cast<i::Object*>(exception_);
1680 if (!raw_obj->IsJSObject()) return v8::Local<Value>();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001681 i::HandleScope scope(isolate_);
1682 i::Handle<i::JSObject> obj(i::JSObject::cast(raw_obj), isolate_);
1683 i::Handle<i::String> name = isolate_->factory()->LookupAsciiSymbol("stack");
1684 if (!obj->HasProperty(*name)) return v8::Local<Value>();
1685 i::Handle<i::Object> value = i::GetProperty(obj, name);
1686 if (value.is_null()) return v8::Local<Value>();
1687 return v8::Utils::ToLocal(scope.CloseAndEscape(value));
Steve Blocka7e24c12009-10-30 11:49:00 +00001688 } else {
1689 return v8::Local<Value>();
1690 }
1691}
1692
1693
1694v8::Local<v8::Message> v8::TryCatch::Message() const {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001695 ASSERT(isolate_ == i::Isolate::Current());
Steve Blocka7e24c12009-10-30 11:49:00 +00001696 if (HasCaught() && message_ != i::Smi::FromInt(0)) {
1697 i::Object* message = reinterpret_cast<i::Object*>(message_);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001698 return v8::Utils::MessageToLocal(i::Handle<i::Object>(message, isolate_));
Steve Blocka7e24c12009-10-30 11:49:00 +00001699 } else {
1700 return v8::Local<v8::Message>();
1701 }
1702}
1703
1704
1705void v8::TryCatch::Reset() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001706 ASSERT(isolate_ == i::Isolate::Current());
1707 exception_ = isolate_->heap()->the_hole_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001708 message_ = i::Smi::FromInt(0);
1709}
1710
1711
1712void v8::TryCatch::SetVerbose(bool value) {
1713 is_verbose_ = value;
1714}
1715
1716
1717void v8::TryCatch::SetCaptureMessage(bool value) {
1718 capture_message_ = value;
1719}
1720
1721
1722// --- M e s s a g e ---
1723
1724
1725Local<String> Message::Get() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001726 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1727 ON_BAILOUT(isolate, "v8::Message::Get()", return Local<String>());
1728 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001729 HandleScope scope;
1730 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1731 i::Handle<i::String> raw_result = i::MessageHandler::GetMessage(obj);
1732 Local<String> result = Utils::ToLocal(raw_result);
1733 return scope.Close(result);
1734}
1735
1736
1737v8::Handle<Value> Message::GetScriptResourceName() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001738 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1739 if (IsDeadCheck(isolate, "v8::Message::GetScriptResourceName()")) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001740 return Local<String>();
1741 }
Steve Block44f0eee2011-05-26 01:26:41 +01001742 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001743 HandleScope scope;
Steve Block1e0659c2011-05-24 12:43:12 +01001744 i::Handle<i::JSMessageObject> message =
1745 i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
Steve Blocka7e24c12009-10-30 11:49:00 +00001746 // Return this.script.name.
1747 i::Handle<i::JSValue> script =
Steve Block1e0659c2011-05-24 12:43:12 +01001748 i::Handle<i::JSValue>::cast(i::Handle<i::Object>(message->script()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001749 i::Handle<i::Object> resource_name(i::Script::cast(script->value())->name());
1750 return scope.Close(Utils::ToLocal(resource_name));
1751}
1752
1753
1754v8::Handle<Value> Message::GetScriptData() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001755 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1756 if (IsDeadCheck(isolate, "v8::Message::GetScriptResourceData()")) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001757 return Local<Value>();
1758 }
Steve Block44f0eee2011-05-26 01:26:41 +01001759 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001760 HandleScope scope;
Steve Block1e0659c2011-05-24 12:43:12 +01001761 i::Handle<i::JSMessageObject> message =
1762 i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
Steve Blocka7e24c12009-10-30 11:49:00 +00001763 // Return this.script.data.
1764 i::Handle<i::JSValue> script =
Steve Block1e0659c2011-05-24 12:43:12 +01001765 i::Handle<i::JSValue>::cast(i::Handle<i::Object>(message->script()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001766 i::Handle<i::Object> data(i::Script::cast(script->value())->data());
1767 return scope.Close(Utils::ToLocal(data));
1768}
1769
1770
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001771v8::Handle<v8::StackTrace> Message::GetStackTrace() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001772 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1773 if (IsDeadCheck(isolate, "v8::Message::GetStackTrace()")) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001774 return Local<v8::StackTrace>();
1775 }
Steve Block44f0eee2011-05-26 01:26:41 +01001776 ENTER_V8(isolate);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001777 HandleScope scope;
Steve Block1e0659c2011-05-24 12:43:12 +01001778 i::Handle<i::JSMessageObject> message =
1779 i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
1780 i::Handle<i::Object> stackFramesObj(message->stack_frames());
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001781 if (!stackFramesObj->IsJSArray()) return v8::Handle<v8::StackTrace>();
1782 i::Handle<i::JSArray> stackTrace =
1783 i::Handle<i::JSArray>::cast(stackFramesObj);
1784 return scope.Close(Utils::StackTraceToLocal(stackTrace));
1785}
1786
1787
Steve Blocka7e24c12009-10-30 11:49:00 +00001788static i::Handle<i::Object> CallV8HeapFunction(const char* name,
1789 i::Handle<i::Object> recv,
1790 int argc,
1791 i::Object** argv[],
1792 bool* has_pending_exception) {
Steve Block44f0eee2011-05-26 01:26:41 +01001793 i::Isolate* isolate = i::Isolate::Current();
1794 i::Handle<i::String> fmt_str = isolate->factory()->LookupAsciiSymbol(name);
John Reck59135872010-11-02 12:39:01 -07001795 i::Object* object_fun =
Steve Block44f0eee2011-05-26 01:26:41 +01001796 isolate->js_builtins_object()->GetPropertyNoExceptionThrown(*fmt_str);
Steve Blocka7e24c12009-10-30 11:49:00 +00001797 i::Handle<i::JSFunction> fun =
1798 i::Handle<i::JSFunction>(i::JSFunction::cast(object_fun));
1799 i::Handle<i::Object> value =
1800 i::Execution::Call(fun, recv, argc, argv, has_pending_exception);
1801 return value;
1802}
1803
1804
1805static i::Handle<i::Object> CallV8HeapFunction(const char* name,
1806 i::Handle<i::Object> data,
1807 bool* has_pending_exception) {
1808 i::Object** argv[1] = { data.location() };
1809 return CallV8HeapFunction(name,
Steve Block44f0eee2011-05-26 01:26:41 +01001810 i::Isolate::Current()->js_builtins_object(),
Steve Blocka7e24c12009-10-30 11:49:00 +00001811 1,
1812 argv,
1813 has_pending_exception);
1814}
1815
1816
1817int Message::GetLineNumber() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001818 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1819 ON_BAILOUT(isolate, "v8::Message::GetLineNumber()", return kNoLineNumberInfo);
1820 ENTER_V8(isolate);
1821 i::HandleScope scope(isolate);
Steve Block1e0659c2011-05-24 12:43:12 +01001822
Steve Block44f0eee2011-05-26 01:26:41 +01001823 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001824 i::Handle<i::Object> result = CallV8HeapFunction("GetLineNumber",
1825 Utils::OpenHandle(this),
1826 &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01001827 EXCEPTION_BAILOUT_CHECK(isolate, 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001828 return static_cast<int>(result->Number());
1829}
1830
1831
1832int Message::GetStartPosition() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001833 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1834 if (IsDeadCheck(isolate, "v8::Message::GetStartPosition()")) return 0;
1835 ENTER_V8(isolate);
1836 i::HandleScope scope(isolate);
Steve Block1e0659c2011-05-24 12:43:12 +01001837 i::Handle<i::JSMessageObject> message =
1838 i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
1839 return message->start_position();
Steve Blocka7e24c12009-10-30 11:49:00 +00001840}
1841
1842
1843int Message::GetEndPosition() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001844 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1845 if (IsDeadCheck(isolate, "v8::Message::GetEndPosition()")) return 0;
1846 ENTER_V8(isolate);
1847 i::HandleScope scope(isolate);
Steve Block1e0659c2011-05-24 12:43:12 +01001848 i::Handle<i::JSMessageObject> message =
1849 i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
1850 return message->end_position();
Steve Blocka7e24c12009-10-30 11:49:00 +00001851}
1852
1853
1854int Message::GetStartColumn() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001855 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1856 if (IsDeadCheck(isolate, "v8::Message::GetStartColumn()")) {
1857 return kNoColumnInfo;
1858 }
1859 ENTER_V8(isolate);
1860 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001861 i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01001862 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001863 i::Handle<i::Object> start_col_obj = CallV8HeapFunction(
1864 "GetPositionInLine",
1865 data_obj,
1866 &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01001867 EXCEPTION_BAILOUT_CHECK(isolate, 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001868 return static_cast<int>(start_col_obj->Number());
1869}
1870
1871
1872int Message::GetEndColumn() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001873 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1874 if (IsDeadCheck(isolate, "v8::Message::GetEndColumn()")) return kNoColumnInfo;
1875 ENTER_V8(isolate);
1876 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001877 i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01001878 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001879 i::Handle<i::Object> start_col_obj = CallV8HeapFunction(
1880 "GetPositionInLine",
1881 data_obj,
1882 &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01001883 EXCEPTION_BAILOUT_CHECK(isolate, 0);
Steve Block1e0659c2011-05-24 12:43:12 +01001884 i::Handle<i::JSMessageObject> message =
1885 i::Handle<i::JSMessageObject>::cast(data_obj);
1886 int start = message->start_position();
1887 int end = message->end_position();
Steve Blocka7e24c12009-10-30 11:49:00 +00001888 return static_cast<int>(start_col_obj->Number()) + (end - start);
1889}
1890
1891
1892Local<String> Message::GetSourceLine() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001893 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1894 ON_BAILOUT(isolate, "v8::Message::GetSourceLine()", return Local<String>());
1895 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001896 HandleScope scope;
Steve Block44f0eee2011-05-26 01:26:41 +01001897 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001898 i::Handle<i::Object> result = CallV8HeapFunction("GetSourceLine",
1899 Utils::OpenHandle(this),
1900 &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01001901 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::String>());
Steve Blocka7e24c12009-10-30 11:49:00 +00001902 if (result->IsString()) {
1903 return scope.Close(Utils::ToLocal(i::Handle<i::String>::cast(result)));
1904 } else {
1905 return Local<String>();
1906 }
1907}
1908
1909
1910void Message::PrintCurrentStackTrace(FILE* out) {
Steve Block44f0eee2011-05-26 01:26:41 +01001911 i::Isolate* isolate = i::Isolate::Current();
1912 if (IsDeadCheck(isolate, "v8::Message::PrintCurrentStackTrace()")) return;
1913 ENTER_V8(isolate);
1914 isolate->PrintCurrentStackTrace(out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001915}
1916
1917
Kristian Monsen25f61362010-05-21 11:50:48 +01001918// --- S t a c k T r a c e ---
1919
1920Local<StackFrame> StackTrace::GetFrame(uint32_t index) const {
Steve Block44f0eee2011-05-26 01:26:41 +01001921 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1922 if (IsDeadCheck(isolate, "v8::StackTrace::GetFrame()")) {
1923 return Local<StackFrame>();
1924 }
1925 ENTER_V8(isolate);
Kristian Monsen25f61362010-05-21 11:50:48 +01001926 HandleScope scope;
1927 i::Handle<i::JSArray> self = Utils::OpenHandle(this);
John Reck59135872010-11-02 12:39:01 -07001928 i::Object* raw_object = self->GetElementNoExceptionThrown(index);
1929 i::Handle<i::JSObject> obj(i::JSObject::cast(raw_object));
Kristian Monsen25f61362010-05-21 11:50:48 +01001930 return scope.Close(Utils::StackFrameToLocal(obj));
1931}
1932
1933
1934int StackTrace::GetFrameCount() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001935 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1936 if (IsDeadCheck(isolate, "v8::StackTrace::GetFrameCount()")) return -1;
1937 ENTER_V8(isolate);
Kristian Monsen25f61362010-05-21 11:50:48 +01001938 return i::Smi::cast(Utils::OpenHandle(this)->length())->value();
1939}
1940
1941
1942Local<Array> StackTrace::AsArray() {
Steve Block44f0eee2011-05-26 01:26:41 +01001943 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1944 if (IsDeadCheck(isolate, "v8::StackTrace::AsArray()")) Local<Array>();
1945 ENTER_V8(isolate);
Kristian Monsen25f61362010-05-21 11:50:48 +01001946 return Utils::ToLocal(Utils::OpenHandle(this));
1947}
1948
1949
1950Local<StackTrace> StackTrace::CurrentStackTrace(int frame_limit,
1951 StackTraceOptions options) {
Steve Block44f0eee2011-05-26 01:26:41 +01001952 i::Isolate* isolate = i::Isolate::Current();
1953 if (IsDeadCheck(isolate, "v8::StackTrace::CurrentStackTrace()")) {
1954 Local<StackTrace>();
1955 }
1956 ENTER_V8(isolate);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001957 i::Handle<i::JSArray> stackTrace =
Steve Block44f0eee2011-05-26 01:26:41 +01001958 isolate->CaptureCurrentStackTrace(frame_limit, options);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001959 return Utils::StackTraceToLocal(stackTrace);
Kristian Monsen25f61362010-05-21 11:50:48 +01001960}
1961
1962
1963// --- S t a c k F r a m e ---
1964
1965int StackFrame::GetLineNumber() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001966 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1967 if (IsDeadCheck(isolate, "v8::StackFrame::GetLineNumber()")) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001968 return Message::kNoLineNumberInfo;
1969 }
Steve Block44f0eee2011-05-26 01:26:41 +01001970 ENTER_V8(isolate);
1971 i::HandleScope scope(isolate);
Kristian Monsen25f61362010-05-21 11:50:48 +01001972 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
1973 i::Handle<i::Object> line = GetProperty(self, "lineNumber");
1974 if (!line->IsSmi()) {
1975 return Message::kNoLineNumberInfo;
1976 }
1977 return i::Smi::cast(*line)->value();
1978}
1979
1980
1981int StackFrame::GetColumn() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001982 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1983 if (IsDeadCheck(isolate, "v8::StackFrame::GetColumn()")) {
Kristian Monsen25f61362010-05-21 11:50:48 +01001984 return Message::kNoColumnInfo;
1985 }
Steve Block44f0eee2011-05-26 01:26:41 +01001986 ENTER_V8(isolate);
1987 i::HandleScope scope(isolate);
Kristian Monsen25f61362010-05-21 11:50:48 +01001988 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
1989 i::Handle<i::Object> column = GetProperty(self, "column");
1990 if (!column->IsSmi()) {
1991 return Message::kNoColumnInfo;
1992 }
1993 return i::Smi::cast(*column)->value();
1994}
1995
1996
1997Local<String> StackFrame::GetScriptName() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001998 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1999 if (IsDeadCheck(isolate, "v8::StackFrame::GetScriptName()")) {
2000 return Local<String>();
2001 }
2002 ENTER_V8(isolate);
Kristian Monsen25f61362010-05-21 11:50:48 +01002003 HandleScope scope;
2004 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2005 i::Handle<i::Object> name = GetProperty(self, "scriptName");
2006 if (!name->IsString()) {
2007 return Local<String>();
2008 }
2009 return scope.Close(Local<String>::Cast(Utils::ToLocal(name)));
2010}
2011
2012
Ben Murdochf87a2032010-10-22 12:50:53 +01002013Local<String> StackFrame::GetScriptNameOrSourceURL() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002014 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2015 if (IsDeadCheck(isolate, "v8::StackFrame::GetScriptNameOrSourceURL()")) {
Ben Murdochf87a2032010-10-22 12:50:53 +01002016 return Local<String>();
2017 }
Steve Block44f0eee2011-05-26 01:26:41 +01002018 ENTER_V8(isolate);
Ben Murdochf87a2032010-10-22 12:50:53 +01002019 HandleScope scope;
2020 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2021 i::Handle<i::Object> name = GetProperty(self, "scriptNameOrSourceURL");
2022 if (!name->IsString()) {
2023 return Local<String>();
2024 }
2025 return scope.Close(Local<String>::Cast(Utils::ToLocal(name)));
2026}
2027
2028
Kristian Monsen25f61362010-05-21 11:50:48 +01002029Local<String> StackFrame::GetFunctionName() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002030 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2031 if (IsDeadCheck(isolate, "v8::StackFrame::GetFunctionName()")) {
2032 return Local<String>();
2033 }
2034 ENTER_V8(isolate);
Kristian Monsen25f61362010-05-21 11:50:48 +01002035 HandleScope scope;
2036 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2037 i::Handle<i::Object> name = GetProperty(self, "functionName");
2038 if (!name->IsString()) {
2039 return Local<String>();
2040 }
2041 return scope.Close(Local<String>::Cast(Utils::ToLocal(name)));
2042}
2043
2044
2045bool StackFrame::IsEval() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002046 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2047 if (IsDeadCheck(isolate, "v8::StackFrame::IsEval()")) return false;
2048 ENTER_V8(isolate);
2049 i::HandleScope scope(isolate);
Kristian Monsen25f61362010-05-21 11:50:48 +01002050 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2051 i::Handle<i::Object> is_eval = GetProperty(self, "isEval");
2052 return is_eval->IsTrue();
2053}
2054
2055
2056bool StackFrame::IsConstructor() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002057 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2058 if (IsDeadCheck(isolate, "v8::StackFrame::IsConstructor()")) return false;
2059 ENTER_V8(isolate);
2060 i::HandleScope scope(isolate);
Kristian Monsen25f61362010-05-21 11:50:48 +01002061 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2062 i::Handle<i::Object> is_constructor = GetProperty(self, "isConstructor");
2063 return is_constructor->IsTrue();
2064}
2065
2066
Steve Blocka7e24c12009-10-30 11:49:00 +00002067// --- D a t a ---
2068
2069bool Value::IsUndefined() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002070 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsUndefined()")) {
2071 return false;
2072 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002073 return Utils::OpenHandle(this)->IsUndefined();
2074}
2075
2076
2077bool Value::IsNull() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002078 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsNull()")) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002079 return Utils::OpenHandle(this)->IsNull();
2080}
2081
2082
2083bool Value::IsTrue() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002084 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsTrue()")) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002085 return Utils::OpenHandle(this)->IsTrue();
2086}
2087
2088
2089bool Value::IsFalse() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002090 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsFalse()")) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002091 return Utils::OpenHandle(this)->IsFalse();
2092}
2093
2094
2095bool Value::IsFunction() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002096 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsFunction()")) {
2097 return false;
2098 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002099 return Utils::OpenHandle(this)->IsJSFunction();
2100}
2101
2102
2103bool Value::FullIsString() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002104 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsString()")) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002105 bool result = Utils::OpenHandle(this)->IsString();
2106 ASSERT_EQ(result, QuickIsString());
2107 return result;
2108}
2109
2110
2111bool Value::IsArray() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002112 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsArray()")) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002113 return Utils::OpenHandle(this)->IsJSArray();
2114}
2115
2116
2117bool Value::IsObject() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002118 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsObject()")) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002119 return Utils::OpenHandle(this)->IsJSObject();
2120}
2121
2122
2123bool Value::IsNumber() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002124 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsNumber()")) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002125 return Utils::OpenHandle(this)->IsNumber();
2126}
2127
2128
2129bool Value::IsBoolean() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002130 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsBoolean()")) {
2131 return false;
2132 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002133 return Utils::OpenHandle(this)->IsBoolean();
2134}
2135
2136
2137bool Value::IsExternal() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002138 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsExternal()")) {
2139 return false;
2140 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002141 return Utils::OpenHandle(this)->IsForeign();
Steve Blocka7e24c12009-10-30 11:49:00 +00002142}
2143
2144
2145bool Value::IsInt32() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002146 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsInt32()")) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002147 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2148 if (obj->IsSmi()) return true;
2149 if (obj->IsNumber()) {
2150 double value = obj->Number();
2151 return i::FastI2D(i::FastD2I(value)) == value;
2152 }
2153 return false;
2154}
2155
2156
Steve Block6ded16b2010-05-10 14:33:55 +01002157bool Value::IsUint32() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002158 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsUint32()")) return false;
Steve Block6ded16b2010-05-10 14:33:55 +01002159 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2160 if (obj->IsSmi()) return i::Smi::cast(*obj)->value() >= 0;
2161 if (obj->IsNumber()) {
2162 double value = obj->Number();
2163 return i::FastUI2D(i::FastD2UI(value)) == value;
2164 }
2165 return false;
2166}
2167
2168
Steve Blocka7e24c12009-10-30 11:49:00 +00002169bool Value::IsDate() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002170 i::Isolate* isolate = i::Isolate::Current();
2171 if (IsDeadCheck(isolate, "v8::Value::IsDate()")) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002172 i::Handle<i::Object> obj = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01002173 return obj->HasSpecificClassOf(isolate->heap()->Date_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +00002174}
2175
2176
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002177bool Value::IsStringObject() const {
2178 i::Isolate* isolate = i::Isolate::Current();
2179 if (IsDeadCheck(isolate, "v8::Value::IsStringObject()")) return false;
2180 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2181 return obj->HasSpecificClassOf(isolate->heap()->String_symbol());
2182}
2183
2184
2185bool Value::IsNumberObject() const {
2186 i::Isolate* isolate = i::Isolate::Current();
2187 if (IsDeadCheck(isolate, "v8::Value::IsNumberObject()")) return false;
2188 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2189 return obj->HasSpecificClassOf(isolate->heap()->Number_symbol());
2190}
2191
2192
2193static i::Object* LookupBuiltin(i::Isolate* isolate,
2194 const char* builtin_name) {
2195 i::Handle<i::String> symbol =
2196 isolate->factory()->LookupAsciiSymbol(builtin_name);
2197 i::Handle<i::JSBuiltinsObject> builtins = isolate->js_builtins_object();
2198 return builtins->GetPropertyNoExceptionThrown(*symbol);
2199}
2200
2201
2202static bool CheckConstructor(i::Isolate* isolate,
2203 i::Handle<i::JSObject> obj,
2204 const char* class_name) {
2205 return obj->map()->constructor() == LookupBuiltin(isolate, class_name);
2206}
2207
2208
2209bool Value::IsNativeError() const {
2210 i::Isolate* isolate = i::Isolate::Current();
2211 if (IsDeadCheck(isolate, "v8::Value::IsNativeError()")) return false;
2212 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2213 if (obj->IsJSObject()) {
2214 i::Handle<i::JSObject> js_obj(i::JSObject::cast(*obj));
2215 return CheckConstructor(isolate, js_obj, "$Error") ||
2216 CheckConstructor(isolate, js_obj, "$EvalError") ||
2217 CheckConstructor(isolate, js_obj, "$RangeError") ||
2218 CheckConstructor(isolate, js_obj, "$ReferenceError") ||
2219 CheckConstructor(isolate, js_obj, "$SyntaxError") ||
2220 CheckConstructor(isolate, js_obj, "$TypeError") ||
2221 CheckConstructor(isolate, js_obj, "$URIError");
2222 } else {
2223 return false;
2224 }
2225}
2226
2227
2228bool Value::IsBooleanObject() const {
2229 i::Isolate* isolate = i::Isolate::Current();
2230 if (IsDeadCheck(isolate, "v8::Value::IsBooleanObject()")) return false;
2231 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2232 return obj->HasSpecificClassOf(isolate->heap()->Boolean_symbol());
2233}
2234
2235
Iain Merrick75681382010-08-19 15:07:18 +01002236bool Value::IsRegExp() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002237 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsRegExp()")) return false;
Iain Merrick75681382010-08-19 15:07:18 +01002238 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2239 return obj->IsJSRegExp();
2240}
2241
2242
Steve Blocka7e24c12009-10-30 11:49:00 +00002243Local<String> Value::ToString() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002244 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2245 i::Handle<i::Object> str;
2246 if (obj->IsString()) {
2247 str = obj;
2248 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01002249 i::Isolate* isolate = i::Isolate::Current();
2250 if (IsDeadCheck(isolate, "v8::Value::ToString()")) {
2251 return Local<String>();
2252 }
2253 LOG_API(isolate, "ToString");
2254 ENTER_V8(isolate);
2255 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002256 str = i::Execution::ToString(obj, &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01002257 EXCEPTION_BAILOUT_CHECK(isolate, Local<String>());
Steve Blocka7e24c12009-10-30 11:49:00 +00002258 }
2259 return Local<String>(ToApi<String>(str));
2260}
2261
2262
2263Local<String> Value::ToDetailString() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002264 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2265 i::Handle<i::Object> str;
2266 if (obj->IsString()) {
2267 str = obj;
2268 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01002269 i::Isolate* isolate = i::Isolate::Current();
2270 if (IsDeadCheck(isolate, "v8::Value::ToDetailString()")) {
2271 return Local<String>();
2272 }
2273 LOG_API(isolate, "ToDetailString");
2274 ENTER_V8(isolate);
2275 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002276 str = i::Execution::ToDetailString(obj, &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01002277 EXCEPTION_BAILOUT_CHECK(isolate, Local<String>());
Steve Blocka7e24c12009-10-30 11:49:00 +00002278 }
2279 return Local<String>(ToApi<String>(str));
2280}
2281
2282
2283Local<v8::Object> Value::ToObject() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002284 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2285 i::Handle<i::Object> val;
2286 if (obj->IsJSObject()) {
2287 val = obj;
2288 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01002289 i::Isolate* isolate = i::Isolate::Current();
2290 if (IsDeadCheck(isolate, "v8::Value::ToObject()")) {
2291 return Local<v8::Object>();
2292 }
2293 LOG_API(isolate, "ToObject");
2294 ENTER_V8(isolate);
2295 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002296 val = i::Execution::ToObject(obj, &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01002297 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>());
Steve Blocka7e24c12009-10-30 11:49:00 +00002298 }
2299 return Local<v8::Object>(ToApi<Object>(val));
2300}
2301
2302
2303Local<Boolean> Value::ToBoolean() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002304 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2305 if (obj->IsBoolean()) {
2306 return Local<Boolean>(ToApi<Boolean>(obj));
2307 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01002308 i::Isolate* isolate = i::Isolate::Current();
2309 if (IsDeadCheck(isolate, "v8::Value::ToBoolean()")) {
2310 return Local<Boolean>();
2311 }
2312 LOG_API(isolate, "ToBoolean");
2313 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002314 i::Handle<i::Object> val = i::Execution::ToBoolean(obj);
2315 return Local<Boolean>(ToApi<Boolean>(val));
2316 }
2317}
2318
2319
2320Local<Number> Value::ToNumber() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002321 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2322 i::Handle<i::Object> num;
2323 if (obj->IsNumber()) {
2324 num = obj;
2325 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01002326 i::Isolate* isolate = i::Isolate::Current();
2327 if (IsDeadCheck(isolate, "v8::Value::ToNumber()")) {
2328 return Local<Number>();
2329 }
2330 LOG_API(isolate, "ToNumber");
2331 ENTER_V8(isolate);
2332 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002333 num = i::Execution::ToNumber(obj, &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01002334 EXCEPTION_BAILOUT_CHECK(isolate, Local<Number>());
Steve Blocka7e24c12009-10-30 11:49:00 +00002335 }
2336 return Local<Number>(ToApi<Number>(num));
2337}
2338
2339
2340Local<Integer> Value::ToInteger() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002341 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2342 i::Handle<i::Object> num;
2343 if (obj->IsSmi()) {
2344 num = obj;
2345 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01002346 i::Isolate* isolate = i::Isolate::Current();
2347 if (IsDeadCheck(isolate, "v8::Value::ToInteger()")) return Local<Integer>();
2348 LOG_API(isolate, "ToInteger");
2349 ENTER_V8(isolate);
2350 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002351 num = i::Execution::ToInteger(obj, &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01002352 EXCEPTION_BAILOUT_CHECK(isolate, Local<Integer>());
Steve Blocka7e24c12009-10-30 11:49:00 +00002353 }
2354 return Local<Integer>(ToApi<Integer>(num));
2355}
2356
2357
2358void External::CheckCast(v8::Value* that) {
Steve Block44f0eee2011-05-26 01:26:41 +01002359 if (IsDeadCheck(i::Isolate::Current(), "v8::External::Cast()")) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002360 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdoch257744e2011-11-30 15:57:28 +00002361 ApiCheck(obj->IsForeign(),
Steve Blocka7e24c12009-10-30 11:49:00 +00002362 "v8::External::Cast()",
2363 "Could not convert to external");
2364}
2365
2366
2367void v8::Object::CheckCast(Value* that) {
Steve Block44f0eee2011-05-26 01:26:41 +01002368 if (IsDeadCheck(i::Isolate::Current(), "v8::Object::Cast()")) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002369 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2370 ApiCheck(obj->IsJSObject(),
2371 "v8::Object::Cast()",
2372 "Could not convert to object");
2373}
2374
2375
2376void v8::Function::CheckCast(Value* that) {
Steve Block44f0eee2011-05-26 01:26:41 +01002377 if (IsDeadCheck(i::Isolate::Current(), "v8::Function::Cast()")) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002378 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2379 ApiCheck(obj->IsJSFunction(),
2380 "v8::Function::Cast()",
2381 "Could not convert to function");
2382}
2383
2384
2385void v8::String::CheckCast(v8::Value* that) {
Steve Block44f0eee2011-05-26 01:26:41 +01002386 if (IsDeadCheck(i::Isolate::Current(), "v8::String::Cast()")) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002387 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2388 ApiCheck(obj->IsString(),
2389 "v8::String::Cast()",
2390 "Could not convert to string");
2391}
2392
2393
2394void v8::Number::CheckCast(v8::Value* that) {
Steve Block44f0eee2011-05-26 01:26:41 +01002395 if (IsDeadCheck(i::Isolate::Current(), "v8::Number::Cast()")) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002396 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2397 ApiCheck(obj->IsNumber(),
2398 "v8::Number::Cast()",
2399 "Could not convert to number");
2400}
2401
2402
2403void v8::Integer::CheckCast(v8::Value* that) {
Steve Block44f0eee2011-05-26 01:26:41 +01002404 if (IsDeadCheck(i::Isolate::Current(), "v8::Integer::Cast()")) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002405 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2406 ApiCheck(obj->IsNumber(),
2407 "v8::Integer::Cast()",
2408 "Could not convert to number");
2409}
2410
2411
2412void v8::Array::CheckCast(Value* that) {
Steve Block44f0eee2011-05-26 01:26:41 +01002413 if (IsDeadCheck(i::Isolate::Current(), "v8::Array::Cast()")) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002414 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2415 ApiCheck(obj->IsJSArray(),
2416 "v8::Array::Cast()",
2417 "Could not convert to array");
2418}
2419
2420
2421void v8::Date::CheckCast(v8::Value* that) {
Steve Block44f0eee2011-05-26 01:26:41 +01002422 i::Isolate* isolate = i::Isolate::Current();
2423 if (IsDeadCheck(isolate, "v8::Date::Cast()")) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002424 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Steve Block44f0eee2011-05-26 01:26:41 +01002425 ApiCheck(obj->HasSpecificClassOf(isolate->heap()->Date_symbol()),
Steve Blocka7e24c12009-10-30 11:49:00 +00002426 "v8::Date::Cast()",
2427 "Could not convert to date");
2428}
2429
2430
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002431void v8::StringObject::CheckCast(v8::Value* that) {
2432 i::Isolate* isolate = i::Isolate::Current();
2433 if (IsDeadCheck(isolate, "v8::StringObject::Cast()")) return;
2434 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2435 ApiCheck(obj->HasSpecificClassOf(isolate->heap()->String_symbol()),
2436 "v8::StringObject::Cast()",
2437 "Could not convert to StringObject");
2438}
2439
2440
2441void v8::NumberObject::CheckCast(v8::Value* that) {
2442 i::Isolate* isolate = i::Isolate::Current();
2443 if (IsDeadCheck(isolate, "v8::NumberObject::Cast()")) return;
2444 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2445 ApiCheck(obj->HasSpecificClassOf(isolate->heap()->Number_symbol()),
2446 "v8::NumberObject::Cast()",
2447 "Could not convert to NumberObject");
2448}
2449
2450
2451void v8::BooleanObject::CheckCast(v8::Value* that) {
2452 i::Isolate* isolate = i::Isolate::Current();
2453 if (IsDeadCheck(isolate, "v8::BooleanObject::Cast()")) return;
2454 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2455 ApiCheck(obj->HasSpecificClassOf(isolate->heap()->Boolean_symbol()),
2456 "v8::BooleanObject::Cast()",
2457 "Could not convert to BooleanObject");
2458}
2459
2460
Ben Murdochf87a2032010-10-22 12:50:53 +01002461void v8::RegExp::CheckCast(v8::Value* that) {
Steve Block44f0eee2011-05-26 01:26:41 +01002462 if (IsDeadCheck(i::Isolate::Current(), "v8::RegExp::Cast()")) return;
Ben Murdochf87a2032010-10-22 12:50:53 +01002463 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2464 ApiCheck(obj->IsJSRegExp(),
2465 "v8::RegExp::Cast()",
2466 "Could not convert to regular expression");
2467}
2468
2469
Steve Blocka7e24c12009-10-30 11:49:00 +00002470bool Value::BooleanValue() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002471 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2472 if (obj->IsBoolean()) {
2473 return obj->IsTrue();
2474 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01002475 i::Isolate* isolate = i::Isolate::Current();
2476 if (IsDeadCheck(isolate, "v8::Value::BooleanValue()")) return false;
2477 LOG_API(isolate, "BooleanValue");
2478 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002479 i::Handle<i::Object> value = i::Execution::ToBoolean(obj);
2480 return value->IsTrue();
2481 }
2482}
2483
2484
2485double Value::NumberValue() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002486 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2487 i::Handle<i::Object> num;
2488 if (obj->IsNumber()) {
2489 num = obj;
2490 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01002491 i::Isolate* isolate = i::Isolate::Current();
2492 if (IsDeadCheck(isolate, "v8::Value::NumberValue()")) {
2493 return i::OS::nan_value();
2494 }
2495 LOG_API(isolate, "NumberValue");
2496 ENTER_V8(isolate);
2497 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002498 num = i::Execution::ToNumber(obj, &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01002499 EXCEPTION_BAILOUT_CHECK(isolate, i::OS::nan_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00002500 }
2501 return num->Number();
2502}
2503
2504
2505int64_t Value::IntegerValue() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002506 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2507 i::Handle<i::Object> num;
2508 if (obj->IsNumber()) {
2509 num = obj;
2510 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01002511 i::Isolate* isolate = i::Isolate::Current();
2512 if (IsDeadCheck(isolate, "v8::Value::IntegerValue()")) return 0;
2513 LOG_API(isolate, "IntegerValue");
2514 ENTER_V8(isolate);
2515 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002516 num = i::Execution::ToInteger(obj, &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01002517 EXCEPTION_BAILOUT_CHECK(isolate, 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002518 }
2519 if (num->IsSmi()) {
2520 return i::Smi::cast(*num)->value();
2521 } else {
2522 return static_cast<int64_t>(num->Number());
2523 }
2524}
2525
2526
2527Local<Int32> Value::ToInt32() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002528 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2529 i::Handle<i::Object> num;
2530 if (obj->IsSmi()) {
2531 num = obj;
2532 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01002533 i::Isolate* isolate = i::Isolate::Current();
2534 if (IsDeadCheck(isolate, "v8::Value::ToInt32()")) return Local<Int32>();
2535 LOG_API(isolate, "ToInt32");
2536 ENTER_V8(isolate);
2537 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002538 num = i::Execution::ToInt32(obj, &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01002539 EXCEPTION_BAILOUT_CHECK(isolate, Local<Int32>());
Steve Blocka7e24c12009-10-30 11:49:00 +00002540 }
2541 return Local<Int32>(ToApi<Int32>(num));
2542}
2543
2544
2545Local<Uint32> Value::ToUint32() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002546 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2547 i::Handle<i::Object> num;
2548 if (obj->IsSmi()) {
2549 num = obj;
2550 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01002551 i::Isolate* isolate = i::Isolate::Current();
2552 if (IsDeadCheck(isolate, "v8::Value::ToUint32()")) return Local<Uint32>();
2553 LOG_API(isolate, "ToUInt32");
2554 ENTER_V8(isolate);
2555 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002556 num = i::Execution::ToUint32(obj, &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01002557 EXCEPTION_BAILOUT_CHECK(isolate, Local<Uint32>());
Steve Blocka7e24c12009-10-30 11:49:00 +00002558 }
2559 return Local<Uint32>(ToApi<Uint32>(num));
2560}
2561
2562
2563Local<Uint32> Value::ToArrayIndex() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002564 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2565 if (obj->IsSmi()) {
2566 if (i::Smi::cast(*obj)->value() >= 0) return Utils::Uint32ToLocal(obj);
2567 return Local<Uint32>();
2568 }
Steve Block44f0eee2011-05-26 01:26:41 +01002569 i::Isolate* isolate = i::Isolate::Current();
2570 if (IsDeadCheck(isolate, "v8::Value::ToArrayIndex()")) return Local<Uint32>();
2571 LOG_API(isolate, "ToArrayIndex");
2572 ENTER_V8(isolate);
2573 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002574 i::Handle<i::Object> string_obj =
2575 i::Execution::ToString(obj, &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01002576 EXCEPTION_BAILOUT_CHECK(isolate, Local<Uint32>());
Steve Blocka7e24c12009-10-30 11:49:00 +00002577 i::Handle<i::String> str = i::Handle<i::String>::cast(string_obj);
2578 uint32_t index;
2579 if (str->AsArrayIndex(&index)) {
2580 i::Handle<i::Object> value;
2581 if (index <= static_cast<uint32_t>(i::Smi::kMaxValue)) {
2582 value = i::Handle<i::Object>(i::Smi::FromInt(index));
2583 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01002584 value = isolate->factory()->NewNumber(index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002585 }
2586 return Utils::Uint32ToLocal(value);
2587 }
2588 return Local<Uint32>();
2589}
2590
2591
2592int32_t Value::Int32Value() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002593 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2594 if (obj->IsSmi()) {
2595 return i::Smi::cast(*obj)->value();
2596 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01002597 i::Isolate* isolate = i::Isolate::Current();
2598 if (IsDeadCheck(isolate, "v8::Value::Int32Value()")) return 0;
2599 LOG_API(isolate, "Int32Value (slow)");
2600 ENTER_V8(isolate);
2601 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002602 i::Handle<i::Object> num =
2603 i::Execution::ToInt32(obj, &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01002604 EXCEPTION_BAILOUT_CHECK(isolate, 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002605 if (num->IsSmi()) {
2606 return i::Smi::cast(*num)->value();
2607 } else {
2608 return static_cast<int32_t>(num->Number());
2609 }
2610 }
2611}
2612
2613
2614bool Value::Equals(Handle<Value> that) const {
Steve Block44f0eee2011-05-26 01:26:41 +01002615 i::Isolate* isolate = i::Isolate::Current();
2616 if (IsDeadCheck(isolate, "v8::Value::Equals()")
Steve Blocka7e24c12009-10-30 11:49:00 +00002617 || EmptyCheck("v8::Value::Equals()", this)
2618 || EmptyCheck("v8::Value::Equals()", that)) {
2619 return false;
2620 }
Steve Block44f0eee2011-05-26 01:26:41 +01002621 LOG_API(isolate, "Equals");
2622 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002623 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2624 i::Handle<i::Object> other = Utils::OpenHandle(*that);
Steve Block1e0659c2011-05-24 12:43:12 +01002625 // If both obj and other are JSObjects, we'd better compare by identity
2626 // immediately when going into JS builtin. The reason is Invoke
2627 // would overwrite global object receiver with global proxy.
2628 if (obj->IsJSObject() && other->IsJSObject()) {
2629 return *obj == *other;
2630 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002631 i::Object** args[1] = { other.location() };
Steve Block44f0eee2011-05-26 01:26:41 +01002632 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002633 i::Handle<i::Object> result =
2634 CallV8HeapFunction("EQUALS", obj, 1, args, &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01002635 EXCEPTION_BAILOUT_CHECK(isolate, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00002636 return *result == i::Smi::FromInt(i::EQUAL);
2637}
2638
2639
2640bool Value::StrictEquals(Handle<Value> that) const {
Steve Block44f0eee2011-05-26 01:26:41 +01002641 i::Isolate* isolate = i::Isolate::Current();
2642 if (IsDeadCheck(isolate, "v8::Value::StrictEquals()")
Steve Blocka7e24c12009-10-30 11:49:00 +00002643 || EmptyCheck("v8::Value::StrictEquals()", this)
2644 || EmptyCheck("v8::Value::StrictEquals()", that)) {
2645 return false;
2646 }
Steve Block44f0eee2011-05-26 01:26:41 +01002647 LOG_API(isolate, "StrictEquals");
Steve Blocka7e24c12009-10-30 11:49:00 +00002648 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2649 i::Handle<i::Object> other = Utils::OpenHandle(*that);
2650 // Must check HeapNumber first, since NaN !== NaN.
2651 if (obj->IsHeapNumber()) {
2652 if (!other->IsNumber()) return false;
2653 double x = obj->Number();
2654 double y = other->Number();
2655 // Must check explicitly for NaN:s on Windows, but -0 works fine.
2656 return x == y && !isnan(x) && !isnan(y);
2657 } else if (*obj == *other) { // Also covers Booleans.
2658 return true;
2659 } else if (obj->IsSmi()) {
2660 return other->IsNumber() && obj->Number() == other->Number();
2661 } else if (obj->IsString()) {
2662 return other->IsString() &&
2663 i::String::cast(*obj)->Equals(i::String::cast(*other));
2664 } else if (obj->IsUndefined() || obj->IsUndetectableObject()) {
2665 return other->IsUndefined() || other->IsUndetectableObject();
2666 } else {
2667 return false;
2668 }
2669}
2670
2671
2672uint32_t Value::Uint32Value() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002673 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2674 if (obj->IsSmi()) {
2675 return i::Smi::cast(*obj)->value();
2676 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01002677 i::Isolate* isolate = i::Isolate::Current();
2678 if (IsDeadCheck(isolate, "v8::Value::Uint32Value()")) return 0;
2679 LOG_API(isolate, "Uint32Value");
2680 ENTER_V8(isolate);
2681 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002682 i::Handle<i::Object> num =
2683 i::Execution::ToUint32(obj, &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01002684 EXCEPTION_BAILOUT_CHECK(isolate, 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002685 if (num->IsSmi()) {
2686 return i::Smi::cast(*num)->value();
2687 } else {
2688 return static_cast<uint32_t>(num->Number());
2689 }
2690 }
2691}
2692
2693
2694bool v8::Object::Set(v8::Handle<Value> key, v8::Handle<Value> value,
2695 v8::PropertyAttribute attribs) {
Steve Block44f0eee2011-05-26 01:26:41 +01002696 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2697 ON_BAILOUT(isolate, "v8::Object::Set()", return false);
2698 ENTER_V8(isolate);
2699 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002700 i::Handle<i::Object> self = Utils::OpenHandle(this);
2701 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2702 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
Steve Block44f0eee2011-05-26 01:26:41 +01002703 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002704 i::Handle<i::Object> obj = i::SetProperty(
2705 self,
2706 key_obj,
2707 value_obj,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002708 static_cast<PropertyAttributes>(attribs),
2709 i::kNonStrictMode);
Steve Blocka7e24c12009-10-30 11:49:00 +00002710 has_pending_exception = obj.is_null();
Steve Block44f0eee2011-05-26 01:26:41 +01002711 EXCEPTION_BAILOUT_CHECK(isolate, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00002712 return true;
2713}
2714
2715
Steve Block6ded16b2010-05-10 14:33:55 +01002716bool v8::Object::Set(uint32_t index, v8::Handle<Value> value) {
Steve Block44f0eee2011-05-26 01:26:41 +01002717 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2718 ON_BAILOUT(isolate, "v8::Object::Set()", return false);
2719 ENTER_V8(isolate);
2720 i::HandleScope scope(isolate);
Steve Block6ded16b2010-05-10 14:33:55 +01002721 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2722 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
Steve Block44f0eee2011-05-26 01:26:41 +01002723 EXCEPTION_PREAMBLE(isolate);
Steve Block6ded16b2010-05-10 14:33:55 +01002724 i::Handle<i::Object> obj = i::SetElement(
2725 self,
2726 index,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002727 value_obj,
2728 i::kNonStrictMode);
Steve Block6ded16b2010-05-10 14:33:55 +01002729 has_pending_exception = obj.is_null();
Steve Block44f0eee2011-05-26 01:26:41 +01002730 EXCEPTION_BAILOUT_CHECK(isolate, false);
Steve Block6ded16b2010-05-10 14:33:55 +01002731 return true;
2732}
2733
2734
Steve Blocka7e24c12009-10-30 11:49:00 +00002735bool v8::Object::ForceSet(v8::Handle<Value> key,
2736 v8::Handle<Value> value,
2737 v8::PropertyAttribute attribs) {
Steve Block44f0eee2011-05-26 01:26:41 +01002738 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2739 ON_BAILOUT(isolate, "v8::Object::ForceSet()", return false);
2740 ENTER_V8(isolate);
2741 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002742 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2743 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2744 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
Steve Block44f0eee2011-05-26 01:26:41 +01002745 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002746 i::Handle<i::Object> obj = i::ForceSetProperty(
2747 self,
2748 key_obj,
2749 value_obj,
2750 static_cast<PropertyAttributes>(attribs));
2751 has_pending_exception = obj.is_null();
Steve Block44f0eee2011-05-26 01:26:41 +01002752 EXCEPTION_BAILOUT_CHECK(isolate, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00002753 return true;
2754}
2755
2756
2757bool v8::Object::ForceDelete(v8::Handle<Value> key) {
Steve Block44f0eee2011-05-26 01:26:41 +01002758 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2759 ON_BAILOUT(isolate, "v8::Object::ForceDelete()", return false);
2760 ENTER_V8(isolate);
2761 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002762 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2763 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002764
2765 // When turning on access checks for a global object deoptimize all functions
2766 // as optimized code does not always handle access checks.
2767 i::Deoptimizer::DeoptimizeGlobalObject(*self);
2768
Steve Block44f0eee2011-05-26 01:26:41 +01002769 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002770 i::Handle<i::Object> obj = i::ForceDeleteProperty(self, key_obj);
2771 has_pending_exception = obj.is_null();
Steve Block44f0eee2011-05-26 01:26:41 +01002772 EXCEPTION_BAILOUT_CHECK(isolate, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00002773 return obj->IsTrue();
2774}
2775
2776
2777Local<Value> v8::Object::Get(v8::Handle<Value> key) {
Steve Block44f0eee2011-05-26 01:26:41 +01002778 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2779 ON_BAILOUT(isolate, "v8::Object::Get()", return Local<v8::Value>());
2780 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002781 i::Handle<i::Object> self = Utils::OpenHandle(this);
2782 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
Steve Block44f0eee2011-05-26 01:26:41 +01002783 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002784 i::Handle<i::Object> result = i::GetProperty(self, key_obj);
2785 has_pending_exception = result.is_null();
Steve Block44f0eee2011-05-26 01:26:41 +01002786 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +00002787 return Utils::ToLocal(result);
2788}
2789
2790
Steve Block6ded16b2010-05-10 14:33:55 +01002791Local<Value> v8::Object::Get(uint32_t index) {
Steve Block44f0eee2011-05-26 01:26:41 +01002792 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2793 ON_BAILOUT(isolate, "v8::Object::Get()", return Local<v8::Value>());
2794 ENTER_V8(isolate);
Steve Block6ded16b2010-05-10 14:33:55 +01002795 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01002796 EXCEPTION_PREAMBLE(isolate);
Steve Block6ded16b2010-05-10 14:33:55 +01002797 i::Handle<i::Object> result = i::GetElement(self, index);
2798 has_pending_exception = result.is_null();
Steve Block44f0eee2011-05-26 01:26:41 +01002799 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
Steve Block6ded16b2010-05-10 14:33:55 +01002800 return Utils::ToLocal(result);
2801}
2802
2803
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002804PropertyAttribute v8::Object::GetPropertyAttributes(v8::Handle<Value> key) {
2805 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2806 ON_BAILOUT(isolate, "v8::Object::GetPropertyAttribute()",
2807 return static_cast<PropertyAttribute>(NONE));
2808 ENTER_V8(isolate);
2809 i::HandleScope scope(isolate);
2810 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2811 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2812 if (!key_obj->IsString()) {
2813 EXCEPTION_PREAMBLE(isolate);
2814 key_obj = i::Execution::ToString(key_obj, &has_pending_exception);
2815 EXCEPTION_BAILOUT_CHECK(isolate, static_cast<PropertyAttribute>(NONE));
2816 }
2817 i::Handle<i::String> key_string = i::Handle<i::String>::cast(key_obj);
2818 PropertyAttributes result = self->GetPropertyAttribute(*key_string);
2819 if (result == ABSENT) return static_cast<PropertyAttribute>(NONE);
2820 return static_cast<PropertyAttribute>(result);
2821}
2822
2823
Steve Blocka7e24c12009-10-30 11:49:00 +00002824Local<Value> v8::Object::GetPrototype() {
Steve Block44f0eee2011-05-26 01:26:41 +01002825 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2826 ON_BAILOUT(isolate, "v8::Object::GetPrototype()",
2827 return Local<v8::Value>());
2828 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002829 i::Handle<i::Object> self = Utils::OpenHandle(this);
2830 i::Handle<i::Object> result = i::GetPrototype(self);
2831 return Utils::ToLocal(result);
2832}
2833
2834
Andrei Popescu402d9372010-02-26 13:31:12 +00002835bool v8::Object::SetPrototype(Handle<Value> value) {
Steve Block44f0eee2011-05-26 01:26:41 +01002836 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2837 ON_BAILOUT(isolate, "v8::Object::SetPrototype()", return false);
2838 ENTER_V8(isolate);
Andrei Popescu402d9372010-02-26 13:31:12 +00002839 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2840 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002841 // We do not allow exceptions thrown while setting the prototype
2842 // to propagate outside.
2843 TryCatch try_catch;
Steve Block44f0eee2011-05-26 01:26:41 +01002844 EXCEPTION_PREAMBLE(isolate);
Andrei Popescu402d9372010-02-26 13:31:12 +00002845 i::Handle<i::Object> result = i::SetPrototype(self, value_obj);
2846 has_pending_exception = result.is_null();
Steve Block44f0eee2011-05-26 01:26:41 +01002847 EXCEPTION_BAILOUT_CHECK(isolate, false);
Andrei Popescu402d9372010-02-26 13:31:12 +00002848 return true;
2849}
2850
2851
Steve Blocka7e24c12009-10-30 11:49:00 +00002852Local<Object> v8::Object::FindInstanceInPrototypeChain(
2853 v8::Handle<FunctionTemplate> tmpl) {
Steve Block44f0eee2011-05-26 01:26:41 +01002854 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2855 ON_BAILOUT(isolate,
2856 "v8::Object::FindInstanceInPrototypeChain()",
Steve Blocka7e24c12009-10-30 11:49:00 +00002857 return Local<v8::Object>());
Steve Block44f0eee2011-05-26 01:26:41 +01002858 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002859 i::JSObject* object = *Utils::OpenHandle(this);
2860 i::FunctionTemplateInfo* tmpl_info = *Utils::OpenHandle(*tmpl);
2861 while (!object->IsInstanceOf(tmpl_info)) {
2862 i::Object* prototype = object->GetPrototype();
2863 if (!prototype->IsJSObject()) return Local<Object>();
2864 object = i::JSObject::cast(prototype);
2865 }
2866 return Utils::ToLocal(i::Handle<i::JSObject>(object));
2867}
2868
2869
2870Local<Array> v8::Object::GetPropertyNames() {
Steve Block44f0eee2011-05-26 01:26:41 +01002871 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2872 ON_BAILOUT(isolate, "v8::Object::GetPropertyNames()",
2873 return Local<v8::Array>());
2874 ENTER_V8(isolate);
2875 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002876 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2877 i::Handle<i::FixedArray> value =
2878 i::GetKeysInFixedArrayFor(self, i::INCLUDE_PROTOS);
2879 // Because we use caching to speed up enumeration it is important
2880 // to never change the result of the basic enumeration function so
2881 // we clone the result.
Steve Block44f0eee2011-05-26 01:26:41 +01002882 i::Handle<i::FixedArray> elms = isolate->factory()->CopyFixedArray(value);
2883 i::Handle<i::JSArray> result =
2884 isolate->factory()->NewJSArrayWithElements(elms);
2885 return Utils::ToLocal(scope.CloseAndEscape(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00002886}
2887
2888
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002889Local<Array> v8::Object::GetOwnPropertyNames() {
2890 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2891 ON_BAILOUT(isolate, "v8::Object::GetOwnPropertyNames()",
2892 return Local<v8::Array>());
2893 ENTER_V8(isolate);
2894 i::HandleScope scope(isolate);
2895 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2896 i::Handle<i::FixedArray> value =
2897 i::GetKeysInFixedArrayFor(self, i::LOCAL_ONLY);
2898 // Because we use caching to speed up enumeration it is important
2899 // to never change the result of the basic enumeration function so
2900 // we clone the result.
2901 i::Handle<i::FixedArray> elms = isolate->factory()->CopyFixedArray(value);
2902 i::Handle<i::JSArray> result =
2903 isolate->factory()->NewJSArrayWithElements(elms);
2904 return Utils::ToLocal(scope.CloseAndEscape(result));
2905}
2906
2907
Steve Blocka7e24c12009-10-30 11:49:00 +00002908Local<String> v8::Object::ObjectProtoToString() {
Steve Block44f0eee2011-05-26 01:26:41 +01002909 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2910 ON_BAILOUT(isolate, "v8::Object::ObjectProtoToString()",
2911 return Local<v8::String>());
2912 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002913 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2914
2915 i::Handle<i::Object> name(self->class_name());
2916
2917 // Native implementation of Object.prototype.toString (v8natives.js):
2918 // var c = %ClassOf(this);
2919 // if (c === 'Arguments') c = 'Object';
2920 // return "[object " + c + "]";
2921
2922 if (!name->IsString()) {
2923 return v8::String::New("[object ]");
2924
2925 } else {
2926 i::Handle<i::String> class_name = i::Handle<i::String>::cast(name);
2927 if (class_name->IsEqualTo(i::CStrVector("Arguments"))) {
2928 return v8::String::New("[object Object]");
2929
2930 } else {
2931 const char* prefix = "[object ";
2932 Local<String> str = Utils::ToLocal(class_name);
2933 const char* postfix = "]";
2934
Steve Blockd0582a62009-12-15 09:54:21 +00002935 int prefix_len = i::StrLength(prefix);
2936 int str_len = str->Length();
2937 int postfix_len = i::StrLength(postfix);
Steve Blocka7e24c12009-10-30 11:49:00 +00002938
Steve Blockd0582a62009-12-15 09:54:21 +00002939 int buf_len = prefix_len + str_len + postfix_len;
Kristian Monsen25f61362010-05-21 11:50:48 +01002940 i::ScopedVector<char> buf(buf_len);
Steve Blocka7e24c12009-10-30 11:49:00 +00002941
2942 // Write prefix.
Kristian Monsen25f61362010-05-21 11:50:48 +01002943 char* ptr = buf.start();
Steve Blocka7e24c12009-10-30 11:49:00 +00002944 memcpy(ptr, prefix, prefix_len * v8::internal::kCharSize);
2945 ptr += prefix_len;
2946
2947 // Write real content.
2948 str->WriteAscii(ptr, 0, str_len);
2949 ptr += str_len;
2950
2951 // Write postfix.
2952 memcpy(ptr, postfix, postfix_len * v8::internal::kCharSize);
2953
2954 // Copy the buffer into a heap-allocated string and return it.
Kristian Monsen25f61362010-05-21 11:50:48 +01002955 Local<String> result = v8::String::New(buf.start(), buf_len);
Steve Blocka7e24c12009-10-30 11:49:00 +00002956 return result;
2957 }
2958 }
2959}
2960
2961
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002962Local<String> v8::Object::GetConstructorName() {
Steve Block44f0eee2011-05-26 01:26:41 +01002963 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2964 ON_BAILOUT(isolate, "v8::Object::GetConstructorName()",
2965 return Local<v8::String>());
2966 ENTER_V8(isolate);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002967 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2968 i::Handle<i::String> name(self->constructor_name());
2969 return Utils::ToLocal(name);
2970}
2971
2972
Steve Blocka7e24c12009-10-30 11:49:00 +00002973bool v8::Object::Delete(v8::Handle<String> key) {
Steve Block44f0eee2011-05-26 01:26:41 +01002974 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2975 ON_BAILOUT(isolate, "v8::Object::Delete()", return false);
2976 ENTER_V8(isolate);
2977 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002978 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2979 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
2980 return i::DeleteProperty(self, key_obj)->IsTrue();
2981}
2982
2983
2984bool v8::Object::Has(v8::Handle<String> key) {
Steve Block44f0eee2011-05-26 01:26:41 +01002985 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2986 ON_BAILOUT(isolate, "v8::Object::Has()", return false);
2987 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002988 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2989 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
2990 return self->HasProperty(*key_obj);
2991}
2992
2993
2994bool v8::Object::Delete(uint32_t index) {
Steve Block44f0eee2011-05-26 01:26:41 +01002995 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2996 ON_BAILOUT(isolate, "v8::Object::DeleteProperty()",
2997 return false);
2998 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002999 HandleScope scope;
3000 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3001 return i::DeleteElement(self, index)->IsTrue();
3002}
3003
3004
3005bool v8::Object::Has(uint32_t index) {
Steve Block44f0eee2011-05-26 01:26:41 +01003006 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3007 ON_BAILOUT(isolate, "v8::Object::HasProperty()", return false);
Steve Blocka7e24c12009-10-30 11:49:00 +00003008 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3009 return self->HasElement(index);
3010}
3011
3012
Leon Clarkef7060e22010-06-03 12:02:55 +01003013bool Object::SetAccessor(Handle<String> name,
3014 AccessorGetter getter,
3015 AccessorSetter setter,
3016 v8::Handle<Value> data,
3017 AccessControl settings,
3018 PropertyAttribute attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +01003019 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3020 ON_BAILOUT(isolate, "v8::Object::SetAccessor()", return false);
3021 ENTER_V8(isolate);
3022 i::HandleScope scope(isolate);
Leon Clarkef7060e22010-06-03 12:02:55 +01003023 i::Handle<i::AccessorInfo> info = MakeAccessorInfo(name,
3024 getter, setter, data,
3025 settings, attributes);
3026 i::Handle<i::Object> result = i::SetAccessor(Utils::OpenHandle(this), info);
3027 return !result.is_null() && !result->IsUndefined();
3028}
3029
3030
Ben Murdoch257744e2011-11-30 15:57:28 +00003031bool v8::Object::HasOwnProperty(Handle<String> key) {
3032 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3033 ON_BAILOUT(isolate, "v8::Object::HasOwnProperty()",
3034 return false);
3035 return Utils::OpenHandle(this)->HasLocalProperty(
3036 *Utils::OpenHandle(*key));
3037}
3038
3039
Steve Blocka7e24c12009-10-30 11:49:00 +00003040bool v8::Object::HasRealNamedProperty(Handle<String> key) {
Steve Block44f0eee2011-05-26 01:26:41 +01003041 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3042 ON_BAILOUT(isolate, "v8::Object::HasRealNamedProperty()",
3043 return false);
Steve Blocka7e24c12009-10-30 11:49:00 +00003044 return Utils::OpenHandle(this)->HasRealNamedProperty(
3045 *Utils::OpenHandle(*key));
3046}
3047
3048
3049bool v8::Object::HasRealIndexedProperty(uint32_t index) {
Steve Block44f0eee2011-05-26 01:26:41 +01003050 ON_BAILOUT(Utils::OpenHandle(this)->GetIsolate(),
3051 "v8::Object::HasRealIndexedProperty()",
3052 return false);
Steve Blocka7e24c12009-10-30 11:49:00 +00003053 return Utils::OpenHandle(this)->HasRealElementProperty(index);
3054}
3055
3056
3057bool v8::Object::HasRealNamedCallbackProperty(Handle<String> key) {
Steve Block44f0eee2011-05-26 01:26:41 +01003058 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3059 ON_BAILOUT(isolate,
3060 "v8::Object::HasRealNamedCallbackProperty()",
3061 return false);
3062 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003063 return Utils::OpenHandle(this)->HasRealNamedCallbackProperty(
3064 *Utils::OpenHandle(*key));
3065}
3066
3067
3068bool v8::Object::HasNamedLookupInterceptor() {
Steve Block44f0eee2011-05-26 01:26:41 +01003069 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3070 ON_BAILOUT(isolate, "v8::Object::HasNamedLookupInterceptor()",
3071 return false);
Steve Blocka7e24c12009-10-30 11:49:00 +00003072 return Utils::OpenHandle(this)->HasNamedInterceptor();
3073}
3074
3075
3076bool v8::Object::HasIndexedLookupInterceptor() {
Steve Block44f0eee2011-05-26 01:26:41 +01003077 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3078 ON_BAILOUT(isolate, "v8::Object::HasIndexedLookupInterceptor()",
3079 return false);
Steve Blocka7e24c12009-10-30 11:49:00 +00003080 return Utils::OpenHandle(this)->HasIndexedInterceptor();
3081}
3082
3083
Ben Murdoch8b112d22011-06-08 16:22:53 +01003084static Local<Value> GetPropertyByLookup(i::Isolate* isolate,
3085 i::Handle<i::JSObject> receiver,
3086 i::Handle<i::String> name,
3087 i::LookupResult* lookup) {
3088 if (!lookup->IsProperty()) {
3089 // No real property was found.
3090 return Local<Value>();
3091 }
3092
3093 // If the property being looked up is a callback, it can throw
3094 // an exception.
3095 EXCEPTION_PREAMBLE(isolate);
3096 i::Handle<i::Object> result = i::GetProperty(receiver, name, lookup);
3097 has_pending_exception = result.is_null();
3098 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
3099
3100 return Utils::ToLocal(result);
3101}
3102
3103
Steve Blocka7e24c12009-10-30 11:49:00 +00003104Local<Value> v8::Object::GetRealNamedPropertyInPrototypeChain(
3105 Handle<String> key) {
Steve Block44f0eee2011-05-26 01:26:41 +01003106 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3107 ON_BAILOUT(isolate,
3108 "v8::Object::GetRealNamedPropertyInPrototypeChain()",
Steve Blocka7e24c12009-10-30 11:49:00 +00003109 return Local<Value>());
Steve Block44f0eee2011-05-26 01:26:41 +01003110 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003111 i::Handle<i::JSObject> self_obj = Utils::OpenHandle(this);
3112 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
3113 i::LookupResult lookup;
3114 self_obj->LookupRealNamedPropertyInPrototypes(*key_obj, &lookup);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003115 return GetPropertyByLookup(isolate, self_obj, key_obj, &lookup);
Steve Blocka7e24c12009-10-30 11:49:00 +00003116}
3117
3118
3119Local<Value> v8::Object::GetRealNamedProperty(Handle<String> key) {
Steve Block44f0eee2011-05-26 01:26:41 +01003120 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3121 ON_BAILOUT(isolate, "v8::Object::GetRealNamedProperty()",
3122 return Local<Value>());
3123 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003124 i::Handle<i::JSObject> self_obj = Utils::OpenHandle(this);
3125 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
3126 i::LookupResult lookup;
3127 self_obj->LookupRealNamedProperty(*key_obj, &lookup);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003128 return GetPropertyByLookup(isolate, self_obj, key_obj, &lookup);
Steve Blocka7e24c12009-10-30 11:49:00 +00003129}
3130
3131
3132// Turns on access checks by copying the map and setting the check flag.
3133// Because the object gets a new map, existing inline cache caching
3134// the old map of this object will fail.
3135void v8::Object::TurnOnAccessCheck() {
Steve Block44f0eee2011-05-26 01:26:41 +01003136 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3137 ON_BAILOUT(isolate, "v8::Object::TurnOnAccessCheck()", return);
3138 ENTER_V8(isolate);
3139 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003140 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3141
Ben Murdochb0fe1622011-05-05 13:52:32 +01003142 // When turning on access checks for a global object deoptimize all functions
3143 // as optimized code does not always handle access checks.
3144 i::Deoptimizer::DeoptimizeGlobalObject(*obj);
3145
Steve Blocka7e24c12009-10-30 11:49:00 +00003146 i::Handle<i::Map> new_map =
Steve Block44f0eee2011-05-26 01:26:41 +01003147 isolate->factory()->CopyMapDropTransitions(i::Handle<i::Map>(obj->map()));
Steve Blocka7e24c12009-10-30 11:49:00 +00003148 new_map->set_is_access_check_needed(true);
3149 obj->set_map(*new_map);
3150}
3151
3152
3153bool v8::Object::IsDirty() {
3154 return Utils::OpenHandle(this)->IsDirty();
3155}
3156
3157
3158Local<v8::Object> v8::Object::Clone() {
Steve Block44f0eee2011-05-26 01:26:41 +01003159 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3160 ON_BAILOUT(isolate, "v8::Object::Clone()", return Local<Object>());
3161 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003162 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003163 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003164 i::Handle<i::JSObject> result = i::Copy(self);
3165 has_pending_exception = result.is_null();
Steve Block44f0eee2011-05-26 01:26:41 +01003166 EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>());
Steve Blocka7e24c12009-10-30 11:49:00 +00003167 return Utils::ToLocal(result);
3168}
3169
3170
Ben Murdoch8b112d22011-06-08 16:22:53 +01003171static i::Context* GetCreationContext(i::JSObject* object) {
3172 i::Object* constructor = object->map()->constructor();
3173 i::JSFunction* function;
3174 if (!constructor->IsJSFunction()) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003175 // Functions have null as a constructor,
Ben Murdoch8b112d22011-06-08 16:22:53 +01003176 // but any JSFunction knows its context immediately.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003177 ASSERT(object->IsJSFunction());
Ben Murdoch8b112d22011-06-08 16:22:53 +01003178 function = i::JSFunction::cast(object);
3179 } else {
3180 function = i::JSFunction::cast(constructor);
3181 }
3182 return function->context()->global_context();
3183}
3184
3185
3186Local<v8::Context> v8::Object::CreationContext() {
3187 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3188 ON_BAILOUT(isolate,
3189 "v8::Object::CreationContext()", return Local<v8::Context>());
3190 ENTER_V8(isolate);
3191 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3192 i::Context* context = GetCreationContext(*self);
3193 return Utils::ToLocal(i::Handle<i::Context>(context));
3194}
3195
3196
Steve Blocka7e24c12009-10-30 11:49:00 +00003197int v8::Object::GetIdentityHash() {
Steve Block44f0eee2011-05-26 01:26:41 +01003198 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3199 ON_BAILOUT(isolate, "v8::Object::GetIdentityHash()", return 0);
3200 ENTER_V8(isolate);
3201 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003202 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003203 return i::GetIdentityHash(self);
Steve Blocka7e24c12009-10-30 11:49:00 +00003204}
3205
3206
3207bool v8::Object::SetHiddenValue(v8::Handle<v8::String> key,
3208 v8::Handle<v8::Value> value) {
Steve Block44f0eee2011-05-26 01:26:41 +01003209 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3210 ON_BAILOUT(isolate, "v8::Object::SetHiddenValue()", return false);
3211 ENTER_V8(isolate);
3212 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003213 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003214 i::Handle<i::Object> hidden_props(i::GetHiddenProperties(
3215 self,
3216 i::JSObject::ALLOW_CREATION));
Steve Blocka7e24c12009-10-30 11:49:00 +00003217 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
3218 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
Steve Block44f0eee2011-05-26 01:26:41 +01003219 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003220 i::Handle<i::Object> obj = i::SetProperty(
3221 hidden_props,
3222 key_obj,
3223 value_obj,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003224 static_cast<PropertyAttributes>(None),
3225 i::kNonStrictMode);
Steve Blocka7e24c12009-10-30 11:49:00 +00003226 has_pending_exception = obj.is_null();
Steve Block44f0eee2011-05-26 01:26:41 +01003227 EXCEPTION_BAILOUT_CHECK(isolate, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00003228 return true;
3229}
3230
3231
3232v8::Local<v8::Value> v8::Object::GetHiddenValue(v8::Handle<v8::String> key) {
Steve Block44f0eee2011-05-26 01:26:41 +01003233 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3234 ON_BAILOUT(isolate, "v8::Object::GetHiddenValue()",
3235 return Local<v8::Value>());
3236 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003237 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003238 i::Handle<i::Object> hidden_props(i::GetHiddenProperties(
3239 self,
3240 i::JSObject::OMIT_CREATION));
Steve Blocka7e24c12009-10-30 11:49:00 +00003241 if (hidden_props->IsUndefined()) {
3242 return v8::Local<v8::Value>();
3243 }
3244 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
Steve Block44f0eee2011-05-26 01:26:41 +01003245 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003246 i::Handle<i::Object> result = i::GetProperty(hidden_props, key_obj);
3247 has_pending_exception = result.is_null();
Steve Block44f0eee2011-05-26 01:26:41 +01003248 EXCEPTION_BAILOUT_CHECK(isolate, v8::Local<v8::Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +00003249 if (result->IsUndefined()) {
3250 return v8::Local<v8::Value>();
3251 }
3252 return Utils::ToLocal(result);
3253}
3254
3255
3256bool v8::Object::DeleteHiddenValue(v8::Handle<v8::String> key) {
Steve Block44f0eee2011-05-26 01:26:41 +01003257 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3258 ON_BAILOUT(isolate, "v8::DeleteHiddenValue()", return false);
3259 ENTER_V8(isolate);
3260 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003261 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003262 i::Handle<i::Object> hidden_props(i::GetHiddenProperties(
3263 self,
3264 i::JSObject::OMIT_CREATION));
Steve Blocka7e24c12009-10-30 11:49:00 +00003265 if (hidden_props->IsUndefined()) {
3266 return true;
3267 }
3268 i::Handle<i::JSObject> js_obj(i::JSObject::cast(*hidden_props));
3269 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
3270 return i::DeleteProperty(js_obj, key_obj)->IsTrue();
3271}
3272
3273
Steve Block44f0eee2011-05-26 01:26:41 +01003274namespace {
3275
Ben Murdoch589d6972011-11-30 16:04:58 +00003276static i::ElementsKind GetElementsKindFromExternalArrayType(
3277 ExternalArrayType array_type) {
3278 switch (array_type) {
3279 case kExternalByteArray:
3280 return i::EXTERNAL_BYTE_ELEMENTS;
3281 break;
3282 case kExternalUnsignedByteArray:
3283 return i::EXTERNAL_UNSIGNED_BYTE_ELEMENTS;
3284 break;
3285 case kExternalShortArray:
3286 return i::EXTERNAL_SHORT_ELEMENTS;
3287 break;
3288 case kExternalUnsignedShortArray:
3289 return i::EXTERNAL_UNSIGNED_SHORT_ELEMENTS;
3290 break;
3291 case kExternalIntArray:
3292 return i::EXTERNAL_INT_ELEMENTS;
3293 break;
3294 case kExternalUnsignedIntArray:
3295 return i::EXTERNAL_UNSIGNED_INT_ELEMENTS;
3296 break;
3297 case kExternalFloatArray:
3298 return i::EXTERNAL_FLOAT_ELEMENTS;
3299 break;
3300 case kExternalDoubleArray:
3301 return i::EXTERNAL_DOUBLE_ELEMENTS;
3302 break;
3303 case kExternalPixelArray:
3304 return i::EXTERNAL_PIXEL_ELEMENTS;
3305 break;
3306 }
3307 UNREACHABLE();
3308 return i::DICTIONARY_ELEMENTS;
3309}
3310
3311
Steve Block44f0eee2011-05-26 01:26:41 +01003312void PrepareExternalArrayElements(i::Handle<i::JSObject> object,
3313 void* data,
3314 ExternalArrayType array_type,
3315 int length) {
3316 i::Isolate* isolate = object->GetIsolate();
3317 i::Handle<i::ExternalArray> array =
3318 isolate->factory()->NewExternalArray(length, array_type, data);
3319
3320 // If the object already has external elements, create a new, unique
3321 // map if the element type is now changing, because assumptions about
3322 // generated code based on the receiver's map will be invalid.
3323 i::Handle<i::HeapObject> elements(object->elements());
3324 bool cant_reuse_map =
3325 elements->map()->IsUndefined() ||
3326 !elements->map()->has_external_array_elements() ||
3327 elements->map() != isolate->heap()->MapForExternalArrayType(array_type);
3328 if (cant_reuse_map) {
3329 i::Handle<i::Map> external_array_map =
Ben Murdoch589d6972011-11-30 16:04:58 +00003330 isolate->factory()->GetElementsTransitionMap(
Steve Block44f0eee2011-05-26 01:26:41 +01003331 i::Handle<i::Map>(object->map()),
Ben Murdoch589d6972011-11-30 16:04:58 +00003332 GetElementsKindFromExternalArrayType(array_type),
Steve Block44f0eee2011-05-26 01:26:41 +01003333 object->HasFastProperties());
3334 object->set_map(*external_array_map);
3335 }
3336 object->set_elements(*array);
3337}
3338
3339} // namespace
3340
3341
Steve Blocka7e24c12009-10-30 11:49:00 +00003342void v8::Object::SetIndexedPropertiesToPixelData(uint8_t* data, int length) {
Steve Block44f0eee2011-05-26 01:26:41 +01003343 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3344 ON_BAILOUT(isolate, "v8::SetElementsToPixelData()", return);
3345 ENTER_V8(isolate);
3346 i::HandleScope scope(isolate);
3347 if (!ApiCheck(length <= i::ExternalPixelArray::kMaxLength,
Steve Blocka7e24c12009-10-30 11:49:00 +00003348 "v8::Object::SetIndexedPropertiesToPixelData()",
3349 "length exceeds max acceptable value")) {
3350 return;
3351 }
3352 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3353 if (!ApiCheck(!self->IsJSArray(),
3354 "v8::Object::SetIndexedPropertiesToPixelData()",
3355 "JSArray is not supported")) {
3356 return;
3357 }
Steve Block44f0eee2011-05-26 01:26:41 +01003358 PrepareExternalArrayElements(self, data, kExternalPixelArray, length);
Steve Blocka7e24c12009-10-30 11:49:00 +00003359}
3360
3361
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003362bool v8::Object::HasIndexedPropertiesInPixelData() {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003363 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003364 ON_BAILOUT(self->GetIsolate(), "v8::HasIndexedPropertiesInPixelData()",
3365 return false);
3366 return self->HasExternalPixelElements();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003367}
3368
3369
3370uint8_t* v8::Object::GetIndexedPropertiesPixelData() {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003371 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003372 ON_BAILOUT(self->GetIsolate(), "v8::GetIndexedPropertiesPixelData()",
3373 return NULL);
3374 if (self->HasExternalPixelElements()) {
3375 return i::ExternalPixelArray::cast(self->elements())->
3376 external_pixel_pointer();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003377 } else {
3378 return NULL;
3379 }
3380}
3381
3382
3383int v8::Object::GetIndexedPropertiesPixelDataLength() {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003384 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003385 ON_BAILOUT(self->GetIsolate(), "v8::GetIndexedPropertiesPixelDataLength()",
3386 return -1);
3387 if (self->HasExternalPixelElements()) {
3388 return i::ExternalPixelArray::cast(self->elements())->length();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003389 } else {
3390 return -1;
3391 }
3392}
3393
Ben Murdoch589d6972011-11-30 16:04:58 +00003394
Steve Block3ce2e202009-11-05 08:53:23 +00003395void v8::Object::SetIndexedPropertiesToExternalArrayData(
3396 void* data,
3397 ExternalArrayType array_type,
3398 int length) {
Steve Block44f0eee2011-05-26 01:26:41 +01003399 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3400 ON_BAILOUT(isolate, "v8::SetIndexedPropertiesToExternalArrayData()", return);
3401 ENTER_V8(isolate);
3402 i::HandleScope scope(isolate);
Steve Block3ce2e202009-11-05 08:53:23 +00003403 if (!ApiCheck(length <= i::ExternalArray::kMaxLength,
3404 "v8::Object::SetIndexedPropertiesToExternalArrayData()",
3405 "length exceeds max acceptable value")) {
3406 return;
3407 }
3408 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3409 if (!ApiCheck(!self->IsJSArray(),
3410 "v8::Object::SetIndexedPropertiesToExternalArrayData()",
3411 "JSArray is not supported")) {
3412 return;
3413 }
Steve Block44f0eee2011-05-26 01:26:41 +01003414 PrepareExternalArrayElements(self, data, array_type, length);
Steve Block3ce2e202009-11-05 08:53:23 +00003415}
3416
3417
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003418bool v8::Object::HasIndexedPropertiesInExternalArrayData() {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003419 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003420 ON_BAILOUT(self->GetIsolate(),
3421 "v8::HasIndexedPropertiesInExternalArrayData()",
3422 return false);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003423 return self->HasExternalArrayElements();
3424}
3425
3426
3427void* v8::Object::GetIndexedPropertiesExternalArrayData() {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003428 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003429 ON_BAILOUT(self->GetIsolate(),
3430 "v8::GetIndexedPropertiesExternalArrayData()",
3431 return NULL);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003432 if (self->HasExternalArrayElements()) {
3433 return i::ExternalArray::cast(self->elements())->external_pointer();
3434 } else {
3435 return NULL;
3436 }
3437}
3438
3439
3440ExternalArrayType v8::Object::GetIndexedPropertiesExternalArrayDataType() {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003441 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003442 ON_BAILOUT(self->GetIsolate(),
3443 "v8::GetIndexedPropertiesExternalArrayDataType()",
3444 return static_cast<ExternalArrayType>(-1));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003445 switch (self->elements()->map()->instance_type()) {
3446 case i::EXTERNAL_BYTE_ARRAY_TYPE:
3447 return kExternalByteArray;
3448 case i::EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE:
3449 return kExternalUnsignedByteArray;
3450 case i::EXTERNAL_SHORT_ARRAY_TYPE:
3451 return kExternalShortArray;
3452 case i::EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE:
3453 return kExternalUnsignedShortArray;
3454 case i::EXTERNAL_INT_ARRAY_TYPE:
3455 return kExternalIntArray;
3456 case i::EXTERNAL_UNSIGNED_INT_ARRAY_TYPE:
3457 return kExternalUnsignedIntArray;
3458 case i::EXTERNAL_FLOAT_ARRAY_TYPE:
3459 return kExternalFloatArray;
Ben Murdoch257744e2011-11-30 15:57:28 +00003460 case i::EXTERNAL_DOUBLE_ARRAY_TYPE:
3461 return kExternalDoubleArray;
Steve Block44f0eee2011-05-26 01:26:41 +01003462 case i::EXTERNAL_PIXEL_ARRAY_TYPE:
3463 return kExternalPixelArray;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003464 default:
3465 return static_cast<ExternalArrayType>(-1);
3466 }
3467}
3468
3469
3470int v8::Object::GetIndexedPropertiesExternalArrayDataLength() {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003471 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003472 ON_BAILOUT(self->GetIsolate(),
3473 "v8::GetIndexedPropertiesExternalArrayDataLength()",
3474 return 0);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003475 if (self->HasExternalArrayElements()) {
3476 return i::ExternalArray::cast(self->elements())->length();
3477 } else {
3478 return -1;
3479 }
3480}
3481
3482
Ben Murdoch257744e2011-11-30 15:57:28 +00003483bool v8::Object::IsCallable() {
3484 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3485 ON_BAILOUT(isolate, "v8::Object::IsCallable()", return false);
3486 ENTER_V8(isolate);
3487 i::HandleScope scope(isolate);
3488 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3489 if (obj->IsJSFunction()) return true;
3490 return i::Execution::GetFunctionDelegate(obj)->IsJSFunction();
3491}
3492
3493
3494Local<v8::Value> Object::CallAsFunction(v8::Handle<v8::Object> recv, int argc,
3495 v8::Handle<v8::Value> argv[]) {
3496 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3497 ON_BAILOUT(isolate, "v8::Object::CallAsFunction()",
3498 return Local<v8::Value>());
3499 LOG_API(isolate, "Object::CallAsFunction");
3500 ENTER_V8(isolate);
3501 i::HandleScope scope(isolate);
3502 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3503 i::Handle<i::Object> recv_obj = Utils::OpenHandle(*recv);
3504 STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**));
3505 i::Object*** args = reinterpret_cast<i::Object***>(argv);
3506 i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>();
3507 if (obj->IsJSFunction()) {
3508 fun = i::Handle<i::JSFunction>::cast(obj);
3509 } else {
3510 EXCEPTION_PREAMBLE(isolate);
3511 i::Handle<i::Object> delegate =
3512 i::Execution::TryGetFunctionDelegate(obj, &has_pending_exception);
3513 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
3514 fun = i::Handle<i::JSFunction>::cast(delegate);
3515 recv_obj = obj;
3516 }
3517 EXCEPTION_PREAMBLE(isolate);
3518 i::Handle<i::Object> returned =
3519 i::Execution::Call(fun, recv_obj, argc, args, &has_pending_exception);
3520 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
3521 return Utils::ToLocal(scope.CloseAndEscape(returned));
3522}
3523
3524
3525Local<v8::Value> Object::CallAsConstructor(int argc,
3526 v8::Handle<v8::Value> argv[]) {
3527 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3528 ON_BAILOUT(isolate, "v8::Object::CallAsConstructor()",
3529 return Local<v8::Object>());
3530 LOG_API(isolate, "Object::CallAsConstructor");
3531 ENTER_V8(isolate);
3532 i::HandleScope scope(isolate);
3533 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3534 STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**));
3535 i::Object*** args = reinterpret_cast<i::Object***>(argv);
3536 if (obj->IsJSFunction()) {
3537 i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(obj);
3538 EXCEPTION_PREAMBLE(isolate);
3539 i::Handle<i::Object> returned =
3540 i::Execution::New(fun, argc, args, &has_pending_exception);
3541 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>());
3542 return Utils::ToLocal(scope.CloseAndEscape(
3543 i::Handle<i::JSObject>::cast(returned)));
3544 }
3545 EXCEPTION_PREAMBLE(isolate);
3546 i::Handle<i::Object> delegate =
3547 i::Execution::TryGetConstructorDelegate(obj, &has_pending_exception);
3548 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>());
3549 if (!delegate->IsUndefined()) {
3550 i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(delegate);
3551 EXCEPTION_PREAMBLE(isolate);
3552 i::Handle<i::Object> returned =
3553 i::Execution::Call(fun, obj, argc, args, &has_pending_exception);
3554 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>());
3555 ASSERT(!delegate->IsUndefined());
3556 return Utils::ToLocal(scope.CloseAndEscape(returned));
3557 }
3558 return Local<v8::Object>();
3559}
3560
3561
Steve Blocka7e24c12009-10-30 11:49:00 +00003562Local<v8::Object> Function::NewInstance() const {
3563 return NewInstance(0, NULL);
3564}
3565
3566
3567Local<v8::Object> Function::NewInstance(int argc,
3568 v8::Handle<v8::Value> argv[]) const {
Steve Block44f0eee2011-05-26 01:26:41 +01003569 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3570 ON_BAILOUT(isolate, "v8::Function::NewInstance()",
3571 return Local<v8::Object>());
3572 LOG_API(isolate, "Function::NewInstance");
3573 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003574 HandleScope scope;
3575 i::Handle<i::JSFunction> function = Utils::OpenHandle(this);
3576 STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**));
3577 i::Object*** args = reinterpret_cast<i::Object***>(argv);
Steve Block44f0eee2011-05-26 01:26:41 +01003578 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003579 i::Handle<i::Object> returned =
3580 i::Execution::New(function, argc, args, &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01003581 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>());
Steve Blocka7e24c12009-10-30 11:49:00 +00003582 return scope.Close(Utils::ToLocal(i::Handle<i::JSObject>::cast(returned)));
3583}
3584
3585
3586Local<v8::Value> Function::Call(v8::Handle<v8::Object> recv, int argc,
3587 v8::Handle<v8::Value> argv[]) {
Steve Block44f0eee2011-05-26 01:26:41 +01003588 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3589 ON_BAILOUT(isolate, "v8::Function::Call()", return Local<v8::Value>());
3590 LOG_API(isolate, "Function::Call");
3591 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003592 i::Object* raw_result = NULL;
3593 {
Steve Block44f0eee2011-05-26 01:26:41 +01003594 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003595 i::Handle<i::JSFunction> fun = Utils::OpenHandle(this);
3596 i::Handle<i::Object> recv_obj = Utils::OpenHandle(*recv);
3597 STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**));
3598 i::Object*** args = reinterpret_cast<i::Object***>(argv);
Steve Block44f0eee2011-05-26 01:26:41 +01003599 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003600 i::Handle<i::Object> returned =
3601 i::Execution::Call(fun, recv_obj, argc, args, &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01003602 EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>());
Steve Blocka7e24c12009-10-30 11:49:00 +00003603 raw_result = *returned;
3604 }
3605 i::Handle<i::Object> result(raw_result);
3606 return Utils::ToLocal(result);
3607}
3608
3609
3610void Function::SetName(v8::Handle<v8::String> name) {
Steve Block44f0eee2011-05-26 01:26:41 +01003611 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3612 ENTER_V8(isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003613 USE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003614 i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
3615 func->shared()->set_name(*Utils::OpenHandle(*name));
3616}
3617
3618
3619Handle<Value> Function::GetName() const {
3620 i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
3621 return Utils::ToLocal(i::Handle<i::Object>(func->shared()->name()));
3622}
3623
3624
Andrei Popescu402d9372010-02-26 13:31:12 +00003625ScriptOrigin Function::GetScriptOrigin() const {
3626 i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
3627 if (func->shared()->script()->IsScript()) {
3628 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
3629 v8::ScriptOrigin origin(
3630 Utils::ToLocal(i::Handle<i::Object>(script->name())),
3631 v8::Integer::New(script->line_offset()->value()),
3632 v8::Integer::New(script->column_offset()->value()));
3633 return origin;
3634 }
3635 return v8::ScriptOrigin(Handle<Value>());
3636}
3637
3638
3639const int Function::kLineOffsetNotFound = -1;
3640
3641
3642int Function::GetScriptLineNumber() const {
3643 i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
3644 if (func->shared()->script()->IsScript()) {
3645 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
3646 return i::GetScriptLineNumber(script, func->shared()->start_position());
3647 }
3648 return kLineOffsetNotFound;
3649}
3650
3651
Steve Blocka7e24c12009-10-30 11:49:00 +00003652int String::Length() const {
Steve Block44f0eee2011-05-26 01:26:41 +01003653 i::Handle<i::String> str = Utils::OpenHandle(this);
3654 if (IsDeadCheck(str->GetIsolate(), "v8::String::Length()")) return 0;
3655 return str->length();
Steve Blocka7e24c12009-10-30 11:49:00 +00003656}
3657
3658
3659int String::Utf8Length() const {
Steve Block44f0eee2011-05-26 01:26:41 +01003660 i::Handle<i::String> str = Utils::OpenHandle(this);
3661 if (IsDeadCheck(str->GetIsolate(), "v8::String::Utf8Length()")) return 0;
3662 return str->Utf8Length();
Steve Blocka7e24c12009-10-30 11:49:00 +00003663}
3664
3665
Steve Block6ded16b2010-05-10 14:33:55 +01003666int String::WriteUtf8(char* buffer,
3667 int capacity,
3668 int* nchars_ref,
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003669 int options) const {
Steve Block44f0eee2011-05-26 01:26:41 +01003670 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3671 if (IsDeadCheck(isolate, "v8::String::WriteUtf8()")) return 0;
3672 LOG_API(isolate, "String::WriteUtf8");
3673 ENTER_V8(isolate);
3674 i::StringInputBuffer& write_input_buffer = *isolate->write_input_buffer();
Steve Blocka7e24c12009-10-30 11:49:00 +00003675 i::Handle<i::String> str = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003676 isolate->string_tracker()->RecordWrite(str);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003677 if (options & HINT_MANY_WRITES_EXPECTED) {
Steve Block6ded16b2010-05-10 14:33:55 +01003678 // Flatten the string for efficiency. This applies whether we are
3679 // using StringInputBuffer or Get(i) to access the characters.
3680 str->TryFlatten();
3681 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003682 write_input_buffer.Reset(0, *str);
3683 int len = str->length();
3684 // Encode the first K - 3 bytes directly into the buffer since we
3685 // know there's room for them. If no capacity is given we copy all
3686 // of them here.
3687 int fast_end = capacity - (unibrow::Utf8::kMaxEncodedSize - 1);
3688 int i;
3689 int pos = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01003690 int nchars = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00003691 for (i = 0; i < len && (capacity == -1 || pos < fast_end); i++) {
3692 i::uc32 c = write_input_buffer.GetNext();
3693 int written = unibrow::Utf8::Encode(buffer + pos, c);
3694 pos += written;
Steve Block6ded16b2010-05-10 14:33:55 +01003695 nchars++;
Steve Blocka7e24c12009-10-30 11:49:00 +00003696 }
3697 if (i < len) {
3698 // For the last characters we need to check the length for each one
3699 // because they may be longer than the remaining space in the
3700 // buffer.
3701 char intermediate[unibrow::Utf8::kMaxEncodedSize];
3702 for (; i < len && pos < capacity; i++) {
3703 i::uc32 c = write_input_buffer.GetNext();
3704 int written = unibrow::Utf8::Encode(intermediate, c);
3705 if (pos + written <= capacity) {
3706 for (int j = 0; j < written; j++)
3707 buffer[pos + j] = intermediate[j];
3708 pos += written;
Steve Block6ded16b2010-05-10 14:33:55 +01003709 nchars++;
Steve Blocka7e24c12009-10-30 11:49:00 +00003710 } else {
3711 // We've reached the end of the buffer
3712 break;
3713 }
3714 }
3715 }
Steve Block6ded16b2010-05-10 14:33:55 +01003716 if (nchars_ref != NULL) *nchars_ref = nchars;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003717 if (!(options & NO_NULL_TERMINATION) &&
3718 (i == len && (capacity == -1 || pos < capacity)))
Steve Blocka7e24c12009-10-30 11:49:00 +00003719 buffer[pos++] = '\0';
3720 return pos;
3721}
3722
3723
Steve Block6ded16b2010-05-10 14:33:55 +01003724int String::WriteAscii(char* buffer,
3725 int start,
3726 int length,
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003727 int options) const {
Steve Block44f0eee2011-05-26 01:26:41 +01003728 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3729 if (IsDeadCheck(isolate, "v8::String::WriteAscii()")) return 0;
3730 LOG_API(isolate, "String::WriteAscii");
3731 ENTER_V8(isolate);
3732 i::StringInputBuffer& write_input_buffer = *isolate->write_input_buffer();
Steve Blocka7e24c12009-10-30 11:49:00 +00003733 ASSERT(start >= 0 && length >= -1);
3734 i::Handle<i::String> str = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003735 isolate->string_tracker()->RecordWrite(str);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003736 if (options & HINT_MANY_WRITES_EXPECTED) {
Steve Block6ded16b2010-05-10 14:33:55 +01003737 // Flatten the string for efficiency. This applies whether we are
3738 // using StringInputBuffer or Get(i) to access the characters.
3739 str->TryFlatten();
3740 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003741 int end = length;
3742 if ( (length == -1) || (length > str->length() - start) )
3743 end = str->length() - start;
3744 if (end < 0) return 0;
3745 write_input_buffer.Reset(start, *str);
3746 int i;
3747 for (i = 0; i < end; i++) {
3748 char c = static_cast<char>(write_input_buffer.GetNext());
3749 if (c == '\0') c = ' ';
3750 buffer[i] = c;
3751 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003752 if (!(options & NO_NULL_TERMINATION) && (length == -1 || i < length))
Steve Blocka7e24c12009-10-30 11:49:00 +00003753 buffer[i] = '\0';
3754 return i;
3755}
3756
3757
Steve Block6ded16b2010-05-10 14:33:55 +01003758int String::Write(uint16_t* buffer,
3759 int start,
3760 int length,
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003761 int options) const {
Steve Block44f0eee2011-05-26 01:26:41 +01003762 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3763 if (IsDeadCheck(isolate, "v8::String::Write()")) return 0;
3764 LOG_API(isolate, "String::Write");
3765 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003766 ASSERT(start >= 0 && length >= -1);
3767 i::Handle<i::String> str = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003768 isolate->string_tracker()->RecordWrite(str);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003769 if (options & HINT_MANY_WRITES_EXPECTED) {
Steve Block6ded16b2010-05-10 14:33:55 +01003770 // Flatten the string for efficiency. This applies whether we are
3771 // using StringInputBuffer or Get(i) to access the characters.
3772 str->TryFlatten();
3773 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003774 int end = start + length;
3775 if ((length == -1) || (length > str->length() - start) )
3776 end = str->length();
Steve Blocka7e24c12009-10-30 11:49:00 +00003777 if (end < 0) return 0;
3778 i::String::WriteToFlat(*str, buffer, start, end);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003779 if (!(options & NO_NULL_TERMINATION) &&
3780 (length == -1 || end - start < length)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01003781 buffer[end - start] = '\0';
3782 }
3783 return end - start;
Steve Blocka7e24c12009-10-30 11:49:00 +00003784}
3785
3786
3787bool v8::String::IsExternal() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00003788 i::Handle<i::String> str = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003789 if (IsDeadCheck(str->GetIsolate(), "v8::String::IsExternal()")) {
3790 return false;
3791 }
3792 EnsureInitializedForIsolate(str->GetIsolate(), "v8::String::IsExternal()");
Steve Blocka7e24c12009-10-30 11:49:00 +00003793 return i::StringShape(*str).IsExternalTwoByte();
3794}
3795
3796
3797bool v8::String::IsExternalAscii() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00003798 i::Handle<i::String> str = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003799 if (IsDeadCheck(str->GetIsolate(), "v8::String::IsExternalAscii()")) {
3800 return false;
3801 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003802 return i::StringShape(*str).IsExternalAscii();
3803}
3804
3805
3806void v8::String::VerifyExternalStringResource(
3807 v8::String::ExternalStringResource* value) const {
3808 i::Handle<i::String> str = Utils::OpenHandle(this);
3809 v8::String::ExternalStringResource* expected;
3810 if (i::StringShape(*str).IsExternalTwoByte()) {
3811 void* resource = i::Handle<i::ExternalTwoByteString>::cast(str)->resource();
3812 expected = reinterpret_cast<ExternalStringResource*>(resource);
3813 } else {
3814 expected = NULL;
3815 }
3816 CHECK_EQ(expected, value);
3817}
3818
3819
3820v8::String::ExternalAsciiStringResource*
3821 v8::String::GetExternalAsciiStringResource() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00003822 i::Handle<i::String> str = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003823 if (IsDeadCheck(str->GetIsolate(),
3824 "v8::String::GetExternalAsciiStringResource()")) {
3825 return NULL;
3826 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003827 if (i::StringShape(*str).IsExternalAscii()) {
3828 void* resource = i::Handle<i::ExternalAsciiString>::cast(str)->resource();
3829 return reinterpret_cast<ExternalAsciiStringResource*>(resource);
3830 } else {
3831 return NULL;
3832 }
3833}
3834
3835
3836double Number::Value() const {
Steve Block44f0eee2011-05-26 01:26:41 +01003837 if (IsDeadCheck(i::Isolate::Current(), "v8::Number::Value()")) return 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00003838 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3839 return obj->Number();
3840}
3841
3842
3843bool Boolean::Value() const {
Steve Block44f0eee2011-05-26 01:26:41 +01003844 if (IsDeadCheck(i::Isolate::Current(), "v8::Boolean::Value()")) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00003845 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3846 return obj->IsTrue();
3847}
3848
3849
3850int64_t Integer::Value() const {
Steve Block44f0eee2011-05-26 01:26:41 +01003851 if (IsDeadCheck(i::Isolate::Current(), "v8::Integer::Value()")) return 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00003852 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3853 if (obj->IsSmi()) {
3854 return i::Smi::cast(*obj)->value();
3855 } else {
3856 return static_cast<int64_t>(obj->Number());
3857 }
3858}
3859
3860
3861int32_t Int32::Value() const {
Steve Block44f0eee2011-05-26 01:26:41 +01003862 if (IsDeadCheck(i::Isolate::Current(), "v8::Int32::Value()")) return 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00003863 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3864 if (obj->IsSmi()) {
3865 return i::Smi::cast(*obj)->value();
3866 } else {
3867 return static_cast<int32_t>(obj->Number());
3868 }
3869}
3870
3871
Steve Block6ded16b2010-05-10 14:33:55 +01003872uint32_t Uint32::Value() const {
Steve Block44f0eee2011-05-26 01:26:41 +01003873 if (IsDeadCheck(i::Isolate::Current(), "v8::Uint32::Value()")) return 0;
Steve Block6ded16b2010-05-10 14:33:55 +01003874 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3875 if (obj->IsSmi()) {
3876 return i::Smi::cast(*obj)->value();
3877 } else {
3878 return static_cast<uint32_t>(obj->Number());
3879 }
3880}
3881
3882
Steve Blocka7e24c12009-10-30 11:49:00 +00003883int v8::Object::InternalFieldCount() {
Steve Blocka7e24c12009-10-30 11:49:00 +00003884 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003885 if (IsDeadCheck(obj->GetIsolate(), "v8::Object::InternalFieldCount()")) {
3886 return 0;
3887 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003888 return obj->GetInternalFieldCount();
3889}
3890
3891
3892Local<Value> v8::Object::CheckedGetInternalField(int index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003893 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003894 if (IsDeadCheck(obj->GetIsolate(), "v8::Object::GetInternalField()")) {
3895 return Local<Value>();
3896 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003897 if (!ApiCheck(index < obj->GetInternalFieldCount(),
3898 "v8::Object::GetInternalField()",
3899 "Reading internal field out of bounds")) {
3900 return Local<Value>();
3901 }
3902 i::Handle<i::Object> value(obj->GetInternalField(index));
3903 Local<Value> result = Utils::ToLocal(value);
3904#ifdef DEBUG
3905 Local<Value> unchecked = UncheckedGetInternalField(index);
3906 ASSERT(unchecked.IsEmpty() || (unchecked == result));
3907#endif
3908 return result;
3909}
3910
3911
3912void v8::Object::SetInternalField(int index, v8::Handle<Value> value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003913 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003914 i::Isolate* isolate = obj->GetIsolate();
3915 if (IsDeadCheck(isolate, "v8::Object::SetInternalField()")) {
3916 return;
3917 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003918 if (!ApiCheck(index < obj->GetInternalFieldCount(),
3919 "v8::Object::SetInternalField()",
3920 "Writing internal field out of bounds")) {
3921 return;
3922 }
Steve Block44f0eee2011-05-26 01:26:41 +01003923 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003924 i::Handle<i::Object> val = Utils::OpenHandle(*value);
3925 obj->SetInternalField(index, *val);
3926}
3927
3928
Ben Murdochb8e0da22011-05-16 14:20:40 +01003929static bool CanBeEncodedAsSmi(void* ptr) {
Steve Block1e0659c2011-05-24 12:43:12 +01003930 const uintptr_t address = reinterpret_cast<uintptr_t>(ptr);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003931 return ((address & i::kEncodablePointerMask) == 0);
3932}
3933
3934
3935static i::Smi* EncodeAsSmi(void* ptr) {
3936 ASSERT(CanBeEncodedAsSmi(ptr));
Steve Block1e0659c2011-05-24 12:43:12 +01003937 const uintptr_t address = reinterpret_cast<uintptr_t>(ptr);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003938 i::Smi* result = reinterpret_cast<i::Smi*>(address << i::kPointerToSmiShift);
3939 ASSERT(i::Internals::HasSmiTag(result));
3940 ASSERT_EQ(result, i::Smi::FromInt(result->value()));
3941 ASSERT_EQ(ptr, i::Internals::GetExternalPointerFromSmi(result));
3942 return result;
3943}
3944
3945
Steve Blocka7e24c12009-10-30 11:49:00 +00003946void v8::Object::SetPointerInInternalField(int index, void* value) {
Steve Block44f0eee2011-05-26 01:26:41 +01003947 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3948 ENTER_V8(isolate);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003949 if (CanBeEncodedAsSmi(value)) {
3950 Utils::OpenHandle(this)->SetInternalField(index, EncodeAsSmi(value));
3951 } else {
3952 HandleScope scope;
Ben Murdoch257744e2011-11-30 15:57:28 +00003953 i::Handle<i::Foreign> foreign =
3954 isolate->factory()->NewForeign(
Steve Block44f0eee2011-05-26 01:26:41 +01003955 reinterpret_cast<i::Address>(value), i::TENURED);
Ben Murdoch257744e2011-11-30 15:57:28 +00003956 if (!foreign.is_null())
3957 Utils::OpenHandle(this)->SetInternalField(index, *foreign);
Steve Block3ce2e202009-11-05 08:53:23 +00003958 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01003959 ASSERT_EQ(value, GetPointerFromInternalField(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00003960}
3961
3962
3963// --- E n v i r o n m e n t ---
3964
Steve Block44f0eee2011-05-26 01:26:41 +01003965
Steve Blocka7e24c12009-10-30 11:49:00 +00003966bool v8::V8::Initialize() {
Steve Block44f0eee2011-05-26 01:26:41 +01003967 i::Isolate* isolate = i::Isolate::UncheckedCurrent();
3968 if (isolate != NULL && isolate->IsInitialized()) {
3969 return true;
3970 }
3971 return InitializeHelper();
Steve Blocka7e24c12009-10-30 11:49:00 +00003972}
3973
3974
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003975void v8::V8::SetEntropySource(EntropySource source) {
3976 i::V8::SetEntropySource(source);
3977}
3978
3979
Steve Blocka7e24c12009-10-30 11:49:00 +00003980bool v8::V8::Dispose() {
Steve Block44f0eee2011-05-26 01:26:41 +01003981 i::Isolate* isolate = i::Isolate::Current();
3982 if (!ApiCheck(isolate != NULL && isolate->IsDefaultIsolate(),
3983 "v8::V8::Dispose()",
3984 "Use v8::Isolate::Dispose() for a non-default isolate.")) {
3985 return false;
3986 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003987 i::V8::TearDown();
3988 return true;
3989}
3990
3991
Russell Brenner90bac252010-11-18 13:33:46 -08003992HeapStatistics::HeapStatistics(): total_heap_size_(0),
3993 total_heap_size_executable_(0),
Ben Murdochb8e0da22011-05-16 14:20:40 +01003994 used_heap_size_(0),
3995 heap_size_limit_(0) { }
Steve Block3ce2e202009-11-05 08:53:23 +00003996
3997
3998void v8::V8::GetHeapStatistics(HeapStatistics* heap_statistics) {
Steve Block44f0eee2011-05-26 01:26:41 +01003999 i::Heap* heap = i::Isolate::Current()->heap();
4000 heap_statistics->set_total_heap_size(heap->CommittedMemory());
Russell Brenner90bac252010-11-18 13:33:46 -08004001 heap_statistics->set_total_heap_size_executable(
Steve Block44f0eee2011-05-26 01:26:41 +01004002 heap->CommittedMemoryExecutable());
4003 heap_statistics->set_used_heap_size(heap->SizeOfObjects());
4004 heap_statistics->set_heap_size_limit(heap->MaxReserved());
Steve Block3ce2e202009-11-05 08:53:23 +00004005}
4006
4007
4008bool v8::V8::IdleNotification() {
Steve Blocka7e24c12009-10-30 11:49:00 +00004009 // Returning true tells the caller that it need not
4010 // continue to call IdleNotification.
Steve Block44f0eee2011-05-26 01:26:41 +01004011 if (!i::Isolate::Current()->IsInitialized()) return true;
Steve Block3ce2e202009-11-05 08:53:23 +00004012 return i::V8::IdleNotification();
Steve Blocka7e24c12009-10-30 11:49:00 +00004013}
4014
4015
4016void v8::V8::LowMemoryNotification() {
Steve Block44f0eee2011-05-26 01:26:41 +01004017 i::Isolate* isolate = i::Isolate::Current();
4018 if (!isolate->IsInitialized()) return;
4019 isolate->heap()->CollectAllGarbage(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00004020}
4021
4022
Steve Block6ded16b2010-05-10 14:33:55 +01004023int v8::V8::ContextDisposedNotification() {
Steve Block44f0eee2011-05-26 01:26:41 +01004024 i::Isolate* isolate = i::Isolate::Current();
4025 if (!isolate->IsInitialized()) return 0;
4026 return isolate->heap()->NotifyContextDisposed();
Steve Block6ded16b2010-05-10 14:33:55 +01004027}
4028
4029
Steve Blocka7e24c12009-10-30 11:49:00 +00004030const char* v8::V8::GetVersion() {
Steve Block44f0eee2011-05-26 01:26:41 +01004031 return i::Version::GetVersion();
Steve Blocka7e24c12009-10-30 11:49:00 +00004032}
4033
4034
4035static i::Handle<i::FunctionTemplateInfo>
4036 EnsureConstructor(i::Handle<i::ObjectTemplateInfo> templ) {
4037 if (templ->constructor()->IsUndefined()) {
4038 Local<FunctionTemplate> constructor = FunctionTemplate::New();
4039 Utils::OpenHandle(*constructor)->set_instance_template(*templ);
4040 templ->set_constructor(*Utils::OpenHandle(*constructor));
4041 }
4042 return i::Handle<i::FunctionTemplateInfo>(
4043 i::FunctionTemplateInfo::cast(templ->constructor()));
4044}
4045
4046
4047Persistent<Context> v8::Context::New(
4048 v8::ExtensionConfiguration* extensions,
4049 v8::Handle<ObjectTemplate> global_template,
4050 v8::Handle<Value> global_object) {
Steve Block44f0eee2011-05-26 01:26:41 +01004051 i::Isolate* isolate = i::Isolate::Current();
4052 EnsureInitializedForIsolate(isolate, "v8::Context::New()");
4053 LOG_API(isolate, "Context::New");
4054 ON_BAILOUT(isolate, "v8::Context::New()", return Persistent<Context>());
Steve Blocka7e24c12009-10-30 11:49:00 +00004055
4056 // Enter V8 via an ENTER_V8 scope.
4057 i::Handle<i::Context> env;
4058 {
Steve Block44f0eee2011-05-26 01:26:41 +01004059 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00004060 v8::Handle<ObjectTemplate> proxy_template = global_template;
4061 i::Handle<i::FunctionTemplateInfo> proxy_constructor;
4062 i::Handle<i::FunctionTemplateInfo> global_constructor;
4063
4064 if (!global_template.IsEmpty()) {
4065 // Make sure that the global_template has a constructor.
4066 global_constructor =
4067 EnsureConstructor(Utils::OpenHandle(*global_template));
4068
4069 // Create a fresh template for the global proxy object.
4070 proxy_template = ObjectTemplate::New();
4071 proxy_constructor =
4072 EnsureConstructor(Utils::OpenHandle(*proxy_template));
4073
4074 // Set the global template to be the prototype template of
4075 // global proxy template.
4076 proxy_constructor->set_prototype_template(
4077 *Utils::OpenHandle(*global_template));
4078
4079 // Migrate security handlers from global_template to
4080 // proxy_template. Temporarily removing access check
4081 // information from the global template.
4082 if (!global_constructor->access_check_info()->IsUndefined()) {
4083 proxy_constructor->set_access_check_info(
4084 global_constructor->access_check_info());
4085 proxy_constructor->set_needs_access_check(
4086 global_constructor->needs_access_check());
4087 global_constructor->set_needs_access_check(false);
Steve Block44f0eee2011-05-26 01:26:41 +01004088 global_constructor->set_access_check_info(
4089 isolate->heap()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00004090 }
4091 }
4092
4093 // Create the environment.
Steve Block44f0eee2011-05-26 01:26:41 +01004094 env = isolate->bootstrapper()->CreateEnvironment(
Ben Murdoch257744e2011-11-30 15:57:28 +00004095 isolate,
Steve Blocka7e24c12009-10-30 11:49:00 +00004096 Utils::OpenHandle(*global_object),
4097 proxy_template,
4098 extensions);
4099
4100 // Restore the access check info on the global template.
4101 if (!global_template.IsEmpty()) {
4102 ASSERT(!global_constructor.is_null());
4103 ASSERT(!proxy_constructor.is_null());
4104 global_constructor->set_access_check_info(
4105 proxy_constructor->access_check_info());
4106 global_constructor->set_needs_access_check(
4107 proxy_constructor->needs_access_check());
4108 }
Steve Block44f0eee2011-05-26 01:26:41 +01004109 isolate->runtime_profiler()->Reset();
Steve Blocka7e24c12009-10-30 11:49:00 +00004110 }
4111 // Leave V8.
4112
4113 if (env.is_null())
4114 return Persistent<Context>();
4115 return Persistent<Context>(Utils::ToLocal(env));
4116}
4117
4118
4119void v8::Context::SetSecurityToken(Handle<Value> token) {
Steve Block44f0eee2011-05-26 01:26:41 +01004120 i::Isolate* isolate = i::Isolate::Current();
4121 if (IsDeadCheck(isolate, "v8::Context::SetSecurityToken()")) {
4122 return;
4123 }
4124 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00004125 i::Handle<i::Context> env = Utils::OpenHandle(this);
4126 i::Handle<i::Object> token_handle = Utils::OpenHandle(*token);
4127 env->set_security_token(*token_handle);
4128}
4129
4130
4131void v8::Context::UseDefaultSecurityToken() {
Steve Block44f0eee2011-05-26 01:26:41 +01004132 i::Isolate* isolate = i::Isolate::Current();
4133 if (IsDeadCheck(isolate,
4134 "v8::Context::UseDefaultSecurityToken()")) {
4135 return;
4136 }
4137 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00004138 i::Handle<i::Context> env = Utils::OpenHandle(this);
4139 env->set_security_token(env->global());
4140}
4141
4142
4143Handle<Value> v8::Context::GetSecurityToken() {
Steve Block44f0eee2011-05-26 01:26:41 +01004144 i::Isolate* isolate = i::Isolate::Current();
4145 if (IsDeadCheck(isolate, "v8::Context::GetSecurityToken()")) {
4146 return Handle<Value>();
4147 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004148 i::Handle<i::Context> env = Utils::OpenHandle(this);
4149 i::Object* security_token = env->security_token();
4150 i::Handle<i::Object> token_handle(security_token);
4151 return Utils::ToLocal(token_handle);
4152}
4153
4154
4155bool Context::HasOutOfMemoryException() {
4156 i::Handle<i::Context> env = Utils::OpenHandle(this);
4157 return env->has_out_of_memory();
4158}
4159
4160
4161bool Context::InContext() {
Steve Block44f0eee2011-05-26 01:26:41 +01004162 return i::Isolate::Current()->context() != NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00004163}
4164
4165
4166v8::Local<v8::Context> Context::GetEntered() {
Steve Block44f0eee2011-05-26 01:26:41 +01004167 i::Isolate* isolate = i::Isolate::Current();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004168 if (!EnsureInitializedForIsolate(isolate, "v8::Context::GetEntered()")) {
Steve Block44f0eee2011-05-26 01:26:41 +01004169 return Local<Context>();
4170 }
4171 i::Handle<i::Object> last =
4172 isolate->handle_scope_implementer()->LastEnteredContext();
Steve Blocka7e24c12009-10-30 11:49:00 +00004173 if (last.is_null()) return Local<Context>();
4174 i::Handle<i::Context> context = i::Handle<i::Context>::cast(last);
4175 return Utils::ToLocal(context);
4176}
4177
4178
4179v8::Local<v8::Context> Context::GetCurrent() {
Steve Block44f0eee2011-05-26 01:26:41 +01004180 i::Isolate* isolate = i::Isolate::Current();
4181 if (IsDeadCheck(isolate, "v8::Context::GetCurrent()")) {
4182 return Local<Context>();
4183 }
4184 i::Handle<i::Object> current = isolate->global_context();
Steve Block3ce2e202009-11-05 08:53:23 +00004185 if (current.is_null()) return Local<Context>();
4186 i::Handle<i::Context> context = i::Handle<i::Context>::cast(current);
Steve Blocka7e24c12009-10-30 11:49:00 +00004187 return Utils::ToLocal(context);
4188}
4189
4190
4191v8::Local<v8::Context> Context::GetCalling() {
Steve Block44f0eee2011-05-26 01:26:41 +01004192 i::Isolate* isolate = i::Isolate::Current();
4193 if (IsDeadCheck(isolate, "v8::Context::GetCalling()")) {
4194 return Local<Context>();
4195 }
4196 i::Handle<i::Object> calling =
4197 isolate->GetCallingGlobalContext();
Steve Blocka7e24c12009-10-30 11:49:00 +00004198 if (calling.is_null()) return Local<Context>();
4199 i::Handle<i::Context> context = i::Handle<i::Context>::cast(calling);
4200 return Utils::ToLocal(context);
4201}
4202
4203
4204v8::Local<v8::Object> Context::Global() {
Steve Block44f0eee2011-05-26 01:26:41 +01004205 if (IsDeadCheck(i::Isolate::Current(), "v8::Context::Global()")) {
4206 return Local<v8::Object>();
4207 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004208 i::Object** ctx = reinterpret_cast<i::Object**>(this);
4209 i::Handle<i::Context> context =
4210 i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx));
4211 i::Handle<i::Object> global(context->global_proxy());
4212 return Utils::ToLocal(i::Handle<i::JSObject>::cast(global));
4213}
4214
4215
4216void Context::DetachGlobal() {
Steve Block44f0eee2011-05-26 01:26:41 +01004217 i::Isolate* isolate = i::Isolate::Current();
4218 if (IsDeadCheck(isolate, "v8::Context::DetachGlobal()")) return;
4219 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00004220 i::Object** ctx = reinterpret_cast<i::Object**>(this);
4221 i::Handle<i::Context> context =
4222 i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx));
Steve Block44f0eee2011-05-26 01:26:41 +01004223 isolate->bootstrapper()->DetachGlobal(context);
Steve Blocka7e24c12009-10-30 11:49:00 +00004224}
4225
4226
Andrei Popescu74b3c142010-03-29 12:03:09 +01004227void Context::ReattachGlobal(Handle<Object> global_object) {
Steve Block44f0eee2011-05-26 01:26:41 +01004228 i::Isolate* isolate = i::Isolate::Current();
4229 if (IsDeadCheck(isolate, "v8::Context::ReattachGlobal()")) return;
4230 ENTER_V8(isolate);
Andrei Popescu74b3c142010-03-29 12:03:09 +01004231 i::Object** ctx = reinterpret_cast<i::Object**>(this);
4232 i::Handle<i::Context> context =
4233 i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx));
Steve Block44f0eee2011-05-26 01:26:41 +01004234 isolate->bootstrapper()->ReattachGlobal(
4235 context,
4236 Utils::OpenHandle(*global_object));
4237}
4238
4239
Ben Murdoch257744e2011-11-30 15:57:28 +00004240void Context::AllowCodeGenerationFromStrings(bool allow) {
4241 i::Isolate* isolate = i::Isolate::Current();
4242 if (IsDeadCheck(isolate, "v8::Context::AllowCodeGenerationFromStrings()")) {
4243 return;
4244 }
4245 ENTER_V8(isolate);
4246 i::Object** ctx = reinterpret_cast<i::Object**>(this);
4247 i::Handle<i::Context> context =
4248 i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx));
4249 context->set_allow_code_gen_from_strings(
4250 allow ? isolate->heap()->true_value() : isolate->heap()->false_value());
4251}
4252
4253
Steve Block44f0eee2011-05-26 01:26:41 +01004254void V8::SetWrapperClassId(i::Object** global_handle, uint16_t class_id) {
4255 i::GlobalHandles::SetWrapperClassId(global_handle, class_id);
Andrei Popescu74b3c142010-03-29 12:03:09 +01004256}
4257
4258
Steve Blocka7e24c12009-10-30 11:49:00 +00004259Local<v8::Object> ObjectTemplate::NewInstance() {
Steve Block44f0eee2011-05-26 01:26:41 +01004260 i::Isolate* isolate = i::Isolate::Current();
4261 ON_BAILOUT(isolate, "v8::ObjectTemplate::NewInstance()",
4262 return Local<v8::Object>());
4263 LOG_API(isolate, "ObjectTemplate::NewInstance");
4264 ENTER_V8(isolate);
4265 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00004266 i::Handle<i::Object> obj =
4267 i::Execution::InstantiateObject(Utils::OpenHandle(this),
4268 &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01004269 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>());
Steve Blocka7e24c12009-10-30 11:49:00 +00004270 return Utils::ToLocal(i::Handle<i::JSObject>::cast(obj));
4271}
4272
4273
4274Local<v8::Function> FunctionTemplate::GetFunction() {
Steve Block44f0eee2011-05-26 01:26:41 +01004275 i::Isolate* isolate = i::Isolate::Current();
4276 ON_BAILOUT(isolate, "v8::FunctionTemplate::GetFunction()",
Steve Blocka7e24c12009-10-30 11:49:00 +00004277 return Local<v8::Function>());
Steve Block44f0eee2011-05-26 01:26:41 +01004278 LOG_API(isolate, "FunctionTemplate::GetFunction");
4279 ENTER_V8(isolate);
4280 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00004281 i::Handle<i::Object> obj =
4282 i::Execution::InstantiateFunction(Utils::OpenHandle(this),
4283 &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01004284 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Function>());
Steve Blocka7e24c12009-10-30 11:49:00 +00004285 return Utils::ToLocal(i::Handle<i::JSFunction>::cast(obj));
4286}
4287
4288
4289bool FunctionTemplate::HasInstance(v8::Handle<v8::Value> value) {
Steve Block44f0eee2011-05-26 01:26:41 +01004290 ON_BAILOUT(i::Isolate::Current(), "v8::FunctionTemplate::HasInstanceOf()",
4291 return false);
Steve Blocka7e24c12009-10-30 11:49:00 +00004292 i::Object* obj = *Utils::OpenHandle(*value);
4293 return obj->IsInstanceOf(*Utils::OpenHandle(this));
4294}
4295
4296
4297static Local<External> ExternalNewImpl(void* data) {
Ben Murdoch257744e2011-11-30 15:57:28 +00004298 return Utils::ToLocal(FACTORY->NewForeign(static_cast<i::Address>(data)));
Steve Blocka7e24c12009-10-30 11:49:00 +00004299}
4300
4301static void* ExternalValueImpl(i::Handle<i::Object> obj) {
Ben Murdoch257744e2011-11-30 15:57:28 +00004302 return reinterpret_cast<void*>(i::Foreign::cast(*obj)->address());
Steve Blocka7e24c12009-10-30 11:49:00 +00004303}
4304
4305
Steve Blocka7e24c12009-10-30 11:49:00 +00004306Local<Value> v8::External::Wrap(void* data) {
Steve Block44f0eee2011-05-26 01:26:41 +01004307 i::Isolate* isolate = i::Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +00004308 STATIC_ASSERT(sizeof(data) == sizeof(i::Address));
Ben Murdoch257744e2011-11-30 15:57:28 +00004309 EnsureInitializedForIsolate(isolate, "v8::External::Wrap()");
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004310 LOG_API(isolate, "External::Wrap");
Steve Block44f0eee2011-05-26 01:26:41 +01004311 ENTER_V8(isolate);
Ben Murdochb8e0da22011-05-16 14:20:40 +01004312
4313 v8::Local<v8::Value> result = CanBeEncodedAsSmi(data)
4314 ? Utils::ToLocal(i::Handle<i::Object>(EncodeAsSmi(data)))
4315 : v8::Local<v8::Value>(ExternalNewImpl(data));
4316
4317 ASSERT_EQ(data, Unwrap(result));
4318 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00004319}
4320
4321
Steve Block3ce2e202009-11-05 08:53:23 +00004322void* v8::Object::SlowGetPointerFromInternalField(int index) {
4323 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
4324 i::Object* value = obj->GetInternalField(index);
4325 if (value->IsSmi()) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01004326 return i::Internals::GetExternalPointerFromSmi(value);
Ben Murdoch257744e2011-11-30 15:57:28 +00004327 } else if (value->IsForeign()) {
4328 return reinterpret_cast<void*>(i::Foreign::cast(value)->address());
Steve Block3ce2e202009-11-05 08:53:23 +00004329 } else {
4330 return NULL;
4331 }
4332}
4333
4334
Steve Blocka7e24c12009-10-30 11:49:00 +00004335void* v8::External::FullUnwrap(v8::Handle<v8::Value> wrapper) {
Steve Block44f0eee2011-05-26 01:26:41 +01004336 if (IsDeadCheck(i::Isolate::Current(), "v8::External::Unwrap()")) return 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00004337 i::Handle<i::Object> obj = Utils::OpenHandle(*wrapper);
4338 void* result;
4339 if (obj->IsSmi()) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01004340 result = i::Internals::GetExternalPointerFromSmi(*obj);
Ben Murdoch257744e2011-11-30 15:57:28 +00004341 } else if (obj->IsForeign()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004342 result = ExternalValueImpl(obj);
4343 } else {
4344 result = NULL;
4345 }
4346 ASSERT_EQ(result, QuickUnwrap(wrapper));
4347 return result;
4348}
4349
4350
4351Local<External> v8::External::New(void* data) {
4352 STATIC_ASSERT(sizeof(data) == sizeof(i::Address));
Steve Block44f0eee2011-05-26 01:26:41 +01004353 i::Isolate* isolate = i::Isolate::Current();
Ben Murdoch257744e2011-11-30 15:57:28 +00004354 EnsureInitializedForIsolate(isolate, "v8::External::New()");
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004355 LOG_API(isolate, "External::New");
Steve Block44f0eee2011-05-26 01:26:41 +01004356 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00004357 return ExternalNewImpl(data);
4358}
4359
4360
4361void* External::Value() const {
Steve Block44f0eee2011-05-26 01:26:41 +01004362 if (IsDeadCheck(i::Isolate::Current(), "v8::External::Value()")) return 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00004363 i::Handle<i::Object> obj = Utils::OpenHandle(this);
4364 return ExternalValueImpl(obj);
4365}
4366
4367
4368Local<String> v8::String::Empty() {
Steve Block44f0eee2011-05-26 01:26:41 +01004369 i::Isolate* isolate = i::Isolate::Current();
4370 EnsureInitializedForIsolate(isolate, "v8::String::Empty()");
4371 LOG_API(isolate, "String::Empty()");
4372 return Utils::ToLocal(isolate->factory()->empty_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +00004373}
4374
4375
4376Local<String> v8::String::New(const char* data, int length) {
Steve Block44f0eee2011-05-26 01:26:41 +01004377 i::Isolate* isolate = i::Isolate::Current();
4378 EnsureInitializedForIsolate(isolate, "v8::String::New()");
4379 LOG_API(isolate, "String::New(char)");
Steve Blocka7e24c12009-10-30 11:49:00 +00004380 if (length == 0) return Empty();
Steve Block44f0eee2011-05-26 01:26:41 +01004381 ENTER_V8(isolate);
Steve Blockd0582a62009-12-15 09:54:21 +00004382 if (length == -1) length = i::StrLength(data);
Steve Blocka7e24c12009-10-30 11:49:00 +00004383 i::Handle<i::String> result =
Steve Block44f0eee2011-05-26 01:26:41 +01004384 isolate->factory()->NewStringFromUtf8(
4385 i::Vector<const char>(data, length));
Steve Blocka7e24c12009-10-30 11:49:00 +00004386 return Utils::ToLocal(result);
4387}
4388
4389
Steve Block3ce2e202009-11-05 08:53:23 +00004390Local<String> v8::String::Concat(Handle<String> left, Handle<String> right) {
Steve Block3ce2e202009-11-05 08:53:23 +00004391 i::Handle<i::String> left_string = Utils::OpenHandle(*left);
Steve Block44f0eee2011-05-26 01:26:41 +01004392 i::Isolate* isolate = left_string->GetIsolate();
4393 EnsureInitializedForIsolate(isolate, "v8::String::New()");
4394 LOG_API(isolate, "String::New(char)");
4395 ENTER_V8(isolate);
Steve Block3ce2e202009-11-05 08:53:23 +00004396 i::Handle<i::String> right_string = Utils::OpenHandle(*right);
Steve Block44f0eee2011-05-26 01:26:41 +01004397 i::Handle<i::String> result = isolate->factory()->NewConsString(left_string,
4398 right_string);
Steve Block3ce2e202009-11-05 08:53:23 +00004399 return Utils::ToLocal(result);
4400}
4401
4402
Steve Blocka7e24c12009-10-30 11:49:00 +00004403Local<String> v8::String::NewUndetectable(const char* data, int length) {
Steve Block44f0eee2011-05-26 01:26:41 +01004404 i::Isolate* isolate = i::Isolate::Current();
4405 EnsureInitializedForIsolate(isolate, "v8::String::NewUndetectable()");
4406 LOG_API(isolate, "String::NewUndetectable(char)");
4407 ENTER_V8(isolate);
Steve Blockd0582a62009-12-15 09:54:21 +00004408 if (length == -1) length = i::StrLength(data);
Steve Blocka7e24c12009-10-30 11:49:00 +00004409 i::Handle<i::String> result =
Steve Block44f0eee2011-05-26 01:26:41 +01004410 isolate->factory()->NewStringFromUtf8(
4411 i::Vector<const char>(data, length));
Steve Blocka7e24c12009-10-30 11:49:00 +00004412 result->MarkAsUndetectable();
4413 return Utils::ToLocal(result);
4414}
4415
4416
4417static int TwoByteStringLength(const uint16_t* data) {
4418 int length = 0;
4419 while (data[length] != '\0') length++;
4420 return length;
4421}
4422
4423
4424Local<String> v8::String::New(const uint16_t* data, int length) {
Steve Block44f0eee2011-05-26 01:26:41 +01004425 i::Isolate* isolate = i::Isolate::Current();
4426 EnsureInitializedForIsolate(isolate, "v8::String::New()");
4427 LOG_API(isolate, "String::New(uint16_)");
Steve Blocka7e24c12009-10-30 11:49:00 +00004428 if (length == 0) return Empty();
Steve Block44f0eee2011-05-26 01:26:41 +01004429 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00004430 if (length == -1) length = TwoByteStringLength(data);
4431 i::Handle<i::String> result =
Steve Block44f0eee2011-05-26 01:26:41 +01004432 isolate->factory()->NewStringFromTwoByte(
4433 i::Vector<const uint16_t>(data, length));
Steve Blocka7e24c12009-10-30 11:49:00 +00004434 return Utils::ToLocal(result);
4435}
4436
4437
4438Local<String> v8::String::NewUndetectable(const uint16_t* data, int length) {
Steve Block44f0eee2011-05-26 01:26:41 +01004439 i::Isolate* isolate = i::Isolate::Current();
4440 EnsureInitializedForIsolate(isolate, "v8::String::NewUndetectable()");
4441 LOG_API(isolate, "String::NewUndetectable(uint16_)");
4442 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00004443 if (length == -1) length = TwoByteStringLength(data);
4444 i::Handle<i::String> result =
Steve Block44f0eee2011-05-26 01:26:41 +01004445 isolate->factory()->NewStringFromTwoByte(
4446 i::Vector<const uint16_t>(data, length));
Steve Blocka7e24c12009-10-30 11:49:00 +00004447 result->MarkAsUndetectable();
4448 return Utils::ToLocal(result);
4449}
4450
4451
Steve Block44f0eee2011-05-26 01:26:41 +01004452i::Handle<i::String> NewExternalStringHandle(i::Isolate* isolate,
Steve Blocka7e24c12009-10-30 11:49:00 +00004453 v8::String::ExternalStringResource* resource) {
4454 i::Handle<i::String> result =
Steve Block44f0eee2011-05-26 01:26:41 +01004455 isolate->factory()->NewExternalStringFromTwoByte(resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00004456 return result;
4457}
4458
4459
Steve Block44f0eee2011-05-26 01:26:41 +01004460i::Handle<i::String> NewExternalAsciiStringHandle(i::Isolate* isolate,
Steve Blocka7e24c12009-10-30 11:49:00 +00004461 v8::String::ExternalAsciiStringResource* resource) {
4462 i::Handle<i::String> result =
Steve Block44f0eee2011-05-26 01:26:41 +01004463 isolate->factory()->NewExternalStringFromAscii(resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00004464 return result;
4465}
4466
4467
Steve Blocka7e24c12009-10-30 11:49:00 +00004468Local<String> v8::String::NewExternal(
4469 v8::String::ExternalStringResource* resource) {
Steve Block44f0eee2011-05-26 01:26:41 +01004470 i::Isolate* isolate = i::Isolate::Current();
4471 EnsureInitializedForIsolate(isolate, "v8::String::NewExternal()");
4472 LOG_API(isolate, "String::NewExternal");
4473 ENTER_V8(isolate);
4474 i::Handle<i::String> result = NewExternalStringHandle(isolate, resource);
4475 isolate->heap()->external_string_table()->AddString(*result);
Steve Blocka7e24c12009-10-30 11:49:00 +00004476 return Utils::ToLocal(result);
4477}
4478
4479
4480bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004481 i::Handle<i::String> obj = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01004482 i::Isolate* isolate = obj->GetIsolate();
4483 if (IsDeadCheck(isolate, "v8::String::MakeExternal()")) return false;
4484 if (i::StringShape(*obj).IsExternalTwoByte()) {
4485 return false; // Already an external string.
4486 }
4487 ENTER_V8(isolate);
4488 if (isolate->string_tracker()->IsFreshUnusedString(obj)) {
4489 return false;
4490 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004491 if (isolate->heap()->IsInGCPostProcessing()) {
4492 return false;
4493 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004494 bool result = obj->MakeExternal(resource);
4495 if (result && !obj->IsSymbol()) {
Steve Block44f0eee2011-05-26 01:26:41 +01004496 isolate->heap()->external_string_table()->AddString(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00004497 }
4498 return result;
4499}
4500
4501
4502Local<String> v8::String::NewExternal(
4503 v8::String::ExternalAsciiStringResource* resource) {
Steve Block44f0eee2011-05-26 01:26:41 +01004504 i::Isolate* isolate = i::Isolate::Current();
4505 EnsureInitializedForIsolate(isolate, "v8::String::NewExternal()");
4506 LOG_API(isolate, "String::NewExternal");
4507 ENTER_V8(isolate);
4508 i::Handle<i::String> result = NewExternalAsciiStringHandle(isolate, resource);
4509 isolate->heap()->external_string_table()->AddString(*result);
Steve Blocka7e24c12009-10-30 11:49:00 +00004510 return Utils::ToLocal(result);
4511}
4512
4513
4514bool v8::String::MakeExternal(
4515 v8::String::ExternalAsciiStringResource* resource) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004516 i::Handle<i::String> obj = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01004517 i::Isolate* isolate = obj->GetIsolate();
4518 if (IsDeadCheck(isolate, "v8::String::MakeExternal()")) return false;
4519 if (i::StringShape(*obj).IsExternalTwoByte()) {
4520 return false; // Already an external string.
4521 }
4522 ENTER_V8(isolate);
4523 if (isolate->string_tracker()->IsFreshUnusedString(obj)) {
4524 return false;
4525 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004526 if (isolate->heap()->IsInGCPostProcessing()) {
4527 return false;
4528 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004529 bool result = obj->MakeExternal(resource);
4530 if (result && !obj->IsSymbol()) {
Steve Block44f0eee2011-05-26 01:26:41 +01004531 isolate->heap()->external_string_table()->AddString(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00004532 }
4533 return result;
4534}
4535
4536
4537bool v8::String::CanMakeExternal() {
Steve Blocka7e24c12009-10-30 11:49:00 +00004538 i::Handle<i::String> obj = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01004539 i::Isolate* isolate = obj->GetIsolate();
4540 if (IsDeadCheck(isolate, "v8::String::CanMakeExternal()")) return false;
4541 if (isolate->string_tracker()->IsFreshUnusedString(obj)) {
4542 return false;
4543 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004544 int size = obj->Size(); // Byte size of the original string.
4545 if (size < i::ExternalString::kSize)
4546 return false;
4547 i::StringShape shape(*obj);
4548 return !shape.IsExternal();
4549}
4550
4551
4552Local<v8::Object> v8::Object::New() {
Steve Block44f0eee2011-05-26 01:26:41 +01004553 i::Isolate* isolate = i::Isolate::Current();
4554 EnsureInitializedForIsolate(isolate, "v8::Object::New()");
4555 LOG_API(isolate, "Object::New");
4556 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00004557 i::Handle<i::JSObject> obj =
Steve Block44f0eee2011-05-26 01:26:41 +01004558 isolate->factory()->NewJSObject(isolate->object_function());
Steve Blocka7e24c12009-10-30 11:49:00 +00004559 return Utils::ToLocal(obj);
4560}
4561
4562
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004563Local<v8::Value> v8::NumberObject::New(double value) {
4564 i::Isolate* isolate = i::Isolate::Current();
4565 EnsureInitializedForIsolate(isolate, "v8::NumberObject::New()");
4566 LOG_API(isolate, "NumberObject::New");
4567 ENTER_V8(isolate);
4568 i::Handle<i::Object> number = isolate->factory()->NewNumber(value);
4569 i::Handle<i::Object> obj = isolate->factory()->ToObject(number);
4570 return Utils::ToLocal(obj);
4571}
4572
4573
4574double v8::NumberObject::NumberValue() const {
4575 i::Isolate* isolate = i::Isolate::Current();
4576 if (IsDeadCheck(isolate, "v8::NumberObject::NumberValue()")) return 0;
4577 LOG_API(isolate, "NumberObject::NumberValue");
4578 i::Handle<i::Object> obj = Utils::OpenHandle(this);
4579 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
4580 return jsvalue->value()->Number();
4581}
4582
4583
4584Local<v8::Value> v8::BooleanObject::New(bool value) {
4585 i::Isolate* isolate = i::Isolate::Current();
4586 EnsureInitializedForIsolate(isolate, "v8::BooleanObject::New()");
4587 LOG_API(isolate, "BooleanObject::New");
4588 ENTER_V8(isolate);
4589 i::Handle<i::Object> boolean(value ? isolate->heap()->true_value()
4590 : isolate->heap()->false_value());
4591 i::Handle<i::Object> obj = isolate->factory()->ToObject(boolean);
4592 return Utils::ToLocal(obj);
4593}
4594
4595
4596bool v8::BooleanObject::BooleanValue() const {
4597 i::Isolate* isolate = i::Isolate::Current();
4598 if (IsDeadCheck(isolate, "v8::BooleanObject::BooleanValue()")) return 0;
4599 LOG_API(isolate, "BooleanObject::BooleanValue");
4600 i::Handle<i::Object> obj = Utils::OpenHandle(this);
4601 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
4602 return jsvalue->value()->IsTrue();
4603}
4604
4605
4606Local<v8::Value> v8::StringObject::New(Handle<String> value) {
4607 i::Isolate* isolate = i::Isolate::Current();
4608 EnsureInitializedForIsolate(isolate, "v8::StringObject::New()");
4609 LOG_API(isolate, "StringObject::New");
4610 ENTER_V8(isolate);
4611 i::Handle<i::Object> obj =
4612 isolate->factory()->ToObject(Utils::OpenHandle(*value));
4613 return Utils::ToLocal(obj);
4614}
4615
4616
4617Local<v8::String> v8::StringObject::StringValue() const {
4618 i::Isolate* isolate = i::Isolate::Current();
4619 if (IsDeadCheck(isolate, "v8::StringObject::StringValue()")) {
4620 return Local<v8::String>();
4621 }
4622 LOG_API(isolate, "StringObject::StringValue");
4623 i::Handle<i::Object> obj = Utils::OpenHandle(this);
4624 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
4625 return Utils::ToLocal(
4626 i::Handle<i::String>(i::String::cast(jsvalue->value())));
4627}
4628
4629
Steve Blocka7e24c12009-10-30 11:49:00 +00004630Local<v8::Value> v8::Date::New(double time) {
Steve Block44f0eee2011-05-26 01:26:41 +01004631 i::Isolate* isolate = i::Isolate::Current();
4632 EnsureInitializedForIsolate(isolate, "v8::Date::New()");
4633 LOG_API(isolate, "Date::New");
Steve Blockd0582a62009-12-15 09:54:21 +00004634 if (isnan(time)) {
4635 // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
4636 time = i::OS::nan_value();
4637 }
Steve Block44f0eee2011-05-26 01:26:41 +01004638 ENTER_V8(isolate);
4639 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00004640 i::Handle<i::Object> obj =
4641 i::Execution::NewDate(time, &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01004642 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +00004643 return Utils::ToLocal(obj);
4644}
4645
4646
4647double v8::Date::NumberValue() const {
Steve Block44f0eee2011-05-26 01:26:41 +01004648 i::Isolate* isolate = i::Isolate::Current();
4649 if (IsDeadCheck(isolate, "v8::Date::NumberValue()")) return 0;
4650 LOG_API(isolate, "Date::NumberValue");
Steve Blocka7e24c12009-10-30 11:49:00 +00004651 i::Handle<i::Object> obj = Utils::OpenHandle(this);
4652 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
4653 return jsvalue->value()->Number();
4654}
4655
4656
Ben Murdochb0fe1622011-05-05 13:52:32 +01004657void v8::Date::DateTimeConfigurationChangeNotification() {
Steve Block44f0eee2011-05-26 01:26:41 +01004658 i::Isolate* isolate = i::Isolate::Current();
4659 ON_BAILOUT(isolate, "v8::Date::DateTimeConfigurationChangeNotification()",
4660 return);
4661 LOG_API(isolate, "Date::DateTimeConfigurationChangeNotification");
4662 ENTER_V8(isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004663
Steve Block44f0eee2011-05-26 01:26:41 +01004664 i::HandleScope scope(isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004665 // Get the function ResetDateCache (defined in date-delay.js).
4666 i::Handle<i::String> func_name_str =
Steve Block44f0eee2011-05-26 01:26:41 +01004667 isolate->factory()->LookupAsciiSymbol("ResetDateCache");
4668 i::MaybeObject* result =
4669 isolate->js_builtins_object()->GetProperty(*func_name_str);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004670 i::Object* object_func;
4671 if (!result->ToObject(&object_func)) {
4672 return;
4673 }
4674
4675 if (object_func->IsJSFunction()) {
4676 i::Handle<i::JSFunction> func =
4677 i::Handle<i::JSFunction>(i::JSFunction::cast(object_func));
4678
4679 // Call ResetDateCache(0 but expect no exceptions:
4680 bool caught_exception = false;
Ben Murdoch8b112d22011-06-08 16:22:53 +01004681 i::Execution::TryCall(func,
4682 isolate->js_builtins_object(),
4683 0,
4684 NULL,
4685 &caught_exception);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004686 }
4687}
4688
4689
Ben Murdochf87a2032010-10-22 12:50:53 +01004690static i::Handle<i::String> RegExpFlagsToString(RegExp::Flags flags) {
4691 char flags_buf[3];
4692 int num_flags = 0;
4693 if ((flags & RegExp::kGlobal) != 0) flags_buf[num_flags++] = 'g';
4694 if ((flags & RegExp::kMultiline) != 0) flags_buf[num_flags++] = 'm';
4695 if ((flags & RegExp::kIgnoreCase) != 0) flags_buf[num_flags++] = 'i';
4696 ASSERT(num_flags <= static_cast<int>(ARRAY_SIZE(flags_buf)));
Steve Block44f0eee2011-05-26 01:26:41 +01004697 return FACTORY->LookupSymbol(
Ben Murdochf87a2032010-10-22 12:50:53 +01004698 i::Vector<const char>(flags_buf, num_flags));
4699}
4700
4701
4702Local<v8::RegExp> v8::RegExp::New(Handle<String> pattern,
4703 Flags flags) {
Steve Block44f0eee2011-05-26 01:26:41 +01004704 i::Isolate* isolate = Utils::OpenHandle(*pattern)->GetIsolate();
4705 EnsureInitializedForIsolate(isolate, "v8::RegExp::New()");
4706 LOG_API(isolate, "RegExp::New");
4707 ENTER_V8(isolate);
4708 EXCEPTION_PREAMBLE(isolate);
Ben Murdochf87a2032010-10-22 12:50:53 +01004709 i::Handle<i::JSRegExp> obj = i::Execution::NewJSRegExp(
4710 Utils::OpenHandle(*pattern),
4711 RegExpFlagsToString(flags),
4712 &has_pending_exception);
Steve Block44f0eee2011-05-26 01:26:41 +01004713 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::RegExp>());
Ben Murdochf87a2032010-10-22 12:50:53 +01004714 return Utils::ToLocal(i::Handle<i::JSRegExp>::cast(obj));
4715}
4716
4717
4718Local<v8::String> v8::RegExp::GetSource() const {
Steve Block44f0eee2011-05-26 01:26:41 +01004719 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4720 if (IsDeadCheck(isolate, "v8::RegExp::GetSource()")) {
4721 return Local<v8::String>();
4722 }
Ben Murdochf87a2032010-10-22 12:50:53 +01004723 i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
4724 return Utils::ToLocal(i::Handle<i::String>(obj->Pattern()));
4725}
4726
4727
4728// Assert that the static flags cast in GetFlags is valid.
4729#define REGEXP_FLAG_ASSERT_EQ(api_flag, internal_flag) \
4730 STATIC_ASSERT(static_cast<int>(v8::RegExp::api_flag) == \
4731 static_cast<int>(i::JSRegExp::internal_flag))
4732REGEXP_FLAG_ASSERT_EQ(kNone, NONE);
4733REGEXP_FLAG_ASSERT_EQ(kGlobal, GLOBAL);
4734REGEXP_FLAG_ASSERT_EQ(kIgnoreCase, IGNORE_CASE);
4735REGEXP_FLAG_ASSERT_EQ(kMultiline, MULTILINE);
4736#undef REGEXP_FLAG_ASSERT_EQ
4737
4738v8::RegExp::Flags v8::RegExp::GetFlags() const {
Steve Block44f0eee2011-05-26 01:26:41 +01004739 if (IsDeadCheck(i::Isolate::Current(), "v8::RegExp::GetFlags()")) {
4740 return v8::RegExp::kNone;
4741 }
Ben Murdochf87a2032010-10-22 12:50:53 +01004742 i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
4743 return static_cast<RegExp::Flags>(obj->GetFlags().value());
4744}
4745
4746
Steve Blocka7e24c12009-10-30 11:49:00 +00004747Local<v8::Array> v8::Array::New(int length) {
Steve Block44f0eee2011-05-26 01:26:41 +01004748 i::Isolate* isolate = i::Isolate::Current();
4749 EnsureInitializedForIsolate(isolate, "v8::Array::New()");
4750 LOG_API(isolate, "Array::New");
4751 ENTER_V8(isolate);
4752 int real_length = length > 0 ? length : 0;
4753 i::Handle<i::JSArray> obj = isolate->factory()->NewJSArray(real_length);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004754 i::Handle<i::Object> length_obj =
4755 isolate->factory()->NewNumberFromInt(real_length);
4756 obj->set_length(*length_obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00004757 return Utils::ToLocal(obj);
4758}
4759
4760
4761uint32_t v8::Array::Length() const {
Steve Block44f0eee2011-05-26 01:26:41 +01004762 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4763 if (IsDeadCheck(isolate, "v8::Array::Length()")) return 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00004764 i::Handle<i::JSArray> obj = Utils::OpenHandle(this);
4765 i::Object* length = obj->length();
4766 if (length->IsSmi()) {
4767 return i::Smi::cast(length)->value();
4768 } else {
4769 return static_cast<uint32_t>(length->Number());
4770 }
4771}
4772
4773
4774Local<Object> Array::CloneElementAt(uint32_t index) {
Steve Block44f0eee2011-05-26 01:26:41 +01004775 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4776 ON_BAILOUT(isolate, "v8::Array::CloneElementAt()", return Local<Object>());
Steve Blocka7e24c12009-10-30 11:49:00 +00004777 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
4778 if (!self->HasFastElements()) {
4779 return Local<Object>();
4780 }
4781 i::FixedArray* elms = i::FixedArray::cast(self->elements());
4782 i::Object* paragon = elms->get(index);
4783 if (!paragon->IsJSObject()) {
4784 return Local<Object>();
4785 }
4786 i::Handle<i::JSObject> paragon_handle(i::JSObject::cast(paragon));
Steve Block44f0eee2011-05-26 01:26:41 +01004787 EXCEPTION_PREAMBLE(isolate);
4788 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00004789 i::Handle<i::JSObject> result = i::Copy(paragon_handle);
4790 has_pending_exception = result.is_null();
Steve Block44f0eee2011-05-26 01:26:41 +01004791 EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>());
Steve Blocka7e24c12009-10-30 11:49:00 +00004792 return Utils::ToLocal(result);
4793}
4794
4795
4796Local<String> v8::String::NewSymbol(const char* data, int length) {
Steve Block44f0eee2011-05-26 01:26:41 +01004797 i::Isolate* isolate = i::Isolate::Current();
4798 EnsureInitializedForIsolate(isolate, "v8::String::NewSymbol()");
4799 LOG_API(isolate, "String::NewSymbol(char)");
4800 ENTER_V8(isolate);
Steve Blockd0582a62009-12-15 09:54:21 +00004801 if (length == -1) length = i::StrLength(data);
Steve Blocka7e24c12009-10-30 11:49:00 +00004802 i::Handle<i::String> result =
Steve Block44f0eee2011-05-26 01:26:41 +01004803 isolate->factory()->LookupSymbol(i::Vector<const char>(data, length));
Steve Blocka7e24c12009-10-30 11:49:00 +00004804 return Utils::ToLocal(result);
4805}
4806
4807
4808Local<Number> v8::Number::New(double value) {
Steve Block44f0eee2011-05-26 01:26:41 +01004809 i::Isolate* isolate = i::Isolate::Current();
4810 EnsureInitializedForIsolate(isolate, "v8::Number::New()");
Steve Blockd0582a62009-12-15 09:54:21 +00004811 if (isnan(value)) {
4812 // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
4813 value = i::OS::nan_value();
4814 }
Steve Block44f0eee2011-05-26 01:26:41 +01004815 ENTER_V8(isolate);
4816 i::Handle<i::Object> result = isolate->factory()->NewNumber(value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004817 return Utils::NumberToLocal(result);
4818}
4819
4820
4821Local<Integer> v8::Integer::New(int32_t value) {
Steve Block44f0eee2011-05-26 01:26:41 +01004822 i::Isolate* isolate = i::Isolate::UncheckedCurrent();
4823 EnsureInitializedForIsolate(isolate, "v8::Integer::New()");
Steve Blocka7e24c12009-10-30 11:49:00 +00004824 if (i::Smi::IsValid(value)) {
Steve Block44f0eee2011-05-26 01:26:41 +01004825 return Utils::IntegerToLocal(i::Handle<i::Object>(i::Smi::FromInt(value),
4826 isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +00004827 }
Steve Block44f0eee2011-05-26 01:26:41 +01004828 ENTER_V8(isolate);
4829 i::Handle<i::Object> result = isolate->factory()->NewNumber(value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004830 return Utils::IntegerToLocal(result);
4831}
4832
4833
Steve Block3ce2e202009-11-05 08:53:23 +00004834Local<Integer> Integer::NewFromUnsigned(uint32_t value) {
4835 bool fits_into_int32_t = (value & (1 << 31)) == 0;
4836 if (fits_into_int32_t) {
4837 return Integer::New(static_cast<int32_t>(value));
4838 }
Steve Block44f0eee2011-05-26 01:26:41 +01004839 i::Isolate* isolate = i::Isolate::Current();
4840 ENTER_V8(isolate);
4841 i::Handle<i::Object> result = isolate->factory()->NewNumber(value);
Steve Block3ce2e202009-11-05 08:53:23 +00004842 return Utils::IntegerToLocal(result);
4843}
4844
4845
Steve Blocka7e24c12009-10-30 11:49:00 +00004846void V8::IgnoreOutOfMemoryException() {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004847 EnterIsolateIfNeeded()->set_ignore_out_of_memory(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00004848}
4849
4850
4851bool V8::AddMessageListener(MessageCallback that, Handle<Value> data) {
Steve Block44f0eee2011-05-26 01:26:41 +01004852 i::Isolate* isolate = i::Isolate::Current();
4853 EnsureInitializedForIsolate(isolate, "v8::V8::AddMessageListener()");
4854 ON_BAILOUT(isolate, "v8::V8::AddMessageListener()", return false);
4855 ENTER_V8(isolate);
4856 i::HandleScope scope(isolate);
4857 NeanderArray listeners(isolate->factory()->message_listeners());
Steve Blocka7e24c12009-10-30 11:49:00 +00004858 NeanderObject obj(2);
Ben Murdoch257744e2011-11-30 15:57:28 +00004859 obj.set(0, *isolate->factory()->NewForeign(FUNCTION_ADDR(that)));
Steve Blocka7e24c12009-10-30 11:49:00 +00004860 obj.set(1, data.IsEmpty() ?
Steve Block44f0eee2011-05-26 01:26:41 +01004861 isolate->heap()->undefined_value() :
Steve Blocka7e24c12009-10-30 11:49:00 +00004862 *Utils::OpenHandle(*data));
4863 listeners.add(obj.value());
4864 return true;
4865}
4866
4867
4868void V8::RemoveMessageListeners(MessageCallback that) {
Steve Block44f0eee2011-05-26 01:26:41 +01004869 i::Isolate* isolate = i::Isolate::Current();
4870 EnsureInitializedForIsolate(isolate, "v8::V8::RemoveMessageListener()");
4871 ON_BAILOUT(isolate, "v8::V8::RemoveMessageListeners()", return);
4872 ENTER_V8(isolate);
4873 i::HandleScope scope(isolate);
4874 NeanderArray listeners(isolate->factory()->message_listeners());
Steve Blocka7e24c12009-10-30 11:49:00 +00004875 for (int i = 0; i < listeners.length(); i++) {
4876 if (listeners.get(i)->IsUndefined()) continue; // skip deleted ones
4877
4878 NeanderObject listener(i::JSObject::cast(listeners.get(i)));
Ben Murdoch257744e2011-11-30 15:57:28 +00004879 i::Handle<i::Foreign> callback_obj(i::Foreign::cast(listener.get(0)));
4880 if (callback_obj->address() == FUNCTION_ADDR(that)) {
Steve Block44f0eee2011-05-26 01:26:41 +01004881 listeners.set(i, isolate->heap()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00004882 }
4883 }
4884}
4885
4886
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004887void V8::SetCaptureStackTraceForUncaughtExceptions(
4888 bool capture,
4889 int frame_limit,
4890 StackTrace::StackTraceOptions options) {
Steve Block44f0eee2011-05-26 01:26:41 +01004891 i::Isolate::Current()->SetCaptureStackTraceForUncaughtExceptions(
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004892 capture,
4893 frame_limit,
4894 options);
4895}
4896
4897
Steve Blocka7e24c12009-10-30 11:49:00 +00004898void V8::SetCounterFunction(CounterLookupCallback callback) {
Steve Block44f0eee2011-05-26 01:26:41 +01004899 i::Isolate* isolate = EnterIsolateIfNeeded();
4900 if (IsDeadCheck(isolate, "v8::V8::SetCounterFunction()")) return;
4901 isolate->stats_table()->SetCounterFunction(callback);
Steve Blocka7e24c12009-10-30 11:49:00 +00004902}
4903
4904void V8::SetCreateHistogramFunction(CreateHistogramCallback callback) {
Steve Block44f0eee2011-05-26 01:26:41 +01004905 i::Isolate* isolate = EnterIsolateIfNeeded();
4906 if (IsDeadCheck(isolate, "v8::V8::SetCreateHistogramFunction()")) return;
4907 isolate->stats_table()->SetCreateHistogramFunction(callback);
Steve Blocka7e24c12009-10-30 11:49:00 +00004908}
4909
4910void V8::SetAddHistogramSampleFunction(AddHistogramSampleCallback callback) {
Steve Block44f0eee2011-05-26 01:26:41 +01004911 i::Isolate* isolate = EnterIsolateIfNeeded();
4912 if (IsDeadCheck(isolate, "v8::V8::SetAddHistogramSampleFunction()")) return;
4913 isolate->stats_table()->
4914 SetAddHistogramSampleFunction(callback);
Steve Blocka7e24c12009-10-30 11:49:00 +00004915}
4916
4917void V8::EnableSlidingStateWindow() {
Steve Block44f0eee2011-05-26 01:26:41 +01004918 i::Isolate* isolate = i::Isolate::Current();
4919 if (IsDeadCheck(isolate, "v8::V8::EnableSlidingStateWindow()")) return;
4920 isolate->logger()->EnableSlidingStateWindow();
Steve Blocka7e24c12009-10-30 11:49:00 +00004921}
4922
4923
4924void V8::SetFailedAccessCheckCallbackFunction(
4925 FailedAccessCheckCallback callback) {
Steve Block44f0eee2011-05-26 01:26:41 +01004926 i::Isolate* isolate = i::Isolate::Current();
4927 if (IsDeadCheck(isolate, "v8::V8::SetFailedAccessCheckCallbackFunction()")) {
4928 return;
4929 }
4930 isolate->SetFailedAccessCheckCallback(callback);
4931}
4932
4933void V8::AddObjectGroup(Persistent<Value>* objects,
4934 size_t length,
4935 RetainedObjectInfo* info) {
4936 i::Isolate* isolate = i::Isolate::Current();
4937 if (IsDeadCheck(isolate, "v8::V8::AddObjectGroup()")) return;
4938 STATIC_ASSERT(sizeof(Persistent<Value>) == sizeof(i::Object**));
4939 isolate->global_handles()->AddObjectGroup(
4940 reinterpret_cast<i::Object***>(objects), length, info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004941}
4942
4943
Steve Block44f0eee2011-05-26 01:26:41 +01004944void V8::AddImplicitReferences(Persistent<Object> parent,
4945 Persistent<Value>* children,
4946 size_t length) {
4947 i::Isolate* isolate = i::Isolate::Current();
4948 if (IsDeadCheck(isolate, "v8::V8::AddImplicitReferences()")) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00004949 STATIC_ASSERT(sizeof(Persistent<Value>) == sizeof(i::Object**));
Steve Block44f0eee2011-05-26 01:26:41 +01004950 isolate->global_handles()->AddImplicitReferences(
Ben Murdoch8b112d22011-06-08 16:22:53 +01004951 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(*parent)).location(),
Steve Block44f0eee2011-05-26 01:26:41 +01004952 reinterpret_cast<i::Object***>(children), length);
Steve Blocka7e24c12009-10-30 11:49:00 +00004953}
4954
4955
4956int V8::AdjustAmountOfExternalAllocatedMemory(int change_in_bytes) {
Steve Block44f0eee2011-05-26 01:26:41 +01004957 i::Isolate* isolate = i::Isolate::Current();
4958 if (IsDeadCheck(isolate, "v8::V8::AdjustAmountOfExternalAllocatedMemory()")) {
4959 return 0;
4960 }
4961 return isolate->heap()->AdjustAmountOfExternalAllocatedMemory(
4962 change_in_bytes);
Steve Blocka7e24c12009-10-30 11:49:00 +00004963}
4964
4965
4966void V8::SetGlobalGCPrologueCallback(GCCallback callback) {
Steve Block44f0eee2011-05-26 01:26:41 +01004967 i::Isolate* isolate = i::Isolate::Current();
4968 if (IsDeadCheck(isolate, "v8::V8::SetGlobalGCPrologueCallback()")) return;
4969 isolate->heap()->SetGlobalGCPrologueCallback(callback);
Steve Blocka7e24c12009-10-30 11:49:00 +00004970}
4971
4972
4973void V8::SetGlobalGCEpilogueCallback(GCCallback callback) {
Steve Block44f0eee2011-05-26 01:26:41 +01004974 i::Isolate* isolate = i::Isolate::Current();
4975 if (IsDeadCheck(isolate, "v8::V8::SetGlobalGCEpilogueCallback()")) return;
4976 isolate->heap()->SetGlobalGCEpilogueCallback(callback);
Steve Blocka7e24c12009-10-30 11:49:00 +00004977}
4978
4979
Steve Block6ded16b2010-05-10 14:33:55 +01004980void V8::AddGCPrologueCallback(GCPrologueCallback callback, GCType gc_type) {
Steve Block44f0eee2011-05-26 01:26:41 +01004981 i::Isolate* isolate = i::Isolate::Current();
4982 if (IsDeadCheck(isolate, "v8::V8::AddGCPrologueCallback()")) return;
4983 isolate->heap()->AddGCPrologueCallback(callback, gc_type);
Steve Block6ded16b2010-05-10 14:33:55 +01004984}
4985
4986
4987void V8::RemoveGCPrologueCallback(GCPrologueCallback callback) {
Steve Block44f0eee2011-05-26 01:26:41 +01004988 i::Isolate* isolate = i::Isolate::Current();
4989 if (IsDeadCheck(isolate, "v8::V8::RemoveGCPrologueCallback()")) return;
4990 isolate->heap()->RemoveGCPrologueCallback(callback);
Steve Block6ded16b2010-05-10 14:33:55 +01004991}
4992
4993
4994void V8::AddGCEpilogueCallback(GCEpilogueCallback callback, GCType gc_type) {
Steve Block44f0eee2011-05-26 01:26:41 +01004995 i::Isolate* isolate = i::Isolate::Current();
4996 if (IsDeadCheck(isolate, "v8::V8::AddGCEpilogueCallback()")) return;
4997 isolate->heap()->AddGCEpilogueCallback(callback, gc_type);
Steve Block6ded16b2010-05-10 14:33:55 +01004998}
4999
5000
5001void V8::RemoveGCEpilogueCallback(GCEpilogueCallback callback) {
Steve Block44f0eee2011-05-26 01:26:41 +01005002 i::Isolate* isolate = i::Isolate::Current();
5003 if (IsDeadCheck(isolate, "v8::V8::RemoveGCEpilogueCallback()")) return;
5004 isolate->heap()->RemoveGCEpilogueCallback(callback);
Steve Block6ded16b2010-05-10 14:33:55 +01005005}
5006
5007
Iain Merrick9ac36c92010-09-13 15:29:50 +01005008void V8::AddMemoryAllocationCallback(MemoryAllocationCallback callback,
5009 ObjectSpace space,
5010 AllocationAction action) {
Steve Block44f0eee2011-05-26 01:26:41 +01005011 i::Isolate* isolate = i::Isolate::Current();
5012 if (IsDeadCheck(isolate, "v8::V8::AddMemoryAllocationCallback()")) return;
5013 isolate->memory_allocator()->AddMemoryAllocationCallback(
5014 callback, space, action);
Iain Merrick9ac36c92010-09-13 15:29:50 +01005015}
5016
5017
5018void V8::RemoveMemoryAllocationCallback(MemoryAllocationCallback callback) {
Steve Block44f0eee2011-05-26 01:26:41 +01005019 i::Isolate* isolate = i::Isolate::Current();
5020 if (IsDeadCheck(isolate, "v8::V8::RemoveMemoryAllocationCallback()")) return;
5021 isolate->memory_allocator()->RemoveMemoryAllocationCallback(
5022 callback);
Iain Merrick9ac36c92010-09-13 15:29:50 +01005023}
5024
5025
Steve Blocka7e24c12009-10-30 11:49:00 +00005026void V8::PauseProfiler() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005027 i::Isolate* isolate = i::Isolate::Current();
5028 isolate->logger()->PauseProfiler();
Steve Blocka7e24c12009-10-30 11:49:00 +00005029}
5030
5031
5032void V8::ResumeProfiler() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005033 i::Isolate* isolate = i::Isolate::Current();
5034 isolate->logger()->ResumeProfiler();
Steve Blocka7e24c12009-10-30 11:49:00 +00005035}
5036
5037
5038bool V8::IsProfilerPaused() {
Steve Block44f0eee2011-05-26 01:26:41 +01005039 i::Isolate* isolate = i::Isolate::Current();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005040 return isolate->logger()->IsProfilerPaused();
Steve Blocka7e24c12009-10-30 11:49:00 +00005041}
5042
5043
5044int V8::GetCurrentThreadId() {
Steve Block44f0eee2011-05-26 01:26:41 +01005045 i::Isolate* isolate = i::Isolate::Current();
5046 EnsureInitializedForIsolate(isolate, "V8::GetCurrentThreadId()");
Ben Murdoch8b112d22011-06-08 16:22:53 +01005047 return isolate->thread_id().ToInteger();
Steve Blocka7e24c12009-10-30 11:49:00 +00005048}
5049
5050
5051void V8::TerminateExecution(int thread_id) {
Steve Block44f0eee2011-05-26 01:26:41 +01005052 i::Isolate* isolate = i::Isolate::Current();
5053 if (!isolate->IsInitialized()) return;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005054 API_ENTRY_CHECK(isolate, "V8::TerminateExecution()");
Steve Blocka7e24c12009-10-30 11:49:00 +00005055 // If the thread_id identifies the current thread just terminate
5056 // execution right away. Otherwise, ask the thread manager to
5057 // terminate the thread with the given id if any.
Ben Murdoch8b112d22011-06-08 16:22:53 +01005058 i::ThreadId internal_tid = i::ThreadId::FromInteger(thread_id);
5059 if (isolate->thread_id().Equals(internal_tid)) {
Steve Block44f0eee2011-05-26 01:26:41 +01005060 isolate->stack_guard()->TerminateExecution();
Steve Blocka7e24c12009-10-30 11:49:00 +00005061 } else {
Ben Murdoch8b112d22011-06-08 16:22:53 +01005062 isolate->thread_manager()->TerminateExecution(internal_tid);
Steve Blocka7e24c12009-10-30 11:49:00 +00005063 }
5064}
5065
5066
Steve Block44f0eee2011-05-26 01:26:41 +01005067void V8::TerminateExecution(Isolate* isolate) {
5068 // If no isolate is supplied, use the default isolate.
5069 if (isolate != NULL) {
5070 reinterpret_cast<i::Isolate*>(isolate)->stack_guard()->TerminateExecution();
5071 } else {
5072 i::Isolate::GetDefaultIsolateStackGuard()->TerminateExecution();
5073 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005074}
5075
5076
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005077bool V8::IsExecutionTerminating(Isolate* isolate) {
5078 i::Isolate* i_isolate = isolate != NULL ?
5079 reinterpret_cast<i::Isolate*>(isolate) : i::Isolate::Current();
5080 return IsExecutionTerminatingCheck(i_isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01005081}
5082
5083
5084Isolate* Isolate::GetCurrent() {
5085 i::Isolate* isolate = i::Isolate::UncheckedCurrent();
5086 return reinterpret_cast<Isolate*>(isolate);
5087}
5088
5089
5090Isolate* Isolate::New() {
5091 i::Isolate* isolate = new i::Isolate();
5092 return reinterpret_cast<Isolate*>(isolate);
5093}
5094
5095
5096void Isolate::Dispose() {
5097 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
5098 if (!ApiCheck(!isolate->IsInUse(),
5099 "v8::Isolate::Dispose()",
5100 "Disposing the isolate that is entered by a thread.")) {
5101 return;
Steve Block6ded16b2010-05-10 14:33:55 +01005102 }
Steve Block44f0eee2011-05-26 01:26:41 +01005103 isolate->TearDown();
5104}
5105
5106
5107void Isolate::Enter() {
5108 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
5109 isolate->Enter();
5110}
5111
5112
5113void Isolate::Exit() {
5114 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
5115 isolate->Exit();
Steve Block6ded16b2010-05-10 14:33:55 +01005116}
5117
5118
Ben Murdoch257744e2011-11-30 15:57:28 +00005119void Isolate::SetData(void* data) {
5120 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
5121 isolate->SetData(data);
5122}
5123
5124void* Isolate::GetData() {
5125 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
5126 return isolate->GetData();
5127}
5128
5129
5130String::Utf8Value::Utf8Value(v8::Handle<v8::Value> obj)
5131 : str_(NULL), length_(0) {
Steve Block44f0eee2011-05-26 01:26:41 +01005132 i::Isolate* isolate = i::Isolate::Current();
5133 if (IsDeadCheck(isolate, "v8::String::Utf8Value::Utf8Value()")) return;
Ben Murdoch257744e2011-11-30 15:57:28 +00005134 if (obj.IsEmpty()) return;
Steve Block44f0eee2011-05-26 01:26:41 +01005135 ENTER_V8(isolate);
5136 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005137 TryCatch try_catch;
5138 Handle<String> str = obj->ToString();
Ben Murdoch257744e2011-11-30 15:57:28 +00005139 if (str.IsEmpty()) return;
5140 length_ = str->Utf8Length();
5141 str_ = i::NewArray<char>(length_ + 1);
5142 str->WriteUtf8(str_);
Steve Blocka7e24c12009-10-30 11:49:00 +00005143}
5144
5145
5146String::Utf8Value::~Utf8Value() {
5147 i::DeleteArray(str_);
5148}
5149
5150
Ben Murdoch257744e2011-11-30 15:57:28 +00005151String::AsciiValue::AsciiValue(v8::Handle<v8::Value> obj)
5152 : str_(NULL), length_(0) {
Steve Block44f0eee2011-05-26 01:26:41 +01005153 i::Isolate* isolate = i::Isolate::Current();
5154 if (IsDeadCheck(isolate, "v8::String::AsciiValue::AsciiValue()")) return;
Ben Murdoch257744e2011-11-30 15:57:28 +00005155 if (obj.IsEmpty()) return;
Steve Block44f0eee2011-05-26 01:26:41 +01005156 ENTER_V8(isolate);
5157 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005158 TryCatch try_catch;
5159 Handle<String> str = obj->ToString();
Ben Murdoch257744e2011-11-30 15:57:28 +00005160 if (str.IsEmpty()) return;
5161 length_ = str->Length();
5162 str_ = i::NewArray<char>(length_ + 1);
5163 str->WriteAscii(str_);
Steve Blocka7e24c12009-10-30 11:49:00 +00005164}
5165
5166
5167String::AsciiValue::~AsciiValue() {
5168 i::DeleteArray(str_);
5169}
5170
5171
Ben Murdoch257744e2011-11-30 15:57:28 +00005172String::Value::Value(v8::Handle<v8::Value> obj)
5173 : str_(NULL), length_(0) {
Steve Block44f0eee2011-05-26 01:26:41 +01005174 i::Isolate* isolate = i::Isolate::Current();
5175 if (IsDeadCheck(isolate, "v8::String::Value::Value()")) return;
Ben Murdoch257744e2011-11-30 15:57:28 +00005176 if (obj.IsEmpty()) return;
Steve Block44f0eee2011-05-26 01:26:41 +01005177 ENTER_V8(isolate);
5178 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005179 TryCatch try_catch;
5180 Handle<String> str = obj->ToString();
Ben Murdoch257744e2011-11-30 15:57:28 +00005181 if (str.IsEmpty()) return;
5182 length_ = str->Length();
5183 str_ = i::NewArray<uint16_t>(length_ + 1);
5184 str->Write(str_);
Steve Blocka7e24c12009-10-30 11:49:00 +00005185}
5186
5187
5188String::Value::~Value() {
5189 i::DeleteArray(str_);
5190}
5191
5192Local<Value> Exception::RangeError(v8::Handle<v8::String> raw_message) {
Steve Block44f0eee2011-05-26 01:26:41 +01005193 i::Isolate* isolate = i::Isolate::Current();
5194 LOG_API(isolate, "RangeError");
5195 ON_BAILOUT(isolate, "v8::Exception::RangeError()", return Local<Value>());
5196 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005197 i::Object* error;
5198 {
Steve Block44f0eee2011-05-26 01:26:41 +01005199 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005200 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
Steve Block44f0eee2011-05-26 01:26:41 +01005201 i::Handle<i::Object> result = isolate->factory()->NewRangeError(message);
Steve Blocka7e24c12009-10-30 11:49:00 +00005202 error = *result;
5203 }
5204 i::Handle<i::Object> result(error);
5205 return Utils::ToLocal(result);
5206}
5207
5208Local<Value> Exception::ReferenceError(v8::Handle<v8::String> raw_message) {
Steve Block44f0eee2011-05-26 01:26:41 +01005209 i::Isolate* isolate = i::Isolate::Current();
5210 LOG_API(isolate, "ReferenceError");
5211 ON_BAILOUT(isolate, "v8::Exception::ReferenceError()", return Local<Value>());
5212 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005213 i::Object* error;
5214 {
Steve Block44f0eee2011-05-26 01:26:41 +01005215 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005216 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
Steve Block44f0eee2011-05-26 01:26:41 +01005217 i::Handle<i::Object> result =
5218 isolate->factory()->NewReferenceError(message);
Steve Blocka7e24c12009-10-30 11:49:00 +00005219 error = *result;
5220 }
5221 i::Handle<i::Object> result(error);
5222 return Utils::ToLocal(result);
5223}
5224
5225Local<Value> Exception::SyntaxError(v8::Handle<v8::String> raw_message) {
Steve Block44f0eee2011-05-26 01:26:41 +01005226 i::Isolate* isolate = i::Isolate::Current();
5227 LOG_API(isolate, "SyntaxError");
5228 ON_BAILOUT(isolate, "v8::Exception::SyntaxError()", return Local<Value>());
5229 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005230 i::Object* error;
5231 {
Steve Block44f0eee2011-05-26 01:26:41 +01005232 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005233 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
Steve Block44f0eee2011-05-26 01:26:41 +01005234 i::Handle<i::Object> result = isolate->factory()->NewSyntaxError(message);
Steve Blocka7e24c12009-10-30 11:49:00 +00005235 error = *result;
5236 }
5237 i::Handle<i::Object> result(error);
5238 return Utils::ToLocal(result);
5239}
5240
5241Local<Value> Exception::TypeError(v8::Handle<v8::String> raw_message) {
Steve Block44f0eee2011-05-26 01:26:41 +01005242 i::Isolate* isolate = i::Isolate::Current();
5243 LOG_API(isolate, "TypeError");
5244 ON_BAILOUT(isolate, "v8::Exception::TypeError()", return Local<Value>());
5245 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005246 i::Object* error;
5247 {
Steve Block44f0eee2011-05-26 01:26:41 +01005248 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005249 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
Steve Block44f0eee2011-05-26 01:26:41 +01005250 i::Handle<i::Object> result = isolate->factory()->NewTypeError(message);
Steve Blocka7e24c12009-10-30 11:49:00 +00005251 error = *result;
5252 }
5253 i::Handle<i::Object> result(error);
5254 return Utils::ToLocal(result);
5255}
5256
5257Local<Value> Exception::Error(v8::Handle<v8::String> raw_message) {
Steve Block44f0eee2011-05-26 01:26:41 +01005258 i::Isolate* isolate = i::Isolate::Current();
5259 LOG_API(isolate, "Error");
5260 ON_BAILOUT(isolate, "v8::Exception::Error()", return Local<Value>());
5261 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005262 i::Object* error;
5263 {
Steve Block44f0eee2011-05-26 01:26:41 +01005264 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005265 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
Steve Block44f0eee2011-05-26 01:26:41 +01005266 i::Handle<i::Object> result = isolate->factory()->NewError(message);
Steve Blocka7e24c12009-10-30 11:49:00 +00005267 error = *result;
5268 }
5269 i::Handle<i::Object> result(error);
5270 return Utils::ToLocal(result);
5271}
5272
5273
5274// --- D e b u g S u p p o r t ---
5275
5276#ifdef ENABLE_DEBUGGER_SUPPORT
Leon Clarkef7060e22010-06-03 12:02:55 +01005277
Leon Clarkef7060e22010-06-03 12:02:55 +01005278static void EventCallbackWrapper(const v8::Debug::EventDetails& event_details) {
Steve Block44f0eee2011-05-26 01:26:41 +01005279 i::Isolate* isolate = i::Isolate::Current();
5280 if (isolate->debug_event_callback() != NULL) {
5281 isolate->debug_event_callback()(event_details.GetEvent(),
5282 event_details.GetExecutionState(),
5283 event_details.GetEventData(),
5284 event_details.GetCallbackData());
Leon Clarkef7060e22010-06-03 12:02:55 +01005285 }
5286}
5287
5288
Steve Blocka7e24c12009-10-30 11:49:00 +00005289bool Debug::SetDebugEventListener(EventCallback that, Handle<Value> data) {
Steve Block44f0eee2011-05-26 01:26:41 +01005290 i::Isolate* isolate = i::Isolate::Current();
5291 EnsureInitializedForIsolate(isolate, "v8::Debug::SetDebugEventListener()");
5292 ON_BAILOUT(isolate, "v8::Debug::SetDebugEventListener()", return false);
5293 ENTER_V8(isolate);
Leon Clarkef7060e22010-06-03 12:02:55 +01005294
Steve Block44f0eee2011-05-26 01:26:41 +01005295 isolate->set_debug_event_callback(that);
Leon Clarkef7060e22010-06-03 12:02:55 +01005296
Steve Block44f0eee2011-05-26 01:26:41 +01005297 i::HandleScope scope(isolate);
Ben Murdoch257744e2011-11-30 15:57:28 +00005298 i::Handle<i::Object> foreign = isolate->factory()->undefined_value();
Leon Clarkef7060e22010-06-03 12:02:55 +01005299 if (that != NULL) {
Ben Murdoch257744e2011-11-30 15:57:28 +00005300 foreign =
5301 isolate->factory()->NewForeign(FUNCTION_ADDR(EventCallbackWrapper));
Leon Clarkef7060e22010-06-03 12:02:55 +01005302 }
Ben Murdoch257744e2011-11-30 15:57:28 +00005303 isolate->debugger()->SetEventListener(foreign, Utils::OpenHandle(*data));
Leon Clarkef7060e22010-06-03 12:02:55 +01005304 return true;
5305}
5306
5307
5308bool Debug::SetDebugEventListener2(EventCallback2 that, Handle<Value> data) {
Steve Block44f0eee2011-05-26 01:26:41 +01005309 i::Isolate* isolate = i::Isolate::Current();
5310 EnsureInitializedForIsolate(isolate, "v8::Debug::SetDebugEventListener2()");
5311 ON_BAILOUT(isolate, "v8::Debug::SetDebugEventListener2()", return false);
5312 ENTER_V8(isolate);
5313 i::HandleScope scope(isolate);
Ben Murdoch257744e2011-11-30 15:57:28 +00005314 i::Handle<i::Object> foreign = isolate->factory()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00005315 if (that != NULL) {
Ben Murdoch257744e2011-11-30 15:57:28 +00005316 foreign = isolate->factory()->NewForeign(FUNCTION_ADDR(that));
Steve Blocka7e24c12009-10-30 11:49:00 +00005317 }
Ben Murdoch257744e2011-11-30 15:57:28 +00005318 isolate->debugger()->SetEventListener(foreign, Utils::OpenHandle(*data));
Steve Blocka7e24c12009-10-30 11:49:00 +00005319 return true;
5320}
5321
5322
5323bool Debug::SetDebugEventListener(v8::Handle<v8::Object> that,
5324 Handle<Value> data) {
Steve Block44f0eee2011-05-26 01:26:41 +01005325 i::Isolate* isolate = i::Isolate::Current();
5326 ON_BAILOUT(isolate, "v8::Debug::SetDebugEventListener()", return false);
5327 ENTER_V8(isolate);
5328 isolate->debugger()->SetEventListener(Utils::OpenHandle(*that),
5329 Utils::OpenHandle(*data));
Steve Blocka7e24c12009-10-30 11:49:00 +00005330 return true;
5331}
5332
5333
Steve Block44f0eee2011-05-26 01:26:41 +01005334void Debug::DebugBreak(Isolate* isolate) {
5335 // If no isolate is supplied, use the default isolate.
5336 if (isolate != NULL) {
5337 reinterpret_cast<i::Isolate*>(isolate)->stack_guard()->DebugBreak();
5338 } else {
5339 i::Isolate::GetDefaultIsolateStackGuard()->DebugBreak();
5340 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005341}
5342
5343
Steve Block44f0eee2011-05-26 01:26:41 +01005344void Debug::CancelDebugBreak(Isolate* isolate) {
5345 // If no isolate is supplied, use the default isolate.
5346 if (isolate != NULL) {
5347 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
5348 internal_isolate->stack_guard()->Continue(i::DEBUGBREAK);
5349 } else {
5350 i::Isolate::GetDefaultIsolateStackGuard()->Continue(i::DEBUGBREAK);
5351 }
Ben Murdochf87a2032010-10-22 12:50:53 +01005352}
5353
5354
Steve Block44f0eee2011-05-26 01:26:41 +01005355void Debug::DebugBreakForCommand(ClientData* data, Isolate* isolate) {
5356 // If no isolate is supplied, use the default isolate.
5357 if (isolate != NULL) {
5358 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
5359 internal_isolate->debugger()->EnqueueDebugCommand(data);
5360 } else {
5361 i::Isolate::GetDefaultIsolateDebugger()->EnqueueDebugCommand(data);
5362 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +01005363}
5364
5365
Steve Blocka7e24c12009-10-30 11:49:00 +00005366static void MessageHandlerWrapper(const v8::Debug::Message& message) {
Steve Block44f0eee2011-05-26 01:26:41 +01005367 i::Isolate* isolate = i::Isolate::Current();
5368 if (isolate->message_handler()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005369 v8::String::Value json(message.GetJSON());
Steve Block44f0eee2011-05-26 01:26:41 +01005370 (isolate->message_handler())(*json, json.length(), message.GetClientData());
Steve Blocka7e24c12009-10-30 11:49:00 +00005371 }
5372}
5373
5374
5375void Debug::SetMessageHandler(v8::Debug::MessageHandler handler,
5376 bool message_handler_thread) {
Steve Block44f0eee2011-05-26 01:26:41 +01005377 i::Isolate* isolate = i::Isolate::Current();
5378 EnsureInitializedForIsolate(isolate, "v8::Debug::SetMessageHandler");
5379 ENTER_V8(isolate);
5380
Steve Blocka7e24c12009-10-30 11:49:00 +00005381 // Message handler thread not supported any more. Parameter temporally left in
Steve Block44f0eee2011-05-26 01:26:41 +01005382 // the API for client compatibility reasons.
Steve Blocka7e24c12009-10-30 11:49:00 +00005383 CHECK(!message_handler_thread);
5384
5385 // TODO(sgjesse) support the old message handler API through a simple wrapper.
Steve Block44f0eee2011-05-26 01:26:41 +01005386 isolate->set_message_handler(handler);
5387 if (handler != NULL) {
5388 isolate->debugger()->SetMessageHandler(MessageHandlerWrapper);
Steve Blocka7e24c12009-10-30 11:49:00 +00005389 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01005390 isolate->debugger()->SetMessageHandler(NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +00005391 }
5392}
5393
5394
5395void Debug::SetMessageHandler2(v8::Debug::MessageHandler2 handler) {
Steve Block44f0eee2011-05-26 01:26:41 +01005396 i::Isolate* isolate = i::Isolate::Current();
5397 EnsureInitializedForIsolate(isolate, "v8::Debug::SetMessageHandler");
5398 ENTER_V8(isolate);
5399 isolate->debugger()->SetMessageHandler(handler);
Steve Blocka7e24c12009-10-30 11:49:00 +00005400}
5401
5402
5403void Debug::SendCommand(const uint16_t* command, int length,
Steve Block44f0eee2011-05-26 01:26:41 +01005404 ClientData* client_data,
5405 Isolate* isolate) {
5406 // If no isolate is supplied, use the default isolate.
5407 if (isolate != NULL) {
5408 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
5409 internal_isolate->debugger()->ProcessCommand(
5410 i::Vector<const uint16_t>(command, length), client_data);
5411 } else {
5412 i::Isolate::GetDefaultIsolateDebugger()->ProcessCommand(
5413 i::Vector<const uint16_t>(command, length), client_data);
5414 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005415}
5416
5417
5418void Debug::SetHostDispatchHandler(HostDispatchHandler handler,
5419 int period) {
Steve Block44f0eee2011-05-26 01:26:41 +01005420 i::Isolate* isolate = i::Isolate::Current();
5421 EnsureInitializedForIsolate(isolate, "v8::Debug::SetHostDispatchHandler");
5422 ENTER_V8(isolate);
5423 isolate->debugger()->SetHostDispatchHandler(handler, period);
Steve Blocka7e24c12009-10-30 11:49:00 +00005424}
5425
5426
Steve Blockd0582a62009-12-15 09:54:21 +00005427void Debug::SetDebugMessageDispatchHandler(
Leon Clarkee46be812010-01-19 14:06:41 +00005428 DebugMessageDispatchHandler handler, bool provide_locker) {
Steve Block44f0eee2011-05-26 01:26:41 +01005429 i::Isolate* isolate = i::Isolate::Current();
5430 EnsureInitializedForIsolate(isolate,
5431 "v8::Debug::SetDebugMessageDispatchHandler");
5432 ENTER_V8(isolate);
5433 isolate->debugger()->SetDebugMessageDispatchHandler(
5434 handler, provide_locker);
Steve Blockd0582a62009-12-15 09:54:21 +00005435}
5436
5437
Steve Blocka7e24c12009-10-30 11:49:00 +00005438Local<Value> Debug::Call(v8::Handle<v8::Function> fun,
5439 v8::Handle<v8::Value> data) {
Steve Block44f0eee2011-05-26 01:26:41 +01005440 i::Isolate* isolate = i::Isolate::Current();
5441 if (!isolate->IsInitialized()) return Local<Value>();
5442 ON_BAILOUT(isolate, "v8::Debug::Call()", return Local<Value>());
5443 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005444 i::Handle<i::Object> result;
Steve Block44f0eee2011-05-26 01:26:41 +01005445 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005446 if (data.IsEmpty()) {
Steve Block44f0eee2011-05-26 01:26:41 +01005447 result = isolate->debugger()->Call(Utils::OpenHandle(*fun),
5448 isolate->factory()->undefined_value(),
5449 &has_pending_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +00005450 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01005451 result = isolate->debugger()->Call(Utils::OpenHandle(*fun),
5452 Utils::OpenHandle(*data),
5453 &has_pending_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +00005454 }
Steve Block44f0eee2011-05-26 01:26:41 +01005455 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +00005456 return Utils::ToLocal(result);
5457}
5458
5459
5460Local<Value> Debug::GetMirror(v8::Handle<v8::Value> obj) {
Steve Block44f0eee2011-05-26 01:26:41 +01005461 i::Isolate* isolate = i::Isolate::Current();
5462 if (!isolate->IsInitialized()) return Local<Value>();
5463 ON_BAILOUT(isolate, "v8::Debug::GetMirror()", return Local<Value>());
5464 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005465 v8::HandleScope scope;
Steve Block44f0eee2011-05-26 01:26:41 +01005466 i::Debug* isolate_debug = isolate->debug();
5467 isolate_debug->Load();
5468 i::Handle<i::JSObject> debug(isolate_debug->debug_context()->global());
5469 i::Handle<i::String> name =
5470 isolate->factory()->LookupAsciiSymbol("MakeMirror");
Steve Blocka7e24c12009-10-30 11:49:00 +00005471 i::Handle<i::Object> fun_obj = i::GetProperty(debug, name);
5472 i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(fun_obj);
5473 v8::Handle<v8::Function> v8_fun = Utils::ToLocal(fun);
5474 const int kArgc = 1;
5475 v8::Handle<v8::Value> argv[kArgc] = { obj };
Steve Block44f0eee2011-05-26 01:26:41 +01005476 EXCEPTION_PREAMBLE(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005477 v8::Handle<v8::Value> result = v8_fun->Call(Utils::ToLocal(debug),
5478 kArgc,
5479 argv);
Steve Block44f0eee2011-05-26 01:26:41 +01005480 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +00005481 return scope.Close(result);
5482}
5483
5484
Leon Clarkee46be812010-01-19 14:06:41 +00005485bool Debug::EnableAgent(const char* name, int port, bool wait_for_connection) {
Steve Block44f0eee2011-05-26 01:26:41 +01005486 return i::Isolate::Current()->debugger()->StartAgent(name, port,
5487 wait_for_connection);
Steve Blocka7e24c12009-10-30 11:49:00 +00005488}
Leon Clarkee46be812010-01-19 14:06:41 +00005489
5490void Debug::ProcessDebugMessages() {
5491 i::Execution::ProcessDebugMesssages(true);
5492}
5493
Steve Block6ded16b2010-05-10 14:33:55 +01005494Local<Context> Debug::GetDebugContext() {
Steve Block44f0eee2011-05-26 01:26:41 +01005495 i::Isolate* isolate = i::Isolate::Current();
5496 EnsureInitializedForIsolate(isolate, "v8::Debug::GetDebugContext()");
5497 ENTER_V8(isolate);
5498 return Utils::ToLocal(i::Isolate::Current()->debugger()->GetDebugContext());
Steve Block6ded16b2010-05-10 14:33:55 +01005499}
5500
Steve Blocka7e24c12009-10-30 11:49:00 +00005501#endif // ENABLE_DEBUGGER_SUPPORT
5502
Steve Block6ded16b2010-05-10 14:33:55 +01005503
Steve Block6ded16b2010-05-10 14:33:55 +01005504Handle<String> CpuProfileNode::GetFunctionName() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005505 i::Isolate* isolate = i::Isolate::Current();
5506 IsDeadCheck(isolate, "v8::CpuProfileNode::GetFunctionName");
Steve Block6ded16b2010-05-10 14:33:55 +01005507 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
5508 const i::CodeEntry* entry = node->entry();
5509 if (!entry->has_name_prefix()) {
5510 return Handle<String>(ToApi<String>(
Steve Block44f0eee2011-05-26 01:26:41 +01005511 isolate->factory()->LookupAsciiSymbol(entry->name())));
Steve Block6ded16b2010-05-10 14:33:55 +01005512 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01005513 return Handle<String>(ToApi<String>(isolate->factory()->NewConsString(
5514 isolate->factory()->LookupAsciiSymbol(entry->name_prefix()),
5515 isolate->factory()->LookupAsciiSymbol(entry->name()))));
Steve Block6ded16b2010-05-10 14:33:55 +01005516 }
5517}
5518
5519
5520Handle<String> CpuProfileNode::GetScriptResourceName() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005521 i::Isolate* isolate = i::Isolate::Current();
5522 IsDeadCheck(isolate, "v8::CpuProfileNode::GetScriptResourceName");
Steve Block6ded16b2010-05-10 14:33:55 +01005523 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
Steve Block44f0eee2011-05-26 01:26:41 +01005524 return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol(
Steve Block6ded16b2010-05-10 14:33:55 +01005525 node->entry()->resource_name())));
5526}
5527
5528
5529int CpuProfileNode::GetLineNumber() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005530 i::Isolate* isolate = i::Isolate::Current();
5531 IsDeadCheck(isolate, "v8::CpuProfileNode::GetLineNumber");
Steve Block6ded16b2010-05-10 14:33:55 +01005532 return reinterpret_cast<const i::ProfileNode*>(this)->entry()->line_number();
5533}
5534
5535
5536double CpuProfileNode::GetTotalTime() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005537 i::Isolate* isolate = i::Isolate::Current();
5538 IsDeadCheck(isolate, "v8::CpuProfileNode::GetTotalTime");
Steve Block6ded16b2010-05-10 14:33:55 +01005539 return reinterpret_cast<const i::ProfileNode*>(this)->GetTotalMillis();
5540}
5541
5542
5543double CpuProfileNode::GetSelfTime() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005544 i::Isolate* isolate = i::Isolate::Current();
5545 IsDeadCheck(isolate, "v8::CpuProfileNode::GetSelfTime");
Steve Block6ded16b2010-05-10 14:33:55 +01005546 return reinterpret_cast<const i::ProfileNode*>(this)->GetSelfMillis();
5547}
5548
5549
5550double CpuProfileNode::GetTotalSamplesCount() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005551 i::Isolate* isolate = i::Isolate::Current();
5552 IsDeadCheck(isolate, "v8::CpuProfileNode::GetTotalSamplesCount");
Steve Block6ded16b2010-05-10 14:33:55 +01005553 return reinterpret_cast<const i::ProfileNode*>(this)->total_ticks();
5554}
5555
5556
5557double CpuProfileNode::GetSelfSamplesCount() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005558 i::Isolate* isolate = i::Isolate::Current();
5559 IsDeadCheck(isolate, "v8::CpuProfileNode::GetSelfSamplesCount");
Steve Block6ded16b2010-05-10 14:33:55 +01005560 return reinterpret_cast<const i::ProfileNode*>(this)->self_ticks();
5561}
5562
5563
5564unsigned CpuProfileNode::GetCallUid() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005565 i::Isolate* isolate = i::Isolate::Current();
5566 IsDeadCheck(isolate, "v8::CpuProfileNode::GetCallUid");
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005567 return reinterpret_cast<const i::ProfileNode*>(this)->entry()->GetCallUid();
Steve Block6ded16b2010-05-10 14:33:55 +01005568}
5569
5570
5571int CpuProfileNode::GetChildrenCount() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005572 i::Isolate* isolate = i::Isolate::Current();
5573 IsDeadCheck(isolate, "v8::CpuProfileNode::GetChildrenCount");
Steve Block6ded16b2010-05-10 14:33:55 +01005574 return reinterpret_cast<const i::ProfileNode*>(this)->children()->length();
5575}
5576
5577
5578const CpuProfileNode* CpuProfileNode::GetChild(int index) const {
Steve Block44f0eee2011-05-26 01:26:41 +01005579 i::Isolate* isolate = i::Isolate::Current();
5580 IsDeadCheck(isolate, "v8::CpuProfileNode::GetChild");
Steve Block6ded16b2010-05-10 14:33:55 +01005581 const i::ProfileNode* child =
5582 reinterpret_cast<const i::ProfileNode*>(this)->children()->at(index);
5583 return reinterpret_cast<const CpuProfileNode*>(child);
5584}
5585
5586
Steve Block44f0eee2011-05-26 01:26:41 +01005587void CpuProfile::Delete() {
5588 i::Isolate* isolate = i::Isolate::Current();
5589 IsDeadCheck(isolate, "v8::CpuProfile::Delete");
5590 i::CpuProfiler::DeleteProfile(reinterpret_cast<i::CpuProfile*>(this));
5591 if (i::CpuProfiler::GetProfilesCount() == 0 &&
5592 !i::CpuProfiler::HasDetachedProfiles()) {
5593 // If this was the last profile, clean up all accessory data as well.
5594 i::CpuProfiler::DeleteAllProfiles();
5595 }
5596}
5597
5598
Steve Block6ded16b2010-05-10 14:33:55 +01005599unsigned CpuProfile::GetUid() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005600 i::Isolate* isolate = i::Isolate::Current();
5601 IsDeadCheck(isolate, "v8::CpuProfile::GetUid");
Steve Block6ded16b2010-05-10 14:33:55 +01005602 return reinterpret_cast<const i::CpuProfile*>(this)->uid();
5603}
5604
5605
5606Handle<String> CpuProfile::GetTitle() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005607 i::Isolate* isolate = i::Isolate::Current();
5608 IsDeadCheck(isolate, "v8::CpuProfile::GetTitle");
Steve Block6ded16b2010-05-10 14:33:55 +01005609 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
Steve Block44f0eee2011-05-26 01:26:41 +01005610 return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol(
Steve Block6ded16b2010-05-10 14:33:55 +01005611 profile->title())));
5612}
5613
5614
5615const CpuProfileNode* CpuProfile::GetBottomUpRoot() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005616 i::Isolate* isolate = i::Isolate::Current();
5617 IsDeadCheck(isolate, "v8::CpuProfile::GetBottomUpRoot");
Steve Block6ded16b2010-05-10 14:33:55 +01005618 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
5619 return reinterpret_cast<const CpuProfileNode*>(profile->bottom_up()->root());
5620}
5621
5622
5623const CpuProfileNode* CpuProfile::GetTopDownRoot() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005624 i::Isolate* isolate = i::Isolate::Current();
5625 IsDeadCheck(isolate, "v8::CpuProfile::GetTopDownRoot");
Steve Block6ded16b2010-05-10 14:33:55 +01005626 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
5627 return reinterpret_cast<const CpuProfileNode*>(profile->top_down()->root());
5628}
5629
5630
5631int CpuProfiler::GetProfilesCount() {
Steve Block44f0eee2011-05-26 01:26:41 +01005632 i::Isolate* isolate = i::Isolate::Current();
5633 IsDeadCheck(isolate, "v8::CpuProfiler::GetProfilesCount");
Steve Block6ded16b2010-05-10 14:33:55 +01005634 return i::CpuProfiler::GetProfilesCount();
5635}
5636
5637
Leon Clarkef7060e22010-06-03 12:02:55 +01005638const CpuProfile* CpuProfiler::GetProfile(int index,
5639 Handle<Value> security_token) {
Steve Block44f0eee2011-05-26 01:26:41 +01005640 i::Isolate* isolate = i::Isolate::Current();
5641 IsDeadCheck(isolate, "v8::CpuProfiler::GetProfile");
Leon Clarkef7060e22010-06-03 12:02:55 +01005642 return reinterpret_cast<const CpuProfile*>(
5643 i::CpuProfiler::GetProfile(
5644 security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
5645 index));
Steve Block6ded16b2010-05-10 14:33:55 +01005646}
5647
5648
Leon Clarkef7060e22010-06-03 12:02:55 +01005649const CpuProfile* CpuProfiler::FindProfile(unsigned uid,
5650 Handle<Value> security_token) {
Steve Block44f0eee2011-05-26 01:26:41 +01005651 i::Isolate* isolate = i::Isolate::Current();
5652 IsDeadCheck(isolate, "v8::CpuProfiler::FindProfile");
Leon Clarkef7060e22010-06-03 12:02:55 +01005653 return reinterpret_cast<const CpuProfile*>(
5654 i::CpuProfiler::FindProfile(
5655 security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
5656 uid));
Steve Block6ded16b2010-05-10 14:33:55 +01005657}
5658
5659
5660void CpuProfiler::StartProfiling(Handle<String> title) {
Steve Block44f0eee2011-05-26 01:26:41 +01005661 i::Isolate* isolate = i::Isolate::Current();
5662 IsDeadCheck(isolate, "v8::CpuProfiler::StartProfiling");
Steve Block6ded16b2010-05-10 14:33:55 +01005663 i::CpuProfiler::StartProfiling(*Utils::OpenHandle(*title));
5664}
5665
5666
Leon Clarkef7060e22010-06-03 12:02:55 +01005667const CpuProfile* CpuProfiler::StopProfiling(Handle<String> title,
5668 Handle<Value> security_token) {
Steve Block44f0eee2011-05-26 01:26:41 +01005669 i::Isolate* isolate = i::Isolate::Current();
5670 IsDeadCheck(isolate, "v8::CpuProfiler::StopProfiling");
Steve Block6ded16b2010-05-10 14:33:55 +01005671 return reinterpret_cast<const CpuProfile*>(
Leon Clarkef7060e22010-06-03 12:02:55 +01005672 i::CpuProfiler::StopProfiling(
5673 security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
5674 *Utils::OpenHandle(*title)));
Steve Block6ded16b2010-05-10 14:33:55 +01005675}
5676
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005677
Steve Block44f0eee2011-05-26 01:26:41 +01005678void CpuProfiler::DeleteAllProfiles() {
5679 i::Isolate* isolate = i::Isolate::Current();
5680 IsDeadCheck(isolate, "v8::CpuProfiler::DeleteAllProfiles");
5681 i::CpuProfiler::DeleteAllProfiles();
5682}
5683
5684
Iain Merrick75681382010-08-19 15:07:18 +01005685static i::HeapGraphEdge* ToInternal(const HeapGraphEdge* edge) {
5686 return const_cast<i::HeapGraphEdge*>(
5687 reinterpret_cast<const i::HeapGraphEdge*>(edge));
5688}
5689
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005690
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005691HeapGraphEdge::Type HeapGraphEdge::GetType() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005692 i::Isolate* isolate = i::Isolate::Current();
5693 IsDeadCheck(isolate, "v8::HeapGraphEdge::GetType");
Iain Merrick75681382010-08-19 15:07:18 +01005694 return static_cast<HeapGraphEdge::Type>(ToInternal(this)->type());
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005695}
5696
5697
5698Handle<Value> HeapGraphEdge::GetName() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005699 i::Isolate* isolate = i::Isolate::Current();
5700 IsDeadCheck(isolate, "v8::HeapGraphEdge::GetName");
Iain Merrick75681382010-08-19 15:07:18 +01005701 i::HeapGraphEdge* edge = ToInternal(this);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005702 switch (edge->type()) {
Iain Merrick75681382010-08-19 15:07:18 +01005703 case i::HeapGraphEdge::kContextVariable:
5704 case i::HeapGraphEdge::kInternal:
5705 case i::HeapGraphEdge::kProperty:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08005706 case i::HeapGraphEdge::kShortcut:
Steve Block44f0eee2011-05-26 01:26:41 +01005707 return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol(
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005708 edge->name())));
Iain Merrick75681382010-08-19 15:07:18 +01005709 case i::HeapGraphEdge::kElement:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08005710 case i::HeapGraphEdge::kHidden:
Steve Block44f0eee2011-05-26 01:26:41 +01005711 return Handle<Number>(ToApi<Number>(isolate->factory()->NewNumberFromInt(
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005712 edge->index())));
5713 default: UNREACHABLE();
5714 }
Steve Block44f0eee2011-05-26 01:26:41 +01005715 return v8::Undefined();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005716}
5717
5718
5719const HeapGraphNode* HeapGraphEdge::GetFromNode() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005720 i::Isolate* isolate = i::Isolate::Current();
5721 IsDeadCheck(isolate, "v8::HeapGraphEdge::GetFromNode");
Iain Merrick75681382010-08-19 15:07:18 +01005722 const i::HeapEntry* from = ToInternal(this)->From();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005723 return reinterpret_cast<const HeapGraphNode*>(from);
5724}
5725
5726
5727const HeapGraphNode* HeapGraphEdge::GetToNode() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005728 i::Isolate* isolate = i::Isolate::Current();
5729 IsDeadCheck(isolate, "v8::HeapGraphEdge::GetToNode");
Iain Merrick75681382010-08-19 15:07:18 +01005730 const i::HeapEntry* to = ToInternal(this)->to();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005731 return reinterpret_cast<const HeapGraphNode*>(to);
5732}
5733
5734
Iain Merrick75681382010-08-19 15:07:18 +01005735static i::HeapEntry* ToInternal(const HeapGraphNode* entry) {
5736 return const_cast<i::HeapEntry*>(
5737 reinterpret_cast<const i::HeapEntry*>(entry));
5738}
5739
5740
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005741HeapGraphNode::Type HeapGraphNode::GetType() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005742 i::Isolate* isolate = i::Isolate::Current();
5743 IsDeadCheck(isolate, "v8::HeapGraphNode::GetType");
Iain Merrick75681382010-08-19 15:07:18 +01005744 return static_cast<HeapGraphNode::Type>(ToInternal(this)->type());
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005745}
5746
5747
5748Handle<String> HeapGraphNode::GetName() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005749 i::Isolate* isolate = i::Isolate::Current();
5750 IsDeadCheck(isolate, "v8::HeapGraphNode::GetName");
5751 return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol(
Iain Merrick75681382010-08-19 15:07:18 +01005752 ToInternal(this)->name())));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005753}
5754
5755
Ben Murdoch3bec4d22010-07-22 14:51:16 +01005756uint64_t HeapGraphNode::GetId() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005757 i::Isolate* isolate = i::Isolate::Current();
5758 IsDeadCheck(isolate, "v8::HeapGraphNode::GetId");
Iain Merrick75681382010-08-19 15:07:18 +01005759 return ToInternal(this)->id();
Ben Murdoch3bec4d22010-07-22 14:51:16 +01005760}
5761
5762
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005763int HeapGraphNode::GetSelfSize() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005764 i::Isolate* isolate = i::Isolate::Current();
5765 IsDeadCheck(isolate, "v8::HeapGraphNode::GetSelfSize");
Iain Merrick75681382010-08-19 15:07:18 +01005766 return ToInternal(this)->self_size();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005767}
5768
5769
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08005770int HeapGraphNode::GetRetainedSize(bool exact) const {
Steve Block44f0eee2011-05-26 01:26:41 +01005771 i::Isolate* isolate = i::Isolate::Current();
5772 IsDeadCheck(isolate, "v8::HeapSnapshot::GetRetainedSize");
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08005773 return ToInternal(this)->RetainedSize(exact);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005774}
5775
5776
5777int HeapGraphNode::GetChildrenCount() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005778 i::Isolate* isolate = i::Isolate::Current();
5779 IsDeadCheck(isolate, "v8::HeapSnapshot::GetChildrenCount");
Iain Merrick75681382010-08-19 15:07:18 +01005780 return ToInternal(this)->children().length();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005781}
5782
5783
5784const HeapGraphEdge* HeapGraphNode::GetChild(int index) const {
Steve Block44f0eee2011-05-26 01:26:41 +01005785 i::Isolate* isolate = i::Isolate::Current();
5786 IsDeadCheck(isolate, "v8::HeapSnapshot::GetChild");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005787 return reinterpret_cast<const HeapGraphEdge*>(
Iain Merrick75681382010-08-19 15:07:18 +01005788 &ToInternal(this)->children()[index]);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005789}
5790
5791
5792int HeapGraphNode::GetRetainersCount() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005793 i::Isolate* isolate = i::Isolate::Current();
5794 IsDeadCheck(isolate, "v8::HeapSnapshot::GetRetainersCount");
Iain Merrick75681382010-08-19 15:07:18 +01005795 return ToInternal(this)->retainers().length();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005796}
5797
5798
5799const HeapGraphEdge* HeapGraphNode::GetRetainer(int index) const {
Steve Block44f0eee2011-05-26 01:26:41 +01005800 i::Isolate* isolate = i::Isolate::Current();
5801 IsDeadCheck(isolate, "v8::HeapSnapshot::GetRetainer");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005802 return reinterpret_cast<const HeapGraphEdge*>(
Iain Merrick75681382010-08-19 15:07:18 +01005803 ToInternal(this)->retainers()[index]);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005804}
5805
5806
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08005807const HeapGraphNode* HeapGraphNode::GetDominatorNode() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005808 i::Isolate* isolate = i::Isolate::Current();
5809 IsDeadCheck(isolate, "v8::HeapSnapshot::GetDominatorNode");
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08005810 return reinterpret_cast<const HeapGraphNode*>(ToInternal(this)->dominator());
5811}
5812
5813
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005814v8::Handle<v8::Value> HeapGraphNode::GetHeapValue() const {
5815 i::Isolate* isolate = i::Isolate::Current();
5816 IsDeadCheck(isolate, "v8::HeapGraphNode::GetHeapValue");
5817 i::Handle<i::HeapObject> object = ToInternal(this)->GetHeapObject();
5818 return v8::Handle<Value>(!object.is_null() ?
5819 ToApi<Value>(object) : ToApi<Value>(
5820 isolate->factory()->undefined_value()));
5821}
5822
5823
Iain Merrick75681382010-08-19 15:07:18 +01005824static i::HeapSnapshot* ToInternal(const HeapSnapshot* snapshot) {
5825 return const_cast<i::HeapSnapshot*>(
5826 reinterpret_cast<const i::HeapSnapshot*>(snapshot));
5827}
5828
5829
Steve Block44f0eee2011-05-26 01:26:41 +01005830void HeapSnapshot::Delete() {
5831 i::Isolate* isolate = i::Isolate::Current();
5832 IsDeadCheck(isolate, "v8::HeapSnapshot::Delete");
5833 if (i::HeapProfiler::GetSnapshotsCount() > 1) {
5834 ToInternal(this)->Delete();
5835 } else {
5836 // If this is the last snapshot, clean up all accessory data as well.
5837 i::HeapProfiler::DeleteAllSnapshots();
5838 }
5839}
5840
5841
Steve Block791712a2010-08-27 10:21:07 +01005842HeapSnapshot::Type HeapSnapshot::GetType() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005843 i::Isolate* isolate = i::Isolate::Current();
5844 IsDeadCheck(isolate, "v8::HeapSnapshot::GetType");
Steve Block791712a2010-08-27 10:21:07 +01005845 return static_cast<HeapSnapshot::Type>(ToInternal(this)->type());
5846}
5847
5848
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005849unsigned HeapSnapshot::GetUid() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005850 i::Isolate* isolate = i::Isolate::Current();
5851 IsDeadCheck(isolate, "v8::HeapSnapshot::GetUid");
Iain Merrick75681382010-08-19 15:07:18 +01005852 return ToInternal(this)->uid();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005853}
5854
5855
5856Handle<String> HeapSnapshot::GetTitle() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005857 i::Isolate* isolate = i::Isolate::Current();
5858 IsDeadCheck(isolate, "v8::HeapSnapshot::GetTitle");
5859 return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol(
Iain Merrick75681382010-08-19 15:07:18 +01005860 ToInternal(this)->title())));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005861}
5862
5863
Ben Murdoch3bec4d22010-07-22 14:51:16 +01005864const HeapGraphNode* HeapSnapshot::GetRoot() const {
Steve Block44f0eee2011-05-26 01:26:41 +01005865 i::Isolate* isolate = i::Isolate::Current();
5866 IsDeadCheck(isolate, "v8::HeapSnapshot::GetHead");
Iain Merrick75681382010-08-19 15:07:18 +01005867 return reinterpret_cast<const HeapGraphNode*>(ToInternal(this)->root());
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005868}
5869
5870
Ben Murdochb0fe1622011-05-05 13:52:32 +01005871const HeapGraphNode* HeapSnapshot::GetNodeById(uint64_t id) const {
Steve Block44f0eee2011-05-26 01:26:41 +01005872 i::Isolate* isolate = i::Isolate::Current();
5873 IsDeadCheck(isolate, "v8::HeapSnapshot::GetNodeById");
Ben Murdochb0fe1622011-05-05 13:52:32 +01005874 return reinterpret_cast<const HeapGraphNode*>(
5875 ToInternal(this)->GetEntryById(id));
5876}
5877
5878
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005879int HeapSnapshot::GetNodesCount() const {
5880 i::Isolate* isolate = i::Isolate::Current();
5881 IsDeadCheck(isolate, "v8::HeapSnapshot::GetNodesCount");
5882 return ToInternal(this)->entries()->length();
5883}
5884
5885
5886const HeapGraphNode* HeapSnapshot::GetNode(int index) const {
5887 i::Isolate* isolate = i::Isolate::Current();
5888 IsDeadCheck(isolate, "v8::HeapSnapshot::GetNode");
5889 return reinterpret_cast<const HeapGraphNode*>(
5890 ToInternal(this)->entries()->at(index));
5891}
5892
5893
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005894void HeapSnapshot::Serialize(OutputStream* stream,
5895 HeapSnapshot::SerializationFormat format) const {
Steve Block44f0eee2011-05-26 01:26:41 +01005896 i::Isolate* isolate = i::Isolate::Current();
5897 IsDeadCheck(isolate, "v8::HeapSnapshot::Serialize");
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005898 ApiCheck(format == kJSON,
5899 "v8::HeapSnapshot::Serialize",
5900 "Unknown serialization format");
5901 ApiCheck(stream->GetOutputEncoding() == OutputStream::kAscii,
5902 "v8::HeapSnapshot::Serialize",
5903 "Unsupported output encoding");
5904 ApiCheck(stream->GetChunkSize() > 0,
5905 "v8::HeapSnapshot::Serialize",
5906 "Invalid stream chunk size");
5907 i::HeapSnapshotJSONSerializer serializer(ToInternal(this));
5908 serializer.Serialize(stream);
5909}
5910
5911
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005912int HeapProfiler::GetSnapshotsCount() {
Steve Block44f0eee2011-05-26 01:26:41 +01005913 i::Isolate* isolate = i::Isolate::Current();
5914 IsDeadCheck(isolate, "v8::HeapProfiler::GetSnapshotsCount");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005915 return i::HeapProfiler::GetSnapshotsCount();
5916}
5917
5918
5919const HeapSnapshot* HeapProfiler::GetSnapshot(int index) {
Steve Block44f0eee2011-05-26 01:26:41 +01005920 i::Isolate* isolate = i::Isolate::Current();
5921 IsDeadCheck(isolate, "v8::HeapProfiler::GetSnapshot");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005922 return reinterpret_cast<const HeapSnapshot*>(
5923 i::HeapProfiler::GetSnapshot(index));
5924}
5925
5926
5927const HeapSnapshot* HeapProfiler::FindSnapshot(unsigned uid) {
Steve Block44f0eee2011-05-26 01:26:41 +01005928 i::Isolate* isolate = i::Isolate::Current();
5929 IsDeadCheck(isolate, "v8::HeapProfiler::FindSnapshot");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005930 return reinterpret_cast<const HeapSnapshot*>(
5931 i::HeapProfiler::FindSnapshot(uid));
5932}
5933
5934
Steve Block791712a2010-08-27 10:21:07 +01005935const HeapSnapshot* HeapProfiler::TakeSnapshot(Handle<String> title,
Ben Murdochb0fe1622011-05-05 13:52:32 +01005936 HeapSnapshot::Type type,
5937 ActivityControl* control) {
Steve Block44f0eee2011-05-26 01:26:41 +01005938 i::Isolate* isolate = i::Isolate::Current();
5939 IsDeadCheck(isolate, "v8::HeapProfiler::TakeSnapshot");
Steve Block791712a2010-08-27 10:21:07 +01005940 i::HeapSnapshot::Type internal_type = i::HeapSnapshot::kFull;
5941 switch (type) {
5942 case HeapSnapshot::kFull:
5943 internal_type = i::HeapSnapshot::kFull;
5944 break;
Steve Block791712a2010-08-27 10:21:07 +01005945 default:
5946 UNREACHABLE();
5947 }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005948 return reinterpret_cast<const HeapSnapshot*>(
Ben Murdochb0fe1622011-05-05 13:52:32 +01005949 i::HeapProfiler::TakeSnapshot(
5950 *Utils::OpenHandle(*title), internal_type, control));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005951}
5952
Steve Block44f0eee2011-05-26 01:26:41 +01005953
5954void HeapProfiler::DeleteAllSnapshots() {
5955 i::Isolate* isolate = i::Isolate::Current();
5956 IsDeadCheck(isolate, "v8::HeapProfiler::DeleteAllSnapshots");
5957 i::HeapProfiler::DeleteAllSnapshots();
5958}
5959
5960
5961void HeapProfiler::DefineWrapperClass(uint16_t class_id,
5962 WrapperInfoCallback callback) {
5963 i::Isolate::Current()->heap_profiler()->DefineWrapperClass(class_id,
5964 callback);
5965}
5966
Steve Block6ded16b2010-05-10 14:33:55 +01005967
5968
Ben Murdochb0fe1622011-05-05 13:52:32 +01005969v8::Testing::StressType internal::Testing::stress_type_ =
5970 v8::Testing::kStressTypeOpt;
5971
5972
5973void Testing::SetStressRunType(Testing::StressType type) {
5974 internal::Testing::set_stress_type(type);
5975}
5976
5977int Testing::GetStressRuns() {
5978 if (internal::FLAG_stress_runs != 0) return internal::FLAG_stress_runs;
5979#ifdef DEBUG
5980 // In debug mode the code runs much slower so stressing will only make two
5981 // runs.
5982 return 2;
5983#else
5984 return 5;
5985#endif
5986}
5987
5988
5989static void SetFlagsFromString(const char* flags) {
5990 V8::SetFlagsFromString(flags, i::StrLength(flags));
5991}
5992
5993
5994void Testing::PrepareStressRun(int run) {
5995 static const char* kLazyOptimizations =
5996 "--prepare-always-opt --nolimit-inlining "
5997 "--noalways-opt --noopt-eagerly";
5998 static const char* kEagerOptimizations = "--opt-eagerly";
5999 static const char* kForcedOptimizations = "--always-opt";
6000
6001 // If deoptimization stressed turn on frequent deoptimization. If no value
6002 // is spefified through --deopt-every-n-times use a default default value.
6003 static const char* kDeoptEvery13Times = "--deopt-every-n-times=13";
6004 if (internal::Testing::stress_type() == Testing::kStressTypeDeopt &&
6005 internal::FLAG_deopt_every_n_times == 0) {
6006 SetFlagsFromString(kDeoptEvery13Times);
6007 }
6008
6009#ifdef DEBUG
6010 // As stressing in debug mode only make two runs skip the deopt stressing
6011 // here.
6012 if (run == GetStressRuns() - 1) {
6013 SetFlagsFromString(kForcedOptimizations);
6014 } else {
6015 SetFlagsFromString(kEagerOptimizations);
6016 SetFlagsFromString(kLazyOptimizations);
6017 }
6018#else
6019 if (run == GetStressRuns() - 1) {
6020 SetFlagsFromString(kForcedOptimizations);
6021 } else if (run == GetStressRuns() - 2) {
6022 SetFlagsFromString(kEagerOptimizations);
6023 } else {
6024 SetFlagsFromString(kLazyOptimizations);
6025 }
6026#endif
6027}
6028
6029
Steve Block44f0eee2011-05-26 01:26:41 +01006030void Testing::DeoptimizeAll() {
6031 internal::Deoptimizer::DeoptimizeAll();
Steve Blocka7e24c12009-10-30 11:49:00 +00006032}
6033
6034
Steve Block44f0eee2011-05-26 01:26:41 +01006035namespace internal {
6036
6037
Steve Blocka7e24c12009-10-30 11:49:00 +00006038void HandleScopeImplementer::FreeThreadResources() {
Steve Block44f0eee2011-05-26 01:26:41 +01006039 Free();
Steve Blocka7e24c12009-10-30 11:49:00 +00006040}
6041
6042
6043char* HandleScopeImplementer::ArchiveThread(char* storage) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006044 v8::ImplementationUtilities::HandleScopeData* current =
Ben Murdoch257744e2011-11-30 15:57:28 +00006045 isolate_->handle_scope_data();
Steve Blocka7e24c12009-10-30 11:49:00 +00006046 handle_scope_data_ = *current;
6047 memcpy(storage, this, sizeof(*this));
6048
6049 ResetAfterArchive();
6050 current->Initialize();
6051
6052 return storage + ArchiveSpacePerThread();
6053}
6054
6055
6056int HandleScopeImplementer::ArchiveSpacePerThread() {
Steve Block44f0eee2011-05-26 01:26:41 +01006057 return sizeof(HandleScopeImplementer);
Steve Blocka7e24c12009-10-30 11:49:00 +00006058}
6059
6060
6061char* HandleScopeImplementer::RestoreThread(char* storage) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006062 memcpy(this, storage, sizeof(*this));
Ben Murdoch257744e2011-11-30 15:57:28 +00006063 *isolate_->handle_scope_data() = handle_scope_data_;
Steve Blocka7e24c12009-10-30 11:49:00 +00006064 return storage + ArchiveSpacePerThread();
6065}
6066
6067
6068void HandleScopeImplementer::IterateThis(ObjectVisitor* v) {
6069 // Iterate over all handles in the blocks except for the last.
6070 for (int i = blocks()->length() - 2; i >= 0; --i) {
6071 Object** block = blocks()->at(i);
6072 v->VisitPointers(block, &block[kHandleBlockSize]);
6073 }
6074
6075 // Iterate over live handles in the last block (if any).
6076 if (!blocks()->is_empty()) {
6077 v->VisitPointers(blocks()->last(), handle_scope_data_.next);
6078 }
6079
6080 if (!saved_contexts_.is_empty()) {
6081 Object** start = reinterpret_cast<Object**>(&saved_contexts_.first());
6082 v->VisitPointers(start, start + saved_contexts_.length());
6083 }
6084}
6085
6086
6087void HandleScopeImplementer::Iterate(ObjectVisitor* v) {
6088 v8::ImplementationUtilities::HandleScopeData* current =
Ben Murdoch257744e2011-11-30 15:57:28 +00006089 isolate_->handle_scope_data();
Steve Block44f0eee2011-05-26 01:26:41 +01006090 handle_scope_data_ = *current;
6091 IterateThis(v);
Steve Blocka7e24c12009-10-30 11:49:00 +00006092}
6093
6094
6095char* HandleScopeImplementer::Iterate(ObjectVisitor* v, char* storage) {
Steve Block44f0eee2011-05-26 01:26:41 +01006096 HandleScopeImplementer* scope_implementer =
Steve Blocka7e24c12009-10-30 11:49:00 +00006097 reinterpret_cast<HandleScopeImplementer*>(storage);
Steve Block44f0eee2011-05-26 01:26:41 +01006098 scope_implementer->IterateThis(v);
Steve Blocka7e24c12009-10-30 11:49:00 +00006099 return storage + ArchiveSpacePerThread();
6100}
6101
6102} } // namespace v8::internal