1 // Copyright 2011 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
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.
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.
32 #include "arguments.h"
33 #include "bootstrapper.h"
36 #include "deoptimizer.h"
37 #include "execution.h"
39 #include "global-handles.h"
40 #include "heap-profiler.h"
45 #include "profile-generator-inl.h"
46 #include "runtime-profiler.h"
47 #include "scanner-character-streams.h"
48 #include "serialize.h"
50 #include "v8threads.h"
52 #include "vm-state-inl.h"
54 #include "../include/v8-profiler.h"
55 #include "../include/v8-testing.h"
57 #define LOG_API(isolate, expr) LOG(isolate, ApiEntryCall(expr))
59 #define ENTER_V8(isolate) \
60 ASSERT((isolate)->IsInitialized()); \
61 i::VMState __state__((isolate), i::OTHER)
62 #define LEAVE_V8(isolate) \
63 i::VMState __state__((isolate), i::EXTERNAL)
67 #define ON_BAILOUT(isolate, location, code) \
68 if (IsDeadCheck(isolate, location) || \
69 IsExecutionTerminatingCheck(isolate)) { \
75 #define EXCEPTION_PREAMBLE(isolate) \
76 (isolate)->handle_scope_implementer()->IncrementCallDepth(); \
77 ASSERT(!(isolate)->external_caught_exception()); \
78 bool has_pending_exception = false
81 #define EXCEPTION_BAILOUT_CHECK(isolate, value) \
83 i::HandleScopeImplementer* handle_scope_implementer = \
84 (isolate)->handle_scope_implementer(); \
85 handle_scope_implementer->DecrementCallDepth(); \
86 if (has_pending_exception) { \
87 if (handle_scope_implementer->CallDepthIsZero() && \
88 (isolate)->is_out_of_memory()) { \
89 if (!(isolate)->ignore_out_of_memory()) \
90 i::V8::FatalProcessOutOfMemory(NULL); \
92 bool call_depth_is_zero = handle_scope_implementer->CallDepthIsZero(); \
93 (isolate)->OptionalRescheduleException(call_depth_is_zero); \
99 #define API_ENTRY_CHECK(isolate, msg) \
101 if (v8::Locker::IsActive()) { \
102 ApiCheck(isolate->thread_manager()->IsLockedByCurrentThread(), \
104 "Entering the V8 API without proper locking in place"); \
109 // --- E x c e p t i o n B e h a v i o r ---
112 static void DefaultFatalErrorHandler(const char* location,
113 const char* message) {
114 i::VMState __state__(i::Isolate::Current(), i::OTHER);
115 API_Fatal(location, message);
119 static FatalErrorCallback GetFatalErrorHandler() {
120 i::Isolate* isolate = i::Isolate::Current();
121 if (isolate->exception_behavior() == NULL) {
122 isolate->set_exception_behavior(DefaultFatalErrorHandler);
124 return isolate->exception_behavior();
128 void i::FatalProcessOutOfMemory(const char* location) {
129 i::V8::FatalProcessOutOfMemory(location, false);
133 // When V8 cannot allocated memory FatalProcessOutOfMemory is called.
134 // The default fatal error handler is called and execution is stopped.
135 void i::V8::FatalProcessOutOfMemory(const char* location, bool take_snapshot) {
136 i::HeapStats heap_stats;
138 heap_stats.start_marker = &start_marker;
140 heap_stats.new_space_size = &new_space_size;
141 int new_space_capacity;
142 heap_stats.new_space_capacity = &new_space_capacity;
143 intptr_t old_pointer_space_size;
144 heap_stats.old_pointer_space_size = &old_pointer_space_size;
145 intptr_t old_pointer_space_capacity;
146 heap_stats.old_pointer_space_capacity = &old_pointer_space_capacity;
147 intptr_t old_data_space_size;
148 heap_stats.old_data_space_size = &old_data_space_size;
149 intptr_t old_data_space_capacity;
150 heap_stats.old_data_space_capacity = &old_data_space_capacity;
151 intptr_t code_space_size;
152 heap_stats.code_space_size = &code_space_size;
153 intptr_t code_space_capacity;
154 heap_stats.code_space_capacity = &code_space_capacity;
155 intptr_t map_space_size;
156 heap_stats.map_space_size = &map_space_size;
157 intptr_t map_space_capacity;
158 heap_stats.map_space_capacity = &map_space_capacity;
159 intptr_t cell_space_size;
160 heap_stats.cell_space_size = &cell_space_size;
161 intptr_t cell_space_capacity;
162 heap_stats.cell_space_capacity = &cell_space_capacity;
163 intptr_t lo_space_size;
164 heap_stats.lo_space_size = &lo_space_size;
165 int global_handle_count;
166 heap_stats.global_handle_count = &global_handle_count;
167 int weak_global_handle_count;
168 heap_stats.weak_global_handle_count = &weak_global_handle_count;
169 int pending_global_handle_count;
170 heap_stats.pending_global_handle_count = &pending_global_handle_count;
171 int near_death_global_handle_count;
172 heap_stats.near_death_global_handle_count = &near_death_global_handle_count;
173 int free_global_handle_count;
174 heap_stats.free_global_handle_count = &free_global_handle_count;
175 intptr_t memory_allocator_size;
176 heap_stats.memory_allocator_size = &memory_allocator_size;
177 intptr_t memory_allocator_capacity;
178 heap_stats.memory_allocator_capacity = &memory_allocator_capacity;
179 int objects_per_type[LAST_TYPE + 1] = {0};
180 heap_stats.objects_per_type = objects_per_type;
181 int size_per_type[LAST_TYPE + 1] = {0};
182 heap_stats.size_per_type = size_per_type;
184 heap_stats.os_error = &os_error;
186 heap_stats.end_marker = &end_marker;
187 i::Isolate* isolate = i::Isolate::Current();
189 // Don't use the take_snapshot since we don't support HeapIterator here
190 // without doing a special GC.
191 isolate->heap()->RecordStats(&heap_stats, false);
192 i::V8::SetFatalError();
193 FatalErrorCallback callback = GetFatalErrorHandler();
196 callback(location, "Allocation failed - process out of memory");
198 // If the callback returns, we stop execution.
203 bool Utils::ReportApiFailure(const char* location, const char* message) {
204 FatalErrorCallback callback = GetFatalErrorHandler();
205 callback(location, message);
206 i::V8::SetFatalError();
212 return i::V8::IsDead();
216 static inline bool ApiCheck(bool condition,
217 const char* location,
218 const char* message) {
219 return condition ? true : Utils::ReportApiFailure(location, message);
223 static bool ReportV8Dead(const char* location) {
224 FatalErrorCallback callback = GetFatalErrorHandler();
225 callback(location, "V8 is no longer usable");
230 static bool ReportEmptyHandle(const char* location) {
231 FatalErrorCallback callback = GetFatalErrorHandler();
232 callback(location, "Reading from empty handle");
238 * IsDeadCheck checks that the vm is usable. If, for instance, the vm has been
239 * out of memory at some point this check will fail. It should be called on
240 * entry to all methods that touch anything in the heap, except destructors
241 * which you sometimes can't avoid calling after the vm has crashed. Functions
242 * that call EnsureInitialized or ON_BAILOUT don't have to also call
243 * IsDeadCheck. ON_BAILOUT has the advantage over EnsureInitialized that you
244 * can arrange to return if the VM is dead. This is needed to ensure that no VM
245 * heap allocations are attempted on a dead VM. EnsureInitialized has the
246 * advantage over ON_BAILOUT that it actually initializes the VM if this has not
249 static inline bool IsDeadCheck(i::Isolate* isolate, const char* location) {
250 return !isolate->IsInitialized()
251 && i::V8::IsDead() ? ReportV8Dead(location) : false;
255 static inline bool IsExecutionTerminatingCheck(i::Isolate* isolate) {
256 if (!isolate->IsInitialized()) return false;
257 if (isolate->has_scheduled_exception()) {
258 return isolate->scheduled_exception() ==
259 isolate->heap()->termination_exception();
265 static inline bool EmptyCheck(const char* location, v8::Handle<v8::Data> obj) {
266 return obj.IsEmpty() ? ReportEmptyHandle(location) : false;
270 static inline bool EmptyCheck(const char* location, const v8::Data* obj) {
271 return (obj == 0) ? ReportEmptyHandle(location) : false;
274 // --- S t a t i c s ---
277 static bool InitializeHelper() {
278 if (i::Snapshot::Initialize()) return true;
279 return i::V8::Initialize(NULL);
283 static inline bool EnsureInitializedForIsolate(i::Isolate* isolate,
284 const char* location) {
285 if (IsDeadCheck(isolate, location)) return false;
286 if (isolate != NULL) {
287 if (isolate->IsInitialized()) return true;
289 ASSERT(isolate == i::Isolate::Current());
290 return ApiCheck(InitializeHelper(), location, "Error initializing V8");
293 // Some initializing API functions are called early and may be
294 // called on a thread different from static initializer thread.
295 // If Isolate API is used, Isolate::Enter() will initialize TLS so
296 // Isolate::Current() works. If it's a legacy case, then the thread
297 // may not have TLS initialized yet. However, in initializing APIs it
298 // may be too early to call EnsureInitialized() - some pre-init
299 // parameters still have to be configured.
300 static inline i::Isolate* EnterIsolateIfNeeded() {
301 i::Isolate* isolate = i::Isolate::UncheckedCurrent();
305 i::Isolate::EnterDefaultIsolate();
306 isolate = i::Isolate::Current();
311 StartupDataDecompressor::StartupDataDecompressor()
312 : raw_data(i::NewArray<char*>(V8::GetCompressedStartupDataCount())) {
313 for (int i = 0; i < V8::GetCompressedStartupDataCount(); ++i) {
319 StartupDataDecompressor::~StartupDataDecompressor() {
320 for (int i = 0; i < V8::GetCompressedStartupDataCount(); ++i) {
321 i::DeleteArray(raw_data[i]);
323 i::DeleteArray(raw_data);
327 int StartupDataDecompressor::Decompress() {
328 int compressed_data_count = V8::GetCompressedStartupDataCount();
329 StartupData* compressed_data =
330 i::NewArray<StartupData>(compressed_data_count);
331 V8::GetCompressedStartupData(compressed_data);
332 for (int i = 0; i < compressed_data_count; ++i) {
333 char* decompressed = raw_data[i] =
334 i::NewArray<char>(compressed_data[i].raw_size);
335 if (compressed_data[i].compressed_size != 0) {
336 int result = DecompressData(decompressed,
337 &compressed_data[i].raw_size,
338 compressed_data[i].data,
339 compressed_data[i].compressed_size);
340 if (result != 0) return result;
342 ASSERT_EQ(0, compressed_data[i].raw_size);
344 compressed_data[i].data = decompressed;
346 V8::SetDecompressedStartupData(compressed_data);
351 StartupData::CompressionAlgorithm V8::GetCompressedStartupDataAlgorithm() {
352 #ifdef COMPRESS_STARTUP_DATA_BZ2
353 return StartupData::kBZip2;
355 return StartupData::kUncompressed;
360 enum CompressedStartupDataItems {
364 kExperimentalLibraries,
365 kCompressedStartupDataCount
368 int V8::GetCompressedStartupDataCount() {
369 #ifdef COMPRESS_STARTUP_DATA_BZ2
370 return kCompressedStartupDataCount;
377 void V8::GetCompressedStartupData(StartupData* compressed_data) {
378 #ifdef COMPRESS_STARTUP_DATA_BZ2
379 compressed_data[kSnapshot].data =
380 reinterpret_cast<const char*>(i::Snapshot::data());
381 compressed_data[kSnapshot].compressed_size = i::Snapshot::size();
382 compressed_data[kSnapshot].raw_size = i::Snapshot::raw_size();
384 compressed_data[kSnapshotContext].data =
385 reinterpret_cast<const char*>(i::Snapshot::context_data());
386 compressed_data[kSnapshotContext].compressed_size =
387 i::Snapshot::context_size();
388 compressed_data[kSnapshotContext].raw_size = i::Snapshot::context_raw_size();
390 i::Vector<const i::byte> libraries_source = i::Natives::GetScriptsSource();
391 compressed_data[kLibraries].data =
392 reinterpret_cast<const char*>(libraries_source.start());
393 compressed_data[kLibraries].compressed_size = libraries_source.length();
394 compressed_data[kLibraries].raw_size = i::Natives::GetRawScriptsSize();
396 i::Vector<const i::byte> exp_libraries_source =
397 i::ExperimentalNatives::GetScriptsSource();
398 compressed_data[kExperimentalLibraries].data =
399 reinterpret_cast<const char*>(exp_libraries_source.start());
400 compressed_data[kExperimentalLibraries].compressed_size =
401 exp_libraries_source.length();
402 compressed_data[kExperimentalLibraries].raw_size =
403 i::ExperimentalNatives::GetRawScriptsSize();
408 void V8::SetDecompressedStartupData(StartupData* decompressed_data) {
409 #ifdef COMPRESS_STARTUP_DATA_BZ2
410 ASSERT_EQ(i::Snapshot::raw_size(), decompressed_data[kSnapshot].raw_size);
411 i::Snapshot::set_raw_data(
412 reinterpret_cast<const i::byte*>(decompressed_data[kSnapshot].data));
414 ASSERT_EQ(i::Snapshot::context_raw_size(),
415 decompressed_data[kSnapshotContext].raw_size);
416 i::Snapshot::set_context_raw_data(
417 reinterpret_cast<const i::byte*>(
418 decompressed_data[kSnapshotContext].data));
420 ASSERT_EQ(i::Natives::GetRawScriptsSize(),
421 decompressed_data[kLibraries].raw_size);
422 i::Vector<const char> libraries_source(
423 decompressed_data[kLibraries].data,
424 decompressed_data[kLibraries].raw_size);
425 i::Natives::SetRawScriptsSource(libraries_source);
427 ASSERT_EQ(i::ExperimentalNatives::GetRawScriptsSize(),
428 decompressed_data[kExperimentalLibraries].raw_size);
429 i::Vector<const char> exp_libraries_source(
430 decompressed_data[kExperimentalLibraries].data,
431 decompressed_data[kExperimentalLibraries].raw_size);
432 i::ExperimentalNatives::SetRawScriptsSource(exp_libraries_source);
437 void V8::SetFatalErrorHandler(FatalErrorCallback that) {
438 i::Isolate* isolate = EnterIsolateIfNeeded();
439 isolate->set_exception_behavior(that);
443 void V8::SetAllowCodeGenerationFromStringsCallback(
444 AllowCodeGenerationFromStringsCallback callback) {
445 i::Isolate* isolate = EnterIsolateIfNeeded();
446 isolate->set_allow_code_gen_callback(callback);
451 void ImplementationUtilities::ZapHandleRange(i::Object** begin,
453 i::HandleScope::ZapRange(begin, end);
458 void V8::SetFlagsFromString(const char* str, int length) {
459 i::FlagList::SetFlagsFromString(str, length);
463 void V8::SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags) {
464 i::FlagList::SetFlagsFromCommandLine(argc, argv, remove_flags);
468 v8::Handle<Value> ThrowException(v8::Handle<v8::Value> value) {
469 i::Isolate* isolate = i::Isolate::Current();
470 if (IsDeadCheck(isolate, "v8::ThrowException()")) {
471 return v8::Handle<Value>();
474 // If we're passed an empty handle, we throw an undefined exception
475 // to deal more gracefully with out of memory situations.
476 if (value.IsEmpty()) {
477 isolate->ScheduleThrow(isolate->heap()->undefined_value());
479 isolate->ScheduleThrow(*Utils::OpenHandle(*value));
481 return v8::Undefined();
485 RegisteredExtension* RegisteredExtension::first_extension_ = NULL;
488 RegisteredExtension::RegisteredExtension(Extension* extension)
489 : extension_(extension), state_(UNVISITED) { }
492 void RegisteredExtension::Register(RegisteredExtension* that) {
493 that->next_ = first_extension_;
494 first_extension_ = that;
498 void RegisterExtension(Extension* that) {
499 RegisteredExtension* extension = new RegisteredExtension(that);
500 RegisteredExtension::Register(extension);
504 Extension::Extension(const char* name,
510 source_length_(source_length >= 0 ?
511 source_length : (source ? strlen(source) : 0)),
512 source_(source, source_length_),
513 dep_count_(dep_count),
515 auto_enable_(false) { }
518 v8::Handle<Primitive> Undefined() {
519 i::Isolate* isolate = i::Isolate::Current();
520 if (!EnsureInitializedForIsolate(isolate, "v8::Undefined()")) {
521 return v8::Handle<v8::Primitive>();
523 return v8::Handle<Primitive>(ToApi<Primitive>(
524 isolate->factory()->undefined_value()));
528 v8::Handle<Primitive> Null() {
529 i::Isolate* isolate = i::Isolate::Current();
530 if (!EnsureInitializedForIsolate(isolate, "v8::Null()")) {
531 return v8::Handle<v8::Primitive>();
533 return v8::Handle<Primitive>(
534 ToApi<Primitive>(isolate->factory()->null_value()));
538 v8::Handle<Boolean> True() {
539 i::Isolate* isolate = i::Isolate::Current();
540 if (!EnsureInitializedForIsolate(isolate, "v8::True()")) {
541 return v8::Handle<Boolean>();
543 return v8::Handle<Boolean>(
544 ToApi<Boolean>(isolate->factory()->true_value()));
548 v8::Handle<Boolean> False() {
549 i::Isolate* isolate = i::Isolate::Current();
550 if (!EnsureInitializedForIsolate(isolate, "v8::False()")) {
551 return v8::Handle<Boolean>();
553 return v8::Handle<Boolean>(
554 ToApi<Boolean>(isolate->factory()->false_value()));
558 ResourceConstraints::ResourceConstraints()
559 : max_young_space_size_(0),
560 max_old_space_size_(0),
561 max_executable_size_(0),
562 stack_limit_(NULL) { }
565 bool SetResourceConstraints(ResourceConstraints* constraints) {
566 i::Isolate* isolate = EnterIsolateIfNeeded();
568 int young_space_size = constraints->max_young_space_size();
569 int old_gen_size = constraints->max_old_space_size();
570 int max_executable_size = constraints->max_executable_size();
571 if (young_space_size != 0 || old_gen_size != 0 || max_executable_size != 0) {
572 // After initialization it's too late to change Heap constraints.
573 ASSERT(!isolate->IsInitialized());
574 bool result = isolate->heap()->ConfigureHeap(young_space_size / 2,
576 max_executable_size);
577 if (!result) return false;
579 if (constraints->stack_limit() != NULL) {
580 uintptr_t limit = reinterpret_cast<uintptr_t>(constraints->stack_limit());
581 isolate->stack_guard()->SetStackLimit(limit);
587 i::Object** V8::GlobalizeReference(i::Object** obj) {
588 i::Isolate* isolate = i::Isolate::Current();
589 if (IsDeadCheck(isolate, "V8::Persistent::New")) return NULL;
590 LOG_API(isolate, "Persistent::New");
591 i::Handle<i::Object> result =
592 isolate->global_handles()->Create(*obj);
593 return result.location();
597 void V8::MakeWeak(i::Object** object, void* parameters,
598 WeakReferenceCallback callback) {
599 i::Isolate* isolate = i::Isolate::Current();
600 LOG_API(isolate, "MakeWeak");
601 isolate->global_handles()->MakeWeak(object, parameters,
606 void V8::ClearWeak(i::Object** obj) {
607 i::Isolate* isolate = i::Isolate::Current();
608 LOG_API(isolate, "ClearWeak");
609 isolate->global_handles()->ClearWeakness(obj);
613 void V8::MarkIndependent(i::Object** object) {
614 i::Isolate* isolate = i::Isolate::Current();
615 LOG_API(isolate, "MakeIndependent");
616 isolate->global_handles()->MarkIndependent(object);
620 bool V8::IsGlobalNearDeath(i::Object** obj) {
621 i::Isolate* isolate = i::Isolate::Current();
622 LOG_API(isolate, "IsGlobalNearDeath");
623 if (!isolate->IsInitialized()) return false;
624 return i::GlobalHandles::IsNearDeath(obj);
628 bool V8::IsGlobalWeak(i::Object** obj) {
629 i::Isolate* isolate = i::Isolate::Current();
630 LOG_API(isolate, "IsGlobalWeak");
631 if (!isolate->IsInitialized()) return false;
632 return i::GlobalHandles::IsWeak(obj);
636 void V8::DisposeGlobal(i::Object** obj) {
637 i::Isolate* isolate = i::Isolate::Current();
638 LOG_API(isolate, "DisposeGlobal");
639 if (!isolate->IsInitialized()) return;
640 isolate->global_handles()->Destroy(obj);
643 // --- H a n d l e s ---
646 HandleScope::HandleScope() {
647 i::Isolate* isolate = i::Isolate::Current();
648 API_ENTRY_CHECK(isolate, "HandleScope::HandleScope");
649 v8::ImplementationUtilities::HandleScopeData* current =
650 isolate->handle_scope_data();
652 prev_next_ = current->next;
653 prev_limit_ = current->limit;
659 HandleScope::~HandleScope() {
666 void HandleScope::Leave() {
667 ASSERT(isolate_ == i::Isolate::Current());
668 v8::ImplementationUtilities::HandleScopeData* current =
669 isolate_->handle_scope_data();
671 ASSERT(current->level >= 0);
672 current->next = prev_next_;
673 if (current->limit != prev_limit_) {
674 current->limit = prev_limit_;
675 i::HandleScope::DeleteExtensions(isolate_);
679 i::HandleScope::ZapRange(prev_next_, prev_limit_);
684 int HandleScope::NumberOfHandles() {
685 EnsureInitializedForIsolate(
686 i::Isolate::Current(), "HandleScope::NumberOfHandles");
687 return i::HandleScope::NumberOfHandles();
691 i::Object** HandleScope::CreateHandle(i::Object* value) {
692 return i::HandleScope::CreateHandle(value, i::Isolate::Current());
696 i::Object** HandleScope::CreateHandle(i::HeapObject* value) {
697 ASSERT(value->IsHeapObject());
698 return reinterpret_cast<i::Object**>(
699 i::HandleScope::CreateHandle(value, value->GetIsolate()));
703 void Context::Enter() {
704 i::Handle<i::Context> env = Utils::OpenHandle(this);
705 i::Isolate* isolate = env->GetIsolate();
706 if (IsDeadCheck(isolate, "v8::Context::Enter()")) return;
709 isolate->handle_scope_implementer()->EnterContext(env);
711 isolate->handle_scope_implementer()->SaveContext(isolate->context());
712 isolate->set_context(*env);
716 void Context::Exit() {
717 // Exit is essentially a static function and doesn't use the
718 // receiver, so we have to get the current isolate from the thread
720 i::Isolate* isolate = i::Isolate::Current();
721 if (!isolate->IsInitialized()) return;
723 if (!ApiCheck(isolate->handle_scope_implementer()->LeaveLastContext(),
724 "v8::Context::Exit()",
725 "Cannot exit non-entered context")) {
729 // Content of 'last_context' could be NULL.
730 i::Context* last_context =
731 isolate->handle_scope_implementer()->RestoreContext();
732 isolate->set_context(last_context);
736 void Context::SetData(v8::Handle<String> data) {
737 i::Handle<i::Context> env = Utils::OpenHandle(this);
738 i::Isolate* isolate = env->GetIsolate();
739 if (IsDeadCheck(isolate, "v8::Context::SetData()")) return;
740 i::Handle<i::Object> raw_data = Utils::OpenHandle(*data);
741 ASSERT(env->IsGlobalContext());
742 if (env->IsGlobalContext()) {
743 env->set_data(*raw_data);
748 v8::Local<v8::Value> Context::GetData() {
749 i::Handle<i::Context> env = Utils::OpenHandle(this);
750 i::Isolate* isolate = env->GetIsolate();
751 if (IsDeadCheck(isolate, "v8::Context::GetData()")) {
752 return v8::Local<Value>();
754 i::Object* raw_result = NULL;
755 ASSERT(env->IsGlobalContext());
756 if (env->IsGlobalContext()) {
757 raw_result = env->data();
759 return Local<Value>();
761 i::Handle<i::Object> result(raw_result, isolate);
762 return Utils::ToLocal(result);
766 i::Object** v8::HandleScope::RawClose(i::Object** value) {
767 if (!ApiCheck(!is_closed_,
768 "v8::HandleScope::Close()",
769 "Local scope has already been closed")) {
772 LOG_API(isolate_, "CloseHandleScope");
774 // Read the result before popping the handle block.
775 i::Object* result = NULL;
786 // Allocate a new handle on the previous handle block.
787 i::Handle<i::Object> handle(result);
788 return handle.location();
792 // --- N e a n d e r ---
795 // A constructor cannot easily return an error value, therefore it is necessary
796 // to check for a dead VM with ON_BAILOUT before constructing any Neander
797 // objects. To remind you about this there is no HandleScope in the
798 // NeanderObject constructor. When you add one to the site calling the
799 // constructor you should check that you ensured the VM was not dead first.
800 NeanderObject::NeanderObject(int size) {
801 i::Isolate* isolate = i::Isolate::Current();
802 EnsureInitializedForIsolate(isolate, "v8::Nowhere");
804 value_ = isolate->factory()->NewNeanderObject();
805 i::Handle<i::FixedArray> elements = isolate->factory()->NewFixedArray(size);
806 value_->set_elements(*elements);
810 int NeanderObject::size() {
811 return i::FixedArray::cast(value_->elements())->length();
815 NeanderArray::NeanderArray() : obj_(2) {
816 obj_.set(0, i::Smi::FromInt(0));
820 int NeanderArray::length() {
821 return i::Smi::cast(obj_.get(0))->value();
825 i::Object* NeanderArray::get(int offset) {
827 ASSERT(offset < length());
828 return obj_.get(offset + 1);
832 // This method cannot easily return an error value, therefore it is necessary
833 // to check for a dead VM with ON_BAILOUT before calling it. To remind you
834 // about this there is no HandleScope in this method. When you add one to the
835 // site calling this method you should check that you ensured the VM was not
837 void NeanderArray::add(i::Handle<i::Object> value) {
838 int length = this->length();
839 int size = obj_.size();
840 if (length == size - 1) {
841 i::Handle<i::FixedArray> new_elms = FACTORY->NewFixedArray(2 * size);
842 for (int i = 0; i < length; i++)
843 new_elms->set(i + 1, get(i));
844 obj_.value()->set_elements(*new_elms);
846 obj_.set(length + 1, *value);
847 obj_.set(0, i::Smi::FromInt(length + 1));
851 void NeanderArray::set(int index, i::Object* value) {
852 if (index < 0 || index >= this->length()) return;
853 obj_.set(index + 1, value);
857 // --- T e m p l a t e ---
860 static void InitializeTemplate(i::Handle<i::TemplateInfo> that, int type) {
861 that->set_tag(i::Smi::FromInt(type));
865 void Template::Set(v8::Handle<String> name, v8::Handle<Data> value,
866 v8::PropertyAttribute attribute) {
867 i::Isolate* isolate = i::Isolate::Current();
868 if (IsDeadCheck(isolate, "v8::Template::Set()")) return;
870 i::HandleScope scope(isolate);
871 i::Handle<i::Object> list(Utils::OpenHandle(this)->property_list());
872 if (list->IsUndefined()) {
873 list = NeanderArray().value();
874 Utils::OpenHandle(this)->set_property_list(*list);
876 NeanderArray array(list);
877 array.add(Utils::OpenHandle(*name));
878 array.add(Utils::OpenHandle(*value));
879 array.add(Utils::OpenHandle(*v8::Integer::New(attribute)));
883 // --- F u n c t i o n T e m p l a t e ---
884 static void InitializeFunctionTemplate(
885 i::Handle<i::FunctionTemplateInfo> info) {
886 info->set_tag(i::Smi::FromInt(Consts::FUNCTION_TEMPLATE));
891 Local<ObjectTemplate> FunctionTemplate::PrototypeTemplate() {
892 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
893 if (IsDeadCheck(isolate, "v8::FunctionTemplate::PrototypeTemplate()")) {
894 return Local<ObjectTemplate>();
897 i::Handle<i::Object> result(Utils::OpenHandle(this)->prototype_template());
898 if (result->IsUndefined()) {
899 result = Utils::OpenHandle(*ObjectTemplate::New());
900 Utils::OpenHandle(this)->set_prototype_template(*result);
902 return Local<ObjectTemplate>(ToApi<ObjectTemplate>(result));
906 void FunctionTemplate::Inherit(v8::Handle<FunctionTemplate> value) {
907 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
908 if (IsDeadCheck(isolate, "v8::FunctionTemplate::Inherit()")) return;
910 Utils::OpenHandle(this)->set_parent_template(*Utils::OpenHandle(*value));
914 Local<FunctionTemplate> FunctionTemplate::New(InvocationCallback callback,
915 v8::Handle<Value> data, v8::Handle<Signature> signature) {
916 i::Isolate* isolate = i::Isolate::Current();
917 EnsureInitializedForIsolate(isolate, "v8::FunctionTemplate::New()");
918 LOG_API(isolate, "FunctionTemplate::New");
920 i::Handle<i::Struct> struct_obj =
921 isolate->factory()->NewStruct(i::FUNCTION_TEMPLATE_INFO_TYPE);
922 i::Handle<i::FunctionTemplateInfo> obj =
923 i::Handle<i::FunctionTemplateInfo>::cast(struct_obj);
924 InitializeFunctionTemplate(obj);
925 int next_serial_number = isolate->next_serial_number();
926 isolate->set_next_serial_number(next_serial_number + 1);
927 obj->set_serial_number(i::Smi::FromInt(next_serial_number));
929 if (data.IsEmpty()) data = v8::Undefined();
930 Utils::ToLocal(obj)->SetCallHandler(callback, data);
932 obj->set_undetectable(false);
933 obj->set_needs_access_check(false);
935 if (!signature.IsEmpty())
936 obj->set_signature(*Utils::OpenHandle(*signature));
937 return Utils::ToLocal(obj);
941 Local<Signature> Signature::New(Handle<FunctionTemplate> receiver,
942 int argc, Handle<FunctionTemplate> argv[]) {
943 i::Isolate* isolate = i::Isolate::Current();
944 EnsureInitializedForIsolate(isolate, "v8::Signature::New()");
945 LOG_API(isolate, "Signature::New");
947 i::Handle<i::Struct> struct_obj =
948 isolate->factory()->NewStruct(i::SIGNATURE_INFO_TYPE);
949 i::Handle<i::SignatureInfo> obj =
950 i::Handle<i::SignatureInfo>::cast(struct_obj);
951 if (!receiver.IsEmpty()) obj->set_receiver(*Utils::OpenHandle(*receiver));
953 i::Handle<i::FixedArray> args = isolate->factory()->NewFixedArray(argc);
954 for (int i = 0; i < argc; i++) {
955 if (!argv[i].IsEmpty())
956 args->set(i, *Utils::OpenHandle(*argv[i]));
958 obj->set_args(*args);
960 return Utils::ToLocal(obj);
964 Local<TypeSwitch> TypeSwitch::New(Handle<FunctionTemplate> type) {
965 Handle<FunctionTemplate> types[1] = { type };
966 return TypeSwitch::New(1, types);
970 Local<TypeSwitch> TypeSwitch::New(int argc, Handle<FunctionTemplate> types[]) {
971 i::Isolate* isolate = i::Isolate::Current();
972 EnsureInitializedForIsolate(isolate, "v8::TypeSwitch::New()");
973 LOG_API(isolate, "TypeSwitch::New");
975 i::Handle<i::FixedArray> vector = isolate->factory()->NewFixedArray(argc);
976 for (int i = 0; i < argc; i++)
977 vector->set(i, *Utils::OpenHandle(*types[i]));
978 i::Handle<i::Struct> struct_obj =
979 isolate->factory()->NewStruct(i::TYPE_SWITCH_INFO_TYPE);
980 i::Handle<i::TypeSwitchInfo> obj =
981 i::Handle<i::TypeSwitchInfo>::cast(struct_obj);
982 obj->set_types(*vector);
983 return Utils::ToLocal(obj);
987 int TypeSwitch::match(v8::Handle<Value> value) {
988 i::Isolate* isolate = i::Isolate::Current();
989 LOG_API(isolate, "TypeSwitch::match");
991 i::Handle<i::Object> obj = Utils::OpenHandle(*value);
992 i::Handle<i::TypeSwitchInfo> info = Utils::OpenHandle(this);
993 i::FixedArray* types = i::FixedArray::cast(info->types());
994 for (int i = 0; i < types->length(); i++) {
995 if (obj->IsInstanceOf(i::FunctionTemplateInfo::cast(types->get(i))))
1002 #define SET_FIELD_WRAPPED(obj, setter, cdata) do { \
1003 i::Handle<i::Object> foreign = FromCData(cdata); \
1004 (obj)->setter(*foreign); \
1008 void FunctionTemplate::SetCallHandler(InvocationCallback callback,
1009 v8::Handle<Value> data) {
1010 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1011 if (IsDeadCheck(isolate, "v8::FunctionTemplate::SetCallHandler()")) return;
1013 i::HandleScope scope(isolate);
1014 i::Handle<i::Struct> struct_obj =
1015 isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE);
1016 i::Handle<i::CallHandlerInfo> obj =
1017 i::Handle<i::CallHandlerInfo>::cast(struct_obj);
1018 SET_FIELD_WRAPPED(obj, set_callback, callback);
1019 if (data.IsEmpty()) data = v8::Undefined();
1020 obj->set_data(*Utils::OpenHandle(*data));
1021 Utils::OpenHandle(this)->set_call_code(*obj);
1025 static i::Handle<i::AccessorInfo> MakeAccessorInfo(
1026 v8::Handle<String> name,
1027 AccessorGetter getter,
1028 AccessorSetter setter,
1029 v8::Handle<Value> data,
1030 v8::AccessControl settings,
1031 v8::PropertyAttribute attributes) {
1032 i::Handle<i::AccessorInfo> obj = FACTORY->NewAccessorInfo();
1033 ASSERT(getter != NULL);
1034 SET_FIELD_WRAPPED(obj, set_getter, getter);
1035 SET_FIELD_WRAPPED(obj, set_setter, setter);
1036 if (data.IsEmpty()) data = v8::Undefined();
1037 obj->set_data(*Utils::OpenHandle(*data));
1038 obj->set_name(*Utils::OpenHandle(*name));
1039 if (settings & ALL_CAN_READ) obj->set_all_can_read(true);
1040 if (settings & ALL_CAN_WRITE) obj->set_all_can_write(true);
1041 if (settings & PROHIBITS_OVERWRITING) obj->set_prohibits_overwriting(true);
1042 obj->set_property_attributes(static_cast<PropertyAttributes>(attributes));
1047 void FunctionTemplate::AddInstancePropertyAccessor(
1048 v8::Handle<String> name,
1049 AccessorGetter getter,
1050 AccessorSetter setter,
1051 v8::Handle<Value> data,
1052 v8::AccessControl settings,
1053 v8::PropertyAttribute attributes) {
1054 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1055 if (IsDeadCheck(isolate,
1056 "v8::FunctionTemplate::AddInstancePropertyAccessor()")) {
1060 i::HandleScope scope(isolate);
1062 i::Handle<i::AccessorInfo> obj = MakeAccessorInfo(name,
1063 getter, setter, data,
1064 settings, attributes);
1065 i::Handle<i::Object> list(Utils::OpenHandle(this)->property_accessors());
1066 if (list->IsUndefined()) {
1067 list = NeanderArray().value();
1068 Utils::OpenHandle(this)->set_property_accessors(*list);
1070 NeanderArray array(list);
1075 Local<ObjectTemplate> FunctionTemplate::InstanceTemplate() {
1076 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1077 if (IsDeadCheck(isolate, "v8::FunctionTemplate::InstanceTemplate()")
1078 || EmptyCheck("v8::FunctionTemplate::InstanceTemplate()", this))
1079 return Local<ObjectTemplate>();
1081 if (Utils::OpenHandle(this)->instance_template()->IsUndefined()) {
1082 Local<ObjectTemplate> templ =
1083 ObjectTemplate::New(v8::Handle<FunctionTemplate>(this));
1084 Utils::OpenHandle(this)->set_instance_template(*Utils::OpenHandle(*templ));
1086 i::Handle<i::ObjectTemplateInfo> result(i::ObjectTemplateInfo::cast(
1087 Utils::OpenHandle(this)->instance_template()));
1088 return Utils::ToLocal(result);
1092 void FunctionTemplate::SetClassName(Handle<String> name) {
1093 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1094 if (IsDeadCheck(isolate, "v8::FunctionTemplate::SetClassName()")) return;
1096 Utils::OpenHandle(this)->set_class_name(*Utils::OpenHandle(*name));
1100 void FunctionTemplate::SetHiddenPrototype(bool value) {
1101 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1102 if (IsDeadCheck(isolate, "v8::FunctionTemplate::SetHiddenPrototype()")) {
1106 Utils::OpenHandle(this)->set_hidden_prototype(value);
1110 void FunctionTemplate::ReadOnlyPrototype() {
1111 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1112 if (IsDeadCheck(isolate, "v8::FunctionTemplate::SetPrototypeAttributes()")) {
1116 Utils::OpenHandle(this)->set_read_only_prototype(true);
1120 void FunctionTemplate::SetNamedInstancePropertyHandler(
1121 NamedPropertyGetter getter,
1122 NamedPropertySetter setter,
1123 NamedPropertyQuery query,
1124 NamedPropertyDeleter remover,
1125 NamedPropertyEnumerator enumerator,
1127 Handle<Value> data) {
1128 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1129 if (IsDeadCheck(isolate,
1130 "v8::FunctionTemplate::SetNamedInstancePropertyHandler()")) {
1134 i::HandleScope scope(isolate);
1135 i::Handle<i::Struct> struct_obj =
1136 isolate->factory()->NewStruct(i::INTERCEPTOR_INFO_TYPE);
1137 i::Handle<i::InterceptorInfo> obj =
1138 i::Handle<i::InterceptorInfo>::cast(struct_obj);
1140 if (getter != 0) SET_FIELD_WRAPPED(obj, set_getter, getter);
1141 if (setter != 0) SET_FIELD_WRAPPED(obj, set_setter, setter);
1142 if (query != 0) SET_FIELD_WRAPPED(obj, set_query, query);
1143 if (remover != 0) SET_FIELD_WRAPPED(obj, set_deleter, remover);
1144 if (enumerator != 0) SET_FIELD_WRAPPED(obj, set_enumerator, enumerator);
1145 obj->set_is_fallback(i::Smi::FromInt(is_fallback));
1147 if (data.IsEmpty()) data = v8::Undefined();
1148 obj->set_data(*Utils::OpenHandle(*data));
1149 Utils::OpenHandle(this)->set_named_property_handler(*obj);
1153 void FunctionTemplate::SetIndexedInstancePropertyHandler(
1154 IndexedPropertyGetter getter,
1155 IndexedPropertySetter setter,
1156 IndexedPropertyQuery query,
1157 IndexedPropertyDeleter remover,
1158 IndexedPropertyEnumerator enumerator,
1159 Handle<Value> data) {
1160 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1161 if (IsDeadCheck(isolate,
1162 "v8::FunctionTemplate::SetIndexedInstancePropertyHandler()")) {
1166 i::HandleScope scope(isolate);
1167 i::Handle<i::Struct> struct_obj =
1168 isolate->factory()->NewStruct(i::INTERCEPTOR_INFO_TYPE);
1169 i::Handle<i::InterceptorInfo> obj =
1170 i::Handle<i::InterceptorInfo>::cast(struct_obj);
1172 if (getter != 0) SET_FIELD_WRAPPED(obj, set_getter, getter);
1173 if (setter != 0) SET_FIELD_WRAPPED(obj, set_setter, setter);
1174 if (query != 0) SET_FIELD_WRAPPED(obj, set_query, query);
1175 if (remover != 0) SET_FIELD_WRAPPED(obj, set_deleter, remover);
1176 if (enumerator != 0) SET_FIELD_WRAPPED(obj, set_enumerator, enumerator);
1178 if (data.IsEmpty()) data = v8::Undefined();
1179 obj->set_data(*Utils::OpenHandle(*data));
1180 Utils::OpenHandle(this)->set_indexed_property_handler(*obj);
1184 void FunctionTemplate::SetInstanceCallAsFunctionHandler(
1185 InvocationCallback callback,
1186 Handle<Value> data) {
1187 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1188 if (IsDeadCheck(isolate,
1189 "v8::FunctionTemplate::SetInstanceCallAsFunctionHandler()")) {
1193 i::HandleScope scope(isolate);
1194 i::Handle<i::Struct> struct_obj =
1195 isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE);
1196 i::Handle<i::CallHandlerInfo> obj =
1197 i::Handle<i::CallHandlerInfo>::cast(struct_obj);
1198 SET_FIELD_WRAPPED(obj, set_callback, callback);
1199 if (data.IsEmpty()) data = v8::Undefined();
1200 obj->set_data(*Utils::OpenHandle(*data));
1201 Utils::OpenHandle(this)->set_instance_call_handler(*obj);
1205 // --- O b j e c t T e m p l a t e ---
1208 Local<ObjectTemplate> ObjectTemplate::New() {
1209 return New(Local<FunctionTemplate>());
1213 Local<ObjectTemplate> ObjectTemplate::New(
1214 v8::Handle<FunctionTemplate> constructor) {
1215 i::Isolate* isolate = i::Isolate::Current();
1216 if (IsDeadCheck(isolate, "v8::ObjectTemplate::New()")) {
1217 return Local<ObjectTemplate>();
1219 EnsureInitializedForIsolate(isolate, "v8::ObjectTemplate::New()");
1220 LOG_API(isolate, "ObjectTemplate::New");
1222 i::Handle<i::Struct> struct_obj =
1223 isolate->factory()->NewStruct(i::OBJECT_TEMPLATE_INFO_TYPE);
1224 i::Handle<i::ObjectTemplateInfo> obj =
1225 i::Handle<i::ObjectTemplateInfo>::cast(struct_obj);
1226 InitializeTemplate(obj, Consts::OBJECT_TEMPLATE);
1227 if (!constructor.IsEmpty())
1228 obj->set_constructor(*Utils::OpenHandle(*constructor));
1229 obj->set_internal_field_count(i::Smi::FromInt(0));
1230 return Utils::ToLocal(obj);
1234 // Ensure that the object template has a constructor. If no
1235 // constructor is available we create one.
1236 static void EnsureConstructor(ObjectTemplate* object_template) {
1237 if (Utils::OpenHandle(object_template)->constructor()->IsUndefined()) {
1238 Local<FunctionTemplate> templ = FunctionTemplate::New();
1239 i::Handle<i::FunctionTemplateInfo> constructor = Utils::OpenHandle(*templ);
1240 constructor->set_instance_template(*Utils::OpenHandle(object_template));
1241 Utils::OpenHandle(object_template)->set_constructor(*constructor);
1246 void ObjectTemplate::SetAccessor(v8::Handle<String> name,
1247 AccessorGetter getter,
1248 AccessorSetter setter,
1249 v8::Handle<Value> data,
1250 AccessControl settings,
1251 PropertyAttribute attribute) {
1252 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1253 if (IsDeadCheck(isolate, "v8::ObjectTemplate::SetAccessor()")) return;
1255 i::HandleScope scope(isolate);
1256 EnsureConstructor(this);
1257 i::FunctionTemplateInfo* constructor =
1258 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1259 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1260 Utils::ToLocal(cons)->AddInstancePropertyAccessor(name,
1269 void ObjectTemplate::SetNamedPropertyHandler(NamedPropertyGetter getter,
1270 NamedPropertySetter setter,
1271 NamedPropertyQuery query,
1272 NamedPropertyDeleter remover,
1273 NamedPropertyEnumerator enumerator,
1274 Handle<Value> data) {
1275 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1276 if (IsDeadCheck(isolate, "v8::ObjectTemplate::SetNamedPropertyHandler()")) {
1280 i::HandleScope scope(isolate);
1281 EnsureConstructor(this);
1282 i::FunctionTemplateInfo* constructor =
1283 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1284 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1285 Utils::ToLocal(cons)->SetNamedInstancePropertyHandler(getter,
1295 void ObjectTemplate::SetFallbackPropertyHandler(NamedPropertyGetter getter,
1296 NamedPropertySetter setter,
1297 NamedPropertyQuery query,
1298 NamedPropertyDeleter remover,
1299 NamedPropertyEnumerator enumerator,
1300 Handle<Value> data) {
1301 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1302 if (IsDeadCheck(isolate, "v8::ObjectTemplate::SetNamedPropertyHandler()")) {
1306 i::HandleScope scope(isolate);
1307 EnsureConstructor(this);
1308 i::FunctionTemplateInfo* constructor =
1309 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1310 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1311 Utils::ToLocal(cons)->SetNamedInstancePropertyHandler(getter,
1321 void ObjectTemplate::MarkAsUndetectable() {
1322 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1323 if (IsDeadCheck(isolate, "v8::ObjectTemplate::MarkAsUndetectable()")) return;
1325 i::HandleScope scope(isolate);
1326 EnsureConstructor(this);
1327 i::FunctionTemplateInfo* constructor =
1328 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1329 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1330 cons->set_undetectable(true);
1334 void ObjectTemplate::SetAccessCheckCallbacks(
1335 NamedSecurityCallback named_callback,
1336 IndexedSecurityCallback indexed_callback,
1338 bool turned_on_by_default) {
1339 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1340 if (IsDeadCheck(isolate, "v8::ObjectTemplate::SetAccessCheckCallbacks()")) {
1344 i::HandleScope scope(isolate);
1345 EnsureConstructor(this);
1347 i::Handle<i::Struct> struct_info =
1348 isolate->factory()->NewStruct(i::ACCESS_CHECK_INFO_TYPE);
1349 i::Handle<i::AccessCheckInfo> info =
1350 i::Handle<i::AccessCheckInfo>::cast(struct_info);
1352 SET_FIELD_WRAPPED(info, set_named_callback, named_callback);
1353 SET_FIELD_WRAPPED(info, set_indexed_callback, indexed_callback);
1355 if (data.IsEmpty()) data = v8::Undefined();
1356 info->set_data(*Utils::OpenHandle(*data));
1358 i::FunctionTemplateInfo* constructor =
1359 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1360 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1361 cons->set_access_check_info(*info);
1362 cons->set_needs_access_check(turned_on_by_default);
1366 void ObjectTemplate::SetIndexedPropertyHandler(
1367 IndexedPropertyGetter getter,
1368 IndexedPropertySetter setter,
1369 IndexedPropertyQuery query,
1370 IndexedPropertyDeleter remover,
1371 IndexedPropertyEnumerator enumerator,
1372 Handle<Value> data) {
1373 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1374 if (IsDeadCheck(isolate, "v8::ObjectTemplate::SetIndexedPropertyHandler()")) {
1378 i::HandleScope scope(isolate);
1379 EnsureConstructor(this);
1380 i::FunctionTemplateInfo* constructor =
1381 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1382 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1383 Utils::ToLocal(cons)->SetIndexedInstancePropertyHandler(getter,
1392 void ObjectTemplate::SetCallAsFunctionHandler(InvocationCallback callback,
1393 Handle<Value> data) {
1394 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1395 if (IsDeadCheck(isolate,
1396 "v8::ObjectTemplate::SetCallAsFunctionHandler()")) {
1400 i::HandleScope scope(isolate);
1401 EnsureConstructor(this);
1402 i::FunctionTemplateInfo* constructor =
1403 i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1404 i::Handle<i::FunctionTemplateInfo> cons(constructor);
1405 Utils::ToLocal(cons)->SetInstanceCallAsFunctionHandler(callback, data);
1409 int ObjectTemplate::InternalFieldCount() {
1410 if (IsDeadCheck(Utils::OpenHandle(this)->GetIsolate(),
1411 "v8::ObjectTemplate::InternalFieldCount()")) {
1414 return i::Smi::cast(Utils::OpenHandle(this)->internal_field_count())->value();
1418 void ObjectTemplate::SetInternalFieldCount(int value) {
1419 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1420 if (IsDeadCheck(isolate, "v8::ObjectTemplate::SetInternalFieldCount()")) {
1423 if (!ApiCheck(i::Smi::IsValid(value),
1424 "v8::ObjectTemplate::SetInternalFieldCount()",
1425 "Invalid internal field count")) {
1430 // The internal field count is set by the constructor function's
1431 // construct code, so we ensure that there is a constructor
1432 // function to do the setting.
1433 EnsureConstructor(this);
1435 Utils::OpenHandle(this)->set_internal_field_count(i::Smi::FromInt(value));
1439 bool ObjectTemplate::HasExternalResource()
1441 if (IsDeadCheck(Utils::OpenHandle(this)->GetIsolate(),
1442 "v8::ObjectTemplate::HasExternalResource()")) {
1445 return !Utils::OpenHandle(this)->has_external_resource()->IsUndefined();
1449 void ObjectTemplate::SetHasExternalResource(bool value)
1451 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1452 if (IsDeadCheck(isolate, "v8::ObjectTemplate::SetHasExternalResource()")) {
1457 EnsureConstructor(this);
1460 Utils::OpenHandle(this)->set_has_external_resource(i::Smi::FromInt(1));
1462 Utils::OpenHandle(this)->set_has_external_resource(Utils::OpenHandle(this)->GetHeap()->undefined_value());
1467 // --- S c r i p t D a t a ---
1470 ScriptData* ScriptData::PreCompile(const char* input, int length) {
1471 i::Utf8ToUC16CharacterStream stream(
1472 reinterpret_cast<const unsigned char*>(input), length);
1473 return i::ParserApi::PreParse(&stream, NULL, i::FLAG_harmony_scoping);
1477 ScriptData* ScriptData::PreCompile(v8::Handle<String> source) {
1478 i::Handle<i::String> str = Utils::OpenHandle(*source);
1479 if (str->IsExternalTwoByteString()) {
1480 i::ExternalTwoByteStringUC16CharacterStream stream(
1481 i::Handle<i::ExternalTwoByteString>::cast(str), 0, str->length());
1482 return i::ParserApi::PreParse(&stream, NULL, i::FLAG_harmony_scoping);
1484 i::GenericStringUC16CharacterStream stream(str, 0, str->length());
1485 return i::ParserApi::PreParse(&stream, NULL, i::FLAG_harmony_scoping);
1490 ScriptData* ScriptData::New(const char* data, int length) {
1491 // Return an empty ScriptData if the length is obviously invalid.
1492 if (length % sizeof(unsigned) != 0) {
1493 return new i::ScriptDataImpl();
1496 // Copy the data to ensure it is properly aligned.
1497 int deserialized_data_length = length / sizeof(unsigned);
1498 // If aligned, don't create a copy of the data.
1499 if (reinterpret_cast<intptr_t>(data) % sizeof(unsigned) == 0) {
1500 return new i::ScriptDataImpl(data, length);
1502 // Copy the data to align it.
1503 unsigned* deserialized_data = i::NewArray<unsigned>(deserialized_data_length);
1504 i::OS::MemCopy(deserialized_data, data, length);
1506 return new i::ScriptDataImpl(
1507 i::Vector<unsigned>(deserialized_data, deserialized_data_length));
1511 // --- S c r i p t ---
1514 Local<Script> Script::New(v8::Handle<String> source,
1515 v8::ScriptOrigin* origin,
1516 v8::ScriptData* pre_data,
1517 v8::Handle<String> script_data,
1518 v8::Script::CompileFlags compile_flags) {
1519 i::Isolate* isolate = i::Isolate::Current();
1520 ON_BAILOUT(isolate, "v8::Script::New()", return Local<Script>());
1521 LOG_API(isolate, "Script::New");
1523 i::Handle<i::String> str = Utils::OpenHandle(*source);
1524 i::Handle<i::Object> name_obj;
1525 int line_offset = 0;
1526 int column_offset = 0;
1527 if (origin != NULL) {
1528 if (!origin->ResourceName().IsEmpty()) {
1529 name_obj = Utils::OpenHandle(*origin->ResourceName());
1531 if (!origin->ResourceLineOffset().IsEmpty()) {
1532 line_offset = static_cast<int>(origin->ResourceLineOffset()->Value());
1534 if (!origin->ResourceColumnOffset().IsEmpty()) {
1535 column_offset = static_cast<int>(origin->ResourceColumnOffset()->Value());
1538 EXCEPTION_PREAMBLE(isolate);
1539 i::ScriptDataImpl* pre_data_impl = static_cast<i::ScriptDataImpl*>(pre_data);
1540 // We assert that the pre-data is sane, even though we can actually
1541 // handle it if it turns out not to be in release mode.
1542 ASSERT(pre_data_impl == NULL || pre_data_impl->SanityCheck());
1543 // If the pre-data isn't sane we simply ignore it
1544 if (pre_data_impl != NULL && !pre_data_impl->SanityCheck()) {
1545 pre_data_impl = NULL;
1547 i::Handle<i::SharedFunctionInfo> result =
1548 i::Compiler::Compile(str,
1554 Utils::OpenHandle(*script_data),
1555 i::NOT_NATIVES_CODE,
1557 has_pending_exception = result.is_null();
1558 EXCEPTION_BAILOUT_CHECK(isolate, Local<Script>());
1559 return Local<Script>(ToApi<Script>(result));
1563 Local<Script> Script::New(v8::Handle<String> source,
1564 v8::Handle<Value> file_name,
1565 v8::Script::CompileFlags compile_flags) {
1566 ScriptOrigin origin(file_name);
1567 return New(source, &origin, 0, Handle<String>(), compile_flags);
1571 Local<Script> Script::Compile(v8::Handle<String> source,
1572 v8::ScriptOrigin* origin,
1573 v8::ScriptData* pre_data,
1574 v8::Handle<String> script_data,
1575 v8::Script::CompileFlags compile_flags) {
1576 i::Isolate* isolate = i::Isolate::Current();
1577 ON_BAILOUT(isolate, "v8::Script::Compile()", return Local<Script>());
1578 LOG_API(isolate, "Script::Compile");
1580 Local<Script> generic = New(source, origin, pre_data, script_data, compile_flags);
1581 if (generic.IsEmpty())
1583 i::Handle<i::Object> obj = Utils::OpenHandle(*generic);
1584 i::Handle<i::SharedFunctionInfo> function =
1585 i::Handle<i::SharedFunctionInfo>(i::SharedFunctionInfo::cast(*obj));
1586 i::Handle<i::JSFunction> result =
1587 isolate->factory()->NewFunctionFromSharedFunctionInfo(
1589 isolate->global_context());
1590 return Local<Script>(ToApi<Script>(result));
1594 Local<Script> Script::Compile(v8::Handle<String> source,
1595 v8::Handle<Value> file_name,
1596 v8::Handle<String> script_data,
1597 v8::Script::CompileFlags compile_flags) {
1598 ScriptOrigin origin(file_name);
1599 return Compile(source, &origin, 0, script_data, compile_flags);
1603 Local<Value> Script::Run() {
1604 return Run(Handle<Object>());
1607 Local<Value> Script::Run(Handle<Object> qml) {
1608 i::Isolate* isolate = i::Isolate::Current();
1609 ON_BAILOUT(isolate, "v8::Script::Run()", return Local<Value>());
1610 LOG_API(isolate, "Script::Run");
1612 i::Object* raw_result = NULL;
1614 i::HandleScope scope(isolate);
1615 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1616 i::Handle<i::JSFunction> fun;
1617 if (obj->IsSharedFunctionInfo()) {
1618 i::Handle<i::SharedFunctionInfo>
1619 function_info(i::SharedFunctionInfo::cast(*obj), isolate);
1620 fun = isolate->factory()->NewFunctionFromSharedFunctionInfo(
1621 function_info, isolate->global_context());
1623 fun = i::Handle<i::JSFunction>(i::JSFunction::cast(*obj), isolate);
1625 EXCEPTION_PREAMBLE(isolate);
1626 i::Handle<i::Object> qmlglobal = Utils::OpenHandle(*qml);
1627 i::Handle<i::Object> receiver(
1628 isolate->context()->global_proxy(), isolate);
1629 i::Handle<i::Object> result =
1630 i::Execution::Call(fun, receiver, 0, NULL, &has_pending_exception, false, qmlglobal);
1631 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
1632 raw_result = *result;
1634 i::Handle<i::Object> result(raw_result, isolate);
1635 return Utils::ToLocal(result);
1639 static i::Handle<i::SharedFunctionInfo> OpenScript(Script* script) {
1640 i::Handle<i::Object> obj = Utils::OpenHandle(script);
1641 i::Handle<i::SharedFunctionInfo> result;
1642 if (obj->IsSharedFunctionInfo()) {
1644 i::Handle<i::SharedFunctionInfo>(i::SharedFunctionInfo::cast(*obj));
1647 i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared());
1653 Local<Value> Script::Id() {
1654 i::Isolate* isolate = i::Isolate::Current();
1655 ON_BAILOUT(isolate, "v8::Script::Id()", return Local<Value>());
1656 LOG_API(isolate, "Script::Id");
1657 i::Object* raw_id = NULL;
1659 i::HandleScope scope(isolate);
1660 i::Handle<i::SharedFunctionInfo> function_info = OpenScript(this);
1661 i::Handle<i::Script> script(i::Script::cast(function_info->script()));
1662 i::Handle<i::Object> id(script->id());
1665 i::Handle<i::Object> id(raw_id);
1666 return Utils::ToLocal(id);
1670 void Script::SetData(v8::Handle<String> data) {
1671 i::Isolate* isolate = i::Isolate::Current();
1672 ON_BAILOUT(isolate, "v8::Script::SetData()", return);
1673 LOG_API(isolate, "Script::SetData");
1675 i::HandleScope scope(isolate);
1676 i::Handle<i::SharedFunctionInfo> function_info = OpenScript(this);
1677 i::Handle<i::Object> raw_data = Utils::OpenHandle(*data);
1678 i::Handle<i::Script> script(i::Script::cast(function_info->script()));
1679 script->set_data(*raw_data);
1684 // --- E x c e p t i o n s ---
1687 v8::TryCatch::TryCatch()
1688 : isolate_(i::Isolate::Current()),
1689 next_(isolate_->try_catch_handler_address()),
1690 exception_(isolate_->heap()->the_hole_value()),
1691 message_(i::Smi::FromInt(0)),
1693 can_continue_(true),
1694 capture_message_(true),
1696 isolate_->RegisterTryCatchHandler(this);
1700 v8::TryCatch::~TryCatch() {
1701 ASSERT(isolate_ == i::Isolate::Current());
1703 v8::HandleScope scope;
1704 v8::Local<v8::Value> exc = v8::Local<v8::Value>::New(Exception());
1705 isolate_->UnregisterTryCatchHandler(this);
1706 v8::ThrowException(exc);
1708 isolate_->UnregisterTryCatchHandler(this);
1713 bool v8::TryCatch::HasCaught() const {
1714 return !reinterpret_cast<i::Object*>(exception_)->IsTheHole();
1718 bool v8::TryCatch::CanContinue() const {
1719 return can_continue_;
1723 v8::Handle<v8::Value> v8::TryCatch::ReThrow() {
1724 if (!HasCaught()) return v8::Local<v8::Value>();
1726 return v8::Undefined();
1730 v8::Local<Value> v8::TryCatch::Exception() const {
1731 ASSERT(isolate_ == i::Isolate::Current());
1733 // Check for out of memory exception.
1734 i::Object* exception = reinterpret_cast<i::Object*>(exception_);
1735 return v8::Utils::ToLocal(i::Handle<i::Object>(exception, isolate_));
1737 return v8::Local<Value>();
1742 v8::Local<Value> v8::TryCatch::StackTrace() const {
1743 ASSERT(isolate_ == i::Isolate::Current());
1745 i::Object* raw_obj = reinterpret_cast<i::Object*>(exception_);
1746 if (!raw_obj->IsJSObject()) return v8::Local<Value>();
1747 i::HandleScope scope(isolate_);
1748 i::Handle<i::JSObject> obj(i::JSObject::cast(raw_obj), isolate_);
1749 i::Handle<i::String> name = isolate_->factory()->LookupAsciiSymbol("stack");
1750 if (!obj->HasProperty(*name)) return v8::Local<Value>();
1751 i::Handle<i::Object> value = i::GetProperty(obj, name);
1752 if (value.is_null()) return v8::Local<Value>();
1753 return v8::Utils::ToLocal(scope.CloseAndEscape(value));
1755 return v8::Local<Value>();
1760 v8::Local<v8::Message> v8::TryCatch::Message() const {
1761 ASSERT(isolate_ == i::Isolate::Current());
1762 if (HasCaught() && message_ != i::Smi::FromInt(0)) {
1763 i::Object* message = reinterpret_cast<i::Object*>(message_);
1764 return v8::Utils::MessageToLocal(i::Handle<i::Object>(message, isolate_));
1766 return v8::Local<v8::Message>();
1771 void v8::TryCatch::Reset() {
1772 ASSERT(isolate_ == i::Isolate::Current());
1773 exception_ = isolate_->heap()->the_hole_value();
1774 message_ = i::Smi::FromInt(0);
1778 void v8::TryCatch::SetVerbose(bool value) {
1779 is_verbose_ = value;
1783 void v8::TryCatch::SetCaptureMessage(bool value) {
1784 capture_message_ = value;
1788 // --- M e s s a g e ---
1791 Local<String> Message::Get() const {
1792 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1793 ON_BAILOUT(isolate, "v8::Message::Get()", return Local<String>());
1796 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1797 i::Handle<i::String> raw_result = i::MessageHandler::GetMessage(obj);
1798 Local<String> result = Utils::ToLocal(raw_result);
1799 return scope.Close(result);
1803 v8::Handle<Value> Message::GetScriptResourceName() const {
1804 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1805 if (IsDeadCheck(isolate, "v8::Message::GetScriptResourceName()")) {
1806 return Local<String>();
1810 i::Handle<i::JSMessageObject> message =
1811 i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
1812 // Return this.script.name.
1813 i::Handle<i::JSValue> script =
1814 i::Handle<i::JSValue>::cast(i::Handle<i::Object>(message->script()));
1815 i::Handle<i::Object> resource_name(i::Script::cast(script->value())->name());
1816 return scope.Close(Utils::ToLocal(resource_name));
1820 v8::Handle<Value> Message::GetScriptData() const {
1821 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1822 if (IsDeadCheck(isolate, "v8::Message::GetScriptResourceData()")) {
1823 return Local<Value>();
1827 i::Handle<i::JSMessageObject> message =
1828 i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
1829 // Return this.script.data.
1830 i::Handle<i::JSValue> script =
1831 i::Handle<i::JSValue>::cast(i::Handle<i::Object>(message->script()));
1832 i::Handle<i::Object> data(i::Script::cast(script->value())->data());
1833 return scope.Close(Utils::ToLocal(data));
1837 v8::Handle<v8::StackTrace> Message::GetStackTrace() const {
1838 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1839 if (IsDeadCheck(isolate, "v8::Message::GetStackTrace()")) {
1840 return Local<v8::StackTrace>();
1844 i::Handle<i::JSMessageObject> message =
1845 i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
1846 i::Handle<i::Object> stackFramesObj(message->stack_frames());
1847 if (!stackFramesObj->IsJSArray()) return v8::Handle<v8::StackTrace>();
1848 i::Handle<i::JSArray> stackTrace =
1849 i::Handle<i::JSArray>::cast(stackFramesObj);
1850 return scope.Close(Utils::StackTraceToLocal(stackTrace));
1854 static i::Handle<i::Object> CallV8HeapFunction(const char* name,
1855 i::Handle<i::Object> recv,
1857 i::Handle<i::Object> argv[],
1858 bool* has_pending_exception) {
1859 i::Isolate* isolate = i::Isolate::Current();
1860 i::Handle<i::String> fmt_str = isolate->factory()->LookupAsciiSymbol(name);
1861 i::Object* object_fun =
1862 isolate->js_builtins_object()->GetPropertyNoExceptionThrown(*fmt_str);
1863 i::Handle<i::JSFunction> fun =
1864 i::Handle<i::JSFunction>(i::JSFunction::cast(object_fun));
1865 i::Handle<i::Object> value =
1866 i::Execution::Call(fun, recv, argc, argv, has_pending_exception);
1871 static i::Handle<i::Object> CallV8HeapFunction(const char* name,
1872 i::Handle<i::Object> data,
1873 bool* has_pending_exception) {
1874 i::Handle<i::Object> argv[] = { data };
1875 return CallV8HeapFunction(name,
1876 i::Isolate::Current()->js_builtins_object(),
1879 has_pending_exception);
1883 int Message::GetLineNumber() const {
1884 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1885 ON_BAILOUT(isolate, "v8::Message::GetLineNumber()", return kNoLineNumberInfo);
1887 i::HandleScope scope(isolate);
1889 EXCEPTION_PREAMBLE(isolate);
1890 i::Handle<i::Object> result = CallV8HeapFunction("GetLineNumber",
1891 Utils::OpenHandle(this),
1892 &has_pending_exception);
1893 EXCEPTION_BAILOUT_CHECK(isolate, 0);
1894 return static_cast<int>(result->Number());
1898 int Message::GetStartPosition() const {
1899 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1900 if (IsDeadCheck(isolate, "v8::Message::GetStartPosition()")) return 0;
1902 i::HandleScope scope(isolate);
1903 i::Handle<i::JSMessageObject> message =
1904 i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
1905 return message->start_position();
1909 int Message::GetEndPosition() const {
1910 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1911 if (IsDeadCheck(isolate, "v8::Message::GetEndPosition()")) return 0;
1913 i::HandleScope scope(isolate);
1914 i::Handle<i::JSMessageObject> message =
1915 i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
1916 return message->end_position();
1920 int Message::GetStartColumn() const {
1921 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1922 if (IsDeadCheck(isolate, "v8::Message::GetStartColumn()")) {
1923 return kNoColumnInfo;
1926 i::HandleScope scope(isolate);
1927 i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this);
1928 EXCEPTION_PREAMBLE(isolate);
1929 i::Handle<i::Object> start_col_obj = CallV8HeapFunction(
1930 "GetPositionInLine",
1932 &has_pending_exception);
1933 EXCEPTION_BAILOUT_CHECK(isolate, 0);
1934 return static_cast<int>(start_col_obj->Number());
1938 int Message::GetEndColumn() const {
1939 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1940 if (IsDeadCheck(isolate, "v8::Message::GetEndColumn()")) return kNoColumnInfo;
1942 i::HandleScope scope(isolate);
1943 i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this);
1944 EXCEPTION_PREAMBLE(isolate);
1945 i::Handle<i::Object> start_col_obj = CallV8HeapFunction(
1946 "GetPositionInLine",
1948 &has_pending_exception);
1949 EXCEPTION_BAILOUT_CHECK(isolate, 0);
1950 i::Handle<i::JSMessageObject> message =
1951 i::Handle<i::JSMessageObject>::cast(data_obj);
1952 int start = message->start_position();
1953 int end = message->end_position();
1954 return static_cast<int>(start_col_obj->Number()) + (end - start);
1958 Local<String> Message::GetSourceLine() const {
1959 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1960 ON_BAILOUT(isolate, "v8::Message::GetSourceLine()", return Local<String>());
1963 EXCEPTION_PREAMBLE(isolate);
1964 i::Handle<i::Object> result = CallV8HeapFunction("GetSourceLine",
1965 Utils::OpenHandle(this),
1966 &has_pending_exception);
1967 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::String>());
1968 if (result->IsString()) {
1969 return scope.Close(Utils::ToLocal(i::Handle<i::String>::cast(result)));
1971 return Local<String>();
1976 void Message::PrintCurrentStackTrace(FILE* out) {
1977 i::Isolate* isolate = i::Isolate::Current();
1978 if (IsDeadCheck(isolate, "v8::Message::PrintCurrentStackTrace()")) return;
1980 isolate->PrintCurrentStackTrace(out);
1984 // --- S t a c k T r a c e ---
1986 Local<StackFrame> StackTrace::GetFrame(uint32_t index) const {
1987 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1988 if (IsDeadCheck(isolate, "v8::StackTrace::GetFrame()")) {
1989 return Local<StackFrame>();
1993 i::Handle<i::JSArray> self = Utils::OpenHandle(this);
1994 i::Object* raw_object = self->GetElementNoExceptionThrown(index);
1995 i::Handle<i::JSObject> obj(i::JSObject::cast(raw_object));
1996 return scope.Close(Utils::StackFrameToLocal(obj));
2000 int StackTrace::GetFrameCount() const {
2001 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2002 if (IsDeadCheck(isolate, "v8::StackTrace::GetFrameCount()")) return -1;
2004 return i::Smi::cast(Utils::OpenHandle(this)->length())->value();
2008 Local<Array> StackTrace::AsArray() {
2009 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2010 if (IsDeadCheck(isolate, "v8::StackTrace::AsArray()")) Local<Array>();
2012 return Utils::ToLocal(Utils::OpenHandle(this));
2016 Local<StackTrace> StackTrace::CurrentStackTrace(int frame_limit,
2017 StackTraceOptions options) {
2018 i::Isolate* isolate = i::Isolate::Current();
2019 if (IsDeadCheck(isolate, "v8::StackTrace::CurrentStackTrace()")) {
2020 Local<StackTrace>();
2023 i::Handle<i::JSArray> stackTrace =
2024 isolate->CaptureCurrentStackTrace(frame_limit, options);
2025 return Utils::StackTraceToLocal(stackTrace);
2029 // --- S t a c k F r a m e ---
2031 int StackFrame::GetLineNumber() const {
2032 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2033 if (IsDeadCheck(isolate, "v8::StackFrame::GetLineNumber()")) {
2034 return Message::kNoLineNumberInfo;
2037 i::HandleScope scope(isolate);
2038 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2039 i::Handle<i::Object> line = GetProperty(self, "lineNumber");
2040 if (!line->IsSmi()) {
2041 return Message::kNoLineNumberInfo;
2043 return i::Smi::cast(*line)->value();
2047 int StackFrame::GetColumn() const {
2048 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2049 if (IsDeadCheck(isolate, "v8::StackFrame::GetColumn()")) {
2050 return Message::kNoColumnInfo;
2053 i::HandleScope scope(isolate);
2054 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2055 i::Handle<i::Object> column = GetProperty(self, "column");
2056 if (!column->IsSmi()) {
2057 return Message::kNoColumnInfo;
2059 return i::Smi::cast(*column)->value();
2063 Local<String> StackFrame::GetScriptName() const {
2064 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2065 if (IsDeadCheck(isolate, "v8::StackFrame::GetScriptName()")) {
2066 return Local<String>();
2070 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2071 i::Handle<i::Object> name = GetProperty(self, "scriptName");
2072 if (!name->IsString()) {
2073 return Local<String>();
2075 return scope.Close(Local<String>::Cast(Utils::ToLocal(name)));
2079 Local<String> StackFrame::GetScriptNameOrSourceURL() const {
2080 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2081 if (IsDeadCheck(isolate, "v8::StackFrame::GetScriptNameOrSourceURL()")) {
2082 return Local<String>();
2086 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2087 i::Handle<i::Object> name = GetProperty(self, "scriptNameOrSourceURL");
2088 if (!name->IsString()) {
2089 return Local<String>();
2091 return scope.Close(Local<String>::Cast(Utils::ToLocal(name)));
2095 Local<String> StackFrame::GetFunctionName() const {
2096 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2097 if (IsDeadCheck(isolate, "v8::StackFrame::GetFunctionName()")) {
2098 return Local<String>();
2102 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2103 i::Handle<i::Object> name = GetProperty(self, "functionName");
2104 if (!name->IsString()) {
2105 return Local<String>();
2107 return scope.Close(Local<String>::Cast(Utils::ToLocal(name)));
2111 bool StackFrame::IsEval() const {
2112 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2113 if (IsDeadCheck(isolate, "v8::StackFrame::IsEval()")) return false;
2115 i::HandleScope scope(isolate);
2116 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2117 i::Handle<i::Object> is_eval = GetProperty(self, "isEval");
2118 return is_eval->IsTrue();
2122 bool StackFrame::IsConstructor() const {
2123 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2124 if (IsDeadCheck(isolate, "v8::StackFrame::IsConstructor()")) return false;
2126 i::HandleScope scope(isolate);
2127 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2128 i::Handle<i::Object> is_constructor = GetProperty(self, "isConstructor");
2129 return is_constructor->IsTrue();
2135 bool Value::IsUndefined() const {
2136 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsUndefined()")) {
2139 return Utils::OpenHandle(this)->IsUndefined();
2143 bool Value::IsNull() const {
2144 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsNull()")) return false;
2145 return Utils::OpenHandle(this)->IsNull();
2149 bool Value::IsTrue() const {
2150 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsTrue()")) return false;
2151 return Utils::OpenHandle(this)->IsTrue();
2155 bool Value::IsFalse() const {
2156 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsFalse()")) return false;
2157 return Utils::OpenHandle(this)->IsFalse();
2161 bool Value::IsFunction() const {
2162 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsFunction()")) {
2165 return Utils::OpenHandle(this)->IsJSFunction();
2169 bool Value::FullIsString() const {
2170 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsString()")) return false;
2171 bool result = Utils::OpenHandle(this)->IsString();
2172 ASSERT_EQ(result, QuickIsString());
2177 bool Value::IsArray() const {
2178 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsArray()")) return false;
2179 return Utils::OpenHandle(this)->IsJSArray();
2183 bool Value::IsObject() const {
2184 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsObject()")) return false;
2185 return Utils::OpenHandle(this)->IsJSObject();
2189 bool Value::IsNumber() const {
2190 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsNumber()")) return false;
2191 return Utils::OpenHandle(this)->IsNumber();
2195 bool Value::IsBoolean() const {
2196 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsBoolean()")) {
2199 return Utils::OpenHandle(this)->IsBoolean();
2203 bool Value::IsExternal() const {
2204 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsExternal()")) {
2207 return Utils::OpenHandle(this)->IsForeign();
2211 bool Value::IsInt32() const {
2212 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsInt32()")) return false;
2213 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2214 if (obj->IsSmi()) return true;
2215 if (obj->IsNumber()) {
2216 double value = obj->Number();
2217 return i::FastI2D(i::FastD2I(value)) == value;
2223 bool Value::IsUint32() const {
2224 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsUint32()")) return false;
2225 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2226 if (obj->IsSmi()) return i::Smi::cast(*obj)->value() >= 0;
2227 if (obj->IsNumber()) {
2228 double value = obj->Number();
2229 return i::FastUI2D(i::FastD2UI(value)) == value;
2235 bool Value::IsDate() const {
2236 i::Isolate* isolate = i::Isolate::Current();
2237 if (IsDeadCheck(isolate, "v8::Value::IsDate()")) return false;
2238 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2239 return obj->HasSpecificClassOf(isolate->heap()->Date_symbol());
2243 bool Value::IsStringObject() const {
2244 i::Isolate* isolate = i::Isolate::Current();
2245 if (IsDeadCheck(isolate, "v8::Value::IsStringObject()")) return false;
2246 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2247 return obj->HasSpecificClassOf(isolate->heap()->String_symbol());
2251 bool Value::IsNumberObject() const {
2252 i::Isolate* isolate = i::Isolate::Current();
2253 if (IsDeadCheck(isolate, "v8::Value::IsNumberObject()")) return false;
2254 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2255 return obj->HasSpecificClassOf(isolate->heap()->Number_symbol());
2259 static i::Object* LookupBuiltin(i::Isolate* isolate,
2260 const char* builtin_name) {
2261 i::Handle<i::String> symbol =
2262 isolate->factory()->LookupAsciiSymbol(builtin_name);
2263 i::Handle<i::JSBuiltinsObject> builtins = isolate->js_builtins_object();
2264 return builtins->GetPropertyNoExceptionThrown(*symbol);
2268 static bool CheckConstructor(i::Isolate* isolate,
2269 i::Handle<i::JSObject> obj,
2270 const char* class_name) {
2271 return obj->map()->constructor() == LookupBuiltin(isolate, class_name);
2275 bool Value::IsNativeError() const {
2276 i::Isolate* isolate = i::Isolate::Current();
2277 if (IsDeadCheck(isolate, "v8::Value::IsNativeError()")) return false;
2278 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2279 if (obj->IsJSObject()) {
2280 i::Handle<i::JSObject> js_obj(i::JSObject::cast(*obj));
2281 return CheckConstructor(isolate, js_obj, "$Error") ||
2282 CheckConstructor(isolate, js_obj, "$EvalError") ||
2283 CheckConstructor(isolate, js_obj, "$RangeError") ||
2284 CheckConstructor(isolate, js_obj, "$ReferenceError") ||
2285 CheckConstructor(isolate, js_obj, "$SyntaxError") ||
2286 CheckConstructor(isolate, js_obj, "$TypeError") ||
2287 CheckConstructor(isolate, js_obj, "$URIError");
2294 bool Value::IsBooleanObject() const {
2295 i::Isolate* isolate = i::Isolate::Current();
2296 if (IsDeadCheck(isolate, "v8::Value::IsBooleanObject()")) return false;
2297 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2298 return obj->HasSpecificClassOf(isolate->heap()->Boolean_symbol());
2302 bool Value::IsRegExp() const {
2303 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsRegExp()")) return false;
2304 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2305 return obj->IsJSRegExp();
2309 Local<String> Value::ToString() const {
2310 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2311 i::Handle<i::Object> str;
2312 if (obj->IsString()) {
2315 i::Isolate* isolate = i::Isolate::Current();
2316 if (IsDeadCheck(isolate, "v8::Value::ToString()")) {
2317 return Local<String>();
2319 LOG_API(isolate, "ToString");
2321 EXCEPTION_PREAMBLE(isolate);
2322 str = i::Execution::ToString(obj, &has_pending_exception);
2323 EXCEPTION_BAILOUT_CHECK(isolate, Local<String>());
2325 return Local<String>(ToApi<String>(str));
2329 Local<String> Value::ToDetailString() const {
2330 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2331 i::Handle<i::Object> str;
2332 if (obj->IsString()) {
2335 i::Isolate* isolate = i::Isolate::Current();
2336 if (IsDeadCheck(isolate, "v8::Value::ToDetailString()")) {
2337 return Local<String>();
2339 LOG_API(isolate, "ToDetailString");
2341 EXCEPTION_PREAMBLE(isolate);
2342 str = i::Execution::ToDetailString(obj, &has_pending_exception);
2343 EXCEPTION_BAILOUT_CHECK(isolate, Local<String>());
2345 return Local<String>(ToApi<String>(str));
2349 Local<v8::Object> Value::ToObject() const {
2350 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2351 i::Handle<i::Object> val;
2352 if (obj->IsJSObject()) {
2355 i::Isolate* isolate = i::Isolate::Current();
2356 if (IsDeadCheck(isolate, "v8::Value::ToObject()")) {
2357 return Local<v8::Object>();
2359 LOG_API(isolate, "ToObject");
2361 EXCEPTION_PREAMBLE(isolate);
2362 val = i::Execution::ToObject(obj, &has_pending_exception);
2363 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>());
2365 return Local<v8::Object>(ToApi<Object>(val));
2369 Local<Boolean> Value::ToBoolean() const {
2370 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2371 if (obj->IsBoolean()) {
2372 return Local<Boolean>(ToApi<Boolean>(obj));
2374 i::Isolate* isolate = i::Isolate::Current();
2375 if (IsDeadCheck(isolate, "v8::Value::ToBoolean()")) {
2376 return Local<Boolean>();
2378 LOG_API(isolate, "ToBoolean");
2380 i::Handle<i::Object> val = i::Execution::ToBoolean(obj);
2381 return Local<Boolean>(ToApi<Boolean>(val));
2386 Local<Number> Value::ToNumber() const {
2387 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2388 i::Handle<i::Object> num;
2389 if (obj->IsNumber()) {
2392 i::Isolate* isolate = i::Isolate::Current();
2393 if (IsDeadCheck(isolate, "v8::Value::ToNumber()")) {
2394 return Local<Number>();
2396 LOG_API(isolate, "ToNumber");
2398 EXCEPTION_PREAMBLE(isolate);
2399 num = i::Execution::ToNumber(obj, &has_pending_exception);
2400 EXCEPTION_BAILOUT_CHECK(isolate, Local<Number>());
2402 return Local<Number>(ToApi<Number>(num));
2406 Local<Integer> Value::ToInteger() const {
2407 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2408 i::Handle<i::Object> num;
2412 i::Isolate* isolate = i::Isolate::Current();
2413 if (IsDeadCheck(isolate, "v8::Value::ToInteger()")) return Local<Integer>();
2414 LOG_API(isolate, "ToInteger");
2416 EXCEPTION_PREAMBLE(isolate);
2417 num = i::Execution::ToInteger(obj, &has_pending_exception);
2418 EXCEPTION_BAILOUT_CHECK(isolate, Local<Integer>());
2420 return Local<Integer>(ToApi<Integer>(num));
2424 void External::CheckCast(v8::Value* that) {
2425 if (IsDeadCheck(i::Isolate::Current(), "v8::External::Cast()")) return;
2426 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2427 ApiCheck(obj->IsForeign(),
2428 "v8::External::Cast()",
2429 "Could not convert to external");
2433 void v8::Object::CheckCast(Value* that) {
2434 if (IsDeadCheck(i::Isolate::Current(), "v8::Object::Cast()")) return;
2435 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2436 ApiCheck(obj->IsJSObject(),
2437 "v8::Object::Cast()",
2438 "Could not convert to object");
2442 void v8::Function::CheckCast(Value* that) {
2443 if (IsDeadCheck(i::Isolate::Current(), "v8::Function::Cast()")) return;
2444 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2445 ApiCheck(obj->IsJSFunction(),
2446 "v8::Function::Cast()",
2447 "Could not convert to function");
2451 void v8::String::CheckCast(v8::Value* that) {
2452 if (IsDeadCheck(i::Isolate::Current(), "v8::String::Cast()")) return;
2453 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2454 ApiCheck(obj->IsString(),
2455 "v8::String::Cast()",
2456 "Could not convert to string");
2460 void v8::Number::CheckCast(v8::Value* that) {
2461 if (IsDeadCheck(i::Isolate::Current(), "v8::Number::Cast()")) return;
2462 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2463 ApiCheck(obj->IsNumber(),
2464 "v8::Number::Cast()",
2465 "Could not convert to number");
2469 void v8::Integer::CheckCast(v8::Value* that) {
2470 if (IsDeadCheck(i::Isolate::Current(), "v8::Integer::Cast()")) return;
2471 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2472 ApiCheck(obj->IsNumber(),
2473 "v8::Integer::Cast()",
2474 "Could not convert to number");
2478 void v8::Array::CheckCast(Value* that) {
2479 if (IsDeadCheck(i::Isolate::Current(), "v8::Array::Cast()")) return;
2480 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2481 ApiCheck(obj->IsJSArray(),
2482 "v8::Array::Cast()",
2483 "Could not convert to array");
2487 void v8::Date::CheckCast(v8::Value* that) {
2488 i::Isolate* isolate = i::Isolate::Current();
2489 if (IsDeadCheck(isolate, "v8::Date::Cast()")) return;
2490 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2491 ApiCheck(obj->HasSpecificClassOf(isolate->heap()->Date_symbol()),
2493 "Could not convert to date");
2497 void v8::StringObject::CheckCast(v8::Value* that) {
2498 i::Isolate* isolate = i::Isolate::Current();
2499 if (IsDeadCheck(isolate, "v8::StringObject::Cast()")) return;
2500 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2501 ApiCheck(obj->HasSpecificClassOf(isolate->heap()->String_symbol()),
2502 "v8::StringObject::Cast()",
2503 "Could not convert to StringObject");
2507 void v8::NumberObject::CheckCast(v8::Value* that) {
2508 i::Isolate* isolate = i::Isolate::Current();
2509 if (IsDeadCheck(isolate, "v8::NumberObject::Cast()")) return;
2510 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2511 ApiCheck(obj->HasSpecificClassOf(isolate->heap()->Number_symbol()),
2512 "v8::NumberObject::Cast()",
2513 "Could not convert to NumberObject");
2517 void v8::BooleanObject::CheckCast(v8::Value* that) {
2518 i::Isolate* isolate = i::Isolate::Current();
2519 if (IsDeadCheck(isolate, "v8::BooleanObject::Cast()")) return;
2520 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2521 ApiCheck(obj->HasSpecificClassOf(isolate->heap()->Boolean_symbol()),
2522 "v8::BooleanObject::Cast()",
2523 "Could not convert to BooleanObject");
2527 void v8::RegExp::CheckCast(v8::Value* that) {
2528 if (IsDeadCheck(i::Isolate::Current(), "v8::RegExp::Cast()")) return;
2529 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2530 ApiCheck(obj->IsJSRegExp(),
2531 "v8::RegExp::Cast()",
2532 "Could not convert to regular expression");
2536 bool Value::BooleanValue() const {
2537 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2538 if (obj->IsBoolean()) {
2539 return obj->IsTrue();
2541 i::Isolate* isolate = i::Isolate::Current();
2542 if (IsDeadCheck(isolate, "v8::Value::BooleanValue()")) return false;
2543 LOG_API(isolate, "BooleanValue");
2545 i::Handle<i::Object> value = i::Execution::ToBoolean(obj);
2546 return value->IsTrue();
2551 double Value::NumberValue() const {
2552 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2553 i::Handle<i::Object> num;
2554 if (obj->IsNumber()) {
2557 i::Isolate* isolate = i::Isolate::Current();
2558 if (IsDeadCheck(isolate, "v8::Value::NumberValue()")) {
2559 return i::OS::nan_value();
2561 LOG_API(isolate, "NumberValue");
2563 EXCEPTION_PREAMBLE(isolate);
2564 num = i::Execution::ToNumber(obj, &has_pending_exception);
2565 EXCEPTION_BAILOUT_CHECK(isolate, i::OS::nan_value());
2567 return num->Number();
2571 int64_t Value::IntegerValue() const {
2572 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2573 i::Handle<i::Object> num;
2574 if (obj->IsNumber()) {
2577 i::Isolate* isolate = i::Isolate::Current();
2578 if (IsDeadCheck(isolate, "v8::Value::IntegerValue()")) return 0;
2579 LOG_API(isolate, "IntegerValue");
2581 EXCEPTION_PREAMBLE(isolate);
2582 num = i::Execution::ToInteger(obj, &has_pending_exception);
2583 EXCEPTION_BAILOUT_CHECK(isolate, 0);
2586 return i::Smi::cast(*num)->value();
2588 return static_cast<int64_t>(num->Number());
2593 Local<Int32> Value::ToInt32() const {
2594 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2595 i::Handle<i::Object> num;
2599 i::Isolate* isolate = i::Isolate::Current();
2600 if (IsDeadCheck(isolate, "v8::Value::ToInt32()")) return Local<Int32>();
2601 LOG_API(isolate, "ToInt32");
2603 EXCEPTION_PREAMBLE(isolate);
2604 num = i::Execution::ToInt32(obj, &has_pending_exception);
2605 EXCEPTION_BAILOUT_CHECK(isolate, Local<Int32>());
2607 return Local<Int32>(ToApi<Int32>(num));
2611 Local<Uint32> Value::ToUint32() const {
2612 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2613 i::Handle<i::Object> num;
2617 i::Isolate* isolate = i::Isolate::Current();
2618 if (IsDeadCheck(isolate, "v8::Value::ToUint32()")) return Local<Uint32>();
2619 LOG_API(isolate, "ToUInt32");
2621 EXCEPTION_PREAMBLE(isolate);
2622 num = i::Execution::ToUint32(obj, &has_pending_exception);
2623 EXCEPTION_BAILOUT_CHECK(isolate, Local<Uint32>());
2625 return Local<Uint32>(ToApi<Uint32>(num));
2629 Local<Uint32> Value::ToArrayIndex() const {
2630 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2632 if (i::Smi::cast(*obj)->value() >= 0) return Utils::Uint32ToLocal(obj);
2633 return Local<Uint32>();
2635 i::Isolate* isolate = i::Isolate::Current();
2636 if (IsDeadCheck(isolate, "v8::Value::ToArrayIndex()")) return Local<Uint32>();
2637 LOG_API(isolate, "ToArrayIndex");
2639 EXCEPTION_PREAMBLE(isolate);
2640 i::Handle<i::Object> string_obj =
2641 i::Execution::ToString(obj, &has_pending_exception);
2642 EXCEPTION_BAILOUT_CHECK(isolate, Local<Uint32>());
2643 i::Handle<i::String> str = i::Handle<i::String>::cast(string_obj);
2645 if (str->AsArrayIndex(&index)) {
2646 i::Handle<i::Object> value;
2647 if (index <= static_cast<uint32_t>(i::Smi::kMaxValue)) {
2648 value = i::Handle<i::Object>(i::Smi::FromInt(index));
2650 value = isolate->factory()->NewNumber(index);
2652 return Utils::Uint32ToLocal(value);
2654 return Local<Uint32>();
2658 int32_t Value::Int32Value() const {
2659 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2661 return i::Smi::cast(*obj)->value();
2663 i::Isolate* isolate = i::Isolate::Current();
2664 if (IsDeadCheck(isolate, "v8::Value::Int32Value()")) return 0;
2665 LOG_API(isolate, "Int32Value (slow)");
2667 EXCEPTION_PREAMBLE(isolate);
2668 i::Handle<i::Object> num =
2669 i::Execution::ToInt32(obj, &has_pending_exception);
2670 EXCEPTION_BAILOUT_CHECK(isolate, 0);
2672 return i::Smi::cast(*num)->value();
2674 return static_cast<int32_t>(num->Number());
2680 bool Value::Equals(Handle<Value> that) const {
2681 i::Isolate* isolate = i::Isolate::Current();
2682 if (IsDeadCheck(isolate, "v8::Value::Equals()")
2683 || EmptyCheck("v8::Value::Equals()", this)
2684 || EmptyCheck("v8::Value::Equals()", that)) {
2687 LOG_API(isolate, "Equals");
2689 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2690 i::Handle<i::Object> other = Utils::OpenHandle(*that);
2691 // If both obj and other are JSObjects, we'd better compare by identity
2692 // immediately when going into JS builtin. The reason is Invoke
2693 // would overwrite global object receiver with global proxy.
2694 if (obj->IsJSObject() && other->IsJSObject()) {
2695 return *obj == *other;
2697 i::Handle<i::Object> args[] = { other };
2698 EXCEPTION_PREAMBLE(isolate);
2699 i::Handle<i::Object> result =
2700 CallV8HeapFunction("EQUALS", obj, ARRAY_SIZE(args), args,
2701 &has_pending_exception);
2702 EXCEPTION_BAILOUT_CHECK(isolate, false);
2703 return *result == i::Smi::FromInt(i::EQUAL);
2707 bool Value::StrictEquals(Handle<Value> that) const {
2708 i::Isolate* isolate = i::Isolate::Current();
2709 if (IsDeadCheck(isolate, "v8::Value::StrictEquals()")
2710 || EmptyCheck("v8::Value::StrictEquals()", this)
2711 || EmptyCheck("v8::Value::StrictEquals()", that)) {
2714 LOG_API(isolate, "StrictEquals");
2715 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2716 i::Handle<i::Object> other = Utils::OpenHandle(*that);
2717 // Must check HeapNumber first, since NaN !== NaN.
2718 if (obj->IsHeapNumber()) {
2719 if (!other->IsNumber()) return false;
2720 double x = obj->Number();
2721 double y = other->Number();
2722 // Must check explicitly for NaN:s on Windows, but -0 works fine.
2723 return x == y && !isnan(x) && !isnan(y);
2724 } else if (*obj == *other) { // Also covers Booleans.
2726 } else if (obj->IsSmi()) {
2727 return other->IsNumber() && obj->Number() == other->Number();
2728 } else if (obj->IsString()) {
2729 return other->IsString() &&
2730 i::String::cast(*obj)->Equals(i::String::cast(*other));
2731 } else if (obj->IsUndefined() || obj->IsUndetectableObject()) {
2732 return other->IsUndefined() || other->IsUndetectableObject();
2739 uint32_t Value::Uint32Value() const {
2740 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2742 return i::Smi::cast(*obj)->value();
2744 i::Isolate* isolate = i::Isolate::Current();
2745 if (IsDeadCheck(isolate, "v8::Value::Uint32Value()")) return 0;
2746 LOG_API(isolate, "Uint32Value");
2748 EXCEPTION_PREAMBLE(isolate);
2749 i::Handle<i::Object> num =
2750 i::Execution::ToUint32(obj, &has_pending_exception);
2751 EXCEPTION_BAILOUT_CHECK(isolate, 0);
2753 return i::Smi::cast(*num)->value();
2755 return static_cast<uint32_t>(num->Number());
2761 bool v8::Object::Set(v8::Handle<Value> key, v8::Handle<Value> value,
2762 v8::PropertyAttribute attribs) {
2763 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2764 ON_BAILOUT(isolate, "v8::Object::Set()", return false);
2766 i::HandleScope scope(isolate);
2767 i::Handle<i::Object> self = Utils::OpenHandle(this);
2768 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2769 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
2770 EXCEPTION_PREAMBLE(isolate);
2771 i::Handle<i::Object> obj = i::SetProperty(
2775 static_cast<PropertyAttributes>(attribs),
2777 has_pending_exception = obj.is_null();
2778 EXCEPTION_BAILOUT_CHECK(isolate, false);
2783 bool v8::Object::Set(uint32_t index, v8::Handle<Value> value) {
2784 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2785 ON_BAILOUT(isolate, "v8::Object::Set()", return false);
2787 i::HandleScope scope(isolate);
2788 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2789 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
2790 EXCEPTION_PREAMBLE(isolate);
2791 i::Handle<i::Object> obj = i::SetElement(
2796 has_pending_exception = obj.is_null();
2797 EXCEPTION_BAILOUT_CHECK(isolate, false);
2802 bool v8::Object::ForceSet(v8::Handle<Value> key,
2803 v8::Handle<Value> value,
2804 v8::PropertyAttribute attribs) {
2805 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2806 ON_BAILOUT(isolate, "v8::Object::ForceSet()", return false);
2808 i::HandleScope scope(isolate);
2809 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2810 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2811 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
2812 EXCEPTION_PREAMBLE(isolate);
2813 i::Handle<i::Object> obj = i::ForceSetProperty(
2817 static_cast<PropertyAttributes>(attribs));
2818 has_pending_exception = obj.is_null();
2819 EXCEPTION_BAILOUT_CHECK(isolate, false);
2824 bool v8::Object::ForceDelete(v8::Handle<Value> key) {
2825 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2826 ON_BAILOUT(isolate, "v8::Object::ForceDelete()", return false);
2828 i::HandleScope scope(isolate);
2829 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2830 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2832 // When turning on access checks for a global object deoptimize all functions
2833 // as optimized code does not always handle access checks.
2834 i::Deoptimizer::DeoptimizeGlobalObject(*self);
2836 EXCEPTION_PREAMBLE(isolate);
2837 i::Handle<i::Object> obj = i::ForceDeleteProperty(self, key_obj);
2838 has_pending_exception = obj.is_null();
2839 EXCEPTION_BAILOUT_CHECK(isolate, false);
2840 return obj->IsTrue();
2844 Local<Value> v8::Object::Get(v8::Handle<Value> key) {
2845 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2846 ON_BAILOUT(isolate, "v8::Object::Get()", return Local<v8::Value>());
2848 i::Handle<i::Object> self = Utils::OpenHandle(this);
2849 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2850 EXCEPTION_PREAMBLE(isolate);
2851 i::Handle<i::Object> result = i::GetProperty(self, key_obj);
2852 has_pending_exception = result.is_null();
2853 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
2854 return Utils::ToLocal(result);
2858 Local<Value> v8::Object::Get(uint32_t index) {
2859 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2860 ON_BAILOUT(isolate, "v8::Object::Get()", return Local<v8::Value>());
2862 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2863 EXCEPTION_PREAMBLE(isolate);
2864 i::Handle<i::Object> result = i::Object::GetElement(self, index);
2865 has_pending_exception = result.is_null();
2866 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
2867 return Utils::ToLocal(result);
2871 PropertyAttribute v8::Object::GetPropertyAttributes(v8::Handle<Value> key) {
2872 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2873 ON_BAILOUT(isolate, "v8::Object::GetPropertyAttribute()",
2874 return static_cast<PropertyAttribute>(NONE));
2876 i::HandleScope scope(isolate);
2877 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2878 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
2879 if (!key_obj->IsString()) {
2880 EXCEPTION_PREAMBLE(isolate);
2881 key_obj = i::Execution::ToString(key_obj, &has_pending_exception);
2882 EXCEPTION_BAILOUT_CHECK(isolate, static_cast<PropertyAttribute>(NONE));
2884 i::Handle<i::String> key_string = i::Handle<i::String>::cast(key_obj);
2885 PropertyAttributes result = self->GetPropertyAttribute(*key_string);
2886 if (result == ABSENT) return static_cast<PropertyAttribute>(NONE);
2887 return static_cast<PropertyAttribute>(result);
2891 Local<Value> v8::Object::GetPrototype() {
2892 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2893 ON_BAILOUT(isolate, "v8::Object::GetPrototype()",
2894 return Local<v8::Value>());
2896 i::Handle<i::Object> self = Utils::OpenHandle(this);
2897 i::Handle<i::Object> result = i::GetPrototype(self);
2898 return Utils::ToLocal(result);
2902 bool v8::Object::SetPrototype(Handle<Value> value) {
2903 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2904 ON_BAILOUT(isolate, "v8::Object::SetPrototype()", return false);
2906 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2907 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
2908 // We do not allow exceptions thrown while setting the prototype
2909 // to propagate outside.
2911 EXCEPTION_PREAMBLE(isolate);
2912 i::Handle<i::Object> result = i::SetPrototype(self, value_obj);
2913 has_pending_exception = result.is_null();
2914 EXCEPTION_BAILOUT_CHECK(isolate, false);
2919 Local<Object> v8::Object::FindInstanceInPrototypeChain(
2920 v8::Handle<FunctionTemplate> tmpl) {
2921 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2923 "v8::Object::FindInstanceInPrototypeChain()",
2924 return Local<v8::Object>());
2926 i::JSObject* object = *Utils::OpenHandle(this);
2927 i::FunctionTemplateInfo* tmpl_info = *Utils::OpenHandle(*tmpl);
2928 while (!object->IsInstanceOf(tmpl_info)) {
2929 i::Object* prototype = object->GetPrototype();
2930 if (!prototype->IsJSObject()) return Local<Object>();
2931 object = i::JSObject::cast(prototype);
2933 return Utils::ToLocal(i::Handle<i::JSObject>(object));
2937 Local<Array> v8::Object::GetPropertyNames() {
2938 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2939 ON_BAILOUT(isolate, "v8::Object::GetPropertyNames()",
2940 return Local<v8::Array>());
2942 i::HandleScope scope(isolate);
2943 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2945 i::Handle<i::FixedArray> value =
2946 i::GetKeysInFixedArrayFor(self, i::INCLUDE_PROTOS, &threw);
2947 if (threw) return Local<v8::Array>();
2948 // Because we use caching to speed up enumeration it is important
2949 // to never change the result of the basic enumeration function so
2950 // we clone the result.
2951 i::Handle<i::FixedArray> elms = isolate->factory()->CopyFixedArray(value);
2952 i::Handle<i::JSArray> result =
2953 isolate->factory()->NewJSArrayWithElements(elms);
2954 return Utils::ToLocal(scope.CloseAndEscape(result));
2958 Local<Array> v8::Object::GetOwnPropertyNames() {
2959 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2960 ON_BAILOUT(isolate, "v8::Object::GetOwnPropertyNames()",
2961 return Local<v8::Array>());
2963 i::HandleScope scope(isolate);
2964 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2966 i::Handle<i::FixedArray> value =
2967 i::GetKeysInFixedArrayFor(self, i::LOCAL_ONLY, &threw);
2968 if (threw) return Local<v8::Array>();
2969 // Because we use caching to speed up enumeration it is important
2970 // to never change the result of the basic enumeration function so
2971 // we clone the result.
2972 i::Handle<i::FixedArray> elms = isolate->factory()->CopyFixedArray(value);
2973 i::Handle<i::JSArray> result =
2974 isolate->factory()->NewJSArrayWithElements(elms);
2975 return Utils::ToLocal(scope.CloseAndEscape(result));
2979 Local<String> v8::Object::ObjectProtoToString() {
2980 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2981 ON_BAILOUT(isolate, "v8::Object::ObjectProtoToString()",
2982 return Local<v8::String>());
2984 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
2986 i::Handle<i::Object> name(self->class_name());
2988 // Native implementation of Object.prototype.toString (v8natives.js):
2989 // var c = %ClassOf(this);
2990 // if (c === 'Arguments') c = 'Object';
2991 // return "[object " + c + "]";
2993 if (!name->IsString()) {
2994 return v8::String::New("[object ]");
2997 i::Handle<i::String> class_name = i::Handle<i::String>::cast(name);
2998 if (class_name->IsEqualTo(i::CStrVector("Arguments"))) {
2999 return v8::String::New("[object Object]");
3002 const char* prefix = "[object ";
3003 Local<String> str = Utils::ToLocal(class_name);
3004 const char* postfix = "]";
3006 int prefix_len = i::StrLength(prefix);
3007 int str_len = str->Length();
3008 int postfix_len = i::StrLength(postfix);
3010 int buf_len = prefix_len + str_len + postfix_len;
3011 i::ScopedVector<char> buf(buf_len);
3014 char* ptr = buf.start();
3015 memcpy(ptr, prefix, prefix_len * v8::internal::kCharSize);
3018 // Write real content.
3019 str->WriteAscii(ptr, 0, str_len);
3023 memcpy(ptr, postfix, postfix_len * v8::internal::kCharSize);
3025 // Copy the buffer into a heap-allocated string and return it.
3026 Local<String> result = v8::String::New(buf.start(), buf_len);
3033 Local<String> v8::Object::GetConstructorName() {
3034 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3035 ON_BAILOUT(isolate, "v8::Object::GetConstructorName()",
3036 return Local<v8::String>());
3038 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3039 i::Handle<i::String> name(self->constructor_name());
3040 return Utils::ToLocal(name);
3044 bool v8::Object::Delete(v8::Handle<String> key) {
3045 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3046 ON_BAILOUT(isolate, "v8::Object::Delete()", return false);
3048 i::HandleScope scope(isolate);
3049 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3050 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
3051 return i::DeleteProperty(self, key_obj)->IsTrue();
3055 bool v8::Object::Has(v8::Handle<String> key) {
3056 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3057 ON_BAILOUT(isolate, "v8::Object::Has()", return false);
3059 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3060 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
3061 return self->HasProperty(*key_obj);
3065 bool v8::Object::Delete(uint32_t index) {
3066 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3067 ON_BAILOUT(isolate, "v8::Object::DeleteProperty()",
3071 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3072 return i::DeleteElement(self, index)->IsTrue();
3076 bool v8::Object::Has(uint32_t index) {
3077 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3078 ON_BAILOUT(isolate, "v8::Object::HasProperty()", return false);
3079 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3080 return self->HasElement(index);
3084 bool Object::SetAccessor(Handle<String> name,
3085 AccessorGetter getter,
3086 AccessorSetter setter,
3087 v8::Handle<Value> data,
3088 AccessControl settings,
3089 PropertyAttribute attributes) {
3090 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3091 ON_BAILOUT(isolate, "v8::Object::SetAccessor()", return false);
3093 i::HandleScope scope(isolate);
3094 i::Handle<i::AccessorInfo> info = MakeAccessorInfo(name,
3095 getter, setter, data,
3096 settings, attributes);
3097 i::Handle<i::Object> result = i::SetAccessor(Utils::OpenHandle(this), info);
3098 return !result.is_null() && !result->IsUndefined();
3102 bool v8::Object::HasOwnProperty(Handle<String> key) {
3103 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3104 ON_BAILOUT(isolate, "v8::Object::HasOwnProperty()",
3106 return Utils::OpenHandle(this)->HasLocalProperty(
3107 *Utils::OpenHandle(*key));
3111 bool v8::Object::HasRealNamedProperty(Handle<String> key) {
3112 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3113 ON_BAILOUT(isolate, "v8::Object::HasRealNamedProperty()",
3115 return Utils::OpenHandle(this)->HasRealNamedProperty(
3116 *Utils::OpenHandle(*key));
3120 bool v8::Object::HasRealIndexedProperty(uint32_t index) {
3121 ON_BAILOUT(Utils::OpenHandle(this)->GetIsolate(),
3122 "v8::Object::HasRealIndexedProperty()",
3124 return Utils::OpenHandle(this)->HasRealElementProperty(index);
3128 bool v8::Object::HasRealNamedCallbackProperty(Handle<String> key) {
3129 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3131 "v8::Object::HasRealNamedCallbackProperty()",
3134 return Utils::OpenHandle(this)->HasRealNamedCallbackProperty(
3135 *Utils::OpenHandle(*key));
3139 bool v8::Object::HasNamedLookupInterceptor() {
3140 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3141 ON_BAILOUT(isolate, "v8::Object::HasNamedLookupInterceptor()",
3143 return Utils::OpenHandle(this)->HasNamedInterceptor();
3147 bool v8::Object::HasIndexedLookupInterceptor() {
3148 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3149 ON_BAILOUT(isolate, "v8::Object::HasIndexedLookupInterceptor()",
3151 return Utils::OpenHandle(this)->HasIndexedInterceptor();
3155 static Local<Value> GetPropertyByLookup(i::Isolate* isolate,
3156 i::Handle<i::JSObject> receiver,
3157 i::Handle<i::String> name,
3158 i::LookupResult* lookup) {
3159 if (!lookup->IsProperty()) {
3160 // No real property was found.
3161 return Local<Value>();
3164 // If the property being looked up is a callback, it can throw
3166 EXCEPTION_PREAMBLE(isolate);
3167 PropertyAttributes ignored;
3168 i::Handle<i::Object> result =
3169 i::Object::GetProperty(receiver, receiver, lookup, name,
3171 has_pending_exception = result.is_null();
3172 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
3174 return Utils::ToLocal(result);
3178 Local<Value> v8::Object::GetRealNamedPropertyInPrototypeChain(
3179 Handle<String> key) {
3180 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3182 "v8::Object::GetRealNamedPropertyInPrototypeChain()",
3183 return Local<Value>());
3185 i::Handle<i::JSObject> self_obj = Utils::OpenHandle(this);
3186 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
3187 i::LookupResult lookup(isolate);
3188 self_obj->LookupRealNamedPropertyInPrototypes(*key_obj, &lookup);
3189 return GetPropertyByLookup(isolate, self_obj, key_obj, &lookup);
3193 Local<Value> v8::Object::GetRealNamedProperty(Handle<String> key) {
3194 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3195 ON_BAILOUT(isolate, "v8::Object::GetRealNamedProperty()",
3196 return Local<Value>());
3198 i::Handle<i::JSObject> self_obj = Utils::OpenHandle(this);
3199 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
3200 i::LookupResult lookup(isolate);
3201 self_obj->LookupRealNamedProperty(*key_obj, &lookup);
3202 return GetPropertyByLookup(isolate, self_obj, key_obj, &lookup);
3206 // Turns on access checks by copying the map and setting the check flag.
3207 // Because the object gets a new map, existing inline cache caching
3208 // the old map of this object will fail.
3209 void v8::Object::TurnOnAccessCheck() {
3210 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3211 ON_BAILOUT(isolate, "v8::Object::TurnOnAccessCheck()", return);
3213 i::HandleScope scope(isolate);
3214 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3216 // When turning on access checks for a global object deoptimize all functions
3217 // as optimized code does not always handle access checks.
3218 i::Deoptimizer::DeoptimizeGlobalObject(*obj);
3220 i::Handle<i::Map> new_map =
3221 isolate->factory()->CopyMapDropTransitions(i::Handle<i::Map>(obj->map()));
3222 new_map->set_is_access_check_needed(true);
3223 obj->set_map(*new_map);
3227 bool v8::Object::IsDirty() {
3228 return Utils::OpenHandle(this)->IsDirty();
3232 Local<v8::Object> v8::Object::Clone() {
3233 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3234 ON_BAILOUT(isolate, "v8::Object::Clone()", return Local<Object>());
3236 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3237 EXCEPTION_PREAMBLE(isolate);
3238 i::Handle<i::JSObject> result = i::Copy(self);
3239 has_pending_exception = result.is_null();
3240 EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>());
3241 return Utils::ToLocal(result);
3245 static i::Context* GetCreationContext(i::JSObject* object) {
3246 i::Object* constructor = object->map()->constructor();
3247 i::JSFunction* function;
3248 if (!constructor->IsJSFunction()) {
3249 // Functions have null as a constructor,
3250 // but any JSFunction knows its context immediately.
3251 ASSERT(object->IsJSFunction());
3252 function = i::JSFunction::cast(object);
3254 function = i::JSFunction::cast(constructor);
3256 return function->context()->global_context();
3260 Local<v8::Context> v8::Object::CreationContext() {
3261 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3263 "v8::Object::CreationContext()", return Local<v8::Context>());
3265 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3266 i::Context* context = GetCreationContext(*self);
3267 return Utils::ToLocal(i::Handle<i::Context>(context));
3271 int v8::Object::GetIdentityHash() {
3272 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3273 ON_BAILOUT(isolate, "v8::Object::GetIdentityHash()", return 0);
3275 i::HandleScope scope(isolate);
3276 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3277 return i::GetIdentityHash(self);
3281 bool v8::Object::SetHiddenValue(v8::Handle<v8::String> key,
3282 v8::Handle<v8::Value> value) {
3283 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3284 ON_BAILOUT(isolate, "v8::Object::SetHiddenValue()", return false);
3286 i::HandleScope scope(isolate);
3287 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3288 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
3289 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
3290 i::Handle<i::Object> result = i::SetHiddenProperty(self, key_obj, value_obj);
3291 return *result == *self;
3295 v8::Local<v8::Value> v8::Object::GetHiddenValue(v8::Handle<v8::String> key) {
3296 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3297 ON_BAILOUT(isolate, "v8::Object::GetHiddenValue()",
3298 return Local<v8::Value>());
3300 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3301 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
3302 i::Handle<i::Object> result(self->GetHiddenProperty(*key_obj));
3303 if (result->IsUndefined()) return v8::Local<v8::Value>();
3304 return Utils::ToLocal(result);
3308 bool v8::Object::DeleteHiddenValue(v8::Handle<v8::String> key) {
3309 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3310 ON_BAILOUT(isolate, "v8::DeleteHiddenValue()", return false);
3312 i::HandleScope scope(isolate);
3313 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3314 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
3315 self->DeleteHiddenProperty(*key_obj);
3322 static i::ElementsKind GetElementsKindFromExternalArrayType(
3323 ExternalArrayType array_type) {
3324 switch (array_type) {
3325 case kExternalByteArray:
3326 return i::EXTERNAL_BYTE_ELEMENTS;
3328 case kExternalUnsignedByteArray:
3329 return i::EXTERNAL_UNSIGNED_BYTE_ELEMENTS;
3331 case kExternalShortArray:
3332 return i::EXTERNAL_SHORT_ELEMENTS;
3334 case kExternalUnsignedShortArray:
3335 return i::EXTERNAL_UNSIGNED_SHORT_ELEMENTS;
3337 case kExternalIntArray:
3338 return i::EXTERNAL_INT_ELEMENTS;
3340 case kExternalUnsignedIntArray:
3341 return i::EXTERNAL_UNSIGNED_INT_ELEMENTS;
3343 case kExternalFloatArray:
3344 return i::EXTERNAL_FLOAT_ELEMENTS;
3346 case kExternalDoubleArray:
3347 return i::EXTERNAL_DOUBLE_ELEMENTS;
3349 case kExternalPixelArray:
3350 return i::EXTERNAL_PIXEL_ELEMENTS;
3354 return i::DICTIONARY_ELEMENTS;
3358 void PrepareExternalArrayElements(i::Handle<i::JSObject> object,
3360 ExternalArrayType array_type,
3362 i::Isolate* isolate = object->GetIsolate();
3363 i::Handle<i::ExternalArray> array =
3364 isolate->factory()->NewExternalArray(length, array_type, data);
3366 i::Handle<i::Map> external_array_map =
3367 isolate->factory()->GetElementsTransitionMap(
3369 GetElementsKindFromExternalArrayType(array_type));
3371 object->set_map(*external_array_map);
3372 object->set_elements(*array);
3378 void v8::Object::SetIndexedPropertiesToPixelData(uint8_t* data, int length) {
3379 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3380 ON_BAILOUT(isolate, "v8::SetElementsToPixelData()", return);
3382 i::HandleScope scope(isolate);
3383 if (!ApiCheck(length <= i::ExternalPixelArray::kMaxLength,
3384 "v8::Object::SetIndexedPropertiesToPixelData()",
3385 "length exceeds max acceptable value")) {
3388 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3389 if (!ApiCheck(!self->IsJSArray(),
3390 "v8::Object::SetIndexedPropertiesToPixelData()",
3391 "JSArray is not supported")) {
3394 PrepareExternalArrayElements(self, data, kExternalPixelArray, length);
3398 bool v8::Object::HasIndexedPropertiesInPixelData() {
3399 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3400 ON_BAILOUT(self->GetIsolate(), "v8::HasIndexedPropertiesInPixelData()",
3402 return self->HasExternalPixelElements();
3406 uint8_t* v8::Object::GetIndexedPropertiesPixelData() {
3407 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3408 ON_BAILOUT(self->GetIsolate(), "v8::GetIndexedPropertiesPixelData()",
3410 if (self->HasExternalPixelElements()) {
3411 return i::ExternalPixelArray::cast(self->elements())->
3412 external_pixel_pointer();
3419 int v8::Object::GetIndexedPropertiesPixelDataLength() {
3420 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3421 ON_BAILOUT(self->GetIsolate(), "v8::GetIndexedPropertiesPixelDataLength()",
3423 if (self->HasExternalPixelElements()) {
3424 return i::ExternalPixelArray::cast(self->elements())->length();
3431 void v8::Object::SetIndexedPropertiesToExternalArrayData(
3433 ExternalArrayType array_type,
3435 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3436 ON_BAILOUT(isolate, "v8::SetIndexedPropertiesToExternalArrayData()", return);
3438 i::HandleScope scope(isolate);
3439 if (!ApiCheck(length <= i::ExternalArray::kMaxLength,
3440 "v8::Object::SetIndexedPropertiesToExternalArrayData()",
3441 "length exceeds max acceptable value")) {
3444 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3445 if (!ApiCheck(!self->IsJSArray(),
3446 "v8::Object::SetIndexedPropertiesToExternalArrayData()",
3447 "JSArray is not supported")) {
3450 PrepareExternalArrayElements(self, data, array_type, length);
3454 bool v8::Object::HasIndexedPropertiesInExternalArrayData() {
3455 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3456 ON_BAILOUT(self->GetIsolate(),
3457 "v8::HasIndexedPropertiesInExternalArrayData()",
3459 return self->HasExternalArrayElements();
3463 void* v8::Object::GetIndexedPropertiesExternalArrayData() {
3464 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3465 ON_BAILOUT(self->GetIsolate(),
3466 "v8::GetIndexedPropertiesExternalArrayData()",
3468 if (self->HasExternalArrayElements()) {
3469 return i::ExternalArray::cast(self->elements())->external_pointer();
3476 ExternalArrayType v8::Object::GetIndexedPropertiesExternalArrayDataType() {
3477 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3478 ON_BAILOUT(self->GetIsolate(),
3479 "v8::GetIndexedPropertiesExternalArrayDataType()",
3480 return static_cast<ExternalArrayType>(-1));
3481 switch (self->elements()->map()->instance_type()) {
3482 case i::EXTERNAL_BYTE_ARRAY_TYPE:
3483 return kExternalByteArray;
3484 case i::EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE:
3485 return kExternalUnsignedByteArray;
3486 case i::EXTERNAL_SHORT_ARRAY_TYPE:
3487 return kExternalShortArray;
3488 case i::EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE:
3489 return kExternalUnsignedShortArray;
3490 case i::EXTERNAL_INT_ARRAY_TYPE:
3491 return kExternalIntArray;
3492 case i::EXTERNAL_UNSIGNED_INT_ARRAY_TYPE:
3493 return kExternalUnsignedIntArray;
3494 case i::EXTERNAL_FLOAT_ARRAY_TYPE:
3495 return kExternalFloatArray;
3496 case i::EXTERNAL_DOUBLE_ARRAY_TYPE:
3497 return kExternalDoubleArray;
3498 case i::EXTERNAL_PIXEL_ARRAY_TYPE:
3499 return kExternalPixelArray;
3501 return static_cast<ExternalArrayType>(-1);
3506 int v8::Object::GetIndexedPropertiesExternalArrayDataLength() {
3507 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3508 ON_BAILOUT(self->GetIsolate(),
3509 "v8::GetIndexedPropertiesExternalArrayDataLength()",
3511 if (self->HasExternalArrayElements()) {
3512 return i::ExternalArray::cast(self->elements())->length();
3519 bool v8::Object::IsCallable() {
3520 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3521 ON_BAILOUT(isolate, "v8::Object::IsCallable()", return false);
3523 i::HandleScope scope(isolate);
3524 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3525 if (obj->IsJSFunction()) return true;
3526 return i::Execution::GetFunctionDelegate(obj)->IsJSFunction();
3530 Local<v8::Value> Object::CallAsFunction(v8::Handle<v8::Object> recv,
3532 v8::Handle<v8::Value> argv[]) {
3533 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3534 ON_BAILOUT(isolate, "v8::Object::CallAsFunction()",
3535 return Local<v8::Value>());
3536 LOG_API(isolate, "Object::CallAsFunction");
3538 i::HandleScope scope(isolate);
3539 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3540 i::Handle<i::Object> recv_obj = Utils::OpenHandle(*recv);
3541 STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**));
3542 i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
3543 i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>();
3544 if (obj->IsJSFunction()) {
3545 fun = i::Handle<i::JSFunction>::cast(obj);
3547 EXCEPTION_PREAMBLE(isolate);
3548 i::Handle<i::Object> delegate =
3549 i::Execution::TryGetFunctionDelegate(obj, &has_pending_exception);
3550 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
3551 fun = i::Handle<i::JSFunction>::cast(delegate);
3554 EXCEPTION_PREAMBLE(isolate);
3555 i::Handle<i::Object> returned =
3556 i::Execution::Call(fun, recv_obj, argc, args, &has_pending_exception);
3557 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
3558 return Utils::ToLocal(scope.CloseAndEscape(returned));
3562 Local<v8::Value> Object::CallAsConstructor(int argc,
3563 v8::Handle<v8::Value> argv[]) {
3564 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3565 ON_BAILOUT(isolate, "v8::Object::CallAsConstructor()",
3566 return Local<v8::Object>());
3567 LOG_API(isolate, "Object::CallAsConstructor");
3569 i::HandleScope scope(isolate);
3570 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3571 STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**));
3572 i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
3573 if (obj->IsJSFunction()) {
3574 i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(obj);
3575 EXCEPTION_PREAMBLE(isolate);
3576 i::Handle<i::Object> returned =
3577 i::Execution::New(fun, argc, args, &has_pending_exception);
3578 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>());
3579 return Utils::ToLocal(scope.CloseAndEscape(
3580 i::Handle<i::JSObject>::cast(returned)));
3582 EXCEPTION_PREAMBLE(isolate);
3583 i::Handle<i::Object> delegate =
3584 i::Execution::TryGetConstructorDelegate(obj, &has_pending_exception);
3585 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>());
3586 if (!delegate->IsUndefined()) {
3587 i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(delegate);
3588 EXCEPTION_PREAMBLE(isolate);
3589 i::Handle<i::Object> returned =
3590 i::Execution::Call(fun, obj, argc, args, &has_pending_exception);
3591 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>());
3592 ASSERT(!delegate->IsUndefined());
3593 return Utils::ToLocal(scope.CloseAndEscape(returned));
3595 return Local<v8::Object>();
3599 Local<v8::Object> Function::NewInstance() const {
3600 return NewInstance(0, NULL);
3604 Local<v8::Object> Function::NewInstance(int argc,
3605 v8::Handle<v8::Value> argv[]) const {
3606 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3607 ON_BAILOUT(isolate, "v8::Function::NewInstance()",
3608 return Local<v8::Object>());
3609 LOG_API(isolate, "Function::NewInstance");
3612 i::Handle<i::JSFunction> function = Utils::OpenHandle(this);
3613 STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**));
3614 i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
3615 EXCEPTION_PREAMBLE(isolate);
3616 i::Handle<i::Object> returned =
3617 i::Execution::New(function, argc, args, &has_pending_exception);
3618 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>());
3619 return scope.Close(Utils::ToLocal(i::Handle<i::JSObject>::cast(returned)));
3623 Local<v8::Value> Function::Call(v8::Handle<v8::Object> recv, int argc,
3624 v8::Handle<v8::Value> argv[]) {
3625 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3626 ON_BAILOUT(isolate, "v8::Function::Call()", return Local<v8::Value>());
3627 LOG_API(isolate, "Function::Call");
3629 i::Object* raw_result = NULL;
3631 i::HandleScope scope(isolate);
3632 i::Handle<i::JSFunction> fun = Utils::OpenHandle(this);
3633 i::Handle<i::Object> recv_obj = Utils::OpenHandle(*recv);
3634 STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**));
3635 i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
3636 EXCEPTION_PREAMBLE(isolate);
3637 i::Handle<i::Object> returned =
3638 i::Execution::Call(fun, recv_obj, argc, args, &has_pending_exception);
3639 EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>());
3640 raw_result = *returned;
3642 i::Handle<i::Object> result(raw_result);
3643 return Utils::ToLocal(result);
3647 void Function::SetName(v8::Handle<v8::String> name) {
3648 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3651 i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
3652 func->shared()->set_name(*Utils::OpenHandle(*name));
3656 Handle<Value> Function::GetName() const {
3657 i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
3658 return Utils::ToLocal(i::Handle<i::Object>(func->shared()->name()));
3662 ScriptOrigin Function::GetScriptOrigin() const {
3663 i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
3664 if (func->shared()->script()->IsScript()) {
3665 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
3666 v8::ScriptOrigin origin(
3667 Utils::ToLocal(i::Handle<i::Object>(script->name())),
3668 v8::Integer::New(script->line_offset()->value()),
3669 v8::Integer::New(script->column_offset()->value()));
3672 return v8::ScriptOrigin(Handle<Value>());
3676 const int Function::kLineOffsetNotFound = -1;
3679 int Function::GetScriptLineNumber() const {
3680 i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
3681 if (func->shared()->script()->IsScript()) {
3682 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
3683 return i::GetScriptLineNumber(script, func->shared()->start_position());
3685 return kLineOffsetNotFound;
3689 int String::Length() const {
3690 i::Handle<i::String> str = Utils::OpenHandle(this);
3691 if (IsDeadCheck(str->GetIsolate(), "v8::String::Length()")) return 0;
3692 return str->length();
3696 int String::Utf8Length() const {
3697 i::Handle<i::String> str = Utils::OpenHandle(this);
3698 if (IsDeadCheck(str->GetIsolate(), "v8::String::Utf8Length()")) return 0;
3699 return str->Utf8Length();
3703 uint32_t String::Hash() const {
3704 i::Handle<i::String> str = Utils::OpenHandle(this);
3705 if (IsDeadCheck(str->GetIsolate(), "v8::String::Hash()")) return 0;
3710 String::CompleteHashData String::CompleteHash() const {
3711 i::Handle<i::String> str = Utils::OpenHandle(this);
3712 if (IsDeadCheck(str->GetIsolate(), "v8::String::CompleteHash()")) return CompleteHashData();
3713 CompleteHashData result;
3714 result.length = str->length();
3715 result.hash = str->Hash();
3716 if (str->IsSeqString())
3717 result.symbol_id = i::SeqString::cast(*str)->symbol_id();
3722 uint32_t String::ComputeHash(uint16_t *string, int length) {
3723 return i::HashSequentialString<i::uc16>(string, length) >> i::String::kHashShift;
3727 uint32_t String::ComputeHash(char *string, int length) {
3728 return i::HashSequentialString<char>(string, length) >> i::String::kHashShift;
3732 uint16_t String::GetCharacter(int index)
3734 i::Handle<i::String> str = Utils::OpenHandle(this);
3735 return str->Get(index);
3739 bool String::Equals(uint16_t *string, int length) {
3740 i::Handle<i::String> str = Utils::OpenHandle(this);
3741 if (IsDeadCheck(str->GetIsolate(), "v8::String::Equals()")) return 0;
3742 return str->SlowEqualsExternal(string, length);
3746 bool String::Equals(char *string, int length)
3748 i::Handle<i::String> str = Utils::OpenHandle(this);
3749 if (IsDeadCheck(str->GetIsolate(), "v8::String::Equals()")) return 0;
3750 return str->SlowEqualsExternal(string, length);
3754 int String::WriteUtf8(char* buffer,
3757 int options) const {
3758 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3759 if (IsDeadCheck(isolate, "v8::String::WriteUtf8()")) return 0;
3760 LOG_API(isolate, "String::WriteUtf8");
3762 i::Handle<i::String> str = Utils::OpenHandle(this);
3763 if (str->IsAsciiRepresentation()) {
3765 if (capacity == -1) {
3766 capacity = str->length() + 1;
3767 len = str->length();
3769 len = i::Min(capacity, str->length());
3771 i::String::WriteToFlat(*str, buffer, 0, len);
3772 if (nchars_ref != NULL) *nchars_ref = len;
3773 if (!(options & NO_NULL_TERMINATION) && capacity > len) {
3780 i::StringInputBuffer& write_input_buffer = *isolate->write_input_buffer();
3781 isolate->string_tracker()->RecordWrite(str);
3782 if (options & HINT_MANY_WRITES_EXPECTED) {
3783 // Flatten the string for efficiency. This applies whether we are
3784 // using StringInputBuffer or Get(i) to access the characters.
3787 write_input_buffer.Reset(0, *str);
3788 int len = str->length();
3789 // Encode the first K - 3 bytes directly into the buffer since we
3790 // know there's room for them. If no capacity is given we copy all
3792 int fast_end = capacity - (unibrow::Utf8::kMaxEncodedSize - 1);
3796 for (i = 0; i < len && (capacity == -1 || pos < fast_end); i++) {
3797 i::uc32 c = write_input_buffer.GetNext();
3798 int written = unibrow::Utf8::Encode(buffer + pos, c);
3803 // For the last characters we need to check the length for each one
3804 // because they may be longer than the remaining space in the
3806 char intermediate[unibrow::Utf8::kMaxEncodedSize];
3807 for (; i < len && pos < capacity; i++) {
3808 i::uc32 c = write_input_buffer.GetNext();
3809 int written = unibrow::Utf8::Encode(intermediate, c);
3810 if (pos + written <= capacity) {
3811 for (int j = 0; j < written; j++)
3812 buffer[pos + j] = intermediate[j];
3816 // We've reached the end of the buffer
3821 if (nchars_ref != NULL) *nchars_ref = nchars;
3822 if (!(options & NO_NULL_TERMINATION) &&
3823 (i == len && (capacity == -1 || pos < capacity)))
3824 buffer[pos++] = '\0';
3829 int String::WriteAscii(char* buffer,
3832 int options) const {
3833 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3834 if (IsDeadCheck(isolate, "v8::String::WriteAscii()")) return 0;
3835 LOG_API(isolate, "String::WriteAscii");
3837 i::StringInputBuffer& write_input_buffer = *isolate->write_input_buffer();
3838 ASSERT(start >= 0 && length >= -1);
3839 i::Handle<i::String> str = Utils::OpenHandle(this);
3840 isolate->string_tracker()->RecordWrite(str);
3841 if (options & HINT_MANY_WRITES_EXPECTED) {
3842 // Flatten the string for efficiency. This applies whether we are
3843 // using StringInputBuffer or Get(i) to access the characters.
3847 if ( (length == -1) || (length > str->length() - start) )
3848 end = str->length() - start;
3849 if (end < 0) return 0;
3850 write_input_buffer.Reset(start, *str);
3852 for (i = 0; i < end; i++) {
3853 char c = static_cast<char>(write_input_buffer.GetNext());
3854 if (c == '\0') c = ' ';
3857 if (!(options & NO_NULL_TERMINATION) && (length == -1 || i < length))
3863 int String::Write(uint16_t* buffer,
3866 int options) const {
3867 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3868 if (IsDeadCheck(isolate, "v8::String::Write()")) return 0;
3869 LOG_API(isolate, "String::Write");
3871 ASSERT(start >= 0 && length >= -1);
3872 i::Handle<i::String> str = Utils::OpenHandle(this);
3873 isolate->string_tracker()->RecordWrite(str);
3874 if (options & HINT_MANY_WRITES_EXPECTED) {
3875 // Flatten the string for efficiency. This applies whether we are
3876 // using StringInputBuffer or Get(i) to access the characters.
3879 int end = start + length;
3880 if ((length == -1) || (length > str->length() - start) )
3881 end = str->length();
3882 if (end < 0) return 0;
3883 i::String::WriteToFlat(*str, buffer, start, end);
3884 if (!(options & NO_NULL_TERMINATION) &&
3885 (length == -1 || end - start < length)) {
3886 buffer[end - start] = '\0';
3892 bool v8::String::IsExternal() const {
3893 i::Handle<i::String> str = Utils::OpenHandle(this);
3894 if (IsDeadCheck(str->GetIsolate(), "v8::String::IsExternal()")) {
3897 EnsureInitializedForIsolate(str->GetIsolate(), "v8::String::IsExternal()");
3898 return i::StringShape(*str).IsExternalTwoByte();
3902 bool v8::String::IsExternalAscii() const {
3903 i::Handle<i::String> str = Utils::OpenHandle(this);
3904 if (IsDeadCheck(str->GetIsolate(), "v8::String::IsExternalAscii()")) {
3907 return i::StringShape(*str).IsExternalAscii();
3911 void v8::String::VerifyExternalStringResource(
3912 v8::String::ExternalStringResource* value) const {
3913 i::Handle<i::String> str = Utils::OpenHandle(this);
3914 const v8::String::ExternalStringResource* expected;
3915 if (i::StringShape(*str).IsExternalTwoByte()) {
3916 const void* resource =
3917 i::Handle<i::ExternalTwoByteString>::cast(str)->resource();
3918 expected = reinterpret_cast<const ExternalStringResource*>(resource);
3922 CHECK_EQ(expected, value);
3926 const v8::String::ExternalAsciiStringResource*
3927 v8::String::GetExternalAsciiStringResource() const {
3928 i::Handle<i::String> str = Utils::OpenHandle(this);
3929 if (IsDeadCheck(str->GetIsolate(),
3930 "v8::String::GetExternalAsciiStringResource()")) {
3933 if (i::StringShape(*str).IsExternalAscii()) {
3934 const void* resource =
3935 i::Handle<i::ExternalAsciiString>::cast(str)->resource();
3936 return reinterpret_cast<const ExternalAsciiStringResource*>(resource);
3943 double Number::Value() const {
3944 if (IsDeadCheck(i::Isolate::Current(), "v8::Number::Value()")) return 0;
3945 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3946 return obj->Number();
3950 bool Boolean::Value() const {
3951 if (IsDeadCheck(i::Isolate::Current(), "v8::Boolean::Value()")) return false;
3952 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3953 return obj->IsTrue();
3957 int64_t Integer::Value() const {
3958 if (IsDeadCheck(i::Isolate::Current(), "v8::Integer::Value()")) return 0;
3959 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3961 return i::Smi::cast(*obj)->value();
3963 return static_cast<int64_t>(obj->Number());
3968 int32_t Int32::Value() const {
3969 if (IsDeadCheck(i::Isolate::Current(), "v8::Int32::Value()")) return 0;
3970 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3972 return i::Smi::cast(*obj)->value();
3974 return static_cast<int32_t>(obj->Number());
3979 uint32_t Uint32::Value() const {
3980 if (IsDeadCheck(i::Isolate::Current(), "v8::Uint32::Value()")) return 0;
3981 i::Handle<i::Object> obj = Utils::OpenHandle(this);
3983 return i::Smi::cast(*obj)->value();
3985 return static_cast<uint32_t>(obj->Number());
3990 int v8::Object::InternalFieldCount() {
3991 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3992 if (IsDeadCheck(obj->GetIsolate(), "v8::Object::InternalFieldCount()")) {
3995 return obj->GetInternalFieldCount();
3999 Local<Value> v8::Object::CheckedGetInternalField(int index) {
4000 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
4001 if (IsDeadCheck(obj->GetIsolate(), "v8::Object::GetInternalField()")) {
4002 return Local<Value>();
4004 if (!ApiCheck(index < obj->GetInternalFieldCount(),
4005 "v8::Object::GetInternalField()",
4006 "Reading internal field out of bounds")) {
4007 return Local<Value>();
4009 i::Handle<i::Object> value(obj->GetInternalField(index));
4010 Local<Value> result = Utils::ToLocal(value);
4012 Local<Value> unchecked = UncheckedGetInternalField(index);
4013 ASSERT(unchecked.IsEmpty() || (unchecked == result));
4019 void v8::Object::SetInternalField(int index, v8::Handle<Value> value) {
4020 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
4021 i::Isolate* isolate = obj->GetIsolate();
4022 if (IsDeadCheck(isolate, "v8::Object::SetInternalField()")) {
4025 if (!ApiCheck(index < obj->GetInternalFieldCount(),
4026 "v8::Object::SetInternalField()",
4027 "Writing internal field out of bounds")) {
4031 i::Handle<i::Object> val = Utils::OpenHandle(*value);
4032 obj->SetInternalField(index, *val);
4036 static bool CanBeEncodedAsSmi(void* ptr) {
4037 const uintptr_t address = reinterpret_cast<uintptr_t>(ptr);
4038 return ((address & i::kEncodablePointerMask) == 0);
4042 static i::Smi* EncodeAsSmi(void* ptr) {
4043 ASSERT(CanBeEncodedAsSmi(ptr));
4044 const uintptr_t address = reinterpret_cast<uintptr_t>(ptr);
4045 i::Smi* result = reinterpret_cast<i::Smi*>(address << i::kPointerToSmiShift);
4046 ASSERT(i::Internals::HasSmiTag(result));
4047 ASSERT_EQ(result, i::Smi::FromInt(result->value()));
4048 ASSERT_EQ(ptr, i::Internals::GetExternalPointerFromSmi(result));
4053 void v8::Object::SetPointerInInternalField(int index, void* value) {
4054 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4056 if (CanBeEncodedAsSmi(value)) {
4057 Utils::OpenHandle(this)->SetInternalField(index, EncodeAsSmi(value));
4060 i::Handle<i::Foreign> foreign =
4061 isolate->factory()->NewForeign(
4062 reinterpret_cast<i::Address>(value), i::TENURED);
4063 if (!foreign.is_null())
4064 Utils::OpenHandle(this)->SetInternalField(index, *foreign);
4066 ASSERT_EQ(value, GetPointerFromInternalField(index));
4070 void v8::Object::SetExternalResource(v8::Object::ExternalResource *resource) {
4071 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4073 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
4074 if (CanBeEncodedAsSmi(resource)) {
4075 obj->SetExternalResourceObject(EncodeAsSmi(resource));
4077 obj->SetExternalResourceObject(*isolate->factory()->NewForeign(static_cast<i::Address>((void *)resource)));
4079 if (!obj->IsSymbol()) {
4080 isolate->heap()->external_string_table()->AddObject(*obj);
4085 v8::Object::ExternalResource *v8::Object::GetExternalResource() {
4086 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
4087 i::Object* value = obj->GetExternalResourceObject();
4088 if (value->IsSmi()) {
4089 return reinterpret_cast<v8::Object::ExternalResource*>(i::Internals::GetExternalPointerFromSmi(value));
4090 } else if (value->IsForeign()) {
4091 return reinterpret_cast<v8::Object::ExternalResource*>(i::Foreign::cast(value)->address());
4098 // --- E n v i r o n m e n t ---
4101 bool v8::V8::Initialize() {
4102 i::Isolate* isolate = i::Isolate::UncheckedCurrent();
4103 if (isolate != NULL && isolate->IsInitialized()) {
4106 return InitializeHelper();
4110 void v8::V8::SetEntropySource(EntropySource source) {
4111 i::V8::SetEntropySource(source);
4115 bool v8::V8::Dispose() {
4116 i::Isolate* isolate = i::Isolate::Current();
4117 if (!ApiCheck(isolate != NULL && isolate->IsDefaultIsolate(),
4118 "v8::V8::Dispose()",
4119 "Use v8::Isolate::Dispose() for a non-default isolate.")) {
4127 HeapStatistics::HeapStatistics(): total_heap_size_(0),
4128 total_heap_size_executable_(0),
4130 heap_size_limit_(0) { }
4133 void v8::V8::GetHeapStatistics(HeapStatistics* heap_statistics) {
4134 if (!i::Isolate::Current()->IsInitialized()) {
4135 // Isolate is unitialized thus heap is not configured yet.
4136 heap_statistics->set_total_heap_size(0);
4137 heap_statistics->set_total_heap_size_executable(0);
4138 heap_statistics->set_used_heap_size(0);
4139 heap_statistics->set_heap_size_limit(0);
4143 i::Heap* heap = i::Isolate::Current()->heap();
4144 heap_statistics->set_total_heap_size(heap->CommittedMemory());
4145 heap_statistics->set_total_heap_size_executable(
4146 heap->CommittedMemoryExecutable());
4147 heap_statistics->set_used_heap_size(heap->SizeOfObjects());
4148 heap_statistics->set_heap_size_limit(heap->MaxReserved());
4152 bool v8::V8::IdleNotification() {
4153 // Returning true tells the caller that it need not
4154 // continue to call IdleNotification.
4155 i::Isolate* isolate = i::Isolate::Current();
4156 if (isolate == NULL || !isolate->IsInitialized()) return true;
4157 return i::V8::IdleNotification();
4161 void v8::V8::LowMemoryNotification() {
4162 i::Isolate* isolate = i::Isolate::Current();
4163 if (isolate == NULL || !isolate->IsInitialized()) return;
4164 isolate->heap()->CollectAllAvailableGarbage();
4168 int v8::V8::ContextDisposedNotification() {
4169 i::Isolate* isolate = i::Isolate::Current();
4170 if (!isolate->IsInitialized()) return 0;
4171 return isolate->heap()->NotifyContextDisposed();
4175 const char* v8::V8::GetVersion() {
4176 return i::Version::GetVersion();
4180 static i::Handle<i::FunctionTemplateInfo>
4181 EnsureConstructor(i::Handle<i::ObjectTemplateInfo> templ) {
4182 if (templ->constructor()->IsUndefined()) {
4183 Local<FunctionTemplate> constructor = FunctionTemplate::New();
4184 Utils::OpenHandle(*constructor)->set_instance_template(*templ);
4185 templ->set_constructor(*Utils::OpenHandle(*constructor));
4187 return i::Handle<i::FunctionTemplateInfo>(
4188 i::FunctionTemplateInfo::cast(templ->constructor()));
4192 Persistent<Context> v8::Context::New(
4193 v8::ExtensionConfiguration* extensions,
4194 v8::Handle<ObjectTemplate> global_template,
4195 v8::Handle<Value> global_object) {
4196 i::Isolate* isolate = i::Isolate::Current();
4197 EnsureInitializedForIsolate(isolate, "v8::Context::New()");
4198 LOG_API(isolate, "Context::New");
4199 ON_BAILOUT(isolate, "v8::Context::New()", return Persistent<Context>());
4201 // Enter V8 via an ENTER_V8 scope.
4202 i::Handle<i::Context> env;
4205 v8::Handle<ObjectTemplate> proxy_template = global_template;
4206 i::Handle<i::FunctionTemplateInfo> proxy_constructor;
4207 i::Handle<i::FunctionTemplateInfo> global_constructor;
4209 if (!global_template.IsEmpty()) {
4210 // Make sure that the global_template has a constructor.
4211 global_constructor =
4212 EnsureConstructor(Utils::OpenHandle(*global_template));
4214 // Create a fresh template for the global proxy object.
4215 proxy_template = ObjectTemplate::New();
4217 EnsureConstructor(Utils::OpenHandle(*proxy_template));
4219 // Set the global template to be the prototype template of
4220 // global proxy template.
4221 proxy_constructor->set_prototype_template(
4222 *Utils::OpenHandle(*global_template));
4224 // Migrate security handlers from global_template to
4225 // proxy_template. Temporarily removing access check
4226 // information from the global template.
4227 if (!global_constructor->access_check_info()->IsUndefined()) {
4228 proxy_constructor->set_access_check_info(
4229 global_constructor->access_check_info());
4230 proxy_constructor->set_needs_access_check(
4231 global_constructor->needs_access_check());
4232 global_constructor->set_needs_access_check(false);
4233 global_constructor->set_access_check_info(
4234 isolate->heap()->undefined_value());
4238 // Create the environment.
4239 env = isolate->bootstrapper()->CreateEnvironment(
4241 Utils::OpenHandle(*global_object),
4245 // Restore the access check info on the global template.
4246 if (!global_template.IsEmpty()) {
4247 ASSERT(!global_constructor.is_null());
4248 ASSERT(!proxy_constructor.is_null());
4249 global_constructor->set_access_check_info(
4250 proxy_constructor->access_check_info());
4251 global_constructor->set_needs_access_check(
4252 proxy_constructor->needs_access_check());
4254 isolate->runtime_profiler()->Reset();
4258 if (env.is_null()) {
4259 return Persistent<Context>();
4261 return Persistent<Context>(Utils::ToLocal(env));
4265 void v8::Context::SetSecurityToken(Handle<Value> token) {
4266 i::Isolate* isolate = i::Isolate::Current();
4267 if (IsDeadCheck(isolate, "v8::Context::SetSecurityToken()")) {
4271 i::Handle<i::Context> env = Utils::OpenHandle(this);
4272 i::Handle<i::Object> token_handle = Utils::OpenHandle(*token);
4273 env->set_security_token(*token_handle);
4277 void v8::Context::UseDefaultSecurityToken() {
4278 i::Isolate* isolate = i::Isolate::Current();
4279 if (IsDeadCheck(isolate,
4280 "v8::Context::UseDefaultSecurityToken()")) {
4284 i::Handle<i::Context> env = Utils::OpenHandle(this);
4285 env->set_security_token(env->global());
4289 Handle<Value> v8::Context::GetSecurityToken() {
4290 i::Isolate* isolate = i::Isolate::Current();
4291 if (IsDeadCheck(isolate, "v8::Context::GetSecurityToken()")) {
4292 return Handle<Value>();
4294 i::Handle<i::Context> env = Utils::OpenHandle(this);
4295 i::Object* security_token = env->security_token();
4296 i::Handle<i::Object> token_handle(security_token);
4297 return Utils::ToLocal(token_handle);
4301 bool Context::HasOutOfMemoryException() {
4302 i::Handle<i::Context> env = Utils::OpenHandle(this);
4303 return env->has_out_of_memory();
4307 bool Context::InContext() {
4308 return i::Isolate::Current()->context() != NULL;
4312 v8::Local<v8::Context> Context::GetEntered() {
4313 i::Isolate* isolate = i::Isolate::Current();
4314 if (!EnsureInitializedForIsolate(isolate, "v8::Context::GetEntered()")) {
4315 return Local<Context>();
4317 i::Handle<i::Object> last =
4318 isolate->handle_scope_implementer()->LastEnteredContext();
4319 if (last.is_null()) return Local<Context>();
4320 i::Handle<i::Context> context = i::Handle<i::Context>::cast(last);
4321 return Utils::ToLocal(context);
4325 v8::Local<v8::Context> Context::GetCurrent() {
4326 i::Isolate* isolate = i::Isolate::Current();
4327 if (IsDeadCheck(isolate, "v8::Context::GetCurrent()")) {
4328 return Local<Context>();
4330 i::Handle<i::Object> current = isolate->global_context();
4331 if (current.is_null()) return Local<Context>();
4332 i::Handle<i::Context> context = i::Handle<i::Context>::cast(current);
4333 return Utils::ToLocal(context);
4337 v8::Local<v8::Context> Context::GetCalling() {
4338 i::Isolate* isolate = i::Isolate::Current();
4339 if (IsDeadCheck(isolate, "v8::Context::GetCalling()")) {
4340 return Local<Context>();
4342 i::Handle<i::Object> calling =
4343 isolate->GetCallingGlobalContext();
4344 if (calling.is_null()) return Local<Context>();
4345 i::Handle<i::Context> context = i::Handle<i::Context>::cast(calling);
4346 return Utils::ToLocal(context);
4350 v8::Local<v8::Object> Context::GetCallingQmlGlobal() {
4351 i::Isolate* isolate = i::Isolate::Current();
4352 if (IsDeadCheck(isolate, "v8::Context::GetCallingQmlGlobal()")) {
4353 return Local<Object>();
4356 i::Context *context = isolate->context();
4357 if (!context->qml_global()->IsUndefined()) {
4358 i::Handle<i::Object> qmlglobal(context->qml_global());
4359 return Utils::ToLocal(i::Handle<i::JSObject>::cast(qmlglobal));
4362 i::JavaScriptFrameIterator it;
4363 if (it.done()) return Local<Object>();
4364 context = i::Context::cast(it.frame()->context());
4365 if (!context->qml_global()->IsUndefined()) {
4366 i::Handle<i::Object> qmlglobal(context->qml_global());
4367 return Utils::ToLocal(i::Handle<i::JSObject>::cast(qmlglobal));
4369 return Local<Object>();
4373 v8::Local<v8::Value> Context::GetCallingScriptData()
4375 i::Isolate* isolate = i::Isolate::Current();
4376 if (IsDeadCheck(isolate, "v8::Context::GetCallingScriptData()")) {
4377 return Local<Object>();
4380 i::JavaScriptFrameIterator it;
4381 if (it.done()) return Local<Object>();
4382 i::Handle<i::Script> script(i::Script::cast(i::JSFunction::cast(it.frame()->function())->shared()->script()));
4383 return Utils::ToLocal(i::Handle<i::Object>(script->data()));
4386 v8::Local<v8::Object> Context::Global() {
4387 if (IsDeadCheck(i::Isolate::Current(), "v8::Context::Global()")) {
4388 return Local<v8::Object>();
4390 i::Object** ctx = reinterpret_cast<i::Object**>(this);
4391 i::Handle<i::Context> context =
4392 i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx));
4393 i::Handle<i::Object> global(context->global_proxy());
4394 return Utils::ToLocal(i::Handle<i::JSObject>::cast(global));
4398 void Context::DetachGlobal() {
4399 i::Isolate* isolate = i::Isolate::Current();
4400 if (IsDeadCheck(isolate, "v8::Context::DetachGlobal()")) return;
4402 i::Object** ctx = reinterpret_cast<i::Object**>(this);
4403 i::Handle<i::Context> context =
4404 i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx));
4405 isolate->bootstrapper()->DetachGlobal(context);
4409 void Context::ReattachGlobal(Handle<Object> global_object) {
4410 i::Isolate* isolate = i::Isolate::Current();
4411 if (IsDeadCheck(isolate, "v8::Context::ReattachGlobal()")) return;
4413 i::Object** ctx = reinterpret_cast<i::Object**>(this);
4414 i::Handle<i::Context> context =
4415 i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx));
4416 isolate->bootstrapper()->ReattachGlobal(
4418 Utils::OpenHandle(*global_object));
4422 void Context::AllowCodeGenerationFromStrings(bool allow) {
4423 i::Isolate* isolate = i::Isolate::Current();
4424 if (IsDeadCheck(isolate, "v8::Context::AllowCodeGenerationFromStrings()")) {
4428 i::Object** ctx = reinterpret_cast<i::Object**>(this);
4429 i::Handle<i::Context> context =
4430 i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx));
4431 context->set_allow_code_gen_from_strings(
4432 allow ? isolate->heap()->true_value() : isolate->heap()->false_value());
4436 void V8::SetWrapperClassId(i::Object** global_handle, uint16_t class_id) {
4437 i::GlobalHandles::SetWrapperClassId(global_handle, class_id);
4441 Local<v8::Object> ObjectTemplate::NewInstance() {
4442 i::Isolate* isolate = i::Isolate::Current();
4443 ON_BAILOUT(isolate, "v8::ObjectTemplate::NewInstance()",
4444 return Local<v8::Object>());
4445 LOG_API(isolate, "ObjectTemplate::NewInstance");
4447 EXCEPTION_PREAMBLE(isolate);
4448 i::Handle<i::Object> obj =
4449 i::Execution::InstantiateObject(Utils::OpenHandle(this),
4450 &has_pending_exception);
4451 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>());
4452 return Utils::ToLocal(i::Handle<i::JSObject>::cast(obj));
4456 Local<v8::Function> FunctionTemplate::GetFunction() {
4457 i::Isolate* isolate = i::Isolate::Current();
4458 ON_BAILOUT(isolate, "v8::FunctionTemplate::GetFunction()",
4459 return Local<v8::Function>());
4460 LOG_API(isolate, "FunctionTemplate::GetFunction");
4462 EXCEPTION_PREAMBLE(isolate);
4463 i::Handle<i::Object> obj =
4464 i::Execution::InstantiateFunction(Utils::OpenHandle(this),
4465 &has_pending_exception);
4466 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Function>());
4467 return Utils::ToLocal(i::Handle<i::JSFunction>::cast(obj));
4471 bool FunctionTemplate::HasInstance(v8::Handle<v8::Value> value) {
4472 ON_BAILOUT(i::Isolate::Current(), "v8::FunctionTemplate::HasInstanceOf()",
4474 i::Object* obj = *Utils::OpenHandle(*value);
4475 return obj->IsInstanceOf(*Utils::OpenHandle(this));
4479 static Local<External> ExternalNewImpl(void* data) {
4480 return Utils::ToLocal(FACTORY->NewForeign(static_cast<i::Address>(data)));
4483 static void* ExternalValueImpl(i::Handle<i::Object> obj) {
4484 return reinterpret_cast<void*>(i::Foreign::cast(*obj)->address());
4488 Local<Value> v8::External::Wrap(void* data) {
4489 i::Isolate* isolate = i::Isolate::Current();
4490 STATIC_ASSERT(sizeof(data) == sizeof(i::Address));
4491 EnsureInitializedForIsolate(isolate, "v8::External::Wrap()");
4492 LOG_API(isolate, "External::Wrap");
4495 v8::Local<v8::Value> result = CanBeEncodedAsSmi(data)
4496 ? Utils::ToLocal(i::Handle<i::Object>(EncodeAsSmi(data)))
4497 : v8::Local<v8::Value>(ExternalNewImpl(data));
4499 ASSERT_EQ(data, Unwrap(result));
4504 void* v8::Object::SlowGetPointerFromInternalField(int index) {
4505 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
4506 i::Object* value = obj->GetInternalField(index);
4507 if (value->IsSmi()) {
4508 return i::Internals::GetExternalPointerFromSmi(value);
4509 } else if (value->IsForeign()) {
4510 return reinterpret_cast<void*>(i::Foreign::cast(value)->address());
4517 void* v8::External::FullUnwrap(v8::Handle<v8::Value> wrapper) {
4518 if (IsDeadCheck(i::Isolate::Current(), "v8::External::Unwrap()")) return 0;
4519 i::Handle<i::Object> obj = Utils::OpenHandle(*wrapper);
4522 result = i::Internals::GetExternalPointerFromSmi(*obj);
4523 } else if (obj->IsForeign()) {
4524 result = ExternalValueImpl(obj);
4528 ASSERT_EQ(result, QuickUnwrap(wrapper));
4533 Local<External> v8::External::New(void* data) {
4534 STATIC_ASSERT(sizeof(data) == sizeof(i::Address));
4535 i::Isolate* isolate = i::Isolate::Current();
4536 EnsureInitializedForIsolate(isolate, "v8::External::New()");
4537 LOG_API(isolate, "External::New");
4539 return ExternalNewImpl(data);
4543 void* External::Value() const {
4544 if (IsDeadCheck(i::Isolate::Current(), "v8::External::Value()")) return 0;
4545 i::Handle<i::Object> obj = Utils::OpenHandle(this);
4546 return ExternalValueImpl(obj);
4550 Local<String> v8::String::Empty() {
4551 i::Isolate* isolate = i::Isolate::Current();
4552 EnsureInitializedForIsolate(isolate, "v8::String::Empty()");
4553 LOG_API(isolate, "String::Empty()");
4554 return Utils::ToLocal(isolate->factory()->empty_symbol());
4558 Local<String> v8::String::New(const char* data, int length) {
4559 i::Isolate* isolate = i::Isolate::Current();
4560 EnsureInitializedForIsolate(isolate, "v8::String::New()");
4561 LOG_API(isolate, "String::New(char)");
4562 if (length == 0) return Empty();
4564 if (length == -1) length = i::StrLength(data);
4565 i::Handle<i::String> result =
4566 isolate->factory()->NewStringFromUtf8(
4567 i::Vector<const char>(data, length));
4568 return Utils::ToLocal(result);
4572 Local<String> v8::String::Concat(Handle<String> left, Handle<String> right) {
4573 i::Handle<i::String> left_string = Utils::OpenHandle(*left);
4574 i::Isolate* isolate = left_string->GetIsolate();
4575 EnsureInitializedForIsolate(isolate, "v8::String::New()");
4576 LOG_API(isolate, "String::New(char)");
4578 i::Handle<i::String> right_string = Utils::OpenHandle(*right);
4579 i::Handle<i::String> result = isolate->factory()->NewConsString(left_string,
4581 return Utils::ToLocal(result);
4585 Local<String> v8::String::NewUndetectable(const char* data, int length) {
4586 i::Isolate* isolate = i::Isolate::Current();
4587 EnsureInitializedForIsolate(isolate, "v8::String::NewUndetectable()");
4588 LOG_API(isolate, "String::NewUndetectable(char)");
4590 if (length == -1) length = i::StrLength(data);
4591 i::Handle<i::String> result =
4592 isolate->factory()->NewStringFromUtf8(
4593 i::Vector<const char>(data, length));
4594 result->MarkAsUndetectable();
4595 return Utils::ToLocal(result);
4599 static int TwoByteStringLength(const uint16_t* data) {
4601 while (data[length] != '\0') length++;
4606 Local<String> v8::String::New(const uint16_t* data, int length) {
4607 i::Isolate* isolate = i::Isolate::Current();
4608 EnsureInitializedForIsolate(isolate, "v8::String::New()");
4609 LOG_API(isolate, "String::New(uint16_)");
4610 if (length == 0) return Empty();
4612 if (length == -1) length = TwoByteStringLength(data);
4613 i::Handle<i::String> result =
4614 isolate->factory()->NewStringFromTwoByte(
4615 i::Vector<const uint16_t>(data, length));
4616 return Utils::ToLocal(result);
4620 Local<String> v8::String::NewUndetectable(const uint16_t* data, int length) {
4621 i::Isolate* isolate = i::Isolate::Current();
4622 EnsureInitializedForIsolate(isolate, "v8::String::NewUndetectable()");
4623 LOG_API(isolate, "String::NewUndetectable(uint16_)");
4625 if (length == -1) length = TwoByteStringLength(data);
4626 i::Handle<i::String> result =
4627 isolate->factory()->NewStringFromTwoByte(
4628 i::Vector<const uint16_t>(data, length));
4629 result->MarkAsUndetectable();
4630 return Utils::ToLocal(result);
4634 i::Handle<i::String> NewExternalStringHandle(i::Isolate* isolate,
4635 v8::String::ExternalStringResource* resource) {
4636 i::Handle<i::String> result =
4637 isolate->factory()->NewExternalStringFromTwoByte(resource);
4642 i::Handle<i::String> NewExternalAsciiStringHandle(i::Isolate* isolate,
4643 v8::String::ExternalAsciiStringResource* resource) {
4644 i::Handle<i::String> result =
4645 isolate->factory()->NewExternalStringFromAscii(resource);
4650 Local<String> v8::String::NewExternal(
4651 v8::String::ExternalStringResource* resource) {
4652 i::Isolate* isolate = i::Isolate::Current();
4653 EnsureInitializedForIsolate(isolate, "v8::String::NewExternal()");
4654 LOG_API(isolate, "String::NewExternal");
4656 i::Handle<i::String> result = NewExternalStringHandle(isolate, resource);
4657 isolate->heap()->external_string_table()->AddString(*result);
4658 return Utils::ToLocal(result);
4662 bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) {
4663 i::Handle<i::String> obj = Utils::OpenHandle(this);
4664 i::Isolate* isolate = obj->GetIsolate();
4665 if (IsDeadCheck(isolate, "v8::String::MakeExternal()")) return false;
4666 if (i::StringShape(*obj).IsExternalTwoByte()) {
4667 return false; // Already an external string.
4670 if (isolate->string_tracker()->IsFreshUnusedString(obj)) {
4673 if (isolate->heap()->IsInGCPostProcessing()) {
4676 bool result = obj->MakeExternal(resource);
4677 if (result && !obj->IsSymbol()) {
4678 isolate->heap()->external_string_table()->AddString(*obj);
4684 Local<String> v8::String::NewExternal(
4685 v8::String::ExternalAsciiStringResource* resource) {
4686 i::Isolate* isolate = i::Isolate::Current();
4687 EnsureInitializedForIsolate(isolate, "v8::String::NewExternal()");
4688 LOG_API(isolate, "String::NewExternal");
4690 i::Handle<i::String> result = NewExternalAsciiStringHandle(isolate, resource);
4691 isolate->heap()->external_string_table()->AddString(*result);
4692 return Utils::ToLocal(result);
4696 bool v8::String::MakeExternal(
4697 v8::String::ExternalAsciiStringResource* resource) {
4698 i::Handle<i::String> obj = Utils::OpenHandle(this);
4699 i::Isolate* isolate = obj->GetIsolate();
4700 if (IsDeadCheck(isolate, "v8::String::MakeExternal()")) return false;
4701 if (i::StringShape(*obj).IsExternalTwoByte()) {
4702 return false; // Already an external string.
4705 if (isolate->string_tracker()->IsFreshUnusedString(obj)) {
4708 if (isolate->heap()->IsInGCPostProcessing()) {
4711 bool result = obj->MakeExternal(resource);
4712 if (result && !obj->IsSymbol()) {
4713 isolate->heap()->external_string_table()->AddString(*obj);
4719 bool v8::String::CanMakeExternal() {
4720 if (!internal::FLAG_clever_optimizations) return false;
4721 i::Handle<i::String> obj = Utils::OpenHandle(this);
4722 i::Isolate* isolate = obj->GetIsolate();
4723 if (IsDeadCheck(isolate, "v8::String::CanMakeExternal()")) return false;
4724 if (isolate->string_tracker()->IsFreshUnusedString(obj)) {
4727 int size = obj->Size(); // Byte size of the original string.
4728 if (size < i::ExternalString::kSize)
4730 i::StringShape shape(*obj);
4731 return !shape.IsExternal();
4735 Local<v8::Object> v8::Object::New() {
4736 i::Isolate* isolate = i::Isolate::Current();
4737 EnsureInitializedForIsolate(isolate, "v8::Object::New()");
4738 LOG_API(isolate, "Object::New");
4740 i::Handle<i::JSObject> obj =
4741 isolate->factory()->NewJSObject(isolate->object_function());
4742 return Utils::ToLocal(obj);
4746 Local<v8::Value> v8::NumberObject::New(double value) {
4747 i::Isolate* isolate = i::Isolate::Current();
4748 EnsureInitializedForIsolate(isolate, "v8::NumberObject::New()");
4749 LOG_API(isolate, "NumberObject::New");
4751 i::Handle<i::Object> number = isolate->factory()->NewNumber(value);
4752 i::Handle<i::Object> obj = isolate->factory()->ToObject(number);
4753 return Utils::ToLocal(obj);
4757 double v8::NumberObject::NumberValue() const {
4758 i::Isolate* isolate = i::Isolate::Current();
4759 if (IsDeadCheck(isolate, "v8::NumberObject::NumberValue()")) return 0;
4760 LOG_API(isolate, "NumberObject::NumberValue");
4761 i::Handle<i::Object> obj = Utils::OpenHandle(this);
4762 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
4763 return jsvalue->value()->Number();
4767 Local<v8::Value> v8::BooleanObject::New(bool value) {
4768 i::Isolate* isolate = i::Isolate::Current();
4769 EnsureInitializedForIsolate(isolate, "v8::BooleanObject::New()");
4770 LOG_API(isolate, "BooleanObject::New");
4772 i::Handle<i::Object> boolean(value ? isolate->heap()->true_value()
4773 : isolate->heap()->false_value());
4774 i::Handle<i::Object> obj = isolate->factory()->ToObject(boolean);
4775 return Utils::ToLocal(obj);
4779 bool v8::BooleanObject::BooleanValue() const {
4780 i::Isolate* isolate = i::Isolate::Current();
4781 if (IsDeadCheck(isolate, "v8::BooleanObject::BooleanValue()")) return 0;
4782 LOG_API(isolate, "BooleanObject::BooleanValue");
4783 i::Handle<i::Object> obj = Utils::OpenHandle(this);
4784 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
4785 return jsvalue->value()->IsTrue();
4789 Local<v8::Value> v8::StringObject::New(Handle<String> value) {
4790 i::Isolate* isolate = i::Isolate::Current();
4791 EnsureInitializedForIsolate(isolate, "v8::StringObject::New()");
4792 LOG_API(isolate, "StringObject::New");
4794 i::Handle<i::Object> obj =
4795 isolate->factory()->ToObject(Utils::OpenHandle(*value));
4796 return Utils::ToLocal(obj);
4800 Local<v8::String> v8::StringObject::StringValue() const {
4801 i::Isolate* isolate = i::Isolate::Current();
4802 if (IsDeadCheck(isolate, "v8::StringObject::StringValue()")) {
4803 return Local<v8::String>();
4805 LOG_API(isolate, "StringObject::StringValue");
4806 i::Handle<i::Object> obj = Utils::OpenHandle(this);
4807 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
4808 return Utils::ToLocal(
4809 i::Handle<i::String>(i::String::cast(jsvalue->value())));
4813 Local<v8::Value> v8::Date::New(double time) {
4814 i::Isolate* isolate = i::Isolate::Current();
4815 EnsureInitializedForIsolate(isolate, "v8::Date::New()");
4816 LOG_API(isolate, "Date::New");
4818 // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
4819 time = i::OS::nan_value();
4822 EXCEPTION_PREAMBLE(isolate);
4823 i::Handle<i::Object> obj =
4824 i::Execution::NewDate(time, &has_pending_exception);
4825 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Value>());
4826 return Utils::ToLocal(obj);
4830 double v8::Date::NumberValue() const {
4831 i::Isolate* isolate = i::Isolate::Current();
4832 if (IsDeadCheck(isolate, "v8::Date::NumberValue()")) return 0;
4833 LOG_API(isolate, "Date::NumberValue");
4834 i::Handle<i::Object> obj = Utils::OpenHandle(this);
4835 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
4836 return jsvalue->value()->Number();
4840 void v8::Date::DateTimeConfigurationChangeNotification() {
4841 i::Isolate* isolate = i::Isolate::Current();
4842 ON_BAILOUT(isolate, "v8::Date::DateTimeConfigurationChangeNotification()",
4844 LOG_API(isolate, "Date::DateTimeConfigurationChangeNotification");
4847 i::HandleScope scope(isolate);
4848 // Get the function ResetDateCache (defined in date-delay.js).
4849 i::Handle<i::String> func_name_str =
4850 isolate->factory()->LookupAsciiSymbol("ResetDateCache");
4851 i::MaybeObject* result =
4852 isolate->js_builtins_object()->GetProperty(*func_name_str);
4853 i::Object* object_func;
4854 if (!result->ToObject(&object_func)) {
4858 if (object_func->IsJSFunction()) {
4859 i::Handle<i::JSFunction> func =
4860 i::Handle<i::JSFunction>(i::JSFunction::cast(object_func));
4862 // Call ResetDateCache(0 but expect no exceptions:
4863 bool caught_exception = false;
4864 i::Execution::TryCall(func,
4865 isolate->js_builtins_object(),
4873 static i::Handle<i::String> RegExpFlagsToString(RegExp::Flags flags) {
4876 if ((flags & RegExp::kGlobal) != 0) flags_buf[num_flags++] = 'g';
4877 if ((flags & RegExp::kMultiline) != 0) flags_buf[num_flags++] = 'm';
4878 if ((flags & RegExp::kIgnoreCase) != 0) flags_buf[num_flags++] = 'i';
4879 ASSERT(num_flags <= static_cast<int>(ARRAY_SIZE(flags_buf)));
4880 return FACTORY->LookupSymbol(
4881 i::Vector<const char>(flags_buf, num_flags));
4885 Local<v8::RegExp> v8::RegExp::New(Handle<String> pattern,
4887 i::Isolate* isolate = Utils::OpenHandle(*pattern)->GetIsolate();
4888 EnsureInitializedForIsolate(isolate, "v8::RegExp::New()");
4889 LOG_API(isolate, "RegExp::New");
4891 EXCEPTION_PREAMBLE(isolate);
4892 i::Handle<i::JSRegExp> obj = i::Execution::NewJSRegExp(
4893 Utils::OpenHandle(*pattern),
4894 RegExpFlagsToString(flags),
4895 &has_pending_exception);
4896 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::RegExp>());
4897 return Utils::ToLocal(i::Handle<i::JSRegExp>::cast(obj));
4901 Local<v8::String> v8::RegExp::GetSource() const {
4902 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4903 if (IsDeadCheck(isolate, "v8::RegExp::GetSource()")) {
4904 return Local<v8::String>();
4906 i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
4907 return Utils::ToLocal(i::Handle<i::String>(obj->Pattern()));
4911 // Assert that the static flags cast in GetFlags is valid.
4912 #define REGEXP_FLAG_ASSERT_EQ(api_flag, internal_flag) \
4913 STATIC_ASSERT(static_cast<int>(v8::RegExp::api_flag) == \
4914 static_cast<int>(i::JSRegExp::internal_flag))
4915 REGEXP_FLAG_ASSERT_EQ(kNone, NONE);
4916 REGEXP_FLAG_ASSERT_EQ(kGlobal, GLOBAL);
4917 REGEXP_FLAG_ASSERT_EQ(kIgnoreCase, IGNORE_CASE);
4918 REGEXP_FLAG_ASSERT_EQ(kMultiline, MULTILINE);
4919 #undef REGEXP_FLAG_ASSERT_EQ
4921 v8::RegExp::Flags v8::RegExp::GetFlags() const {
4922 if (IsDeadCheck(i::Isolate::Current(), "v8::RegExp::GetFlags()")) {
4923 return v8::RegExp::kNone;
4925 i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
4926 return static_cast<RegExp::Flags>(obj->GetFlags().value());
4930 Local<v8::Array> v8::Array::New(int length) {
4931 i::Isolate* isolate = i::Isolate::Current();
4932 EnsureInitializedForIsolate(isolate, "v8::Array::New()");
4933 LOG_API(isolate, "Array::New");
4935 int real_length = length > 0 ? length : 0;
4936 i::Handle<i::JSArray> obj = isolate->factory()->NewJSArray(real_length);
4937 i::Handle<i::Object> length_obj =
4938 isolate->factory()->NewNumberFromInt(real_length);
4939 obj->set_length(*length_obj);
4940 return Utils::ToLocal(obj);
4944 uint32_t v8::Array::Length() const {
4945 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4946 if (IsDeadCheck(isolate, "v8::Array::Length()")) return 0;
4947 i::Handle<i::JSArray> obj = Utils::OpenHandle(this);
4948 i::Object* length = obj->length();
4949 if (length->IsSmi()) {
4950 return i::Smi::cast(length)->value();
4952 return static_cast<uint32_t>(length->Number());
4957 Local<Object> Array::CloneElementAt(uint32_t index) {
4958 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4959 ON_BAILOUT(isolate, "v8::Array::CloneElementAt()", return Local<Object>());
4960 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
4961 if (!self->HasFastElements()) {
4962 return Local<Object>();
4964 i::FixedArray* elms = i::FixedArray::cast(self->elements());
4965 i::Object* paragon = elms->get(index);
4966 if (!paragon->IsJSObject()) {
4967 return Local<Object>();
4969 i::Handle<i::JSObject> paragon_handle(i::JSObject::cast(paragon));
4970 EXCEPTION_PREAMBLE(isolate);
4972 i::Handle<i::JSObject> result = i::Copy(paragon_handle);
4973 has_pending_exception = result.is_null();
4974 EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>());
4975 return Utils::ToLocal(result);
4979 Local<String> v8::String::NewSymbol(const char* data, int length) {
4980 i::Isolate* isolate = i::Isolate::Current();
4981 EnsureInitializedForIsolate(isolate, "v8::String::NewSymbol()");
4982 LOG_API(isolate, "String::NewSymbol(char)");
4984 if (length == -1) length = i::StrLength(data);
4985 i::Handle<i::String> result =
4986 isolate->factory()->LookupSymbol(i::Vector<const char>(data, length));
4987 return Utils::ToLocal(result);
4991 Local<Number> v8::Number::New(double value) {
4992 i::Isolate* isolate = i::Isolate::Current();
4993 EnsureInitializedForIsolate(isolate, "v8::Number::New()");
4995 // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
4996 value = i::OS::nan_value();
4999 i::Handle<i::Object> result = isolate->factory()->NewNumber(value);
5000 return Utils::NumberToLocal(result);
5004 Local<Integer> v8::Integer::New(int32_t value) {
5005 i::Isolate* isolate = i::Isolate::UncheckedCurrent();
5006 EnsureInitializedForIsolate(isolate, "v8::Integer::New()");
5007 if (i::Smi::IsValid(value)) {
5008 return Utils::IntegerToLocal(i::Handle<i::Object>(i::Smi::FromInt(value),
5012 i::Handle<i::Object> result = isolate->factory()->NewNumber(value);
5013 return Utils::IntegerToLocal(result);
5017 Local<Integer> Integer::NewFromUnsigned(uint32_t value) {
5018 bool fits_into_int32_t = (value & (1 << 31)) == 0;
5019 if (fits_into_int32_t) {
5020 return Integer::New(static_cast<int32_t>(value));
5022 i::Isolate* isolate = i::Isolate::Current();
5024 i::Handle<i::Object> result = isolate->factory()->NewNumber(value);
5025 return Utils::IntegerToLocal(result);
5029 void V8::IgnoreOutOfMemoryException() {
5030 EnterIsolateIfNeeded()->set_ignore_out_of_memory(true);
5034 bool V8::AddMessageListener(MessageCallback that, Handle<Value> data) {
5035 i::Isolate* isolate = i::Isolate::Current();
5036 EnsureInitializedForIsolate(isolate, "v8::V8::AddMessageListener()");
5037 ON_BAILOUT(isolate, "v8::V8::AddMessageListener()", return false);
5039 i::HandleScope scope(isolate);
5040 NeanderArray listeners(isolate->factory()->message_listeners());
5041 NeanderObject obj(2);
5042 obj.set(0, *isolate->factory()->NewForeign(FUNCTION_ADDR(that)));
5043 obj.set(1, data.IsEmpty() ?
5044 isolate->heap()->undefined_value() :
5045 *Utils::OpenHandle(*data));
5046 listeners.add(obj.value());
5051 void V8::RemoveMessageListeners(MessageCallback that) {
5052 i::Isolate* isolate = i::Isolate::Current();
5053 EnsureInitializedForIsolate(isolate, "v8::V8::RemoveMessageListener()");
5054 ON_BAILOUT(isolate, "v8::V8::RemoveMessageListeners()", return);
5056 i::HandleScope scope(isolate);
5057 NeanderArray listeners(isolate->factory()->message_listeners());
5058 for (int i = 0; i < listeners.length(); i++) {
5059 if (listeners.get(i)->IsUndefined()) continue; // skip deleted ones
5061 NeanderObject listener(i::JSObject::cast(listeners.get(i)));
5062 i::Handle<i::Foreign> callback_obj(i::Foreign::cast(listener.get(0)));
5063 if (callback_obj->address() == FUNCTION_ADDR(that)) {
5064 listeners.set(i, isolate->heap()->undefined_value());
5070 void V8::SetCaptureStackTraceForUncaughtExceptions(
5073 StackTrace::StackTraceOptions options) {
5074 i::Isolate::Current()->SetCaptureStackTraceForUncaughtExceptions(
5081 void V8::SetCounterFunction(CounterLookupCallback callback) {
5082 i::Isolate* isolate = EnterIsolateIfNeeded();
5083 if (IsDeadCheck(isolate, "v8::V8::SetCounterFunction()")) return;
5084 isolate->stats_table()->SetCounterFunction(callback);
5087 void V8::SetCreateHistogramFunction(CreateHistogramCallback callback) {
5088 i::Isolate* isolate = EnterIsolateIfNeeded();
5089 if (IsDeadCheck(isolate, "v8::V8::SetCreateHistogramFunction()")) return;
5090 isolate->stats_table()->SetCreateHistogramFunction(callback);
5093 void V8::SetAddHistogramSampleFunction(AddHistogramSampleCallback callback) {
5094 i::Isolate* isolate = EnterIsolateIfNeeded();
5095 if (IsDeadCheck(isolate, "v8::V8::SetAddHistogramSampleFunction()")) return;
5096 isolate->stats_table()->
5097 SetAddHistogramSampleFunction(callback);
5100 void V8::EnableSlidingStateWindow() {
5101 i::Isolate* isolate = i::Isolate::Current();
5102 if (IsDeadCheck(isolate, "v8::V8::EnableSlidingStateWindow()")) return;
5103 isolate->logger()->EnableSlidingStateWindow();
5107 void V8::SetFailedAccessCheckCallbackFunction(
5108 FailedAccessCheckCallback callback) {
5109 i::Isolate* isolate = i::Isolate::Current();
5110 if (IsDeadCheck(isolate, "v8::V8::SetFailedAccessCheckCallbackFunction()")) {
5113 isolate->SetFailedAccessCheckCallback(callback);
5116 void V8::AddObjectGroup(Persistent<Value>* objects,
5118 RetainedObjectInfo* info) {
5119 i::Isolate* isolate = i::Isolate::Current();
5120 if (IsDeadCheck(isolate, "v8::V8::AddObjectGroup()")) return;
5121 STATIC_ASSERT(sizeof(Persistent<Value>) == sizeof(i::Object**));
5122 isolate->global_handles()->AddObjectGroup(
5123 reinterpret_cast<i::Object***>(objects), length, info);
5127 void V8::AddImplicitReferences(Persistent<Object> parent,
5128 Persistent<Value>* children,
5130 i::Isolate* isolate = i::Isolate::Current();
5131 if (IsDeadCheck(isolate, "v8::V8::AddImplicitReferences()")) return;
5132 STATIC_ASSERT(sizeof(Persistent<Value>) == sizeof(i::Object**));
5133 isolate->global_handles()->AddImplicitReferences(
5134 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(*parent)).location(),
5135 reinterpret_cast<i::Object***>(children), length);
5139 int V8::AdjustAmountOfExternalAllocatedMemory(int change_in_bytes) {
5140 i::Isolate* isolate = i::Isolate::Current();
5141 if (IsDeadCheck(isolate, "v8::V8::AdjustAmountOfExternalAllocatedMemory()")) {
5144 return isolate->heap()->AdjustAmountOfExternalAllocatedMemory(
5149 void V8::SetGlobalGCPrologueCallback(GCCallback callback) {
5150 i::Isolate* isolate = i::Isolate::Current();
5151 if (IsDeadCheck(isolate, "v8::V8::SetGlobalGCPrologueCallback()")) return;
5152 isolate->heap()->SetGlobalGCPrologueCallback(callback);
5156 void V8::SetGlobalGCEpilogueCallback(GCCallback callback) {
5157 i::Isolate* isolate = i::Isolate::Current();
5158 if (IsDeadCheck(isolate, "v8::V8::SetGlobalGCEpilogueCallback()")) return;
5159 isolate->heap()->SetGlobalGCEpilogueCallback(callback);
5163 void V8::AddGCPrologueCallback(GCPrologueCallback callback, GCType gc_type) {
5164 i::Isolate* isolate = i::Isolate::Current();
5165 if (IsDeadCheck(isolate, "v8::V8::AddGCPrologueCallback()")) return;
5166 isolate->heap()->AddGCPrologueCallback(callback, gc_type);
5170 void V8::RemoveGCPrologueCallback(GCPrologueCallback callback) {
5171 i::Isolate* isolate = i::Isolate::Current();
5172 if (IsDeadCheck(isolate, "v8::V8::RemoveGCPrologueCallback()")) return;
5173 isolate->heap()->RemoveGCPrologueCallback(callback);
5177 void V8::AddGCEpilogueCallback(GCEpilogueCallback callback, GCType gc_type) {
5178 i::Isolate* isolate = i::Isolate::Current();
5179 if (IsDeadCheck(isolate, "v8::V8::AddGCEpilogueCallback()")) return;
5180 isolate->heap()->AddGCEpilogueCallback(callback, gc_type);
5184 void V8::RemoveGCEpilogueCallback(GCEpilogueCallback callback) {
5185 i::Isolate* isolate = i::Isolate::Current();
5186 if (IsDeadCheck(isolate, "v8::V8::RemoveGCEpilogueCallback()")) return;
5187 isolate->heap()->RemoveGCEpilogueCallback(callback);
5191 void V8::AddMemoryAllocationCallback(MemoryAllocationCallback callback,
5193 AllocationAction action) {
5194 i::Isolate* isolate = i::Isolate::Current();
5195 if (IsDeadCheck(isolate, "v8::V8::AddMemoryAllocationCallback()")) return;
5196 isolate->memory_allocator()->AddMemoryAllocationCallback(
5197 callback, space, action);
5201 void V8::RemoveMemoryAllocationCallback(MemoryAllocationCallback callback) {
5202 i::Isolate* isolate = i::Isolate::Current();
5203 if (IsDeadCheck(isolate, "v8::V8::RemoveMemoryAllocationCallback()")) return;
5204 isolate->memory_allocator()->RemoveMemoryAllocationCallback(
5209 void V8::PauseProfiler() {
5210 i::Isolate* isolate = i::Isolate::Current();
5211 isolate->logger()->PauseProfiler();
5215 void V8::ResumeProfiler() {
5216 i::Isolate* isolate = i::Isolate::Current();
5217 isolate->logger()->ResumeProfiler();
5221 bool V8::IsProfilerPaused() {
5222 i::Isolate* isolate = i::Isolate::Current();
5223 return isolate->logger()->IsProfilerPaused();
5227 int V8::GetCurrentThreadId() {
5228 i::Isolate* isolate = i::Isolate::Current();
5229 EnsureInitializedForIsolate(isolate, "V8::GetCurrentThreadId()");
5230 return isolate->thread_id().ToInteger();
5234 void V8::TerminateExecution(int thread_id) {
5235 i::Isolate* isolate = i::Isolate::Current();
5236 if (!isolate->IsInitialized()) return;
5237 API_ENTRY_CHECK(isolate, "V8::TerminateExecution()");
5238 // If the thread_id identifies the current thread just terminate
5239 // execution right away. Otherwise, ask the thread manager to
5240 // terminate the thread with the given id if any.
5241 i::ThreadId internal_tid = i::ThreadId::FromInteger(thread_id);
5242 if (isolate->thread_id().Equals(internal_tid)) {
5243 isolate->stack_guard()->TerminateExecution();
5245 isolate->thread_manager()->TerminateExecution(internal_tid);
5250 void V8::TerminateExecution(Isolate* isolate) {
5251 // If no isolate is supplied, use the default isolate.
5252 if (isolate != NULL) {
5253 reinterpret_cast<i::Isolate*>(isolate)->stack_guard()->TerminateExecution();
5255 i::Isolate::GetDefaultIsolateStackGuard()->TerminateExecution();
5260 bool V8::IsExecutionTerminating(Isolate* isolate) {
5261 i::Isolate* i_isolate = isolate != NULL ?
5262 reinterpret_cast<i::Isolate*>(isolate) : i::Isolate::Current();
5263 return IsExecutionTerminatingCheck(i_isolate);
5267 Isolate* Isolate::GetCurrent() {
5268 i::Isolate* isolate = i::Isolate::UncheckedCurrent();
5269 return reinterpret_cast<Isolate*>(isolate);
5273 Isolate* Isolate::New() {
5274 i::Isolate* isolate = new i::Isolate();
5275 return reinterpret_cast<Isolate*>(isolate);
5279 void Isolate::Dispose() {
5280 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
5281 if (!ApiCheck(!isolate->IsInUse(),
5282 "v8::Isolate::Dispose()",
5283 "Disposing the isolate that is entered by a thread.")) {
5286 isolate->TearDown();
5290 void Isolate::Enter() {
5291 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
5296 void Isolate::Exit() {
5297 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
5302 void Isolate::SetData(void* data) {
5303 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
5304 isolate->SetData(data);
5307 void* Isolate::GetData() {
5308 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
5309 return isolate->GetData();
5313 String::Utf8Value::Utf8Value(v8::Handle<v8::Value> obj)
5314 : str_(NULL), length_(0) {
5315 i::Isolate* isolate = i::Isolate::Current();
5316 if (IsDeadCheck(isolate, "v8::String::Utf8Value::Utf8Value()")) return;
5317 if (obj.IsEmpty()) return;
5319 i::HandleScope scope(isolate);
5321 Handle<String> str = obj->ToString();
5322 if (str.IsEmpty()) return;
5323 length_ = str->Utf8Length();
5324 str_ = i::NewArray<char>(length_ + 1);
5325 str->WriteUtf8(str_);
5329 String::Utf8Value::~Utf8Value() {
5330 i::DeleteArray(str_);
5334 String::AsciiValue::AsciiValue(v8::Handle<v8::Value> obj)
5335 : str_(NULL), length_(0) {
5336 i::Isolate* isolate = i::Isolate::Current();
5337 if (IsDeadCheck(isolate, "v8::String::AsciiValue::AsciiValue()")) return;
5338 if (obj.IsEmpty()) return;
5340 i::HandleScope scope(isolate);
5342 Handle<String> str = obj->ToString();
5343 if (str.IsEmpty()) return;
5344 length_ = str->Length();
5345 str_ = i::NewArray<char>(length_ + 1);
5346 str->WriteAscii(str_);
5350 String::AsciiValue::~AsciiValue() {
5351 i::DeleteArray(str_);
5355 String::Value::Value(v8::Handle<v8::Value> obj)
5356 : str_(NULL), length_(0) {
5357 i::Isolate* isolate = i::Isolate::Current();
5358 if (IsDeadCheck(isolate, "v8::String::Value::Value()")) return;
5359 if (obj.IsEmpty()) return;
5361 i::HandleScope scope(isolate);
5363 Handle<String> str = obj->ToString();
5364 if (str.IsEmpty()) return;
5365 length_ = str->Length();
5366 str_ = i::NewArray<uint16_t>(length_ + 1);
5371 String::Value::~Value() {
5372 i::DeleteArray(str_);
5375 Local<Value> Exception::RangeError(v8::Handle<v8::String> raw_message) {
5376 i::Isolate* isolate = i::Isolate::Current();
5377 LOG_API(isolate, "RangeError");
5378 ON_BAILOUT(isolate, "v8::Exception::RangeError()", return Local<Value>());
5382 i::HandleScope scope(isolate);
5383 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
5384 i::Handle<i::Object> result = isolate->factory()->NewRangeError(message);
5387 i::Handle<i::Object> result(error);
5388 return Utils::ToLocal(result);
5391 Local<Value> Exception::ReferenceError(v8::Handle<v8::String> raw_message) {
5392 i::Isolate* isolate = i::Isolate::Current();
5393 LOG_API(isolate, "ReferenceError");
5394 ON_BAILOUT(isolate, "v8::Exception::ReferenceError()", return Local<Value>());
5398 i::HandleScope scope(isolate);
5399 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
5400 i::Handle<i::Object> result =
5401 isolate->factory()->NewReferenceError(message);
5404 i::Handle<i::Object> result(error);
5405 return Utils::ToLocal(result);
5408 Local<Value> Exception::SyntaxError(v8::Handle<v8::String> raw_message) {
5409 i::Isolate* isolate = i::Isolate::Current();
5410 LOG_API(isolate, "SyntaxError");
5411 ON_BAILOUT(isolate, "v8::Exception::SyntaxError()", return Local<Value>());
5415 i::HandleScope scope(isolate);
5416 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
5417 i::Handle<i::Object> result = isolate->factory()->NewSyntaxError(message);
5420 i::Handle<i::Object> result(error);
5421 return Utils::ToLocal(result);
5424 Local<Value> Exception::TypeError(v8::Handle<v8::String> raw_message) {
5425 i::Isolate* isolate = i::Isolate::Current();
5426 LOG_API(isolate, "TypeError");
5427 ON_BAILOUT(isolate, "v8::Exception::TypeError()", return Local<Value>());
5431 i::HandleScope scope(isolate);
5432 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
5433 i::Handle<i::Object> result = isolate->factory()->NewTypeError(message);
5436 i::Handle<i::Object> result(error);
5437 return Utils::ToLocal(result);
5440 Local<Value> Exception::Error(v8::Handle<v8::String> raw_message) {
5441 i::Isolate* isolate = i::Isolate::Current();
5442 LOG_API(isolate, "Error");
5443 ON_BAILOUT(isolate, "v8::Exception::Error()", return Local<Value>());
5447 i::HandleScope scope(isolate);
5448 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
5449 i::Handle<i::Object> result = isolate->factory()->NewError(message);
5452 i::Handle<i::Object> result(error);
5453 return Utils::ToLocal(result);
5457 // --- D e b u g S u p p o r t ---
5459 #ifdef ENABLE_DEBUGGER_SUPPORT
5461 static void EventCallbackWrapper(const v8::Debug::EventDetails& event_details) {
5462 i::Isolate* isolate = i::Isolate::Current();
5463 if (isolate->debug_event_callback() != NULL) {
5464 isolate->debug_event_callback()(event_details.GetEvent(),
5465 event_details.GetExecutionState(),
5466 event_details.GetEventData(),
5467 event_details.GetCallbackData());
5472 bool Debug::SetDebugEventListener(EventCallback that, Handle<Value> data) {
5473 i::Isolate* isolate = i::Isolate::Current();
5474 EnsureInitializedForIsolate(isolate, "v8::Debug::SetDebugEventListener()");
5475 ON_BAILOUT(isolate, "v8::Debug::SetDebugEventListener()", return false);
5478 isolate->set_debug_event_callback(that);
5480 i::HandleScope scope(isolate);
5481 i::Handle<i::Object> foreign = isolate->factory()->undefined_value();
5484 isolate->factory()->NewForeign(FUNCTION_ADDR(EventCallbackWrapper));
5486 isolate->debugger()->SetEventListener(foreign, Utils::OpenHandle(*data));
5491 bool Debug::SetDebugEventListener2(EventCallback2 that, Handle<Value> data) {
5492 i::Isolate* isolate = i::Isolate::Current();
5493 EnsureInitializedForIsolate(isolate, "v8::Debug::SetDebugEventListener2()");
5494 ON_BAILOUT(isolate, "v8::Debug::SetDebugEventListener2()", return false);
5496 i::HandleScope scope(isolate);
5497 i::Handle<i::Object> foreign = isolate->factory()->undefined_value();
5499 foreign = isolate->factory()->NewForeign(FUNCTION_ADDR(that));
5501 isolate->debugger()->SetEventListener(foreign, Utils::OpenHandle(*data));
5506 bool Debug::SetDebugEventListener(v8::Handle<v8::Object> that,
5507 Handle<Value> data) {
5508 i::Isolate* isolate = i::Isolate::Current();
5509 ON_BAILOUT(isolate, "v8::Debug::SetDebugEventListener()", return false);
5511 isolate->debugger()->SetEventListener(Utils::OpenHandle(*that),
5512 Utils::OpenHandle(*data));
5517 void Debug::DebugBreak(Isolate* isolate) {
5518 // If no isolate is supplied, use the default isolate.
5519 if (isolate != NULL) {
5520 reinterpret_cast<i::Isolate*>(isolate)->stack_guard()->DebugBreak();
5522 i::Isolate::GetDefaultIsolateStackGuard()->DebugBreak();
5527 void Debug::CancelDebugBreak(Isolate* isolate) {
5528 // If no isolate is supplied, use the default isolate.
5529 if (isolate != NULL) {
5530 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
5531 internal_isolate->stack_guard()->Continue(i::DEBUGBREAK);
5533 i::Isolate::GetDefaultIsolateStackGuard()->Continue(i::DEBUGBREAK);
5538 void Debug::DebugBreakForCommand(ClientData* data, Isolate* isolate) {
5539 // If no isolate is supplied, use the default isolate.
5540 if (isolate != NULL) {
5541 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
5542 internal_isolate->debugger()->EnqueueDebugCommand(data);
5544 i::Isolate::GetDefaultIsolateDebugger()->EnqueueDebugCommand(data);
5549 static void MessageHandlerWrapper(const v8::Debug::Message& message) {
5550 i::Isolate* isolate = i::Isolate::Current();
5551 if (isolate->message_handler()) {
5552 v8::String::Value json(message.GetJSON());
5553 (isolate->message_handler())(*json, json.length(), message.GetClientData());
5558 void Debug::SetMessageHandler(v8::Debug::MessageHandler handler,
5559 bool message_handler_thread) {
5560 i::Isolate* isolate = i::Isolate::Current();
5561 EnsureInitializedForIsolate(isolate, "v8::Debug::SetMessageHandler");
5564 // Message handler thread not supported any more. Parameter temporally left in
5565 // the API for client compatibility reasons.
5566 CHECK(!message_handler_thread);
5568 // TODO(sgjesse) support the old message handler API through a simple wrapper.
5569 isolate->set_message_handler(handler);
5570 if (handler != NULL) {
5571 isolate->debugger()->SetMessageHandler(MessageHandlerWrapper);
5573 isolate->debugger()->SetMessageHandler(NULL);
5578 void Debug::SetMessageHandler2(v8::Debug::MessageHandler2 handler) {
5579 i::Isolate* isolate = i::Isolate::Current();
5580 EnsureInitializedForIsolate(isolate, "v8::Debug::SetMessageHandler");
5582 isolate->debugger()->SetMessageHandler(handler);
5586 void Debug::SendCommand(const uint16_t* command, int length,
5587 ClientData* client_data,
5589 // If no isolate is supplied, use the default isolate.
5590 if (isolate != NULL) {
5591 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
5592 internal_isolate->debugger()->ProcessCommand(
5593 i::Vector<const uint16_t>(command, length), client_data);
5595 i::Isolate::GetDefaultIsolateDebugger()->ProcessCommand(
5596 i::Vector<const uint16_t>(command, length), client_data);
5601 void Debug::SetHostDispatchHandler(HostDispatchHandler handler,
5603 i::Isolate* isolate = i::Isolate::Current();
5604 EnsureInitializedForIsolate(isolate, "v8::Debug::SetHostDispatchHandler");
5606 isolate->debugger()->SetHostDispatchHandler(handler, period);
5610 void Debug::SetDebugMessageDispatchHandler(
5611 DebugMessageDispatchHandler handler, bool provide_locker) {
5612 i::Isolate* isolate = i::Isolate::Current();
5613 EnsureInitializedForIsolate(isolate,
5614 "v8::Debug::SetDebugMessageDispatchHandler");
5616 isolate->debugger()->SetDebugMessageDispatchHandler(
5617 handler, provide_locker);
5621 Local<Value> Debug::Call(v8::Handle<v8::Function> fun,
5622 v8::Handle<v8::Value> data) {
5623 i::Isolate* isolate = i::Isolate::Current();
5624 if (!isolate->IsInitialized()) return Local<Value>();
5625 ON_BAILOUT(isolate, "v8::Debug::Call()", return Local<Value>());
5627 i::Handle<i::Object> result;
5628 EXCEPTION_PREAMBLE(isolate);
5629 if (data.IsEmpty()) {
5630 result = isolate->debugger()->Call(Utils::OpenHandle(*fun),
5631 isolate->factory()->undefined_value(),
5632 &has_pending_exception);
5634 result = isolate->debugger()->Call(Utils::OpenHandle(*fun),
5635 Utils::OpenHandle(*data),
5636 &has_pending_exception);
5638 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
5639 return Utils::ToLocal(result);
5643 Local<Value> Debug::GetMirror(v8::Handle<v8::Value> obj) {
5644 i::Isolate* isolate = i::Isolate::Current();
5645 if (!isolate->IsInitialized()) return Local<Value>();
5646 ON_BAILOUT(isolate, "v8::Debug::GetMirror()", return Local<Value>());
5648 v8::HandleScope scope;
5649 i::Debug* isolate_debug = isolate->debug();
5650 isolate_debug->Load();
5651 i::Handle<i::JSObject> debug(isolate_debug->debug_context()->global());
5652 i::Handle<i::String> name =
5653 isolate->factory()->LookupAsciiSymbol("MakeMirror");
5654 i::Handle<i::Object> fun_obj = i::GetProperty(debug, name);
5655 i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(fun_obj);
5656 v8::Handle<v8::Function> v8_fun = Utils::ToLocal(fun);
5657 const int kArgc = 1;
5658 v8::Handle<v8::Value> argv[kArgc] = { obj };
5659 EXCEPTION_PREAMBLE(isolate);
5660 v8::Handle<v8::Value> result = v8_fun->Call(Utils::ToLocal(debug),
5663 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
5664 return scope.Close(result);
5668 bool Debug::EnableAgent(const char* name, int port, bool wait_for_connection) {
5669 return i::Isolate::Current()->debugger()->StartAgent(name, port,
5670 wait_for_connection);
5674 void Debug::DisableAgent() {
5675 return i::Isolate::Current()->debugger()->StopAgent();
5679 void Debug::ProcessDebugMessages() {
5680 i::Execution::ProcessDebugMesssages(true);
5683 Local<Context> Debug::GetDebugContext() {
5684 i::Isolate* isolate = i::Isolate::Current();
5685 EnsureInitializedForIsolate(isolate, "v8::Debug::GetDebugContext()");
5687 return Utils::ToLocal(i::Isolate::Current()->debugger()->GetDebugContext());
5690 #endif // ENABLE_DEBUGGER_SUPPORT
5693 Handle<String> CpuProfileNode::GetFunctionName() const {
5694 i::Isolate* isolate = i::Isolate::Current();
5695 IsDeadCheck(isolate, "v8::CpuProfileNode::GetFunctionName");
5696 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
5697 const i::CodeEntry* entry = node->entry();
5698 if (!entry->has_name_prefix()) {
5699 return Handle<String>(ToApi<String>(
5700 isolate->factory()->LookupAsciiSymbol(entry->name())));
5702 return Handle<String>(ToApi<String>(isolate->factory()->NewConsString(
5703 isolate->factory()->LookupAsciiSymbol(entry->name_prefix()),
5704 isolate->factory()->LookupAsciiSymbol(entry->name()))));
5709 Handle<String> CpuProfileNode::GetScriptResourceName() const {
5710 i::Isolate* isolate = i::Isolate::Current();
5711 IsDeadCheck(isolate, "v8::CpuProfileNode::GetScriptResourceName");
5712 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
5713 return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol(
5714 node->entry()->resource_name())));
5718 int CpuProfileNode::GetLineNumber() const {
5719 i::Isolate* isolate = i::Isolate::Current();
5720 IsDeadCheck(isolate, "v8::CpuProfileNode::GetLineNumber");
5721 return reinterpret_cast<const i::ProfileNode*>(this)->entry()->line_number();
5725 double CpuProfileNode::GetTotalTime() const {
5726 i::Isolate* isolate = i::Isolate::Current();
5727 IsDeadCheck(isolate, "v8::CpuProfileNode::GetTotalTime");
5728 return reinterpret_cast<const i::ProfileNode*>(this)->GetTotalMillis();
5732 double CpuProfileNode::GetSelfTime() const {
5733 i::Isolate* isolate = i::Isolate::Current();
5734 IsDeadCheck(isolate, "v8::CpuProfileNode::GetSelfTime");
5735 return reinterpret_cast<const i::ProfileNode*>(this)->GetSelfMillis();
5739 double CpuProfileNode::GetTotalSamplesCount() const {
5740 i::Isolate* isolate = i::Isolate::Current();
5741 IsDeadCheck(isolate, "v8::CpuProfileNode::GetTotalSamplesCount");
5742 return reinterpret_cast<const i::ProfileNode*>(this)->total_ticks();
5746 double CpuProfileNode::GetSelfSamplesCount() const {
5747 i::Isolate* isolate = i::Isolate::Current();
5748 IsDeadCheck(isolate, "v8::CpuProfileNode::GetSelfSamplesCount");
5749 return reinterpret_cast<const i::ProfileNode*>(this)->self_ticks();
5753 unsigned CpuProfileNode::GetCallUid() const {
5754 i::Isolate* isolate = i::Isolate::Current();
5755 IsDeadCheck(isolate, "v8::CpuProfileNode::GetCallUid");
5756 return reinterpret_cast<const i::ProfileNode*>(this)->entry()->GetCallUid();
5760 int CpuProfileNode::GetChildrenCount() const {
5761 i::Isolate* isolate = i::Isolate::Current();
5762 IsDeadCheck(isolate, "v8::CpuProfileNode::GetChildrenCount");
5763 return reinterpret_cast<const i::ProfileNode*>(this)->children()->length();
5767 const CpuProfileNode* CpuProfileNode::GetChild(int index) const {
5768 i::Isolate* isolate = i::Isolate::Current();
5769 IsDeadCheck(isolate, "v8::CpuProfileNode::GetChild");
5770 const i::ProfileNode* child =
5771 reinterpret_cast<const i::ProfileNode*>(this)->children()->at(index);
5772 return reinterpret_cast<const CpuProfileNode*>(child);
5776 void CpuProfile::Delete() {
5777 i::Isolate* isolate = i::Isolate::Current();
5778 IsDeadCheck(isolate, "v8::CpuProfile::Delete");
5779 i::CpuProfiler::DeleteProfile(reinterpret_cast<i::CpuProfile*>(this));
5780 if (i::CpuProfiler::GetProfilesCount() == 0 &&
5781 !i::CpuProfiler::HasDetachedProfiles()) {
5782 // If this was the last profile, clean up all accessory data as well.
5783 i::CpuProfiler::DeleteAllProfiles();
5788 unsigned CpuProfile::GetUid() const {
5789 i::Isolate* isolate = i::Isolate::Current();
5790 IsDeadCheck(isolate, "v8::CpuProfile::GetUid");
5791 return reinterpret_cast<const i::CpuProfile*>(this)->uid();
5795 Handle<String> CpuProfile::GetTitle() const {
5796 i::Isolate* isolate = i::Isolate::Current();
5797 IsDeadCheck(isolate, "v8::CpuProfile::GetTitle");
5798 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
5799 return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol(
5800 profile->title())));
5804 const CpuProfileNode* CpuProfile::GetBottomUpRoot() const {
5805 i::Isolate* isolate = i::Isolate::Current();
5806 IsDeadCheck(isolate, "v8::CpuProfile::GetBottomUpRoot");
5807 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
5808 return reinterpret_cast<const CpuProfileNode*>(profile->bottom_up()->root());
5812 const CpuProfileNode* CpuProfile::GetTopDownRoot() const {
5813 i::Isolate* isolate = i::Isolate::Current();
5814 IsDeadCheck(isolate, "v8::CpuProfile::GetTopDownRoot");
5815 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
5816 return reinterpret_cast<const CpuProfileNode*>(profile->top_down()->root());
5820 int CpuProfiler::GetProfilesCount() {
5821 i::Isolate* isolate = i::Isolate::Current();
5822 IsDeadCheck(isolate, "v8::CpuProfiler::GetProfilesCount");
5823 return i::CpuProfiler::GetProfilesCount();
5827 const CpuProfile* CpuProfiler::GetProfile(int index,
5828 Handle<Value> security_token) {
5829 i::Isolate* isolate = i::Isolate::Current();
5830 IsDeadCheck(isolate, "v8::CpuProfiler::GetProfile");
5831 return reinterpret_cast<const CpuProfile*>(
5832 i::CpuProfiler::GetProfile(
5833 security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
5838 const CpuProfile* CpuProfiler::FindProfile(unsigned uid,
5839 Handle<Value> security_token) {
5840 i::Isolate* isolate = i::Isolate::Current();
5841 IsDeadCheck(isolate, "v8::CpuProfiler::FindProfile");
5842 return reinterpret_cast<const CpuProfile*>(
5843 i::CpuProfiler::FindProfile(
5844 security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
5849 void CpuProfiler::StartProfiling(Handle<String> title) {
5850 i::Isolate* isolate = i::Isolate::Current();
5851 IsDeadCheck(isolate, "v8::CpuProfiler::StartProfiling");
5852 i::CpuProfiler::StartProfiling(*Utils::OpenHandle(*title));
5856 const CpuProfile* CpuProfiler::StopProfiling(Handle<String> title,
5857 Handle<Value> security_token) {
5858 i::Isolate* isolate = i::Isolate::Current();
5859 IsDeadCheck(isolate, "v8::CpuProfiler::StopProfiling");
5860 return reinterpret_cast<const CpuProfile*>(
5861 i::CpuProfiler::StopProfiling(
5862 security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
5863 *Utils::OpenHandle(*title)));
5867 void CpuProfiler::DeleteAllProfiles() {
5868 i::Isolate* isolate = i::Isolate::Current();
5869 IsDeadCheck(isolate, "v8::CpuProfiler::DeleteAllProfiles");
5870 i::CpuProfiler::DeleteAllProfiles();
5874 static i::HeapGraphEdge* ToInternal(const HeapGraphEdge* edge) {
5875 return const_cast<i::HeapGraphEdge*>(
5876 reinterpret_cast<const i::HeapGraphEdge*>(edge));
5880 HeapGraphEdge::Type HeapGraphEdge::GetType() const {
5881 i::Isolate* isolate = i::Isolate::Current();
5882 IsDeadCheck(isolate, "v8::HeapGraphEdge::GetType");
5883 return static_cast<HeapGraphEdge::Type>(ToInternal(this)->type());
5887 Handle<Value> HeapGraphEdge::GetName() const {
5888 i::Isolate* isolate = i::Isolate::Current();
5889 IsDeadCheck(isolate, "v8::HeapGraphEdge::GetName");
5890 i::HeapGraphEdge* edge = ToInternal(this);
5891 switch (edge->type()) {
5892 case i::HeapGraphEdge::kContextVariable:
5893 case i::HeapGraphEdge::kInternal:
5894 case i::HeapGraphEdge::kProperty:
5895 case i::HeapGraphEdge::kShortcut:
5896 return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol(
5898 case i::HeapGraphEdge::kElement:
5899 case i::HeapGraphEdge::kHidden:
5900 return Handle<Number>(ToApi<Number>(isolate->factory()->NewNumberFromInt(
5902 default: UNREACHABLE();
5904 return v8::Undefined();
5908 const HeapGraphNode* HeapGraphEdge::GetFromNode() const {
5909 i::Isolate* isolate = i::Isolate::Current();
5910 IsDeadCheck(isolate, "v8::HeapGraphEdge::GetFromNode");
5911 const i::HeapEntry* from = ToInternal(this)->From();
5912 return reinterpret_cast<const HeapGraphNode*>(from);
5916 const HeapGraphNode* HeapGraphEdge::GetToNode() const {
5917 i::Isolate* isolate = i::Isolate::Current();
5918 IsDeadCheck(isolate, "v8::HeapGraphEdge::GetToNode");
5919 const i::HeapEntry* to = ToInternal(this)->to();
5920 return reinterpret_cast<const HeapGraphNode*>(to);
5924 static i::HeapEntry* ToInternal(const HeapGraphNode* entry) {
5925 return const_cast<i::HeapEntry*>(
5926 reinterpret_cast<const i::HeapEntry*>(entry));
5930 HeapGraphNode::Type HeapGraphNode::GetType() const {
5931 i::Isolate* isolate = i::Isolate::Current();
5932 IsDeadCheck(isolate, "v8::HeapGraphNode::GetType");
5933 return static_cast<HeapGraphNode::Type>(ToInternal(this)->type());
5937 Handle<String> HeapGraphNode::GetName() const {
5938 i::Isolate* isolate = i::Isolate::Current();
5939 IsDeadCheck(isolate, "v8::HeapGraphNode::GetName");
5940 return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol(
5941 ToInternal(this)->name())));
5945 uint64_t HeapGraphNode::GetId() const {
5946 i::Isolate* isolate = i::Isolate::Current();
5947 IsDeadCheck(isolate, "v8::HeapGraphNode::GetId");
5948 return ToInternal(this)->id();
5952 int HeapGraphNode::GetSelfSize() const {
5953 i::Isolate* isolate = i::Isolate::Current();
5954 IsDeadCheck(isolate, "v8::HeapGraphNode::GetSelfSize");
5955 return ToInternal(this)->self_size();
5959 int HeapGraphNode::GetRetainedSize(bool exact) const {
5960 i::Isolate* isolate = i::Isolate::Current();
5961 IsDeadCheck(isolate, "v8::HeapSnapshot::GetRetainedSize");
5962 return ToInternal(this)->RetainedSize(exact);
5966 int HeapGraphNode::GetChildrenCount() const {
5967 i::Isolate* isolate = i::Isolate::Current();
5968 IsDeadCheck(isolate, "v8::HeapSnapshot::GetChildrenCount");
5969 return ToInternal(this)->children().length();
5973 const HeapGraphEdge* HeapGraphNode::GetChild(int index) const {
5974 i::Isolate* isolate = i::Isolate::Current();
5975 IsDeadCheck(isolate, "v8::HeapSnapshot::GetChild");
5976 return reinterpret_cast<const HeapGraphEdge*>(
5977 &ToInternal(this)->children()[index]);
5981 int HeapGraphNode::GetRetainersCount() const {
5982 i::Isolate* isolate = i::Isolate::Current();
5983 IsDeadCheck(isolate, "v8::HeapSnapshot::GetRetainersCount");
5984 return ToInternal(this)->retainers().length();
5988 const HeapGraphEdge* HeapGraphNode::GetRetainer(int index) const {
5989 i::Isolate* isolate = i::Isolate::Current();
5990 IsDeadCheck(isolate, "v8::HeapSnapshot::GetRetainer");
5991 return reinterpret_cast<const HeapGraphEdge*>(
5992 ToInternal(this)->retainers()[index]);
5996 const HeapGraphNode* HeapGraphNode::GetDominatorNode() const {
5997 i::Isolate* isolate = i::Isolate::Current();
5998 IsDeadCheck(isolate, "v8::HeapSnapshot::GetDominatorNode");
5999 return reinterpret_cast<const HeapGraphNode*>(ToInternal(this)->dominator());
6003 v8::Handle<v8::Value> HeapGraphNode::GetHeapValue() const {
6004 i::Isolate* isolate = i::Isolate::Current();
6005 IsDeadCheck(isolate, "v8::HeapGraphNode::GetHeapValue");
6006 i::Handle<i::HeapObject> object = ToInternal(this)->GetHeapObject();
6007 return v8::Handle<Value>(!object.is_null() ?
6008 ToApi<Value>(object) : ToApi<Value>(
6009 isolate->factory()->undefined_value()));
6013 static i::HeapSnapshot* ToInternal(const HeapSnapshot* snapshot) {
6014 return const_cast<i::HeapSnapshot*>(
6015 reinterpret_cast<const i::HeapSnapshot*>(snapshot));
6019 void HeapSnapshot::Delete() {
6020 i::Isolate* isolate = i::Isolate::Current();
6021 IsDeadCheck(isolate, "v8::HeapSnapshot::Delete");
6022 if (i::HeapProfiler::GetSnapshotsCount() > 1) {
6023 ToInternal(this)->Delete();
6025 // If this is the last snapshot, clean up all accessory data as well.
6026 i::HeapProfiler::DeleteAllSnapshots();
6031 HeapSnapshot::Type HeapSnapshot::GetType() const {
6032 i::Isolate* isolate = i::Isolate::Current();
6033 IsDeadCheck(isolate, "v8::HeapSnapshot::GetType");
6034 return static_cast<HeapSnapshot::Type>(ToInternal(this)->type());
6038 unsigned HeapSnapshot::GetUid() const {
6039 i::Isolate* isolate = i::Isolate::Current();
6040 IsDeadCheck(isolate, "v8::HeapSnapshot::GetUid");
6041 return ToInternal(this)->uid();
6045 Handle<String> HeapSnapshot::GetTitle() const {
6046 i::Isolate* isolate = i::Isolate::Current();
6047 IsDeadCheck(isolate, "v8::HeapSnapshot::GetTitle");
6048 return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol(
6049 ToInternal(this)->title())));
6053 const HeapGraphNode* HeapSnapshot::GetRoot() const {
6054 i::Isolate* isolate = i::Isolate::Current();
6055 IsDeadCheck(isolate, "v8::HeapSnapshot::GetHead");
6056 return reinterpret_cast<const HeapGraphNode*>(ToInternal(this)->root());
6060 const HeapGraphNode* HeapSnapshot::GetNodeById(uint64_t id) const {
6061 i::Isolate* isolate = i::Isolate::Current();
6062 IsDeadCheck(isolate, "v8::HeapSnapshot::GetNodeById");
6063 return reinterpret_cast<const HeapGraphNode*>(
6064 ToInternal(this)->GetEntryById(id));
6068 int HeapSnapshot::GetNodesCount() const {
6069 i::Isolate* isolate = i::Isolate::Current();
6070 IsDeadCheck(isolate, "v8::HeapSnapshot::GetNodesCount");
6071 return ToInternal(this)->entries()->length();
6075 const HeapGraphNode* HeapSnapshot::GetNode(int index) const {
6076 i::Isolate* isolate = i::Isolate::Current();
6077 IsDeadCheck(isolate, "v8::HeapSnapshot::GetNode");
6078 return reinterpret_cast<const HeapGraphNode*>(
6079 ToInternal(this)->entries()->at(index));
6083 void HeapSnapshot::Serialize(OutputStream* stream,
6084 HeapSnapshot::SerializationFormat format) const {
6085 i::Isolate* isolate = i::Isolate::Current();
6086 IsDeadCheck(isolate, "v8::HeapSnapshot::Serialize");
6087 ApiCheck(format == kJSON,
6088 "v8::HeapSnapshot::Serialize",
6089 "Unknown serialization format");
6090 ApiCheck(stream->GetOutputEncoding() == OutputStream::kAscii,
6091 "v8::HeapSnapshot::Serialize",
6092 "Unsupported output encoding");
6093 ApiCheck(stream->GetChunkSize() > 0,
6094 "v8::HeapSnapshot::Serialize",
6095 "Invalid stream chunk size");
6096 i::HeapSnapshotJSONSerializer serializer(ToInternal(this));
6097 serializer.Serialize(stream);
6101 int HeapProfiler::GetSnapshotsCount() {
6102 i::Isolate* isolate = i::Isolate::Current();
6103 IsDeadCheck(isolate, "v8::HeapProfiler::GetSnapshotsCount");
6104 return i::HeapProfiler::GetSnapshotsCount();
6108 const HeapSnapshot* HeapProfiler::GetSnapshot(int index) {
6109 i::Isolate* isolate = i::Isolate::Current();
6110 IsDeadCheck(isolate, "v8::HeapProfiler::GetSnapshot");
6111 return reinterpret_cast<const HeapSnapshot*>(
6112 i::HeapProfiler::GetSnapshot(index));
6116 const HeapSnapshot* HeapProfiler::FindSnapshot(unsigned uid) {
6117 i::Isolate* isolate = i::Isolate::Current();
6118 IsDeadCheck(isolate, "v8::HeapProfiler::FindSnapshot");
6119 return reinterpret_cast<const HeapSnapshot*>(
6120 i::HeapProfiler::FindSnapshot(uid));
6124 const HeapSnapshot* HeapProfiler::TakeSnapshot(Handle<String> title,
6125 HeapSnapshot::Type type,
6126 ActivityControl* control) {
6127 i::Isolate* isolate = i::Isolate::Current();
6128 IsDeadCheck(isolate, "v8::HeapProfiler::TakeSnapshot");
6129 i::HeapSnapshot::Type internal_type = i::HeapSnapshot::kFull;
6131 case HeapSnapshot::kFull:
6132 internal_type = i::HeapSnapshot::kFull;
6137 return reinterpret_cast<const HeapSnapshot*>(
6138 i::HeapProfiler::TakeSnapshot(
6139 *Utils::OpenHandle(*title), internal_type, control));
6143 void HeapProfiler::DeleteAllSnapshots() {
6144 i::Isolate* isolate = i::Isolate::Current();
6145 IsDeadCheck(isolate, "v8::HeapProfiler::DeleteAllSnapshots");
6146 i::HeapProfiler::DeleteAllSnapshots();
6150 void HeapProfiler::DefineWrapperClass(uint16_t class_id,
6151 WrapperInfoCallback callback) {
6152 i::Isolate::Current()->heap_profiler()->DefineWrapperClass(class_id,
6158 v8::Testing::StressType internal::Testing::stress_type_ =
6159 v8::Testing::kStressTypeOpt;
6162 void Testing::SetStressRunType(Testing::StressType type) {
6163 internal::Testing::set_stress_type(type);
6166 int Testing::GetStressRuns() {
6167 if (internal::FLAG_stress_runs != 0) return internal::FLAG_stress_runs;
6169 // In debug mode the code runs much slower so stressing will only make two
6178 static void SetFlagsFromString(const char* flags) {
6179 V8::SetFlagsFromString(flags, i::StrLength(flags));
6183 void Testing::PrepareStressRun(int run) {
6184 static const char* kLazyOptimizations =
6185 "--prepare-always-opt --nolimit-inlining "
6186 "--noalways-opt --noopt-eagerly";
6187 static const char* kEagerOptimizations = "--opt-eagerly";
6188 static const char* kForcedOptimizations = "--always-opt";
6190 // If deoptimization stressed turn on frequent deoptimization. If no value
6191 // is spefified through --deopt-every-n-times use a default default value.
6192 static const char* kDeoptEvery13Times = "--deopt-every-n-times=13";
6193 if (internal::Testing::stress_type() == Testing::kStressTypeDeopt &&
6194 internal::FLAG_deopt_every_n_times == 0) {
6195 SetFlagsFromString(kDeoptEvery13Times);
6199 // As stressing in debug mode only make two runs skip the deopt stressing
6201 if (run == GetStressRuns() - 1) {
6202 SetFlagsFromString(kForcedOptimizations);
6204 SetFlagsFromString(kEagerOptimizations);
6205 SetFlagsFromString(kLazyOptimizations);
6208 if (run == GetStressRuns() - 1) {
6209 SetFlagsFromString(kForcedOptimizations);
6210 } else if (run == GetStressRuns() - 2) {
6211 SetFlagsFromString(kEagerOptimizations);
6213 SetFlagsFromString(kLazyOptimizations);
6219 void Testing::DeoptimizeAll() {
6220 internal::Deoptimizer::DeoptimizeAll();
6224 namespace internal {
6227 void HandleScopeImplementer::FreeThreadResources() {
6232 char* HandleScopeImplementer::ArchiveThread(char* storage) {
6233 v8::ImplementationUtilities::HandleScopeData* current =
6234 isolate_->handle_scope_data();
6235 handle_scope_data_ = *current;
6236 memcpy(storage, this, sizeof(*this));
6238 ResetAfterArchive();
6239 current->Initialize();
6241 return storage + ArchiveSpacePerThread();
6245 int HandleScopeImplementer::ArchiveSpacePerThread() {
6246 return sizeof(HandleScopeImplementer);
6250 char* HandleScopeImplementer::RestoreThread(char* storage) {
6251 memcpy(this, storage, sizeof(*this));
6252 *isolate_->handle_scope_data() = handle_scope_data_;
6253 return storage + ArchiveSpacePerThread();
6257 void HandleScopeImplementer::IterateThis(ObjectVisitor* v) {
6258 // Iterate over all handles in the blocks except for the last.
6259 for (int i = blocks()->length() - 2; i >= 0; --i) {
6260 Object** block = blocks()->at(i);
6261 v->VisitPointers(block, &block[kHandleBlockSize]);
6264 // Iterate over live handles in the last block (if any).
6265 if (!blocks()->is_empty()) {
6266 v->VisitPointers(blocks()->last(), handle_scope_data_.next);
6269 if (!saved_contexts_.is_empty()) {
6270 Object** start = reinterpret_cast<Object**>(&saved_contexts_.first());
6271 v->VisitPointers(start, start + saved_contexts_.length());
6276 void HandleScopeImplementer::Iterate(ObjectVisitor* v) {
6277 v8::ImplementationUtilities::HandleScopeData* current =
6278 isolate_->handle_scope_data();
6279 handle_scope_data_ = *current;
6284 char* HandleScopeImplementer::Iterate(ObjectVisitor* v, char* storage) {
6285 HandleScopeImplementer* scope_implementer =
6286 reinterpret_cast<HandleScopeImplementer*>(storage);
6287 scope_implementer->IterateThis(v);
6288 return storage + ArchiveSpacePerThread();
6291 } } // namespace v8::internal