blob: 464ca54d2d4f89eaa71a9e87d62372562b643d51 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "api.h"
31#include "arguments.h"
32#include "bootstrapper.h"
33#include "compiler.h"
34#include "debug.h"
35#include "execution.h"
36#include "global-handles.h"
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010037#include "heap-profiler.h"
Steve Block6ded16b2010-05-10 14:33:55 +010038#include "messages.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000039#include "platform.h"
Steve Block6ded16b2010-05-10 14:33:55 +010040#include "profile-generator-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000041#include "serialize.h"
42#include "snapshot.h"
Steve Block6ded16b2010-05-10 14:33:55 +010043#include "top.h"
Steve Blockd0582a62009-12-15 09:54:21 +000044#include "utils.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000045#include "v8threads.h"
46#include "version.h"
47
Steve Block6ded16b2010-05-10 14:33:55 +010048#include "../include/v8-profiler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000049
50#define LOG_API(expr) LOG(ApiEntryCall(expr))
51
Leon Clarkef7060e22010-06-03 12:02:55 +010052#ifdef ENABLE_VMSTATE_TRACKING
Steve Blocka7e24c12009-10-30 11:49:00 +000053#define ENTER_V8 i::VMState __state__(i::OTHER)
54#define LEAVE_V8 i::VMState __state__(i::EXTERNAL)
55#else
56#define ENTER_V8 ((void) 0)
57#define LEAVE_V8 ((void) 0)
58#endif
59
60namespace v8 {
61
Leon Clarkef7060e22010-06-03 12:02:55 +010062#define ON_BAILOUT(location, code) \
63 if (IsDeadCheck(location) || v8::V8::IsExecutionTerminating()) { \
64 code; \
65 UNREACHABLE(); \
Steve Blocka7e24c12009-10-30 11:49:00 +000066 }
67
68
69#define EXCEPTION_PREAMBLE() \
70 thread_local.IncrementCallDepth(); \
71 ASSERT(!i::Top::external_caught_exception()); \
72 bool has_pending_exception = false
73
74
75#define EXCEPTION_BAILOUT_CHECK(value) \
76 do { \
77 thread_local.DecrementCallDepth(); \
78 if (has_pending_exception) { \
79 if (thread_local.CallDepthIsZero() && i::Top::is_out_of_memory()) { \
80 if (!thread_local.ignore_out_of_memory()) \
81 i::V8::FatalProcessOutOfMemory(NULL); \
82 } \
83 bool call_depth_is_zero = thread_local.CallDepthIsZero(); \
84 i::Top::OptionalRescheduleException(call_depth_is_zero); \
85 return value; \
86 } \
87 } while (false)
88
89
90#define API_ENTRY_CHECK(msg) \
91 do { \
92 if (v8::Locker::IsActive()) { \
93 ApiCheck(i::ThreadManager::IsLockedByCurrentThread(), \
94 msg, \
95 "Entering the V8 API without proper locking in place"); \
96 } \
97 } while (false)
98
99// --- 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 ---
100
101
102static i::HandleScopeImplementer thread_local;
103
104
105// --- E x c e p t i o n B e h a v i o r ---
106
107
108static FatalErrorCallback exception_behavior = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000109
110static void DefaultFatalErrorHandler(const char* location,
111 const char* message) {
112 ENTER_V8;
113 API_Fatal(location, message);
114}
115
116
117
118static FatalErrorCallback& GetFatalErrorHandler() {
119 if (exception_behavior == NULL) {
120 exception_behavior = DefaultFatalErrorHandler;
121 }
122 return exception_behavior;
123}
124
125
126
127// When V8 cannot allocated memory FatalProcessOutOfMemory is called.
128// The default fatal error handler is called and execution is stopped.
129void i::V8::FatalProcessOutOfMemory(const char* location) {
Steve Blockd0582a62009-12-15 09:54:21 +0000130 i::HeapStats heap_stats;
131 int start_marker;
132 heap_stats.start_marker = &start_marker;
133 int new_space_size;
134 heap_stats.new_space_size = &new_space_size;
135 int new_space_capacity;
136 heap_stats.new_space_capacity = &new_space_capacity;
137 int old_pointer_space_size;
138 heap_stats.old_pointer_space_size = &old_pointer_space_size;
139 int old_pointer_space_capacity;
140 heap_stats.old_pointer_space_capacity = &old_pointer_space_capacity;
141 int old_data_space_size;
142 heap_stats.old_data_space_size = &old_data_space_size;
143 int old_data_space_capacity;
144 heap_stats.old_data_space_capacity = &old_data_space_capacity;
145 int code_space_size;
146 heap_stats.code_space_size = &code_space_size;
147 int code_space_capacity;
148 heap_stats.code_space_capacity = &code_space_capacity;
149 int map_space_size;
150 heap_stats.map_space_size = &map_space_size;
151 int map_space_capacity;
152 heap_stats.map_space_capacity = &map_space_capacity;
153 int cell_space_size;
154 heap_stats.cell_space_size = &cell_space_size;
155 int cell_space_capacity;
156 heap_stats.cell_space_capacity = &cell_space_capacity;
157 int lo_space_size;
158 heap_stats.lo_space_size = &lo_space_size;
159 int global_handle_count;
160 heap_stats.global_handle_count = &global_handle_count;
161 int weak_global_handle_count;
162 heap_stats.weak_global_handle_count = &weak_global_handle_count;
163 int pending_global_handle_count;
164 heap_stats.pending_global_handle_count = &pending_global_handle_count;
165 int near_death_global_handle_count;
166 heap_stats.near_death_global_handle_count = &near_death_global_handle_count;
167 int destroyed_global_handle_count;
168 heap_stats.destroyed_global_handle_count = &destroyed_global_handle_count;
169 int end_marker;
170 heap_stats.end_marker = &end_marker;
171 i::Heap::RecordStats(&heap_stats);
Steve Blocka7e24c12009-10-30 11:49:00 +0000172 i::V8::SetFatalError();
173 FatalErrorCallback callback = GetFatalErrorHandler();
174 {
175 LEAVE_V8;
176 callback(location, "Allocation failed - process out of memory");
177 }
178 // If the callback returns, we stop execution.
179 UNREACHABLE();
180}
181
182
183void V8::SetFatalErrorHandler(FatalErrorCallback that) {
184 exception_behavior = that;
185}
186
187
188bool Utils::ReportApiFailure(const char* location, const char* message) {
189 FatalErrorCallback callback = GetFatalErrorHandler();
190 callback(location, message);
191 i::V8::SetFatalError();
192 return false;
193}
194
195
196bool V8::IsDead() {
197 return i::V8::IsDead();
198}
199
200
201static inline bool ApiCheck(bool condition,
202 const char* location,
203 const char* message) {
204 return condition ? true : Utils::ReportApiFailure(location, message);
205}
206
207
208static bool ReportV8Dead(const char* location) {
209 FatalErrorCallback callback = GetFatalErrorHandler();
210 callback(location, "V8 is no longer usable");
211 return true;
212}
213
214
215static bool ReportEmptyHandle(const char* location) {
216 FatalErrorCallback callback = GetFatalErrorHandler();
217 callback(location, "Reading from empty handle");
218 return true;
219}
220
221
222/**
223 * IsDeadCheck checks that the vm is usable. If, for instance, the vm has been
224 * out of memory at some point this check will fail. It should be called on
225 * entry to all methods that touch anything in the heap, except destructors
226 * which you sometimes can't avoid calling after the vm has crashed. Functions
227 * that call EnsureInitialized or ON_BAILOUT don't have to also call
228 * IsDeadCheck. ON_BAILOUT has the advantage over EnsureInitialized that you
229 * can arrange to return if the VM is dead. This is needed to ensure that no VM
230 * heap allocations are attempted on a dead VM. EnsureInitialized has the
231 * advantage over ON_BAILOUT that it actually initializes the VM if this has not
232 * yet been done.
233 */
234static inline bool IsDeadCheck(const char* location) {
235 return !i::V8::IsRunning()
236 && i::V8::IsDead() ? ReportV8Dead(location) : false;
237}
238
239
240static inline bool EmptyCheck(const char* location, v8::Handle<v8::Data> obj) {
241 return obj.IsEmpty() ? ReportEmptyHandle(location) : false;
242}
243
244
245static inline bool EmptyCheck(const char* location, const v8::Data* obj) {
246 return (obj == 0) ? ReportEmptyHandle(location) : false;
247}
248
249// --- S t a t i c s ---
250
251
252static i::StringInputBuffer write_input_buffer;
253
254
255static inline bool EnsureInitialized(const char* location) {
256 if (i::V8::IsRunning()) {
257 return true;
258 }
259 if (IsDeadCheck(location)) {
260 return false;
261 }
262 return ApiCheck(v8::V8::Initialize(), location, "Error initializing V8");
263}
264
265
266ImplementationUtilities::HandleScopeData*
267 ImplementationUtilities::CurrentHandleScope() {
268 return &i::HandleScope::current_;
269}
270
271
272#ifdef DEBUG
273void ImplementationUtilities::ZapHandleRange(i::Object** begin,
274 i::Object** end) {
275 i::HandleScope::ZapRange(begin, end);
276}
277#endif
278
279
280v8::Handle<v8::Primitive> ImplementationUtilities::Undefined() {
281 if (!EnsureInitialized("v8::Undefined()")) return v8::Handle<v8::Primitive>();
282 return v8::Handle<Primitive>(ToApi<Primitive>(i::Factory::undefined_value()));
283}
284
285
286v8::Handle<v8::Primitive> ImplementationUtilities::Null() {
287 if (!EnsureInitialized("v8::Null()")) return v8::Handle<v8::Primitive>();
288 return v8::Handle<Primitive>(ToApi<Primitive>(i::Factory::null_value()));
289}
290
291
292v8::Handle<v8::Boolean> ImplementationUtilities::True() {
293 if (!EnsureInitialized("v8::True()")) return v8::Handle<v8::Boolean>();
294 return v8::Handle<v8::Boolean>(ToApi<Boolean>(i::Factory::true_value()));
295}
296
297
298v8::Handle<v8::Boolean> ImplementationUtilities::False() {
299 if (!EnsureInitialized("v8::False()")) return v8::Handle<v8::Boolean>();
300 return v8::Handle<v8::Boolean>(ToApi<Boolean>(i::Factory::false_value()));
301}
302
303
304void V8::SetFlagsFromString(const char* str, int length) {
305 i::FlagList::SetFlagsFromString(str, length);
306}
307
308
309void V8::SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags) {
310 i::FlagList::SetFlagsFromCommandLine(argc, argv, remove_flags);
311}
312
313
314v8::Handle<Value> ThrowException(v8::Handle<v8::Value> value) {
315 if (IsDeadCheck("v8::ThrowException()")) return v8::Handle<Value>();
316 ENTER_V8;
317 // If we're passed an empty handle, we throw an undefined exception
318 // to deal more gracefully with out of memory situations.
319 if (value.IsEmpty()) {
320 i::Top::ScheduleThrow(i::Heap::undefined_value());
321 } else {
322 i::Top::ScheduleThrow(*Utils::OpenHandle(*value));
323 }
324 return v8::Undefined();
325}
326
327
328RegisteredExtension* RegisteredExtension::first_extension_ = NULL;
329
330
331RegisteredExtension::RegisteredExtension(Extension* extension)
332 : extension_(extension), state_(UNVISITED) { }
333
334
335void RegisteredExtension::Register(RegisteredExtension* that) {
336 that->next_ = RegisteredExtension::first_extension_;
337 RegisteredExtension::first_extension_ = that;
338}
339
340
341void RegisterExtension(Extension* that) {
342 RegisteredExtension* extension = new RegisteredExtension(that);
343 RegisteredExtension::Register(extension);
344}
345
346
347Extension::Extension(const char* name,
348 const char* source,
349 int dep_count,
350 const char** deps)
351 : name_(name),
352 source_(source),
353 dep_count_(dep_count),
354 deps_(deps),
355 auto_enable_(false) { }
356
357
358v8::Handle<Primitive> Undefined() {
359 LOG_API("Undefined");
360 return ImplementationUtilities::Undefined();
361}
362
363
364v8::Handle<Primitive> Null() {
365 LOG_API("Null");
366 return ImplementationUtilities::Null();
367}
368
369
370v8::Handle<Boolean> True() {
371 LOG_API("True");
372 return ImplementationUtilities::True();
373}
374
375
376v8::Handle<Boolean> False() {
377 LOG_API("False");
378 return ImplementationUtilities::False();
379}
380
381
382ResourceConstraints::ResourceConstraints()
383 : max_young_space_size_(0),
384 max_old_space_size_(0),
385 stack_limit_(NULL) { }
386
387
388bool SetResourceConstraints(ResourceConstraints* constraints) {
Steve Block3ce2e202009-11-05 08:53:23 +0000389 int young_space_size = constraints->max_young_space_size();
Steve Blocka7e24c12009-10-30 11:49:00 +0000390 int old_gen_size = constraints->max_old_space_size();
Steve Block3ce2e202009-11-05 08:53:23 +0000391 if (young_space_size != 0 || old_gen_size != 0) {
392 bool result = i::Heap::ConfigureHeap(young_space_size / 2, old_gen_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000393 if (!result) return false;
394 }
395 if (constraints->stack_limit() != NULL) {
396 uintptr_t limit = reinterpret_cast<uintptr_t>(constraints->stack_limit());
397 i::StackGuard::SetStackLimit(limit);
398 }
399 return true;
400}
401
402
403i::Object** V8::GlobalizeReference(i::Object** obj) {
404 if (IsDeadCheck("V8::Persistent::New")) return NULL;
405 LOG_API("Persistent::New");
406 i::Handle<i::Object> result =
407 i::GlobalHandles::Create(*obj);
408 return result.location();
409}
410
411
412void V8::MakeWeak(i::Object** object, void* parameters,
413 WeakReferenceCallback callback) {
414 LOG_API("MakeWeak");
415 i::GlobalHandles::MakeWeak(object, parameters, callback);
416}
417
418
419void V8::ClearWeak(i::Object** obj) {
420 LOG_API("ClearWeak");
421 i::GlobalHandles::ClearWeakness(obj);
422}
423
424
425bool V8::IsGlobalNearDeath(i::Object** obj) {
426 LOG_API("IsGlobalNearDeath");
427 if (!i::V8::IsRunning()) return false;
428 return i::GlobalHandles::IsNearDeath(obj);
429}
430
431
432bool V8::IsGlobalWeak(i::Object** obj) {
433 LOG_API("IsGlobalWeak");
434 if (!i::V8::IsRunning()) return false;
435 return i::GlobalHandles::IsWeak(obj);
436}
437
438
439void V8::DisposeGlobal(i::Object** obj) {
440 LOG_API("DisposeGlobal");
441 if (!i::V8::IsRunning()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000442 i::GlobalHandles::Destroy(obj);
443}
444
445// --- H a n d l e s ---
446
447
448HandleScope::HandleScope() : is_closed_(false) {
449 API_ENTRY_CHECK("HandleScope::HandleScope");
450 i::HandleScope::Enter(&previous_);
451}
452
453
454HandleScope::~HandleScope() {
455 if (!is_closed_) {
456 i::HandleScope::Leave(&previous_);
457 }
458}
459
460
461int HandleScope::NumberOfHandles() {
462 return i::HandleScope::NumberOfHandles();
463}
464
465
466i::Object** v8::HandleScope::CreateHandle(i::Object* value) {
467 return i::HandleScope::CreateHandle(value);
468}
469
470
471void Context::Enter() {
472 if (IsDeadCheck("v8::Context::Enter()")) return;
473 ENTER_V8;
474 i::Handle<i::Context> env = Utils::OpenHandle(this);
475 thread_local.EnterContext(env);
476
477 thread_local.SaveContext(i::Top::context());
478 i::Top::set_context(*env);
479}
480
481
482void Context::Exit() {
483 if (!i::V8::IsRunning()) return;
484 if (!ApiCheck(thread_local.LeaveLastContext(),
485 "v8::Context::Exit()",
486 "Cannot exit non-entered context")) {
487 return;
488 }
489
490 // Content of 'last_context' could be NULL.
491 i::Context* last_context = thread_local.RestoreContext();
492 i::Top::set_context(last_context);
493}
494
495
Steve Blockd0582a62009-12-15 09:54:21 +0000496void Context::SetData(v8::Handle<String> data) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000497 if (IsDeadCheck("v8::Context::SetData()")) return;
498 ENTER_V8;
499 {
500 HandleScope scope;
501 i::Handle<i::Context> env = Utils::OpenHandle(this);
502 i::Handle<i::Object> raw_data = Utils::OpenHandle(*data);
503 ASSERT(env->IsGlobalContext());
504 if (env->IsGlobalContext()) {
505 env->set_data(*raw_data);
506 }
507 }
508}
509
510
511v8::Local<v8::Value> Context::GetData() {
512 if (IsDeadCheck("v8::Context::GetData()")) return v8::Local<Value>();
513 ENTER_V8;
514 i::Object* raw_result = NULL;
515 {
516 HandleScope scope;
517 i::Handle<i::Context> env = Utils::OpenHandle(this);
518 ASSERT(env->IsGlobalContext());
519 if (env->IsGlobalContext()) {
520 raw_result = env->data();
521 } else {
522 return Local<Value>();
523 }
524 }
525 i::Handle<i::Object> result(raw_result);
526 return Utils::ToLocal(result);
527}
528
529
530i::Object** v8::HandleScope::RawClose(i::Object** value) {
531 if (!ApiCheck(!is_closed_,
532 "v8::HandleScope::Close()",
533 "Local scope has already been closed")) {
534 return 0;
535 }
536 LOG_API("CloseHandleScope");
537
538 // Read the result before popping the handle block.
Steve Block6ded16b2010-05-10 14:33:55 +0100539 i::Object* result = NULL;
540 if (value != NULL) {
541 result = *value;
542 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000543 is_closed_ = true;
544 i::HandleScope::Leave(&previous_);
545
Steve Block6ded16b2010-05-10 14:33:55 +0100546 if (value == NULL) {
547 return NULL;
548 }
549
Steve Blocka7e24c12009-10-30 11:49:00 +0000550 // Allocate a new handle on the previous handle block.
551 i::Handle<i::Object> handle(result);
552 return handle.location();
553}
554
555
556// --- N e a n d e r ---
557
558
559// A constructor cannot easily return an error value, therefore it is necessary
560// to check for a dead VM with ON_BAILOUT before constructing any Neander
561// objects. To remind you about this there is no HandleScope in the
562// NeanderObject constructor. When you add one to the site calling the
563// constructor you should check that you ensured the VM was not dead first.
564NeanderObject::NeanderObject(int size) {
565 EnsureInitialized("v8::Nowhere");
566 ENTER_V8;
567 value_ = i::Factory::NewNeanderObject();
568 i::Handle<i::FixedArray> elements = i::Factory::NewFixedArray(size);
569 value_->set_elements(*elements);
570}
571
572
573int NeanderObject::size() {
574 return i::FixedArray::cast(value_->elements())->length();
575}
576
577
578NeanderArray::NeanderArray() : obj_(2) {
579 obj_.set(0, i::Smi::FromInt(0));
580}
581
582
583int NeanderArray::length() {
584 return i::Smi::cast(obj_.get(0))->value();
585}
586
587
588i::Object* NeanderArray::get(int offset) {
589 ASSERT(0 <= offset);
590 ASSERT(offset < length());
591 return obj_.get(offset + 1);
592}
593
594
595// This method cannot easily return an error value, therefore it is necessary
596// to check for a dead VM with ON_BAILOUT before calling it. To remind you
597// about this there is no HandleScope in this method. When you add one to the
598// site calling this method you should check that you ensured the VM was not
599// dead first.
600void NeanderArray::add(i::Handle<i::Object> value) {
601 int length = this->length();
602 int size = obj_.size();
603 if (length == size - 1) {
604 i::Handle<i::FixedArray> new_elms = i::Factory::NewFixedArray(2 * size);
605 for (int i = 0; i < length; i++)
606 new_elms->set(i + 1, get(i));
607 obj_.value()->set_elements(*new_elms);
608 }
609 obj_.set(length + 1, *value);
610 obj_.set(0, i::Smi::FromInt(length + 1));
611}
612
613
614void NeanderArray::set(int index, i::Object* value) {
615 if (index < 0 || index >= this->length()) return;
616 obj_.set(index + 1, value);
617}
618
619
620// --- T e m p l a t e ---
621
622
623static void InitializeTemplate(i::Handle<i::TemplateInfo> that, int type) {
624 that->set_tag(i::Smi::FromInt(type));
625}
626
627
628void Template::Set(v8::Handle<String> name, v8::Handle<Data> value,
629 v8::PropertyAttribute attribute) {
630 if (IsDeadCheck("v8::Template::SetProperty()")) return;
631 ENTER_V8;
632 HandleScope scope;
633 i::Handle<i::Object> list(Utils::OpenHandle(this)->property_list());
634 if (list->IsUndefined()) {
635 list = NeanderArray().value();
636 Utils::OpenHandle(this)->set_property_list(*list);
637 }
638 NeanderArray array(list);
639 array.add(Utils::OpenHandle(*name));
640 array.add(Utils::OpenHandle(*value));
641 array.add(Utils::OpenHandle(*v8::Integer::New(attribute)));
642}
643
644
645// --- F u n c t i o n T e m p l a t e ---
646static void InitializeFunctionTemplate(
647 i::Handle<i::FunctionTemplateInfo> info) {
648 info->set_tag(i::Smi::FromInt(Consts::FUNCTION_TEMPLATE));
649 info->set_flag(0);
650}
651
652
653Local<ObjectTemplate> FunctionTemplate::PrototypeTemplate() {
654 if (IsDeadCheck("v8::FunctionTemplate::PrototypeTemplate()")) {
655 return Local<ObjectTemplate>();
656 }
657 ENTER_V8;
658 i::Handle<i::Object> result(Utils::OpenHandle(this)->prototype_template());
659 if (result->IsUndefined()) {
660 result = Utils::OpenHandle(*ObjectTemplate::New());
661 Utils::OpenHandle(this)->set_prototype_template(*result);
662 }
663 return Local<ObjectTemplate>(ToApi<ObjectTemplate>(result));
664}
665
666
667void FunctionTemplate::Inherit(v8::Handle<FunctionTemplate> value) {
668 if (IsDeadCheck("v8::FunctionTemplate::Inherit()")) return;
669 ENTER_V8;
670 Utils::OpenHandle(this)->set_parent_template(*Utils::OpenHandle(*value));
671}
672
673
674// To distinguish the function templates, so that we can find them in the
675// function cache of the global context.
676static int next_serial_number = 0;
677
678
679Local<FunctionTemplate> FunctionTemplate::New(InvocationCallback callback,
680 v8::Handle<Value> data, v8::Handle<Signature> signature) {
681 EnsureInitialized("v8::FunctionTemplate::New()");
682 LOG_API("FunctionTemplate::New");
683 ENTER_V8;
684 i::Handle<i::Struct> struct_obj =
685 i::Factory::NewStruct(i::FUNCTION_TEMPLATE_INFO_TYPE);
686 i::Handle<i::FunctionTemplateInfo> obj =
687 i::Handle<i::FunctionTemplateInfo>::cast(struct_obj);
688 InitializeFunctionTemplate(obj);
689 obj->set_serial_number(i::Smi::FromInt(next_serial_number++));
690 if (callback != 0) {
691 if (data.IsEmpty()) data = v8::Undefined();
692 Utils::ToLocal(obj)->SetCallHandler(callback, data);
693 }
694 obj->set_undetectable(false);
695 obj->set_needs_access_check(false);
696
697 if (!signature.IsEmpty())
698 obj->set_signature(*Utils::OpenHandle(*signature));
699 return Utils::ToLocal(obj);
700}
701
702
703Local<Signature> Signature::New(Handle<FunctionTemplate> receiver,
704 int argc, Handle<FunctionTemplate> argv[]) {
705 EnsureInitialized("v8::Signature::New()");
706 LOG_API("Signature::New");
707 ENTER_V8;
708 i::Handle<i::Struct> struct_obj =
709 i::Factory::NewStruct(i::SIGNATURE_INFO_TYPE);
710 i::Handle<i::SignatureInfo> obj =
711 i::Handle<i::SignatureInfo>::cast(struct_obj);
712 if (!receiver.IsEmpty()) obj->set_receiver(*Utils::OpenHandle(*receiver));
713 if (argc > 0) {
714 i::Handle<i::FixedArray> args = i::Factory::NewFixedArray(argc);
715 for (int i = 0; i < argc; i++) {
716 if (!argv[i].IsEmpty())
717 args->set(i, *Utils::OpenHandle(*argv[i]));
718 }
719 obj->set_args(*args);
720 }
721 return Utils::ToLocal(obj);
722}
723
724
725Local<TypeSwitch> TypeSwitch::New(Handle<FunctionTemplate> type) {
726 Handle<FunctionTemplate> types[1] = { type };
727 return TypeSwitch::New(1, types);
728}
729
730
731Local<TypeSwitch> TypeSwitch::New(int argc, Handle<FunctionTemplate> types[]) {
732 EnsureInitialized("v8::TypeSwitch::New()");
733 LOG_API("TypeSwitch::New");
734 ENTER_V8;
735 i::Handle<i::FixedArray> vector = i::Factory::NewFixedArray(argc);
736 for (int i = 0; i < argc; i++)
737 vector->set(i, *Utils::OpenHandle(*types[i]));
738 i::Handle<i::Struct> struct_obj =
739 i::Factory::NewStruct(i::TYPE_SWITCH_INFO_TYPE);
740 i::Handle<i::TypeSwitchInfo> obj =
741 i::Handle<i::TypeSwitchInfo>::cast(struct_obj);
742 obj->set_types(*vector);
743 return Utils::ToLocal(obj);
744}
745
746
747int TypeSwitch::match(v8::Handle<Value> value) {
748 LOG_API("TypeSwitch::match");
749 i::Handle<i::Object> obj = Utils::OpenHandle(*value);
750 i::Handle<i::TypeSwitchInfo> info = Utils::OpenHandle(this);
751 i::FixedArray* types = i::FixedArray::cast(info->types());
752 for (int i = 0; i < types->length(); i++) {
753 if (obj->IsInstanceOf(i::FunctionTemplateInfo::cast(types->get(i))))
754 return i + 1;
755 }
756 return 0;
757}
758
759
760void FunctionTemplate::SetCallHandler(InvocationCallback callback,
761 v8::Handle<Value> data) {
762 if (IsDeadCheck("v8::FunctionTemplate::SetCallHandler()")) return;
763 ENTER_V8;
764 HandleScope scope;
765 i::Handle<i::Struct> struct_obj =
766 i::Factory::NewStruct(i::CALL_HANDLER_INFO_TYPE);
767 i::Handle<i::CallHandlerInfo> obj =
768 i::Handle<i::CallHandlerInfo>::cast(struct_obj);
769 obj->set_callback(*FromCData(callback));
770 if (data.IsEmpty()) data = v8::Undefined();
771 obj->set_data(*Utils::OpenHandle(*data));
772 Utils::OpenHandle(this)->set_call_code(*obj);
773}
774
775
Leon Clarkef7060e22010-06-03 12:02:55 +0100776static i::Handle<i::AccessorInfo> MakeAccessorInfo(
777 v8::Handle<String> name,
778 AccessorGetter getter,
779 AccessorSetter setter,
780 v8::Handle<Value> data,
781 v8::AccessControl settings,
782 v8::PropertyAttribute attributes) {
783 i::Handle<i::AccessorInfo> obj = i::Factory::NewAccessorInfo();
784 ASSERT(getter != NULL);
785 obj->set_getter(*FromCData(getter));
786 obj->set_setter(*FromCData(setter));
787 if (data.IsEmpty()) data = v8::Undefined();
788 obj->set_data(*Utils::OpenHandle(*data));
789 obj->set_name(*Utils::OpenHandle(*name));
790 if (settings & ALL_CAN_READ) obj->set_all_can_read(true);
791 if (settings & ALL_CAN_WRITE) obj->set_all_can_write(true);
792 if (settings & PROHIBITS_OVERWRITING) obj->set_prohibits_overwriting(true);
793 obj->set_property_attributes(static_cast<PropertyAttributes>(attributes));
794 return obj;
795}
796
797
Steve Blocka7e24c12009-10-30 11:49:00 +0000798void FunctionTemplate::AddInstancePropertyAccessor(
799 v8::Handle<String> name,
800 AccessorGetter getter,
801 AccessorSetter setter,
802 v8::Handle<Value> data,
803 v8::AccessControl settings,
804 v8::PropertyAttribute attributes) {
805 if (IsDeadCheck("v8::FunctionTemplate::AddInstancePropertyAccessor()")) {
806 return;
807 }
808 ENTER_V8;
809 HandleScope scope;
Steve Blocka7e24c12009-10-30 11:49:00 +0000810
Leon Clarkef7060e22010-06-03 12:02:55 +0100811 i::Handle<i::AccessorInfo> obj = MakeAccessorInfo(name,
812 getter, setter, data,
813 settings, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +0000814 i::Handle<i::Object> list(Utils::OpenHandle(this)->property_accessors());
815 if (list->IsUndefined()) {
816 list = NeanderArray().value();
817 Utils::OpenHandle(this)->set_property_accessors(*list);
818 }
819 NeanderArray array(list);
820 array.add(obj);
821}
822
823
824Local<ObjectTemplate> FunctionTemplate::InstanceTemplate() {
825 if (IsDeadCheck("v8::FunctionTemplate::InstanceTemplate()")
826 || EmptyCheck("v8::FunctionTemplate::InstanceTemplate()", this))
827 return Local<ObjectTemplate>();
828 ENTER_V8;
829 if (Utils::OpenHandle(this)->instance_template()->IsUndefined()) {
830 Local<ObjectTemplate> templ =
831 ObjectTemplate::New(v8::Handle<FunctionTemplate>(this));
832 Utils::OpenHandle(this)->set_instance_template(*Utils::OpenHandle(*templ));
833 }
834 i::Handle<i::ObjectTemplateInfo> result(i::ObjectTemplateInfo::cast(
835 Utils::OpenHandle(this)->instance_template()));
836 return Utils::ToLocal(result);
837}
838
839
840void FunctionTemplate::SetClassName(Handle<String> name) {
841 if (IsDeadCheck("v8::FunctionTemplate::SetClassName()")) return;
842 ENTER_V8;
843 Utils::OpenHandle(this)->set_class_name(*Utils::OpenHandle(*name));
844}
845
846
847void FunctionTemplate::SetHiddenPrototype(bool value) {
848 if (IsDeadCheck("v8::FunctionTemplate::SetHiddenPrototype()")) return;
849 ENTER_V8;
850 Utils::OpenHandle(this)->set_hidden_prototype(value);
851}
852
853
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100854void FunctionTemplate::SetNamedInstancePropertyHandler(
Steve Blocka7e24c12009-10-30 11:49:00 +0000855 NamedPropertyGetter getter,
856 NamedPropertySetter setter,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100857 NamedPropertyQuery query,
Steve Blocka7e24c12009-10-30 11:49:00 +0000858 NamedPropertyDeleter remover,
859 NamedPropertyEnumerator enumerator,
860 Handle<Value> data) {
861 if (IsDeadCheck("v8::FunctionTemplate::SetNamedInstancePropertyHandler()")) {
862 return;
863 }
864 ENTER_V8;
865 HandleScope scope;
866 i::Handle<i::Struct> struct_obj =
867 i::Factory::NewStruct(i::INTERCEPTOR_INFO_TYPE);
868 i::Handle<i::InterceptorInfo> obj =
869 i::Handle<i::InterceptorInfo>::cast(struct_obj);
870 if (getter != 0) obj->set_getter(*FromCData(getter));
871 if (setter != 0) obj->set_setter(*FromCData(setter));
872 if (query != 0) obj->set_query(*FromCData(query));
873 if (remover != 0) obj->set_deleter(*FromCData(remover));
874 if (enumerator != 0) obj->set_enumerator(*FromCData(enumerator));
875 if (data.IsEmpty()) data = v8::Undefined();
876 obj->set_data(*Utils::OpenHandle(*data));
877 Utils::OpenHandle(this)->set_named_property_handler(*obj);
878}
879
880
881void FunctionTemplate::SetIndexedInstancePropertyHandler(
882 IndexedPropertyGetter getter,
883 IndexedPropertySetter setter,
884 IndexedPropertyQuery query,
885 IndexedPropertyDeleter remover,
886 IndexedPropertyEnumerator enumerator,
887 Handle<Value> data) {
888 if (IsDeadCheck(
889 "v8::FunctionTemplate::SetIndexedInstancePropertyHandler()")) {
890 return;
891 }
892 ENTER_V8;
893 HandleScope scope;
894 i::Handle<i::Struct> struct_obj =
895 i::Factory::NewStruct(i::INTERCEPTOR_INFO_TYPE);
896 i::Handle<i::InterceptorInfo> obj =
897 i::Handle<i::InterceptorInfo>::cast(struct_obj);
898 if (getter != 0) obj->set_getter(*FromCData(getter));
899 if (setter != 0) obj->set_setter(*FromCData(setter));
900 if (query != 0) obj->set_query(*FromCData(query));
901 if (remover != 0) obj->set_deleter(*FromCData(remover));
902 if (enumerator != 0) obj->set_enumerator(*FromCData(enumerator));
903 if (data.IsEmpty()) data = v8::Undefined();
904 obj->set_data(*Utils::OpenHandle(*data));
905 Utils::OpenHandle(this)->set_indexed_property_handler(*obj);
906}
907
908
909void FunctionTemplate::SetInstanceCallAsFunctionHandler(
910 InvocationCallback callback,
911 Handle<Value> data) {
912 if (IsDeadCheck("v8::FunctionTemplate::SetInstanceCallAsFunctionHandler()")) {
913 return;
914 }
915 ENTER_V8;
916 HandleScope scope;
917 i::Handle<i::Struct> struct_obj =
918 i::Factory::NewStruct(i::CALL_HANDLER_INFO_TYPE);
919 i::Handle<i::CallHandlerInfo> obj =
920 i::Handle<i::CallHandlerInfo>::cast(struct_obj);
921 obj->set_callback(*FromCData(callback));
922 if (data.IsEmpty()) data = v8::Undefined();
923 obj->set_data(*Utils::OpenHandle(*data));
924 Utils::OpenHandle(this)->set_instance_call_handler(*obj);
925}
926
927
928// --- O b j e c t T e m p l a t e ---
929
930
931Local<ObjectTemplate> ObjectTemplate::New() {
932 return New(Local<FunctionTemplate>());
933}
934
935
936Local<ObjectTemplate> ObjectTemplate::New(
937 v8::Handle<FunctionTemplate> constructor) {
938 if (IsDeadCheck("v8::ObjectTemplate::New()")) return Local<ObjectTemplate>();
939 EnsureInitialized("v8::ObjectTemplate::New()");
940 LOG_API("ObjectTemplate::New");
941 ENTER_V8;
942 i::Handle<i::Struct> struct_obj =
943 i::Factory::NewStruct(i::OBJECT_TEMPLATE_INFO_TYPE);
944 i::Handle<i::ObjectTemplateInfo> obj =
945 i::Handle<i::ObjectTemplateInfo>::cast(struct_obj);
946 InitializeTemplate(obj, Consts::OBJECT_TEMPLATE);
947 if (!constructor.IsEmpty())
948 obj->set_constructor(*Utils::OpenHandle(*constructor));
949 obj->set_internal_field_count(i::Smi::FromInt(0));
950 return Utils::ToLocal(obj);
951}
952
953
954// Ensure that the object template has a constructor. If no
955// constructor is available we create one.
956static void EnsureConstructor(ObjectTemplate* object_template) {
957 if (Utils::OpenHandle(object_template)->constructor()->IsUndefined()) {
958 Local<FunctionTemplate> templ = FunctionTemplate::New();
959 i::Handle<i::FunctionTemplateInfo> constructor = Utils::OpenHandle(*templ);
960 constructor->set_instance_template(*Utils::OpenHandle(object_template));
961 Utils::OpenHandle(object_template)->set_constructor(*constructor);
962 }
963}
964
965
966void ObjectTemplate::SetAccessor(v8::Handle<String> name,
967 AccessorGetter getter,
968 AccessorSetter setter,
969 v8::Handle<Value> data,
970 AccessControl settings,
971 PropertyAttribute attribute) {
972 if (IsDeadCheck("v8::ObjectTemplate::SetAccessor()")) return;
973 ENTER_V8;
974 HandleScope scope;
975 EnsureConstructor(this);
976 i::FunctionTemplateInfo* constructor =
977 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
978 i::Handle<i::FunctionTemplateInfo> cons(constructor);
979 Utils::ToLocal(cons)->AddInstancePropertyAccessor(name,
980 getter,
981 setter,
982 data,
983 settings,
984 attribute);
985}
986
987
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100988void ObjectTemplate::SetNamedPropertyHandler(NamedPropertyGetter getter,
989 NamedPropertySetter setter,
990 NamedPropertyQuery query,
991 NamedPropertyDeleter remover,
992 NamedPropertyEnumerator enumerator,
993 Handle<Value> data) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000994 if (IsDeadCheck("v8::ObjectTemplate::SetNamedPropertyHandler()")) return;
995 ENTER_V8;
996 HandleScope scope;
997 EnsureConstructor(this);
998 i::FunctionTemplateInfo* constructor =
999 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1000 i::Handle<i::FunctionTemplateInfo> cons(constructor);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001001 Utils::ToLocal(cons)->SetNamedInstancePropertyHandler(getter,
1002 setter,
1003 query,
1004 remover,
1005 enumerator,
1006 data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001007}
1008
1009
1010void ObjectTemplate::MarkAsUndetectable() {
1011 if (IsDeadCheck("v8::ObjectTemplate::MarkAsUndetectable()")) return;
1012 ENTER_V8;
1013 HandleScope scope;
1014 EnsureConstructor(this);
1015 i::FunctionTemplateInfo* constructor =
1016 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1017 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1018 cons->set_undetectable(true);
1019}
1020
1021
1022void ObjectTemplate::SetAccessCheckCallbacks(
1023 NamedSecurityCallback named_callback,
1024 IndexedSecurityCallback indexed_callback,
1025 Handle<Value> data,
1026 bool turned_on_by_default) {
1027 if (IsDeadCheck("v8::ObjectTemplate::SetAccessCheckCallbacks()")) return;
1028 ENTER_V8;
1029 HandleScope scope;
1030 EnsureConstructor(this);
1031
1032 i::Handle<i::Struct> struct_info =
1033 i::Factory::NewStruct(i::ACCESS_CHECK_INFO_TYPE);
1034 i::Handle<i::AccessCheckInfo> info =
1035 i::Handle<i::AccessCheckInfo>::cast(struct_info);
1036 info->set_named_callback(*FromCData(named_callback));
1037 info->set_indexed_callback(*FromCData(indexed_callback));
1038 if (data.IsEmpty()) data = v8::Undefined();
1039 info->set_data(*Utils::OpenHandle(*data));
1040
1041 i::FunctionTemplateInfo* constructor =
1042 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1043 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1044 cons->set_access_check_info(*info);
1045 cons->set_needs_access_check(turned_on_by_default);
1046}
1047
1048
1049void ObjectTemplate::SetIndexedPropertyHandler(
1050 IndexedPropertyGetter getter,
1051 IndexedPropertySetter setter,
1052 IndexedPropertyQuery query,
1053 IndexedPropertyDeleter remover,
1054 IndexedPropertyEnumerator enumerator,
1055 Handle<Value> data) {
1056 if (IsDeadCheck("v8::ObjectTemplate::SetIndexedPropertyHandler()")) return;
1057 ENTER_V8;
1058 HandleScope scope;
1059 EnsureConstructor(this);
1060 i::FunctionTemplateInfo* constructor =
1061 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1062 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1063 Utils::ToLocal(cons)->SetIndexedInstancePropertyHandler(getter,
1064 setter,
1065 query,
1066 remover,
1067 enumerator,
1068 data);
1069}
1070
1071
1072void ObjectTemplate::SetCallAsFunctionHandler(InvocationCallback callback,
1073 Handle<Value> data) {
1074 if (IsDeadCheck("v8::ObjectTemplate::SetCallAsFunctionHandler()")) return;
1075 ENTER_V8;
1076 HandleScope scope;
1077 EnsureConstructor(this);
1078 i::FunctionTemplateInfo* constructor =
1079 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1080 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1081 Utils::ToLocal(cons)->SetInstanceCallAsFunctionHandler(callback, data);
1082}
1083
1084
1085int ObjectTemplate::InternalFieldCount() {
1086 if (IsDeadCheck("v8::ObjectTemplate::InternalFieldCount()")) {
1087 return 0;
1088 }
1089 return i::Smi::cast(Utils::OpenHandle(this)->internal_field_count())->value();
1090}
1091
1092
1093void ObjectTemplate::SetInternalFieldCount(int value) {
1094 if (IsDeadCheck("v8::ObjectTemplate::SetInternalFieldCount()")) return;
1095 if (!ApiCheck(i::Smi::IsValid(value),
1096 "v8::ObjectTemplate::SetInternalFieldCount()",
1097 "Invalid internal field count")) {
1098 return;
1099 }
1100 ENTER_V8;
1101 if (value > 0) {
1102 // The internal field count is set by the constructor function's
1103 // construct code, so we ensure that there is a constructor
1104 // function to do the setting.
1105 EnsureConstructor(this);
1106 }
1107 Utils::OpenHandle(this)->set_internal_field_count(i::Smi::FromInt(value));
1108}
1109
1110
1111// --- S c r i p t D a t a ---
1112
1113
1114ScriptData* ScriptData::PreCompile(const char* input, int length) {
1115 unibrow::Utf8InputBuffer<> buf(input, length);
1116 return i::PreParse(i::Handle<i::String>(), &buf, NULL);
1117}
1118
1119
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001120ScriptData* ScriptData::PreCompile(v8::Handle<String> source) {
1121 i::Handle<i::String> str = Utils::OpenHandle(*source);
1122 return i::PreParse(str, NULL, NULL);
1123}
1124
1125
Leon Clarkef7060e22010-06-03 12:02:55 +01001126ScriptData* ScriptData::New(const char* data, int length) {
1127 // Return an empty ScriptData if the length is obviously invalid.
1128 if (length % sizeof(unsigned) != 0) {
1129 return new i::ScriptDataImpl(i::Vector<unsigned>());
1130 }
1131
1132 // Copy the data to ensure it is properly aligned.
1133 int deserialized_data_length = length / sizeof(unsigned);
1134 unsigned* deserialized_data = i::NewArray<unsigned>(deserialized_data_length);
1135 memcpy(deserialized_data, data, length);
1136
1137 return new i::ScriptDataImpl(
1138 i::Vector<unsigned>(deserialized_data, deserialized_data_length));
Steve Blocka7e24c12009-10-30 11:49:00 +00001139}
1140
1141
1142// --- S c r i p t ---
1143
1144
1145Local<Script> Script::New(v8::Handle<String> source,
1146 v8::ScriptOrigin* origin,
Andrei Popescu402d9372010-02-26 13:31:12 +00001147 v8::ScriptData* pre_data,
1148 v8::Handle<String> script_data) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001149 ON_BAILOUT("v8::Script::New()", return Local<Script>());
1150 LOG_API("Script::New");
1151 ENTER_V8;
1152 i::Handle<i::String> str = Utils::OpenHandle(*source);
1153 i::Handle<i::Object> name_obj;
1154 int line_offset = 0;
1155 int column_offset = 0;
1156 if (origin != NULL) {
1157 if (!origin->ResourceName().IsEmpty()) {
1158 name_obj = Utils::OpenHandle(*origin->ResourceName());
1159 }
1160 if (!origin->ResourceLineOffset().IsEmpty()) {
1161 line_offset = static_cast<int>(origin->ResourceLineOffset()->Value());
1162 }
1163 if (!origin->ResourceColumnOffset().IsEmpty()) {
1164 column_offset = static_cast<int>(origin->ResourceColumnOffset()->Value());
1165 }
1166 }
1167 EXCEPTION_PREAMBLE();
Andrei Popescu402d9372010-02-26 13:31:12 +00001168 i::ScriptDataImpl* pre_data_impl = static_cast<i::ScriptDataImpl*>(pre_data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001169 // We assert that the pre-data is sane, even though we can actually
1170 // handle it if it turns out not to be in release mode.
Andrei Popescu402d9372010-02-26 13:31:12 +00001171 ASSERT(pre_data_impl == NULL || pre_data_impl->SanityCheck());
Steve Blocka7e24c12009-10-30 11:49:00 +00001172 // If the pre-data isn't sane we simply ignore it
Andrei Popescu402d9372010-02-26 13:31:12 +00001173 if (pre_data_impl != NULL && !pre_data_impl->SanityCheck()) {
1174 pre_data_impl = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00001175 }
Steve Block6ded16b2010-05-10 14:33:55 +01001176 i::Handle<i::SharedFunctionInfo> result =
Andrei Popescu31002712010-02-23 13:46:05 +00001177 i::Compiler::Compile(str,
1178 name_obj,
1179 line_offset,
1180 column_offset,
1181 NULL,
Andrei Popescu402d9372010-02-26 13:31:12 +00001182 pre_data_impl,
1183 Utils::OpenHandle(*script_data),
Andrei Popescu31002712010-02-23 13:46:05 +00001184 i::NOT_NATIVES_CODE);
Steve Block6ded16b2010-05-10 14:33:55 +01001185 has_pending_exception = result.is_null();
Steve Blocka7e24c12009-10-30 11:49:00 +00001186 EXCEPTION_BAILOUT_CHECK(Local<Script>());
Steve Block6ded16b2010-05-10 14:33:55 +01001187 return Local<Script>(ToApi<Script>(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00001188}
1189
1190
1191Local<Script> Script::New(v8::Handle<String> source,
1192 v8::Handle<Value> file_name) {
1193 ScriptOrigin origin(file_name);
1194 return New(source, &origin);
1195}
1196
1197
1198Local<Script> Script::Compile(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::Compile()", return Local<Script>());
1203 LOG_API("Script::Compile");
1204 ENTER_V8;
Andrei Popescu402d9372010-02-26 13:31:12 +00001205 Local<Script> generic = New(source, origin, pre_data, script_data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001206 if (generic.IsEmpty())
1207 return generic;
Steve Block6ded16b2010-05-10 14:33:55 +01001208 i::Handle<i::Object> obj = Utils::OpenHandle(*generic);
1209 i::Handle<i::SharedFunctionInfo> function =
1210 i::Handle<i::SharedFunctionInfo>(i::SharedFunctionInfo::cast(*obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00001211 i::Handle<i::JSFunction> result =
Steve Block6ded16b2010-05-10 14:33:55 +01001212 i::Factory::NewFunctionFromSharedFunctionInfo(function,
1213 i::Top::global_context());
Steve Blocka7e24c12009-10-30 11:49:00 +00001214 return Local<Script>(ToApi<Script>(result));
1215}
1216
1217
1218Local<Script> Script::Compile(v8::Handle<String> source,
Andrei Popescu402d9372010-02-26 13:31:12 +00001219 v8::Handle<Value> file_name,
1220 v8::Handle<String> script_data) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001221 ScriptOrigin origin(file_name);
Andrei Popescu402d9372010-02-26 13:31:12 +00001222 return Compile(source, &origin, 0, script_data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001223}
1224
1225
1226Local<Value> Script::Run() {
1227 ON_BAILOUT("v8::Script::Run()", return Local<Value>());
1228 LOG_API("Script::Run");
1229 ENTER_V8;
1230 i::Object* raw_result = NULL;
1231 {
1232 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +01001233 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1234 i::Handle<i::JSFunction> fun;
1235 if (obj->IsSharedFunctionInfo()) {
1236 i::Handle<i::SharedFunctionInfo>
1237 function_info(i::SharedFunctionInfo::cast(*obj));
1238 fun = i::Factory::NewFunctionFromSharedFunctionInfo(
1239 function_info, i::Top::global_context());
1240 } else {
1241 fun = i::Handle<i::JSFunction>(i::JSFunction::cast(*obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00001242 }
1243 EXCEPTION_PREAMBLE();
1244 i::Handle<i::Object> receiver(i::Top::context()->global_proxy());
1245 i::Handle<i::Object> result =
1246 i::Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
1247 EXCEPTION_BAILOUT_CHECK(Local<Value>());
1248 raw_result = *result;
1249 }
1250 i::Handle<i::Object> result(raw_result);
1251 return Utils::ToLocal(result);
1252}
1253
1254
Steve Block6ded16b2010-05-10 14:33:55 +01001255static i::Handle<i::SharedFunctionInfo> OpenScript(Script* script) {
1256 i::Handle<i::Object> obj = Utils::OpenHandle(script);
1257 i::Handle<i::SharedFunctionInfo> result;
1258 if (obj->IsSharedFunctionInfo()) {
1259 result =
1260 i::Handle<i::SharedFunctionInfo>(i::SharedFunctionInfo::cast(*obj));
1261 } else {
1262 result =
1263 i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared());
1264 }
1265 return result;
1266}
1267
1268
Steve Blocka7e24c12009-10-30 11:49:00 +00001269Local<Value> Script::Id() {
1270 ON_BAILOUT("v8::Script::Id()", return Local<Value>());
1271 LOG_API("Script::Id");
1272 i::Object* raw_id = NULL;
1273 {
1274 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +01001275 i::Handle<i::SharedFunctionInfo> function_info = OpenScript(this);
1276 i::Handle<i::Script> script(i::Script::cast(function_info->script()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001277 i::Handle<i::Object> id(script->id());
1278 raw_id = *id;
1279 }
1280 i::Handle<i::Object> id(raw_id);
1281 return Utils::ToLocal(id);
1282}
1283
1284
Steve Blockd0582a62009-12-15 09:54:21 +00001285void Script::SetData(v8::Handle<String> data) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001286 ON_BAILOUT("v8::Script::SetData()", return);
1287 LOG_API("Script::SetData");
1288 {
1289 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +01001290 i::Handle<i::SharedFunctionInfo> function_info = OpenScript(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001291 i::Handle<i::Object> raw_data = Utils::OpenHandle(*data);
Steve Block6ded16b2010-05-10 14:33:55 +01001292 i::Handle<i::Script> script(i::Script::cast(function_info->script()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001293 script->set_data(*raw_data);
1294 }
1295}
1296
1297
1298// --- E x c e p t i o n s ---
1299
1300
1301v8::TryCatch::TryCatch()
Steve Blockd0582a62009-12-15 09:54:21 +00001302 : next_(i::Top::try_catch_handler_address()),
Steve Blocka7e24c12009-10-30 11:49:00 +00001303 exception_(i::Heap::the_hole_value()),
1304 message_(i::Smi::FromInt(0)),
1305 is_verbose_(false),
1306 can_continue_(true),
1307 capture_message_(true),
Steve Blockd0582a62009-12-15 09:54:21 +00001308 rethrow_(false) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001309 i::Top::RegisterTryCatchHandler(this);
1310}
1311
1312
1313v8::TryCatch::~TryCatch() {
Steve Blockd0582a62009-12-15 09:54:21 +00001314 if (rethrow_) {
1315 v8::HandleScope scope;
1316 v8::Local<v8::Value> exc = v8::Local<v8::Value>::New(Exception());
1317 i::Top::UnregisterTryCatchHandler(this);
1318 v8::ThrowException(exc);
1319 } else {
1320 i::Top::UnregisterTryCatchHandler(this);
1321 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001322}
1323
1324
1325bool v8::TryCatch::HasCaught() const {
1326 return !reinterpret_cast<i::Object*>(exception_)->IsTheHole();
1327}
1328
1329
1330bool v8::TryCatch::CanContinue() const {
1331 return can_continue_;
1332}
1333
1334
Steve Blockd0582a62009-12-15 09:54:21 +00001335v8::Handle<v8::Value> v8::TryCatch::ReThrow() {
1336 if (!HasCaught()) return v8::Local<v8::Value>();
1337 rethrow_ = true;
1338 return v8::Undefined();
1339}
1340
1341
Steve Blocka7e24c12009-10-30 11:49:00 +00001342v8::Local<Value> v8::TryCatch::Exception() const {
1343 if (HasCaught()) {
1344 // Check for out of memory exception.
1345 i::Object* exception = reinterpret_cast<i::Object*>(exception_);
1346 return v8::Utils::ToLocal(i::Handle<i::Object>(exception));
1347 } else {
1348 return v8::Local<Value>();
1349 }
1350}
1351
1352
1353v8::Local<Value> v8::TryCatch::StackTrace() const {
1354 if (HasCaught()) {
1355 i::Object* raw_obj = reinterpret_cast<i::Object*>(exception_);
1356 if (!raw_obj->IsJSObject()) return v8::Local<Value>();
1357 v8::HandleScope scope;
1358 i::Handle<i::JSObject> obj(i::JSObject::cast(raw_obj));
1359 i::Handle<i::String> name = i::Factory::LookupAsciiSymbol("stack");
1360 if (!obj->HasProperty(*name))
1361 return v8::Local<Value>();
1362 return scope.Close(v8::Utils::ToLocal(i::GetProperty(obj, name)));
1363 } else {
1364 return v8::Local<Value>();
1365 }
1366}
1367
1368
1369v8::Local<v8::Message> v8::TryCatch::Message() const {
1370 if (HasCaught() && message_ != i::Smi::FromInt(0)) {
1371 i::Object* message = reinterpret_cast<i::Object*>(message_);
1372 return v8::Utils::MessageToLocal(i::Handle<i::Object>(message));
1373 } else {
1374 return v8::Local<v8::Message>();
1375 }
1376}
1377
1378
1379void v8::TryCatch::Reset() {
1380 exception_ = i::Heap::the_hole_value();
1381 message_ = i::Smi::FromInt(0);
1382}
1383
1384
1385void v8::TryCatch::SetVerbose(bool value) {
1386 is_verbose_ = value;
1387}
1388
1389
1390void v8::TryCatch::SetCaptureMessage(bool value) {
1391 capture_message_ = value;
1392}
1393
1394
1395// --- M e s s a g e ---
1396
1397
1398Local<String> Message::Get() const {
1399 ON_BAILOUT("v8::Message::Get()", return Local<String>());
1400 ENTER_V8;
1401 HandleScope scope;
1402 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1403 i::Handle<i::String> raw_result = i::MessageHandler::GetMessage(obj);
1404 Local<String> result = Utils::ToLocal(raw_result);
1405 return scope.Close(result);
1406}
1407
1408
1409v8::Handle<Value> Message::GetScriptResourceName() const {
1410 if (IsDeadCheck("v8::Message::GetScriptResourceName()")) {
1411 return Local<String>();
1412 }
1413 ENTER_V8;
1414 HandleScope scope;
1415 i::Handle<i::JSObject> obj =
1416 i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
1417 // Return this.script.name.
1418 i::Handle<i::JSValue> script =
1419 i::Handle<i::JSValue>::cast(GetProperty(obj, "script"));
1420 i::Handle<i::Object> resource_name(i::Script::cast(script->value())->name());
1421 return scope.Close(Utils::ToLocal(resource_name));
1422}
1423
1424
1425v8::Handle<Value> Message::GetScriptData() const {
1426 if (IsDeadCheck("v8::Message::GetScriptResourceData()")) {
1427 return Local<Value>();
1428 }
1429 ENTER_V8;
1430 HandleScope scope;
1431 i::Handle<i::JSObject> obj =
1432 i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
1433 // Return this.script.data.
1434 i::Handle<i::JSValue> script =
1435 i::Handle<i::JSValue>::cast(GetProperty(obj, "script"));
1436 i::Handle<i::Object> data(i::Script::cast(script->value())->data());
1437 return scope.Close(Utils::ToLocal(data));
1438}
1439
1440
1441static i::Handle<i::Object> CallV8HeapFunction(const char* name,
1442 i::Handle<i::Object> recv,
1443 int argc,
1444 i::Object** argv[],
1445 bool* has_pending_exception) {
1446 i::Handle<i::String> fmt_str = i::Factory::LookupAsciiSymbol(name);
1447 i::Object* object_fun = i::Top::builtins()->GetProperty(*fmt_str);
1448 i::Handle<i::JSFunction> fun =
1449 i::Handle<i::JSFunction>(i::JSFunction::cast(object_fun));
1450 i::Handle<i::Object> value =
1451 i::Execution::Call(fun, recv, argc, argv, has_pending_exception);
1452 return value;
1453}
1454
1455
1456static i::Handle<i::Object> CallV8HeapFunction(const char* name,
1457 i::Handle<i::Object> data,
1458 bool* has_pending_exception) {
1459 i::Object** argv[1] = { data.location() };
1460 return CallV8HeapFunction(name,
1461 i::Top::builtins(),
1462 1,
1463 argv,
1464 has_pending_exception);
1465}
1466
1467
1468int Message::GetLineNumber() const {
Kristian Monsen25f61362010-05-21 11:50:48 +01001469 ON_BAILOUT("v8::Message::GetLineNumber()", return kNoLineNumberInfo);
Steve Blocka7e24c12009-10-30 11:49:00 +00001470 ENTER_V8;
1471 HandleScope scope;
1472 EXCEPTION_PREAMBLE();
1473 i::Handle<i::Object> result = CallV8HeapFunction("GetLineNumber",
1474 Utils::OpenHandle(this),
1475 &has_pending_exception);
1476 EXCEPTION_BAILOUT_CHECK(0);
1477 return static_cast<int>(result->Number());
1478}
1479
1480
1481int Message::GetStartPosition() const {
1482 if (IsDeadCheck("v8::Message::GetStartPosition()")) return 0;
1483 ENTER_V8;
1484 HandleScope scope;
1485
1486 i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this);
1487 return static_cast<int>(GetProperty(data_obj, "startPos")->Number());
1488}
1489
1490
1491int Message::GetEndPosition() const {
1492 if (IsDeadCheck("v8::Message::GetEndPosition()")) return 0;
1493 ENTER_V8;
1494 HandleScope scope;
1495 i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this);
1496 return static_cast<int>(GetProperty(data_obj, "endPos")->Number());
1497}
1498
1499
1500int Message::GetStartColumn() const {
Kristian Monsen25f61362010-05-21 11:50:48 +01001501 if (IsDeadCheck("v8::Message::GetStartColumn()")) return kNoColumnInfo;
Steve Blocka7e24c12009-10-30 11:49:00 +00001502 ENTER_V8;
1503 HandleScope scope;
1504 i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this);
1505 EXCEPTION_PREAMBLE();
1506 i::Handle<i::Object> start_col_obj = CallV8HeapFunction(
1507 "GetPositionInLine",
1508 data_obj,
1509 &has_pending_exception);
1510 EXCEPTION_BAILOUT_CHECK(0);
1511 return static_cast<int>(start_col_obj->Number());
1512}
1513
1514
1515int Message::GetEndColumn() const {
Kristian Monsen25f61362010-05-21 11:50:48 +01001516 if (IsDeadCheck("v8::Message::GetEndColumn()")) return kNoColumnInfo;
Steve Blocka7e24c12009-10-30 11:49:00 +00001517 ENTER_V8;
1518 HandleScope scope;
1519 i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this);
1520 EXCEPTION_PREAMBLE();
1521 i::Handle<i::Object> start_col_obj = CallV8HeapFunction(
1522 "GetPositionInLine",
1523 data_obj,
1524 &has_pending_exception);
1525 EXCEPTION_BAILOUT_CHECK(0);
1526 int start = static_cast<int>(GetProperty(data_obj, "startPos")->Number());
1527 int end = static_cast<int>(GetProperty(data_obj, "endPos")->Number());
1528 return static_cast<int>(start_col_obj->Number()) + (end - start);
1529}
1530
1531
1532Local<String> Message::GetSourceLine() const {
1533 ON_BAILOUT("v8::Message::GetSourceLine()", return Local<String>());
1534 ENTER_V8;
1535 HandleScope scope;
1536 EXCEPTION_PREAMBLE();
1537 i::Handle<i::Object> result = CallV8HeapFunction("GetSourceLine",
1538 Utils::OpenHandle(this),
1539 &has_pending_exception);
1540 EXCEPTION_BAILOUT_CHECK(Local<v8::String>());
1541 if (result->IsString()) {
1542 return scope.Close(Utils::ToLocal(i::Handle<i::String>::cast(result)));
1543 } else {
1544 return Local<String>();
1545 }
1546}
1547
1548
1549void Message::PrintCurrentStackTrace(FILE* out) {
1550 if (IsDeadCheck("v8::Message::PrintCurrentStackTrace()")) return;
1551 ENTER_V8;
1552 i::Top::PrintCurrentStackTrace(out);
1553}
1554
1555
Kristian Monsen25f61362010-05-21 11:50:48 +01001556// --- S t a c k T r a c e ---
1557
1558Local<StackFrame> StackTrace::GetFrame(uint32_t index) const {
1559 if (IsDeadCheck("v8::StackTrace::GetFrame()")) return Local<StackFrame>();
1560 ENTER_V8;
1561 HandleScope scope;
1562 i::Handle<i::JSArray> self = Utils::OpenHandle(this);
1563 i::Handle<i::JSObject> obj(i::JSObject::cast(self->GetElement(index)));
1564 return scope.Close(Utils::StackFrameToLocal(obj));
1565}
1566
1567
1568int StackTrace::GetFrameCount() const {
1569 if (IsDeadCheck("v8::StackTrace::GetFrameCount()")) return -1;
1570 ENTER_V8;
1571 return i::Smi::cast(Utils::OpenHandle(this)->length())->value();
1572}
1573
1574
1575Local<Array> StackTrace::AsArray() {
1576 if (IsDeadCheck("v8::StackTrace::AsArray()")) Local<Array>();
1577 ENTER_V8;
1578 return Utils::ToLocal(Utils::OpenHandle(this));
1579}
1580
1581
1582Local<StackTrace> StackTrace::CurrentStackTrace(int frame_limit,
1583 StackTraceOptions options) {
1584 if (IsDeadCheck("v8::StackTrace::CurrentStackTrace()")) Local<StackTrace>();
1585 ENTER_V8;
1586 return i::Top::CaptureCurrentStackTrace(frame_limit, options);
1587}
1588
1589
1590// --- S t a c k F r a m e ---
1591
1592int StackFrame::GetLineNumber() const {
1593 if (IsDeadCheck("v8::StackFrame::GetLineNumber()")) {
1594 return Message::kNoLineNumberInfo;
1595 }
1596 ENTER_V8;
1597 i::HandleScope scope;
1598 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
1599 i::Handle<i::Object> line = GetProperty(self, "lineNumber");
1600 if (!line->IsSmi()) {
1601 return Message::kNoLineNumberInfo;
1602 }
1603 return i::Smi::cast(*line)->value();
1604}
1605
1606
1607int StackFrame::GetColumn() const {
1608 if (IsDeadCheck("v8::StackFrame::GetColumn()")) {
1609 return Message::kNoColumnInfo;
1610 }
1611 ENTER_V8;
1612 i::HandleScope scope;
1613 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
1614 i::Handle<i::Object> column = GetProperty(self, "column");
1615 if (!column->IsSmi()) {
1616 return Message::kNoColumnInfo;
1617 }
1618 return i::Smi::cast(*column)->value();
1619}
1620
1621
1622Local<String> StackFrame::GetScriptName() const {
1623 if (IsDeadCheck("v8::StackFrame::GetScriptName()")) return Local<String>();
1624 ENTER_V8;
1625 HandleScope scope;
1626 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
1627 i::Handle<i::Object> name = GetProperty(self, "scriptName");
1628 if (!name->IsString()) {
1629 return Local<String>();
1630 }
1631 return scope.Close(Local<String>::Cast(Utils::ToLocal(name)));
1632}
1633
1634
1635Local<String> StackFrame::GetFunctionName() const {
1636 if (IsDeadCheck("v8::StackFrame::GetFunctionName()")) return Local<String>();
1637 ENTER_V8;
1638 HandleScope scope;
1639 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
1640 i::Handle<i::Object> name = GetProperty(self, "functionName");
1641 if (!name->IsString()) {
1642 return Local<String>();
1643 }
1644 return scope.Close(Local<String>::Cast(Utils::ToLocal(name)));
1645}
1646
1647
1648bool StackFrame::IsEval() const {
1649 if (IsDeadCheck("v8::StackFrame::IsEval()")) return false;
1650 ENTER_V8;
1651 i::HandleScope scope;
1652 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
1653 i::Handle<i::Object> is_eval = GetProperty(self, "isEval");
1654 return is_eval->IsTrue();
1655}
1656
1657
1658bool StackFrame::IsConstructor() const {
1659 if (IsDeadCheck("v8::StackFrame::IsConstructor()")) return false;
1660 ENTER_V8;
1661 i::HandleScope scope;
1662 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
1663 i::Handle<i::Object> is_constructor = GetProperty(self, "isConstructor");
1664 return is_constructor->IsTrue();
1665}
1666
1667
Steve Blocka7e24c12009-10-30 11:49:00 +00001668// --- D a t a ---
1669
1670bool Value::IsUndefined() const {
1671 if (IsDeadCheck("v8::Value::IsUndefined()")) return false;
1672 return Utils::OpenHandle(this)->IsUndefined();
1673}
1674
1675
1676bool Value::IsNull() const {
1677 if (IsDeadCheck("v8::Value::IsNull()")) return false;
1678 return Utils::OpenHandle(this)->IsNull();
1679}
1680
1681
1682bool Value::IsTrue() const {
1683 if (IsDeadCheck("v8::Value::IsTrue()")) return false;
1684 return Utils::OpenHandle(this)->IsTrue();
1685}
1686
1687
1688bool Value::IsFalse() const {
1689 if (IsDeadCheck("v8::Value::IsFalse()")) return false;
1690 return Utils::OpenHandle(this)->IsFalse();
1691}
1692
1693
1694bool Value::IsFunction() const {
1695 if (IsDeadCheck("v8::Value::IsFunction()")) return false;
1696 return Utils::OpenHandle(this)->IsJSFunction();
1697}
1698
1699
1700bool Value::FullIsString() const {
1701 if (IsDeadCheck("v8::Value::IsString()")) return false;
1702 bool result = Utils::OpenHandle(this)->IsString();
1703 ASSERT_EQ(result, QuickIsString());
1704 return result;
1705}
1706
1707
1708bool Value::IsArray() const {
1709 if (IsDeadCheck("v8::Value::IsArray()")) return false;
1710 return Utils::OpenHandle(this)->IsJSArray();
1711}
1712
1713
1714bool Value::IsObject() const {
1715 if (IsDeadCheck("v8::Value::IsObject()")) return false;
1716 return Utils::OpenHandle(this)->IsJSObject();
1717}
1718
1719
1720bool Value::IsNumber() const {
1721 if (IsDeadCheck("v8::Value::IsNumber()")) return false;
1722 return Utils::OpenHandle(this)->IsNumber();
1723}
1724
1725
1726bool Value::IsBoolean() const {
1727 if (IsDeadCheck("v8::Value::IsBoolean()")) return false;
1728 return Utils::OpenHandle(this)->IsBoolean();
1729}
1730
1731
1732bool Value::IsExternal() const {
1733 if (IsDeadCheck("v8::Value::IsExternal()")) return false;
1734 return Utils::OpenHandle(this)->IsProxy();
1735}
1736
1737
1738bool Value::IsInt32() const {
1739 if (IsDeadCheck("v8::Value::IsInt32()")) return false;
1740 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1741 if (obj->IsSmi()) return true;
1742 if (obj->IsNumber()) {
1743 double value = obj->Number();
1744 return i::FastI2D(i::FastD2I(value)) == value;
1745 }
1746 return false;
1747}
1748
1749
Steve Block6ded16b2010-05-10 14:33:55 +01001750bool Value::IsUint32() const {
1751 if (IsDeadCheck("v8::Value::IsUint32()")) return false;
1752 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1753 if (obj->IsSmi()) return i::Smi::cast(*obj)->value() >= 0;
1754 if (obj->IsNumber()) {
1755 double value = obj->Number();
1756 return i::FastUI2D(i::FastD2UI(value)) == value;
1757 }
1758 return false;
1759}
1760
1761
Steve Blocka7e24c12009-10-30 11:49:00 +00001762bool Value::IsDate() const {
1763 if (IsDeadCheck("v8::Value::IsDate()")) return false;
1764 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1765 return obj->HasSpecificClassOf(i::Heap::Date_symbol());
1766}
1767
1768
1769Local<String> Value::ToString() const {
1770 if (IsDeadCheck("v8::Value::ToString()")) return Local<String>();
1771 LOG_API("ToString");
1772 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1773 i::Handle<i::Object> str;
1774 if (obj->IsString()) {
1775 str = obj;
1776 } else {
1777 ENTER_V8;
1778 EXCEPTION_PREAMBLE();
1779 str = i::Execution::ToString(obj, &has_pending_exception);
1780 EXCEPTION_BAILOUT_CHECK(Local<String>());
1781 }
1782 return Local<String>(ToApi<String>(str));
1783}
1784
1785
1786Local<String> Value::ToDetailString() const {
1787 if (IsDeadCheck("v8::Value::ToDetailString()")) return Local<String>();
1788 LOG_API("ToDetailString");
1789 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1790 i::Handle<i::Object> str;
1791 if (obj->IsString()) {
1792 str = obj;
1793 } else {
1794 ENTER_V8;
1795 EXCEPTION_PREAMBLE();
1796 str = i::Execution::ToDetailString(obj, &has_pending_exception);
1797 EXCEPTION_BAILOUT_CHECK(Local<String>());
1798 }
1799 return Local<String>(ToApi<String>(str));
1800}
1801
1802
1803Local<v8::Object> Value::ToObject() const {
1804 if (IsDeadCheck("v8::Value::ToObject()")) return Local<v8::Object>();
1805 LOG_API("ToObject");
1806 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1807 i::Handle<i::Object> val;
1808 if (obj->IsJSObject()) {
1809 val = obj;
1810 } else {
1811 ENTER_V8;
1812 EXCEPTION_PREAMBLE();
1813 val = i::Execution::ToObject(obj, &has_pending_exception);
1814 EXCEPTION_BAILOUT_CHECK(Local<v8::Object>());
1815 }
1816 return Local<v8::Object>(ToApi<Object>(val));
1817}
1818
1819
1820Local<Boolean> Value::ToBoolean() const {
1821 if (IsDeadCheck("v8::Value::ToBoolean()")) return Local<Boolean>();
1822 LOG_API("ToBoolean");
1823 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1824 if (obj->IsBoolean()) {
1825 return Local<Boolean>(ToApi<Boolean>(obj));
1826 } else {
1827 ENTER_V8;
1828 i::Handle<i::Object> val = i::Execution::ToBoolean(obj);
1829 return Local<Boolean>(ToApi<Boolean>(val));
1830 }
1831}
1832
1833
1834Local<Number> Value::ToNumber() const {
1835 if (IsDeadCheck("v8::Value::ToNumber()")) return Local<Number>();
1836 LOG_API("ToNumber");
1837 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1838 i::Handle<i::Object> num;
1839 if (obj->IsNumber()) {
1840 num = obj;
1841 } else {
1842 ENTER_V8;
1843 EXCEPTION_PREAMBLE();
1844 num = i::Execution::ToNumber(obj, &has_pending_exception);
1845 EXCEPTION_BAILOUT_CHECK(Local<Number>());
1846 }
1847 return Local<Number>(ToApi<Number>(num));
1848}
1849
1850
1851Local<Integer> Value::ToInteger() const {
1852 if (IsDeadCheck("v8::Value::ToInteger()")) return Local<Integer>();
1853 LOG_API("ToInteger");
1854 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1855 i::Handle<i::Object> num;
1856 if (obj->IsSmi()) {
1857 num = obj;
1858 } else {
1859 ENTER_V8;
1860 EXCEPTION_PREAMBLE();
1861 num = i::Execution::ToInteger(obj, &has_pending_exception);
1862 EXCEPTION_BAILOUT_CHECK(Local<Integer>());
1863 }
1864 return Local<Integer>(ToApi<Integer>(num));
1865}
1866
1867
1868void External::CheckCast(v8::Value* that) {
1869 if (IsDeadCheck("v8::External::Cast()")) return;
1870 i::Handle<i::Object> obj = Utils::OpenHandle(that);
1871 ApiCheck(obj->IsProxy(),
1872 "v8::External::Cast()",
1873 "Could not convert to external");
1874}
1875
1876
1877void v8::Object::CheckCast(Value* that) {
1878 if (IsDeadCheck("v8::Object::Cast()")) return;
1879 i::Handle<i::Object> obj = Utils::OpenHandle(that);
1880 ApiCheck(obj->IsJSObject(),
1881 "v8::Object::Cast()",
1882 "Could not convert to object");
1883}
1884
1885
1886void v8::Function::CheckCast(Value* that) {
1887 if (IsDeadCheck("v8::Function::Cast()")) return;
1888 i::Handle<i::Object> obj = Utils::OpenHandle(that);
1889 ApiCheck(obj->IsJSFunction(),
1890 "v8::Function::Cast()",
1891 "Could not convert to function");
1892}
1893
1894
1895void v8::String::CheckCast(v8::Value* that) {
1896 if (IsDeadCheck("v8::String::Cast()")) return;
1897 i::Handle<i::Object> obj = Utils::OpenHandle(that);
1898 ApiCheck(obj->IsString(),
1899 "v8::String::Cast()",
1900 "Could not convert to string");
1901}
1902
1903
1904void v8::Number::CheckCast(v8::Value* that) {
1905 if (IsDeadCheck("v8::Number::Cast()")) return;
1906 i::Handle<i::Object> obj = Utils::OpenHandle(that);
1907 ApiCheck(obj->IsNumber(),
1908 "v8::Number::Cast()",
1909 "Could not convert to number");
1910}
1911
1912
1913void v8::Integer::CheckCast(v8::Value* that) {
1914 if (IsDeadCheck("v8::Integer::Cast()")) return;
1915 i::Handle<i::Object> obj = Utils::OpenHandle(that);
1916 ApiCheck(obj->IsNumber(),
1917 "v8::Integer::Cast()",
1918 "Could not convert to number");
1919}
1920
1921
1922void v8::Array::CheckCast(Value* that) {
1923 if (IsDeadCheck("v8::Array::Cast()")) return;
1924 i::Handle<i::Object> obj = Utils::OpenHandle(that);
1925 ApiCheck(obj->IsJSArray(),
1926 "v8::Array::Cast()",
1927 "Could not convert to array");
1928}
1929
1930
1931void v8::Date::CheckCast(v8::Value* that) {
1932 if (IsDeadCheck("v8::Date::Cast()")) return;
1933 i::Handle<i::Object> obj = Utils::OpenHandle(that);
1934 ApiCheck(obj->HasSpecificClassOf(i::Heap::Date_symbol()),
1935 "v8::Date::Cast()",
1936 "Could not convert to date");
1937}
1938
1939
1940bool Value::BooleanValue() const {
1941 if (IsDeadCheck("v8::Value::BooleanValue()")) return false;
1942 LOG_API("BooleanValue");
1943 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1944 if (obj->IsBoolean()) {
1945 return obj->IsTrue();
1946 } else {
1947 ENTER_V8;
1948 i::Handle<i::Object> value = i::Execution::ToBoolean(obj);
1949 return value->IsTrue();
1950 }
1951}
1952
1953
1954double Value::NumberValue() const {
1955 if (IsDeadCheck("v8::Value::NumberValue()")) return i::OS::nan_value();
1956 LOG_API("NumberValue");
1957 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1958 i::Handle<i::Object> num;
1959 if (obj->IsNumber()) {
1960 num = obj;
1961 } else {
1962 ENTER_V8;
1963 EXCEPTION_PREAMBLE();
1964 num = i::Execution::ToNumber(obj, &has_pending_exception);
1965 EXCEPTION_BAILOUT_CHECK(i::OS::nan_value());
1966 }
1967 return num->Number();
1968}
1969
1970
1971int64_t Value::IntegerValue() const {
1972 if (IsDeadCheck("v8::Value::IntegerValue()")) return 0;
1973 LOG_API("IntegerValue");
1974 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1975 i::Handle<i::Object> num;
1976 if (obj->IsNumber()) {
1977 num = obj;
1978 } else {
1979 ENTER_V8;
1980 EXCEPTION_PREAMBLE();
1981 num = i::Execution::ToInteger(obj, &has_pending_exception);
1982 EXCEPTION_BAILOUT_CHECK(0);
1983 }
1984 if (num->IsSmi()) {
1985 return i::Smi::cast(*num)->value();
1986 } else {
1987 return static_cast<int64_t>(num->Number());
1988 }
1989}
1990
1991
1992Local<Int32> Value::ToInt32() const {
1993 if (IsDeadCheck("v8::Value::ToInt32()")) return Local<Int32>();
1994 LOG_API("ToInt32");
1995 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1996 i::Handle<i::Object> num;
1997 if (obj->IsSmi()) {
1998 num = obj;
1999 } else {
2000 ENTER_V8;
2001 EXCEPTION_PREAMBLE();
2002 num = i::Execution::ToInt32(obj, &has_pending_exception);
2003 EXCEPTION_BAILOUT_CHECK(Local<Int32>());
2004 }
2005 return Local<Int32>(ToApi<Int32>(num));
2006}
2007
2008
2009Local<Uint32> Value::ToUint32() const {
2010 if (IsDeadCheck("v8::Value::ToUint32()")) return Local<Uint32>();
2011 LOG_API("ToUInt32");
2012 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2013 i::Handle<i::Object> num;
2014 if (obj->IsSmi()) {
2015 num = obj;
2016 } else {
2017 ENTER_V8;
2018 EXCEPTION_PREAMBLE();
2019 num = i::Execution::ToUint32(obj, &has_pending_exception);
2020 EXCEPTION_BAILOUT_CHECK(Local<Uint32>());
2021 }
2022 return Local<Uint32>(ToApi<Uint32>(num));
2023}
2024
2025
2026Local<Uint32> Value::ToArrayIndex() const {
2027 if (IsDeadCheck("v8::Value::ToArrayIndex()")) return Local<Uint32>();
2028 LOG_API("ToArrayIndex");
2029 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2030 if (obj->IsSmi()) {
2031 if (i::Smi::cast(*obj)->value() >= 0) return Utils::Uint32ToLocal(obj);
2032 return Local<Uint32>();
2033 }
2034 ENTER_V8;
2035 EXCEPTION_PREAMBLE();
2036 i::Handle<i::Object> string_obj =
2037 i::Execution::ToString(obj, &has_pending_exception);
2038 EXCEPTION_BAILOUT_CHECK(Local<Uint32>());
2039 i::Handle<i::String> str = i::Handle<i::String>::cast(string_obj);
2040 uint32_t index;
2041 if (str->AsArrayIndex(&index)) {
2042 i::Handle<i::Object> value;
2043 if (index <= static_cast<uint32_t>(i::Smi::kMaxValue)) {
2044 value = i::Handle<i::Object>(i::Smi::FromInt(index));
2045 } else {
2046 value = i::Factory::NewNumber(index);
2047 }
2048 return Utils::Uint32ToLocal(value);
2049 }
2050 return Local<Uint32>();
2051}
2052
2053
2054int32_t Value::Int32Value() const {
2055 if (IsDeadCheck("v8::Value::Int32Value()")) return 0;
2056 LOG_API("Int32Value");
2057 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2058 if (obj->IsSmi()) {
2059 return i::Smi::cast(*obj)->value();
2060 } else {
2061 LOG_API("Int32Value (slow)");
2062 ENTER_V8;
2063 EXCEPTION_PREAMBLE();
2064 i::Handle<i::Object> num =
2065 i::Execution::ToInt32(obj, &has_pending_exception);
2066 EXCEPTION_BAILOUT_CHECK(0);
2067 if (num->IsSmi()) {
2068 return i::Smi::cast(*num)->value();
2069 } else {
2070 return static_cast<int32_t>(num->Number());
2071 }
2072 }
2073}
2074
2075
2076bool Value::Equals(Handle<Value> that) const {
2077 if (IsDeadCheck("v8::Value::Equals()")
2078 || EmptyCheck("v8::Value::Equals()", this)
2079 || EmptyCheck("v8::Value::Equals()", that)) {
2080 return false;
2081 }
2082 LOG_API("Equals");
2083 ENTER_V8;
2084 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2085 i::Handle<i::Object> other = Utils::OpenHandle(*that);
2086 i::Object** args[1] = { other.location() };
2087 EXCEPTION_PREAMBLE();
2088 i::Handle<i::Object> result =
2089 CallV8HeapFunction("EQUALS", obj, 1, args, &has_pending_exception);
2090 EXCEPTION_BAILOUT_CHECK(false);
2091 return *result == i::Smi::FromInt(i::EQUAL);
2092}
2093
2094
2095bool Value::StrictEquals(Handle<Value> that) const {
2096 if (IsDeadCheck("v8::Value::StrictEquals()")
2097 || EmptyCheck("v8::Value::StrictEquals()", this)
2098 || EmptyCheck("v8::Value::StrictEquals()", that)) {
2099 return false;
2100 }
2101 LOG_API("StrictEquals");
2102 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2103 i::Handle<i::Object> other = Utils::OpenHandle(*that);
2104 // Must check HeapNumber first, since NaN !== NaN.
2105 if (obj->IsHeapNumber()) {
2106 if (!other->IsNumber()) return false;
2107 double x = obj->Number();
2108 double y = other->Number();
2109 // Must check explicitly for NaN:s on Windows, but -0 works fine.
2110 return x == y && !isnan(x) && !isnan(y);
2111 } else if (*obj == *other) { // Also covers Booleans.
2112 return true;
2113 } else if (obj->IsSmi()) {
2114 return other->IsNumber() && obj->Number() == other->Number();
2115 } else if (obj->IsString()) {
2116 return other->IsString() &&
2117 i::String::cast(*obj)->Equals(i::String::cast(*other));
2118 } else if (obj->IsUndefined() || obj->IsUndetectableObject()) {
2119 return other->IsUndefined() || other->IsUndetectableObject();
2120 } else {
2121 return false;
2122 }
2123}
2124
2125
2126uint32_t Value::Uint32Value() const {
2127 if (IsDeadCheck("v8::Value::Uint32Value()")) return 0;
2128 LOG_API("Uint32Value");
2129 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2130 if (obj->IsSmi()) {
2131 return i::Smi::cast(*obj)->value();
2132 } else {
2133 ENTER_V8;
2134 EXCEPTION_PREAMBLE();
2135 i::Handle<i::Object> num =
2136 i::Execution::ToUint32(obj, &has_pending_exception);
2137 EXCEPTION_BAILOUT_CHECK(0);
2138 if (num->IsSmi()) {
2139 return i::Smi::cast(*num)->value();
2140 } else {
2141 return static_cast<uint32_t>(num->Number());
2142 }
2143 }
2144}
2145
2146
2147bool v8::Object::Set(v8::Handle<Value> key, v8::Handle<Value> value,
2148 v8::PropertyAttribute attribs) {
2149 ON_BAILOUT("v8::Object::Set()", return false);
2150 ENTER_V8;
2151 HandleScope scope;
2152 i::Handle<i::Object> self = Utils::OpenHandle(this);
2153 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2154 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
2155 EXCEPTION_PREAMBLE();
2156 i::Handle<i::Object> obj = i::SetProperty(
2157 self,
2158 key_obj,
2159 value_obj,
2160 static_cast<PropertyAttributes>(attribs));
2161 has_pending_exception = obj.is_null();
2162 EXCEPTION_BAILOUT_CHECK(false);
2163 return true;
2164}
2165
2166
Steve Block6ded16b2010-05-10 14:33:55 +01002167bool v8::Object::Set(uint32_t index, v8::Handle<Value> value) {
2168 ON_BAILOUT("v8::Object::Set()", return false);
2169 ENTER_V8;
2170 HandleScope scope;
2171 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2172 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
2173 EXCEPTION_PREAMBLE();
2174 i::Handle<i::Object> obj = i::SetElement(
2175 self,
2176 index,
2177 value_obj);
2178 has_pending_exception = obj.is_null();
2179 EXCEPTION_BAILOUT_CHECK(false);
2180 return true;
2181}
2182
2183
Steve Blocka7e24c12009-10-30 11:49:00 +00002184bool v8::Object::ForceSet(v8::Handle<Value> key,
2185 v8::Handle<Value> value,
2186 v8::PropertyAttribute attribs) {
2187 ON_BAILOUT("v8::Object::ForceSet()", return false);
2188 ENTER_V8;
2189 HandleScope scope;
2190 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2191 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2192 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
2193 EXCEPTION_PREAMBLE();
2194 i::Handle<i::Object> obj = i::ForceSetProperty(
2195 self,
2196 key_obj,
2197 value_obj,
2198 static_cast<PropertyAttributes>(attribs));
2199 has_pending_exception = obj.is_null();
2200 EXCEPTION_BAILOUT_CHECK(false);
2201 return true;
2202}
2203
2204
2205bool v8::Object::ForceDelete(v8::Handle<Value> key) {
2206 ON_BAILOUT("v8::Object::ForceDelete()", return false);
2207 ENTER_V8;
2208 HandleScope scope;
2209 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2210 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2211 EXCEPTION_PREAMBLE();
2212 i::Handle<i::Object> obj = i::ForceDeleteProperty(self, key_obj);
2213 has_pending_exception = obj.is_null();
2214 EXCEPTION_BAILOUT_CHECK(false);
2215 return obj->IsTrue();
2216}
2217
2218
2219Local<Value> v8::Object::Get(v8::Handle<Value> key) {
2220 ON_BAILOUT("v8::Object::Get()", return Local<v8::Value>());
2221 ENTER_V8;
2222 i::Handle<i::Object> self = Utils::OpenHandle(this);
2223 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2224 EXCEPTION_PREAMBLE();
2225 i::Handle<i::Object> result = i::GetProperty(self, key_obj);
2226 has_pending_exception = result.is_null();
2227 EXCEPTION_BAILOUT_CHECK(Local<Value>());
2228 return Utils::ToLocal(result);
2229}
2230
2231
Steve Block6ded16b2010-05-10 14:33:55 +01002232Local<Value> v8::Object::Get(uint32_t index) {
2233 ON_BAILOUT("v8::Object::Get()", return Local<v8::Value>());
2234 ENTER_V8;
2235 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2236 EXCEPTION_PREAMBLE();
2237 i::Handle<i::Object> result = i::GetElement(self, index);
2238 has_pending_exception = result.is_null();
2239 EXCEPTION_BAILOUT_CHECK(Local<Value>());
2240 return Utils::ToLocal(result);
2241}
2242
2243
Steve Blocka7e24c12009-10-30 11:49:00 +00002244Local<Value> v8::Object::GetPrototype() {
2245 ON_BAILOUT("v8::Object::GetPrototype()", return Local<v8::Value>());
2246 ENTER_V8;
2247 i::Handle<i::Object> self = Utils::OpenHandle(this);
2248 i::Handle<i::Object> result = i::GetPrototype(self);
2249 return Utils::ToLocal(result);
2250}
2251
2252
Andrei Popescu402d9372010-02-26 13:31:12 +00002253bool v8::Object::SetPrototype(Handle<Value> value) {
2254 ON_BAILOUT("v8::Object::SetPrototype()", return false);
2255 ENTER_V8;
2256 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2257 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
2258 EXCEPTION_PREAMBLE();
2259 i::Handle<i::Object> result = i::SetPrototype(self, value_obj);
2260 has_pending_exception = result.is_null();
2261 EXCEPTION_BAILOUT_CHECK(false);
2262 return true;
2263}
2264
2265
Steve Blocka7e24c12009-10-30 11:49:00 +00002266Local<Object> v8::Object::FindInstanceInPrototypeChain(
2267 v8::Handle<FunctionTemplate> tmpl) {
2268 ON_BAILOUT("v8::Object::FindInstanceInPrototypeChain()",
2269 return Local<v8::Object>());
2270 ENTER_V8;
2271 i::JSObject* object = *Utils::OpenHandle(this);
2272 i::FunctionTemplateInfo* tmpl_info = *Utils::OpenHandle(*tmpl);
2273 while (!object->IsInstanceOf(tmpl_info)) {
2274 i::Object* prototype = object->GetPrototype();
2275 if (!prototype->IsJSObject()) return Local<Object>();
2276 object = i::JSObject::cast(prototype);
2277 }
2278 return Utils::ToLocal(i::Handle<i::JSObject>(object));
2279}
2280
2281
2282Local<Array> v8::Object::GetPropertyNames() {
2283 ON_BAILOUT("v8::Object::GetPropertyNames()", return Local<v8::Array>());
2284 ENTER_V8;
2285 v8::HandleScope scope;
2286 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2287 i::Handle<i::FixedArray> value =
2288 i::GetKeysInFixedArrayFor(self, i::INCLUDE_PROTOS);
2289 // Because we use caching to speed up enumeration it is important
2290 // to never change the result of the basic enumeration function so
2291 // we clone the result.
2292 i::Handle<i::FixedArray> elms = i::Factory::CopyFixedArray(value);
2293 i::Handle<i::JSArray> result = i::Factory::NewJSArrayWithElements(elms);
2294 return scope.Close(Utils::ToLocal(result));
2295}
2296
2297
2298Local<String> v8::Object::ObjectProtoToString() {
2299 ON_BAILOUT("v8::Object::ObjectProtoToString()", return Local<v8::String>());
2300 ENTER_V8;
2301 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2302
2303 i::Handle<i::Object> name(self->class_name());
2304
2305 // Native implementation of Object.prototype.toString (v8natives.js):
2306 // var c = %ClassOf(this);
2307 // if (c === 'Arguments') c = 'Object';
2308 // return "[object " + c + "]";
2309
2310 if (!name->IsString()) {
2311 return v8::String::New("[object ]");
2312
2313 } else {
2314 i::Handle<i::String> class_name = i::Handle<i::String>::cast(name);
2315 if (class_name->IsEqualTo(i::CStrVector("Arguments"))) {
2316 return v8::String::New("[object Object]");
2317
2318 } else {
2319 const char* prefix = "[object ";
2320 Local<String> str = Utils::ToLocal(class_name);
2321 const char* postfix = "]";
2322
Steve Blockd0582a62009-12-15 09:54:21 +00002323 int prefix_len = i::StrLength(prefix);
2324 int str_len = str->Length();
2325 int postfix_len = i::StrLength(postfix);
Steve Blocka7e24c12009-10-30 11:49:00 +00002326
Steve Blockd0582a62009-12-15 09:54:21 +00002327 int buf_len = prefix_len + str_len + postfix_len;
Kristian Monsen25f61362010-05-21 11:50:48 +01002328 i::ScopedVector<char> buf(buf_len);
Steve Blocka7e24c12009-10-30 11:49:00 +00002329
2330 // Write prefix.
Kristian Monsen25f61362010-05-21 11:50:48 +01002331 char* ptr = buf.start();
Steve Blocka7e24c12009-10-30 11:49:00 +00002332 memcpy(ptr, prefix, prefix_len * v8::internal::kCharSize);
2333 ptr += prefix_len;
2334
2335 // Write real content.
2336 str->WriteAscii(ptr, 0, str_len);
2337 ptr += str_len;
2338
2339 // Write postfix.
2340 memcpy(ptr, postfix, postfix_len * v8::internal::kCharSize);
2341
2342 // Copy the buffer into a heap-allocated string and return it.
Kristian Monsen25f61362010-05-21 11:50:48 +01002343 Local<String> result = v8::String::New(buf.start(), buf_len);
Steve Blocka7e24c12009-10-30 11:49:00 +00002344 return result;
2345 }
2346 }
2347}
2348
2349
2350bool v8::Object::Delete(v8::Handle<String> key) {
2351 ON_BAILOUT("v8::Object::Delete()", return false);
2352 ENTER_V8;
2353 HandleScope scope;
2354 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2355 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
2356 return i::DeleteProperty(self, key_obj)->IsTrue();
2357}
2358
2359
2360bool v8::Object::Has(v8::Handle<String> key) {
2361 ON_BAILOUT("v8::Object::Has()", return false);
2362 ENTER_V8;
2363 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2364 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
2365 return self->HasProperty(*key_obj);
2366}
2367
2368
2369bool v8::Object::Delete(uint32_t index) {
2370 ON_BAILOUT("v8::Object::DeleteProperty()", return false);
2371 ENTER_V8;
2372 HandleScope scope;
2373 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2374 return i::DeleteElement(self, index)->IsTrue();
2375}
2376
2377
2378bool v8::Object::Has(uint32_t index) {
2379 ON_BAILOUT("v8::Object::HasProperty()", return false);
2380 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2381 return self->HasElement(index);
2382}
2383
2384
Leon Clarkef7060e22010-06-03 12:02:55 +01002385bool Object::SetAccessor(Handle<String> name,
2386 AccessorGetter getter,
2387 AccessorSetter setter,
2388 v8::Handle<Value> data,
2389 AccessControl settings,
2390 PropertyAttribute attributes) {
2391 ON_BAILOUT("v8::Object::SetAccessor()", return false);
2392 ENTER_V8;
2393 HandleScope scope;
2394 i::Handle<i::AccessorInfo> info = MakeAccessorInfo(name,
2395 getter, setter, data,
2396 settings, attributes);
2397 i::Handle<i::Object> result = i::SetAccessor(Utils::OpenHandle(this), info);
2398 return !result.is_null() && !result->IsUndefined();
2399}
2400
2401
Steve Blocka7e24c12009-10-30 11:49:00 +00002402bool v8::Object::HasRealNamedProperty(Handle<String> key) {
2403 ON_BAILOUT("v8::Object::HasRealNamedProperty()", return false);
2404 return Utils::OpenHandle(this)->HasRealNamedProperty(
2405 *Utils::OpenHandle(*key));
2406}
2407
2408
2409bool v8::Object::HasRealIndexedProperty(uint32_t index) {
2410 ON_BAILOUT("v8::Object::HasRealIndexedProperty()", return false);
2411 return Utils::OpenHandle(this)->HasRealElementProperty(index);
2412}
2413
2414
2415bool v8::Object::HasRealNamedCallbackProperty(Handle<String> key) {
2416 ON_BAILOUT("v8::Object::HasRealNamedCallbackProperty()", return false);
2417 ENTER_V8;
2418 return Utils::OpenHandle(this)->HasRealNamedCallbackProperty(
2419 *Utils::OpenHandle(*key));
2420}
2421
2422
2423bool v8::Object::HasNamedLookupInterceptor() {
2424 ON_BAILOUT("v8::Object::HasNamedLookupInterceptor()", return false);
2425 return Utils::OpenHandle(this)->HasNamedInterceptor();
2426}
2427
2428
2429bool v8::Object::HasIndexedLookupInterceptor() {
2430 ON_BAILOUT("v8::Object::HasIndexedLookupInterceptor()", return false);
2431 return Utils::OpenHandle(this)->HasIndexedInterceptor();
2432}
2433
2434
2435Local<Value> v8::Object::GetRealNamedPropertyInPrototypeChain(
2436 Handle<String> key) {
2437 ON_BAILOUT("v8::Object::GetRealNamedPropertyInPrototypeChain()",
2438 return Local<Value>());
2439 ENTER_V8;
2440 i::Handle<i::JSObject> self_obj = Utils::OpenHandle(this);
2441 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
2442 i::LookupResult lookup;
2443 self_obj->LookupRealNamedPropertyInPrototypes(*key_obj, &lookup);
Andrei Popescu402d9372010-02-26 13:31:12 +00002444 if (lookup.IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002445 PropertyAttributes attributes;
2446 i::Handle<i::Object> result(self_obj->GetProperty(*self_obj,
2447 &lookup,
2448 *key_obj,
2449 &attributes));
2450 return Utils::ToLocal(result);
2451 }
2452 return Local<Value>(); // No real property was found in prototype chain.
2453}
2454
2455
2456Local<Value> v8::Object::GetRealNamedProperty(Handle<String> key) {
2457 ON_BAILOUT("v8::Object::GetRealNamedProperty()", return Local<Value>());
2458 ENTER_V8;
2459 i::Handle<i::JSObject> self_obj = Utils::OpenHandle(this);
2460 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
2461 i::LookupResult lookup;
2462 self_obj->LookupRealNamedProperty(*key_obj, &lookup);
Andrei Popescu402d9372010-02-26 13:31:12 +00002463 if (lookup.IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002464 PropertyAttributes attributes;
2465 i::Handle<i::Object> result(self_obj->GetProperty(*self_obj,
2466 &lookup,
2467 *key_obj,
2468 &attributes));
2469 return Utils::ToLocal(result);
2470 }
2471 return Local<Value>(); // No real property was found in prototype chain.
2472}
2473
2474
2475// Turns on access checks by copying the map and setting the check flag.
2476// Because the object gets a new map, existing inline cache caching
2477// the old map of this object will fail.
2478void v8::Object::TurnOnAccessCheck() {
2479 ON_BAILOUT("v8::Object::TurnOnAccessCheck()", return);
2480 ENTER_V8;
2481 HandleScope scope;
2482 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
2483
2484 i::Handle<i::Map> new_map =
2485 i::Factory::CopyMapDropTransitions(i::Handle<i::Map>(obj->map()));
2486 new_map->set_is_access_check_needed(true);
2487 obj->set_map(*new_map);
2488}
2489
2490
2491bool v8::Object::IsDirty() {
2492 return Utils::OpenHandle(this)->IsDirty();
2493}
2494
2495
2496Local<v8::Object> v8::Object::Clone() {
2497 ON_BAILOUT("v8::Object::Clone()", return Local<Object>());
2498 ENTER_V8;
2499 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2500 EXCEPTION_PREAMBLE();
2501 i::Handle<i::JSObject> result = i::Copy(self);
2502 has_pending_exception = result.is_null();
2503 EXCEPTION_BAILOUT_CHECK(Local<Object>());
2504 return Utils::ToLocal(result);
2505}
2506
2507
2508int v8::Object::GetIdentityHash() {
2509 ON_BAILOUT("v8::Object::GetIdentityHash()", return 0);
2510 ENTER_V8;
2511 HandleScope scope;
2512 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2513 i::Handle<i::Object> hidden_props(i::GetHiddenProperties(self, true));
2514 i::Handle<i::Object> hash_symbol = i::Factory::identity_hash_symbol();
2515 i::Handle<i::Object> hash = i::GetProperty(hidden_props, hash_symbol);
2516 int hash_value;
2517 if (hash->IsSmi()) {
2518 hash_value = i::Smi::cast(*hash)->value();
2519 } else {
2520 int attempts = 0;
2521 do {
2522 // Generate a random 32-bit hash value but limit range to fit
2523 // within a smi.
2524 hash_value = i::V8::Random() & i::Smi::kMaxValue;
2525 attempts++;
2526 } while (hash_value == 0 && attempts < 30);
2527 hash_value = hash_value != 0 ? hash_value : 1; // never return 0
2528 i::SetProperty(hidden_props,
2529 hash_symbol,
2530 i::Handle<i::Object>(i::Smi::FromInt(hash_value)),
2531 static_cast<PropertyAttributes>(None));
2532 }
2533 return hash_value;
2534}
2535
2536
2537bool v8::Object::SetHiddenValue(v8::Handle<v8::String> key,
2538 v8::Handle<v8::Value> value) {
2539 ON_BAILOUT("v8::Object::SetHiddenValue()", return false);
2540 ENTER_V8;
2541 HandleScope scope;
2542 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2543 i::Handle<i::Object> hidden_props(i::GetHiddenProperties(self, true));
2544 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2545 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
2546 EXCEPTION_PREAMBLE();
2547 i::Handle<i::Object> obj = i::SetProperty(
2548 hidden_props,
2549 key_obj,
2550 value_obj,
2551 static_cast<PropertyAttributes>(None));
2552 has_pending_exception = obj.is_null();
2553 EXCEPTION_BAILOUT_CHECK(false);
2554 return true;
2555}
2556
2557
2558v8::Local<v8::Value> v8::Object::GetHiddenValue(v8::Handle<v8::String> key) {
2559 ON_BAILOUT("v8::Object::GetHiddenValue()", return Local<v8::Value>());
2560 ENTER_V8;
2561 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2562 i::Handle<i::Object> hidden_props(i::GetHiddenProperties(self, false));
2563 if (hidden_props->IsUndefined()) {
2564 return v8::Local<v8::Value>();
2565 }
2566 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
2567 EXCEPTION_PREAMBLE();
2568 i::Handle<i::Object> result = i::GetProperty(hidden_props, key_obj);
2569 has_pending_exception = result.is_null();
2570 EXCEPTION_BAILOUT_CHECK(v8::Local<v8::Value>());
2571 if (result->IsUndefined()) {
2572 return v8::Local<v8::Value>();
2573 }
2574 return Utils::ToLocal(result);
2575}
2576
2577
2578bool v8::Object::DeleteHiddenValue(v8::Handle<v8::String> key) {
2579 ON_BAILOUT("v8::DeleteHiddenValue()", return false);
2580 ENTER_V8;
2581 HandleScope scope;
2582 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2583 i::Handle<i::Object> hidden_props(i::GetHiddenProperties(self, false));
2584 if (hidden_props->IsUndefined()) {
2585 return true;
2586 }
2587 i::Handle<i::JSObject> js_obj(i::JSObject::cast(*hidden_props));
2588 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
2589 return i::DeleteProperty(js_obj, key_obj)->IsTrue();
2590}
2591
2592
2593void v8::Object::SetIndexedPropertiesToPixelData(uint8_t* data, int length) {
2594 ON_BAILOUT("v8::SetElementsToPixelData()", return);
2595 ENTER_V8;
2596 HandleScope scope;
Steve Block3ce2e202009-11-05 08:53:23 +00002597 if (!ApiCheck(length <= i::PixelArray::kMaxLength,
Steve Blocka7e24c12009-10-30 11:49:00 +00002598 "v8::Object::SetIndexedPropertiesToPixelData()",
2599 "length exceeds max acceptable value")) {
2600 return;
2601 }
2602 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2603 if (!ApiCheck(!self->IsJSArray(),
2604 "v8::Object::SetIndexedPropertiesToPixelData()",
2605 "JSArray is not supported")) {
2606 return;
2607 }
2608 i::Handle<i::PixelArray> pixels = i::Factory::NewPixelArray(length, data);
2609 self->set_elements(*pixels);
2610}
2611
2612
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002613bool v8::Object::HasIndexedPropertiesInPixelData() {
2614 ON_BAILOUT("v8::HasIndexedPropertiesInPixelData()", return false);
2615 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2616 return self->HasPixelElements();
2617}
2618
2619
2620uint8_t* v8::Object::GetIndexedPropertiesPixelData() {
2621 ON_BAILOUT("v8::GetIndexedPropertiesPixelData()", return NULL);
2622 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2623 if (self->HasPixelElements()) {
2624 return i::PixelArray::cast(self->elements())->external_pointer();
2625 } else {
2626 return NULL;
2627 }
2628}
2629
2630
2631int v8::Object::GetIndexedPropertiesPixelDataLength() {
2632 ON_BAILOUT("v8::GetIndexedPropertiesPixelDataLength()", return -1);
2633 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2634 if (self->HasPixelElements()) {
2635 return i::PixelArray::cast(self->elements())->length();
2636 } else {
2637 return -1;
2638 }
2639}
2640
2641
Steve Block3ce2e202009-11-05 08:53:23 +00002642void v8::Object::SetIndexedPropertiesToExternalArrayData(
2643 void* data,
2644 ExternalArrayType array_type,
2645 int length) {
2646 ON_BAILOUT("v8::SetIndexedPropertiesToExternalArrayData()", return);
2647 ENTER_V8;
2648 HandleScope scope;
2649 if (!ApiCheck(length <= i::ExternalArray::kMaxLength,
2650 "v8::Object::SetIndexedPropertiesToExternalArrayData()",
2651 "length exceeds max acceptable value")) {
2652 return;
2653 }
2654 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2655 if (!ApiCheck(!self->IsJSArray(),
2656 "v8::Object::SetIndexedPropertiesToExternalArrayData()",
2657 "JSArray is not supported")) {
2658 return;
2659 }
2660 i::Handle<i::ExternalArray> array =
2661 i::Factory::NewExternalArray(length, array_type, data);
2662 self->set_elements(*array);
2663}
2664
2665
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002666bool v8::Object::HasIndexedPropertiesInExternalArrayData() {
2667 ON_BAILOUT("v8::HasIndexedPropertiesInExternalArrayData()", return false);
2668 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2669 return self->HasExternalArrayElements();
2670}
2671
2672
2673void* v8::Object::GetIndexedPropertiesExternalArrayData() {
2674 ON_BAILOUT("v8::GetIndexedPropertiesExternalArrayData()", return NULL);
2675 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2676 if (self->HasExternalArrayElements()) {
2677 return i::ExternalArray::cast(self->elements())->external_pointer();
2678 } else {
2679 return NULL;
2680 }
2681}
2682
2683
2684ExternalArrayType v8::Object::GetIndexedPropertiesExternalArrayDataType() {
2685 ON_BAILOUT("v8::GetIndexedPropertiesExternalArrayDataType()",
2686 return static_cast<ExternalArrayType>(-1));
2687 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2688 switch (self->elements()->map()->instance_type()) {
2689 case i::EXTERNAL_BYTE_ARRAY_TYPE:
2690 return kExternalByteArray;
2691 case i::EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE:
2692 return kExternalUnsignedByteArray;
2693 case i::EXTERNAL_SHORT_ARRAY_TYPE:
2694 return kExternalShortArray;
2695 case i::EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE:
2696 return kExternalUnsignedShortArray;
2697 case i::EXTERNAL_INT_ARRAY_TYPE:
2698 return kExternalIntArray;
2699 case i::EXTERNAL_UNSIGNED_INT_ARRAY_TYPE:
2700 return kExternalUnsignedIntArray;
2701 case i::EXTERNAL_FLOAT_ARRAY_TYPE:
2702 return kExternalFloatArray;
2703 default:
2704 return static_cast<ExternalArrayType>(-1);
2705 }
2706}
2707
2708
2709int v8::Object::GetIndexedPropertiesExternalArrayDataLength() {
2710 ON_BAILOUT("v8::GetIndexedPropertiesExternalArrayDataLength()", return 0);
2711 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2712 if (self->HasExternalArrayElements()) {
2713 return i::ExternalArray::cast(self->elements())->length();
2714 } else {
2715 return -1;
2716 }
2717}
2718
2719
Steve Blocka7e24c12009-10-30 11:49:00 +00002720Local<v8::Object> Function::NewInstance() const {
2721 return NewInstance(0, NULL);
2722}
2723
2724
2725Local<v8::Object> Function::NewInstance(int argc,
2726 v8::Handle<v8::Value> argv[]) const {
2727 ON_BAILOUT("v8::Function::NewInstance()", return Local<v8::Object>());
2728 LOG_API("Function::NewInstance");
2729 ENTER_V8;
2730 HandleScope scope;
2731 i::Handle<i::JSFunction> function = Utils::OpenHandle(this);
2732 STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**));
2733 i::Object*** args = reinterpret_cast<i::Object***>(argv);
2734 EXCEPTION_PREAMBLE();
2735 i::Handle<i::Object> returned =
2736 i::Execution::New(function, argc, args, &has_pending_exception);
2737 EXCEPTION_BAILOUT_CHECK(Local<v8::Object>());
2738 return scope.Close(Utils::ToLocal(i::Handle<i::JSObject>::cast(returned)));
2739}
2740
2741
2742Local<v8::Value> Function::Call(v8::Handle<v8::Object> recv, int argc,
2743 v8::Handle<v8::Value> argv[]) {
2744 ON_BAILOUT("v8::Function::Call()", return Local<v8::Value>());
2745 LOG_API("Function::Call");
2746 ENTER_V8;
2747 i::Object* raw_result = NULL;
2748 {
2749 HandleScope scope;
2750 i::Handle<i::JSFunction> fun = Utils::OpenHandle(this);
2751 i::Handle<i::Object> recv_obj = Utils::OpenHandle(*recv);
2752 STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**));
2753 i::Object*** args = reinterpret_cast<i::Object***>(argv);
2754 EXCEPTION_PREAMBLE();
2755 i::Handle<i::Object> returned =
2756 i::Execution::Call(fun, recv_obj, argc, args, &has_pending_exception);
2757 EXCEPTION_BAILOUT_CHECK(Local<Object>());
2758 raw_result = *returned;
2759 }
2760 i::Handle<i::Object> result(raw_result);
2761 return Utils::ToLocal(result);
2762}
2763
2764
2765void Function::SetName(v8::Handle<v8::String> name) {
2766 ENTER_V8;
2767 i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
2768 func->shared()->set_name(*Utils::OpenHandle(*name));
2769}
2770
2771
2772Handle<Value> Function::GetName() const {
2773 i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
2774 return Utils::ToLocal(i::Handle<i::Object>(func->shared()->name()));
2775}
2776
2777
Andrei Popescu402d9372010-02-26 13:31:12 +00002778ScriptOrigin Function::GetScriptOrigin() const {
2779 i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
2780 if (func->shared()->script()->IsScript()) {
2781 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
2782 v8::ScriptOrigin origin(
2783 Utils::ToLocal(i::Handle<i::Object>(script->name())),
2784 v8::Integer::New(script->line_offset()->value()),
2785 v8::Integer::New(script->column_offset()->value()));
2786 return origin;
2787 }
2788 return v8::ScriptOrigin(Handle<Value>());
2789}
2790
2791
2792const int Function::kLineOffsetNotFound = -1;
2793
2794
2795int Function::GetScriptLineNumber() const {
2796 i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
2797 if (func->shared()->script()->IsScript()) {
2798 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
2799 return i::GetScriptLineNumber(script, func->shared()->start_position());
2800 }
2801 return kLineOffsetNotFound;
2802}
2803
2804
2805namespace {
2806
2807// Tracks string usage to help make better decisions when
2808// externalizing strings.
2809//
2810// Implementation note: internally this class only tracks fresh
2811// strings and keeps a single use counter for them.
2812class StringTracker {
2813 public:
2814 // Records that the given string's characters were copied to some
2815 // external buffer. If this happens often we should honor
2816 // externalization requests for the string.
2817 static void RecordWrite(i::Handle<i::String> string) {
2818 i::Address address = reinterpret_cast<i::Address>(*string);
2819 i::Address top = i::Heap::NewSpaceTop();
2820 if (IsFreshString(address, top)) {
2821 IncrementUseCount(top);
2822 }
2823 }
2824
2825 // Estimates freshness and use frequency of the given string based
2826 // on how close it is to the new space top and the recorded usage
2827 // history.
2828 static inline bool IsFreshUnusedString(i::Handle<i::String> string) {
2829 i::Address address = reinterpret_cast<i::Address>(*string);
2830 i::Address top = i::Heap::NewSpaceTop();
2831 return IsFreshString(address, top) && IsUseCountLow(top);
2832 }
2833
2834 private:
2835 static inline bool IsFreshString(i::Address string, i::Address top) {
2836 return top - kFreshnessLimit <= string && string <= top;
2837 }
2838
2839 static inline bool IsUseCountLow(i::Address top) {
2840 if (last_top_ != top) return true;
2841 return use_count_ < kUseLimit;
2842 }
2843
2844 static inline void IncrementUseCount(i::Address top) {
2845 if (last_top_ != top) {
2846 use_count_ = 0;
2847 last_top_ = top;
2848 }
2849 ++use_count_;
2850 }
2851
2852 // How close to the new space top a fresh string has to be.
2853 static const int kFreshnessLimit = 1024;
2854
2855 // The number of uses required to consider a string useful.
2856 static const int kUseLimit = 32;
2857
2858 // Single use counter shared by all fresh strings.
2859 static int use_count_;
2860
2861 // Last new space top when the use count above was valid.
2862 static i::Address last_top_;
2863};
2864
2865int StringTracker::use_count_ = 0;
2866i::Address StringTracker::last_top_ = NULL;
2867
2868} // namespace
2869
2870
Steve Blocka7e24c12009-10-30 11:49:00 +00002871int String::Length() const {
2872 if (IsDeadCheck("v8::String::Length()")) return 0;
2873 return Utils::OpenHandle(this)->length();
2874}
2875
2876
2877int String::Utf8Length() const {
2878 if (IsDeadCheck("v8::String::Utf8Length()")) return 0;
2879 return Utils::OpenHandle(this)->Utf8Length();
2880}
2881
2882
Steve Block6ded16b2010-05-10 14:33:55 +01002883int String::WriteUtf8(char* buffer,
2884 int capacity,
2885 int* nchars_ref,
2886 WriteHints hints) const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002887 if (IsDeadCheck("v8::String::WriteUtf8()")) return 0;
2888 LOG_API("String::WriteUtf8");
2889 ENTER_V8;
2890 i::Handle<i::String> str = Utils::OpenHandle(this);
Andrei Popescu402d9372010-02-26 13:31:12 +00002891 StringTracker::RecordWrite(str);
Steve Block6ded16b2010-05-10 14:33:55 +01002892 if (hints & HINT_MANY_WRITES_EXPECTED) {
2893 // Flatten the string for efficiency. This applies whether we are
2894 // using StringInputBuffer or Get(i) to access the characters.
2895 str->TryFlatten();
2896 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002897 write_input_buffer.Reset(0, *str);
2898 int len = str->length();
2899 // Encode the first K - 3 bytes directly into the buffer since we
2900 // know there's room for them. If no capacity is given we copy all
2901 // of them here.
2902 int fast_end = capacity - (unibrow::Utf8::kMaxEncodedSize - 1);
2903 int i;
2904 int pos = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01002905 int nchars = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00002906 for (i = 0; i < len && (capacity == -1 || pos < fast_end); i++) {
2907 i::uc32 c = write_input_buffer.GetNext();
2908 int written = unibrow::Utf8::Encode(buffer + pos, c);
2909 pos += written;
Steve Block6ded16b2010-05-10 14:33:55 +01002910 nchars++;
Steve Blocka7e24c12009-10-30 11:49:00 +00002911 }
2912 if (i < len) {
2913 // For the last characters we need to check the length for each one
2914 // because they may be longer than the remaining space in the
2915 // buffer.
2916 char intermediate[unibrow::Utf8::kMaxEncodedSize];
2917 for (; i < len && pos < capacity; i++) {
2918 i::uc32 c = write_input_buffer.GetNext();
2919 int written = unibrow::Utf8::Encode(intermediate, c);
2920 if (pos + written <= capacity) {
2921 for (int j = 0; j < written; j++)
2922 buffer[pos + j] = intermediate[j];
2923 pos += written;
Steve Block6ded16b2010-05-10 14:33:55 +01002924 nchars++;
Steve Blocka7e24c12009-10-30 11:49:00 +00002925 } else {
2926 // We've reached the end of the buffer
2927 break;
2928 }
2929 }
2930 }
Steve Block6ded16b2010-05-10 14:33:55 +01002931 if (nchars_ref != NULL) *nchars_ref = nchars;
Steve Blocka7e24c12009-10-30 11:49:00 +00002932 if (i == len && (capacity == -1 || pos < capacity))
2933 buffer[pos++] = '\0';
2934 return pos;
2935}
2936
2937
Steve Block6ded16b2010-05-10 14:33:55 +01002938int String::WriteAscii(char* buffer,
2939 int start,
2940 int length,
2941 WriteHints hints) const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002942 if (IsDeadCheck("v8::String::WriteAscii()")) return 0;
2943 LOG_API("String::WriteAscii");
2944 ENTER_V8;
2945 ASSERT(start >= 0 && length >= -1);
2946 i::Handle<i::String> str = Utils::OpenHandle(this);
Andrei Popescu402d9372010-02-26 13:31:12 +00002947 StringTracker::RecordWrite(str);
Steve Block6ded16b2010-05-10 14:33:55 +01002948 if (hints & HINT_MANY_WRITES_EXPECTED) {
2949 // Flatten the string for efficiency. This applies whether we are
2950 // using StringInputBuffer or Get(i) to access the characters.
2951 str->TryFlatten();
2952 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002953 int end = length;
2954 if ( (length == -1) || (length > str->length() - start) )
2955 end = str->length() - start;
2956 if (end < 0) return 0;
2957 write_input_buffer.Reset(start, *str);
2958 int i;
2959 for (i = 0; i < end; i++) {
2960 char c = static_cast<char>(write_input_buffer.GetNext());
2961 if (c == '\0') c = ' ';
2962 buffer[i] = c;
2963 }
2964 if (length == -1 || i < length)
2965 buffer[i] = '\0';
2966 return i;
2967}
2968
2969
Steve Block6ded16b2010-05-10 14:33:55 +01002970int String::Write(uint16_t* buffer,
2971 int start,
2972 int length,
2973 WriteHints hints) const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002974 if (IsDeadCheck("v8::String::Write()")) return 0;
2975 LOG_API("String::Write");
2976 ENTER_V8;
2977 ASSERT(start >= 0 && length >= -1);
2978 i::Handle<i::String> str = Utils::OpenHandle(this);
Andrei Popescu402d9372010-02-26 13:31:12 +00002979 StringTracker::RecordWrite(str);
Steve Block6ded16b2010-05-10 14:33:55 +01002980 if (hints & HINT_MANY_WRITES_EXPECTED) {
2981 // Flatten the string for efficiency. This applies whether we are
2982 // using StringInputBuffer or Get(i) to access the characters.
2983 str->TryFlatten();
2984 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002985 int end = length;
2986 if ( (length == -1) || (length > str->length() - start) )
2987 end = str->length() - start;
2988 if (end < 0) return 0;
2989 i::String::WriteToFlat(*str, buffer, start, end);
2990 if (length == -1 || end < length)
2991 buffer[end] = '\0';
2992 return end;
2993}
2994
2995
2996bool v8::String::IsExternal() const {
2997 EnsureInitialized("v8::String::IsExternal()");
2998 i::Handle<i::String> str = Utils::OpenHandle(this);
2999 return i::StringShape(*str).IsExternalTwoByte();
3000}
3001
3002
3003bool v8::String::IsExternalAscii() const {
3004 EnsureInitialized("v8::String::IsExternalAscii()");
3005 i::Handle<i::String> str = Utils::OpenHandle(this);
3006 return i::StringShape(*str).IsExternalAscii();
3007}
3008
3009
3010void v8::String::VerifyExternalStringResource(
3011 v8::String::ExternalStringResource* value) const {
3012 i::Handle<i::String> str = Utils::OpenHandle(this);
3013 v8::String::ExternalStringResource* expected;
3014 if (i::StringShape(*str).IsExternalTwoByte()) {
3015 void* resource = i::Handle<i::ExternalTwoByteString>::cast(str)->resource();
3016 expected = reinterpret_cast<ExternalStringResource*>(resource);
3017 } else {
3018 expected = NULL;
3019 }
3020 CHECK_EQ(expected, value);
3021}
3022
3023
3024v8::String::ExternalAsciiStringResource*
3025 v8::String::GetExternalAsciiStringResource() const {
3026 EnsureInitialized("v8::String::GetExternalAsciiStringResource()");
3027 i::Handle<i::String> str = Utils::OpenHandle(this);
3028 if (i::StringShape(*str).IsExternalAscii()) {
3029 void* resource = i::Handle<i::ExternalAsciiString>::cast(str)->resource();
3030 return reinterpret_cast<ExternalAsciiStringResource*>(resource);
3031 } else {
3032 return NULL;
3033 }
3034}
3035
3036
3037double Number::Value() const {
3038 if (IsDeadCheck("v8::Number::Value()")) return 0;
3039 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3040 return obj->Number();
3041}
3042
3043
3044bool Boolean::Value() const {
3045 if (IsDeadCheck("v8::Boolean::Value()")) return false;
3046 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3047 return obj->IsTrue();
3048}
3049
3050
3051int64_t Integer::Value() const {
3052 if (IsDeadCheck("v8::Integer::Value()")) return 0;
3053 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3054 if (obj->IsSmi()) {
3055 return i::Smi::cast(*obj)->value();
3056 } else {
3057 return static_cast<int64_t>(obj->Number());
3058 }
3059}
3060
3061
3062int32_t Int32::Value() const {
3063 if (IsDeadCheck("v8::Int32::Value()")) return 0;
3064 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3065 if (obj->IsSmi()) {
3066 return i::Smi::cast(*obj)->value();
3067 } else {
3068 return static_cast<int32_t>(obj->Number());
3069 }
3070}
3071
3072
Steve Block6ded16b2010-05-10 14:33:55 +01003073uint32_t Uint32::Value() const {
3074 if (IsDeadCheck("v8::Uint32::Value()")) return 0;
3075 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3076 if (obj->IsSmi()) {
3077 return i::Smi::cast(*obj)->value();
3078 } else {
3079 return static_cast<uint32_t>(obj->Number());
3080 }
3081}
3082
3083
Steve Blocka7e24c12009-10-30 11:49:00 +00003084int v8::Object::InternalFieldCount() {
3085 if (IsDeadCheck("v8::Object::InternalFieldCount()")) return 0;
3086 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3087 return obj->GetInternalFieldCount();
3088}
3089
3090
3091Local<Value> v8::Object::CheckedGetInternalField(int index) {
3092 if (IsDeadCheck("v8::Object::GetInternalField()")) return Local<Value>();
3093 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3094 if (!ApiCheck(index < obj->GetInternalFieldCount(),
3095 "v8::Object::GetInternalField()",
3096 "Reading internal field out of bounds")) {
3097 return Local<Value>();
3098 }
3099 i::Handle<i::Object> value(obj->GetInternalField(index));
3100 Local<Value> result = Utils::ToLocal(value);
3101#ifdef DEBUG
3102 Local<Value> unchecked = UncheckedGetInternalField(index);
3103 ASSERT(unchecked.IsEmpty() || (unchecked == result));
3104#endif
3105 return result;
3106}
3107
3108
3109void v8::Object::SetInternalField(int index, v8::Handle<Value> value) {
3110 if (IsDeadCheck("v8::Object::SetInternalField()")) return;
3111 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3112 if (!ApiCheck(index < obj->GetInternalFieldCount(),
3113 "v8::Object::SetInternalField()",
3114 "Writing internal field out of bounds")) {
3115 return;
3116 }
3117 ENTER_V8;
3118 i::Handle<i::Object> val = Utils::OpenHandle(*value);
3119 obj->SetInternalField(index, *val);
3120}
3121
3122
3123void v8::Object::SetPointerInInternalField(int index, void* value) {
Steve Block6ded16b2010-05-10 14:33:55 +01003124 ENTER_V8;
Steve Block3ce2e202009-11-05 08:53:23 +00003125 i::Object* as_object = reinterpret_cast<i::Object*>(value);
3126 if (as_object->IsSmi()) {
3127 Utils::OpenHandle(this)->SetInternalField(index, as_object);
3128 return;
3129 }
3130 HandleScope scope;
3131 i::Handle<i::Proxy> proxy =
3132 i::Factory::NewProxy(reinterpret_cast<i::Address>(value), i::TENURED);
3133 if (!proxy.is_null())
3134 Utils::OpenHandle(this)->SetInternalField(index, *proxy);
Steve Blocka7e24c12009-10-30 11:49:00 +00003135}
3136
3137
3138// --- E n v i r o n m e n t ---
3139
3140bool v8::V8::Initialize() {
3141 if (i::V8::IsRunning()) return true;
3142 ENTER_V8;
3143 HandleScope scope;
Steve Blockd0582a62009-12-15 09:54:21 +00003144 if (i::Snapshot::Initialize()) return true;
3145 return i::V8::Initialize(NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +00003146}
3147
3148
3149bool v8::V8::Dispose() {
3150 i::V8::TearDown();
3151 return true;
3152}
3153
3154
Steve Block3ce2e202009-11-05 08:53:23 +00003155HeapStatistics::HeapStatistics(): total_heap_size_(0), used_heap_size_(0) { }
3156
3157
3158void v8::V8::GetHeapStatistics(HeapStatistics* heap_statistics) {
3159 heap_statistics->set_total_heap_size(i::Heap::CommittedMemory());
3160 heap_statistics->set_used_heap_size(i::Heap::SizeOfObjects());
3161}
3162
3163
3164bool v8::V8::IdleNotification() {
Steve Blocka7e24c12009-10-30 11:49:00 +00003165 // Returning true tells the caller that it need not
3166 // continue to call IdleNotification.
3167 if (!i::V8::IsRunning()) return true;
Steve Block3ce2e202009-11-05 08:53:23 +00003168 return i::V8::IdleNotification();
Steve Blocka7e24c12009-10-30 11:49:00 +00003169}
3170
3171
3172void v8::V8::LowMemoryNotification() {
Steve Blocka7e24c12009-10-30 11:49:00 +00003173 if (!i::V8::IsRunning()) return;
3174 i::Heap::CollectAllGarbage(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00003175}
3176
3177
Steve Block6ded16b2010-05-10 14:33:55 +01003178int v8::V8::ContextDisposedNotification() {
3179 if (!i::V8::IsRunning()) return 0;
3180 return i::Heap::NotifyContextDisposed();
3181}
3182
3183
Steve Blocka7e24c12009-10-30 11:49:00 +00003184const char* v8::V8::GetVersion() {
3185 static v8::internal::EmbeddedVector<char, 128> buffer;
3186 v8::internal::Version::GetString(buffer);
3187 return buffer.start();
3188}
3189
3190
3191static i::Handle<i::FunctionTemplateInfo>
3192 EnsureConstructor(i::Handle<i::ObjectTemplateInfo> templ) {
3193 if (templ->constructor()->IsUndefined()) {
3194 Local<FunctionTemplate> constructor = FunctionTemplate::New();
3195 Utils::OpenHandle(*constructor)->set_instance_template(*templ);
3196 templ->set_constructor(*Utils::OpenHandle(*constructor));
3197 }
3198 return i::Handle<i::FunctionTemplateInfo>(
3199 i::FunctionTemplateInfo::cast(templ->constructor()));
3200}
3201
3202
3203Persistent<Context> v8::Context::New(
3204 v8::ExtensionConfiguration* extensions,
3205 v8::Handle<ObjectTemplate> global_template,
3206 v8::Handle<Value> global_object) {
3207 EnsureInitialized("v8::Context::New()");
3208 LOG_API("Context::New");
3209 ON_BAILOUT("v8::Context::New()", return Persistent<Context>());
3210
3211 // Enter V8 via an ENTER_V8 scope.
3212 i::Handle<i::Context> env;
3213 {
3214 ENTER_V8;
Steve Blocka7e24c12009-10-30 11:49:00 +00003215 v8::Handle<ObjectTemplate> proxy_template = global_template;
3216 i::Handle<i::FunctionTemplateInfo> proxy_constructor;
3217 i::Handle<i::FunctionTemplateInfo> global_constructor;
3218
3219 if (!global_template.IsEmpty()) {
3220 // Make sure that the global_template has a constructor.
3221 global_constructor =
3222 EnsureConstructor(Utils::OpenHandle(*global_template));
3223
3224 // Create a fresh template for the global proxy object.
3225 proxy_template = ObjectTemplate::New();
3226 proxy_constructor =
3227 EnsureConstructor(Utils::OpenHandle(*proxy_template));
3228
3229 // Set the global template to be the prototype template of
3230 // global proxy template.
3231 proxy_constructor->set_prototype_template(
3232 *Utils::OpenHandle(*global_template));
3233
3234 // Migrate security handlers from global_template to
3235 // proxy_template. Temporarily removing access check
3236 // information from the global template.
3237 if (!global_constructor->access_check_info()->IsUndefined()) {
3238 proxy_constructor->set_access_check_info(
3239 global_constructor->access_check_info());
3240 proxy_constructor->set_needs_access_check(
3241 global_constructor->needs_access_check());
3242 global_constructor->set_needs_access_check(false);
3243 global_constructor->set_access_check_info(i::Heap::undefined_value());
3244 }
3245 }
3246
3247 // Create the environment.
3248 env = i::Bootstrapper::CreateEnvironment(
3249 Utils::OpenHandle(*global_object),
3250 proxy_template,
3251 extensions);
3252
3253 // Restore the access check info on the global template.
3254 if (!global_template.IsEmpty()) {
3255 ASSERT(!global_constructor.is_null());
3256 ASSERT(!proxy_constructor.is_null());
3257 global_constructor->set_access_check_info(
3258 proxy_constructor->access_check_info());
3259 global_constructor->set_needs_access_check(
3260 proxy_constructor->needs_access_check());
3261 }
3262 }
3263 // Leave V8.
3264
3265 if (env.is_null())
3266 return Persistent<Context>();
3267 return Persistent<Context>(Utils::ToLocal(env));
3268}
3269
3270
3271void v8::Context::SetSecurityToken(Handle<Value> token) {
3272 if (IsDeadCheck("v8::Context::SetSecurityToken()")) return;
3273 ENTER_V8;
3274 i::Handle<i::Context> env = Utils::OpenHandle(this);
3275 i::Handle<i::Object> token_handle = Utils::OpenHandle(*token);
3276 env->set_security_token(*token_handle);
3277}
3278
3279
3280void v8::Context::UseDefaultSecurityToken() {
3281 if (IsDeadCheck("v8::Context::UseDefaultSecurityToken()")) return;
3282 ENTER_V8;
3283 i::Handle<i::Context> env = Utils::OpenHandle(this);
3284 env->set_security_token(env->global());
3285}
3286
3287
3288Handle<Value> v8::Context::GetSecurityToken() {
3289 if (IsDeadCheck("v8::Context::GetSecurityToken()")) return Handle<Value>();
3290 i::Handle<i::Context> env = Utils::OpenHandle(this);
3291 i::Object* security_token = env->security_token();
3292 i::Handle<i::Object> token_handle(security_token);
3293 return Utils::ToLocal(token_handle);
3294}
3295
3296
3297bool Context::HasOutOfMemoryException() {
3298 i::Handle<i::Context> env = Utils::OpenHandle(this);
3299 return env->has_out_of_memory();
3300}
3301
3302
3303bool Context::InContext() {
3304 return i::Top::context() != NULL;
3305}
3306
3307
3308v8::Local<v8::Context> Context::GetEntered() {
3309 if (IsDeadCheck("v8::Context::GetEntered()")) return Local<Context>();
3310 i::Handle<i::Object> last = thread_local.LastEnteredContext();
3311 if (last.is_null()) return Local<Context>();
3312 i::Handle<i::Context> context = i::Handle<i::Context>::cast(last);
3313 return Utils::ToLocal(context);
3314}
3315
3316
3317v8::Local<v8::Context> Context::GetCurrent() {
3318 if (IsDeadCheck("v8::Context::GetCurrent()")) return Local<Context>();
Steve Block3ce2e202009-11-05 08:53:23 +00003319 i::Handle<i::Object> current = i::Top::global_context();
3320 if (current.is_null()) return Local<Context>();
3321 i::Handle<i::Context> context = i::Handle<i::Context>::cast(current);
Steve Blocka7e24c12009-10-30 11:49:00 +00003322 return Utils::ToLocal(context);
3323}
3324
3325
3326v8::Local<v8::Context> Context::GetCalling() {
3327 if (IsDeadCheck("v8::Context::GetCalling()")) return Local<Context>();
3328 i::Handle<i::Object> calling = i::Top::GetCallingGlobalContext();
3329 if (calling.is_null()) return Local<Context>();
3330 i::Handle<i::Context> context = i::Handle<i::Context>::cast(calling);
3331 return Utils::ToLocal(context);
3332}
3333
3334
3335v8::Local<v8::Object> Context::Global() {
3336 if (IsDeadCheck("v8::Context::Global()")) return Local<v8::Object>();
3337 i::Object** ctx = reinterpret_cast<i::Object**>(this);
3338 i::Handle<i::Context> context =
3339 i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx));
3340 i::Handle<i::Object> global(context->global_proxy());
3341 return Utils::ToLocal(i::Handle<i::JSObject>::cast(global));
3342}
3343
3344
3345void Context::DetachGlobal() {
3346 if (IsDeadCheck("v8::Context::DetachGlobal()")) return;
3347 ENTER_V8;
3348 i::Object** ctx = reinterpret_cast<i::Object**>(this);
3349 i::Handle<i::Context> context =
3350 i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx));
3351 i::Bootstrapper::DetachGlobal(context);
3352}
3353
3354
Andrei Popescu74b3c142010-03-29 12:03:09 +01003355void Context::ReattachGlobal(Handle<Object> global_object) {
3356 if (IsDeadCheck("v8::Context::ReattachGlobal()")) return;
3357 ENTER_V8;
3358 i::Object** ctx = reinterpret_cast<i::Object**>(this);
3359 i::Handle<i::Context> context =
3360 i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx));
3361 i::Bootstrapper::ReattachGlobal(context, Utils::OpenHandle(*global_object));
3362}
3363
3364
Steve Blocka7e24c12009-10-30 11:49:00 +00003365Local<v8::Object> ObjectTemplate::NewInstance() {
3366 ON_BAILOUT("v8::ObjectTemplate::NewInstance()", return Local<v8::Object>());
3367 LOG_API("ObjectTemplate::NewInstance");
3368 ENTER_V8;
3369 EXCEPTION_PREAMBLE();
3370 i::Handle<i::Object> obj =
3371 i::Execution::InstantiateObject(Utils::OpenHandle(this),
3372 &has_pending_exception);
3373 EXCEPTION_BAILOUT_CHECK(Local<v8::Object>());
3374 return Utils::ToLocal(i::Handle<i::JSObject>::cast(obj));
3375}
3376
3377
3378Local<v8::Function> FunctionTemplate::GetFunction() {
3379 ON_BAILOUT("v8::FunctionTemplate::GetFunction()",
3380 return Local<v8::Function>());
3381 LOG_API("FunctionTemplate::GetFunction");
3382 ENTER_V8;
3383 EXCEPTION_PREAMBLE();
3384 i::Handle<i::Object> obj =
3385 i::Execution::InstantiateFunction(Utils::OpenHandle(this),
3386 &has_pending_exception);
3387 EXCEPTION_BAILOUT_CHECK(Local<v8::Function>());
3388 return Utils::ToLocal(i::Handle<i::JSFunction>::cast(obj));
3389}
3390
3391
3392bool FunctionTemplate::HasInstance(v8::Handle<v8::Value> value) {
3393 ON_BAILOUT("v8::FunctionTemplate::HasInstanceOf()", return false);
3394 i::Object* obj = *Utils::OpenHandle(*value);
3395 return obj->IsInstanceOf(*Utils::OpenHandle(this));
3396}
3397
3398
3399static Local<External> ExternalNewImpl(void* data) {
3400 return Utils::ToLocal(i::Factory::NewProxy(static_cast<i::Address>(data)));
3401}
3402
3403static void* ExternalValueImpl(i::Handle<i::Object> obj) {
3404 return reinterpret_cast<void*>(i::Proxy::cast(*obj)->proxy());
3405}
3406
3407
Steve Blocka7e24c12009-10-30 11:49:00 +00003408Local<Value> v8::External::Wrap(void* data) {
3409 STATIC_ASSERT(sizeof(data) == sizeof(i::Address));
3410 LOG_API("External::Wrap");
3411 EnsureInitialized("v8::External::Wrap()");
3412 ENTER_V8;
Steve Block3ce2e202009-11-05 08:53:23 +00003413 i::Object* as_object = reinterpret_cast<i::Object*>(data);
3414 if (as_object->IsSmi()) {
3415 return Utils::ToLocal(i::Handle<i::Object>(as_object));
Steve Blocka7e24c12009-10-30 11:49:00 +00003416 }
3417 return ExternalNewImpl(data);
3418}
3419
3420
Steve Block3ce2e202009-11-05 08:53:23 +00003421void* v8::Object::SlowGetPointerFromInternalField(int index) {
3422 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3423 i::Object* value = obj->GetInternalField(index);
3424 if (value->IsSmi()) {
3425 return value;
3426 } else if (value->IsProxy()) {
3427 return reinterpret_cast<void*>(i::Proxy::cast(value)->proxy());
3428 } else {
3429 return NULL;
3430 }
3431}
3432
3433
Steve Blocka7e24c12009-10-30 11:49:00 +00003434void* v8::External::FullUnwrap(v8::Handle<v8::Value> wrapper) {
3435 if (IsDeadCheck("v8::External::Unwrap()")) return 0;
3436 i::Handle<i::Object> obj = Utils::OpenHandle(*wrapper);
3437 void* result;
3438 if (obj->IsSmi()) {
3439 // The external value was an aligned pointer.
Steve Block3ce2e202009-11-05 08:53:23 +00003440 result = *obj;
Steve Blocka7e24c12009-10-30 11:49:00 +00003441 } else if (obj->IsProxy()) {
3442 result = ExternalValueImpl(obj);
3443 } else {
3444 result = NULL;
3445 }
3446 ASSERT_EQ(result, QuickUnwrap(wrapper));
3447 return result;
3448}
3449
3450
3451Local<External> v8::External::New(void* data) {
3452 STATIC_ASSERT(sizeof(data) == sizeof(i::Address));
3453 LOG_API("External::New");
3454 EnsureInitialized("v8::External::New()");
3455 ENTER_V8;
3456 return ExternalNewImpl(data);
3457}
3458
3459
3460void* External::Value() const {
3461 if (IsDeadCheck("v8::External::Value()")) return 0;
3462 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3463 return ExternalValueImpl(obj);
3464}
3465
3466
3467Local<String> v8::String::Empty() {
3468 EnsureInitialized("v8::String::Empty()");
3469 LOG_API("String::Empty()");
3470 return Utils::ToLocal(i::Factory::empty_symbol());
3471}
3472
3473
3474Local<String> v8::String::New(const char* data, int length) {
3475 EnsureInitialized("v8::String::New()");
3476 LOG_API("String::New(char)");
3477 if (length == 0) return Empty();
3478 ENTER_V8;
Steve Blockd0582a62009-12-15 09:54:21 +00003479 if (length == -1) length = i::StrLength(data);
Steve Blocka7e24c12009-10-30 11:49:00 +00003480 i::Handle<i::String> result =
3481 i::Factory::NewStringFromUtf8(i::Vector<const char>(data, length));
3482 return Utils::ToLocal(result);
3483}
3484
3485
Steve Block3ce2e202009-11-05 08:53:23 +00003486Local<String> v8::String::Concat(Handle<String> left, Handle<String> right) {
3487 EnsureInitialized("v8::String::New()");
3488 LOG_API("String::New(char)");
3489 ENTER_V8;
3490 i::Handle<i::String> left_string = Utils::OpenHandle(*left);
3491 i::Handle<i::String> right_string = Utils::OpenHandle(*right);
3492 i::Handle<i::String> result = i::Factory::NewConsString(left_string,
3493 right_string);
3494 return Utils::ToLocal(result);
3495}
3496
3497
Steve Blocka7e24c12009-10-30 11:49:00 +00003498Local<String> v8::String::NewUndetectable(const char* data, int length) {
3499 EnsureInitialized("v8::String::NewUndetectable()");
3500 LOG_API("String::NewUndetectable(char)");
3501 ENTER_V8;
Steve Blockd0582a62009-12-15 09:54:21 +00003502 if (length == -1) length = i::StrLength(data);
Steve Blocka7e24c12009-10-30 11:49:00 +00003503 i::Handle<i::String> result =
3504 i::Factory::NewStringFromUtf8(i::Vector<const char>(data, length));
3505 result->MarkAsUndetectable();
3506 return Utils::ToLocal(result);
3507}
3508
3509
3510static int TwoByteStringLength(const uint16_t* data) {
3511 int length = 0;
3512 while (data[length] != '\0') length++;
3513 return length;
3514}
3515
3516
3517Local<String> v8::String::New(const uint16_t* data, int length) {
3518 EnsureInitialized("v8::String::New()");
3519 LOG_API("String::New(uint16_)");
3520 if (length == 0) return Empty();
3521 ENTER_V8;
3522 if (length == -1) length = TwoByteStringLength(data);
3523 i::Handle<i::String> result =
3524 i::Factory::NewStringFromTwoByte(i::Vector<const uint16_t>(data, length));
3525 return Utils::ToLocal(result);
3526}
3527
3528
3529Local<String> v8::String::NewUndetectable(const uint16_t* data, int length) {
3530 EnsureInitialized("v8::String::NewUndetectable()");
3531 LOG_API("String::NewUndetectable(uint16_)");
3532 ENTER_V8;
3533 if (length == -1) length = TwoByteStringLength(data);
3534 i::Handle<i::String> result =
3535 i::Factory::NewStringFromTwoByte(i::Vector<const uint16_t>(data, length));
3536 result->MarkAsUndetectable();
3537 return Utils::ToLocal(result);
3538}
3539
3540
3541i::Handle<i::String> NewExternalStringHandle(
3542 v8::String::ExternalStringResource* resource) {
3543 i::Handle<i::String> result =
3544 i::Factory::NewExternalStringFromTwoByte(resource);
3545 return result;
3546}
3547
3548
3549i::Handle<i::String> NewExternalAsciiStringHandle(
3550 v8::String::ExternalAsciiStringResource* resource) {
3551 i::Handle<i::String> result =
3552 i::Factory::NewExternalStringFromAscii(resource);
3553 return result;
3554}
3555
3556
Steve Blocka7e24c12009-10-30 11:49:00 +00003557Local<String> v8::String::NewExternal(
3558 v8::String::ExternalStringResource* resource) {
3559 EnsureInitialized("v8::String::NewExternal()");
3560 LOG_API("String::NewExternal");
3561 ENTER_V8;
Steve Blocka7e24c12009-10-30 11:49:00 +00003562 i::Handle<i::String> result = NewExternalStringHandle(resource);
Leon Clarkee46be812010-01-19 14:06:41 +00003563 i::ExternalStringTable::AddString(*result);
Steve Blocka7e24c12009-10-30 11:49:00 +00003564 return Utils::ToLocal(result);
3565}
3566
3567
3568bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) {
3569 if (IsDeadCheck("v8::String::MakeExternal()")) return false;
3570 if (this->IsExternal()) return false; // Already an external string.
3571 ENTER_V8;
3572 i::Handle<i::String> obj = Utils::OpenHandle(this);
Andrei Popescu402d9372010-02-26 13:31:12 +00003573 if (StringTracker::IsFreshUnusedString(obj)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00003574 bool result = obj->MakeExternal(resource);
3575 if (result && !obj->IsSymbol()) {
Leon Clarkee46be812010-01-19 14:06:41 +00003576 i::ExternalStringTable::AddString(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003577 }
3578 return result;
3579}
3580
3581
3582Local<String> v8::String::NewExternal(
3583 v8::String::ExternalAsciiStringResource* resource) {
3584 EnsureInitialized("v8::String::NewExternal()");
3585 LOG_API("String::NewExternal");
3586 ENTER_V8;
Steve Blocka7e24c12009-10-30 11:49:00 +00003587 i::Handle<i::String> result = NewExternalAsciiStringHandle(resource);
Leon Clarkee46be812010-01-19 14:06:41 +00003588 i::ExternalStringTable::AddString(*result);
Steve Blocka7e24c12009-10-30 11:49:00 +00003589 return Utils::ToLocal(result);
3590}
3591
3592
3593bool v8::String::MakeExternal(
3594 v8::String::ExternalAsciiStringResource* resource) {
3595 if (IsDeadCheck("v8::String::MakeExternal()")) return false;
3596 if (this->IsExternal()) return false; // Already an external string.
3597 ENTER_V8;
3598 i::Handle<i::String> obj = Utils::OpenHandle(this);
Andrei Popescu402d9372010-02-26 13:31:12 +00003599 if (StringTracker::IsFreshUnusedString(obj)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00003600 bool result = obj->MakeExternal(resource);
3601 if (result && !obj->IsSymbol()) {
Leon Clarkee46be812010-01-19 14:06:41 +00003602 i::ExternalStringTable::AddString(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003603 }
3604 return result;
3605}
3606
3607
3608bool v8::String::CanMakeExternal() {
3609 if (IsDeadCheck("v8::String::CanMakeExternal()")) return false;
3610 i::Handle<i::String> obj = Utils::OpenHandle(this);
Andrei Popescu402d9372010-02-26 13:31:12 +00003611 if (StringTracker::IsFreshUnusedString(obj)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00003612 int size = obj->Size(); // Byte size of the original string.
3613 if (size < i::ExternalString::kSize)
3614 return false;
3615 i::StringShape shape(*obj);
3616 return !shape.IsExternal();
3617}
3618
3619
3620Local<v8::Object> v8::Object::New() {
3621 EnsureInitialized("v8::Object::New()");
3622 LOG_API("Object::New");
3623 ENTER_V8;
3624 i::Handle<i::JSObject> obj =
3625 i::Factory::NewJSObject(i::Top::object_function());
3626 return Utils::ToLocal(obj);
3627}
3628
3629
3630Local<v8::Value> v8::Date::New(double time) {
3631 EnsureInitialized("v8::Date::New()");
3632 LOG_API("Date::New");
Steve Blockd0582a62009-12-15 09:54:21 +00003633 if (isnan(time)) {
3634 // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
3635 time = i::OS::nan_value();
3636 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003637 ENTER_V8;
3638 EXCEPTION_PREAMBLE();
3639 i::Handle<i::Object> obj =
3640 i::Execution::NewDate(time, &has_pending_exception);
3641 EXCEPTION_BAILOUT_CHECK(Local<v8::Value>());
3642 return Utils::ToLocal(obj);
3643}
3644
3645
3646double v8::Date::NumberValue() const {
3647 if (IsDeadCheck("v8::Date::NumberValue()")) return 0;
3648 LOG_API("Date::NumberValue");
3649 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3650 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
3651 return jsvalue->value()->Number();
3652}
3653
3654
3655Local<v8::Array> v8::Array::New(int length) {
3656 EnsureInitialized("v8::Array::New()");
3657 LOG_API("Array::New");
3658 ENTER_V8;
3659 i::Handle<i::JSArray> obj = i::Factory::NewJSArray(length);
3660 return Utils::ToLocal(obj);
3661}
3662
3663
3664uint32_t v8::Array::Length() const {
3665 if (IsDeadCheck("v8::Array::Length()")) return 0;
3666 i::Handle<i::JSArray> obj = Utils::OpenHandle(this);
3667 i::Object* length = obj->length();
3668 if (length->IsSmi()) {
3669 return i::Smi::cast(length)->value();
3670 } else {
3671 return static_cast<uint32_t>(length->Number());
3672 }
3673}
3674
3675
3676Local<Object> Array::CloneElementAt(uint32_t index) {
3677 ON_BAILOUT("v8::Array::CloneElementAt()", return Local<Object>());
3678 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3679 if (!self->HasFastElements()) {
3680 return Local<Object>();
3681 }
3682 i::FixedArray* elms = i::FixedArray::cast(self->elements());
3683 i::Object* paragon = elms->get(index);
3684 if (!paragon->IsJSObject()) {
3685 return Local<Object>();
3686 }
3687 i::Handle<i::JSObject> paragon_handle(i::JSObject::cast(paragon));
3688 EXCEPTION_PREAMBLE();
Steve Block6ded16b2010-05-10 14:33:55 +01003689 ENTER_V8;
Steve Blocka7e24c12009-10-30 11:49:00 +00003690 i::Handle<i::JSObject> result = i::Copy(paragon_handle);
3691 has_pending_exception = result.is_null();
3692 EXCEPTION_BAILOUT_CHECK(Local<Object>());
3693 return Utils::ToLocal(result);
3694}
3695
3696
3697Local<String> v8::String::NewSymbol(const char* data, int length) {
3698 EnsureInitialized("v8::String::NewSymbol()");
3699 LOG_API("String::NewSymbol(char)");
3700 ENTER_V8;
Steve Blockd0582a62009-12-15 09:54:21 +00003701 if (length == -1) length = i::StrLength(data);
Steve Blocka7e24c12009-10-30 11:49:00 +00003702 i::Handle<i::String> result =
3703 i::Factory::LookupSymbol(i::Vector<const char>(data, length));
3704 return Utils::ToLocal(result);
3705}
3706
3707
3708Local<Number> v8::Number::New(double value) {
3709 EnsureInitialized("v8::Number::New()");
Steve Blockd0582a62009-12-15 09:54:21 +00003710 if (isnan(value)) {
3711 // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
3712 value = i::OS::nan_value();
3713 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003714 ENTER_V8;
3715 i::Handle<i::Object> result = i::Factory::NewNumber(value);
3716 return Utils::NumberToLocal(result);
3717}
3718
3719
3720Local<Integer> v8::Integer::New(int32_t value) {
3721 EnsureInitialized("v8::Integer::New()");
3722 if (i::Smi::IsValid(value)) {
3723 return Utils::IntegerToLocal(i::Handle<i::Object>(i::Smi::FromInt(value)));
3724 }
3725 ENTER_V8;
3726 i::Handle<i::Object> result = i::Factory::NewNumber(value);
3727 return Utils::IntegerToLocal(result);
3728}
3729
3730
Steve Block3ce2e202009-11-05 08:53:23 +00003731Local<Integer> Integer::NewFromUnsigned(uint32_t value) {
3732 bool fits_into_int32_t = (value & (1 << 31)) == 0;
3733 if (fits_into_int32_t) {
3734 return Integer::New(static_cast<int32_t>(value));
3735 }
3736 ENTER_V8;
3737 i::Handle<i::Object> result = i::Factory::NewNumber(value);
3738 return Utils::IntegerToLocal(result);
3739}
3740
3741
Steve Blocka7e24c12009-10-30 11:49:00 +00003742void V8::IgnoreOutOfMemoryException() {
3743 thread_local.set_ignore_out_of_memory(true);
3744}
3745
3746
3747bool V8::AddMessageListener(MessageCallback that, Handle<Value> data) {
3748 EnsureInitialized("v8::V8::AddMessageListener()");
3749 ON_BAILOUT("v8::V8::AddMessageListener()", return false);
3750 ENTER_V8;
3751 HandleScope scope;
3752 NeanderArray listeners(i::Factory::message_listeners());
3753 NeanderObject obj(2);
3754 obj.set(0, *i::Factory::NewProxy(FUNCTION_ADDR(that)));
3755 obj.set(1, data.IsEmpty() ?
3756 i::Heap::undefined_value() :
3757 *Utils::OpenHandle(*data));
3758 listeners.add(obj.value());
3759 return true;
3760}
3761
3762
3763void V8::RemoveMessageListeners(MessageCallback that) {
3764 EnsureInitialized("v8::V8::RemoveMessageListener()");
3765 ON_BAILOUT("v8::V8::RemoveMessageListeners()", return);
3766 ENTER_V8;
3767 HandleScope scope;
3768 NeanderArray listeners(i::Factory::message_listeners());
3769 for (int i = 0; i < listeners.length(); i++) {
3770 if (listeners.get(i)->IsUndefined()) continue; // skip deleted ones
3771
3772 NeanderObject listener(i::JSObject::cast(listeners.get(i)));
3773 i::Handle<i::Proxy> callback_obj(i::Proxy::cast(listener.get(0)));
3774 if (callback_obj->proxy() == FUNCTION_ADDR(that)) {
3775 listeners.set(i, i::Heap::undefined_value());
3776 }
3777 }
3778}
3779
3780
3781void V8::SetCounterFunction(CounterLookupCallback callback) {
3782 if (IsDeadCheck("v8::V8::SetCounterFunction()")) return;
3783 i::StatsTable::SetCounterFunction(callback);
3784}
3785
3786void V8::SetCreateHistogramFunction(CreateHistogramCallback callback) {
3787 if (IsDeadCheck("v8::V8::SetCreateHistogramFunction()")) return;
3788 i::StatsTable::SetCreateHistogramFunction(callback);
3789}
3790
3791void V8::SetAddHistogramSampleFunction(AddHistogramSampleCallback callback) {
3792 if (IsDeadCheck("v8::V8::SetAddHistogramSampleFunction()")) return;
3793 i::StatsTable::SetAddHistogramSampleFunction(callback);
3794}
3795
3796void V8::EnableSlidingStateWindow() {
3797 if (IsDeadCheck("v8::V8::EnableSlidingStateWindow()")) return;
3798 i::Logger::EnableSlidingStateWindow();
3799}
3800
3801
3802void V8::SetFailedAccessCheckCallbackFunction(
3803 FailedAccessCheckCallback callback) {
3804 if (IsDeadCheck("v8::V8::SetFailedAccessCheckCallbackFunction()")) return;
3805 i::Top::SetFailedAccessCheckCallback(callback);
3806}
3807
3808
3809void V8::AddObjectGroup(Persistent<Value>* objects, size_t length) {
3810 if (IsDeadCheck("v8::V8::AddObjectGroup()")) return;
3811 STATIC_ASSERT(sizeof(Persistent<Value>) == sizeof(i::Object**));
3812 i::GlobalHandles::AddGroup(reinterpret_cast<i::Object***>(objects), length);
3813}
3814
3815
3816int V8::AdjustAmountOfExternalAllocatedMemory(int change_in_bytes) {
3817 if (IsDeadCheck("v8::V8::AdjustAmountOfExternalAllocatedMemory()")) return 0;
3818 return i::Heap::AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
3819}
3820
3821
3822void V8::SetGlobalGCPrologueCallback(GCCallback callback) {
3823 if (IsDeadCheck("v8::V8::SetGlobalGCPrologueCallback()")) return;
3824 i::Heap::SetGlobalGCPrologueCallback(callback);
3825}
3826
3827
3828void V8::SetGlobalGCEpilogueCallback(GCCallback callback) {
3829 if (IsDeadCheck("v8::V8::SetGlobalGCEpilogueCallback()")) return;
3830 i::Heap::SetGlobalGCEpilogueCallback(callback);
3831}
3832
3833
Steve Block6ded16b2010-05-10 14:33:55 +01003834void V8::AddGCPrologueCallback(GCPrologueCallback callback, GCType gc_type) {
3835 if (IsDeadCheck("v8::V8::AddGCPrologueCallback()")) return;
3836 i::Heap::AddGCPrologueCallback(callback, gc_type);
3837}
3838
3839
3840void V8::RemoveGCPrologueCallback(GCPrologueCallback callback) {
3841 if (IsDeadCheck("v8::V8::RemoveGCPrologueCallback()")) return;
3842 i::Heap::RemoveGCPrologueCallback(callback);
3843}
3844
3845
3846void V8::AddGCEpilogueCallback(GCEpilogueCallback callback, GCType gc_type) {
3847 if (IsDeadCheck("v8::V8::AddGCEpilogueCallback()")) return;
3848 i::Heap::AddGCEpilogueCallback(callback, gc_type);
3849}
3850
3851
3852void V8::RemoveGCEpilogueCallback(GCEpilogueCallback callback) {
3853 if (IsDeadCheck("v8::V8::RemoveGCEpilogueCallback()")) return;
3854 i::Heap::RemoveGCEpilogueCallback(callback);
3855}
3856
3857
Steve Blocka7e24c12009-10-30 11:49:00 +00003858void V8::PauseProfiler() {
3859#ifdef ENABLE_LOGGING_AND_PROFILING
Andrei Popescu402d9372010-02-26 13:31:12 +00003860 PauseProfilerEx(PROFILER_MODULE_CPU);
Steve Blocka7e24c12009-10-30 11:49:00 +00003861#endif
3862}
3863
3864
3865void V8::ResumeProfiler() {
3866#ifdef ENABLE_LOGGING_AND_PROFILING
Andrei Popescu402d9372010-02-26 13:31:12 +00003867 ResumeProfilerEx(PROFILER_MODULE_CPU);
Steve Blocka7e24c12009-10-30 11:49:00 +00003868#endif
3869}
3870
3871
3872bool V8::IsProfilerPaused() {
3873#ifdef ENABLE_LOGGING_AND_PROFILING
3874 return i::Logger::GetActiveProfilerModules() & PROFILER_MODULE_CPU;
3875#else
3876 return true;
3877#endif
3878}
3879
3880
Andrei Popescu402d9372010-02-26 13:31:12 +00003881void V8::ResumeProfilerEx(int flags, int tag) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003882#ifdef ENABLE_LOGGING_AND_PROFILING
3883 if (flags & PROFILER_MODULE_HEAP_SNAPSHOT) {
3884 // Snapshot mode: resume modules, perform GC, then pause only
3885 // those modules which haven't been started prior to making a
3886 // snapshot.
3887
Steve Block6ded16b2010-05-10 14:33:55 +01003888 // Make a GC prior to taking a snapshot.
3889 i::Heap::CollectAllGarbage(false);
Steve Blocka7e24c12009-10-30 11:49:00 +00003890 // Reset snapshot flag and CPU module flags.
3891 flags &= ~(PROFILER_MODULE_HEAP_SNAPSHOT | PROFILER_MODULE_CPU);
3892 const int current_flags = i::Logger::GetActiveProfilerModules();
Andrei Popescu402d9372010-02-26 13:31:12 +00003893 i::Logger::ResumeProfiler(flags, tag);
Steve Blocka7e24c12009-10-30 11:49:00 +00003894 i::Heap::CollectAllGarbage(false);
Andrei Popescu402d9372010-02-26 13:31:12 +00003895 i::Logger::PauseProfiler(~current_flags & flags, tag);
Steve Blocka7e24c12009-10-30 11:49:00 +00003896 } else {
Andrei Popescu402d9372010-02-26 13:31:12 +00003897 i::Logger::ResumeProfiler(flags, tag);
Steve Blocka7e24c12009-10-30 11:49:00 +00003898 }
3899#endif
3900}
3901
3902
Andrei Popescu402d9372010-02-26 13:31:12 +00003903void V8::PauseProfilerEx(int flags, int tag) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003904#ifdef ENABLE_LOGGING_AND_PROFILING
Andrei Popescu402d9372010-02-26 13:31:12 +00003905 i::Logger::PauseProfiler(flags, tag);
Steve Blocka7e24c12009-10-30 11:49:00 +00003906#endif
3907}
3908
3909
3910int V8::GetActiveProfilerModules() {
3911#ifdef ENABLE_LOGGING_AND_PROFILING
3912 return i::Logger::GetActiveProfilerModules();
3913#else
3914 return PROFILER_MODULE_NONE;
3915#endif
3916}
3917
3918
3919int V8::GetLogLines(int from_pos, char* dest_buf, int max_size) {
3920#ifdef ENABLE_LOGGING_AND_PROFILING
Steve Block6ded16b2010-05-10 14:33:55 +01003921 ASSERT(max_size >= kMinimumSizeForLogLinesBuffer);
Steve Blocka7e24c12009-10-30 11:49:00 +00003922 return i::Logger::GetLogLines(from_pos, dest_buf, max_size);
3923#endif
3924 return 0;
3925}
3926
3927
3928int V8::GetCurrentThreadId() {
3929 API_ENTRY_CHECK("V8::GetCurrentThreadId()");
3930 EnsureInitialized("V8::GetCurrentThreadId()");
3931 return i::Top::thread_id();
3932}
3933
3934
3935void V8::TerminateExecution(int thread_id) {
3936 if (!i::V8::IsRunning()) return;
3937 API_ENTRY_CHECK("V8::GetCurrentThreadId()");
3938 // If the thread_id identifies the current thread just terminate
3939 // execution right away. Otherwise, ask the thread manager to
3940 // terminate the thread with the given id if any.
3941 if (thread_id == i::Top::thread_id()) {
3942 i::StackGuard::TerminateExecution();
3943 } else {
3944 i::ThreadManager::TerminateExecution(thread_id);
3945 }
3946}
3947
3948
3949void V8::TerminateExecution() {
3950 if (!i::V8::IsRunning()) return;
3951 i::StackGuard::TerminateExecution();
3952}
3953
3954
Steve Block6ded16b2010-05-10 14:33:55 +01003955bool V8::IsExecutionTerminating() {
3956 if (!i::V8::IsRunning()) return false;
3957 if (i::Top::has_scheduled_exception()) {
3958 return i::Top::scheduled_exception() == i::Heap::termination_exception();
3959 }
3960 return false;
3961}
3962
3963
Steve Blocka7e24c12009-10-30 11:49:00 +00003964String::Utf8Value::Utf8Value(v8::Handle<v8::Value> obj) {
3965 EnsureInitialized("v8::String::Utf8Value::Utf8Value()");
3966 if (obj.IsEmpty()) {
3967 str_ = NULL;
3968 length_ = 0;
3969 return;
3970 }
3971 ENTER_V8;
3972 HandleScope scope;
3973 TryCatch try_catch;
3974 Handle<String> str = obj->ToString();
3975 if (str.IsEmpty()) {
3976 str_ = NULL;
3977 length_ = 0;
3978 } else {
3979 length_ = str->Utf8Length();
3980 str_ = i::NewArray<char>(length_ + 1);
3981 str->WriteUtf8(str_);
3982 }
3983}
3984
3985
3986String::Utf8Value::~Utf8Value() {
3987 i::DeleteArray(str_);
3988}
3989
3990
3991String::AsciiValue::AsciiValue(v8::Handle<v8::Value> obj) {
3992 EnsureInitialized("v8::String::AsciiValue::AsciiValue()");
3993 if (obj.IsEmpty()) {
3994 str_ = NULL;
3995 length_ = 0;
3996 return;
3997 }
3998 ENTER_V8;
3999 HandleScope scope;
4000 TryCatch try_catch;
4001 Handle<String> str = obj->ToString();
4002 if (str.IsEmpty()) {
4003 str_ = NULL;
4004 length_ = 0;
4005 } else {
4006 length_ = str->Length();
4007 str_ = i::NewArray<char>(length_ + 1);
4008 str->WriteAscii(str_);
4009 }
4010}
4011
4012
4013String::AsciiValue::~AsciiValue() {
4014 i::DeleteArray(str_);
4015}
4016
4017
4018String::Value::Value(v8::Handle<v8::Value> obj) {
4019 EnsureInitialized("v8::String::Value::Value()");
4020 if (obj.IsEmpty()) {
4021 str_ = NULL;
4022 length_ = 0;
4023 return;
4024 }
4025 ENTER_V8;
4026 HandleScope scope;
4027 TryCatch try_catch;
4028 Handle<String> str = obj->ToString();
4029 if (str.IsEmpty()) {
4030 str_ = NULL;
4031 length_ = 0;
4032 } else {
4033 length_ = str->Length();
4034 str_ = i::NewArray<uint16_t>(length_ + 1);
4035 str->Write(str_);
4036 }
4037}
4038
4039
4040String::Value::~Value() {
4041 i::DeleteArray(str_);
4042}
4043
4044Local<Value> Exception::RangeError(v8::Handle<v8::String> raw_message) {
4045 LOG_API("RangeError");
4046 ON_BAILOUT("v8::Exception::RangeError()", return Local<Value>());
4047 ENTER_V8;
4048 i::Object* error;
4049 {
4050 HandleScope scope;
4051 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
4052 i::Handle<i::Object> result = i::Factory::NewRangeError(message);
4053 error = *result;
4054 }
4055 i::Handle<i::Object> result(error);
4056 return Utils::ToLocal(result);
4057}
4058
4059Local<Value> Exception::ReferenceError(v8::Handle<v8::String> raw_message) {
4060 LOG_API("ReferenceError");
4061 ON_BAILOUT("v8::Exception::ReferenceError()", return Local<Value>());
4062 ENTER_V8;
4063 i::Object* error;
4064 {
4065 HandleScope scope;
4066 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
4067 i::Handle<i::Object> result = i::Factory::NewReferenceError(message);
4068 error = *result;
4069 }
4070 i::Handle<i::Object> result(error);
4071 return Utils::ToLocal(result);
4072}
4073
4074Local<Value> Exception::SyntaxError(v8::Handle<v8::String> raw_message) {
4075 LOG_API("SyntaxError");
4076 ON_BAILOUT("v8::Exception::SyntaxError()", return Local<Value>());
4077 ENTER_V8;
4078 i::Object* error;
4079 {
4080 HandleScope scope;
4081 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
4082 i::Handle<i::Object> result = i::Factory::NewSyntaxError(message);
4083 error = *result;
4084 }
4085 i::Handle<i::Object> result(error);
4086 return Utils::ToLocal(result);
4087}
4088
4089Local<Value> Exception::TypeError(v8::Handle<v8::String> raw_message) {
4090 LOG_API("TypeError");
4091 ON_BAILOUT("v8::Exception::TypeError()", return Local<Value>());
4092 ENTER_V8;
4093 i::Object* error;
4094 {
4095 HandleScope scope;
4096 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
4097 i::Handle<i::Object> result = i::Factory::NewTypeError(message);
4098 error = *result;
4099 }
4100 i::Handle<i::Object> result(error);
4101 return Utils::ToLocal(result);
4102}
4103
4104Local<Value> Exception::Error(v8::Handle<v8::String> raw_message) {
4105 LOG_API("Error");
4106 ON_BAILOUT("v8::Exception::Error()", return Local<Value>());
4107 ENTER_V8;
4108 i::Object* error;
4109 {
4110 HandleScope scope;
4111 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
4112 i::Handle<i::Object> result = i::Factory::NewError(message);
4113 error = *result;
4114 }
4115 i::Handle<i::Object> result(error);
4116 return Utils::ToLocal(result);
4117}
4118
4119
4120// --- D e b u g S u p p o r t ---
4121
4122#ifdef ENABLE_DEBUGGER_SUPPORT
Leon Clarkef7060e22010-06-03 12:02:55 +01004123
4124static v8::Debug::EventCallback event_callback = NULL;
4125
4126static void EventCallbackWrapper(const v8::Debug::EventDetails& event_details) {
4127 if (event_callback) {
4128 event_callback(event_details.GetEvent(),
4129 event_details.GetExecutionState(),
4130 event_details.GetEventData(),
4131 event_details.GetCallbackData());
4132 }
4133}
4134
4135
Steve Blocka7e24c12009-10-30 11:49:00 +00004136bool Debug::SetDebugEventListener(EventCallback that, Handle<Value> data) {
4137 EnsureInitialized("v8::Debug::SetDebugEventListener()");
4138 ON_BAILOUT("v8::Debug::SetDebugEventListener()", return false);
4139 ENTER_V8;
Leon Clarkef7060e22010-06-03 12:02:55 +01004140
4141 event_callback = that;
4142
4143 HandleScope scope;
4144 i::Handle<i::Object> proxy = i::Factory::undefined_value();
4145 if (that != NULL) {
4146 proxy = i::Factory::NewProxy(FUNCTION_ADDR(EventCallbackWrapper));
4147 }
4148 i::Debugger::SetEventListener(proxy, Utils::OpenHandle(*data));
4149 return true;
4150}
4151
4152
4153bool Debug::SetDebugEventListener2(EventCallback2 that, Handle<Value> data) {
4154 EnsureInitialized("v8::Debug::SetDebugEventListener2()");
4155 ON_BAILOUT("v8::Debug::SetDebugEventListener2()", return false);
4156 ENTER_V8;
Steve Blocka7e24c12009-10-30 11:49:00 +00004157 HandleScope scope;
4158 i::Handle<i::Object> proxy = i::Factory::undefined_value();
4159 if (that != NULL) {
4160 proxy = i::Factory::NewProxy(FUNCTION_ADDR(that));
4161 }
4162 i::Debugger::SetEventListener(proxy, Utils::OpenHandle(*data));
4163 return true;
4164}
4165
4166
4167bool Debug::SetDebugEventListener(v8::Handle<v8::Object> that,
4168 Handle<Value> data) {
4169 ON_BAILOUT("v8::Debug::SetDebugEventListener()", return false);
4170 ENTER_V8;
4171 i::Debugger::SetEventListener(Utils::OpenHandle(*that),
4172 Utils::OpenHandle(*data));
4173 return true;
4174}
4175
4176
4177void Debug::DebugBreak() {
4178 if (!i::V8::IsRunning()) return;
4179 i::StackGuard::DebugBreak();
4180}
4181
4182
4183static v8::Debug::MessageHandler message_handler = NULL;
4184
4185static void MessageHandlerWrapper(const v8::Debug::Message& message) {
4186 if (message_handler) {
4187 v8::String::Value json(message.GetJSON());
4188 message_handler(*json, json.length(), message.GetClientData());
4189 }
4190}
4191
4192
4193void Debug::SetMessageHandler(v8::Debug::MessageHandler handler,
4194 bool message_handler_thread) {
4195 EnsureInitialized("v8::Debug::SetMessageHandler");
4196 ENTER_V8;
4197 // Message handler thread not supported any more. Parameter temporally left in
4198 // the API for client compatability reasons.
4199 CHECK(!message_handler_thread);
4200
4201 // TODO(sgjesse) support the old message handler API through a simple wrapper.
4202 message_handler = handler;
4203 if (message_handler != NULL) {
4204 i::Debugger::SetMessageHandler(MessageHandlerWrapper);
4205 } else {
4206 i::Debugger::SetMessageHandler(NULL);
4207 }
4208}
4209
4210
4211void Debug::SetMessageHandler2(v8::Debug::MessageHandler2 handler) {
4212 EnsureInitialized("v8::Debug::SetMessageHandler");
4213 ENTER_V8;
Steve Blocka7e24c12009-10-30 11:49:00 +00004214 i::Debugger::SetMessageHandler(handler);
4215}
4216
4217
4218void Debug::SendCommand(const uint16_t* command, int length,
4219 ClientData* client_data) {
4220 if (!i::V8::IsRunning()) return;
4221 i::Debugger::ProcessCommand(i::Vector<const uint16_t>(command, length),
4222 client_data);
4223}
4224
4225
4226void Debug::SetHostDispatchHandler(HostDispatchHandler handler,
4227 int period) {
4228 EnsureInitialized("v8::Debug::SetHostDispatchHandler");
4229 ENTER_V8;
4230 i::Debugger::SetHostDispatchHandler(handler, period);
4231}
4232
4233
Steve Blockd0582a62009-12-15 09:54:21 +00004234void Debug::SetDebugMessageDispatchHandler(
Leon Clarkee46be812010-01-19 14:06:41 +00004235 DebugMessageDispatchHandler handler, bool provide_locker) {
Steve Blockd0582a62009-12-15 09:54:21 +00004236 EnsureInitialized("v8::Debug::SetDebugMessageDispatchHandler");
4237 ENTER_V8;
Leon Clarkee46be812010-01-19 14:06:41 +00004238 i::Debugger::SetDebugMessageDispatchHandler(handler, provide_locker);
Steve Blockd0582a62009-12-15 09:54:21 +00004239}
4240
4241
Steve Blocka7e24c12009-10-30 11:49:00 +00004242Local<Value> Debug::Call(v8::Handle<v8::Function> fun,
4243 v8::Handle<v8::Value> data) {
4244 if (!i::V8::IsRunning()) return Local<Value>();
4245 ON_BAILOUT("v8::Debug::Call()", return Local<Value>());
4246 ENTER_V8;
4247 i::Handle<i::Object> result;
4248 EXCEPTION_PREAMBLE();
4249 if (data.IsEmpty()) {
4250 result = i::Debugger::Call(Utils::OpenHandle(*fun),
4251 i::Factory::undefined_value(),
4252 &has_pending_exception);
4253 } else {
4254 result = i::Debugger::Call(Utils::OpenHandle(*fun),
4255 Utils::OpenHandle(*data),
4256 &has_pending_exception);
4257 }
4258 EXCEPTION_BAILOUT_CHECK(Local<Value>());
4259 return Utils::ToLocal(result);
4260}
4261
4262
4263Local<Value> Debug::GetMirror(v8::Handle<v8::Value> obj) {
4264 if (!i::V8::IsRunning()) return Local<Value>();
4265 ON_BAILOUT("v8::Debug::GetMirror()", return Local<Value>());
4266 ENTER_V8;
4267 v8::HandleScope scope;
4268 i::Debug::Load();
4269 i::Handle<i::JSObject> debug(i::Debug::debug_context()->global());
4270 i::Handle<i::String> name = i::Factory::LookupAsciiSymbol("MakeMirror");
4271 i::Handle<i::Object> fun_obj = i::GetProperty(debug, name);
4272 i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(fun_obj);
4273 v8::Handle<v8::Function> v8_fun = Utils::ToLocal(fun);
4274 const int kArgc = 1;
4275 v8::Handle<v8::Value> argv[kArgc] = { obj };
4276 EXCEPTION_PREAMBLE();
4277 v8::Handle<v8::Value> result = v8_fun->Call(Utils::ToLocal(debug),
4278 kArgc,
4279 argv);
4280 EXCEPTION_BAILOUT_CHECK(Local<Value>());
4281 return scope.Close(result);
4282}
4283
4284
Leon Clarkee46be812010-01-19 14:06:41 +00004285bool Debug::EnableAgent(const char* name, int port, bool wait_for_connection) {
4286 return i::Debugger::StartAgent(name, port, wait_for_connection);
Steve Blocka7e24c12009-10-30 11:49:00 +00004287}
Leon Clarkee46be812010-01-19 14:06:41 +00004288
4289void Debug::ProcessDebugMessages() {
4290 i::Execution::ProcessDebugMesssages(true);
4291}
4292
Steve Block6ded16b2010-05-10 14:33:55 +01004293Local<Context> Debug::GetDebugContext() {
4294 EnsureInitialized("v8::Debug::GetDebugContext()");
4295 ENTER_V8;
4296 return Utils::ToLocal(i::Debugger::GetDebugContext());
4297}
4298
Steve Blocka7e24c12009-10-30 11:49:00 +00004299#endif // ENABLE_DEBUGGER_SUPPORT
4300
Steve Block6ded16b2010-05-10 14:33:55 +01004301
4302#ifdef ENABLE_LOGGING_AND_PROFILING
4303
4304Handle<String> CpuProfileNode::GetFunctionName() const {
4305 IsDeadCheck("v8::CpuProfileNode::GetFunctionName");
4306 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
4307 const i::CodeEntry* entry = node->entry();
4308 if (!entry->has_name_prefix()) {
4309 return Handle<String>(ToApi<String>(
4310 i::Factory::LookupAsciiSymbol(entry->name())));
4311 } else {
4312 return Handle<String>(ToApi<String>(i::Factory::NewConsString(
4313 i::Factory::LookupAsciiSymbol(entry->name_prefix()),
4314 i::Factory::LookupAsciiSymbol(entry->name()))));
4315 }
4316}
4317
4318
4319Handle<String> CpuProfileNode::GetScriptResourceName() const {
4320 IsDeadCheck("v8::CpuProfileNode::GetScriptResourceName");
4321 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
4322 return Handle<String>(ToApi<String>(i::Factory::LookupAsciiSymbol(
4323 node->entry()->resource_name())));
4324}
4325
4326
4327int CpuProfileNode::GetLineNumber() const {
4328 IsDeadCheck("v8::CpuProfileNode::GetLineNumber");
4329 return reinterpret_cast<const i::ProfileNode*>(this)->entry()->line_number();
4330}
4331
4332
4333double CpuProfileNode::GetTotalTime() const {
4334 IsDeadCheck("v8::CpuProfileNode::GetTotalTime");
4335 return reinterpret_cast<const i::ProfileNode*>(this)->GetTotalMillis();
4336}
4337
4338
4339double CpuProfileNode::GetSelfTime() const {
4340 IsDeadCheck("v8::CpuProfileNode::GetSelfTime");
4341 return reinterpret_cast<const i::ProfileNode*>(this)->GetSelfMillis();
4342}
4343
4344
4345double CpuProfileNode::GetTotalSamplesCount() const {
4346 IsDeadCheck("v8::CpuProfileNode::GetTotalSamplesCount");
4347 return reinterpret_cast<const i::ProfileNode*>(this)->total_ticks();
4348}
4349
4350
4351double CpuProfileNode::GetSelfSamplesCount() const {
4352 IsDeadCheck("v8::CpuProfileNode::GetSelfSamplesCount");
4353 return reinterpret_cast<const i::ProfileNode*>(this)->self_ticks();
4354}
4355
4356
4357unsigned CpuProfileNode::GetCallUid() const {
4358 IsDeadCheck("v8::CpuProfileNode::GetCallUid");
4359 return reinterpret_cast<const i::ProfileNode*>(this)->entry()->call_uid();
4360}
4361
4362
4363int CpuProfileNode::GetChildrenCount() const {
4364 IsDeadCheck("v8::CpuProfileNode::GetChildrenCount");
4365 return reinterpret_cast<const i::ProfileNode*>(this)->children()->length();
4366}
4367
4368
4369const CpuProfileNode* CpuProfileNode::GetChild(int index) const {
4370 IsDeadCheck("v8::CpuProfileNode::GetChild");
4371 const i::ProfileNode* child =
4372 reinterpret_cast<const i::ProfileNode*>(this)->children()->at(index);
4373 return reinterpret_cast<const CpuProfileNode*>(child);
4374}
4375
4376
4377unsigned CpuProfile::GetUid() const {
4378 IsDeadCheck("v8::CpuProfile::GetUid");
4379 return reinterpret_cast<const i::CpuProfile*>(this)->uid();
4380}
4381
4382
4383Handle<String> CpuProfile::GetTitle() const {
4384 IsDeadCheck("v8::CpuProfile::GetTitle");
4385 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
4386 return Handle<String>(ToApi<String>(i::Factory::LookupAsciiSymbol(
4387 profile->title())));
4388}
4389
4390
4391const CpuProfileNode* CpuProfile::GetBottomUpRoot() const {
4392 IsDeadCheck("v8::CpuProfile::GetBottomUpRoot");
4393 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
4394 return reinterpret_cast<const CpuProfileNode*>(profile->bottom_up()->root());
4395}
4396
4397
4398const CpuProfileNode* CpuProfile::GetTopDownRoot() const {
4399 IsDeadCheck("v8::CpuProfile::GetTopDownRoot");
4400 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
4401 return reinterpret_cast<const CpuProfileNode*>(profile->top_down()->root());
4402}
4403
4404
4405int CpuProfiler::GetProfilesCount() {
4406 IsDeadCheck("v8::CpuProfiler::GetProfilesCount");
4407 return i::CpuProfiler::GetProfilesCount();
4408}
4409
4410
Leon Clarkef7060e22010-06-03 12:02:55 +01004411const CpuProfile* CpuProfiler::GetProfile(int index,
4412 Handle<Value> security_token) {
Steve Block6ded16b2010-05-10 14:33:55 +01004413 IsDeadCheck("v8::CpuProfiler::GetProfile");
Leon Clarkef7060e22010-06-03 12:02:55 +01004414 return reinterpret_cast<const CpuProfile*>(
4415 i::CpuProfiler::GetProfile(
4416 security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
4417 index));
Steve Block6ded16b2010-05-10 14:33:55 +01004418}
4419
4420
Leon Clarkef7060e22010-06-03 12:02:55 +01004421const CpuProfile* CpuProfiler::FindProfile(unsigned uid,
4422 Handle<Value> security_token) {
Steve Block6ded16b2010-05-10 14:33:55 +01004423 IsDeadCheck("v8::CpuProfiler::FindProfile");
Leon Clarkef7060e22010-06-03 12:02:55 +01004424 return reinterpret_cast<const CpuProfile*>(
4425 i::CpuProfiler::FindProfile(
4426 security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
4427 uid));
Steve Block6ded16b2010-05-10 14:33:55 +01004428}
4429
4430
4431void CpuProfiler::StartProfiling(Handle<String> title) {
4432 IsDeadCheck("v8::CpuProfiler::StartProfiling");
4433 i::CpuProfiler::StartProfiling(*Utils::OpenHandle(*title));
4434}
4435
4436
Leon Clarkef7060e22010-06-03 12:02:55 +01004437const CpuProfile* CpuProfiler::StopProfiling(Handle<String> title,
4438 Handle<Value> security_token) {
Steve Block6ded16b2010-05-10 14:33:55 +01004439 IsDeadCheck("v8::CpuProfiler::StopProfiling");
4440 return reinterpret_cast<const CpuProfile*>(
Leon Clarkef7060e22010-06-03 12:02:55 +01004441 i::CpuProfiler::StopProfiling(
4442 security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
4443 *Utils::OpenHandle(*title)));
Steve Block6ded16b2010-05-10 14:33:55 +01004444}
4445
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004446
4447HeapGraphEdge::Type HeapGraphEdge::GetType() const {
4448 IsDeadCheck("v8::HeapGraphEdge::GetType");
4449 return static_cast<HeapGraphEdge::Type>(
4450 reinterpret_cast<const i::HeapGraphEdge*>(this)->type());
4451}
4452
4453
4454Handle<Value> HeapGraphEdge::GetName() const {
4455 IsDeadCheck("v8::HeapGraphEdge::GetName");
4456 const i::HeapGraphEdge* edge =
4457 reinterpret_cast<const i::HeapGraphEdge*>(this);
4458 switch (edge->type()) {
4459 case i::HeapGraphEdge::CONTEXT_VARIABLE:
4460 case i::HeapGraphEdge::INTERNAL:
4461 case i::HeapGraphEdge::PROPERTY:
4462 return Handle<String>(ToApi<String>(i::Factory::LookupAsciiSymbol(
4463 edge->name())));
4464 case i::HeapGraphEdge::ELEMENT:
4465 return Handle<Number>(ToApi<Number>(i::Factory::NewNumberFromInt(
4466 edge->index())));
4467 default: UNREACHABLE();
4468 }
4469 return ImplementationUtilities::Undefined();
4470}
4471
4472
4473const HeapGraphNode* HeapGraphEdge::GetFromNode() const {
4474 IsDeadCheck("v8::HeapGraphEdge::GetFromNode");
4475 const i::HeapEntry* from =
4476 reinterpret_cast<const i::HeapGraphEdge*>(this)->from();
4477 return reinterpret_cast<const HeapGraphNode*>(from);
4478}
4479
4480
4481const HeapGraphNode* HeapGraphEdge::GetToNode() const {
4482 IsDeadCheck("v8::HeapGraphEdge::GetToNode");
4483 const i::HeapEntry* to =
4484 reinterpret_cast<const i::HeapGraphEdge*>(this)->to();
4485 return reinterpret_cast<const HeapGraphNode*>(to);
4486}
4487
4488
4489int HeapGraphPath::GetEdgesCount() const {
4490 return reinterpret_cast<const i::HeapGraphPath*>(this)->path()->length();
4491}
4492
4493
4494const HeapGraphEdge* HeapGraphPath::GetEdge(int index) const {
4495 return reinterpret_cast<const HeapGraphEdge*>(
4496 reinterpret_cast<const i::HeapGraphPath*>(this)->path()->at(index));
4497}
4498
4499
4500const HeapGraphNode* HeapGraphPath::GetFromNode() const {
4501 return GetEdgesCount() > 0 ? GetEdge(0)->GetFromNode() : NULL;
4502}
4503
4504
4505const HeapGraphNode* HeapGraphPath::GetToNode() const {
4506 const int count = GetEdgesCount();
4507 return count > 0 ? GetEdge(count - 1)->GetToNode() : NULL;
4508}
4509
4510
4511HeapGraphNode::Type HeapGraphNode::GetType() const {
4512 IsDeadCheck("v8::HeapGraphNode::GetType");
4513 return static_cast<HeapGraphNode::Type>(
4514 reinterpret_cast<const i::HeapEntry*>(this)->type());
4515}
4516
4517
4518Handle<String> HeapGraphNode::GetName() const {
4519 IsDeadCheck("v8::HeapGraphNode::GetName");
4520 return Handle<String>(ToApi<String>(i::Factory::LookupAsciiSymbol(
4521 reinterpret_cast<const i::HeapEntry*>(this)->name())));
4522}
4523
4524
4525int HeapGraphNode::GetSelfSize() const {
4526 IsDeadCheck("v8::HeapGraphNode::GetSelfSize");
4527 return reinterpret_cast<const i::HeapEntry*>(this)->self_size();
4528}
4529
4530
4531int HeapGraphNode::GetTotalSize() const {
4532 IsDeadCheck("v8::HeapSnapshot::GetHead");
4533 return const_cast<i::HeapEntry*>(
4534 reinterpret_cast<const i::HeapEntry*>(this))->TotalSize();
4535}
4536
4537
4538int HeapGraphNode::GetPrivateSize() const {
4539 IsDeadCheck("v8::HeapSnapshot::GetPrivateSize");
4540 return const_cast<i::HeapEntry*>(
4541 reinterpret_cast<const i::HeapEntry*>(this))->NonSharedTotalSize();
4542}
4543
4544
4545int HeapGraphNode::GetChildrenCount() const {
4546 IsDeadCheck("v8::HeapSnapshot::GetChildrenCount");
4547 return reinterpret_cast<const i::HeapEntry*>(this)->children()->length();
4548}
4549
4550
4551const HeapGraphEdge* HeapGraphNode::GetChild(int index) const {
4552 IsDeadCheck("v8::HeapSnapshot::GetChild");
4553 return reinterpret_cast<const HeapGraphEdge*>(
4554 reinterpret_cast<const i::HeapEntry*>(this)->children()->at(index));
4555}
4556
4557
4558int HeapGraphNode::GetRetainersCount() const {
4559 IsDeadCheck("v8::HeapSnapshot::GetRetainersCount");
4560 return reinterpret_cast<const i::HeapEntry*>(this)->retainers()->length();
4561}
4562
4563
4564const HeapGraphEdge* HeapGraphNode::GetRetainer(int index) const {
4565 IsDeadCheck("v8::HeapSnapshot::GetRetainer");
4566 return reinterpret_cast<const HeapGraphEdge*>(
4567 reinterpret_cast<const i::HeapEntry*>(this)->retainers()->at(index));
4568}
4569
4570
4571int HeapGraphNode::GetRetainingPathsCount() const {
4572 IsDeadCheck("v8::HeapSnapshot::GetRetainingPathsCount");
4573 return const_cast<i::HeapEntry*>(
4574 reinterpret_cast<const i::HeapEntry*>(
4575 this))->GetRetainingPaths()->length();
4576}
4577
4578
4579const HeapGraphPath* HeapGraphNode::GetRetainingPath(int index) const {
4580 IsDeadCheck("v8::HeapSnapshot::GetRetainingPath");
4581 return reinterpret_cast<const HeapGraphPath*>(
4582 const_cast<i::HeapEntry*>(
4583 reinterpret_cast<const i::HeapEntry*>(
4584 this))->GetRetainingPaths()->at(index));
4585}
4586
4587
4588unsigned HeapSnapshot::GetUid() const {
4589 IsDeadCheck("v8::HeapSnapshot::GetUid");
4590 return reinterpret_cast<const i::HeapSnapshot*>(this)->uid();
4591}
4592
4593
4594Handle<String> HeapSnapshot::GetTitle() const {
4595 IsDeadCheck("v8::HeapSnapshot::GetTitle");
4596 const i::HeapSnapshot* snapshot =
4597 reinterpret_cast<const i::HeapSnapshot*>(this);
4598 return Handle<String>(ToApi<String>(i::Factory::LookupAsciiSymbol(
4599 snapshot->title())));
4600}
4601
4602
4603const HeapGraphNode* HeapSnapshot::GetHead() const {
4604 IsDeadCheck("v8::HeapSnapshot::GetHead");
4605 const i::HeapSnapshot* snapshot =
4606 reinterpret_cast<const i::HeapSnapshot*>(this);
4607 return reinterpret_cast<const HeapGraphNode*>(snapshot->const_root());
4608}
4609
4610
4611int HeapProfiler::GetSnapshotsCount() {
4612 IsDeadCheck("v8::HeapProfiler::GetSnapshotsCount");
4613 return i::HeapProfiler::GetSnapshotsCount();
4614}
4615
4616
4617const HeapSnapshot* HeapProfiler::GetSnapshot(int index) {
4618 IsDeadCheck("v8::HeapProfiler::GetSnapshot");
4619 return reinterpret_cast<const HeapSnapshot*>(
4620 i::HeapProfiler::GetSnapshot(index));
4621}
4622
4623
4624const HeapSnapshot* HeapProfiler::FindSnapshot(unsigned uid) {
4625 IsDeadCheck("v8::HeapProfiler::FindSnapshot");
4626 return reinterpret_cast<const HeapSnapshot*>(
4627 i::HeapProfiler::FindSnapshot(uid));
4628}
4629
4630
4631const HeapSnapshot* HeapProfiler::TakeSnapshot(Handle<String> title) {
4632 IsDeadCheck("v8::HeapProfiler::TakeSnapshot");
4633 return reinterpret_cast<const HeapSnapshot*>(
4634 i::HeapProfiler::TakeSnapshot(*Utils::OpenHandle(*title)));
4635}
4636
Steve Block6ded16b2010-05-10 14:33:55 +01004637#endif // ENABLE_LOGGING_AND_PROFILING
4638
4639
Steve Blocka7e24c12009-10-30 11:49:00 +00004640namespace internal {
4641
4642
4643HandleScopeImplementer* HandleScopeImplementer::instance() {
4644 return &thread_local;
4645}
4646
4647
4648void HandleScopeImplementer::FreeThreadResources() {
4649 thread_local.Free();
4650}
4651
4652
4653char* HandleScopeImplementer::ArchiveThread(char* storage) {
4654 return thread_local.ArchiveThreadHelper(storage);
4655}
4656
4657
4658char* HandleScopeImplementer::ArchiveThreadHelper(char* storage) {
4659 v8::ImplementationUtilities::HandleScopeData* current =
4660 v8::ImplementationUtilities::CurrentHandleScope();
4661 handle_scope_data_ = *current;
4662 memcpy(storage, this, sizeof(*this));
4663
4664 ResetAfterArchive();
4665 current->Initialize();
4666
4667 return storage + ArchiveSpacePerThread();
4668}
4669
4670
4671int HandleScopeImplementer::ArchiveSpacePerThread() {
4672 return sizeof(thread_local);
4673}
4674
4675
4676char* HandleScopeImplementer::RestoreThread(char* storage) {
4677 return thread_local.RestoreThreadHelper(storage);
4678}
4679
4680
4681char* HandleScopeImplementer::RestoreThreadHelper(char* storage) {
4682 memcpy(this, storage, sizeof(*this));
4683 *v8::ImplementationUtilities::CurrentHandleScope() = handle_scope_data_;
4684 return storage + ArchiveSpacePerThread();
4685}
4686
4687
4688void HandleScopeImplementer::IterateThis(ObjectVisitor* v) {
4689 // Iterate over all handles in the blocks except for the last.
4690 for (int i = blocks()->length() - 2; i >= 0; --i) {
4691 Object** block = blocks()->at(i);
4692 v->VisitPointers(block, &block[kHandleBlockSize]);
4693 }
4694
4695 // Iterate over live handles in the last block (if any).
4696 if (!blocks()->is_empty()) {
4697 v->VisitPointers(blocks()->last(), handle_scope_data_.next);
4698 }
4699
4700 if (!saved_contexts_.is_empty()) {
4701 Object** start = reinterpret_cast<Object**>(&saved_contexts_.first());
4702 v->VisitPointers(start, start + saved_contexts_.length());
4703 }
4704}
4705
4706
4707void HandleScopeImplementer::Iterate(ObjectVisitor* v) {
4708 v8::ImplementationUtilities::HandleScopeData* current =
4709 v8::ImplementationUtilities::CurrentHandleScope();
4710 thread_local.handle_scope_data_ = *current;
4711 thread_local.IterateThis(v);
4712}
4713
4714
4715char* HandleScopeImplementer::Iterate(ObjectVisitor* v, char* storage) {
4716 HandleScopeImplementer* thread_local =
4717 reinterpret_cast<HandleScopeImplementer*>(storage);
4718 thread_local->IterateThis(v);
4719 return storage + ArchiveSpacePerThread();
4720}
4721
4722} } // namespace v8::internal