blob: 19af866c24a80cbba08a3586326692e69dd3a71e [file] [log] [blame]
Ben Murdochf87a2032010-10-22 12:50:53 +01001// Copyright 2010 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"
36#include "execution.h"
37#include "global-handles.h"
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010038#include "heap-profiler.h"
Steve Block6ded16b2010-05-10 14:33:55 +010039#include "messages.h"
Ben Murdochf87a2032010-10-22 12:50:53 +010040#include "parser.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000041#include "platform.h"
Steve Block6ded16b2010-05-10 14:33:55 +010042#include "profile-generator-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000043#include "serialize.h"
44#include "snapshot.h"
Steve Block6ded16b2010-05-10 14:33:55 +010045#include "top.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000046#include "v8threads.h"
47#include "version.h"
48
Steve Block6ded16b2010-05-10 14:33:55 +010049#include "../include/v8-profiler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000050
51#define LOG_API(expr) LOG(ApiEntryCall(expr))
52
Leon Clarkef7060e22010-06-03 12:02:55 +010053#ifdef ENABLE_VMSTATE_TRACKING
Steve Blocka7e24c12009-10-30 11:49:00 +000054#define ENTER_V8 i::VMState __state__(i::OTHER)
55#define LEAVE_V8 i::VMState __state__(i::EXTERNAL)
56#else
57#define ENTER_V8 ((void) 0)
58#define LEAVE_V8 ((void) 0)
59#endif
60
61namespace v8 {
62
Leon Clarkef7060e22010-06-03 12:02:55 +010063#define ON_BAILOUT(location, code) \
64 if (IsDeadCheck(location) || v8::V8::IsExecutionTerminating()) { \
65 code; \
66 UNREACHABLE(); \
Steve Blocka7e24c12009-10-30 11:49:00 +000067 }
68
69
70#define EXCEPTION_PREAMBLE() \
71 thread_local.IncrementCallDepth(); \
72 ASSERT(!i::Top::external_caught_exception()); \
73 bool has_pending_exception = false
74
75
76#define EXCEPTION_BAILOUT_CHECK(value) \
77 do { \
78 thread_local.DecrementCallDepth(); \
79 if (has_pending_exception) { \
80 if (thread_local.CallDepthIsZero() && i::Top::is_out_of_memory()) { \
81 if (!thread_local.ignore_out_of_memory()) \
82 i::V8::FatalProcessOutOfMemory(NULL); \
83 } \
84 bool call_depth_is_zero = thread_local.CallDepthIsZero(); \
85 i::Top::OptionalRescheduleException(call_depth_is_zero); \
86 return value; \
87 } \
88 } while (false)
89
90
91#define API_ENTRY_CHECK(msg) \
92 do { \
93 if (v8::Locker::IsActive()) { \
94 ApiCheck(i::ThreadManager::IsLockedByCurrentThread(), \
95 msg, \
96 "Entering the V8 API without proper locking in place"); \
97 } \
98 } while (false)
99
100// --- D a t a t h a t i s s p e c i f i c t o a t h r e a d ---
101
102
103static i::HandleScopeImplementer thread_local;
104
105
106// --- E x c e p t i o n B e h a v i o r ---
107
108
109static FatalErrorCallback exception_behavior = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000110
111static void DefaultFatalErrorHandler(const char* location,
112 const char* message) {
113 ENTER_V8;
114 API_Fatal(location, message);
115}
116
117
Steve Blocka7e24c12009-10-30 11:49:00 +0000118static FatalErrorCallback& GetFatalErrorHandler() {
119 if (exception_behavior == NULL) {
120 exception_behavior = DefaultFatalErrorHandler;
121 }
122 return exception_behavior;
123}
124
125
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800126void i::FatalProcessOutOfMemory(const char* location) {
127 i::V8::FatalProcessOutOfMemory(location, false);
128}
129
Steve Blocka7e24c12009-10-30 11:49:00 +0000130
131// When V8 cannot allocated memory FatalProcessOutOfMemory is called.
132// The default fatal error handler is called and execution is stopped.
Ben Murdochbb769b22010-08-11 14:56:33 +0100133void i::V8::FatalProcessOutOfMemory(const char* location, bool take_snapshot) {
Steve Blockd0582a62009-12-15 09:54:21 +0000134 i::HeapStats heap_stats;
135 int start_marker;
136 heap_stats.start_marker = &start_marker;
137 int new_space_size;
138 heap_stats.new_space_size = &new_space_size;
139 int new_space_capacity;
140 heap_stats.new_space_capacity = &new_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100141 intptr_t old_pointer_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000142 heap_stats.old_pointer_space_size = &old_pointer_space_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100143 intptr_t old_pointer_space_capacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000144 heap_stats.old_pointer_space_capacity = &old_pointer_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100145 intptr_t old_data_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000146 heap_stats.old_data_space_size = &old_data_space_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100147 intptr_t old_data_space_capacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000148 heap_stats.old_data_space_capacity = &old_data_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100149 intptr_t code_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000150 heap_stats.code_space_size = &code_space_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100151 intptr_t code_space_capacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000152 heap_stats.code_space_capacity = &code_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100153 intptr_t map_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000154 heap_stats.map_space_size = &map_space_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100155 intptr_t map_space_capacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000156 heap_stats.map_space_capacity = &map_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100157 intptr_t cell_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000158 heap_stats.cell_space_size = &cell_space_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100159 intptr_t cell_space_capacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000160 heap_stats.cell_space_capacity = &cell_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100161 intptr_t lo_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000162 heap_stats.lo_space_size = &lo_space_size;
163 int global_handle_count;
164 heap_stats.global_handle_count = &global_handle_count;
165 int weak_global_handle_count;
166 heap_stats.weak_global_handle_count = &weak_global_handle_count;
167 int pending_global_handle_count;
168 heap_stats.pending_global_handle_count = &pending_global_handle_count;
169 int near_death_global_handle_count;
170 heap_stats.near_death_global_handle_count = &near_death_global_handle_count;
171 int destroyed_global_handle_count;
172 heap_stats.destroyed_global_handle_count = &destroyed_global_handle_count;
Ben Murdochf87a2032010-10-22 12:50:53 +0100173 intptr_t memory_allocator_size;
Ben Murdochbb769b22010-08-11 14:56:33 +0100174 heap_stats.memory_allocator_size = &memory_allocator_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100175 intptr_t memory_allocator_capacity;
Ben Murdochbb769b22010-08-11 14:56:33 +0100176 heap_stats.memory_allocator_capacity = &memory_allocator_capacity;
177 int objects_per_type[LAST_TYPE + 1] = {0};
178 heap_stats.objects_per_type = objects_per_type;
179 int size_per_type[LAST_TYPE + 1] = {0};
180 heap_stats.size_per_type = size_per_type;
Iain Merrick75681382010-08-19 15:07:18 +0100181 int os_error;
182 heap_stats.os_error = &os_error;
Steve Blockd0582a62009-12-15 09:54:21 +0000183 int end_marker;
184 heap_stats.end_marker = &end_marker;
Ben Murdochbb769b22010-08-11 14:56:33 +0100185 i::Heap::RecordStats(&heap_stats, take_snapshot);
Steve Blocka7e24c12009-10-30 11:49:00 +0000186 i::V8::SetFatalError();
187 FatalErrorCallback callback = GetFatalErrorHandler();
188 {
189 LEAVE_V8;
190 callback(location, "Allocation failed - process out of memory");
191 }
192 // If the callback returns, we stop execution.
193 UNREACHABLE();
194}
195
196
197void V8::SetFatalErrorHandler(FatalErrorCallback that) {
198 exception_behavior = that;
199}
200
201
202bool Utils::ReportApiFailure(const char* location, const char* message) {
203 FatalErrorCallback callback = GetFatalErrorHandler();
204 callback(location, message);
205 i::V8::SetFatalError();
206 return false;
207}
208
209
210bool V8::IsDead() {
211 return i::V8::IsDead();
212}
213
214
215static inline bool ApiCheck(bool condition,
216 const char* location,
217 const char* message) {
218 return condition ? true : Utils::ReportApiFailure(location, message);
219}
220
221
222static bool ReportV8Dead(const char* location) {
223 FatalErrorCallback callback = GetFatalErrorHandler();
224 callback(location, "V8 is no longer usable");
225 return true;
226}
227
228
229static bool ReportEmptyHandle(const char* location) {
230 FatalErrorCallback callback = GetFatalErrorHandler();
231 callback(location, "Reading from empty handle");
232 return true;
233}
234
235
236/**
237 * IsDeadCheck checks that the vm is usable. If, for instance, the vm has been
238 * out of memory at some point this check will fail. It should be called on
239 * entry to all methods that touch anything in the heap, except destructors
240 * which you sometimes can't avoid calling after the vm has crashed. Functions
241 * that call EnsureInitialized or ON_BAILOUT don't have to also call
242 * IsDeadCheck. ON_BAILOUT has the advantage over EnsureInitialized that you
243 * can arrange to return if the VM is dead. This is needed to ensure that no VM
244 * heap allocations are attempted on a dead VM. EnsureInitialized has the
245 * advantage over ON_BAILOUT that it actually initializes the VM if this has not
246 * yet been done.
247 */
248static inline bool IsDeadCheck(const char* location) {
249 return !i::V8::IsRunning()
250 && i::V8::IsDead() ? ReportV8Dead(location) : false;
251}
252
253
254static inline bool EmptyCheck(const char* location, v8::Handle<v8::Data> obj) {
255 return obj.IsEmpty() ? ReportEmptyHandle(location) : false;
256}
257
258
259static inline bool EmptyCheck(const char* location, const v8::Data* obj) {
260 return (obj == 0) ? ReportEmptyHandle(location) : false;
261}
262
263// --- S t a t i c s ---
264
265
266static i::StringInputBuffer write_input_buffer;
267
268
269static inline bool EnsureInitialized(const char* location) {
270 if (i::V8::IsRunning()) {
271 return true;
272 }
273 if (IsDeadCheck(location)) {
274 return false;
275 }
276 return ApiCheck(v8::V8::Initialize(), location, "Error initializing V8");
277}
278
279
280ImplementationUtilities::HandleScopeData*
281 ImplementationUtilities::CurrentHandleScope() {
282 return &i::HandleScope::current_;
283}
284
285
286#ifdef DEBUG
287void ImplementationUtilities::ZapHandleRange(i::Object** begin,
288 i::Object** end) {
289 i::HandleScope::ZapRange(begin, end);
290}
291#endif
292
293
294v8::Handle<v8::Primitive> ImplementationUtilities::Undefined() {
295 if (!EnsureInitialized("v8::Undefined()")) return v8::Handle<v8::Primitive>();
296 return v8::Handle<Primitive>(ToApi<Primitive>(i::Factory::undefined_value()));
297}
298
299
300v8::Handle<v8::Primitive> ImplementationUtilities::Null() {
301 if (!EnsureInitialized("v8::Null()")) return v8::Handle<v8::Primitive>();
302 return v8::Handle<Primitive>(ToApi<Primitive>(i::Factory::null_value()));
303}
304
305
306v8::Handle<v8::Boolean> ImplementationUtilities::True() {
307 if (!EnsureInitialized("v8::True()")) return v8::Handle<v8::Boolean>();
308 return v8::Handle<v8::Boolean>(ToApi<Boolean>(i::Factory::true_value()));
309}
310
311
312v8::Handle<v8::Boolean> ImplementationUtilities::False() {
313 if (!EnsureInitialized("v8::False()")) return v8::Handle<v8::Boolean>();
314 return v8::Handle<v8::Boolean>(ToApi<Boolean>(i::Factory::false_value()));
315}
316
317
318void V8::SetFlagsFromString(const char* str, int length) {
319 i::FlagList::SetFlagsFromString(str, length);
320}
321
322
323void V8::SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags) {
324 i::FlagList::SetFlagsFromCommandLine(argc, argv, remove_flags);
325}
326
327
328v8::Handle<Value> ThrowException(v8::Handle<v8::Value> value) {
329 if (IsDeadCheck("v8::ThrowException()")) return v8::Handle<Value>();
330 ENTER_V8;
331 // If we're passed an empty handle, we throw an undefined exception
332 // to deal more gracefully with out of memory situations.
333 if (value.IsEmpty()) {
334 i::Top::ScheduleThrow(i::Heap::undefined_value());
335 } else {
336 i::Top::ScheduleThrow(*Utils::OpenHandle(*value));
337 }
338 return v8::Undefined();
339}
340
341
342RegisteredExtension* RegisteredExtension::first_extension_ = NULL;
343
344
345RegisteredExtension::RegisteredExtension(Extension* extension)
346 : extension_(extension), state_(UNVISITED) { }
347
348
349void RegisteredExtension::Register(RegisteredExtension* that) {
350 that->next_ = RegisteredExtension::first_extension_;
351 RegisteredExtension::first_extension_ = that;
352}
353
354
355void RegisterExtension(Extension* that) {
356 RegisteredExtension* extension = new RegisteredExtension(that);
357 RegisteredExtension::Register(extension);
358}
359
360
361Extension::Extension(const char* name,
362 const char* source,
363 int dep_count,
364 const char** deps)
365 : name_(name),
366 source_(source),
367 dep_count_(dep_count),
368 deps_(deps),
369 auto_enable_(false) { }
370
371
372v8::Handle<Primitive> Undefined() {
373 LOG_API("Undefined");
374 return ImplementationUtilities::Undefined();
375}
376
377
378v8::Handle<Primitive> Null() {
379 LOG_API("Null");
380 return ImplementationUtilities::Null();
381}
382
383
384v8::Handle<Boolean> True() {
385 LOG_API("True");
386 return ImplementationUtilities::True();
387}
388
389
390v8::Handle<Boolean> False() {
391 LOG_API("False");
392 return ImplementationUtilities::False();
393}
394
395
396ResourceConstraints::ResourceConstraints()
397 : max_young_space_size_(0),
398 max_old_space_size_(0),
Russell Brenner90bac252010-11-18 13:33:46 -0800399 max_executable_size_(0),
Steve Blocka7e24c12009-10-30 11:49:00 +0000400 stack_limit_(NULL) { }
401
402
403bool SetResourceConstraints(ResourceConstraints* constraints) {
Steve Block3ce2e202009-11-05 08:53:23 +0000404 int young_space_size = constraints->max_young_space_size();
Steve Blocka7e24c12009-10-30 11:49:00 +0000405 int old_gen_size = constraints->max_old_space_size();
Russell Brenner90bac252010-11-18 13:33:46 -0800406 int max_executable_size = constraints->max_executable_size();
407 if (young_space_size != 0 || old_gen_size != 0 || max_executable_size != 0) {
408 bool result = i::Heap::ConfigureHeap(young_space_size / 2,
409 old_gen_size,
410 max_executable_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000411 if (!result) return false;
412 }
413 if (constraints->stack_limit() != NULL) {
414 uintptr_t limit = reinterpret_cast<uintptr_t>(constraints->stack_limit());
415 i::StackGuard::SetStackLimit(limit);
416 }
417 return true;
418}
419
420
421i::Object** V8::GlobalizeReference(i::Object** obj) {
422 if (IsDeadCheck("V8::Persistent::New")) return NULL;
423 LOG_API("Persistent::New");
424 i::Handle<i::Object> result =
425 i::GlobalHandles::Create(*obj);
426 return result.location();
427}
428
429
430void V8::MakeWeak(i::Object** object, void* parameters,
431 WeakReferenceCallback callback) {
432 LOG_API("MakeWeak");
433 i::GlobalHandles::MakeWeak(object, parameters, callback);
434}
435
436
437void V8::ClearWeak(i::Object** obj) {
438 LOG_API("ClearWeak");
439 i::GlobalHandles::ClearWeakness(obj);
440}
441
442
443bool V8::IsGlobalNearDeath(i::Object** obj) {
444 LOG_API("IsGlobalNearDeath");
445 if (!i::V8::IsRunning()) return false;
446 return i::GlobalHandles::IsNearDeath(obj);
447}
448
449
450bool V8::IsGlobalWeak(i::Object** obj) {
451 LOG_API("IsGlobalWeak");
452 if (!i::V8::IsRunning()) return false;
453 return i::GlobalHandles::IsWeak(obj);
454}
455
456
457void V8::DisposeGlobal(i::Object** obj) {
458 LOG_API("DisposeGlobal");
459 if (!i::V8::IsRunning()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000460 i::GlobalHandles::Destroy(obj);
461}
462
463// --- H a n d l e s ---
464
465
John Reck59135872010-11-02 12:39:01 -0700466HandleScope::HandleScope()
467 : prev_next_(i::HandleScope::current_.next),
468 prev_limit_(i::HandleScope::current_.limit),
469 is_closed_(false) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000470 API_ENTRY_CHECK("HandleScope::HandleScope");
John Reck59135872010-11-02 12:39:01 -0700471 i::HandleScope::current_.level++;
Steve Blocka7e24c12009-10-30 11:49:00 +0000472}
473
474
475HandleScope::~HandleScope() {
476 if (!is_closed_) {
John Reck59135872010-11-02 12:39:01 -0700477 Leave();
Steve Blocka7e24c12009-10-30 11:49:00 +0000478 }
479}
480
481
John Reck59135872010-11-02 12:39:01 -0700482void HandleScope::Leave() {
483 i::HandleScope::current_.level--;
484 ASSERT(i::HandleScope::current_.level >= 0);
485 i::HandleScope::current_.next = prev_next_;
486 if (i::HandleScope::current_.limit != prev_limit_) {
487 i::HandleScope::current_.limit = prev_limit_;
488 i::HandleScope::DeleteExtensions();
489 }
490
491#ifdef DEBUG
492 i::HandleScope::ZapRange(prev_next_, prev_limit_);
493#endif
494}
495
496
Steve Blocka7e24c12009-10-30 11:49:00 +0000497int HandleScope::NumberOfHandles() {
498 return i::HandleScope::NumberOfHandles();
499}
500
501
502i::Object** v8::HandleScope::CreateHandle(i::Object* value) {
503 return i::HandleScope::CreateHandle(value);
504}
505
506
507void Context::Enter() {
508 if (IsDeadCheck("v8::Context::Enter()")) return;
509 ENTER_V8;
510 i::Handle<i::Context> env = Utils::OpenHandle(this);
511 thread_local.EnterContext(env);
512
513 thread_local.SaveContext(i::Top::context());
514 i::Top::set_context(*env);
515}
516
517
518void Context::Exit() {
519 if (!i::V8::IsRunning()) return;
520 if (!ApiCheck(thread_local.LeaveLastContext(),
521 "v8::Context::Exit()",
522 "Cannot exit non-entered context")) {
523 return;
524 }
525
526 // Content of 'last_context' could be NULL.
527 i::Context* last_context = thread_local.RestoreContext();
528 i::Top::set_context(last_context);
529}
530
531
Steve Blockd0582a62009-12-15 09:54:21 +0000532void Context::SetData(v8::Handle<String> data) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000533 if (IsDeadCheck("v8::Context::SetData()")) return;
534 ENTER_V8;
535 {
536 HandleScope scope;
537 i::Handle<i::Context> env = Utils::OpenHandle(this);
538 i::Handle<i::Object> raw_data = Utils::OpenHandle(*data);
539 ASSERT(env->IsGlobalContext());
540 if (env->IsGlobalContext()) {
541 env->set_data(*raw_data);
542 }
543 }
544}
545
546
547v8::Local<v8::Value> Context::GetData() {
548 if (IsDeadCheck("v8::Context::GetData()")) return v8::Local<Value>();
549 ENTER_V8;
550 i::Object* raw_result = NULL;
551 {
552 HandleScope scope;
553 i::Handle<i::Context> env = Utils::OpenHandle(this);
554 ASSERT(env->IsGlobalContext());
555 if (env->IsGlobalContext()) {
556 raw_result = env->data();
557 } else {
558 return Local<Value>();
559 }
560 }
561 i::Handle<i::Object> result(raw_result);
562 return Utils::ToLocal(result);
563}
564
565
566i::Object** v8::HandleScope::RawClose(i::Object** value) {
567 if (!ApiCheck(!is_closed_,
568 "v8::HandleScope::Close()",
569 "Local scope has already been closed")) {
570 return 0;
571 }
572 LOG_API("CloseHandleScope");
573
574 // Read the result before popping the handle block.
Steve Block6ded16b2010-05-10 14:33:55 +0100575 i::Object* result = NULL;
576 if (value != NULL) {
577 result = *value;
578 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000579 is_closed_ = true;
John Reck59135872010-11-02 12:39:01 -0700580 Leave();
Steve Blocka7e24c12009-10-30 11:49:00 +0000581
Steve Block6ded16b2010-05-10 14:33:55 +0100582 if (value == NULL) {
583 return NULL;
584 }
585
Steve Blocka7e24c12009-10-30 11:49:00 +0000586 // Allocate a new handle on the previous handle block.
587 i::Handle<i::Object> handle(result);
588 return handle.location();
589}
590
591
592// --- N e a n d e r ---
593
594
595// A constructor cannot easily return an error value, therefore it is necessary
596// to check for a dead VM with ON_BAILOUT before constructing any Neander
597// objects. To remind you about this there is no HandleScope in the
598// NeanderObject constructor. When you add one to the site calling the
599// constructor you should check that you ensured the VM was not dead first.
600NeanderObject::NeanderObject(int size) {
601 EnsureInitialized("v8::Nowhere");
602 ENTER_V8;
603 value_ = i::Factory::NewNeanderObject();
604 i::Handle<i::FixedArray> elements = i::Factory::NewFixedArray(size);
605 value_->set_elements(*elements);
606}
607
608
609int NeanderObject::size() {
610 return i::FixedArray::cast(value_->elements())->length();
611}
612
613
614NeanderArray::NeanderArray() : obj_(2) {
615 obj_.set(0, i::Smi::FromInt(0));
616}
617
618
619int NeanderArray::length() {
620 return i::Smi::cast(obj_.get(0))->value();
621}
622
623
624i::Object* NeanderArray::get(int offset) {
625 ASSERT(0 <= offset);
626 ASSERT(offset < length());
627 return obj_.get(offset + 1);
628}
629
630
631// This method cannot easily return an error value, therefore it is necessary
632// to check for a dead VM with ON_BAILOUT before calling it. To remind you
633// about this there is no HandleScope in this method. When you add one to the
634// site calling this method you should check that you ensured the VM was not
635// dead first.
636void NeanderArray::add(i::Handle<i::Object> value) {
637 int length = this->length();
638 int size = obj_.size();
639 if (length == size - 1) {
640 i::Handle<i::FixedArray> new_elms = i::Factory::NewFixedArray(2 * size);
641 for (int i = 0; i < length; i++)
642 new_elms->set(i + 1, get(i));
643 obj_.value()->set_elements(*new_elms);
644 }
645 obj_.set(length + 1, *value);
646 obj_.set(0, i::Smi::FromInt(length + 1));
647}
648
649
650void NeanderArray::set(int index, i::Object* value) {
651 if (index < 0 || index >= this->length()) return;
652 obj_.set(index + 1, value);
653}
654
655
656// --- T e m p l a t e ---
657
658
659static void InitializeTemplate(i::Handle<i::TemplateInfo> that, int type) {
660 that->set_tag(i::Smi::FromInt(type));
661}
662
663
664void Template::Set(v8::Handle<String> name, v8::Handle<Data> value,
665 v8::PropertyAttribute attribute) {
666 if (IsDeadCheck("v8::Template::SetProperty()")) return;
667 ENTER_V8;
668 HandleScope scope;
669 i::Handle<i::Object> list(Utils::OpenHandle(this)->property_list());
670 if (list->IsUndefined()) {
671 list = NeanderArray().value();
672 Utils::OpenHandle(this)->set_property_list(*list);
673 }
674 NeanderArray array(list);
675 array.add(Utils::OpenHandle(*name));
676 array.add(Utils::OpenHandle(*value));
677 array.add(Utils::OpenHandle(*v8::Integer::New(attribute)));
678}
679
680
681// --- F u n c t i o n T e m p l a t e ---
682static void InitializeFunctionTemplate(
683 i::Handle<i::FunctionTemplateInfo> info) {
684 info->set_tag(i::Smi::FromInt(Consts::FUNCTION_TEMPLATE));
685 info->set_flag(0);
686}
687
688
689Local<ObjectTemplate> FunctionTemplate::PrototypeTemplate() {
690 if (IsDeadCheck("v8::FunctionTemplate::PrototypeTemplate()")) {
691 return Local<ObjectTemplate>();
692 }
693 ENTER_V8;
694 i::Handle<i::Object> result(Utils::OpenHandle(this)->prototype_template());
695 if (result->IsUndefined()) {
696 result = Utils::OpenHandle(*ObjectTemplate::New());
697 Utils::OpenHandle(this)->set_prototype_template(*result);
698 }
699 return Local<ObjectTemplate>(ToApi<ObjectTemplate>(result));
700}
701
702
703void FunctionTemplate::Inherit(v8::Handle<FunctionTemplate> value) {
704 if (IsDeadCheck("v8::FunctionTemplate::Inherit()")) return;
705 ENTER_V8;
706 Utils::OpenHandle(this)->set_parent_template(*Utils::OpenHandle(*value));
707}
708
709
710// To distinguish the function templates, so that we can find them in the
711// function cache of the global context.
712static int next_serial_number = 0;
713
714
715Local<FunctionTemplate> FunctionTemplate::New(InvocationCallback callback,
716 v8::Handle<Value> data, v8::Handle<Signature> signature) {
717 EnsureInitialized("v8::FunctionTemplate::New()");
718 LOG_API("FunctionTemplate::New");
719 ENTER_V8;
720 i::Handle<i::Struct> struct_obj =
721 i::Factory::NewStruct(i::FUNCTION_TEMPLATE_INFO_TYPE);
722 i::Handle<i::FunctionTemplateInfo> obj =
723 i::Handle<i::FunctionTemplateInfo>::cast(struct_obj);
724 InitializeFunctionTemplate(obj);
725 obj->set_serial_number(i::Smi::FromInt(next_serial_number++));
726 if (callback != 0) {
727 if (data.IsEmpty()) data = v8::Undefined();
728 Utils::ToLocal(obj)->SetCallHandler(callback, data);
729 }
730 obj->set_undetectable(false);
731 obj->set_needs_access_check(false);
732
733 if (!signature.IsEmpty())
734 obj->set_signature(*Utils::OpenHandle(*signature));
735 return Utils::ToLocal(obj);
736}
737
738
739Local<Signature> Signature::New(Handle<FunctionTemplate> receiver,
740 int argc, Handle<FunctionTemplate> argv[]) {
741 EnsureInitialized("v8::Signature::New()");
742 LOG_API("Signature::New");
743 ENTER_V8;
744 i::Handle<i::Struct> struct_obj =
745 i::Factory::NewStruct(i::SIGNATURE_INFO_TYPE);
746 i::Handle<i::SignatureInfo> obj =
747 i::Handle<i::SignatureInfo>::cast(struct_obj);
748 if (!receiver.IsEmpty()) obj->set_receiver(*Utils::OpenHandle(*receiver));
749 if (argc > 0) {
750 i::Handle<i::FixedArray> args = i::Factory::NewFixedArray(argc);
751 for (int i = 0; i < argc; i++) {
752 if (!argv[i].IsEmpty())
753 args->set(i, *Utils::OpenHandle(*argv[i]));
754 }
755 obj->set_args(*args);
756 }
757 return Utils::ToLocal(obj);
758}
759
760
761Local<TypeSwitch> TypeSwitch::New(Handle<FunctionTemplate> type) {
762 Handle<FunctionTemplate> types[1] = { type };
763 return TypeSwitch::New(1, types);
764}
765
766
767Local<TypeSwitch> TypeSwitch::New(int argc, Handle<FunctionTemplate> types[]) {
768 EnsureInitialized("v8::TypeSwitch::New()");
769 LOG_API("TypeSwitch::New");
770 ENTER_V8;
771 i::Handle<i::FixedArray> vector = i::Factory::NewFixedArray(argc);
772 for (int i = 0; i < argc; i++)
773 vector->set(i, *Utils::OpenHandle(*types[i]));
774 i::Handle<i::Struct> struct_obj =
775 i::Factory::NewStruct(i::TYPE_SWITCH_INFO_TYPE);
776 i::Handle<i::TypeSwitchInfo> obj =
777 i::Handle<i::TypeSwitchInfo>::cast(struct_obj);
778 obj->set_types(*vector);
779 return Utils::ToLocal(obj);
780}
781
782
783int TypeSwitch::match(v8::Handle<Value> value) {
784 LOG_API("TypeSwitch::match");
785 i::Handle<i::Object> obj = Utils::OpenHandle(*value);
786 i::Handle<i::TypeSwitchInfo> info = Utils::OpenHandle(this);
787 i::FixedArray* types = i::FixedArray::cast(info->types());
788 for (int i = 0; i < types->length(); i++) {
789 if (obj->IsInstanceOf(i::FunctionTemplateInfo::cast(types->get(i))))
790 return i + 1;
791 }
792 return 0;
793}
794
795
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100796#define SET_FIELD_WRAPPED(obj, setter, cdata) do { \
797 i::Handle<i::Object> proxy = FromCData(cdata); \
798 (obj)->setter(*proxy); \
799 } while (false)
800
801
Steve Blocka7e24c12009-10-30 11:49:00 +0000802void FunctionTemplate::SetCallHandler(InvocationCallback callback,
803 v8::Handle<Value> data) {
804 if (IsDeadCheck("v8::FunctionTemplate::SetCallHandler()")) return;
805 ENTER_V8;
806 HandleScope scope;
807 i::Handle<i::Struct> struct_obj =
808 i::Factory::NewStruct(i::CALL_HANDLER_INFO_TYPE);
809 i::Handle<i::CallHandlerInfo> obj =
810 i::Handle<i::CallHandlerInfo>::cast(struct_obj);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100811 SET_FIELD_WRAPPED(obj, set_callback, callback);
Steve Blocka7e24c12009-10-30 11:49:00 +0000812 if (data.IsEmpty()) data = v8::Undefined();
813 obj->set_data(*Utils::OpenHandle(*data));
814 Utils::OpenHandle(this)->set_call_code(*obj);
815}
816
817
Leon Clarkef7060e22010-06-03 12:02:55 +0100818static i::Handle<i::AccessorInfo> MakeAccessorInfo(
819 v8::Handle<String> name,
820 AccessorGetter getter,
821 AccessorSetter setter,
822 v8::Handle<Value> data,
823 v8::AccessControl settings,
824 v8::PropertyAttribute attributes) {
825 i::Handle<i::AccessorInfo> obj = i::Factory::NewAccessorInfo();
826 ASSERT(getter != NULL);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100827 SET_FIELD_WRAPPED(obj, set_getter, getter);
828 SET_FIELD_WRAPPED(obj, set_setter, setter);
Leon Clarkef7060e22010-06-03 12:02:55 +0100829 if (data.IsEmpty()) data = v8::Undefined();
830 obj->set_data(*Utils::OpenHandle(*data));
831 obj->set_name(*Utils::OpenHandle(*name));
832 if (settings & ALL_CAN_READ) obj->set_all_can_read(true);
833 if (settings & ALL_CAN_WRITE) obj->set_all_can_write(true);
834 if (settings & PROHIBITS_OVERWRITING) obj->set_prohibits_overwriting(true);
835 obj->set_property_attributes(static_cast<PropertyAttributes>(attributes));
836 return obj;
837}
838
839
Steve Blocka7e24c12009-10-30 11:49:00 +0000840void FunctionTemplate::AddInstancePropertyAccessor(
841 v8::Handle<String> name,
842 AccessorGetter getter,
843 AccessorSetter setter,
844 v8::Handle<Value> data,
845 v8::AccessControl settings,
846 v8::PropertyAttribute attributes) {
847 if (IsDeadCheck("v8::FunctionTemplate::AddInstancePropertyAccessor()")) {
848 return;
849 }
850 ENTER_V8;
851 HandleScope scope;
Steve Blocka7e24c12009-10-30 11:49:00 +0000852
Leon Clarkef7060e22010-06-03 12:02:55 +0100853 i::Handle<i::AccessorInfo> obj = MakeAccessorInfo(name,
854 getter, setter, data,
855 settings, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +0000856 i::Handle<i::Object> list(Utils::OpenHandle(this)->property_accessors());
857 if (list->IsUndefined()) {
858 list = NeanderArray().value();
859 Utils::OpenHandle(this)->set_property_accessors(*list);
860 }
861 NeanderArray array(list);
862 array.add(obj);
863}
864
865
866Local<ObjectTemplate> FunctionTemplate::InstanceTemplate() {
867 if (IsDeadCheck("v8::FunctionTemplate::InstanceTemplate()")
868 || EmptyCheck("v8::FunctionTemplate::InstanceTemplate()", this))
869 return Local<ObjectTemplate>();
870 ENTER_V8;
871 if (Utils::OpenHandle(this)->instance_template()->IsUndefined()) {
872 Local<ObjectTemplate> templ =
873 ObjectTemplate::New(v8::Handle<FunctionTemplate>(this));
874 Utils::OpenHandle(this)->set_instance_template(*Utils::OpenHandle(*templ));
875 }
876 i::Handle<i::ObjectTemplateInfo> result(i::ObjectTemplateInfo::cast(
877 Utils::OpenHandle(this)->instance_template()));
878 return Utils::ToLocal(result);
879}
880
881
882void FunctionTemplate::SetClassName(Handle<String> name) {
883 if (IsDeadCheck("v8::FunctionTemplate::SetClassName()")) return;
884 ENTER_V8;
885 Utils::OpenHandle(this)->set_class_name(*Utils::OpenHandle(*name));
886}
887
888
889void FunctionTemplate::SetHiddenPrototype(bool value) {
890 if (IsDeadCheck("v8::FunctionTemplate::SetHiddenPrototype()")) return;
891 ENTER_V8;
892 Utils::OpenHandle(this)->set_hidden_prototype(value);
893}
894
895
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100896void FunctionTemplate::SetNamedInstancePropertyHandler(
Steve Blocka7e24c12009-10-30 11:49:00 +0000897 NamedPropertyGetter getter,
898 NamedPropertySetter setter,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100899 NamedPropertyQuery query,
Steve Blocka7e24c12009-10-30 11:49:00 +0000900 NamedPropertyDeleter remover,
901 NamedPropertyEnumerator enumerator,
902 Handle<Value> data) {
903 if (IsDeadCheck("v8::FunctionTemplate::SetNamedInstancePropertyHandler()")) {
904 return;
905 }
906 ENTER_V8;
907 HandleScope scope;
908 i::Handle<i::Struct> struct_obj =
909 i::Factory::NewStruct(i::INTERCEPTOR_INFO_TYPE);
910 i::Handle<i::InterceptorInfo> obj =
911 i::Handle<i::InterceptorInfo>::cast(struct_obj);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100912
913 if (getter != 0) SET_FIELD_WRAPPED(obj, set_getter, getter);
914 if (setter != 0) SET_FIELD_WRAPPED(obj, set_setter, setter);
915 if (query != 0) SET_FIELD_WRAPPED(obj, set_query, query);
916 if (remover != 0) SET_FIELD_WRAPPED(obj, set_deleter, remover);
917 if (enumerator != 0) SET_FIELD_WRAPPED(obj, set_enumerator, enumerator);
918
Steve Blocka7e24c12009-10-30 11:49:00 +0000919 if (data.IsEmpty()) data = v8::Undefined();
920 obj->set_data(*Utils::OpenHandle(*data));
921 Utils::OpenHandle(this)->set_named_property_handler(*obj);
922}
923
924
925void FunctionTemplate::SetIndexedInstancePropertyHandler(
926 IndexedPropertyGetter getter,
927 IndexedPropertySetter setter,
928 IndexedPropertyQuery query,
929 IndexedPropertyDeleter remover,
930 IndexedPropertyEnumerator enumerator,
931 Handle<Value> data) {
932 if (IsDeadCheck(
933 "v8::FunctionTemplate::SetIndexedInstancePropertyHandler()")) {
934 return;
935 }
936 ENTER_V8;
937 HandleScope scope;
938 i::Handle<i::Struct> struct_obj =
939 i::Factory::NewStruct(i::INTERCEPTOR_INFO_TYPE);
940 i::Handle<i::InterceptorInfo> obj =
941 i::Handle<i::InterceptorInfo>::cast(struct_obj);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100942
943 if (getter != 0) SET_FIELD_WRAPPED(obj, set_getter, getter);
944 if (setter != 0) SET_FIELD_WRAPPED(obj, set_setter, setter);
945 if (query != 0) SET_FIELD_WRAPPED(obj, set_query, query);
946 if (remover != 0) SET_FIELD_WRAPPED(obj, set_deleter, remover);
947 if (enumerator != 0) SET_FIELD_WRAPPED(obj, set_enumerator, enumerator);
948
Steve Blocka7e24c12009-10-30 11:49:00 +0000949 if (data.IsEmpty()) data = v8::Undefined();
950 obj->set_data(*Utils::OpenHandle(*data));
951 Utils::OpenHandle(this)->set_indexed_property_handler(*obj);
952}
953
954
955void FunctionTemplate::SetInstanceCallAsFunctionHandler(
956 InvocationCallback callback,
957 Handle<Value> data) {
958 if (IsDeadCheck("v8::FunctionTemplate::SetInstanceCallAsFunctionHandler()")) {
959 return;
960 }
961 ENTER_V8;
962 HandleScope scope;
963 i::Handle<i::Struct> struct_obj =
964 i::Factory::NewStruct(i::CALL_HANDLER_INFO_TYPE);
965 i::Handle<i::CallHandlerInfo> obj =
966 i::Handle<i::CallHandlerInfo>::cast(struct_obj);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100967 SET_FIELD_WRAPPED(obj, set_callback, callback);
Steve Blocka7e24c12009-10-30 11:49:00 +0000968 if (data.IsEmpty()) data = v8::Undefined();
969 obj->set_data(*Utils::OpenHandle(*data));
970 Utils::OpenHandle(this)->set_instance_call_handler(*obj);
971}
972
973
974// --- O b j e c t T e m p l a t e ---
975
976
977Local<ObjectTemplate> ObjectTemplate::New() {
978 return New(Local<FunctionTemplate>());
979}
980
981
982Local<ObjectTemplate> ObjectTemplate::New(
983 v8::Handle<FunctionTemplate> constructor) {
984 if (IsDeadCheck("v8::ObjectTemplate::New()")) return Local<ObjectTemplate>();
985 EnsureInitialized("v8::ObjectTemplate::New()");
986 LOG_API("ObjectTemplate::New");
987 ENTER_V8;
988 i::Handle<i::Struct> struct_obj =
989 i::Factory::NewStruct(i::OBJECT_TEMPLATE_INFO_TYPE);
990 i::Handle<i::ObjectTemplateInfo> obj =
991 i::Handle<i::ObjectTemplateInfo>::cast(struct_obj);
992 InitializeTemplate(obj, Consts::OBJECT_TEMPLATE);
993 if (!constructor.IsEmpty())
994 obj->set_constructor(*Utils::OpenHandle(*constructor));
995 obj->set_internal_field_count(i::Smi::FromInt(0));
996 return Utils::ToLocal(obj);
997}
998
999
1000// Ensure that the object template has a constructor. If no
1001// constructor is available we create one.
1002static void EnsureConstructor(ObjectTemplate* object_template) {
1003 if (Utils::OpenHandle(object_template)->constructor()->IsUndefined()) {
1004 Local<FunctionTemplate> templ = FunctionTemplate::New();
1005 i::Handle<i::FunctionTemplateInfo> constructor = Utils::OpenHandle(*templ);
1006 constructor->set_instance_template(*Utils::OpenHandle(object_template));
1007 Utils::OpenHandle(object_template)->set_constructor(*constructor);
1008 }
1009}
1010
1011
1012void ObjectTemplate::SetAccessor(v8::Handle<String> name,
1013 AccessorGetter getter,
1014 AccessorSetter setter,
1015 v8::Handle<Value> data,
1016 AccessControl settings,
1017 PropertyAttribute attribute) {
1018 if (IsDeadCheck("v8::ObjectTemplate::SetAccessor()")) return;
1019 ENTER_V8;
1020 HandleScope scope;
1021 EnsureConstructor(this);
1022 i::FunctionTemplateInfo* constructor =
1023 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1024 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1025 Utils::ToLocal(cons)->AddInstancePropertyAccessor(name,
1026 getter,
1027 setter,
1028 data,
1029 settings,
1030 attribute);
1031}
1032
1033
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001034void ObjectTemplate::SetNamedPropertyHandler(NamedPropertyGetter getter,
1035 NamedPropertySetter setter,
1036 NamedPropertyQuery query,
1037 NamedPropertyDeleter remover,
1038 NamedPropertyEnumerator enumerator,
1039 Handle<Value> data) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001040 if (IsDeadCheck("v8::ObjectTemplate::SetNamedPropertyHandler()")) return;
1041 ENTER_V8;
1042 HandleScope scope;
1043 EnsureConstructor(this);
1044 i::FunctionTemplateInfo* constructor =
1045 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1046 i::Handle<i::FunctionTemplateInfo> cons(constructor);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001047 Utils::ToLocal(cons)->SetNamedInstancePropertyHandler(getter,
1048 setter,
1049 query,
1050 remover,
1051 enumerator,
1052 data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001053}
1054
1055
1056void ObjectTemplate::MarkAsUndetectable() {
1057 if (IsDeadCheck("v8::ObjectTemplate::MarkAsUndetectable()")) return;
1058 ENTER_V8;
1059 HandleScope scope;
1060 EnsureConstructor(this);
1061 i::FunctionTemplateInfo* constructor =
1062 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1063 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1064 cons->set_undetectable(true);
1065}
1066
1067
1068void ObjectTemplate::SetAccessCheckCallbacks(
1069 NamedSecurityCallback named_callback,
1070 IndexedSecurityCallback indexed_callback,
1071 Handle<Value> data,
1072 bool turned_on_by_default) {
1073 if (IsDeadCheck("v8::ObjectTemplate::SetAccessCheckCallbacks()")) return;
1074 ENTER_V8;
1075 HandleScope scope;
1076 EnsureConstructor(this);
1077
1078 i::Handle<i::Struct> struct_info =
1079 i::Factory::NewStruct(i::ACCESS_CHECK_INFO_TYPE);
1080 i::Handle<i::AccessCheckInfo> info =
1081 i::Handle<i::AccessCheckInfo>::cast(struct_info);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001082
1083 SET_FIELD_WRAPPED(info, set_named_callback, named_callback);
1084 SET_FIELD_WRAPPED(info, set_indexed_callback, indexed_callback);
1085
Steve Blocka7e24c12009-10-30 11:49:00 +00001086 if (data.IsEmpty()) data = v8::Undefined();
1087 info->set_data(*Utils::OpenHandle(*data));
1088
1089 i::FunctionTemplateInfo* constructor =
1090 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1091 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1092 cons->set_access_check_info(*info);
1093 cons->set_needs_access_check(turned_on_by_default);
1094}
1095
1096
1097void ObjectTemplate::SetIndexedPropertyHandler(
1098 IndexedPropertyGetter getter,
1099 IndexedPropertySetter setter,
1100 IndexedPropertyQuery query,
1101 IndexedPropertyDeleter remover,
1102 IndexedPropertyEnumerator enumerator,
1103 Handle<Value> data) {
1104 if (IsDeadCheck("v8::ObjectTemplate::SetIndexedPropertyHandler()")) return;
1105 ENTER_V8;
1106 HandleScope scope;
1107 EnsureConstructor(this);
1108 i::FunctionTemplateInfo* constructor =
1109 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1110 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1111 Utils::ToLocal(cons)->SetIndexedInstancePropertyHandler(getter,
1112 setter,
1113 query,
1114 remover,
1115 enumerator,
1116 data);
1117}
1118
1119
1120void ObjectTemplate::SetCallAsFunctionHandler(InvocationCallback callback,
1121 Handle<Value> data) {
1122 if (IsDeadCheck("v8::ObjectTemplate::SetCallAsFunctionHandler()")) return;
1123 ENTER_V8;
1124 HandleScope scope;
1125 EnsureConstructor(this);
1126 i::FunctionTemplateInfo* constructor =
1127 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1128 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1129 Utils::ToLocal(cons)->SetInstanceCallAsFunctionHandler(callback, data);
1130}
1131
1132
1133int ObjectTemplate::InternalFieldCount() {
1134 if (IsDeadCheck("v8::ObjectTemplate::InternalFieldCount()")) {
1135 return 0;
1136 }
1137 return i::Smi::cast(Utils::OpenHandle(this)->internal_field_count())->value();
1138}
1139
1140
1141void ObjectTemplate::SetInternalFieldCount(int value) {
1142 if (IsDeadCheck("v8::ObjectTemplate::SetInternalFieldCount()")) return;
1143 if (!ApiCheck(i::Smi::IsValid(value),
1144 "v8::ObjectTemplate::SetInternalFieldCount()",
1145 "Invalid internal field count")) {
1146 return;
1147 }
1148 ENTER_V8;
1149 if (value > 0) {
1150 // The internal field count is set by the constructor function's
1151 // construct code, so we ensure that there is a constructor
1152 // function to do the setting.
1153 EnsureConstructor(this);
1154 }
1155 Utils::OpenHandle(this)->set_internal_field_count(i::Smi::FromInt(value));
1156}
1157
1158
1159// --- S c r i p t D a t a ---
1160
1161
1162ScriptData* ScriptData::PreCompile(const char* input, int length) {
1163 unibrow::Utf8InputBuffer<> buf(input, length);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001164 return i::ParserApi::PreParse(i::Handle<i::String>(), &buf, NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +00001165}
1166
1167
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001168ScriptData* ScriptData::PreCompile(v8::Handle<String> source) {
1169 i::Handle<i::String> str = Utils::OpenHandle(*source);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001170 return i::ParserApi::PreParse(str, NULL, NULL);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001171}
1172
1173
Leon Clarkef7060e22010-06-03 12:02:55 +01001174ScriptData* ScriptData::New(const char* data, int length) {
1175 // Return an empty ScriptData if the length is obviously invalid.
1176 if (length % sizeof(unsigned) != 0) {
Iain Merrick9ac36c92010-09-13 15:29:50 +01001177 return new i::ScriptDataImpl();
Leon Clarkef7060e22010-06-03 12:02:55 +01001178 }
1179
1180 // Copy the data to ensure it is properly aligned.
1181 int deserialized_data_length = length / sizeof(unsigned);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001182 // If aligned, don't create a copy of the data.
1183 if (reinterpret_cast<intptr_t>(data) % sizeof(unsigned) == 0) {
1184 return new i::ScriptDataImpl(data, length);
1185 }
1186 // Copy the data to align it.
Leon Clarkef7060e22010-06-03 12:02:55 +01001187 unsigned* deserialized_data = i::NewArray<unsigned>(deserialized_data_length);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001188 i::MemCopy(deserialized_data, data, length);
Leon Clarkef7060e22010-06-03 12:02:55 +01001189
1190 return new i::ScriptDataImpl(
1191 i::Vector<unsigned>(deserialized_data, deserialized_data_length));
Steve Blocka7e24c12009-10-30 11:49:00 +00001192}
1193
1194
1195// --- S c r i p t ---
1196
1197
1198Local<Script> Script::New(v8::Handle<String> source,
1199 v8::ScriptOrigin* origin,
Andrei Popescu402d9372010-02-26 13:31:12 +00001200 v8::ScriptData* pre_data,
1201 v8::Handle<String> script_data) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001202 ON_BAILOUT("v8::Script::New()", return Local<Script>());
1203 LOG_API("Script::New");
1204 ENTER_V8;
1205 i::Handle<i::String> str = Utils::OpenHandle(*source);
1206 i::Handle<i::Object> name_obj;
1207 int line_offset = 0;
1208 int column_offset = 0;
1209 if (origin != NULL) {
1210 if (!origin->ResourceName().IsEmpty()) {
1211 name_obj = Utils::OpenHandle(*origin->ResourceName());
1212 }
1213 if (!origin->ResourceLineOffset().IsEmpty()) {
1214 line_offset = static_cast<int>(origin->ResourceLineOffset()->Value());
1215 }
1216 if (!origin->ResourceColumnOffset().IsEmpty()) {
1217 column_offset = static_cast<int>(origin->ResourceColumnOffset()->Value());
1218 }
1219 }
1220 EXCEPTION_PREAMBLE();
Andrei Popescu402d9372010-02-26 13:31:12 +00001221 i::ScriptDataImpl* pre_data_impl = static_cast<i::ScriptDataImpl*>(pre_data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001222 // We assert that the pre-data is sane, even though we can actually
1223 // handle it if it turns out not to be in release mode.
Andrei Popescu402d9372010-02-26 13:31:12 +00001224 ASSERT(pre_data_impl == NULL || pre_data_impl->SanityCheck());
Steve Blocka7e24c12009-10-30 11:49:00 +00001225 // If the pre-data isn't sane we simply ignore it
Andrei Popescu402d9372010-02-26 13:31:12 +00001226 if (pre_data_impl != NULL && !pre_data_impl->SanityCheck()) {
1227 pre_data_impl = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00001228 }
Steve Block6ded16b2010-05-10 14:33:55 +01001229 i::Handle<i::SharedFunctionInfo> result =
Andrei Popescu31002712010-02-23 13:46:05 +00001230 i::Compiler::Compile(str,
1231 name_obj,
1232 line_offset,
1233 column_offset,
1234 NULL,
Andrei Popescu402d9372010-02-26 13:31:12 +00001235 pre_data_impl,
1236 Utils::OpenHandle(*script_data),
Andrei Popescu31002712010-02-23 13:46:05 +00001237 i::NOT_NATIVES_CODE);
Steve Block6ded16b2010-05-10 14:33:55 +01001238 has_pending_exception = result.is_null();
Steve Blocka7e24c12009-10-30 11:49:00 +00001239 EXCEPTION_BAILOUT_CHECK(Local<Script>());
Steve Block6ded16b2010-05-10 14:33:55 +01001240 return Local<Script>(ToApi<Script>(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00001241}
1242
1243
1244Local<Script> Script::New(v8::Handle<String> source,
1245 v8::Handle<Value> file_name) {
1246 ScriptOrigin origin(file_name);
1247 return New(source, &origin);
1248}
1249
1250
1251Local<Script> Script::Compile(v8::Handle<String> source,
1252 v8::ScriptOrigin* origin,
Andrei Popescu402d9372010-02-26 13:31:12 +00001253 v8::ScriptData* pre_data,
1254 v8::Handle<String> script_data) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001255 ON_BAILOUT("v8::Script::Compile()", return Local<Script>());
1256 LOG_API("Script::Compile");
1257 ENTER_V8;
Andrei Popescu402d9372010-02-26 13:31:12 +00001258 Local<Script> generic = New(source, origin, pre_data, script_data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001259 if (generic.IsEmpty())
1260 return generic;
Steve Block6ded16b2010-05-10 14:33:55 +01001261 i::Handle<i::Object> obj = Utils::OpenHandle(*generic);
1262 i::Handle<i::SharedFunctionInfo> function =
1263 i::Handle<i::SharedFunctionInfo>(i::SharedFunctionInfo::cast(*obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00001264 i::Handle<i::JSFunction> result =
Steve Block6ded16b2010-05-10 14:33:55 +01001265 i::Factory::NewFunctionFromSharedFunctionInfo(function,
1266 i::Top::global_context());
Steve Blocka7e24c12009-10-30 11:49:00 +00001267 return Local<Script>(ToApi<Script>(result));
1268}
1269
1270
1271Local<Script> Script::Compile(v8::Handle<String> source,
Andrei Popescu402d9372010-02-26 13:31:12 +00001272 v8::Handle<Value> file_name,
1273 v8::Handle<String> script_data) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001274 ScriptOrigin origin(file_name);
Andrei Popescu402d9372010-02-26 13:31:12 +00001275 return Compile(source, &origin, 0, script_data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001276}
1277
1278
1279Local<Value> Script::Run() {
1280 ON_BAILOUT("v8::Script::Run()", return Local<Value>());
1281 LOG_API("Script::Run");
1282 ENTER_V8;
1283 i::Object* raw_result = NULL;
1284 {
1285 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +01001286 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1287 i::Handle<i::JSFunction> fun;
1288 if (obj->IsSharedFunctionInfo()) {
1289 i::Handle<i::SharedFunctionInfo>
1290 function_info(i::SharedFunctionInfo::cast(*obj));
1291 fun = i::Factory::NewFunctionFromSharedFunctionInfo(
1292 function_info, i::Top::global_context());
1293 } else {
1294 fun = i::Handle<i::JSFunction>(i::JSFunction::cast(*obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00001295 }
1296 EXCEPTION_PREAMBLE();
1297 i::Handle<i::Object> receiver(i::Top::context()->global_proxy());
1298 i::Handle<i::Object> result =
1299 i::Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
1300 EXCEPTION_BAILOUT_CHECK(Local<Value>());
1301 raw_result = *result;
1302 }
1303 i::Handle<i::Object> result(raw_result);
1304 return Utils::ToLocal(result);
1305}
1306
1307
Steve Block6ded16b2010-05-10 14:33:55 +01001308static i::Handle<i::SharedFunctionInfo> OpenScript(Script* script) {
1309 i::Handle<i::Object> obj = Utils::OpenHandle(script);
1310 i::Handle<i::SharedFunctionInfo> result;
1311 if (obj->IsSharedFunctionInfo()) {
1312 result =
1313 i::Handle<i::SharedFunctionInfo>(i::SharedFunctionInfo::cast(*obj));
1314 } else {
1315 result =
1316 i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared());
1317 }
1318 return result;
1319}
1320
1321
Steve Blocka7e24c12009-10-30 11:49:00 +00001322Local<Value> Script::Id() {
1323 ON_BAILOUT("v8::Script::Id()", return Local<Value>());
1324 LOG_API("Script::Id");
1325 i::Object* raw_id = NULL;
1326 {
1327 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +01001328 i::Handle<i::SharedFunctionInfo> function_info = OpenScript(this);
1329 i::Handle<i::Script> script(i::Script::cast(function_info->script()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001330 i::Handle<i::Object> id(script->id());
1331 raw_id = *id;
1332 }
1333 i::Handle<i::Object> id(raw_id);
1334 return Utils::ToLocal(id);
1335}
1336
1337
Steve Blockd0582a62009-12-15 09:54:21 +00001338void Script::SetData(v8::Handle<String> data) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001339 ON_BAILOUT("v8::Script::SetData()", return);
1340 LOG_API("Script::SetData");
1341 {
1342 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +01001343 i::Handle<i::SharedFunctionInfo> function_info = OpenScript(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001344 i::Handle<i::Object> raw_data = Utils::OpenHandle(*data);
Steve Block6ded16b2010-05-10 14:33:55 +01001345 i::Handle<i::Script> script(i::Script::cast(function_info->script()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001346 script->set_data(*raw_data);
1347 }
1348}
1349
1350
1351// --- E x c e p t i o n s ---
1352
1353
1354v8::TryCatch::TryCatch()
Steve Blockd0582a62009-12-15 09:54:21 +00001355 : next_(i::Top::try_catch_handler_address()),
Steve Blocka7e24c12009-10-30 11:49:00 +00001356 exception_(i::Heap::the_hole_value()),
1357 message_(i::Smi::FromInt(0)),
1358 is_verbose_(false),
1359 can_continue_(true),
1360 capture_message_(true),
Steve Blockd0582a62009-12-15 09:54:21 +00001361 rethrow_(false) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001362 i::Top::RegisterTryCatchHandler(this);
1363}
1364
1365
1366v8::TryCatch::~TryCatch() {
Steve Blockd0582a62009-12-15 09:54:21 +00001367 if (rethrow_) {
1368 v8::HandleScope scope;
1369 v8::Local<v8::Value> exc = v8::Local<v8::Value>::New(Exception());
1370 i::Top::UnregisterTryCatchHandler(this);
1371 v8::ThrowException(exc);
1372 } else {
1373 i::Top::UnregisterTryCatchHandler(this);
1374 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001375}
1376
1377
1378bool v8::TryCatch::HasCaught() const {
1379 return !reinterpret_cast<i::Object*>(exception_)->IsTheHole();
1380}
1381
1382
1383bool v8::TryCatch::CanContinue() const {
1384 return can_continue_;
1385}
1386
1387
Steve Blockd0582a62009-12-15 09:54:21 +00001388v8::Handle<v8::Value> v8::TryCatch::ReThrow() {
1389 if (!HasCaught()) return v8::Local<v8::Value>();
1390 rethrow_ = true;
1391 return v8::Undefined();
1392}
1393
1394
Steve Blocka7e24c12009-10-30 11:49:00 +00001395v8::Local<Value> v8::TryCatch::Exception() const {
1396 if (HasCaught()) {
1397 // Check for out of memory exception.
1398 i::Object* exception = reinterpret_cast<i::Object*>(exception_);
1399 return v8::Utils::ToLocal(i::Handle<i::Object>(exception));
1400 } else {
1401 return v8::Local<Value>();
1402 }
1403}
1404
1405
1406v8::Local<Value> v8::TryCatch::StackTrace() const {
1407 if (HasCaught()) {
1408 i::Object* raw_obj = reinterpret_cast<i::Object*>(exception_);
1409 if (!raw_obj->IsJSObject()) return v8::Local<Value>();
1410 v8::HandleScope scope;
1411 i::Handle<i::JSObject> obj(i::JSObject::cast(raw_obj));
1412 i::Handle<i::String> name = i::Factory::LookupAsciiSymbol("stack");
1413 if (!obj->HasProperty(*name))
1414 return v8::Local<Value>();
1415 return scope.Close(v8::Utils::ToLocal(i::GetProperty(obj, name)));
1416 } else {
1417 return v8::Local<Value>();
1418 }
1419}
1420
1421
1422v8::Local<v8::Message> v8::TryCatch::Message() const {
1423 if (HasCaught() && message_ != i::Smi::FromInt(0)) {
1424 i::Object* message = reinterpret_cast<i::Object*>(message_);
1425 return v8::Utils::MessageToLocal(i::Handle<i::Object>(message));
1426 } else {
1427 return v8::Local<v8::Message>();
1428 }
1429}
1430
1431
1432void v8::TryCatch::Reset() {
1433 exception_ = i::Heap::the_hole_value();
1434 message_ = i::Smi::FromInt(0);
1435}
1436
1437
1438void v8::TryCatch::SetVerbose(bool value) {
1439 is_verbose_ = value;
1440}
1441
1442
1443void v8::TryCatch::SetCaptureMessage(bool value) {
1444 capture_message_ = value;
1445}
1446
1447
1448// --- M e s s a g e ---
1449
1450
1451Local<String> Message::Get() const {
1452 ON_BAILOUT("v8::Message::Get()", return Local<String>());
1453 ENTER_V8;
1454 HandleScope scope;
1455 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1456 i::Handle<i::String> raw_result = i::MessageHandler::GetMessage(obj);
1457 Local<String> result = Utils::ToLocal(raw_result);
1458 return scope.Close(result);
1459}
1460
1461
1462v8::Handle<Value> Message::GetScriptResourceName() const {
1463 if (IsDeadCheck("v8::Message::GetScriptResourceName()")) {
1464 return Local<String>();
1465 }
1466 ENTER_V8;
1467 HandleScope scope;
1468 i::Handle<i::JSObject> obj =
1469 i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
1470 // Return this.script.name.
1471 i::Handle<i::JSValue> script =
1472 i::Handle<i::JSValue>::cast(GetProperty(obj, "script"));
1473 i::Handle<i::Object> resource_name(i::Script::cast(script->value())->name());
1474 return scope.Close(Utils::ToLocal(resource_name));
1475}
1476
1477
1478v8::Handle<Value> Message::GetScriptData() const {
1479 if (IsDeadCheck("v8::Message::GetScriptResourceData()")) {
1480 return Local<Value>();
1481 }
1482 ENTER_V8;
1483 HandleScope scope;
1484 i::Handle<i::JSObject> obj =
1485 i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
1486 // Return this.script.data.
1487 i::Handle<i::JSValue> script =
1488 i::Handle<i::JSValue>::cast(GetProperty(obj, "script"));
1489 i::Handle<i::Object> data(i::Script::cast(script->value())->data());
1490 return scope.Close(Utils::ToLocal(data));
1491}
1492
1493
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001494v8::Handle<v8::StackTrace> Message::GetStackTrace() const {
1495 if (IsDeadCheck("v8::Message::GetStackTrace()")) {
1496 return Local<v8::StackTrace>();
1497 }
1498 ENTER_V8;
1499 HandleScope scope;
1500 i::Handle<i::JSObject> obj =
1501 i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
1502 i::Handle<i::Object> stackFramesObj = GetProperty(obj, "stackFrames");
1503 if (!stackFramesObj->IsJSArray()) return v8::Handle<v8::StackTrace>();
1504 i::Handle<i::JSArray> stackTrace =
1505 i::Handle<i::JSArray>::cast(stackFramesObj);
1506 return scope.Close(Utils::StackTraceToLocal(stackTrace));
1507}
1508
1509
Steve Blocka7e24c12009-10-30 11:49:00 +00001510static i::Handle<i::Object> CallV8HeapFunction(const char* name,
1511 i::Handle<i::Object> recv,
1512 int argc,
1513 i::Object** argv[],
1514 bool* has_pending_exception) {
1515 i::Handle<i::String> fmt_str = i::Factory::LookupAsciiSymbol(name);
John Reck59135872010-11-02 12:39:01 -07001516 i::Object* object_fun =
1517 i::Top::builtins()->GetPropertyNoExceptionThrown(*fmt_str);
Steve Blocka7e24c12009-10-30 11:49:00 +00001518 i::Handle<i::JSFunction> fun =
1519 i::Handle<i::JSFunction>(i::JSFunction::cast(object_fun));
1520 i::Handle<i::Object> value =
1521 i::Execution::Call(fun, recv, argc, argv, has_pending_exception);
1522 return value;
1523}
1524
1525
1526static i::Handle<i::Object> CallV8HeapFunction(const char* name,
1527 i::Handle<i::Object> data,
1528 bool* has_pending_exception) {
1529 i::Object** argv[1] = { data.location() };
1530 return CallV8HeapFunction(name,
1531 i::Top::builtins(),
1532 1,
1533 argv,
1534 has_pending_exception);
1535}
1536
1537
1538int Message::GetLineNumber() const {
Kristian Monsen25f61362010-05-21 11:50:48 +01001539 ON_BAILOUT("v8::Message::GetLineNumber()", return kNoLineNumberInfo);
Steve Blocka7e24c12009-10-30 11:49:00 +00001540 ENTER_V8;
1541 HandleScope scope;
1542 EXCEPTION_PREAMBLE();
1543 i::Handle<i::Object> result = CallV8HeapFunction("GetLineNumber",
1544 Utils::OpenHandle(this),
1545 &has_pending_exception);
1546 EXCEPTION_BAILOUT_CHECK(0);
1547 return static_cast<int>(result->Number());
1548}
1549
1550
1551int Message::GetStartPosition() const {
1552 if (IsDeadCheck("v8::Message::GetStartPosition()")) return 0;
1553 ENTER_V8;
1554 HandleScope scope;
1555
1556 i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this);
1557 return static_cast<int>(GetProperty(data_obj, "startPos")->Number());
1558}
1559
1560
1561int Message::GetEndPosition() const {
1562 if (IsDeadCheck("v8::Message::GetEndPosition()")) return 0;
1563 ENTER_V8;
1564 HandleScope scope;
1565 i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this);
1566 return static_cast<int>(GetProperty(data_obj, "endPos")->Number());
1567}
1568
1569
1570int Message::GetStartColumn() const {
Kristian Monsen25f61362010-05-21 11:50:48 +01001571 if (IsDeadCheck("v8::Message::GetStartColumn()")) return kNoColumnInfo;
Steve Blocka7e24c12009-10-30 11:49:00 +00001572 ENTER_V8;
1573 HandleScope scope;
1574 i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this);
1575 EXCEPTION_PREAMBLE();
1576 i::Handle<i::Object> start_col_obj = CallV8HeapFunction(
1577 "GetPositionInLine",
1578 data_obj,
1579 &has_pending_exception);
1580 EXCEPTION_BAILOUT_CHECK(0);
1581 return static_cast<int>(start_col_obj->Number());
1582}
1583
1584
1585int Message::GetEndColumn() const {
Kristian Monsen25f61362010-05-21 11:50:48 +01001586 if (IsDeadCheck("v8::Message::GetEndColumn()")) return kNoColumnInfo;
Steve Blocka7e24c12009-10-30 11:49:00 +00001587 ENTER_V8;
1588 HandleScope scope;
1589 i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this);
1590 EXCEPTION_PREAMBLE();
1591 i::Handle<i::Object> start_col_obj = CallV8HeapFunction(
1592 "GetPositionInLine",
1593 data_obj,
1594 &has_pending_exception);
1595 EXCEPTION_BAILOUT_CHECK(0);
1596 int start = static_cast<int>(GetProperty(data_obj, "startPos")->Number());
1597 int end = static_cast<int>(GetProperty(data_obj, "endPos")->Number());
1598 return static_cast<int>(start_col_obj->Number()) + (end - start);
1599}
1600
1601
1602Local<String> Message::GetSourceLine() const {
1603 ON_BAILOUT("v8::Message::GetSourceLine()", return Local<String>());
1604 ENTER_V8;
1605 HandleScope scope;
1606 EXCEPTION_PREAMBLE();
1607 i::Handle<i::Object> result = CallV8HeapFunction("GetSourceLine",
1608 Utils::OpenHandle(this),
1609 &has_pending_exception);
1610 EXCEPTION_BAILOUT_CHECK(Local<v8::String>());
1611 if (result->IsString()) {
1612 return scope.Close(Utils::ToLocal(i::Handle<i::String>::cast(result)));
1613 } else {
1614 return Local<String>();
1615 }
1616}
1617
1618
1619void Message::PrintCurrentStackTrace(FILE* out) {
1620 if (IsDeadCheck("v8::Message::PrintCurrentStackTrace()")) return;
1621 ENTER_V8;
1622 i::Top::PrintCurrentStackTrace(out);
1623}
1624
1625
Kristian Monsen25f61362010-05-21 11:50:48 +01001626// --- S t a c k T r a c e ---
1627
1628Local<StackFrame> StackTrace::GetFrame(uint32_t index) const {
1629 if (IsDeadCheck("v8::StackTrace::GetFrame()")) return Local<StackFrame>();
1630 ENTER_V8;
1631 HandleScope scope;
1632 i::Handle<i::JSArray> self = Utils::OpenHandle(this);
John Reck59135872010-11-02 12:39:01 -07001633 i::Object* raw_object = self->GetElementNoExceptionThrown(index);
1634 i::Handle<i::JSObject> obj(i::JSObject::cast(raw_object));
Kristian Monsen25f61362010-05-21 11:50:48 +01001635 return scope.Close(Utils::StackFrameToLocal(obj));
1636}
1637
1638
1639int StackTrace::GetFrameCount() const {
1640 if (IsDeadCheck("v8::StackTrace::GetFrameCount()")) return -1;
1641 ENTER_V8;
1642 return i::Smi::cast(Utils::OpenHandle(this)->length())->value();
1643}
1644
1645
1646Local<Array> StackTrace::AsArray() {
1647 if (IsDeadCheck("v8::StackTrace::AsArray()")) Local<Array>();
1648 ENTER_V8;
1649 return Utils::ToLocal(Utils::OpenHandle(this));
1650}
1651
1652
1653Local<StackTrace> StackTrace::CurrentStackTrace(int frame_limit,
1654 StackTraceOptions options) {
1655 if (IsDeadCheck("v8::StackTrace::CurrentStackTrace()")) Local<StackTrace>();
1656 ENTER_V8;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001657 i::Handle<i::JSArray> stackTrace =
1658 i::Top::CaptureCurrentStackTrace(frame_limit, options);
1659 return Utils::StackTraceToLocal(stackTrace);
Kristian Monsen25f61362010-05-21 11:50:48 +01001660}
1661
1662
1663// --- S t a c k F r a m e ---
1664
1665int StackFrame::GetLineNumber() const {
1666 if (IsDeadCheck("v8::StackFrame::GetLineNumber()")) {
1667 return Message::kNoLineNumberInfo;
1668 }
1669 ENTER_V8;
1670 i::HandleScope scope;
1671 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
1672 i::Handle<i::Object> line = GetProperty(self, "lineNumber");
1673 if (!line->IsSmi()) {
1674 return Message::kNoLineNumberInfo;
1675 }
1676 return i::Smi::cast(*line)->value();
1677}
1678
1679
1680int StackFrame::GetColumn() const {
1681 if (IsDeadCheck("v8::StackFrame::GetColumn()")) {
1682 return Message::kNoColumnInfo;
1683 }
1684 ENTER_V8;
1685 i::HandleScope scope;
1686 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
1687 i::Handle<i::Object> column = GetProperty(self, "column");
1688 if (!column->IsSmi()) {
1689 return Message::kNoColumnInfo;
1690 }
1691 return i::Smi::cast(*column)->value();
1692}
1693
1694
1695Local<String> StackFrame::GetScriptName() const {
1696 if (IsDeadCheck("v8::StackFrame::GetScriptName()")) return Local<String>();
1697 ENTER_V8;
1698 HandleScope scope;
1699 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
1700 i::Handle<i::Object> name = GetProperty(self, "scriptName");
1701 if (!name->IsString()) {
1702 return Local<String>();
1703 }
1704 return scope.Close(Local<String>::Cast(Utils::ToLocal(name)));
1705}
1706
1707
Ben Murdochf87a2032010-10-22 12:50:53 +01001708Local<String> StackFrame::GetScriptNameOrSourceURL() const {
1709 if (IsDeadCheck("v8::StackFrame::GetScriptNameOrSourceURL()")) {
1710 return Local<String>();
1711 }
1712 ENTER_V8;
1713 HandleScope scope;
1714 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
1715 i::Handle<i::Object> name = GetProperty(self, "scriptNameOrSourceURL");
1716 if (!name->IsString()) {
1717 return Local<String>();
1718 }
1719 return scope.Close(Local<String>::Cast(Utils::ToLocal(name)));
1720}
1721
1722
Kristian Monsen25f61362010-05-21 11:50:48 +01001723Local<String> StackFrame::GetFunctionName() const {
1724 if (IsDeadCheck("v8::StackFrame::GetFunctionName()")) return Local<String>();
1725 ENTER_V8;
1726 HandleScope scope;
1727 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
1728 i::Handle<i::Object> name = GetProperty(self, "functionName");
1729 if (!name->IsString()) {
1730 return Local<String>();
1731 }
1732 return scope.Close(Local<String>::Cast(Utils::ToLocal(name)));
1733}
1734
1735
1736bool StackFrame::IsEval() const {
1737 if (IsDeadCheck("v8::StackFrame::IsEval()")) return false;
1738 ENTER_V8;
1739 i::HandleScope scope;
1740 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
1741 i::Handle<i::Object> is_eval = GetProperty(self, "isEval");
1742 return is_eval->IsTrue();
1743}
1744
1745
1746bool StackFrame::IsConstructor() const {
1747 if (IsDeadCheck("v8::StackFrame::IsConstructor()")) return false;
1748 ENTER_V8;
1749 i::HandleScope scope;
1750 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
1751 i::Handle<i::Object> is_constructor = GetProperty(self, "isConstructor");
1752 return is_constructor->IsTrue();
1753}
1754
1755
Steve Blocka7e24c12009-10-30 11:49:00 +00001756// --- D a t a ---
1757
1758bool Value::IsUndefined() const {
1759 if (IsDeadCheck("v8::Value::IsUndefined()")) return false;
1760 return Utils::OpenHandle(this)->IsUndefined();
1761}
1762
1763
1764bool Value::IsNull() const {
1765 if (IsDeadCheck("v8::Value::IsNull()")) return false;
1766 return Utils::OpenHandle(this)->IsNull();
1767}
1768
1769
1770bool Value::IsTrue() const {
1771 if (IsDeadCheck("v8::Value::IsTrue()")) return false;
1772 return Utils::OpenHandle(this)->IsTrue();
1773}
1774
1775
1776bool Value::IsFalse() const {
1777 if (IsDeadCheck("v8::Value::IsFalse()")) return false;
1778 return Utils::OpenHandle(this)->IsFalse();
1779}
1780
1781
1782bool Value::IsFunction() const {
1783 if (IsDeadCheck("v8::Value::IsFunction()")) return false;
1784 return Utils::OpenHandle(this)->IsJSFunction();
1785}
1786
1787
1788bool Value::FullIsString() const {
1789 if (IsDeadCheck("v8::Value::IsString()")) return false;
1790 bool result = Utils::OpenHandle(this)->IsString();
1791 ASSERT_EQ(result, QuickIsString());
1792 return result;
1793}
1794
1795
1796bool Value::IsArray() const {
1797 if (IsDeadCheck("v8::Value::IsArray()")) return false;
1798 return Utils::OpenHandle(this)->IsJSArray();
1799}
1800
1801
1802bool Value::IsObject() const {
1803 if (IsDeadCheck("v8::Value::IsObject()")) return false;
1804 return Utils::OpenHandle(this)->IsJSObject();
1805}
1806
1807
1808bool Value::IsNumber() const {
1809 if (IsDeadCheck("v8::Value::IsNumber()")) return false;
1810 return Utils::OpenHandle(this)->IsNumber();
1811}
1812
1813
1814bool Value::IsBoolean() const {
1815 if (IsDeadCheck("v8::Value::IsBoolean()")) return false;
1816 return Utils::OpenHandle(this)->IsBoolean();
1817}
1818
1819
1820bool Value::IsExternal() const {
1821 if (IsDeadCheck("v8::Value::IsExternal()")) return false;
1822 return Utils::OpenHandle(this)->IsProxy();
1823}
1824
1825
1826bool Value::IsInt32() const {
1827 if (IsDeadCheck("v8::Value::IsInt32()")) return false;
1828 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1829 if (obj->IsSmi()) return true;
1830 if (obj->IsNumber()) {
1831 double value = obj->Number();
1832 return i::FastI2D(i::FastD2I(value)) == value;
1833 }
1834 return false;
1835}
1836
1837
Steve Block6ded16b2010-05-10 14:33:55 +01001838bool Value::IsUint32() const {
1839 if (IsDeadCheck("v8::Value::IsUint32()")) return false;
1840 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1841 if (obj->IsSmi()) return i::Smi::cast(*obj)->value() >= 0;
1842 if (obj->IsNumber()) {
1843 double value = obj->Number();
1844 return i::FastUI2D(i::FastD2UI(value)) == value;
1845 }
1846 return false;
1847}
1848
1849
Steve Blocka7e24c12009-10-30 11:49:00 +00001850bool Value::IsDate() const {
1851 if (IsDeadCheck("v8::Value::IsDate()")) return false;
1852 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1853 return obj->HasSpecificClassOf(i::Heap::Date_symbol());
1854}
1855
1856
Iain Merrick75681382010-08-19 15:07:18 +01001857bool Value::IsRegExp() const {
1858 if (IsDeadCheck("v8::Value::IsRegExp()")) return false;
1859 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1860 return obj->IsJSRegExp();
1861}
1862
1863
Steve Blocka7e24c12009-10-30 11:49:00 +00001864Local<String> Value::ToString() const {
1865 if (IsDeadCheck("v8::Value::ToString()")) return Local<String>();
1866 LOG_API("ToString");
1867 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1868 i::Handle<i::Object> str;
1869 if (obj->IsString()) {
1870 str = obj;
1871 } else {
1872 ENTER_V8;
1873 EXCEPTION_PREAMBLE();
1874 str = i::Execution::ToString(obj, &has_pending_exception);
1875 EXCEPTION_BAILOUT_CHECK(Local<String>());
1876 }
1877 return Local<String>(ToApi<String>(str));
1878}
1879
1880
1881Local<String> Value::ToDetailString() const {
1882 if (IsDeadCheck("v8::Value::ToDetailString()")) return Local<String>();
1883 LOG_API("ToDetailString");
1884 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1885 i::Handle<i::Object> str;
1886 if (obj->IsString()) {
1887 str = obj;
1888 } else {
1889 ENTER_V8;
1890 EXCEPTION_PREAMBLE();
1891 str = i::Execution::ToDetailString(obj, &has_pending_exception);
1892 EXCEPTION_BAILOUT_CHECK(Local<String>());
1893 }
1894 return Local<String>(ToApi<String>(str));
1895}
1896
1897
1898Local<v8::Object> Value::ToObject() const {
1899 if (IsDeadCheck("v8::Value::ToObject()")) return Local<v8::Object>();
1900 LOG_API("ToObject");
1901 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1902 i::Handle<i::Object> val;
1903 if (obj->IsJSObject()) {
1904 val = obj;
1905 } else {
1906 ENTER_V8;
1907 EXCEPTION_PREAMBLE();
1908 val = i::Execution::ToObject(obj, &has_pending_exception);
1909 EXCEPTION_BAILOUT_CHECK(Local<v8::Object>());
1910 }
1911 return Local<v8::Object>(ToApi<Object>(val));
1912}
1913
1914
1915Local<Boolean> Value::ToBoolean() const {
1916 if (IsDeadCheck("v8::Value::ToBoolean()")) return Local<Boolean>();
1917 LOG_API("ToBoolean");
1918 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1919 if (obj->IsBoolean()) {
1920 return Local<Boolean>(ToApi<Boolean>(obj));
1921 } else {
1922 ENTER_V8;
1923 i::Handle<i::Object> val = i::Execution::ToBoolean(obj);
1924 return Local<Boolean>(ToApi<Boolean>(val));
1925 }
1926}
1927
1928
1929Local<Number> Value::ToNumber() const {
1930 if (IsDeadCheck("v8::Value::ToNumber()")) return Local<Number>();
1931 LOG_API("ToNumber");
1932 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1933 i::Handle<i::Object> num;
1934 if (obj->IsNumber()) {
1935 num = obj;
1936 } else {
1937 ENTER_V8;
1938 EXCEPTION_PREAMBLE();
1939 num = i::Execution::ToNumber(obj, &has_pending_exception);
1940 EXCEPTION_BAILOUT_CHECK(Local<Number>());
1941 }
1942 return Local<Number>(ToApi<Number>(num));
1943}
1944
1945
1946Local<Integer> Value::ToInteger() const {
1947 if (IsDeadCheck("v8::Value::ToInteger()")) return Local<Integer>();
1948 LOG_API("ToInteger");
1949 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1950 i::Handle<i::Object> num;
1951 if (obj->IsSmi()) {
1952 num = obj;
1953 } else {
1954 ENTER_V8;
1955 EXCEPTION_PREAMBLE();
1956 num = i::Execution::ToInteger(obj, &has_pending_exception);
1957 EXCEPTION_BAILOUT_CHECK(Local<Integer>());
1958 }
1959 return Local<Integer>(ToApi<Integer>(num));
1960}
1961
1962
1963void External::CheckCast(v8::Value* that) {
1964 if (IsDeadCheck("v8::External::Cast()")) return;
1965 i::Handle<i::Object> obj = Utils::OpenHandle(that);
1966 ApiCheck(obj->IsProxy(),
1967 "v8::External::Cast()",
1968 "Could not convert to external");
1969}
1970
1971
1972void v8::Object::CheckCast(Value* that) {
1973 if (IsDeadCheck("v8::Object::Cast()")) return;
1974 i::Handle<i::Object> obj = Utils::OpenHandle(that);
1975 ApiCheck(obj->IsJSObject(),
1976 "v8::Object::Cast()",
1977 "Could not convert to object");
1978}
1979
1980
1981void v8::Function::CheckCast(Value* that) {
1982 if (IsDeadCheck("v8::Function::Cast()")) return;
1983 i::Handle<i::Object> obj = Utils::OpenHandle(that);
1984 ApiCheck(obj->IsJSFunction(),
1985 "v8::Function::Cast()",
1986 "Could not convert to function");
1987}
1988
1989
1990void v8::String::CheckCast(v8::Value* that) {
1991 if (IsDeadCheck("v8::String::Cast()")) return;
1992 i::Handle<i::Object> obj = Utils::OpenHandle(that);
1993 ApiCheck(obj->IsString(),
1994 "v8::String::Cast()",
1995 "Could not convert to string");
1996}
1997
1998
1999void v8::Number::CheckCast(v8::Value* that) {
2000 if (IsDeadCheck("v8::Number::Cast()")) return;
2001 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2002 ApiCheck(obj->IsNumber(),
2003 "v8::Number::Cast()",
2004 "Could not convert to number");
2005}
2006
2007
2008void v8::Integer::CheckCast(v8::Value* that) {
2009 if (IsDeadCheck("v8::Integer::Cast()")) return;
2010 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2011 ApiCheck(obj->IsNumber(),
2012 "v8::Integer::Cast()",
2013 "Could not convert to number");
2014}
2015
2016
2017void v8::Array::CheckCast(Value* that) {
2018 if (IsDeadCheck("v8::Array::Cast()")) return;
2019 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2020 ApiCheck(obj->IsJSArray(),
2021 "v8::Array::Cast()",
2022 "Could not convert to array");
2023}
2024
2025
2026void v8::Date::CheckCast(v8::Value* that) {
2027 if (IsDeadCheck("v8::Date::Cast()")) return;
2028 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2029 ApiCheck(obj->HasSpecificClassOf(i::Heap::Date_symbol()),
2030 "v8::Date::Cast()",
2031 "Could not convert to date");
2032}
2033
2034
Ben Murdochf87a2032010-10-22 12:50:53 +01002035void v8::RegExp::CheckCast(v8::Value* that) {
2036 if (IsDeadCheck("v8::RegExp::Cast()")) return;
2037 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2038 ApiCheck(obj->IsJSRegExp(),
2039 "v8::RegExp::Cast()",
2040 "Could not convert to regular expression");
2041}
2042
2043
Steve Blocka7e24c12009-10-30 11:49:00 +00002044bool Value::BooleanValue() const {
2045 if (IsDeadCheck("v8::Value::BooleanValue()")) return false;
2046 LOG_API("BooleanValue");
2047 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2048 if (obj->IsBoolean()) {
2049 return obj->IsTrue();
2050 } else {
2051 ENTER_V8;
2052 i::Handle<i::Object> value = i::Execution::ToBoolean(obj);
2053 return value->IsTrue();
2054 }
2055}
2056
2057
2058double Value::NumberValue() const {
2059 if (IsDeadCheck("v8::Value::NumberValue()")) return i::OS::nan_value();
2060 LOG_API("NumberValue");
2061 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2062 i::Handle<i::Object> num;
2063 if (obj->IsNumber()) {
2064 num = obj;
2065 } else {
2066 ENTER_V8;
2067 EXCEPTION_PREAMBLE();
2068 num = i::Execution::ToNumber(obj, &has_pending_exception);
2069 EXCEPTION_BAILOUT_CHECK(i::OS::nan_value());
2070 }
2071 return num->Number();
2072}
2073
2074
2075int64_t Value::IntegerValue() const {
2076 if (IsDeadCheck("v8::Value::IntegerValue()")) return 0;
2077 LOG_API("IntegerValue");
2078 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2079 i::Handle<i::Object> num;
2080 if (obj->IsNumber()) {
2081 num = obj;
2082 } else {
2083 ENTER_V8;
2084 EXCEPTION_PREAMBLE();
2085 num = i::Execution::ToInteger(obj, &has_pending_exception);
2086 EXCEPTION_BAILOUT_CHECK(0);
2087 }
2088 if (num->IsSmi()) {
2089 return i::Smi::cast(*num)->value();
2090 } else {
2091 return static_cast<int64_t>(num->Number());
2092 }
2093}
2094
2095
2096Local<Int32> Value::ToInt32() const {
2097 if (IsDeadCheck("v8::Value::ToInt32()")) return Local<Int32>();
2098 LOG_API("ToInt32");
2099 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2100 i::Handle<i::Object> num;
2101 if (obj->IsSmi()) {
2102 num = obj;
2103 } else {
2104 ENTER_V8;
2105 EXCEPTION_PREAMBLE();
2106 num = i::Execution::ToInt32(obj, &has_pending_exception);
2107 EXCEPTION_BAILOUT_CHECK(Local<Int32>());
2108 }
2109 return Local<Int32>(ToApi<Int32>(num));
2110}
2111
2112
2113Local<Uint32> Value::ToUint32() const {
2114 if (IsDeadCheck("v8::Value::ToUint32()")) return Local<Uint32>();
2115 LOG_API("ToUInt32");
2116 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2117 i::Handle<i::Object> num;
2118 if (obj->IsSmi()) {
2119 num = obj;
2120 } else {
2121 ENTER_V8;
2122 EXCEPTION_PREAMBLE();
2123 num = i::Execution::ToUint32(obj, &has_pending_exception);
2124 EXCEPTION_BAILOUT_CHECK(Local<Uint32>());
2125 }
2126 return Local<Uint32>(ToApi<Uint32>(num));
2127}
2128
2129
2130Local<Uint32> Value::ToArrayIndex() const {
2131 if (IsDeadCheck("v8::Value::ToArrayIndex()")) return Local<Uint32>();
2132 LOG_API("ToArrayIndex");
2133 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2134 if (obj->IsSmi()) {
2135 if (i::Smi::cast(*obj)->value() >= 0) return Utils::Uint32ToLocal(obj);
2136 return Local<Uint32>();
2137 }
2138 ENTER_V8;
2139 EXCEPTION_PREAMBLE();
2140 i::Handle<i::Object> string_obj =
2141 i::Execution::ToString(obj, &has_pending_exception);
2142 EXCEPTION_BAILOUT_CHECK(Local<Uint32>());
2143 i::Handle<i::String> str = i::Handle<i::String>::cast(string_obj);
2144 uint32_t index;
2145 if (str->AsArrayIndex(&index)) {
2146 i::Handle<i::Object> value;
2147 if (index <= static_cast<uint32_t>(i::Smi::kMaxValue)) {
2148 value = i::Handle<i::Object>(i::Smi::FromInt(index));
2149 } else {
2150 value = i::Factory::NewNumber(index);
2151 }
2152 return Utils::Uint32ToLocal(value);
2153 }
2154 return Local<Uint32>();
2155}
2156
2157
2158int32_t Value::Int32Value() const {
2159 if (IsDeadCheck("v8::Value::Int32Value()")) return 0;
2160 LOG_API("Int32Value");
2161 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2162 if (obj->IsSmi()) {
2163 return i::Smi::cast(*obj)->value();
2164 } else {
2165 LOG_API("Int32Value (slow)");
2166 ENTER_V8;
2167 EXCEPTION_PREAMBLE();
2168 i::Handle<i::Object> num =
2169 i::Execution::ToInt32(obj, &has_pending_exception);
2170 EXCEPTION_BAILOUT_CHECK(0);
2171 if (num->IsSmi()) {
2172 return i::Smi::cast(*num)->value();
2173 } else {
2174 return static_cast<int32_t>(num->Number());
2175 }
2176 }
2177}
2178
2179
2180bool Value::Equals(Handle<Value> that) const {
2181 if (IsDeadCheck("v8::Value::Equals()")
2182 || EmptyCheck("v8::Value::Equals()", this)
2183 || EmptyCheck("v8::Value::Equals()", that)) {
2184 return false;
2185 }
2186 LOG_API("Equals");
2187 ENTER_V8;
2188 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2189 i::Handle<i::Object> other = Utils::OpenHandle(*that);
2190 i::Object** args[1] = { other.location() };
2191 EXCEPTION_PREAMBLE();
2192 i::Handle<i::Object> result =
2193 CallV8HeapFunction("EQUALS", obj, 1, args, &has_pending_exception);
2194 EXCEPTION_BAILOUT_CHECK(false);
2195 return *result == i::Smi::FromInt(i::EQUAL);
2196}
2197
2198
2199bool Value::StrictEquals(Handle<Value> that) const {
2200 if (IsDeadCheck("v8::Value::StrictEquals()")
2201 || EmptyCheck("v8::Value::StrictEquals()", this)
2202 || EmptyCheck("v8::Value::StrictEquals()", that)) {
2203 return false;
2204 }
2205 LOG_API("StrictEquals");
2206 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2207 i::Handle<i::Object> other = Utils::OpenHandle(*that);
2208 // Must check HeapNumber first, since NaN !== NaN.
2209 if (obj->IsHeapNumber()) {
2210 if (!other->IsNumber()) return false;
2211 double x = obj->Number();
2212 double y = other->Number();
2213 // Must check explicitly for NaN:s on Windows, but -0 works fine.
2214 return x == y && !isnan(x) && !isnan(y);
2215 } else if (*obj == *other) { // Also covers Booleans.
2216 return true;
2217 } else if (obj->IsSmi()) {
2218 return other->IsNumber() && obj->Number() == other->Number();
2219 } else if (obj->IsString()) {
2220 return other->IsString() &&
2221 i::String::cast(*obj)->Equals(i::String::cast(*other));
2222 } else if (obj->IsUndefined() || obj->IsUndetectableObject()) {
2223 return other->IsUndefined() || other->IsUndetectableObject();
2224 } else {
2225 return false;
2226 }
2227}
2228
2229
2230uint32_t Value::Uint32Value() const {
2231 if (IsDeadCheck("v8::Value::Uint32Value()")) return 0;
2232 LOG_API("Uint32Value");
2233 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2234 if (obj->IsSmi()) {
2235 return i::Smi::cast(*obj)->value();
2236 } else {
2237 ENTER_V8;
2238 EXCEPTION_PREAMBLE();
2239 i::Handle<i::Object> num =
2240 i::Execution::ToUint32(obj, &has_pending_exception);
2241 EXCEPTION_BAILOUT_CHECK(0);
2242 if (num->IsSmi()) {
2243 return i::Smi::cast(*num)->value();
2244 } else {
2245 return static_cast<uint32_t>(num->Number());
2246 }
2247 }
2248}
2249
2250
2251bool v8::Object::Set(v8::Handle<Value> key, v8::Handle<Value> value,
2252 v8::PropertyAttribute attribs) {
2253 ON_BAILOUT("v8::Object::Set()", return false);
2254 ENTER_V8;
2255 HandleScope scope;
2256 i::Handle<i::Object> self = Utils::OpenHandle(this);
2257 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2258 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
2259 EXCEPTION_PREAMBLE();
2260 i::Handle<i::Object> obj = i::SetProperty(
2261 self,
2262 key_obj,
2263 value_obj,
2264 static_cast<PropertyAttributes>(attribs));
2265 has_pending_exception = obj.is_null();
2266 EXCEPTION_BAILOUT_CHECK(false);
2267 return true;
2268}
2269
2270
Steve Block6ded16b2010-05-10 14:33:55 +01002271bool v8::Object::Set(uint32_t index, v8::Handle<Value> value) {
2272 ON_BAILOUT("v8::Object::Set()", return false);
2273 ENTER_V8;
2274 HandleScope scope;
2275 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2276 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
2277 EXCEPTION_PREAMBLE();
2278 i::Handle<i::Object> obj = i::SetElement(
2279 self,
2280 index,
2281 value_obj);
2282 has_pending_exception = obj.is_null();
2283 EXCEPTION_BAILOUT_CHECK(false);
2284 return true;
2285}
2286
2287
Steve Blocka7e24c12009-10-30 11:49:00 +00002288bool v8::Object::ForceSet(v8::Handle<Value> key,
2289 v8::Handle<Value> value,
2290 v8::PropertyAttribute attribs) {
2291 ON_BAILOUT("v8::Object::ForceSet()", return false);
2292 ENTER_V8;
2293 HandleScope scope;
2294 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2295 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2296 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
2297 EXCEPTION_PREAMBLE();
2298 i::Handle<i::Object> obj = i::ForceSetProperty(
2299 self,
2300 key_obj,
2301 value_obj,
2302 static_cast<PropertyAttributes>(attribs));
2303 has_pending_exception = obj.is_null();
2304 EXCEPTION_BAILOUT_CHECK(false);
2305 return true;
2306}
2307
2308
2309bool v8::Object::ForceDelete(v8::Handle<Value> key) {
2310 ON_BAILOUT("v8::Object::ForceDelete()", return false);
2311 ENTER_V8;
2312 HandleScope scope;
2313 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2314 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2315 EXCEPTION_PREAMBLE();
2316 i::Handle<i::Object> obj = i::ForceDeleteProperty(self, key_obj);
2317 has_pending_exception = obj.is_null();
2318 EXCEPTION_BAILOUT_CHECK(false);
2319 return obj->IsTrue();
2320}
2321
2322
2323Local<Value> v8::Object::Get(v8::Handle<Value> key) {
2324 ON_BAILOUT("v8::Object::Get()", return Local<v8::Value>());
2325 ENTER_V8;
2326 i::Handle<i::Object> self = Utils::OpenHandle(this);
2327 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2328 EXCEPTION_PREAMBLE();
2329 i::Handle<i::Object> result = i::GetProperty(self, key_obj);
2330 has_pending_exception = result.is_null();
2331 EXCEPTION_BAILOUT_CHECK(Local<Value>());
2332 return Utils::ToLocal(result);
2333}
2334
2335
Steve Block6ded16b2010-05-10 14:33:55 +01002336Local<Value> v8::Object::Get(uint32_t index) {
2337 ON_BAILOUT("v8::Object::Get()", return Local<v8::Value>());
2338 ENTER_V8;
2339 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2340 EXCEPTION_PREAMBLE();
2341 i::Handle<i::Object> result = i::GetElement(self, index);
2342 has_pending_exception = result.is_null();
2343 EXCEPTION_BAILOUT_CHECK(Local<Value>());
2344 return Utils::ToLocal(result);
2345}
2346
2347
Steve Blocka7e24c12009-10-30 11:49:00 +00002348Local<Value> v8::Object::GetPrototype() {
2349 ON_BAILOUT("v8::Object::GetPrototype()", return Local<v8::Value>());
2350 ENTER_V8;
2351 i::Handle<i::Object> self = Utils::OpenHandle(this);
2352 i::Handle<i::Object> result = i::GetPrototype(self);
2353 return Utils::ToLocal(result);
2354}
2355
2356
Andrei Popescu402d9372010-02-26 13:31:12 +00002357bool v8::Object::SetPrototype(Handle<Value> value) {
2358 ON_BAILOUT("v8::Object::SetPrototype()", return false);
2359 ENTER_V8;
2360 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2361 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
2362 EXCEPTION_PREAMBLE();
2363 i::Handle<i::Object> result = i::SetPrototype(self, value_obj);
2364 has_pending_exception = result.is_null();
2365 EXCEPTION_BAILOUT_CHECK(false);
2366 return true;
2367}
2368
2369
Steve Blocka7e24c12009-10-30 11:49:00 +00002370Local<Object> v8::Object::FindInstanceInPrototypeChain(
2371 v8::Handle<FunctionTemplate> tmpl) {
2372 ON_BAILOUT("v8::Object::FindInstanceInPrototypeChain()",
2373 return Local<v8::Object>());
2374 ENTER_V8;
2375 i::JSObject* object = *Utils::OpenHandle(this);
2376 i::FunctionTemplateInfo* tmpl_info = *Utils::OpenHandle(*tmpl);
2377 while (!object->IsInstanceOf(tmpl_info)) {
2378 i::Object* prototype = object->GetPrototype();
2379 if (!prototype->IsJSObject()) return Local<Object>();
2380 object = i::JSObject::cast(prototype);
2381 }
2382 return Utils::ToLocal(i::Handle<i::JSObject>(object));
2383}
2384
2385
2386Local<Array> v8::Object::GetPropertyNames() {
2387 ON_BAILOUT("v8::Object::GetPropertyNames()", return Local<v8::Array>());
2388 ENTER_V8;
2389 v8::HandleScope scope;
2390 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2391 i::Handle<i::FixedArray> value =
2392 i::GetKeysInFixedArrayFor(self, i::INCLUDE_PROTOS);
2393 // Because we use caching to speed up enumeration it is important
2394 // to never change the result of the basic enumeration function so
2395 // we clone the result.
2396 i::Handle<i::FixedArray> elms = i::Factory::CopyFixedArray(value);
2397 i::Handle<i::JSArray> result = i::Factory::NewJSArrayWithElements(elms);
2398 return scope.Close(Utils::ToLocal(result));
2399}
2400
2401
2402Local<String> v8::Object::ObjectProtoToString() {
2403 ON_BAILOUT("v8::Object::ObjectProtoToString()", return Local<v8::String>());
2404 ENTER_V8;
2405 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2406
2407 i::Handle<i::Object> name(self->class_name());
2408
2409 // Native implementation of Object.prototype.toString (v8natives.js):
2410 // var c = %ClassOf(this);
2411 // if (c === 'Arguments') c = 'Object';
2412 // return "[object " + c + "]";
2413
2414 if (!name->IsString()) {
2415 return v8::String::New("[object ]");
2416
2417 } else {
2418 i::Handle<i::String> class_name = i::Handle<i::String>::cast(name);
2419 if (class_name->IsEqualTo(i::CStrVector("Arguments"))) {
2420 return v8::String::New("[object Object]");
2421
2422 } else {
2423 const char* prefix = "[object ";
2424 Local<String> str = Utils::ToLocal(class_name);
2425 const char* postfix = "]";
2426
Steve Blockd0582a62009-12-15 09:54:21 +00002427 int prefix_len = i::StrLength(prefix);
2428 int str_len = str->Length();
2429 int postfix_len = i::StrLength(postfix);
Steve Blocka7e24c12009-10-30 11:49:00 +00002430
Steve Blockd0582a62009-12-15 09:54:21 +00002431 int buf_len = prefix_len + str_len + postfix_len;
Kristian Monsen25f61362010-05-21 11:50:48 +01002432 i::ScopedVector<char> buf(buf_len);
Steve Blocka7e24c12009-10-30 11:49:00 +00002433
2434 // Write prefix.
Kristian Monsen25f61362010-05-21 11:50:48 +01002435 char* ptr = buf.start();
Steve Blocka7e24c12009-10-30 11:49:00 +00002436 memcpy(ptr, prefix, prefix_len * v8::internal::kCharSize);
2437 ptr += prefix_len;
2438
2439 // Write real content.
2440 str->WriteAscii(ptr, 0, str_len);
2441 ptr += str_len;
2442
2443 // Write postfix.
2444 memcpy(ptr, postfix, postfix_len * v8::internal::kCharSize);
2445
2446 // Copy the buffer into a heap-allocated string and return it.
Kristian Monsen25f61362010-05-21 11:50:48 +01002447 Local<String> result = v8::String::New(buf.start(), buf_len);
Steve Blocka7e24c12009-10-30 11:49:00 +00002448 return result;
2449 }
2450 }
2451}
2452
2453
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002454Local<String> v8::Object::GetConstructorName() {
2455 ON_BAILOUT("v8::Object::GetConstructorName()", return Local<v8::String>());
2456 ENTER_V8;
2457 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2458 i::Handle<i::String> name(self->constructor_name());
2459 return Utils::ToLocal(name);
2460}
2461
2462
Steve Blocka7e24c12009-10-30 11:49:00 +00002463bool v8::Object::Delete(v8::Handle<String> key) {
2464 ON_BAILOUT("v8::Object::Delete()", return false);
2465 ENTER_V8;
2466 HandleScope scope;
2467 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2468 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
2469 return i::DeleteProperty(self, key_obj)->IsTrue();
2470}
2471
2472
2473bool v8::Object::Has(v8::Handle<String> key) {
2474 ON_BAILOUT("v8::Object::Has()", return false);
2475 ENTER_V8;
2476 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2477 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
2478 return self->HasProperty(*key_obj);
2479}
2480
2481
2482bool v8::Object::Delete(uint32_t index) {
2483 ON_BAILOUT("v8::Object::DeleteProperty()", return false);
2484 ENTER_V8;
2485 HandleScope scope;
2486 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2487 return i::DeleteElement(self, index)->IsTrue();
2488}
2489
2490
2491bool v8::Object::Has(uint32_t index) {
2492 ON_BAILOUT("v8::Object::HasProperty()", return false);
2493 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2494 return self->HasElement(index);
2495}
2496
2497
Leon Clarkef7060e22010-06-03 12:02:55 +01002498bool Object::SetAccessor(Handle<String> name,
2499 AccessorGetter getter,
2500 AccessorSetter setter,
2501 v8::Handle<Value> data,
2502 AccessControl settings,
2503 PropertyAttribute attributes) {
2504 ON_BAILOUT("v8::Object::SetAccessor()", return false);
2505 ENTER_V8;
2506 HandleScope scope;
2507 i::Handle<i::AccessorInfo> info = MakeAccessorInfo(name,
2508 getter, setter, data,
2509 settings, attributes);
2510 i::Handle<i::Object> result = i::SetAccessor(Utils::OpenHandle(this), info);
2511 return !result.is_null() && !result->IsUndefined();
2512}
2513
2514
Steve Blocka7e24c12009-10-30 11:49:00 +00002515bool v8::Object::HasRealNamedProperty(Handle<String> key) {
2516 ON_BAILOUT("v8::Object::HasRealNamedProperty()", return false);
2517 return Utils::OpenHandle(this)->HasRealNamedProperty(
2518 *Utils::OpenHandle(*key));
2519}
2520
2521
2522bool v8::Object::HasRealIndexedProperty(uint32_t index) {
2523 ON_BAILOUT("v8::Object::HasRealIndexedProperty()", return false);
2524 return Utils::OpenHandle(this)->HasRealElementProperty(index);
2525}
2526
2527
2528bool v8::Object::HasRealNamedCallbackProperty(Handle<String> key) {
2529 ON_BAILOUT("v8::Object::HasRealNamedCallbackProperty()", return false);
2530 ENTER_V8;
2531 return Utils::OpenHandle(this)->HasRealNamedCallbackProperty(
2532 *Utils::OpenHandle(*key));
2533}
2534
2535
2536bool v8::Object::HasNamedLookupInterceptor() {
2537 ON_BAILOUT("v8::Object::HasNamedLookupInterceptor()", return false);
2538 return Utils::OpenHandle(this)->HasNamedInterceptor();
2539}
2540
2541
2542bool v8::Object::HasIndexedLookupInterceptor() {
2543 ON_BAILOUT("v8::Object::HasIndexedLookupInterceptor()", return false);
2544 return Utils::OpenHandle(this)->HasIndexedInterceptor();
2545}
2546
2547
2548Local<Value> v8::Object::GetRealNamedPropertyInPrototypeChain(
2549 Handle<String> key) {
2550 ON_BAILOUT("v8::Object::GetRealNamedPropertyInPrototypeChain()",
2551 return Local<Value>());
2552 ENTER_V8;
2553 i::Handle<i::JSObject> self_obj = Utils::OpenHandle(this);
2554 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
2555 i::LookupResult lookup;
2556 self_obj->LookupRealNamedPropertyInPrototypes(*key_obj, &lookup);
Andrei Popescu402d9372010-02-26 13:31:12 +00002557 if (lookup.IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002558 PropertyAttributes attributes;
John Reck59135872010-11-02 12:39:01 -07002559 i::Object* property =
2560 self_obj->GetProperty(*self_obj,
2561 &lookup,
2562 *key_obj,
2563 &attributes)->ToObjectUnchecked();
2564 i::Handle<i::Object> result(property);
Steve Blocka7e24c12009-10-30 11:49:00 +00002565 return Utils::ToLocal(result);
2566 }
2567 return Local<Value>(); // No real property was found in prototype chain.
2568}
2569
2570
2571Local<Value> v8::Object::GetRealNamedProperty(Handle<String> key) {
2572 ON_BAILOUT("v8::Object::GetRealNamedProperty()", return Local<Value>());
2573 ENTER_V8;
2574 i::Handle<i::JSObject> self_obj = Utils::OpenHandle(this);
2575 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
2576 i::LookupResult lookup;
2577 self_obj->LookupRealNamedProperty(*key_obj, &lookup);
Andrei Popescu402d9372010-02-26 13:31:12 +00002578 if (lookup.IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002579 PropertyAttributes attributes;
John Reck59135872010-11-02 12:39:01 -07002580 i::Object* property =
2581 self_obj->GetProperty(*self_obj,
2582 &lookup,
2583 *key_obj,
2584 &attributes)->ToObjectUnchecked();
2585 i::Handle<i::Object> result(property);
Steve Blocka7e24c12009-10-30 11:49:00 +00002586 return Utils::ToLocal(result);
2587 }
2588 return Local<Value>(); // No real property was found in prototype chain.
2589}
2590
2591
2592// Turns on access checks by copying the map and setting the check flag.
2593// Because the object gets a new map, existing inline cache caching
2594// the old map of this object will fail.
2595void v8::Object::TurnOnAccessCheck() {
2596 ON_BAILOUT("v8::Object::TurnOnAccessCheck()", return);
2597 ENTER_V8;
2598 HandleScope scope;
2599 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
2600
2601 i::Handle<i::Map> new_map =
2602 i::Factory::CopyMapDropTransitions(i::Handle<i::Map>(obj->map()));
2603 new_map->set_is_access_check_needed(true);
2604 obj->set_map(*new_map);
2605}
2606
2607
2608bool v8::Object::IsDirty() {
2609 return Utils::OpenHandle(this)->IsDirty();
2610}
2611
2612
2613Local<v8::Object> v8::Object::Clone() {
2614 ON_BAILOUT("v8::Object::Clone()", return Local<Object>());
2615 ENTER_V8;
2616 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2617 EXCEPTION_PREAMBLE();
2618 i::Handle<i::JSObject> result = i::Copy(self);
2619 has_pending_exception = result.is_null();
2620 EXCEPTION_BAILOUT_CHECK(Local<Object>());
2621 return Utils::ToLocal(result);
2622}
2623
2624
2625int v8::Object::GetIdentityHash() {
2626 ON_BAILOUT("v8::Object::GetIdentityHash()", return 0);
2627 ENTER_V8;
2628 HandleScope scope;
2629 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2630 i::Handle<i::Object> hidden_props(i::GetHiddenProperties(self, true));
2631 i::Handle<i::Object> hash_symbol = i::Factory::identity_hash_symbol();
2632 i::Handle<i::Object> hash = i::GetProperty(hidden_props, hash_symbol);
2633 int hash_value;
2634 if (hash->IsSmi()) {
2635 hash_value = i::Smi::cast(*hash)->value();
2636 } else {
2637 int attempts = 0;
2638 do {
2639 // Generate a random 32-bit hash value but limit range to fit
2640 // within a smi.
2641 hash_value = i::V8::Random() & i::Smi::kMaxValue;
2642 attempts++;
2643 } while (hash_value == 0 && attempts < 30);
2644 hash_value = hash_value != 0 ? hash_value : 1; // never return 0
2645 i::SetProperty(hidden_props,
2646 hash_symbol,
2647 i::Handle<i::Object>(i::Smi::FromInt(hash_value)),
2648 static_cast<PropertyAttributes>(None));
2649 }
2650 return hash_value;
2651}
2652
2653
2654bool v8::Object::SetHiddenValue(v8::Handle<v8::String> key,
2655 v8::Handle<v8::Value> value) {
2656 ON_BAILOUT("v8::Object::SetHiddenValue()", return false);
2657 ENTER_V8;
2658 HandleScope scope;
2659 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2660 i::Handle<i::Object> hidden_props(i::GetHiddenProperties(self, true));
2661 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2662 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
2663 EXCEPTION_PREAMBLE();
2664 i::Handle<i::Object> obj = i::SetProperty(
2665 hidden_props,
2666 key_obj,
2667 value_obj,
2668 static_cast<PropertyAttributes>(None));
2669 has_pending_exception = obj.is_null();
2670 EXCEPTION_BAILOUT_CHECK(false);
2671 return true;
2672}
2673
2674
2675v8::Local<v8::Value> v8::Object::GetHiddenValue(v8::Handle<v8::String> key) {
2676 ON_BAILOUT("v8::Object::GetHiddenValue()", return Local<v8::Value>());
2677 ENTER_V8;
2678 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2679 i::Handle<i::Object> hidden_props(i::GetHiddenProperties(self, false));
2680 if (hidden_props->IsUndefined()) {
2681 return v8::Local<v8::Value>();
2682 }
2683 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
2684 EXCEPTION_PREAMBLE();
2685 i::Handle<i::Object> result = i::GetProperty(hidden_props, key_obj);
2686 has_pending_exception = result.is_null();
2687 EXCEPTION_BAILOUT_CHECK(v8::Local<v8::Value>());
2688 if (result->IsUndefined()) {
2689 return v8::Local<v8::Value>();
2690 }
2691 return Utils::ToLocal(result);
2692}
2693
2694
2695bool v8::Object::DeleteHiddenValue(v8::Handle<v8::String> key) {
2696 ON_BAILOUT("v8::DeleteHiddenValue()", return false);
2697 ENTER_V8;
2698 HandleScope scope;
2699 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2700 i::Handle<i::Object> hidden_props(i::GetHiddenProperties(self, false));
2701 if (hidden_props->IsUndefined()) {
2702 return true;
2703 }
2704 i::Handle<i::JSObject> js_obj(i::JSObject::cast(*hidden_props));
2705 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
2706 return i::DeleteProperty(js_obj, key_obj)->IsTrue();
2707}
2708
2709
2710void v8::Object::SetIndexedPropertiesToPixelData(uint8_t* data, int length) {
2711 ON_BAILOUT("v8::SetElementsToPixelData()", return);
2712 ENTER_V8;
2713 HandleScope scope;
Steve Block3ce2e202009-11-05 08:53:23 +00002714 if (!ApiCheck(length <= i::PixelArray::kMaxLength,
Steve Blocka7e24c12009-10-30 11:49:00 +00002715 "v8::Object::SetIndexedPropertiesToPixelData()",
2716 "length exceeds max acceptable value")) {
2717 return;
2718 }
2719 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2720 if (!ApiCheck(!self->IsJSArray(),
2721 "v8::Object::SetIndexedPropertiesToPixelData()",
2722 "JSArray is not supported")) {
2723 return;
2724 }
2725 i::Handle<i::PixelArray> pixels = i::Factory::NewPixelArray(length, data);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002726 i::Handle<i::Map> slow_map =
2727 i::Factory::GetSlowElementsMap(i::Handle<i::Map>(self->map()));
2728 self->set_map(*slow_map);
Steve Blocka7e24c12009-10-30 11:49:00 +00002729 self->set_elements(*pixels);
2730}
2731
2732
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002733bool v8::Object::HasIndexedPropertiesInPixelData() {
2734 ON_BAILOUT("v8::HasIndexedPropertiesInPixelData()", return false);
2735 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2736 return self->HasPixelElements();
2737}
2738
2739
2740uint8_t* v8::Object::GetIndexedPropertiesPixelData() {
2741 ON_BAILOUT("v8::GetIndexedPropertiesPixelData()", return NULL);
2742 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2743 if (self->HasPixelElements()) {
2744 return i::PixelArray::cast(self->elements())->external_pointer();
2745 } else {
2746 return NULL;
2747 }
2748}
2749
2750
2751int v8::Object::GetIndexedPropertiesPixelDataLength() {
2752 ON_BAILOUT("v8::GetIndexedPropertiesPixelDataLength()", return -1);
2753 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2754 if (self->HasPixelElements()) {
2755 return i::PixelArray::cast(self->elements())->length();
2756 } else {
2757 return -1;
2758 }
2759}
2760
2761
Steve Block3ce2e202009-11-05 08:53:23 +00002762void v8::Object::SetIndexedPropertiesToExternalArrayData(
2763 void* data,
2764 ExternalArrayType array_type,
2765 int length) {
2766 ON_BAILOUT("v8::SetIndexedPropertiesToExternalArrayData()", return);
2767 ENTER_V8;
2768 HandleScope scope;
2769 if (!ApiCheck(length <= i::ExternalArray::kMaxLength,
2770 "v8::Object::SetIndexedPropertiesToExternalArrayData()",
2771 "length exceeds max acceptable value")) {
2772 return;
2773 }
2774 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2775 if (!ApiCheck(!self->IsJSArray(),
2776 "v8::Object::SetIndexedPropertiesToExternalArrayData()",
2777 "JSArray is not supported")) {
2778 return;
2779 }
2780 i::Handle<i::ExternalArray> array =
2781 i::Factory::NewExternalArray(length, array_type, data);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002782 i::Handle<i::Map> slow_map =
2783 i::Factory::GetSlowElementsMap(i::Handle<i::Map>(self->map()));
2784 self->set_map(*slow_map);
Steve Block3ce2e202009-11-05 08:53:23 +00002785 self->set_elements(*array);
2786}
2787
2788
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002789bool v8::Object::HasIndexedPropertiesInExternalArrayData() {
2790 ON_BAILOUT("v8::HasIndexedPropertiesInExternalArrayData()", return false);
2791 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2792 return self->HasExternalArrayElements();
2793}
2794
2795
2796void* v8::Object::GetIndexedPropertiesExternalArrayData() {
2797 ON_BAILOUT("v8::GetIndexedPropertiesExternalArrayData()", return NULL);
2798 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2799 if (self->HasExternalArrayElements()) {
2800 return i::ExternalArray::cast(self->elements())->external_pointer();
2801 } else {
2802 return NULL;
2803 }
2804}
2805
2806
2807ExternalArrayType v8::Object::GetIndexedPropertiesExternalArrayDataType() {
2808 ON_BAILOUT("v8::GetIndexedPropertiesExternalArrayDataType()",
2809 return static_cast<ExternalArrayType>(-1));
2810 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2811 switch (self->elements()->map()->instance_type()) {
2812 case i::EXTERNAL_BYTE_ARRAY_TYPE:
2813 return kExternalByteArray;
2814 case i::EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE:
2815 return kExternalUnsignedByteArray;
2816 case i::EXTERNAL_SHORT_ARRAY_TYPE:
2817 return kExternalShortArray;
2818 case i::EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE:
2819 return kExternalUnsignedShortArray;
2820 case i::EXTERNAL_INT_ARRAY_TYPE:
2821 return kExternalIntArray;
2822 case i::EXTERNAL_UNSIGNED_INT_ARRAY_TYPE:
2823 return kExternalUnsignedIntArray;
2824 case i::EXTERNAL_FLOAT_ARRAY_TYPE:
2825 return kExternalFloatArray;
2826 default:
2827 return static_cast<ExternalArrayType>(-1);
2828 }
2829}
2830
2831
2832int v8::Object::GetIndexedPropertiesExternalArrayDataLength() {
2833 ON_BAILOUT("v8::GetIndexedPropertiesExternalArrayDataLength()", return 0);
2834 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2835 if (self->HasExternalArrayElements()) {
2836 return i::ExternalArray::cast(self->elements())->length();
2837 } else {
2838 return -1;
2839 }
2840}
2841
2842
Steve Blocka7e24c12009-10-30 11:49:00 +00002843Local<v8::Object> Function::NewInstance() const {
2844 return NewInstance(0, NULL);
2845}
2846
2847
2848Local<v8::Object> Function::NewInstance(int argc,
2849 v8::Handle<v8::Value> argv[]) const {
2850 ON_BAILOUT("v8::Function::NewInstance()", return Local<v8::Object>());
2851 LOG_API("Function::NewInstance");
2852 ENTER_V8;
2853 HandleScope scope;
2854 i::Handle<i::JSFunction> function = Utils::OpenHandle(this);
2855 STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**));
2856 i::Object*** args = reinterpret_cast<i::Object***>(argv);
2857 EXCEPTION_PREAMBLE();
2858 i::Handle<i::Object> returned =
2859 i::Execution::New(function, argc, args, &has_pending_exception);
2860 EXCEPTION_BAILOUT_CHECK(Local<v8::Object>());
2861 return scope.Close(Utils::ToLocal(i::Handle<i::JSObject>::cast(returned)));
2862}
2863
2864
2865Local<v8::Value> Function::Call(v8::Handle<v8::Object> recv, int argc,
2866 v8::Handle<v8::Value> argv[]) {
2867 ON_BAILOUT("v8::Function::Call()", return Local<v8::Value>());
2868 LOG_API("Function::Call");
2869 ENTER_V8;
2870 i::Object* raw_result = NULL;
2871 {
2872 HandleScope scope;
2873 i::Handle<i::JSFunction> fun = Utils::OpenHandle(this);
2874 i::Handle<i::Object> recv_obj = Utils::OpenHandle(*recv);
2875 STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**));
2876 i::Object*** args = reinterpret_cast<i::Object***>(argv);
2877 EXCEPTION_PREAMBLE();
2878 i::Handle<i::Object> returned =
2879 i::Execution::Call(fun, recv_obj, argc, args, &has_pending_exception);
2880 EXCEPTION_BAILOUT_CHECK(Local<Object>());
2881 raw_result = *returned;
2882 }
2883 i::Handle<i::Object> result(raw_result);
2884 return Utils::ToLocal(result);
2885}
2886
2887
2888void Function::SetName(v8::Handle<v8::String> name) {
2889 ENTER_V8;
2890 i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
2891 func->shared()->set_name(*Utils::OpenHandle(*name));
2892}
2893
2894
2895Handle<Value> Function::GetName() const {
2896 i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
2897 return Utils::ToLocal(i::Handle<i::Object>(func->shared()->name()));
2898}
2899
2900
Andrei Popescu402d9372010-02-26 13:31:12 +00002901ScriptOrigin Function::GetScriptOrigin() const {
2902 i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
2903 if (func->shared()->script()->IsScript()) {
2904 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
2905 v8::ScriptOrigin origin(
2906 Utils::ToLocal(i::Handle<i::Object>(script->name())),
2907 v8::Integer::New(script->line_offset()->value()),
2908 v8::Integer::New(script->column_offset()->value()));
2909 return origin;
2910 }
2911 return v8::ScriptOrigin(Handle<Value>());
2912}
2913
2914
2915const int Function::kLineOffsetNotFound = -1;
2916
2917
2918int Function::GetScriptLineNumber() const {
2919 i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
2920 if (func->shared()->script()->IsScript()) {
2921 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
2922 return i::GetScriptLineNumber(script, func->shared()->start_position());
2923 }
2924 return kLineOffsetNotFound;
2925}
2926
2927
2928namespace {
2929
2930// Tracks string usage to help make better decisions when
2931// externalizing strings.
2932//
2933// Implementation note: internally this class only tracks fresh
2934// strings and keeps a single use counter for them.
2935class StringTracker {
2936 public:
2937 // Records that the given string's characters were copied to some
2938 // external buffer. If this happens often we should honor
2939 // externalization requests for the string.
2940 static void RecordWrite(i::Handle<i::String> string) {
2941 i::Address address = reinterpret_cast<i::Address>(*string);
2942 i::Address top = i::Heap::NewSpaceTop();
2943 if (IsFreshString(address, top)) {
2944 IncrementUseCount(top);
2945 }
2946 }
2947
2948 // Estimates freshness and use frequency of the given string based
2949 // on how close it is to the new space top and the recorded usage
2950 // history.
2951 static inline bool IsFreshUnusedString(i::Handle<i::String> string) {
2952 i::Address address = reinterpret_cast<i::Address>(*string);
2953 i::Address top = i::Heap::NewSpaceTop();
2954 return IsFreshString(address, top) && IsUseCountLow(top);
2955 }
2956
2957 private:
2958 static inline bool IsFreshString(i::Address string, i::Address top) {
2959 return top - kFreshnessLimit <= string && string <= top;
2960 }
2961
2962 static inline bool IsUseCountLow(i::Address top) {
2963 if (last_top_ != top) return true;
2964 return use_count_ < kUseLimit;
2965 }
2966
2967 static inline void IncrementUseCount(i::Address top) {
2968 if (last_top_ != top) {
2969 use_count_ = 0;
2970 last_top_ = top;
2971 }
2972 ++use_count_;
2973 }
2974
2975 // How close to the new space top a fresh string has to be.
2976 static const int kFreshnessLimit = 1024;
2977
2978 // The number of uses required to consider a string useful.
2979 static const int kUseLimit = 32;
2980
2981 // Single use counter shared by all fresh strings.
2982 static int use_count_;
2983
2984 // Last new space top when the use count above was valid.
2985 static i::Address last_top_;
2986};
2987
2988int StringTracker::use_count_ = 0;
2989i::Address StringTracker::last_top_ = NULL;
2990
2991} // namespace
2992
2993
Steve Blocka7e24c12009-10-30 11:49:00 +00002994int String::Length() const {
2995 if (IsDeadCheck("v8::String::Length()")) return 0;
2996 return Utils::OpenHandle(this)->length();
2997}
2998
2999
3000int String::Utf8Length() const {
3001 if (IsDeadCheck("v8::String::Utf8Length()")) return 0;
3002 return Utils::OpenHandle(this)->Utf8Length();
3003}
3004
3005
Steve Block6ded16b2010-05-10 14:33:55 +01003006int String::WriteUtf8(char* buffer,
3007 int capacity,
3008 int* nchars_ref,
3009 WriteHints hints) const {
Steve Blocka7e24c12009-10-30 11:49:00 +00003010 if (IsDeadCheck("v8::String::WriteUtf8()")) return 0;
3011 LOG_API("String::WriteUtf8");
3012 ENTER_V8;
3013 i::Handle<i::String> str = Utils::OpenHandle(this);
Andrei Popescu402d9372010-02-26 13:31:12 +00003014 StringTracker::RecordWrite(str);
Steve Block6ded16b2010-05-10 14:33:55 +01003015 if (hints & HINT_MANY_WRITES_EXPECTED) {
3016 // Flatten the string for efficiency. This applies whether we are
3017 // using StringInputBuffer or Get(i) to access the characters.
3018 str->TryFlatten();
3019 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003020 write_input_buffer.Reset(0, *str);
3021 int len = str->length();
3022 // Encode the first K - 3 bytes directly into the buffer since we
3023 // know there's room for them. If no capacity is given we copy all
3024 // of them here.
3025 int fast_end = capacity - (unibrow::Utf8::kMaxEncodedSize - 1);
3026 int i;
3027 int pos = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01003028 int nchars = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00003029 for (i = 0; i < len && (capacity == -1 || pos < fast_end); i++) {
3030 i::uc32 c = write_input_buffer.GetNext();
3031 int written = unibrow::Utf8::Encode(buffer + pos, c);
3032 pos += written;
Steve Block6ded16b2010-05-10 14:33:55 +01003033 nchars++;
Steve Blocka7e24c12009-10-30 11:49:00 +00003034 }
3035 if (i < len) {
3036 // For the last characters we need to check the length for each one
3037 // because they may be longer than the remaining space in the
3038 // buffer.
3039 char intermediate[unibrow::Utf8::kMaxEncodedSize];
3040 for (; i < len && pos < capacity; i++) {
3041 i::uc32 c = write_input_buffer.GetNext();
3042 int written = unibrow::Utf8::Encode(intermediate, c);
3043 if (pos + written <= capacity) {
3044 for (int j = 0; j < written; j++)
3045 buffer[pos + j] = intermediate[j];
3046 pos += written;
Steve Block6ded16b2010-05-10 14:33:55 +01003047 nchars++;
Steve Blocka7e24c12009-10-30 11:49:00 +00003048 } else {
3049 // We've reached the end of the buffer
3050 break;
3051 }
3052 }
3053 }
Steve Block6ded16b2010-05-10 14:33:55 +01003054 if (nchars_ref != NULL) *nchars_ref = nchars;
Steve Blocka7e24c12009-10-30 11:49:00 +00003055 if (i == len && (capacity == -1 || pos < capacity))
3056 buffer[pos++] = '\0';
3057 return pos;
3058}
3059
3060
Steve Block6ded16b2010-05-10 14:33:55 +01003061int String::WriteAscii(char* buffer,
3062 int start,
3063 int length,
3064 WriteHints hints) const {
Steve Blocka7e24c12009-10-30 11:49:00 +00003065 if (IsDeadCheck("v8::String::WriteAscii()")) return 0;
3066 LOG_API("String::WriteAscii");
3067 ENTER_V8;
3068 ASSERT(start >= 0 && length >= -1);
3069 i::Handle<i::String> str = Utils::OpenHandle(this);
Andrei Popescu402d9372010-02-26 13:31:12 +00003070 StringTracker::RecordWrite(str);
Steve Block6ded16b2010-05-10 14:33:55 +01003071 if (hints & HINT_MANY_WRITES_EXPECTED) {
3072 // Flatten the string for efficiency. This applies whether we are
3073 // using StringInputBuffer or Get(i) to access the characters.
3074 str->TryFlatten();
3075 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003076 int end = length;
3077 if ( (length == -1) || (length > str->length() - start) )
3078 end = str->length() - start;
3079 if (end < 0) return 0;
3080 write_input_buffer.Reset(start, *str);
3081 int i;
3082 for (i = 0; i < end; i++) {
3083 char c = static_cast<char>(write_input_buffer.GetNext());
3084 if (c == '\0') c = ' ';
3085 buffer[i] = c;
3086 }
3087 if (length == -1 || i < length)
3088 buffer[i] = '\0';
3089 return i;
3090}
3091
3092
Steve Block6ded16b2010-05-10 14:33:55 +01003093int String::Write(uint16_t* buffer,
3094 int start,
3095 int length,
3096 WriteHints hints) const {
Steve Blocka7e24c12009-10-30 11:49:00 +00003097 if (IsDeadCheck("v8::String::Write()")) return 0;
3098 LOG_API("String::Write");
3099 ENTER_V8;
3100 ASSERT(start >= 0 && length >= -1);
3101 i::Handle<i::String> str = Utils::OpenHandle(this);
Andrei Popescu402d9372010-02-26 13:31:12 +00003102 StringTracker::RecordWrite(str);
Steve Block6ded16b2010-05-10 14:33:55 +01003103 if (hints & HINT_MANY_WRITES_EXPECTED) {
3104 // Flatten the string for efficiency. This applies whether we are
3105 // using StringInputBuffer or Get(i) to access the characters.
3106 str->TryFlatten();
3107 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003108 int end = length;
3109 if ( (length == -1) || (length > str->length() - start) )
3110 end = str->length() - start;
3111 if (end < 0) return 0;
3112 i::String::WriteToFlat(*str, buffer, start, end);
3113 if (length == -1 || end < length)
3114 buffer[end] = '\0';
3115 return end;
3116}
3117
3118
3119bool v8::String::IsExternal() const {
3120 EnsureInitialized("v8::String::IsExternal()");
3121 i::Handle<i::String> str = Utils::OpenHandle(this);
3122 return i::StringShape(*str).IsExternalTwoByte();
3123}
3124
3125
3126bool v8::String::IsExternalAscii() const {
3127 EnsureInitialized("v8::String::IsExternalAscii()");
3128 i::Handle<i::String> str = Utils::OpenHandle(this);
3129 return i::StringShape(*str).IsExternalAscii();
3130}
3131
3132
3133void v8::String::VerifyExternalStringResource(
3134 v8::String::ExternalStringResource* value) const {
3135 i::Handle<i::String> str = Utils::OpenHandle(this);
3136 v8::String::ExternalStringResource* expected;
3137 if (i::StringShape(*str).IsExternalTwoByte()) {
3138 void* resource = i::Handle<i::ExternalTwoByteString>::cast(str)->resource();
3139 expected = reinterpret_cast<ExternalStringResource*>(resource);
3140 } else {
3141 expected = NULL;
3142 }
3143 CHECK_EQ(expected, value);
3144}
3145
3146
3147v8::String::ExternalAsciiStringResource*
3148 v8::String::GetExternalAsciiStringResource() const {
3149 EnsureInitialized("v8::String::GetExternalAsciiStringResource()");
3150 i::Handle<i::String> str = Utils::OpenHandle(this);
3151 if (i::StringShape(*str).IsExternalAscii()) {
3152 void* resource = i::Handle<i::ExternalAsciiString>::cast(str)->resource();
3153 return reinterpret_cast<ExternalAsciiStringResource*>(resource);
3154 } else {
3155 return NULL;
3156 }
3157}
3158
3159
3160double Number::Value() const {
3161 if (IsDeadCheck("v8::Number::Value()")) return 0;
3162 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3163 return obj->Number();
3164}
3165
3166
3167bool Boolean::Value() const {
3168 if (IsDeadCheck("v8::Boolean::Value()")) return false;
3169 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3170 return obj->IsTrue();
3171}
3172
3173
3174int64_t Integer::Value() const {
3175 if (IsDeadCheck("v8::Integer::Value()")) return 0;
3176 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3177 if (obj->IsSmi()) {
3178 return i::Smi::cast(*obj)->value();
3179 } else {
3180 return static_cast<int64_t>(obj->Number());
3181 }
3182}
3183
3184
3185int32_t Int32::Value() const {
3186 if (IsDeadCheck("v8::Int32::Value()")) return 0;
3187 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3188 if (obj->IsSmi()) {
3189 return i::Smi::cast(*obj)->value();
3190 } else {
3191 return static_cast<int32_t>(obj->Number());
3192 }
3193}
3194
3195
Steve Block6ded16b2010-05-10 14:33:55 +01003196uint32_t Uint32::Value() const {
3197 if (IsDeadCheck("v8::Uint32::Value()")) return 0;
3198 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3199 if (obj->IsSmi()) {
3200 return i::Smi::cast(*obj)->value();
3201 } else {
3202 return static_cast<uint32_t>(obj->Number());
3203 }
3204}
3205
3206
Steve Blocka7e24c12009-10-30 11:49:00 +00003207int v8::Object::InternalFieldCount() {
3208 if (IsDeadCheck("v8::Object::InternalFieldCount()")) return 0;
3209 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3210 return obj->GetInternalFieldCount();
3211}
3212
3213
3214Local<Value> v8::Object::CheckedGetInternalField(int index) {
3215 if (IsDeadCheck("v8::Object::GetInternalField()")) return Local<Value>();
3216 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3217 if (!ApiCheck(index < obj->GetInternalFieldCount(),
3218 "v8::Object::GetInternalField()",
3219 "Reading internal field out of bounds")) {
3220 return Local<Value>();
3221 }
3222 i::Handle<i::Object> value(obj->GetInternalField(index));
3223 Local<Value> result = Utils::ToLocal(value);
3224#ifdef DEBUG
3225 Local<Value> unchecked = UncheckedGetInternalField(index);
3226 ASSERT(unchecked.IsEmpty() || (unchecked == result));
3227#endif
3228 return result;
3229}
3230
3231
3232void v8::Object::SetInternalField(int index, v8::Handle<Value> value) {
3233 if (IsDeadCheck("v8::Object::SetInternalField()")) return;
3234 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3235 if (!ApiCheck(index < obj->GetInternalFieldCount(),
3236 "v8::Object::SetInternalField()",
3237 "Writing internal field out of bounds")) {
3238 return;
3239 }
3240 ENTER_V8;
3241 i::Handle<i::Object> val = Utils::OpenHandle(*value);
3242 obj->SetInternalField(index, *val);
3243}
3244
3245
3246void v8::Object::SetPointerInInternalField(int index, void* value) {
Steve Block6ded16b2010-05-10 14:33:55 +01003247 ENTER_V8;
Steve Block3ce2e202009-11-05 08:53:23 +00003248 i::Object* as_object = reinterpret_cast<i::Object*>(value);
3249 if (as_object->IsSmi()) {
3250 Utils::OpenHandle(this)->SetInternalField(index, as_object);
3251 return;
3252 }
3253 HandleScope scope;
3254 i::Handle<i::Proxy> proxy =
3255 i::Factory::NewProxy(reinterpret_cast<i::Address>(value), i::TENURED);
3256 if (!proxy.is_null())
3257 Utils::OpenHandle(this)->SetInternalField(index, *proxy);
Steve Blocka7e24c12009-10-30 11:49:00 +00003258}
3259
3260
3261// --- E n v i r o n m e n t ---
3262
3263bool v8::V8::Initialize() {
3264 if (i::V8::IsRunning()) return true;
3265 ENTER_V8;
3266 HandleScope scope;
Steve Blockd0582a62009-12-15 09:54:21 +00003267 if (i::Snapshot::Initialize()) return true;
3268 return i::V8::Initialize(NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +00003269}
3270
3271
3272bool v8::V8::Dispose() {
3273 i::V8::TearDown();
3274 return true;
3275}
3276
3277
Russell Brenner90bac252010-11-18 13:33:46 -08003278HeapStatistics::HeapStatistics(): total_heap_size_(0),
3279 total_heap_size_executable_(0),
3280 used_heap_size_(0) { }
Steve Block3ce2e202009-11-05 08:53:23 +00003281
3282
3283void v8::V8::GetHeapStatistics(HeapStatistics* heap_statistics) {
3284 heap_statistics->set_total_heap_size(i::Heap::CommittedMemory());
Russell Brenner90bac252010-11-18 13:33:46 -08003285 heap_statistics->set_total_heap_size_executable(
3286 i::Heap::CommittedMemoryExecutable());
Steve Block3ce2e202009-11-05 08:53:23 +00003287 heap_statistics->set_used_heap_size(i::Heap::SizeOfObjects());
3288}
3289
3290
3291bool v8::V8::IdleNotification() {
Steve Blocka7e24c12009-10-30 11:49:00 +00003292 // Returning true tells the caller that it need not
3293 // continue to call IdleNotification.
3294 if (!i::V8::IsRunning()) return true;
Steve Block3ce2e202009-11-05 08:53:23 +00003295 return i::V8::IdleNotification();
Steve Blocka7e24c12009-10-30 11:49:00 +00003296}
3297
3298
3299void v8::V8::LowMemoryNotification() {
Steve Blocka7e24c12009-10-30 11:49:00 +00003300 if (!i::V8::IsRunning()) return;
3301 i::Heap::CollectAllGarbage(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00003302}
3303
3304
Steve Block6ded16b2010-05-10 14:33:55 +01003305int v8::V8::ContextDisposedNotification() {
3306 if (!i::V8::IsRunning()) return 0;
3307 return i::Heap::NotifyContextDisposed();
3308}
3309
3310
Steve Blocka7e24c12009-10-30 11:49:00 +00003311const char* v8::V8::GetVersion() {
3312 static v8::internal::EmbeddedVector<char, 128> buffer;
3313 v8::internal::Version::GetString(buffer);
3314 return buffer.start();
3315}
3316
3317
3318static i::Handle<i::FunctionTemplateInfo>
3319 EnsureConstructor(i::Handle<i::ObjectTemplateInfo> templ) {
3320 if (templ->constructor()->IsUndefined()) {
3321 Local<FunctionTemplate> constructor = FunctionTemplate::New();
3322 Utils::OpenHandle(*constructor)->set_instance_template(*templ);
3323 templ->set_constructor(*Utils::OpenHandle(*constructor));
3324 }
3325 return i::Handle<i::FunctionTemplateInfo>(
3326 i::FunctionTemplateInfo::cast(templ->constructor()));
3327}
3328
3329
3330Persistent<Context> v8::Context::New(
3331 v8::ExtensionConfiguration* extensions,
3332 v8::Handle<ObjectTemplate> global_template,
3333 v8::Handle<Value> global_object) {
3334 EnsureInitialized("v8::Context::New()");
3335 LOG_API("Context::New");
3336 ON_BAILOUT("v8::Context::New()", return Persistent<Context>());
3337
3338 // Enter V8 via an ENTER_V8 scope.
3339 i::Handle<i::Context> env;
3340 {
3341 ENTER_V8;
Steve Blocka7e24c12009-10-30 11:49:00 +00003342 v8::Handle<ObjectTemplate> proxy_template = global_template;
3343 i::Handle<i::FunctionTemplateInfo> proxy_constructor;
3344 i::Handle<i::FunctionTemplateInfo> global_constructor;
3345
3346 if (!global_template.IsEmpty()) {
3347 // Make sure that the global_template has a constructor.
3348 global_constructor =
3349 EnsureConstructor(Utils::OpenHandle(*global_template));
3350
3351 // Create a fresh template for the global proxy object.
3352 proxy_template = ObjectTemplate::New();
3353 proxy_constructor =
3354 EnsureConstructor(Utils::OpenHandle(*proxy_template));
3355
3356 // Set the global template to be the prototype template of
3357 // global proxy template.
3358 proxy_constructor->set_prototype_template(
3359 *Utils::OpenHandle(*global_template));
3360
3361 // Migrate security handlers from global_template to
3362 // proxy_template. Temporarily removing access check
3363 // information from the global template.
3364 if (!global_constructor->access_check_info()->IsUndefined()) {
3365 proxy_constructor->set_access_check_info(
3366 global_constructor->access_check_info());
3367 proxy_constructor->set_needs_access_check(
3368 global_constructor->needs_access_check());
3369 global_constructor->set_needs_access_check(false);
3370 global_constructor->set_access_check_info(i::Heap::undefined_value());
3371 }
3372 }
3373
3374 // Create the environment.
3375 env = i::Bootstrapper::CreateEnvironment(
3376 Utils::OpenHandle(*global_object),
3377 proxy_template,
3378 extensions);
3379
3380 // Restore the access check info on the global template.
3381 if (!global_template.IsEmpty()) {
3382 ASSERT(!global_constructor.is_null());
3383 ASSERT(!proxy_constructor.is_null());
3384 global_constructor->set_access_check_info(
3385 proxy_constructor->access_check_info());
3386 global_constructor->set_needs_access_check(
3387 proxy_constructor->needs_access_check());
3388 }
3389 }
3390 // Leave V8.
3391
3392 if (env.is_null())
3393 return Persistent<Context>();
3394 return Persistent<Context>(Utils::ToLocal(env));
3395}
3396
3397
3398void v8::Context::SetSecurityToken(Handle<Value> token) {
3399 if (IsDeadCheck("v8::Context::SetSecurityToken()")) return;
3400 ENTER_V8;
3401 i::Handle<i::Context> env = Utils::OpenHandle(this);
3402 i::Handle<i::Object> token_handle = Utils::OpenHandle(*token);
3403 env->set_security_token(*token_handle);
3404}
3405
3406
3407void v8::Context::UseDefaultSecurityToken() {
3408 if (IsDeadCheck("v8::Context::UseDefaultSecurityToken()")) return;
3409 ENTER_V8;
3410 i::Handle<i::Context> env = Utils::OpenHandle(this);
3411 env->set_security_token(env->global());
3412}
3413
3414
3415Handle<Value> v8::Context::GetSecurityToken() {
3416 if (IsDeadCheck("v8::Context::GetSecurityToken()")) return Handle<Value>();
3417 i::Handle<i::Context> env = Utils::OpenHandle(this);
3418 i::Object* security_token = env->security_token();
3419 i::Handle<i::Object> token_handle(security_token);
3420 return Utils::ToLocal(token_handle);
3421}
3422
3423
3424bool Context::HasOutOfMemoryException() {
3425 i::Handle<i::Context> env = Utils::OpenHandle(this);
3426 return env->has_out_of_memory();
3427}
3428
3429
3430bool Context::InContext() {
3431 return i::Top::context() != NULL;
3432}
3433
3434
3435v8::Local<v8::Context> Context::GetEntered() {
3436 if (IsDeadCheck("v8::Context::GetEntered()")) return Local<Context>();
3437 i::Handle<i::Object> last = thread_local.LastEnteredContext();
3438 if (last.is_null()) return Local<Context>();
3439 i::Handle<i::Context> context = i::Handle<i::Context>::cast(last);
3440 return Utils::ToLocal(context);
3441}
3442
3443
3444v8::Local<v8::Context> Context::GetCurrent() {
3445 if (IsDeadCheck("v8::Context::GetCurrent()")) return Local<Context>();
Steve Block3ce2e202009-11-05 08:53:23 +00003446 i::Handle<i::Object> current = i::Top::global_context();
3447 if (current.is_null()) return Local<Context>();
3448 i::Handle<i::Context> context = i::Handle<i::Context>::cast(current);
Steve Blocka7e24c12009-10-30 11:49:00 +00003449 return Utils::ToLocal(context);
3450}
3451
3452
3453v8::Local<v8::Context> Context::GetCalling() {
3454 if (IsDeadCheck("v8::Context::GetCalling()")) return Local<Context>();
3455 i::Handle<i::Object> calling = i::Top::GetCallingGlobalContext();
3456 if (calling.is_null()) return Local<Context>();
3457 i::Handle<i::Context> context = i::Handle<i::Context>::cast(calling);
3458 return Utils::ToLocal(context);
3459}
3460
3461
3462v8::Local<v8::Object> Context::Global() {
3463 if (IsDeadCheck("v8::Context::Global()")) return Local<v8::Object>();
3464 i::Object** ctx = reinterpret_cast<i::Object**>(this);
3465 i::Handle<i::Context> context =
3466 i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx));
3467 i::Handle<i::Object> global(context->global_proxy());
3468 return Utils::ToLocal(i::Handle<i::JSObject>::cast(global));
3469}
3470
3471
3472void Context::DetachGlobal() {
3473 if (IsDeadCheck("v8::Context::DetachGlobal()")) return;
3474 ENTER_V8;
3475 i::Object** ctx = reinterpret_cast<i::Object**>(this);
3476 i::Handle<i::Context> context =
3477 i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx));
3478 i::Bootstrapper::DetachGlobal(context);
3479}
3480
3481
Andrei Popescu74b3c142010-03-29 12:03:09 +01003482void Context::ReattachGlobal(Handle<Object> global_object) {
3483 if (IsDeadCheck("v8::Context::ReattachGlobal()")) return;
3484 ENTER_V8;
3485 i::Object** ctx = reinterpret_cast<i::Object**>(this);
3486 i::Handle<i::Context> context =
3487 i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx));
3488 i::Bootstrapper::ReattachGlobal(context, Utils::OpenHandle(*global_object));
3489}
3490
3491
Steve Blocka7e24c12009-10-30 11:49:00 +00003492Local<v8::Object> ObjectTemplate::NewInstance() {
3493 ON_BAILOUT("v8::ObjectTemplate::NewInstance()", return Local<v8::Object>());
3494 LOG_API("ObjectTemplate::NewInstance");
3495 ENTER_V8;
3496 EXCEPTION_PREAMBLE();
3497 i::Handle<i::Object> obj =
3498 i::Execution::InstantiateObject(Utils::OpenHandle(this),
3499 &has_pending_exception);
3500 EXCEPTION_BAILOUT_CHECK(Local<v8::Object>());
3501 return Utils::ToLocal(i::Handle<i::JSObject>::cast(obj));
3502}
3503
3504
3505Local<v8::Function> FunctionTemplate::GetFunction() {
3506 ON_BAILOUT("v8::FunctionTemplate::GetFunction()",
3507 return Local<v8::Function>());
3508 LOG_API("FunctionTemplate::GetFunction");
3509 ENTER_V8;
3510 EXCEPTION_PREAMBLE();
3511 i::Handle<i::Object> obj =
3512 i::Execution::InstantiateFunction(Utils::OpenHandle(this),
3513 &has_pending_exception);
3514 EXCEPTION_BAILOUT_CHECK(Local<v8::Function>());
3515 return Utils::ToLocal(i::Handle<i::JSFunction>::cast(obj));
3516}
3517
3518
3519bool FunctionTemplate::HasInstance(v8::Handle<v8::Value> value) {
3520 ON_BAILOUT("v8::FunctionTemplate::HasInstanceOf()", return false);
3521 i::Object* obj = *Utils::OpenHandle(*value);
3522 return obj->IsInstanceOf(*Utils::OpenHandle(this));
3523}
3524
3525
3526static Local<External> ExternalNewImpl(void* data) {
3527 return Utils::ToLocal(i::Factory::NewProxy(static_cast<i::Address>(data)));
3528}
3529
3530static void* ExternalValueImpl(i::Handle<i::Object> obj) {
3531 return reinterpret_cast<void*>(i::Proxy::cast(*obj)->proxy());
3532}
3533
3534
Steve Blocka7e24c12009-10-30 11:49:00 +00003535Local<Value> v8::External::Wrap(void* data) {
3536 STATIC_ASSERT(sizeof(data) == sizeof(i::Address));
3537 LOG_API("External::Wrap");
3538 EnsureInitialized("v8::External::Wrap()");
3539 ENTER_V8;
Steve Block3ce2e202009-11-05 08:53:23 +00003540 i::Object* as_object = reinterpret_cast<i::Object*>(data);
3541 if (as_object->IsSmi()) {
3542 return Utils::ToLocal(i::Handle<i::Object>(as_object));
Steve Blocka7e24c12009-10-30 11:49:00 +00003543 }
3544 return ExternalNewImpl(data);
3545}
3546
3547
Steve Block3ce2e202009-11-05 08:53:23 +00003548void* v8::Object::SlowGetPointerFromInternalField(int index) {
3549 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3550 i::Object* value = obj->GetInternalField(index);
3551 if (value->IsSmi()) {
3552 return value;
3553 } else if (value->IsProxy()) {
3554 return reinterpret_cast<void*>(i::Proxy::cast(value)->proxy());
3555 } else {
3556 return NULL;
3557 }
3558}
3559
3560
Steve Blocka7e24c12009-10-30 11:49:00 +00003561void* v8::External::FullUnwrap(v8::Handle<v8::Value> wrapper) {
3562 if (IsDeadCheck("v8::External::Unwrap()")) return 0;
3563 i::Handle<i::Object> obj = Utils::OpenHandle(*wrapper);
3564 void* result;
3565 if (obj->IsSmi()) {
3566 // The external value was an aligned pointer.
Steve Block3ce2e202009-11-05 08:53:23 +00003567 result = *obj;
Steve Blocka7e24c12009-10-30 11:49:00 +00003568 } else if (obj->IsProxy()) {
3569 result = ExternalValueImpl(obj);
3570 } else {
3571 result = NULL;
3572 }
3573 ASSERT_EQ(result, QuickUnwrap(wrapper));
3574 return result;
3575}
3576
3577
3578Local<External> v8::External::New(void* data) {
3579 STATIC_ASSERT(sizeof(data) == sizeof(i::Address));
3580 LOG_API("External::New");
3581 EnsureInitialized("v8::External::New()");
3582 ENTER_V8;
3583 return ExternalNewImpl(data);
3584}
3585
3586
3587void* External::Value() const {
3588 if (IsDeadCheck("v8::External::Value()")) return 0;
3589 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3590 return ExternalValueImpl(obj);
3591}
3592
3593
3594Local<String> v8::String::Empty() {
3595 EnsureInitialized("v8::String::Empty()");
3596 LOG_API("String::Empty()");
3597 return Utils::ToLocal(i::Factory::empty_symbol());
3598}
3599
3600
3601Local<String> v8::String::New(const char* data, int length) {
3602 EnsureInitialized("v8::String::New()");
3603 LOG_API("String::New(char)");
3604 if (length == 0) return Empty();
3605 ENTER_V8;
Steve Blockd0582a62009-12-15 09:54:21 +00003606 if (length == -1) length = i::StrLength(data);
Steve Blocka7e24c12009-10-30 11:49:00 +00003607 i::Handle<i::String> result =
3608 i::Factory::NewStringFromUtf8(i::Vector<const char>(data, length));
3609 return Utils::ToLocal(result);
3610}
3611
3612
Steve Block3ce2e202009-11-05 08:53:23 +00003613Local<String> v8::String::Concat(Handle<String> left, Handle<String> right) {
3614 EnsureInitialized("v8::String::New()");
3615 LOG_API("String::New(char)");
3616 ENTER_V8;
3617 i::Handle<i::String> left_string = Utils::OpenHandle(*left);
3618 i::Handle<i::String> right_string = Utils::OpenHandle(*right);
3619 i::Handle<i::String> result = i::Factory::NewConsString(left_string,
3620 right_string);
3621 return Utils::ToLocal(result);
3622}
3623
3624
Steve Blocka7e24c12009-10-30 11:49:00 +00003625Local<String> v8::String::NewUndetectable(const char* data, int length) {
3626 EnsureInitialized("v8::String::NewUndetectable()");
3627 LOG_API("String::NewUndetectable(char)");
3628 ENTER_V8;
Steve Blockd0582a62009-12-15 09:54:21 +00003629 if (length == -1) length = i::StrLength(data);
Steve Blocka7e24c12009-10-30 11:49:00 +00003630 i::Handle<i::String> result =
3631 i::Factory::NewStringFromUtf8(i::Vector<const char>(data, length));
3632 result->MarkAsUndetectable();
3633 return Utils::ToLocal(result);
3634}
3635
3636
3637static int TwoByteStringLength(const uint16_t* data) {
3638 int length = 0;
3639 while (data[length] != '\0') length++;
3640 return length;
3641}
3642
3643
3644Local<String> v8::String::New(const uint16_t* data, int length) {
3645 EnsureInitialized("v8::String::New()");
3646 LOG_API("String::New(uint16_)");
3647 if (length == 0) return Empty();
3648 ENTER_V8;
3649 if (length == -1) length = TwoByteStringLength(data);
3650 i::Handle<i::String> result =
3651 i::Factory::NewStringFromTwoByte(i::Vector<const uint16_t>(data, length));
3652 return Utils::ToLocal(result);
3653}
3654
3655
3656Local<String> v8::String::NewUndetectable(const uint16_t* data, int length) {
3657 EnsureInitialized("v8::String::NewUndetectable()");
3658 LOG_API("String::NewUndetectable(uint16_)");
3659 ENTER_V8;
3660 if (length == -1) length = TwoByteStringLength(data);
3661 i::Handle<i::String> result =
3662 i::Factory::NewStringFromTwoByte(i::Vector<const uint16_t>(data, length));
3663 result->MarkAsUndetectable();
3664 return Utils::ToLocal(result);
3665}
3666
3667
3668i::Handle<i::String> NewExternalStringHandle(
3669 v8::String::ExternalStringResource* resource) {
3670 i::Handle<i::String> result =
3671 i::Factory::NewExternalStringFromTwoByte(resource);
3672 return result;
3673}
3674
3675
3676i::Handle<i::String> NewExternalAsciiStringHandle(
3677 v8::String::ExternalAsciiStringResource* resource) {
3678 i::Handle<i::String> result =
3679 i::Factory::NewExternalStringFromAscii(resource);
3680 return result;
3681}
3682
3683
Steve Blocka7e24c12009-10-30 11:49:00 +00003684Local<String> v8::String::NewExternal(
3685 v8::String::ExternalStringResource* resource) {
3686 EnsureInitialized("v8::String::NewExternal()");
3687 LOG_API("String::NewExternal");
3688 ENTER_V8;
Steve Blocka7e24c12009-10-30 11:49:00 +00003689 i::Handle<i::String> result = NewExternalStringHandle(resource);
Leon Clarkee46be812010-01-19 14:06:41 +00003690 i::ExternalStringTable::AddString(*result);
Steve Blocka7e24c12009-10-30 11:49:00 +00003691 return Utils::ToLocal(result);
3692}
3693
3694
3695bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) {
3696 if (IsDeadCheck("v8::String::MakeExternal()")) return false;
3697 if (this->IsExternal()) return false; // Already an external string.
3698 ENTER_V8;
3699 i::Handle<i::String> obj = Utils::OpenHandle(this);
Andrei Popescu402d9372010-02-26 13:31:12 +00003700 if (StringTracker::IsFreshUnusedString(obj)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00003701 bool result = obj->MakeExternal(resource);
3702 if (result && !obj->IsSymbol()) {
Leon Clarkee46be812010-01-19 14:06:41 +00003703 i::ExternalStringTable::AddString(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003704 }
3705 return result;
3706}
3707
3708
3709Local<String> v8::String::NewExternal(
3710 v8::String::ExternalAsciiStringResource* resource) {
3711 EnsureInitialized("v8::String::NewExternal()");
3712 LOG_API("String::NewExternal");
3713 ENTER_V8;
Steve Blocka7e24c12009-10-30 11:49:00 +00003714 i::Handle<i::String> result = NewExternalAsciiStringHandle(resource);
Leon Clarkee46be812010-01-19 14:06:41 +00003715 i::ExternalStringTable::AddString(*result);
Steve Blocka7e24c12009-10-30 11:49:00 +00003716 return Utils::ToLocal(result);
3717}
3718
3719
3720bool v8::String::MakeExternal(
3721 v8::String::ExternalAsciiStringResource* resource) {
3722 if (IsDeadCheck("v8::String::MakeExternal()")) return false;
3723 if (this->IsExternal()) return false; // Already an external string.
3724 ENTER_V8;
3725 i::Handle<i::String> obj = Utils::OpenHandle(this);
Andrei Popescu402d9372010-02-26 13:31:12 +00003726 if (StringTracker::IsFreshUnusedString(obj)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00003727 bool result = obj->MakeExternal(resource);
3728 if (result && !obj->IsSymbol()) {
Leon Clarkee46be812010-01-19 14:06:41 +00003729 i::ExternalStringTable::AddString(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003730 }
3731 return result;
3732}
3733
3734
3735bool v8::String::CanMakeExternal() {
3736 if (IsDeadCheck("v8::String::CanMakeExternal()")) return false;
3737 i::Handle<i::String> obj = Utils::OpenHandle(this);
Andrei Popescu402d9372010-02-26 13:31:12 +00003738 if (StringTracker::IsFreshUnusedString(obj)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00003739 int size = obj->Size(); // Byte size of the original string.
3740 if (size < i::ExternalString::kSize)
3741 return false;
3742 i::StringShape shape(*obj);
3743 return !shape.IsExternal();
3744}
3745
3746
3747Local<v8::Object> v8::Object::New() {
3748 EnsureInitialized("v8::Object::New()");
3749 LOG_API("Object::New");
3750 ENTER_V8;
3751 i::Handle<i::JSObject> obj =
3752 i::Factory::NewJSObject(i::Top::object_function());
3753 return Utils::ToLocal(obj);
3754}
3755
3756
3757Local<v8::Value> v8::Date::New(double time) {
3758 EnsureInitialized("v8::Date::New()");
3759 LOG_API("Date::New");
Steve Blockd0582a62009-12-15 09:54:21 +00003760 if (isnan(time)) {
3761 // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
3762 time = i::OS::nan_value();
3763 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003764 ENTER_V8;
3765 EXCEPTION_PREAMBLE();
3766 i::Handle<i::Object> obj =
3767 i::Execution::NewDate(time, &has_pending_exception);
3768 EXCEPTION_BAILOUT_CHECK(Local<v8::Value>());
3769 return Utils::ToLocal(obj);
3770}
3771
3772
3773double v8::Date::NumberValue() const {
3774 if (IsDeadCheck("v8::Date::NumberValue()")) return 0;
3775 LOG_API("Date::NumberValue");
3776 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3777 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
3778 return jsvalue->value()->Number();
3779}
3780
3781
Ben Murdochf87a2032010-10-22 12:50:53 +01003782static i::Handle<i::String> RegExpFlagsToString(RegExp::Flags flags) {
3783 char flags_buf[3];
3784 int num_flags = 0;
3785 if ((flags & RegExp::kGlobal) != 0) flags_buf[num_flags++] = 'g';
3786 if ((flags & RegExp::kMultiline) != 0) flags_buf[num_flags++] = 'm';
3787 if ((flags & RegExp::kIgnoreCase) != 0) flags_buf[num_flags++] = 'i';
3788 ASSERT(num_flags <= static_cast<int>(ARRAY_SIZE(flags_buf)));
3789 return i::Factory::LookupSymbol(
3790 i::Vector<const char>(flags_buf, num_flags));
3791}
3792
3793
3794Local<v8::RegExp> v8::RegExp::New(Handle<String> pattern,
3795 Flags flags) {
3796 EnsureInitialized("v8::RegExp::New()");
3797 LOG_API("RegExp::New");
3798 ENTER_V8;
3799 EXCEPTION_PREAMBLE();
3800 i::Handle<i::JSRegExp> obj = i::Execution::NewJSRegExp(
3801 Utils::OpenHandle(*pattern),
3802 RegExpFlagsToString(flags),
3803 &has_pending_exception);
3804 EXCEPTION_BAILOUT_CHECK(Local<v8::RegExp>());
3805 return Utils::ToLocal(i::Handle<i::JSRegExp>::cast(obj));
3806}
3807
3808
3809Local<v8::String> v8::RegExp::GetSource() const {
3810 if (IsDeadCheck("v8::RegExp::GetSource()")) return Local<v8::String>();
3811 i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
3812 return Utils::ToLocal(i::Handle<i::String>(obj->Pattern()));
3813}
3814
3815
3816// Assert that the static flags cast in GetFlags is valid.
3817#define REGEXP_FLAG_ASSERT_EQ(api_flag, internal_flag) \
3818 STATIC_ASSERT(static_cast<int>(v8::RegExp::api_flag) == \
3819 static_cast<int>(i::JSRegExp::internal_flag))
3820REGEXP_FLAG_ASSERT_EQ(kNone, NONE);
3821REGEXP_FLAG_ASSERT_EQ(kGlobal, GLOBAL);
3822REGEXP_FLAG_ASSERT_EQ(kIgnoreCase, IGNORE_CASE);
3823REGEXP_FLAG_ASSERT_EQ(kMultiline, MULTILINE);
3824#undef REGEXP_FLAG_ASSERT_EQ
3825
3826v8::RegExp::Flags v8::RegExp::GetFlags() const {
3827 if (IsDeadCheck("v8::RegExp::GetFlags()")) return v8::RegExp::kNone;
3828 i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
3829 return static_cast<RegExp::Flags>(obj->GetFlags().value());
3830}
3831
3832
Steve Blocka7e24c12009-10-30 11:49:00 +00003833Local<v8::Array> v8::Array::New(int length) {
3834 EnsureInitialized("v8::Array::New()");
3835 LOG_API("Array::New");
3836 ENTER_V8;
3837 i::Handle<i::JSArray> obj = i::Factory::NewJSArray(length);
3838 return Utils::ToLocal(obj);
3839}
3840
3841
3842uint32_t v8::Array::Length() const {
3843 if (IsDeadCheck("v8::Array::Length()")) return 0;
3844 i::Handle<i::JSArray> obj = Utils::OpenHandle(this);
3845 i::Object* length = obj->length();
3846 if (length->IsSmi()) {
3847 return i::Smi::cast(length)->value();
3848 } else {
3849 return static_cast<uint32_t>(length->Number());
3850 }
3851}
3852
3853
3854Local<Object> Array::CloneElementAt(uint32_t index) {
3855 ON_BAILOUT("v8::Array::CloneElementAt()", return Local<Object>());
3856 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3857 if (!self->HasFastElements()) {
3858 return Local<Object>();
3859 }
3860 i::FixedArray* elms = i::FixedArray::cast(self->elements());
3861 i::Object* paragon = elms->get(index);
3862 if (!paragon->IsJSObject()) {
3863 return Local<Object>();
3864 }
3865 i::Handle<i::JSObject> paragon_handle(i::JSObject::cast(paragon));
3866 EXCEPTION_PREAMBLE();
Steve Block6ded16b2010-05-10 14:33:55 +01003867 ENTER_V8;
Steve Blocka7e24c12009-10-30 11:49:00 +00003868 i::Handle<i::JSObject> result = i::Copy(paragon_handle);
3869 has_pending_exception = result.is_null();
3870 EXCEPTION_BAILOUT_CHECK(Local<Object>());
3871 return Utils::ToLocal(result);
3872}
3873
3874
3875Local<String> v8::String::NewSymbol(const char* data, int length) {
3876 EnsureInitialized("v8::String::NewSymbol()");
3877 LOG_API("String::NewSymbol(char)");
3878 ENTER_V8;
Steve Blockd0582a62009-12-15 09:54:21 +00003879 if (length == -1) length = i::StrLength(data);
Steve Blocka7e24c12009-10-30 11:49:00 +00003880 i::Handle<i::String> result =
3881 i::Factory::LookupSymbol(i::Vector<const char>(data, length));
3882 return Utils::ToLocal(result);
3883}
3884
3885
3886Local<Number> v8::Number::New(double value) {
3887 EnsureInitialized("v8::Number::New()");
Steve Blockd0582a62009-12-15 09:54:21 +00003888 if (isnan(value)) {
3889 // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
3890 value = i::OS::nan_value();
3891 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003892 ENTER_V8;
3893 i::Handle<i::Object> result = i::Factory::NewNumber(value);
3894 return Utils::NumberToLocal(result);
3895}
3896
3897
3898Local<Integer> v8::Integer::New(int32_t value) {
3899 EnsureInitialized("v8::Integer::New()");
3900 if (i::Smi::IsValid(value)) {
3901 return Utils::IntegerToLocal(i::Handle<i::Object>(i::Smi::FromInt(value)));
3902 }
3903 ENTER_V8;
3904 i::Handle<i::Object> result = i::Factory::NewNumber(value);
3905 return Utils::IntegerToLocal(result);
3906}
3907
3908
Steve Block3ce2e202009-11-05 08:53:23 +00003909Local<Integer> Integer::NewFromUnsigned(uint32_t value) {
3910 bool fits_into_int32_t = (value & (1 << 31)) == 0;
3911 if (fits_into_int32_t) {
3912 return Integer::New(static_cast<int32_t>(value));
3913 }
3914 ENTER_V8;
3915 i::Handle<i::Object> result = i::Factory::NewNumber(value);
3916 return Utils::IntegerToLocal(result);
3917}
3918
3919
Steve Blocka7e24c12009-10-30 11:49:00 +00003920void V8::IgnoreOutOfMemoryException() {
3921 thread_local.set_ignore_out_of_memory(true);
3922}
3923
3924
3925bool V8::AddMessageListener(MessageCallback that, Handle<Value> data) {
3926 EnsureInitialized("v8::V8::AddMessageListener()");
3927 ON_BAILOUT("v8::V8::AddMessageListener()", return false);
3928 ENTER_V8;
3929 HandleScope scope;
3930 NeanderArray listeners(i::Factory::message_listeners());
3931 NeanderObject obj(2);
3932 obj.set(0, *i::Factory::NewProxy(FUNCTION_ADDR(that)));
3933 obj.set(1, data.IsEmpty() ?
3934 i::Heap::undefined_value() :
3935 *Utils::OpenHandle(*data));
3936 listeners.add(obj.value());
3937 return true;
3938}
3939
3940
3941void V8::RemoveMessageListeners(MessageCallback that) {
3942 EnsureInitialized("v8::V8::RemoveMessageListener()");
3943 ON_BAILOUT("v8::V8::RemoveMessageListeners()", return);
3944 ENTER_V8;
3945 HandleScope scope;
3946 NeanderArray listeners(i::Factory::message_listeners());
3947 for (int i = 0; i < listeners.length(); i++) {
3948 if (listeners.get(i)->IsUndefined()) continue; // skip deleted ones
3949
3950 NeanderObject listener(i::JSObject::cast(listeners.get(i)));
3951 i::Handle<i::Proxy> callback_obj(i::Proxy::cast(listener.get(0)));
3952 if (callback_obj->proxy() == FUNCTION_ADDR(that)) {
3953 listeners.set(i, i::Heap::undefined_value());
3954 }
3955 }
3956}
3957
3958
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003959void V8::SetCaptureStackTraceForUncaughtExceptions(
3960 bool capture,
3961 int frame_limit,
3962 StackTrace::StackTraceOptions options) {
3963 i::Top::SetCaptureStackTraceForUncaughtExceptions(
3964 capture,
3965 frame_limit,
3966 options);
3967}
3968
3969
Steve Blocka7e24c12009-10-30 11:49:00 +00003970void V8::SetCounterFunction(CounterLookupCallback callback) {
3971 if (IsDeadCheck("v8::V8::SetCounterFunction()")) return;
3972 i::StatsTable::SetCounterFunction(callback);
3973}
3974
3975void V8::SetCreateHistogramFunction(CreateHistogramCallback callback) {
3976 if (IsDeadCheck("v8::V8::SetCreateHistogramFunction()")) return;
3977 i::StatsTable::SetCreateHistogramFunction(callback);
3978}
3979
3980void V8::SetAddHistogramSampleFunction(AddHistogramSampleCallback callback) {
3981 if (IsDeadCheck("v8::V8::SetAddHistogramSampleFunction()")) return;
3982 i::StatsTable::SetAddHistogramSampleFunction(callback);
3983}
3984
3985void V8::EnableSlidingStateWindow() {
3986 if (IsDeadCheck("v8::V8::EnableSlidingStateWindow()")) return;
3987 i::Logger::EnableSlidingStateWindow();
3988}
3989
3990
3991void V8::SetFailedAccessCheckCallbackFunction(
3992 FailedAccessCheckCallback callback) {
3993 if (IsDeadCheck("v8::V8::SetFailedAccessCheckCallbackFunction()")) return;
3994 i::Top::SetFailedAccessCheckCallback(callback);
3995}
3996
3997
3998void V8::AddObjectGroup(Persistent<Value>* objects, size_t length) {
3999 if (IsDeadCheck("v8::V8::AddObjectGroup()")) return;
4000 STATIC_ASSERT(sizeof(Persistent<Value>) == sizeof(i::Object**));
4001 i::GlobalHandles::AddGroup(reinterpret_cast<i::Object***>(objects), length);
4002}
4003
4004
4005int V8::AdjustAmountOfExternalAllocatedMemory(int change_in_bytes) {
4006 if (IsDeadCheck("v8::V8::AdjustAmountOfExternalAllocatedMemory()")) return 0;
4007 return i::Heap::AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
4008}
4009
4010
4011void V8::SetGlobalGCPrologueCallback(GCCallback callback) {
4012 if (IsDeadCheck("v8::V8::SetGlobalGCPrologueCallback()")) return;
4013 i::Heap::SetGlobalGCPrologueCallback(callback);
4014}
4015
4016
4017void V8::SetGlobalGCEpilogueCallback(GCCallback callback) {
4018 if (IsDeadCheck("v8::V8::SetGlobalGCEpilogueCallback()")) return;
4019 i::Heap::SetGlobalGCEpilogueCallback(callback);
4020}
4021
4022
Steve Block6ded16b2010-05-10 14:33:55 +01004023void V8::AddGCPrologueCallback(GCPrologueCallback callback, GCType gc_type) {
4024 if (IsDeadCheck("v8::V8::AddGCPrologueCallback()")) return;
4025 i::Heap::AddGCPrologueCallback(callback, gc_type);
4026}
4027
4028
4029void V8::RemoveGCPrologueCallback(GCPrologueCallback callback) {
4030 if (IsDeadCheck("v8::V8::RemoveGCPrologueCallback()")) return;
4031 i::Heap::RemoveGCPrologueCallback(callback);
4032}
4033
4034
4035void V8::AddGCEpilogueCallback(GCEpilogueCallback callback, GCType gc_type) {
4036 if (IsDeadCheck("v8::V8::AddGCEpilogueCallback()")) return;
4037 i::Heap::AddGCEpilogueCallback(callback, gc_type);
4038}
4039
4040
4041void V8::RemoveGCEpilogueCallback(GCEpilogueCallback callback) {
4042 if (IsDeadCheck("v8::V8::RemoveGCEpilogueCallback()")) return;
4043 i::Heap::RemoveGCEpilogueCallback(callback);
4044}
4045
4046
Iain Merrick9ac36c92010-09-13 15:29:50 +01004047void V8::AddMemoryAllocationCallback(MemoryAllocationCallback callback,
4048 ObjectSpace space,
4049 AllocationAction action) {
4050 if (IsDeadCheck("v8::V8::AddMemoryAllocationCallback()")) return;
4051 i::MemoryAllocator::AddMemoryAllocationCallback(callback,
4052 space,
4053 action);
4054}
4055
4056
4057void V8::RemoveMemoryAllocationCallback(MemoryAllocationCallback callback) {
4058 if (IsDeadCheck("v8::V8::RemoveMemoryAllocationCallback()")) return;
4059 i::MemoryAllocator::RemoveMemoryAllocationCallback(callback);
4060}
4061
4062
Steve Blocka7e24c12009-10-30 11:49:00 +00004063void V8::PauseProfiler() {
4064#ifdef ENABLE_LOGGING_AND_PROFILING
Andrei Popescu402d9372010-02-26 13:31:12 +00004065 PauseProfilerEx(PROFILER_MODULE_CPU);
Steve Blocka7e24c12009-10-30 11:49:00 +00004066#endif
4067}
4068
4069
4070void V8::ResumeProfiler() {
4071#ifdef ENABLE_LOGGING_AND_PROFILING
Andrei Popescu402d9372010-02-26 13:31:12 +00004072 ResumeProfilerEx(PROFILER_MODULE_CPU);
Steve Blocka7e24c12009-10-30 11:49:00 +00004073#endif
4074}
4075
4076
4077bool V8::IsProfilerPaused() {
4078#ifdef ENABLE_LOGGING_AND_PROFILING
4079 return i::Logger::GetActiveProfilerModules() & PROFILER_MODULE_CPU;
4080#else
4081 return true;
4082#endif
4083}
4084
4085
Andrei Popescu402d9372010-02-26 13:31:12 +00004086void V8::ResumeProfilerEx(int flags, int tag) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004087#ifdef ENABLE_LOGGING_AND_PROFILING
4088 if (flags & PROFILER_MODULE_HEAP_SNAPSHOT) {
4089 // Snapshot mode: resume modules, perform GC, then pause only
4090 // those modules which haven't been started prior to making a
4091 // snapshot.
4092
Steve Block6ded16b2010-05-10 14:33:55 +01004093 // Make a GC prior to taking a snapshot.
4094 i::Heap::CollectAllGarbage(false);
Steve Blocka7e24c12009-10-30 11:49:00 +00004095 // Reset snapshot flag and CPU module flags.
4096 flags &= ~(PROFILER_MODULE_HEAP_SNAPSHOT | PROFILER_MODULE_CPU);
4097 const int current_flags = i::Logger::GetActiveProfilerModules();
Andrei Popescu402d9372010-02-26 13:31:12 +00004098 i::Logger::ResumeProfiler(flags, tag);
Steve Blocka7e24c12009-10-30 11:49:00 +00004099 i::Heap::CollectAllGarbage(false);
Andrei Popescu402d9372010-02-26 13:31:12 +00004100 i::Logger::PauseProfiler(~current_flags & flags, tag);
Steve Blocka7e24c12009-10-30 11:49:00 +00004101 } else {
Andrei Popescu402d9372010-02-26 13:31:12 +00004102 i::Logger::ResumeProfiler(flags, tag);
Steve Blocka7e24c12009-10-30 11:49:00 +00004103 }
4104#endif
4105}
4106
4107
Andrei Popescu402d9372010-02-26 13:31:12 +00004108void V8::PauseProfilerEx(int flags, int tag) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004109#ifdef ENABLE_LOGGING_AND_PROFILING
Andrei Popescu402d9372010-02-26 13:31:12 +00004110 i::Logger::PauseProfiler(flags, tag);
Steve Blocka7e24c12009-10-30 11:49:00 +00004111#endif
4112}
4113
4114
4115int V8::GetActiveProfilerModules() {
4116#ifdef ENABLE_LOGGING_AND_PROFILING
4117 return i::Logger::GetActiveProfilerModules();
4118#else
4119 return PROFILER_MODULE_NONE;
4120#endif
4121}
4122
4123
4124int V8::GetLogLines(int from_pos, char* dest_buf, int max_size) {
4125#ifdef ENABLE_LOGGING_AND_PROFILING
Steve Block6ded16b2010-05-10 14:33:55 +01004126 ASSERT(max_size >= kMinimumSizeForLogLinesBuffer);
Steve Blocka7e24c12009-10-30 11:49:00 +00004127 return i::Logger::GetLogLines(from_pos, dest_buf, max_size);
4128#endif
4129 return 0;
4130}
4131
4132
4133int V8::GetCurrentThreadId() {
4134 API_ENTRY_CHECK("V8::GetCurrentThreadId()");
4135 EnsureInitialized("V8::GetCurrentThreadId()");
4136 return i::Top::thread_id();
4137}
4138
4139
4140void V8::TerminateExecution(int thread_id) {
4141 if (!i::V8::IsRunning()) return;
4142 API_ENTRY_CHECK("V8::GetCurrentThreadId()");
4143 // If the thread_id identifies the current thread just terminate
4144 // execution right away. Otherwise, ask the thread manager to
4145 // terminate the thread with the given id if any.
4146 if (thread_id == i::Top::thread_id()) {
4147 i::StackGuard::TerminateExecution();
4148 } else {
4149 i::ThreadManager::TerminateExecution(thread_id);
4150 }
4151}
4152
4153
4154void V8::TerminateExecution() {
4155 if (!i::V8::IsRunning()) return;
4156 i::StackGuard::TerminateExecution();
4157}
4158
4159
Steve Block6ded16b2010-05-10 14:33:55 +01004160bool V8::IsExecutionTerminating() {
4161 if (!i::V8::IsRunning()) return false;
4162 if (i::Top::has_scheduled_exception()) {
4163 return i::Top::scheduled_exception() == i::Heap::termination_exception();
4164 }
4165 return false;
4166}
4167
4168
Steve Blocka7e24c12009-10-30 11:49:00 +00004169String::Utf8Value::Utf8Value(v8::Handle<v8::Value> obj) {
4170 EnsureInitialized("v8::String::Utf8Value::Utf8Value()");
4171 if (obj.IsEmpty()) {
4172 str_ = NULL;
4173 length_ = 0;
4174 return;
4175 }
4176 ENTER_V8;
4177 HandleScope scope;
4178 TryCatch try_catch;
4179 Handle<String> str = obj->ToString();
4180 if (str.IsEmpty()) {
4181 str_ = NULL;
4182 length_ = 0;
4183 } else {
4184 length_ = str->Utf8Length();
4185 str_ = i::NewArray<char>(length_ + 1);
4186 str->WriteUtf8(str_);
4187 }
4188}
4189
4190
4191String::Utf8Value::~Utf8Value() {
4192 i::DeleteArray(str_);
4193}
4194
4195
4196String::AsciiValue::AsciiValue(v8::Handle<v8::Value> obj) {
4197 EnsureInitialized("v8::String::AsciiValue::AsciiValue()");
4198 if (obj.IsEmpty()) {
4199 str_ = NULL;
4200 length_ = 0;
4201 return;
4202 }
4203 ENTER_V8;
4204 HandleScope scope;
4205 TryCatch try_catch;
4206 Handle<String> str = obj->ToString();
4207 if (str.IsEmpty()) {
4208 str_ = NULL;
4209 length_ = 0;
4210 } else {
4211 length_ = str->Length();
4212 str_ = i::NewArray<char>(length_ + 1);
4213 str->WriteAscii(str_);
4214 }
4215}
4216
4217
4218String::AsciiValue::~AsciiValue() {
4219 i::DeleteArray(str_);
4220}
4221
4222
4223String::Value::Value(v8::Handle<v8::Value> obj) {
4224 EnsureInitialized("v8::String::Value::Value()");
4225 if (obj.IsEmpty()) {
4226 str_ = NULL;
4227 length_ = 0;
4228 return;
4229 }
4230 ENTER_V8;
4231 HandleScope scope;
4232 TryCatch try_catch;
4233 Handle<String> str = obj->ToString();
4234 if (str.IsEmpty()) {
4235 str_ = NULL;
4236 length_ = 0;
4237 } else {
4238 length_ = str->Length();
4239 str_ = i::NewArray<uint16_t>(length_ + 1);
4240 str->Write(str_);
4241 }
4242}
4243
4244
4245String::Value::~Value() {
4246 i::DeleteArray(str_);
4247}
4248
4249Local<Value> Exception::RangeError(v8::Handle<v8::String> raw_message) {
4250 LOG_API("RangeError");
4251 ON_BAILOUT("v8::Exception::RangeError()", return Local<Value>());
4252 ENTER_V8;
4253 i::Object* error;
4254 {
4255 HandleScope scope;
4256 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
4257 i::Handle<i::Object> result = i::Factory::NewRangeError(message);
4258 error = *result;
4259 }
4260 i::Handle<i::Object> result(error);
4261 return Utils::ToLocal(result);
4262}
4263
4264Local<Value> Exception::ReferenceError(v8::Handle<v8::String> raw_message) {
4265 LOG_API("ReferenceError");
4266 ON_BAILOUT("v8::Exception::ReferenceError()", return Local<Value>());
4267 ENTER_V8;
4268 i::Object* error;
4269 {
4270 HandleScope scope;
4271 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
4272 i::Handle<i::Object> result = i::Factory::NewReferenceError(message);
4273 error = *result;
4274 }
4275 i::Handle<i::Object> result(error);
4276 return Utils::ToLocal(result);
4277}
4278
4279Local<Value> Exception::SyntaxError(v8::Handle<v8::String> raw_message) {
4280 LOG_API("SyntaxError");
4281 ON_BAILOUT("v8::Exception::SyntaxError()", return Local<Value>());
4282 ENTER_V8;
4283 i::Object* error;
4284 {
4285 HandleScope scope;
4286 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
4287 i::Handle<i::Object> result = i::Factory::NewSyntaxError(message);
4288 error = *result;
4289 }
4290 i::Handle<i::Object> result(error);
4291 return Utils::ToLocal(result);
4292}
4293
4294Local<Value> Exception::TypeError(v8::Handle<v8::String> raw_message) {
4295 LOG_API("TypeError");
4296 ON_BAILOUT("v8::Exception::TypeError()", return Local<Value>());
4297 ENTER_V8;
4298 i::Object* error;
4299 {
4300 HandleScope scope;
4301 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
4302 i::Handle<i::Object> result = i::Factory::NewTypeError(message);
4303 error = *result;
4304 }
4305 i::Handle<i::Object> result(error);
4306 return Utils::ToLocal(result);
4307}
4308
4309Local<Value> Exception::Error(v8::Handle<v8::String> raw_message) {
4310 LOG_API("Error");
4311 ON_BAILOUT("v8::Exception::Error()", return Local<Value>());
4312 ENTER_V8;
4313 i::Object* error;
4314 {
4315 HandleScope scope;
4316 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
4317 i::Handle<i::Object> result = i::Factory::NewError(message);
4318 error = *result;
4319 }
4320 i::Handle<i::Object> result(error);
4321 return Utils::ToLocal(result);
4322}
4323
4324
4325// --- D e b u g S u p p o r t ---
4326
4327#ifdef ENABLE_DEBUGGER_SUPPORT
Leon Clarkef7060e22010-06-03 12:02:55 +01004328
4329static v8::Debug::EventCallback event_callback = NULL;
4330
4331static void EventCallbackWrapper(const v8::Debug::EventDetails& event_details) {
4332 if (event_callback) {
4333 event_callback(event_details.GetEvent(),
4334 event_details.GetExecutionState(),
4335 event_details.GetEventData(),
4336 event_details.GetCallbackData());
4337 }
4338}
4339
4340
Steve Blocka7e24c12009-10-30 11:49:00 +00004341bool Debug::SetDebugEventListener(EventCallback that, Handle<Value> data) {
4342 EnsureInitialized("v8::Debug::SetDebugEventListener()");
4343 ON_BAILOUT("v8::Debug::SetDebugEventListener()", return false);
4344 ENTER_V8;
Leon Clarkef7060e22010-06-03 12:02:55 +01004345
4346 event_callback = that;
4347
4348 HandleScope scope;
4349 i::Handle<i::Object> proxy = i::Factory::undefined_value();
4350 if (that != NULL) {
4351 proxy = i::Factory::NewProxy(FUNCTION_ADDR(EventCallbackWrapper));
4352 }
4353 i::Debugger::SetEventListener(proxy, Utils::OpenHandle(*data));
4354 return true;
4355}
4356
4357
4358bool Debug::SetDebugEventListener2(EventCallback2 that, Handle<Value> data) {
4359 EnsureInitialized("v8::Debug::SetDebugEventListener2()");
4360 ON_BAILOUT("v8::Debug::SetDebugEventListener2()", return false);
4361 ENTER_V8;
Steve Blocka7e24c12009-10-30 11:49:00 +00004362 HandleScope scope;
4363 i::Handle<i::Object> proxy = i::Factory::undefined_value();
4364 if (that != NULL) {
4365 proxy = i::Factory::NewProxy(FUNCTION_ADDR(that));
4366 }
4367 i::Debugger::SetEventListener(proxy, Utils::OpenHandle(*data));
4368 return true;
4369}
4370
4371
4372bool Debug::SetDebugEventListener(v8::Handle<v8::Object> that,
4373 Handle<Value> data) {
4374 ON_BAILOUT("v8::Debug::SetDebugEventListener()", return false);
4375 ENTER_V8;
4376 i::Debugger::SetEventListener(Utils::OpenHandle(*that),
4377 Utils::OpenHandle(*data));
4378 return true;
4379}
4380
4381
4382void Debug::DebugBreak() {
4383 if (!i::V8::IsRunning()) return;
4384 i::StackGuard::DebugBreak();
4385}
4386
4387
Ben Murdochf87a2032010-10-22 12:50:53 +01004388void Debug::CancelDebugBreak() {
4389 i::StackGuard::Continue(i::DEBUGBREAK);
4390}
4391
4392
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004393void Debug::DebugBreakForCommand(ClientData* data) {
4394 if (!i::V8::IsRunning()) return;
4395 i::Debugger::EnqueueDebugCommand(data);
4396}
4397
4398
Steve Blocka7e24c12009-10-30 11:49:00 +00004399static v8::Debug::MessageHandler message_handler = NULL;
4400
4401static void MessageHandlerWrapper(const v8::Debug::Message& message) {
4402 if (message_handler) {
4403 v8::String::Value json(message.GetJSON());
4404 message_handler(*json, json.length(), message.GetClientData());
4405 }
4406}
4407
4408
4409void Debug::SetMessageHandler(v8::Debug::MessageHandler handler,
4410 bool message_handler_thread) {
4411 EnsureInitialized("v8::Debug::SetMessageHandler");
4412 ENTER_V8;
4413 // Message handler thread not supported any more. Parameter temporally left in
4414 // the API for client compatability reasons.
4415 CHECK(!message_handler_thread);
4416
4417 // TODO(sgjesse) support the old message handler API through a simple wrapper.
4418 message_handler = handler;
4419 if (message_handler != NULL) {
4420 i::Debugger::SetMessageHandler(MessageHandlerWrapper);
4421 } else {
4422 i::Debugger::SetMessageHandler(NULL);
4423 }
4424}
4425
4426
4427void Debug::SetMessageHandler2(v8::Debug::MessageHandler2 handler) {
4428 EnsureInitialized("v8::Debug::SetMessageHandler");
4429 ENTER_V8;
Steve Blocka7e24c12009-10-30 11:49:00 +00004430 i::Debugger::SetMessageHandler(handler);
4431}
4432
4433
4434void Debug::SendCommand(const uint16_t* command, int length,
4435 ClientData* client_data) {
4436 if (!i::V8::IsRunning()) return;
4437 i::Debugger::ProcessCommand(i::Vector<const uint16_t>(command, length),
4438 client_data);
4439}
4440
4441
4442void Debug::SetHostDispatchHandler(HostDispatchHandler handler,
4443 int period) {
4444 EnsureInitialized("v8::Debug::SetHostDispatchHandler");
4445 ENTER_V8;
4446 i::Debugger::SetHostDispatchHandler(handler, period);
4447}
4448
4449
Steve Blockd0582a62009-12-15 09:54:21 +00004450void Debug::SetDebugMessageDispatchHandler(
Leon Clarkee46be812010-01-19 14:06:41 +00004451 DebugMessageDispatchHandler handler, bool provide_locker) {
Steve Blockd0582a62009-12-15 09:54:21 +00004452 EnsureInitialized("v8::Debug::SetDebugMessageDispatchHandler");
4453 ENTER_V8;
Leon Clarkee46be812010-01-19 14:06:41 +00004454 i::Debugger::SetDebugMessageDispatchHandler(handler, provide_locker);
Steve Blockd0582a62009-12-15 09:54:21 +00004455}
4456
4457
Steve Blocka7e24c12009-10-30 11:49:00 +00004458Local<Value> Debug::Call(v8::Handle<v8::Function> fun,
4459 v8::Handle<v8::Value> data) {
4460 if (!i::V8::IsRunning()) return Local<Value>();
4461 ON_BAILOUT("v8::Debug::Call()", return Local<Value>());
4462 ENTER_V8;
4463 i::Handle<i::Object> result;
4464 EXCEPTION_PREAMBLE();
4465 if (data.IsEmpty()) {
4466 result = i::Debugger::Call(Utils::OpenHandle(*fun),
4467 i::Factory::undefined_value(),
4468 &has_pending_exception);
4469 } else {
4470 result = i::Debugger::Call(Utils::OpenHandle(*fun),
4471 Utils::OpenHandle(*data),
4472 &has_pending_exception);
4473 }
4474 EXCEPTION_BAILOUT_CHECK(Local<Value>());
4475 return Utils::ToLocal(result);
4476}
4477
4478
4479Local<Value> Debug::GetMirror(v8::Handle<v8::Value> obj) {
4480 if (!i::V8::IsRunning()) return Local<Value>();
4481 ON_BAILOUT("v8::Debug::GetMirror()", return Local<Value>());
4482 ENTER_V8;
4483 v8::HandleScope scope;
4484 i::Debug::Load();
4485 i::Handle<i::JSObject> debug(i::Debug::debug_context()->global());
4486 i::Handle<i::String> name = i::Factory::LookupAsciiSymbol("MakeMirror");
4487 i::Handle<i::Object> fun_obj = i::GetProperty(debug, name);
4488 i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(fun_obj);
4489 v8::Handle<v8::Function> v8_fun = Utils::ToLocal(fun);
4490 const int kArgc = 1;
4491 v8::Handle<v8::Value> argv[kArgc] = { obj };
4492 EXCEPTION_PREAMBLE();
4493 v8::Handle<v8::Value> result = v8_fun->Call(Utils::ToLocal(debug),
4494 kArgc,
4495 argv);
4496 EXCEPTION_BAILOUT_CHECK(Local<Value>());
4497 return scope.Close(result);
4498}
4499
4500
Leon Clarkee46be812010-01-19 14:06:41 +00004501bool Debug::EnableAgent(const char* name, int port, bool wait_for_connection) {
4502 return i::Debugger::StartAgent(name, port, wait_for_connection);
Steve Blocka7e24c12009-10-30 11:49:00 +00004503}
Leon Clarkee46be812010-01-19 14:06:41 +00004504
4505void Debug::ProcessDebugMessages() {
4506 i::Execution::ProcessDebugMesssages(true);
4507}
4508
Steve Block6ded16b2010-05-10 14:33:55 +01004509Local<Context> Debug::GetDebugContext() {
4510 EnsureInitialized("v8::Debug::GetDebugContext()");
4511 ENTER_V8;
4512 return Utils::ToLocal(i::Debugger::GetDebugContext());
4513}
4514
Steve Blocka7e24c12009-10-30 11:49:00 +00004515#endif // ENABLE_DEBUGGER_SUPPORT
4516
Steve Block6ded16b2010-05-10 14:33:55 +01004517
4518#ifdef ENABLE_LOGGING_AND_PROFILING
4519
4520Handle<String> CpuProfileNode::GetFunctionName() const {
4521 IsDeadCheck("v8::CpuProfileNode::GetFunctionName");
4522 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
4523 const i::CodeEntry* entry = node->entry();
4524 if (!entry->has_name_prefix()) {
4525 return Handle<String>(ToApi<String>(
4526 i::Factory::LookupAsciiSymbol(entry->name())));
4527 } else {
4528 return Handle<String>(ToApi<String>(i::Factory::NewConsString(
4529 i::Factory::LookupAsciiSymbol(entry->name_prefix()),
4530 i::Factory::LookupAsciiSymbol(entry->name()))));
4531 }
4532}
4533
4534
4535Handle<String> CpuProfileNode::GetScriptResourceName() const {
4536 IsDeadCheck("v8::CpuProfileNode::GetScriptResourceName");
4537 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
4538 return Handle<String>(ToApi<String>(i::Factory::LookupAsciiSymbol(
4539 node->entry()->resource_name())));
4540}
4541
4542
4543int CpuProfileNode::GetLineNumber() const {
4544 IsDeadCheck("v8::CpuProfileNode::GetLineNumber");
4545 return reinterpret_cast<const i::ProfileNode*>(this)->entry()->line_number();
4546}
4547
4548
4549double CpuProfileNode::GetTotalTime() const {
4550 IsDeadCheck("v8::CpuProfileNode::GetTotalTime");
4551 return reinterpret_cast<const i::ProfileNode*>(this)->GetTotalMillis();
4552}
4553
4554
4555double CpuProfileNode::GetSelfTime() const {
4556 IsDeadCheck("v8::CpuProfileNode::GetSelfTime");
4557 return reinterpret_cast<const i::ProfileNode*>(this)->GetSelfMillis();
4558}
4559
4560
4561double CpuProfileNode::GetTotalSamplesCount() const {
4562 IsDeadCheck("v8::CpuProfileNode::GetTotalSamplesCount");
4563 return reinterpret_cast<const i::ProfileNode*>(this)->total_ticks();
4564}
4565
4566
4567double CpuProfileNode::GetSelfSamplesCount() const {
4568 IsDeadCheck("v8::CpuProfileNode::GetSelfSamplesCount");
4569 return reinterpret_cast<const i::ProfileNode*>(this)->self_ticks();
4570}
4571
4572
4573unsigned CpuProfileNode::GetCallUid() const {
4574 IsDeadCheck("v8::CpuProfileNode::GetCallUid");
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004575 return reinterpret_cast<const i::ProfileNode*>(this)->entry()->GetCallUid();
Steve Block6ded16b2010-05-10 14:33:55 +01004576}
4577
4578
4579int CpuProfileNode::GetChildrenCount() const {
4580 IsDeadCheck("v8::CpuProfileNode::GetChildrenCount");
4581 return reinterpret_cast<const i::ProfileNode*>(this)->children()->length();
4582}
4583
4584
4585const CpuProfileNode* CpuProfileNode::GetChild(int index) const {
4586 IsDeadCheck("v8::CpuProfileNode::GetChild");
4587 const i::ProfileNode* child =
4588 reinterpret_cast<const i::ProfileNode*>(this)->children()->at(index);
4589 return reinterpret_cast<const CpuProfileNode*>(child);
4590}
4591
4592
4593unsigned CpuProfile::GetUid() const {
4594 IsDeadCheck("v8::CpuProfile::GetUid");
4595 return reinterpret_cast<const i::CpuProfile*>(this)->uid();
4596}
4597
4598
4599Handle<String> CpuProfile::GetTitle() const {
4600 IsDeadCheck("v8::CpuProfile::GetTitle");
4601 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
4602 return Handle<String>(ToApi<String>(i::Factory::LookupAsciiSymbol(
4603 profile->title())));
4604}
4605
4606
4607const CpuProfileNode* CpuProfile::GetBottomUpRoot() const {
4608 IsDeadCheck("v8::CpuProfile::GetBottomUpRoot");
4609 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
4610 return reinterpret_cast<const CpuProfileNode*>(profile->bottom_up()->root());
4611}
4612
4613
4614const CpuProfileNode* CpuProfile::GetTopDownRoot() const {
4615 IsDeadCheck("v8::CpuProfile::GetTopDownRoot");
4616 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
4617 return reinterpret_cast<const CpuProfileNode*>(profile->top_down()->root());
4618}
4619
4620
4621int CpuProfiler::GetProfilesCount() {
4622 IsDeadCheck("v8::CpuProfiler::GetProfilesCount");
4623 return i::CpuProfiler::GetProfilesCount();
4624}
4625
4626
Leon Clarkef7060e22010-06-03 12:02:55 +01004627const CpuProfile* CpuProfiler::GetProfile(int index,
4628 Handle<Value> security_token) {
Steve Block6ded16b2010-05-10 14:33:55 +01004629 IsDeadCheck("v8::CpuProfiler::GetProfile");
Leon Clarkef7060e22010-06-03 12:02:55 +01004630 return reinterpret_cast<const CpuProfile*>(
4631 i::CpuProfiler::GetProfile(
4632 security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
4633 index));
Steve Block6ded16b2010-05-10 14:33:55 +01004634}
4635
4636
Leon Clarkef7060e22010-06-03 12:02:55 +01004637const CpuProfile* CpuProfiler::FindProfile(unsigned uid,
4638 Handle<Value> security_token) {
Steve Block6ded16b2010-05-10 14:33:55 +01004639 IsDeadCheck("v8::CpuProfiler::FindProfile");
Leon Clarkef7060e22010-06-03 12:02:55 +01004640 return reinterpret_cast<const CpuProfile*>(
4641 i::CpuProfiler::FindProfile(
4642 security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
4643 uid));
Steve Block6ded16b2010-05-10 14:33:55 +01004644}
4645
4646
4647void CpuProfiler::StartProfiling(Handle<String> title) {
4648 IsDeadCheck("v8::CpuProfiler::StartProfiling");
4649 i::CpuProfiler::StartProfiling(*Utils::OpenHandle(*title));
4650}
4651
4652
Leon Clarkef7060e22010-06-03 12:02:55 +01004653const CpuProfile* CpuProfiler::StopProfiling(Handle<String> title,
4654 Handle<Value> security_token) {
Steve Block6ded16b2010-05-10 14:33:55 +01004655 IsDeadCheck("v8::CpuProfiler::StopProfiling");
4656 return reinterpret_cast<const CpuProfile*>(
Leon Clarkef7060e22010-06-03 12:02:55 +01004657 i::CpuProfiler::StopProfiling(
4658 security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
4659 *Utils::OpenHandle(*title)));
Steve Block6ded16b2010-05-10 14:33:55 +01004660}
4661
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004662
Iain Merrick75681382010-08-19 15:07:18 +01004663static i::HeapGraphEdge* ToInternal(const HeapGraphEdge* edge) {
4664 return const_cast<i::HeapGraphEdge*>(
4665 reinterpret_cast<const i::HeapGraphEdge*>(edge));
4666}
4667
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004668HeapGraphEdge::Type HeapGraphEdge::GetType() const {
4669 IsDeadCheck("v8::HeapGraphEdge::GetType");
Iain Merrick75681382010-08-19 15:07:18 +01004670 return static_cast<HeapGraphEdge::Type>(ToInternal(this)->type());
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004671}
4672
4673
4674Handle<Value> HeapGraphEdge::GetName() const {
4675 IsDeadCheck("v8::HeapGraphEdge::GetName");
Iain Merrick75681382010-08-19 15:07:18 +01004676 i::HeapGraphEdge* edge = ToInternal(this);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004677 switch (edge->type()) {
Iain Merrick75681382010-08-19 15:07:18 +01004678 case i::HeapGraphEdge::kContextVariable:
4679 case i::HeapGraphEdge::kInternal:
4680 case i::HeapGraphEdge::kProperty:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08004681 case i::HeapGraphEdge::kShortcut:
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004682 return Handle<String>(ToApi<String>(i::Factory::LookupAsciiSymbol(
4683 edge->name())));
Iain Merrick75681382010-08-19 15:07:18 +01004684 case i::HeapGraphEdge::kElement:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08004685 case i::HeapGraphEdge::kHidden:
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004686 return Handle<Number>(ToApi<Number>(i::Factory::NewNumberFromInt(
4687 edge->index())));
4688 default: UNREACHABLE();
4689 }
4690 return ImplementationUtilities::Undefined();
4691}
4692
4693
4694const HeapGraphNode* HeapGraphEdge::GetFromNode() const {
4695 IsDeadCheck("v8::HeapGraphEdge::GetFromNode");
Iain Merrick75681382010-08-19 15:07:18 +01004696 const i::HeapEntry* from = ToInternal(this)->From();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004697 return reinterpret_cast<const HeapGraphNode*>(from);
4698}
4699
4700
4701const HeapGraphNode* HeapGraphEdge::GetToNode() const {
4702 IsDeadCheck("v8::HeapGraphEdge::GetToNode");
Iain Merrick75681382010-08-19 15:07:18 +01004703 const i::HeapEntry* to = ToInternal(this)->to();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004704 return reinterpret_cast<const HeapGraphNode*>(to);
4705}
4706
4707
Iain Merrick75681382010-08-19 15:07:18 +01004708static i::HeapGraphPath* ToInternal(const HeapGraphPath* path) {
4709 return const_cast<i::HeapGraphPath*>(
4710 reinterpret_cast<const i::HeapGraphPath*>(path));
4711}
4712
4713
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004714int HeapGraphPath::GetEdgesCount() const {
Iain Merrick75681382010-08-19 15:07:18 +01004715 return ToInternal(this)->path()->length();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004716}
4717
4718
4719const HeapGraphEdge* HeapGraphPath::GetEdge(int index) const {
4720 return reinterpret_cast<const HeapGraphEdge*>(
Iain Merrick75681382010-08-19 15:07:18 +01004721 ToInternal(this)->path()->at(index));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004722}
4723
4724
4725const HeapGraphNode* HeapGraphPath::GetFromNode() const {
4726 return GetEdgesCount() > 0 ? GetEdge(0)->GetFromNode() : NULL;
4727}
4728
4729
4730const HeapGraphNode* HeapGraphPath::GetToNode() const {
4731 const int count = GetEdgesCount();
4732 return count > 0 ? GetEdge(count - 1)->GetToNode() : NULL;
4733}
4734
4735
Iain Merrick75681382010-08-19 15:07:18 +01004736static i::HeapEntry* ToInternal(const HeapGraphNode* entry) {
4737 return const_cast<i::HeapEntry*>(
4738 reinterpret_cast<const i::HeapEntry*>(entry));
4739}
4740
4741
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004742HeapGraphNode::Type HeapGraphNode::GetType() const {
4743 IsDeadCheck("v8::HeapGraphNode::GetType");
Iain Merrick75681382010-08-19 15:07:18 +01004744 return static_cast<HeapGraphNode::Type>(ToInternal(this)->type());
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004745}
4746
4747
4748Handle<String> HeapGraphNode::GetName() const {
4749 IsDeadCheck("v8::HeapGraphNode::GetName");
4750 return Handle<String>(ToApi<String>(i::Factory::LookupAsciiSymbol(
Iain Merrick75681382010-08-19 15:07:18 +01004751 ToInternal(this)->name())));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004752}
4753
4754
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004755uint64_t HeapGraphNode::GetId() const {
4756 IsDeadCheck("v8::HeapGraphNode::GetId");
Steve Block791712a2010-08-27 10:21:07 +01004757 ASSERT(ToInternal(this)->snapshot()->type() != i::HeapSnapshot::kAggregated);
Iain Merrick75681382010-08-19 15:07:18 +01004758 return ToInternal(this)->id();
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004759}
4760
4761
Steve Block791712a2010-08-27 10:21:07 +01004762int HeapGraphNode::GetInstancesCount() const {
4763 IsDeadCheck("v8::HeapGraphNode::GetInstancesCount");
4764 ASSERT(ToInternal(this)->snapshot()->type() == i::HeapSnapshot::kAggregated);
4765 return static_cast<int>(ToInternal(this)->id());
4766}
4767
4768
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004769int HeapGraphNode::GetSelfSize() const {
4770 IsDeadCheck("v8::HeapGraphNode::GetSelfSize");
Iain Merrick75681382010-08-19 15:07:18 +01004771 return ToInternal(this)->self_size();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004772}
4773
4774
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08004775int HeapGraphNode::GetRetainedSize(bool exact) const {
Iain Merrick75681382010-08-19 15:07:18 +01004776 IsDeadCheck("v8::HeapSnapshot::GetRetainedSize");
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08004777 return ToInternal(this)->RetainedSize(exact);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004778}
4779
4780
4781int HeapGraphNode::GetChildrenCount() const {
4782 IsDeadCheck("v8::HeapSnapshot::GetChildrenCount");
Iain Merrick75681382010-08-19 15:07:18 +01004783 return ToInternal(this)->children().length();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004784}
4785
4786
4787const HeapGraphEdge* HeapGraphNode::GetChild(int index) const {
4788 IsDeadCheck("v8::HeapSnapshot::GetChild");
4789 return reinterpret_cast<const HeapGraphEdge*>(
Iain Merrick75681382010-08-19 15:07:18 +01004790 &ToInternal(this)->children()[index]);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004791}
4792
4793
4794int HeapGraphNode::GetRetainersCount() const {
4795 IsDeadCheck("v8::HeapSnapshot::GetRetainersCount");
Iain Merrick75681382010-08-19 15:07:18 +01004796 return ToInternal(this)->retainers().length();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004797}
4798
4799
4800const HeapGraphEdge* HeapGraphNode::GetRetainer(int index) const {
4801 IsDeadCheck("v8::HeapSnapshot::GetRetainer");
4802 return reinterpret_cast<const HeapGraphEdge*>(
Iain Merrick75681382010-08-19 15:07:18 +01004803 ToInternal(this)->retainers()[index]);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004804}
4805
4806
4807int HeapGraphNode::GetRetainingPathsCount() const {
4808 IsDeadCheck("v8::HeapSnapshot::GetRetainingPathsCount");
Iain Merrick75681382010-08-19 15:07:18 +01004809 return ToInternal(this)->GetRetainingPaths()->length();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004810}
4811
4812
4813const HeapGraphPath* HeapGraphNode::GetRetainingPath(int index) const {
4814 IsDeadCheck("v8::HeapSnapshot::GetRetainingPath");
4815 return reinterpret_cast<const HeapGraphPath*>(
Iain Merrick75681382010-08-19 15:07:18 +01004816 ToInternal(this)->GetRetainingPaths()->at(index));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004817}
4818
4819
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08004820const HeapGraphNode* HeapGraphNode::GetDominatorNode() const {
4821 IsDeadCheck("v8::HeapSnapshot::GetDominatorNode");
4822 return reinterpret_cast<const HeapGraphNode*>(ToInternal(this)->dominator());
4823}
4824
4825
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004826const HeapGraphNode* HeapSnapshotsDiff::GetAdditionsRoot() const {
4827 IsDeadCheck("v8::HeapSnapshotsDiff::GetAdditionsRoot");
Iain Merrick75681382010-08-19 15:07:18 +01004828 i::HeapSnapshotsDiff* diff =
4829 const_cast<i::HeapSnapshotsDiff*>(
4830 reinterpret_cast<const i::HeapSnapshotsDiff*>(this));
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004831 return reinterpret_cast<const HeapGraphNode*>(diff->additions_root());
4832}
4833
4834
4835const HeapGraphNode* HeapSnapshotsDiff::GetDeletionsRoot() const {
4836 IsDeadCheck("v8::HeapSnapshotsDiff::GetDeletionsRoot");
Iain Merrick75681382010-08-19 15:07:18 +01004837 i::HeapSnapshotsDiff* diff =
4838 const_cast<i::HeapSnapshotsDiff*>(
4839 reinterpret_cast<const i::HeapSnapshotsDiff*>(this));
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004840 return reinterpret_cast<const HeapGraphNode*>(diff->deletions_root());
4841}
4842
4843
Iain Merrick75681382010-08-19 15:07:18 +01004844static i::HeapSnapshot* ToInternal(const HeapSnapshot* snapshot) {
4845 return const_cast<i::HeapSnapshot*>(
4846 reinterpret_cast<const i::HeapSnapshot*>(snapshot));
4847}
4848
4849
Steve Block791712a2010-08-27 10:21:07 +01004850HeapSnapshot::Type HeapSnapshot::GetType() const {
4851 IsDeadCheck("v8::HeapSnapshot::GetType");
4852 return static_cast<HeapSnapshot::Type>(ToInternal(this)->type());
4853}
4854
4855
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004856unsigned HeapSnapshot::GetUid() const {
4857 IsDeadCheck("v8::HeapSnapshot::GetUid");
Iain Merrick75681382010-08-19 15:07:18 +01004858 return ToInternal(this)->uid();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004859}
4860
4861
4862Handle<String> HeapSnapshot::GetTitle() const {
4863 IsDeadCheck("v8::HeapSnapshot::GetTitle");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004864 return Handle<String>(ToApi<String>(i::Factory::LookupAsciiSymbol(
Iain Merrick75681382010-08-19 15:07:18 +01004865 ToInternal(this)->title())));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004866}
4867
4868
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004869const HeapGraphNode* HeapSnapshot::GetRoot() const {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004870 IsDeadCheck("v8::HeapSnapshot::GetHead");
Iain Merrick75681382010-08-19 15:07:18 +01004871 return reinterpret_cast<const HeapGraphNode*>(ToInternal(this)->root());
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004872}
4873
4874
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004875const HeapSnapshotsDiff* HeapSnapshot::CompareWith(
4876 const HeapSnapshot* snapshot) const {
4877 IsDeadCheck("v8::HeapSnapshot::CompareWith");
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004878 return reinterpret_cast<const HeapSnapshotsDiff*>(
Iain Merrick75681382010-08-19 15:07:18 +01004879 ToInternal(this)->CompareWith(ToInternal(snapshot)));
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004880}
4881
4882
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004883void HeapSnapshot::Serialize(OutputStream* stream,
4884 HeapSnapshot::SerializationFormat format) const {
4885 IsDeadCheck("v8::HeapSnapshot::Serialize");
4886 ApiCheck(format == kJSON,
4887 "v8::HeapSnapshot::Serialize",
4888 "Unknown serialization format");
4889 ApiCheck(stream->GetOutputEncoding() == OutputStream::kAscii,
4890 "v8::HeapSnapshot::Serialize",
4891 "Unsupported output encoding");
4892 ApiCheck(stream->GetChunkSize() > 0,
4893 "v8::HeapSnapshot::Serialize",
4894 "Invalid stream chunk size");
4895 i::HeapSnapshotJSONSerializer serializer(ToInternal(this));
4896 serializer.Serialize(stream);
4897}
4898
4899
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004900int HeapProfiler::GetSnapshotsCount() {
4901 IsDeadCheck("v8::HeapProfiler::GetSnapshotsCount");
4902 return i::HeapProfiler::GetSnapshotsCount();
4903}
4904
4905
4906const HeapSnapshot* HeapProfiler::GetSnapshot(int index) {
4907 IsDeadCheck("v8::HeapProfiler::GetSnapshot");
4908 return reinterpret_cast<const HeapSnapshot*>(
4909 i::HeapProfiler::GetSnapshot(index));
4910}
4911
4912
4913const HeapSnapshot* HeapProfiler::FindSnapshot(unsigned uid) {
4914 IsDeadCheck("v8::HeapProfiler::FindSnapshot");
4915 return reinterpret_cast<const HeapSnapshot*>(
4916 i::HeapProfiler::FindSnapshot(uid));
4917}
4918
4919
Steve Block791712a2010-08-27 10:21:07 +01004920const HeapSnapshot* HeapProfiler::TakeSnapshot(Handle<String> title,
4921 HeapSnapshot::Type type) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004922 IsDeadCheck("v8::HeapProfiler::TakeSnapshot");
Steve Block791712a2010-08-27 10:21:07 +01004923 i::HeapSnapshot::Type internal_type = i::HeapSnapshot::kFull;
4924 switch (type) {
4925 case HeapSnapshot::kFull:
4926 internal_type = i::HeapSnapshot::kFull;
4927 break;
4928 case HeapSnapshot::kAggregated:
4929 internal_type = i::HeapSnapshot::kAggregated;
4930 break;
4931 default:
4932 UNREACHABLE();
4933 }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004934 return reinterpret_cast<const HeapSnapshot*>(
Steve Block791712a2010-08-27 10:21:07 +01004935 i::HeapProfiler::TakeSnapshot(*Utils::OpenHandle(*title), internal_type));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004936}
4937
Steve Block6ded16b2010-05-10 14:33:55 +01004938#endif // ENABLE_LOGGING_AND_PROFILING
4939
4940
Steve Blocka7e24c12009-10-30 11:49:00 +00004941namespace internal {
4942
4943
4944HandleScopeImplementer* HandleScopeImplementer::instance() {
4945 return &thread_local;
4946}
4947
4948
4949void HandleScopeImplementer::FreeThreadResources() {
4950 thread_local.Free();
4951}
4952
4953
4954char* HandleScopeImplementer::ArchiveThread(char* storage) {
4955 return thread_local.ArchiveThreadHelper(storage);
4956}
4957
4958
4959char* HandleScopeImplementer::ArchiveThreadHelper(char* storage) {
4960 v8::ImplementationUtilities::HandleScopeData* current =
4961 v8::ImplementationUtilities::CurrentHandleScope();
4962 handle_scope_data_ = *current;
4963 memcpy(storage, this, sizeof(*this));
4964
4965 ResetAfterArchive();
4966 current->Initialize();
4967
4968 return storage + ArchiveSpacePerThread();
4969}
4970
4971
4972int HandleScopeImplementer::ArchiveSpacePerThread() {
4973 return sizeof(thread_local);
4974}
4975
4976
4977char* HandleScopeImplementer::RestoreThread(char* storage) {
4978 return thread_local.RestoreThreadHelper(storage);
4979}
4980
4981
4982char* HandleScopeImplementer::RestoreThreadHelper(char* storage) {
4983 memcpy(this, storage, sizeof(*this));
4984 *v8::ImplementationUtilities::CurrentHandleScope() = handle_scope_data_;
4985 return storage + ArchiveSpacePerThread();
4986}
4987
4988
4989void HandleScopeImplementer::IterateThis(ObjectVisitor* v) {
4990 // Iterate over all handles in the blocks except for the last.
4991 for (int i = blocks()->length() - 2; i >= 0; --i) {
4992 Object** block = blocks()->at(i);
4993 v->VisitPointers(block, &block[kHandleBlockSize]);
4994 }
4995
4996 // Iterate over live handles in the last block (if any).
4997 if (!blocks()->is_empty()) {
4998 v->VisitPointers(blocks()->last(), handle_scope_data_.next);
4999 }
5000
5001 if (!saved_contexts_.is_empty()) {
5002 Object** start = reinterpret_cast<Object**>(&saved_contexts_.first());
5003 v->VisitPointers(start, start + saved_contexts_.length());
5004 }
5005}
5006
5007
5008void HandleScopeImplementer::Iterate(ObjectVisitor* v) {
5009 v8::ImplementationUtilities::HandleScopeData* current =
5010 v8::ImplementationUtilities::CurrentHandleScope();
5011 thread_local.handle_scope_data_ = *current;
5012 thread_local.IterateThis(v);
5013}
5014
5015
5016char* HandleScopeImplementer::Iterate(ObjectVisitor* v, char* storage) {
5017 HandleScopeImplementer* thread_local =
5018 reinterpret_cast<HandleScopeImplementer*>(storage);
5019 thread_local->IterateThis(v);
5020 return storage + ArchiveSpacePerThread();
5021}
5022
5023} } // namespace v8::internal