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>();
4374 v8::Local<v8::Object> Context::Global() {
4375 if (IsDeadCheck(i::Isolate::Current(), "v8::Context::Global()")) {
4376 return Local<v8::Object>();
4378 i::Object** ctx = reinterpret_cast<i::Object**>(this);
4379 i::Handle<i::Context> context =
4380 i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx));
4381 i::Handle<i::Object> global(context->global_proxy());
4382 return Utils::ToLocal(i::Handle<i::JSObject>::cast(global));
4386 void Context::DetachGlobal() {
4387 i::Isolate* isolate = i::Isolate::Current();
4388 if (IsDeadCheck(isolate, "v8::Context::DetachGlobal()")) return;
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 isolate->bootstrapper()->DetachGlobal(context);
4397 void Context::ReattachGlobal(Handle<Object> global_object) {
4398 i::Isolate* isolate = i::Isolate::Current();
4399 if (IsDeadCheck(isolate, "v8::Context::ReattachGlobal()")) return;
4401 i::Object** ctx = reinterpret_cast<i::Object**>(this);
4402 i::Handle<i::Context> context =
4403 i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx));
4404 isolate->bootstrapper()->ReattachGlobal(
4406 Utils::OpenHandle(*global_object));
4410 void Context::AllowCodeGenerationFromStrings(bool allow) {
4411 i::Isolate* isolate = i::Isolate::Current();
4412 if (IsDeadCheck(isolate, "v8::Context::AllowCodeGenerationFromStrings()")) {
4416 i::Object** ctx = reinterpret_cast<i::Object**>(this);
4417 i::Handle<i::Context> context =
4418 i::Handle<i::Context>::cast(i::Handle<i::Object>(ctx));
4419 context->set_allow_code_gen_from_strings(
4420 allow ? isolate->heap()->true_value() : isolate->heap()->false_value());
4424 void V8::SetWrapperClassId(i::Object** global_handle, uint16_t class_id) {
4425 i::GlobalHandles::SetWrapperClassId(global_handle, class_id);
4429 Local<v8::Object> ObjectTemplate::NewInstance() {
4430 i::Isolate* isolate = i::Isolate::Current();
4431 ON_BAILOUT(isolate, "v8::ObjectTemplate::NewInstance()",
4432 return Local<v8::Object>());
4433 LOG_API(isolate, "ObjectTemplate::NewInstance");
4435 EXCEPTION_PREAMBLE(isolate);
4436 i::Handle<i::Object> obj =
4437 i::Execution::InstantiateObject(Utils::OpenHandle(this),
4438 &has_pending_exception);
4439 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>());
4440 return Utils::ToLocal(i::Handle<i::JSObject>::cast(obj));
4444 Local<v8::Function> FunctionTemplate::GetFunction() {
4445 i::Isolate* isolate = i::Isolate::Current();
4446 ON_BAILOUT(isolate, "v8::FunctionTemplate::GetFunction()",
4447 return Local<v8::Function>());
4448 LOG_API(isolate, "FunctionTemplate::GetFunction");
4450 EXCEPTION_PREAMBLE(isolate);
4451 i::Handle<i::Object> obj =
4452 i::Execution::InstantiateFunction(Utils::OpenHandle(this),
4453 &has_pending_exception);
4454 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Function>());
4455 return Utils::ToLocal(i::Handle<i::JSFunction>::cast(obj));
4459 bool FunctionTemplate::HasInstance(v8::Handle<v8::Value> value) {
4460 ON_BAILOUT(i::Isolate::Current(), "v8::FunctionTemplate::HasInstanceOf()",
4462 i::Object* obj = *Utils::OpenHandle(*value);
4463 return obj->IsInstanceOf(*Utils::OpenHandle(this));
4467 static Local<External> ExternalNewImpl(void* data) {
4468 return Utils::ToLocal(FACTORY->NewForeign(static_cast<i::Address>(data)));
4471 static void* ExternalValueImpl(i::Handle<i::Object> obj) {
4472 return reinterpret_cast<void*>(i::Foreign::cast(*obj)->address());
4476 Local<Value> v8::External::Wrap(void* data) {
4477 i::Isolate* isolate = i::Isolate::Current();
4478 STATIC_ASSERT(sizeof(data) == sizeof(i::Address));
4479 EnsureInitializedForIsolate(isolate, "v8::External::Wrap()");
4480 LOG_API(isolate, "External::Wrap");
4483 v8::Local<v8::Value> result = CanBeEncodedAsSmi(data)
4484 ? Utils::ToLocal(i::Handle<i::Object>(EncodeAsSmi(data)))
4485 : v8::Local<v8::Value>(ExternalNewImpl(data));
4487 ASSERT_EQ(data, Unwrap(result));
4492 void* v8::Object::SlowGetPointerFromInternalField(int index) {
4493 i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
4494 i::Object* value = obj->GetInternalField(index);
4495 if (value->IsSmi()) {
4496 return i::Internals::GetExternalPointerFromSmi(value);
4497 } else if (value->IsForeign()) {
4498 return reinterpret_cast<void*>(i::Foreign::cast(value)->address());
4505 void* v8::External::FullUnwrap(v8::Handle<v8::Value> wrapper) {
4506 if (IsDeadCheck(i::Isolate::Current(), "v8::External::Unwrap()")) return 0;
4507 i::Handle<i::Object> obj = Utils::OpenHandle(*wrapper);
4510 result = i::Internals::GetExternalPointerFromSmi(*obj);
4511 } else if (obj->IsForeign()) {
4512 result = ExternalValueImpl(obj);
4516 ASSERT_EQ(result, QuickUnwrap(wrapper));
4521 Local<External> v8::External::New(void* data) {
4522 STATIC_ASSERT(sizeof(data) == sizeof(i::Address));
4523 i::Isolate* isolate = i::Isolate::Current();
4524 EnsureInitializedForIsolate(isolate, "v8::External::New()");
4525 LOG_API(isolate, "External::New");
4527 return ExternalNewImpl(data);
4531 void* External::Value() const {
4532 if (IsDeadCheck(i::Isolate::Current(), "v8::External::Value()")) return 0;
4533 i::Handle<i::Object> obj = Utils::OpenHandle(this);
4534 return ExternalValueImpl(obj);
4538 Local<String> v8::String::Empty() {
4539 i::Isolate* isolate = i::Isolate::Current();
4540 EnsureInitializedForIsolate(isolate, "v8::String::Empty()");
4541 LOG_API(isolate, "String::Empty()");
4542 return Utils::ToLocal(isolate->factory()->empty_symbol());
4546 Local<String> v8::String::New(const char* data, int length) {
4547 i::Isolate* isolate = i::Isolate::Current();
4548 EnsureInitializedForIsolate(isolate, "v8::String::New()");
4549 LOG_API(isolate, "String::New(char)");
4550 if (length == 0) return Empty();
4552 if (length == -1) length = i::StrLength(data);
4553 i::Handle<i::String> result =
4554 isolate->factory()->NewStringFromUtf8(
4555 i::Vector<const char>(data, length));
4556 return Utils::ToLocal(result);
4560 Local<String> v8::String::Concat(Handle<String> left, Handle<String> right) {
4561 i::Handle<i::String> left_string = Utils::OpenHandle(*left);
4562 i::Isolate* isolate = left_string->GetIsolate();
4563 EnsureInitializedForIsolate(isolate, "v8::String::New()");
4564 LOG_API(isolate, "String::New(char)");
4566 i::Handle<i::String> right_string = Utils::OpenHandle(*right);
4567 i::Handle<i::String> result = isolate->factory()->NewConsString(left_string,
4569 return Utils::ToLocal(result);
4573 Local<String> v8::String::NewUndetectable(const char* data, int length) {
4574 i::Isolate* isolate = i::Isolate::Current();
4575 EnsureInitializedForIsolate(isolate, "v8::String::NewUndetectable()");
4576 LOG_API(isolate, "String::NewUndetectable(char)");
4578 if (length == -1) length = i::StrLength(data);
4579 i::Handle<i::String> result =
4580 isolate->factory()->NewStringFromUtf8(
4581 i::Vector<const char>(data, length));
4582 result->MarkAsUndetectable();
4583 return Utils::ToLocal(result);
4587 static int TwoByteStringLength(const uint16_t* data) {
4589 while (data[length] != '\0') length++;
4594 Local<String> v8::String::New(const uint16_t* data, int length) {
4595 i::Isolate* isolate = i::Isolate::Current();
4596 EnsureInitializedForIsolate(isolate, "v8::String::New()");
4597 LOG_API(isolate, "String::New(uint16_)");
4598 if (length == 0) return Empty();
4600 if (length == -1) length = TwoByteStringLength(data);
4601 i::Handle<i::String> result =
4602 isolate->factory()->NewStringFromTwoByte(
4603 i::Vector<const uint16_t>(data, length));
4604 return Utils::ToLocal(result);
4608 Local<String> v8::String::NewUndetectable(const uint16_t* data, int length) {
4609 i::Isolate* isolate = i::Isolate::Current();
4610 EnsureInitializedForIsolate(isolate, "v8::String::NewUndetectable()");
4611 LOG_API(isolate, "String::NewUndetectable(uint16_)");
4613 if (length == -1) length = TwoByteStringLength(data);
4614 i::Handle<i::String> result =
4615 isolate->factory()->NewStringFromTwoByte(
4616 i::Vector<const uint16_t>(data, length));
4617 result->MarkAsUndetectable();
4618 return Utils::ToLocal(result);
4622 i::Handle<i::String> NewExternalStringHandle(i::Isolate* isolate,
4623 v8::String::ExternalStringResource* resource) {
4624 i::Handle<i::String> result =
4625 isolate->factory()->NewExternalStringFromTwoByte(resource);
4630 i::Handle<i::String> NewExternalAsciiStringHandle(i::Isolate* isolate,
4631 v8::String::ExternalAsciiStringResource* resource) {
4632 i::Handle<i::String> result =
4633 isolate->factory()->NewExternalStringFromAscii(resource);
4638 Local<String> v8::String::NewExternal(
4639 v8::String::ExternalStringResource* resource) {
4640 i::Isolate* isolate = i::Isolate::Current();
4641 EnsureInitializedForIsolate(isolate, "v8::String::NewExternal()");
4642 LOG_API(isolate, "String::NewExternal");
4644 i::Handle<i::String> result = NewExternalStringHandle(isolate, resource);
4645 isolate->heap()->external_string_table()->AddString(*result);
4646 return Utils::ToLocal(result);
4650 bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) {
4651 i::Handle<i::String> obj = Utils::OpenHandle(this);
4652 i::Isolate* isolate = obj->GetIsolate();
4653 if (IsDeadCheck(isolate, "v8::String::MakeExternal()")) return false;
4654 if (i::StringShape(*obj).IsExternalTwoByte()) {
4655 return false; // Already an external string.
4658 if (isolate->string_tracker()->IsFreshUnusedString(obj)) {
4661 if (isolate->heap()->IsInGCPostProcessing()) {
4664 bool result = obj->MakeExternal(resource);
4665 if (result && !obj->IsSymbol()) {
4666 isolate->heap()->external_string_table()->AddString(*obj);
4672 Local<String> v8::String::NewExternal(
4673 v8::String::ExternalAsciiStringResource* resource) {
4674 i::Isolate* isolate = i::Isolate::Current();
4675 EnsureInitializedForIsolate(isolate, "v8::String::NewExternal()");
4676 LOG_API(isolate, "String::NewExternal");
4678 i::Handle<i::String> result = NewExternalAsciiStringHandle(isolate, resource);
4679 isolate->heap()->external_string_table()->AddString(*result);
4680 return Utils::ToLocal(result);
4684 bool v8::String::MakeExternal(
4685 v8::String::ExternalAsciiStringResource* resource) {
4686 i::Handle<i::String> obj = Utils::OpenHandle(this);
4687 i::Isolate* isolate = obj->GetIsolate();
4688 if (IsDeadCheck(isolate, "v8::String::MakeExternal()")) return false;
4689 if (i::StringShape(*obj).IsExternalTwoByte()) {
4690 return false; // Already an external string.
4693 if (isolate->string_tracker()->IsFreshUnusedString(obj)) {
4696 if (isolate->heap()->IsInGCPostProcessing()) {
4699 bool result = obj->MakeExternal(resource);
4700 if (result && !obj->IsSymbol()) {
4701 isolate->heap()->external_string_table()->AddString(*obj);
4707 bool v8::String::CanMakeExternal() {
4708 if (!internal::FLAG_clever_optimizations) return false;
4709 i::Handle<i::String> obj = Utils::OpenHandle(this);
4710 i::Isolate* isolate = obj->GetIsolate();
4711 if (IsDeadCheck(isolate, "v8::String::CanMakeExternal()")) return false;
4712 if (isolate->string_tracker()->IsFreshUnusedString(obj)) {
4715 int size = obj->Size(); // Byte size of the original string.
4716 if (size < i::ExternalString::kSize)
4718 i::StringShape shape(*obj);
4719 return !shape.IsExternal();
4723 Local<v8::Object> v8::Object::New() {
4724 i::Isolate* isolate = i::Isolate::Current();
4725 EnsureInitializedForIsolate(isolate, "v8::Object::New()");
4726 LOG_API(isolate, "Object::New");
4728 i::Handle<i::JSObject> obj =
4729 isolate->factory()->NewJSObject(isolate->object_function());
4730 return Utils::ToLocal(obj);
4734 Local<v8::Value> v8::NumberObject::New(double value) {
4735 i::Isolate* isolate = i::Isolate::Current();
4736 EnsureInitializedForIsolate(isolate, "v8::NumberObject::New()");
4737 LOG_API(isolate, "NumberObject::New");
4739 i::Handle<i::Object> number = isolate->factory()->NewNumber(value);
4740 i::Handle<i::Object> obj = isolate->factory()->ToObject(number);
4741 return Utils::ToLocal(obj);
4745 double v8::NumberObject::NumberValue() const {
4746 i::Isolate* isolate = i::Isolate::Current();
4747 if (IsDeadCheck(isolate, "v8::NumberObject::NumberValue()")) return 0;
4748 LOG_API(isolate, "NumberObject::NumberValue");
4749 i::Handle<i::Object> obj = Utils::OpenHandle(this);
4750 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
4751 return jsvalue->value()->Number();
4755 Local<v8::Value> v8::BooleanObject::New(bool value) {
4756 i::Isolate* isolate = i::Isolate::Current();
4757 EnsureInitializedForIsolate(isolate, "v8::BooleanObject::New()");
4758 LOG_API(isolate, "BooleanObject::New");
4760 i::Handle<i::Object> boolean(value ? isolate->heap()->true_value()
4761 : isolate->heap()->false_value());
4762 i::Handle<i::Object> obj = isolate->factory()->ToObject(boolean);
4763 return Utils::ToLocal(obj);
4767 bool v8::BooleanObject::BooleanValue() const {
4768 i::Isolate* isolate = i::Isolate::Current();
4769 if (IsDeadCheck(isolate, "v8::BooleanObject::BooleanValue()")) return 0;
4770 LOG_API(isolate, "BooleanObject::BooleanValue");
4771 i::Handle<i::Object> obj = Utils::OpenHandle(this);
4772 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
4773 return jsvalue->value()->IsTrue();
4777 Local<v8::Value> v8::StringObject::New(Handle<String> value) {
4778 i::Isolate* isolate = i::Isolate::Current();
4779 EnsureInitializedForIsolate(isolate, "v8::StringObject::New()");
4780 LOG_API(isolate, "StringObject::New");
4782 i::Handle<i::Object> obj =
4783 isolate->factory()->ToObject(Utils::OpenHandle(*value));
4784 return Utils::ToLocal(obj);
4788 Local<v8::String> v8::StringObject::StringValue() const {
4789 i::Isolate* isolate = i::Isolate::Current();
4790 if (IsDeadCheck(isolate, "v8::StringObject::StringValue()")) {
4791 return Local<v8::String>();
4793 LOG_API(isolate, "StringObject::StringValue");
4794 i::Handle<i::Object> obj = Utils::OpenHandle(this);
4795 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
4796 return Utils::ToLocal(
4797 i::Handle<i::String>(i::String::cast(jsvalue->value())));
4801 Local<v8::Value> v8::Date::New(double time) {
4802 i::Isolate* isolate = i::Isolate::Current();
4803 EnsureInitializedForIsolate(isolate, "v8::Date::New()");
4804 LOG_API(isolate, "Date::New");
4806 // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
4807 time = i::OS::nan_value();
4810 EXCEPTION_PREAMBLE(isolate);
4811 i::Handle<i::Object> obj =
4812 i::Execution::NewDate(time, &has_pending_exception);
4813 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Value>());
4814 return Utils::ToLocal(obj);
4818 double v8::Date::NumberValue() const {
4819 i::Isolate* isolate = i::Isolate::Current();
4820 if (IsDeadCheck(isolate, "v8::Date::NumberValue()")) return 0;
4821 LOG_API(isolate, "Date::NumberValue");
4822 i::Handle<i::Object> obj = Utils::OpenHandle(this);
4823 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
4824 return jsvalue->value()->Number();
4828 void v8::Date::DateTimeConfigurationChangeNotification() {
4829 i::Isolate* isolate = i::Isolate::Current();
4830 ON_BAILOUT(isolate, "v8::Date::DateTimeConfigurationChangeNotification()",
4832 LOG_API(isolate, "Date::DateTimeConfigurationChangeNotification");
4835 i::HandleScope scope(isolate);
4836 // Get the function ResetDateCache (defined in date-delay.js).
4837 i::Handle<i::String> func_name_str =
4838 isolate->factory()->LookupAsciiSymbol("ResetDateCache");
4839 i::MaybeObject* result =
4840 isolate->js_builtins_object()->GetProperty(*func_name_str);
4841 i::Object* object_func;
4842 if (!result->ToObject(&object_func)) {
4846 if (object_func->IsJSFunction()) {
4847 i::Handle<i::JSFunction> func =
4848 i::Handle<i::JSFunction>(i::JSFunction::cast(object_func));
4850 // Call ResetDateCache(0 but expect no exceptions:
4851 bool caught_exception = false;
4852 i::Execution::TryCall(func,
4853 isolate->js_builtins_object(),
4861 static i::Handle<i::String> RegExpFlagsToString(RegExp::Flags flags) {
4864 if ((flags & RegExp::kGlobal) != 0) flags_buf[num_flags++] = 'g';
4865 if ((flags & RegExp::kMultiline) != 0) flags_buf[num_flags++] = 'm';
4866 if ((flags & RegExp::kIgnoreCase) != 0) flags_buf[num_flags++] = 'i';
4867 ASSERT(num_flags <= static_cast<int>(ARRAY_SIZE(flags_buf)));
4868 return FACTORY->LookupSymbol(
4869 i::Vector<const char>(flags_buf, num_flags));
4873 Local<v8::RegExp> v8::RegExp::New(Handle<String> pattern,
4875 i::Isolate* isolate = Utils::OpenHandle(*pattern)->GetIsolate();
4876 EnsureInitializedForIsolate(isolate, "v8::RegExp::New()");
4877 LOG_API(isolate, "RegExp::New");
4879 EXCEPTION_PREAMBLE(isolate);
4880 i::Handle<i::JSRegExp> obj = i::Execution::NewJSRegExp(
4881 Utils::OpenHandle(*pattern),
4882 RegExpFlagsToString(flags),
4883 &has_pending_exception);
4884 EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::RegExp>());
4885 return Utils::ToLocal(i::Handle<i::JSRegExp>::cast(obj));
4889 Local<v8::String> v8::RegExp::GetSource() const {
4890 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4891 if (IsDeadCheck(isolate, "v8::RegExp::GetSource()")) {
4892 return Local<v8::String>();
4894 i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
4895 return Utils::ToLocal(i::Handle<i::String>(obj->Pattern()));
4899 // Assert that the static flags cast in GetFlags is valid.
4900 #define REGEXP_FLAG_ASSERT_EQ(api_flag, internal_flag) \
4901 STATIC_ASSERT(static_cast<int>(v8::RegExp::api_flag) == \
4902 static_cast<int>(i::JSRegExp::internal_flag))
4903 REGEXP_FLAG_ASSERT_EQ(kNone, NONE);
4904 REGEXP_FLAG_ASSERT_EQ(kGlobal, GLOBAL);
4905 REGEXP_FLAG_ASSERT_EQ(kIgnoreCase, IGNORE_CASE);
4906 REGEXP_FLAG_ASSERT_EQ(kMultiline, MULTILINE);
4907 #undef REGEXP_FLAG_ASSERT_EQ
4909 v8::RegExp::Flags v8::RegExp::GetFlags() const {
4910 if (IsDeadCheck(i::Isolate::Current(), "v8::RegExp::GetFlags()")) {
4911 return v8::RegExp::kNone;
4913 i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
4914 return static_cast<RegExp::Flags>(obj->GetFlags().value());
4918 Local<v8::Array> v8::Array::New(int length) {
4919 i::Isolate* isolate = i::Isolate::Current();
4920 EnsureInitializedForIsolate(isolate, "v8::Array::New()");
4921 LOG_API(isolate, "Array::New");
4923 int real_length = length > 0 ? length : 0;
4924 i::Handle<i::JSArray> obj = isolate->factory()->NewJSArray(real_length);
4925 i::Handle<i::Object> length_obj =
4926 isolate->factory()->NewNumberFromInt(real_length);
4927 obj->set_length(*length_obj);
4928 return Utils::ToLocal(obj);
4932 uint32_t v8::Array::Length() const {
4933 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4934 if (IsDeadCheck(isolate, "v8::Array::Length()")) return 0;
4935 i::Handle<i::JSArray> obj = Utils::OpenHandle(this);
4936 i::Object* length = obj->length();
4937 if (length->IsSmi()) {
4938 return i::Smi::cast(length)->value();
4940 return static_cast<uint32_t>(length->Number());
4945 Local<Object> Array::CloneElementAt(uint32_t index) {
4946 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4947 ON_BAILOUT(isolate, "v8::Array::CloneElementAt()", return Local<Object>());
4948 i::Handle<i::JSObject> self = Utils::OpenHandle(this);
4949 if (!self->HasFastElements()) {
4950 return Local<Object>();
4952 i::FixedArray* elms = i::FixedArray::cast(self->elements());
4953 i::Object* paragon = elms->get(index);
4954 if (!paragon->IsJSObject()) {
4955 return Local<Object>();
4957 i::Handle<i::JSObject> paragon_handle(i::JSObject::cast(paragon));
4958 EXCEPTION_PREAMBLE(isolate);
4960 i::Handle<i::JSObject> result = i::Copy(paragon_handle);
4961 has_pending_exception = result.is_null();
4962 EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>());
4963 return Utils::ToLocal(result);
4967 Local<String> v8::String::NewSymbol(const char* data, int length) {
4968 i::Isolate* isolate = i::Isolate::Current();
4969 EnsureInitializedForIsolate(isolate, "v8::String::NewSymbol()");
4970 LOG_API(isolate, "String::NewSymbol(char)");
4972 if (length == -1) length = i::StrLength(data);
4973 i::Handle<i::String> result =
4974 isolate->factory()->LookupSymbol(i::Vector<const char>(data, length));
4975 return Utils::ToLocal(result);
4979 Local<Number> v8::Number::New(double value) {
4980 i::Isolate* isolate = i::Isolate::Current();
4981 EnsureInitializedForIsolate(isolate, "v8::Number::New()");
4983 // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
4984 value = i::OS::nan_value();
4987 i::Handle<i::Object> result = isolate->factory()->NewNumber(value);
4988 return Utils::NumberToLocal(result);
4992 Local<Integer> v8::Integer::New(int32_t value) {
4993 i::Isolate* isolate = i::Isolate::UncheckedCurrent();
4994 EnsureInitializedForIsolate(isolate, "v8::Integer::New()");
4995 if (i::Smi::IsValid(value)) {
4996 return Utils::IntegerToLocal(i::Handle<i::Object>(i::Smi::FromInt(value),
5000 i::Handle<i::Object> result = isolate->factory()->NewNumber(value);
5001 return Utils::IntegerToLocal(result);
5005 Local<Integer> Integer::NewFromUnsigned(uint32_t value) {
5006 bool fits_into_int32_t = (value & (1 << 31)) == 0;
5007 if (fits_into_int32_t) {
5008 return Integer::New(static_cast<int32_t>(value));
5010 i::Isolate* isolate = i::Isolate::Current();
5012 i::Handle<i::Object> result = isolate->factory()->NewNumber(value);
5013 return Utils::IntegerToLocal(result);
5017 void V8::IgnoreOutOfMemoryException() {
5018 EnterIsolateIfNeeded()->set_ignore_out_of_memory(true);
5022 bool V8::AddMessageListener(MessageCallback that, Handle<Value> data) {
5023 i::Isolate* isolate = i::Isolate::Current();
5024 EnsureInitializedForIsolate(isolate, "v8::V8::AddMessageListener()");
5025 ON_BAILOUT(isolate, "v8::V8::AddMessageListener()", return false);
5027 i::HandleScope scope(isolate);
5028 NeanderArray listeners(isolate->factory()->message_listeners());
5029 NeanderObject obj(2);
5030 obj.set(0, *isolate->factory()->NewForeign(FUNCTION_ADDR(that)));
5031 obj.set(1, data.IsEmpty() ?
5032 isolate->heap()->undefined_value() :
5033 *Utils::OpenHandle(*data));
5034 listeners.add(obj.value());
5039 void V8::RemoveMessageListeners(MessageCallback that) {
5040 i::Isolate* isolate = i::Isolate::Current();
5041 EnsureInitializedForIsolate(isolate, "v8::V8::RemoveMessageListener()");
5042 ON_BAILOUT(isolate, "v8::V8::RemoveMessageListeners()", return);
5044 i::HandleScope scope(isolate);
5045 NeanderArray listeners(isolate->factory()->message_listeners());
5046 for (int i = 0; i < listeners.length(); i++) {
5047 if (listeners.get(i)->IsUndefined()) continue; // skip deleted ones
5049 NeanderObject listener(i::JSObject::cast(listeners.get(i)));
5050 i::Handle<i::Foreign> callback_obj(i::Foreign::cast(listener.get(0)));
5051 if (callback_obj->address() == FUNCTION_ADDR(that)) {
5052 listeners.set(i, isolate->heap()->undefined_value());
5058 void V8::SetCaptureStackTraceForUncaughtExceptions(
5061 StackTrace::StackTraceOptions options) {
5062 i::Isolate::Current()->SetCaptureStackTraceForUncaughtExceptions(
5069 void V8::SetCounterFunction(CounterLookupCallback callback) {
5070 i::Isolate* isolate = EnterIsolateIfNeeded();
5071 if (IsDeadCheck(isolate, "v8::V8::SetCounterFunction()")) return;
5072 isolate->stats_table()->SetCounterFunction(callback);
5075 void V8::SetCreateHistogramFunction(CreateHistogramCallback callback) {
5076 i::Isolate* isolate = EnterIsolateIfNeeded();
5077 if (IsDeadCheck(isolate, "v8::V8::SetCreateHistogramFunction()")) return;
5078 isolate->stats_table()->SetCreateHistogramFunction(callback);
5081 void V8::SetAddHistogramSampleFunction(AddHistogramSampleCallback callback) {
5082 i::Isolate* isolate = EnterIsolateIfNeeded();
5083 if (IsDeadCheck(isolate, "v8::V8::SetAddHistogramSampleFunction()")) return;
5084 isolate->stats_table()->
5085 SetAddHistogramSampleFunction(callback);
5088 void V8::EnableSlidingStateWindow() {
5089 i::Isolate* isolate = i::Isolate::Current();
5090 if (IsDeadCheck(isolate, "v8::V8::EnableSlidingStateWindow()")) return;
5091 isolate->logger()->EnableSlidingStateWindow();
5095 void V8::SetFailedAccessCheckCallbackFunction(
5096 FailedAccessCheckCallback callback) {
5097 i::Isolate* isolate = i::Isolate::Current();
5098 if (IsDeadCheck(isolate, "v8::V8::SetFailedAccessCheckCallbackFunction()")) {
5101 isolate->SetFailedAccessCheckCallback(callback);
5104 void V8::AddObjectGroup(Persistent<Value>* objects,
5106 RetainedObjectInfo* info) {
5107 i::Isolate* isolate = i::Isolate::Current();
5108 if (IsDeadCheck(isolate, "v8::V8::AddObjectGroup()")) return;
5109 STATIC_ASSERT(sizeof(Persistent<Value>) == sizeof(i::Object**));
5110 isolate->global_handles()->AddObjectGroup(
5111 reinterpret_cast<i::Object***>(objects), length, info);
5115 void V8::AddImplicitReferences(Persistent<Object> parent,
5116 Persistent<Value>* children,
5118 i::Isolate* isolate = i::Isolate::Current();
5119 if (IsDeadCheck(isolate, "v8::V8::AddImplicitReferences()")) return;
5120 STATIC_ASSERT(sizeof(Persistent<Value>) == sizeof(i::Object**));
5121 isolate->global_handles()->AddImplicitReferences(
5122 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(*parent)).location(),
5123 reinterpret_cast<i::Object***>(children), length);
5127 int V8::AdjustAmountOfExternalAllocatedMemory(int change_in_bytes) {
5128 i::Isolate* isolate = i::Isolate::Current();
5129 if (IsDeadCheck(isolate, "v8::V8::AdjustAmountOfExternalAllocatedMemory()")) {
5132 return isolate->heap()->AdjustAmountOfExternalAllocatedMemory(
5137 void V8::SetGlobalGCPrologueCallback(GCCallback callback) {
5138 i::Isolate* isolate = i::Isolate::Current();
5139 if (IsDeadCheck(isolate, "v8::V8::SetGlobalGCPrologueCallback()")) return;
5140 isolate->heap()->SetGlobalGCPrologueCallback(callback);
5144 void V8::SetGlobalGCEpilogueCallback(GCCallback callback) {
5145 i::Isolate* isolate = i::Isolate::Current();
5146 if (IsDeadCheck(isolate, "v8::V8::SetGlobalGCEpilogueCallback()")) return;
5147 isolate->heap()->SetGlobalGCEpilogueCallback(callback);
5151 void V8::AddGCPrologueCallback(GCPrologueCallback callback, GCType gc_type) {
5152 i::Isolate* isolate = i::Isolate::Current();
5153 if (IsDeadCheck(isolate, "v8::V8::AddGCPrologueCallback()")) return;
5154 isolate->heap()->AddGCPrologueCallback(callback, gc_type);
5158 void V8::RemoveGCPrologueCallback(GCPrologueCallback callback) {
5159 i::Isolate* isolate = i::Isolate::Current();
5160 if (IsDeadCheck(isolate, "v8::V8::RemoveGCPrologueCallback()")) return;
5161 isolate->heap()->RemoveGCPrologueCallback(callback);
5165 void V8::AddGCEpilogueCallback(GCEpilogueCallback callback, GCType gc_type) {
5166 i::Isolate* isolate = i::Isolate::Current();
5167 if (IsDeadCheck(isolate, "v8::V8::AddGCEpilogueCallback()")) return;
5168 isolate->heap()->AddGCEpilogueCallback(callback, gc_type);
5172 void V8::RemoveGCEpilogueCallback(GCEpilogueCallback callback) {
5173 i::Isolate* isolate = i::Isolate::Current();
5174 if (IsDeadCheck(isolate, "v8::V8::RemoveGCEpilogueCallback()")) return;
5175 isolate->heap()->RemoveGCEpilogueCallback(callback);
5179 void V8::AddMemoryAllocationCallback(MemoryAllocationCallback callback,
5181 AllocationAction action) {
5182 i::Isolate* isolate = i::Isolate::Current();
5183 if (IsDeadCheck(isolate, "v8::V8::AddMemoryAllocationCallback()")) return;
5184 isolate->memory_allocator()->AddMemoryAllocationCallback(
5185 callback, space, action);
5189 void V8::RemoveMemoryAllocationCallback(MemoryAllocationCallback callback) {
5190 i::Isolate* isolate = i::Isolate::Current();
5191 if (IsDeadCheck(isolate, "v8::V8::RemoveMemoryAllocationCallback()")) return;
5192 isolate->memory_allocator()->RemoveMemoryAllocationCallback(
5197 void V8::PauseProfiler() {
5198 i::Isolate* isolate = i::Isolate::Current();
5199 isolate->logger()->PauseProfiler();
5203 void V8::ResumeProfiler() {
5204 i::Isolate* isolate = i::Isolate::Current();
5205 isolate->logger()->ResumeProfiler();
5209 bool V8::IsProfilerPaused() {
5210 i::Isolate* isolate = i::Isolate::Current();
5211 return isolate->logger()->IsProfilerPaused();
5215 int V8::GetCurrentThreadId() {
5216 i::Isolate* isolate = i::Isolate::Current();
5217 EnsureInitializedForIsolate(isolate, "V8::GetCurrentThreadId()");
5218 return isolate->thread_id().ToInteger();
5222 void V8::TerminateExecution(int thread_id) {
5223 i::Isolate* isolate = i::Isolate::Current();
5224 if (!isolate->IsInitialized()) return;
5225 API_ENTRY_CHECK(isolate, "V8::TerminateExecution()");
5226 // If the thread_id identifies the current thread just terminate
5227 // execution right away. Otherwise, ask the thread manager to
5228 // terminate the thread with the given id if any.
5229 i::ThreadId internal_tid = i::ThreadId::FromInteger(thread_id);
5230 if (isolate->thread_id().Equals(internal_tid)) {
5231 isolate->stack_guard()->TerminateExecution();
5233 isolate->thread_manager()->TerminateExecution(internal_tid);
5238 void V8::TerminateExecution(Isolate* isolate) {
5239 // If no isolate is supplied, use the default isolate.
5240 if (isolate != NULL) {
5241 reinterpret_cast<i::Isolate*>(isolate)->stack_guard()->TerminateExecution();
5243 i::Isolate::GetDefaultIsolateStackGuard()->TerminateExecution();
5248 bool V8::IsExecutionTerminating(Isolate* isolate) {
5249 i::Isolate* i_isolate = isolate != NULL ?
5250 reinterpret_cast<i::Isolate*>(isolate) : i::Isolate::Current();
5251 return IsExecutionTerminatingCheck(i_isolate);
5255 Isolate* Isolate::GetCurrent() {
5256 i::Isolate* isolate = i::Isolate::UncheckedCurrent();
5257 return reinterpret_cast<Isolate*>(isolate);
5261 Isolate* Isolate::New() {
5262 i::Isolate* isolate = new i::Isolate();
5263 return reinterpret_cast<Isolate*>(isolate);
5267 void Isolate::Dispose() {
5268 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
5269 if (!ApiCheck(!isolate->IsInUse(),
5270 "v8::Isolate::Dispose()",
5271 "Disposing the isolate that is entered by a thread.")) {
5274 isolate->TearDown();
5278 void Isolate::Enter() {
5279 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
5284 void Isolate::Exit() {
5285 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
5290 void Isolate::SetData(void* data) {
5291 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
5292 isolate->SetData(data);
5295 void* Isolate::GetData() {
5296 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
5297 return isolate->GetData();
5301 String::Utf8Value::Utf8Value(v8::Handle<v8::Value> obj)
5302 : str_(NULL), length_(0) {
5303 i::Isolate* isolate = i::Isolate::Current();
5304 if (IsDeadCheck(isolate, "v8::String::Utf8Value::Utf8Value()")) return;
5305 if (obj.IsEmpty()) return;
5307 i::HandleScope scope(isolate);
5309 Handle<String> str = obj->ToString();
5310 if (str.IsEmpty()) return;
5311 length_ = str->Utf8Length();
5312 str_ = i::NewArray<char>(length_ + 1);
5313 str->WriteUtf8(str_);
5317 String::Utf8Value::~Utf8Value() {
5318 i::DeleteArray(str_);
5322 String::AsciiValue::AsciiValue(v8::Handle<v8::Value> obj)
5323 : str_(NULL), length_(0) {
5324 i::Isolate* isolate = i::Isolate::Current();
5325 if (IsDeadCheck(isolate, "v8::String::AsciiValue::AsciiValue()")) return;
5326 if (obj.IsEmpty()) return;
5328 i::HandleScope scope(isolate);
5330 Handle<String> str = obj->ToString();
5331 if (str.IsEmpty()) return;
5332 length_ = str->Length();
5333 str_ = i::NewArray<char>(length_ + 1);
5334 str->WriteAscii(str_);
5338 String::AsciiValue::~AsciiValue() {
5339 i::DeleteArray(str_);
5343 String::Value::Value(v8::Handle<v8::Value> obj)
5344 : str_(NULL), length_(0) {
5345 i::Isolate* isolate = i::Isolate::Current();
5346 if (IsDeadCheck(isolate, "v8::String::Value::Value()")) return;
5347 if (obj.IsEmpty()) return;
5349 i::HandleScope scope(isolate);
5351 Handle<String> str = obj->ToString();
5352 if (str.IsEmpty()) return;
5353 length_ = str->Length();
5354 str_ = i::NewArray<uint16_t>(length_ + 1);
5359 String::Value::~Value() {
5360 i::DeleteArray(str_);
5363 Local<Value> Exception::RangeError(v8::Handle<v8::String> raw_message) {
5364 i::Isolate* isolate = i::Isolate::Current();
5365 LOG_API(isolate, "RangeError");
5366 ON_BAILOUT(isolate, "v8::Exception::RangeError()", return Local<Value>());
5370 i::HandleScope scope(isolate);
5371 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
5372 i::Handle<i::Object> result = isolate->factory()->NewRangeError(message);
5375 i::Handle<i::Object> result(error);
5376 return Utils::ToLocal(result);
5379 Local<Value> Exception::ReferenceError(v8::Handle<v8::String> raw_message) {
5380 i::Isolate* isolate = i::Isolate::Current();
5381 LOG_API(isolate, "ReferenceError");
5382 ON_BAILOUT(isolate, "v8::Exception::ReferenceError()", return Local<Value>());
5386 i::HandleScope scope(isolate);
5387 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
5388 i::Handle<i::Object> result =
5389 isolate->factory()->NewReferenceError(message);
5392 i::Handle<i::Object> result(error);
5393 return Utils::ToLocal(result);
5396 Local<Value> Exception::SyntaxError(v8::Handle<v8::String> raw_message) {
5397 i::Isolate* isolate = i::Isolate::Current();
5398 LOG_API(isolate, "SyntaxError");
5399 ON_BAILOUT(isolate, "v8::Exception::SyntaxError()", return Local<Value>());
5403 i::HandleScope scope(isolate);
5404 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
5405 i::Handle<i::Object> result = isolate->factory()->NewSyntaxError(message);
5408 i::Handle<i::Object> result(error);
5409 return Utils::ToLocal(result);
5412 Local<Value> Exception::TypeError(v8::Handle<v8::String> raw_message) {
5413 i::Isolate* isolate = i::Isolate::Current();
5414 LOG_API(isolate, "TypeError");
5415 ON_BAILOUT(isolate, "v8::Exception::TypeError()", return Local<Value>());
5419 i::HandleScope scope(isolate);
5420 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
5421 i::Handle<i::Object> result = isolate->factory()->NewTypeError(message);
5424 i::Handle<i::Object> result(error);
5425 return Utils::ToLocal(result);
5428 Local<Value> Exception::Error(v8::Handle<v8::String> raw_message) {
5429 i::Isolate* isolate = i::Isolate::Current();
5430 LOG_API(isolate, "Error");
5431 ON_BAILOUT(isolate, "v8::Exception::Error()", return Local<Value>());
5435 i::HandleScope scope(isolate);
5436 i::Handle<i::String> message = Utils::OpenHandle(*raw_message);
5437 i::Handle<i::Object> result = isolate->factory()->NewError(message);
5440 i::Handle<i::Object> result(error);
5441 return Utils::ToLocal(result);
5445 // --- D e b u g S u p p o r t ---
5447 #ifdef ENABLE_DEBUGGER_SUPPORT
5449 static void EventCallbackWrapper(const v8::Debug::EventDetails& event_details) {
5450 i::Isolate* isolate = i::Isolate::Current();
5451 if (isolate->debug_event_callback() != NULL) {
5452 isolate->debug_event_callback()(event_details.GetEvent(),
5453 event_details.GetExecutionState(),
5454 event_details.GetEventData(),
5455 event_details.GetCallbackData());
5460 bool Debug::SetDebugEventListener(EventCallback that, Handle<Value> data) {
5461 i::Isolate* isolate = i::Isolate::Current();
5462 EnsureInitializedForIsolate(isolate, "v8::Debug::SetDebugEventListener()");
5463 ON_BAILOUT(isolate, "v8::Debug::SetDebugEventListener()", return false);
5466 isolate->set_debug_event_callback(that);
5468 i::HandleScope scope(isolate);
5469 i::Handle<i::Object> foreign = isolate->factory()->undefined_value();
5472 isolate->factory()->NewForeign(FUNCTION_ADDR(EventCallbackWrapper));
5474 isolate->debugger()->SetEventListener(foreign, Utils::OpenHandle(*data));
5479 bool Debug::SetDebugEventListener2(EventCallback2 that, Handle<Value> data) {
5480 i::Isolate* isolate = i::Isolate::Current();
5481 EnsureInitializedForIsolate(isolate, "v8::Debug::SetDebugEventListener2()");
5482 ON_BAILOUT(isolate, "v8::Debug::SetDebugEventListener2()", return false);
5484 i::HandleScope scope(isolate);
5485 i::Handle<i::Object> foreign = isolate->factory()->undefined_value();
5487 foreign = isolate->factory()->NewForeign(FUNCTION_ADDR(that));
5489 isolate->debugger()->SetEventListener(foreign, Utils::OpenHandle(*data));
5494 bool Debug::SetDebugEventListener(v8::Handle<v8::Object> that,
5495 Handle<Value> data) {
5496 i::Isolate* isolate = i::Isolate::Current();
5497 ON_BAILOUT(isolate, "v8::Debug::SetDebugEventListener()", return false);
5499 isolate->debugger()->SetEventListener(Utils::OpenHandle(*that),
5500 Utils::OpenHandle(*data));
5505 void Debug::DebugBreak(Isolate* isolate) {
5506 // If no isolate is supplied, use the default isolate.
5507 if (isolate != NULL) {
5508 reinterpret_cast<i::Isolate*>(isolate)->stack_guard()->DebugBreak();
5510 i::Isolate::GetDefaultIsolateStackGuard()->DebugBreak();
5515 void Debug::CancelDebugBreak(Isolate* isolate) {
5516 // If no isolate is supplied, use the default isolate.
5517 if (isolate != NULL) {
5518 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
5519 internal_isolate->stack_guard()->Continue(i::DEBUGBREAK);
5521 i::Isolate::GetDefaultIsolateStackGuard()->Continue(i::DEBUGBREAK);
5526 void Debug::DebugBreakForCommand(ClientData* data, Isolate* isolate) {
5527 // If no isolate is supplied, use the default isolate.
5528 if (isolate != NULL) {
5529 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
5530 internal_isolate->debugger()->EnqueueDebugCommand(data);
5532 i::Isolate::GetDefaultIsolateDebugger()->EnqueueDebugCommand(data);
5537 static void MessageHandlerWrapper(const v8::Debug::Message& message) {
5538 i::Isolate* isolate = i::Isolate::Current();
5539 if (isolate->message_handler()) {
5540 v8::String::Value json(message.GetJSON());
5541 (isolate->message_handler())(*json, json.length(), message.GetClientData());
5546 void Debug::SetMessageHandler(v8::Debug::MessageHandler handler,
5547 bool message_handler_thread) {
5548 i::Isolate* isolate = i::Isolate::Current();
5549 EnsureInitializedForIsolate(isolate, "v8::Debug::SetMessageHandler");
5552 // Message handler thread not supported any more. Parameter temporally left in
5553 // the API for client compatibility reasons.
5554 CHECK(!message_handler_thread);
5556 // TODO(sgjesse) support the old message handler API through a simple wrapper.
5557 isolate->set_message_handler(handler);
5558 if (handler != NULL) {
5559 isolate->debugger()->SetMessageHandler(MessageHandlerWrapper);
5561 isolate->debugger()->SetMessageHandler(NULL);
5566 void Debug::SetMessageHandler2(v8::Debug::MessageHandler2 handler) {
5567 i::Isolate* isolate = i::Isolate::Current();
5568 EnsureInitializedForIsolate(isolate, "v8::Debug::SetMessageHandler");
5570 isolate->debugger()->SetMessageHandler(handler);
5574 void Debug::SendCommand(const uint16_t* command, int length,
5575 ClientData* client_data,
5577 // If no isolate is supplied, use the default isolate.
5578 if (isolate != NULL) {
5579 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
5580 internal_isolate->debugger()->ProcessCommand(
5581 i::Vector<const uint16_t>(command, length), client_data);
5583 i::Isolate::GetDefaultIsolateDebugger()->ProcessCommand(
5584 i::Vector<const uint16_t>(command, length), client_data);
5589 void Debug::SetHostDispatchHandler(HostDispatchHandler handler,
5591 i::Isolate* isolate = i::Isolate::Current();
5592 EnsureInitializedForIsolate(isolate, "v8::Debug::SetHostDispatchHandler");
5594 isolate->debugger()->SetHostDispatchHandler(handler, period);
5598 void Debug::SetDebugMessageDispatchHandler(
5599 DebugMessageDispatchHandler handler, bool provide_locker) {
5600 i::Isolate* isolate = i::Isolate::Current();
5601 EnsureInitializedForIsolate(isolate,
5602 "v8::Debug::SetDebugMessageDispatchHandler");
5604 isolate->debugger()->SetDebugMessageDispatchHandler(
5605 handler, provide_locker);
5609 Local<Value> Debug::Call(v8::Handle<v8::Function> fun,
5610 v8::Handle<v8::Value> data) {
5611 i::Isolate* isolate = i::Isolate::Current();
5612 if (!isolate->IsInitialized()) return Local<Value>();
5613 ON_BAILOUT(isolate, "v8::Debug::Call()", return Local<Value>());
5615 i::Handle<i::Object> result;
5616 EXCEPTION_PREAMBLE(isolate);
5617 if (data.IsEmpty()) {
5618 result = isolate->debugger()->Call(Utils::OpenHandle(*fun),
5619 isolate->factory()->undefined_value(),
5620 &has_pending_exception);
5622 result = isolate->debugger()->Call(Utils::OpenHandle(*fun),
5623 Utils::OpenHandle(*data),
5624 &has_pending_exception);
5626 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
5627 return Utils::ToLocal(result);
5631 Local<Value> Debug::GetMirror(v8::Handle<v8::Value> obj) {
5632 i::Isolate* isolate = i::Isolate::Current();
5633 if (!isolate->IsInitialized()) return Local<Value>();
5634 ON_BAILOUT(isolate, "v8::Debug::GetMirror()", return Local<Value>());
5636 v8::HandleScope scope;
5637 i::Debug* isolate_debug = isolate->debug();
5638 isolate_debug->Load();
5639 i::Handle<i::JSObject> debug(isolate_debug->debug_context()->global());
5640 i::Handle<i::String> name =
5641 isolate->factory()->LookupAsciiSymbol("MakeMirror");
5642 i::Handle<i::Object> fun_obj = i::GetProperty(debug, name);
5643 i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(fun_obj);
5644 v8::Handle<v8::Function> v8_fun = Utils::ToLocal(fun);
5645 const int kArgc = 1;
5646 v8::Handle<v8::Value> argv[kArgc] = { obj };
5647 EXCEPTION_PREAMBLE(isolate);
5648 v8::Handle<v8::Value> result = v8_fun->Call(Utils::ToLocal(debug),
5651 EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
5652 return scope.Close(result);
5656 bool Debug::EnableAgent(const char* name, int port, bool wait_for_connection) {
5657 return i::Isolate::Current()->debugger()->StartAgent(name, port,
5658 wait_for_connection);
5662 void Debug::DisableAgent() {
5663 return i::Isolate::Current()->debugger()->StopAgent();
5667 void Debug::ProcessDebugMessages() {
5668 i::Execution::ProcessDebugMesssages(true);
5671 Local<Context> Debug::GetDebugContext() {
5672 i::Isolate* isolate = i::Isolate::Current();
5673 EnsureInitializedForIsolate(isolate, "v8::Debug::GetDebugContext()");
5675 return Utils::ToLocal(i::Isolate::Current()->debugger()->GetDebugContext());
5678 #endif // ENABLE_DEBUGGER_SUPPORT
5681 Handle<String> CpuProfileNode::GetFunctionName() const {
5682 i::Isolate* isolate = i::Isolate::Current();
5683 IsDeadCheck(isolate, "v8::CpuProfileNode::GetFunctionName");
5684 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
5685 const i::CodeEntry* entry = node->entry();
5686 if (!entry->has_name_prefix()) {
5687 return Handle<String>(ToApi<String>(
5688 isolate->factory()->LookupAsciiSymbol(entry->name())));
5690 return Handle<String>(ToApi<String>(isolate->factory()->NewConsString(
5691 isolate->factory()->LookupAsciiSymbol(entry->name_prefix()),
5692 isolate->factory()->LookupAsciiSymbol(entry->name()))));
5697 Handle<String> CpuProfileNode::GetScriptResourceName() const {
5698 i::Isolate* isolate = i::Isolate::Current();
5699 IsDeadCheck(isolate, "v8::CpuProfileNode::GetScriptResourceName");
5700 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
5701 return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol(
5702 node->entry()->resource_name())));
5706 int CpuProfileNode::GetLineNumber() const {
5707 i::Isolate* isolate = i::Isolate::Current();
5708 IsDeadCheck(isolate, "v8::CpuProfileNode::GetLineNumber");
5709 return reinterpret_cast<const i::ProfileNode*>(this)->entry()->line_number();
5713 double CpuProfileNode::GetTotalTime() const {
5714 i::Isolate* isolate = i::Isolate::Current();
5715 IsDeadCheck(isolate, "v8::CpuProfileNode::GetTotalTime");
5716 return reinterpret_cast<const i::ProfileNode*>(this)->GetTotalMillis();
5720 double CpuProfileNode::GetSelfTime() const {
5721 i::Isolate* isolate = i::Isolate::Current();
5722 IsDeadCheck(isolate, "v8::CpuProfileNode::GetSelfTime");
5723 return reinterpret_cast<const i::ProfileNode*>(this)->GetSelfMillis();
5727 double CpuProfileNode::GetTotalSamplesCount() const {
5728 i::Isolate* isolate = i::Isolate::Current();
5729 IsDeadCheck(isolate, "v8::CpuProfileNode::GetTotalSamplesCount");
5730 return reinterpret_cast<const i::ProfileNode*>(this)->total_ticks();
5734 double CpuProfileNode::GetSelfSamplesCount() const {
5735 i::Isolate* isolate = i::Isolate::Current();
5736 IsDeadCheck(isolate, "v8::CpuProfileNode::GetSelfSamplesCount");
5737 return reinterpret_cast<const i::ProfileNode*>(this)->self_ticks();
5741 unsigned CpuProfileNode::GetCallUid() const {
5742 i::Isolate* isolate = i::Isolate::Current();
5743 IsDeadCheck(isolate, "v8::CpuProfileNode::GetCallUid");
5744 return reinterpret_cast<const i::ProfileNode*>(this)->entry()->GetCallUid();
5748 int CpuProfileNode::GetChildrenCount() const {
5749 i::Isolate* isolate = i::Isolate::Current();
5750 IsDeadCheck(isolate, "v8::CpuProfileNode::GetChildrenCount");
5751 return reinterpret_cast<const i::ProfileNode*>(this)->children()->length();
5755 const CpuProfileNode* CpuProfileNode::GetChild(int index) const {
5756 i::Isolate* isolate = i::Isolate::Current();
5757 IsDeadCheck(isolate, "v8::CpuProfileNode::GetChild");
5758 const i::ProfileNode* child =
5759 reinterpret_cast<const i::ProfileNode*>(this)->children()->at(index);
5760 return reinterpret_cast<const CpuProfileNode*>(child);
5764 void CpuProfile::Delete() {
5765 i::Isolate* isolate = i::Isolate::Current();
5766 IsDeadCheck(isolate, "v8::CpuProfile::Delete");
5767 i::CpuProfiler::DeleteProfile(reinterpret_cast<i::CpuProfile*>(this));
5768 if (i::CpuProfiler::GetProfilesCount() == 0 &&
5769 !i::CpuProfiler::HasDetachedProfiles()) {
5770 // If this was the last profile, clean up all accessory data as well.
5771 i::CpuProfiler::DeleteAllProfiles();
5776 unsigned CpuProfile::GetUid() const {
5777 i::Isolate* isolate = i::Isolate::Current();
5778 IsDeadCheck(isolate, "v8::CpuProfile::GetUid");
5779 return reinterpret_cast<const i::CpuProfile*>(this)->uid();
5783 Handle<String> CpuProfile::GetTitle() const {
5784 i::Isolate* isolate = i::Isolate::Current();
5785 IsDeadCheck(isolate, "v8::CpuProfile::GetTitle");
5786 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
5787 return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol(
5788 profile->title())));
5792 const CpuProfileNode* CpuProfile::GetBottomUpRoot() const {
5793 i::Isolate* isolate = i::Isolate::Current();
5794 IsDeadCheck(isolate, "v8::CpuProfile::GetBottomUpRoot");
5795 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
5796 return reinterpret_cast<const CpuProfileNode*>(profile->bottom_up()->root());
5800 const CpuProfileNode* CpuProfile::GetTopDownRoot() const {
5801 i::Isolate* isolate = i::Isolate::Current();
5802 IsDeadCheck(isolate, "v8::CpuProfile::GetTopDownRoot");
5803 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
5804 return reinterpret_cast<const CpuProfileNode*>(profile->top_down()->root());
5808 int CpuProfiler::GetProfilesCount() {
5809 i::Isolate* isolate = i::Isolate::Current();
5810 IsDeadCheck(isolate, "v8::CpuProfiler::GetProfilesCount");
5811 return i::CpuProfiler::GetProfilesCount();
5815 const CpuProfile* CpuProfiler::GetProfile(int index,
5816 Handle<Value> security_token) {
5817 i::Isolate* isolate = i::Isolate::Current();
5818 IsDeadCheck(isolate, "v8::CpuProfiler::GetProfile");
5819 return reinterpret_cast<const CpuProfile*>(
5820 i::CpuProfiler::GetProfile(
5821 security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
5826 const CpuProfile* CpuProfiler::FindProfile(unsigned uid,
5827 Handle<Value> security_token) {
5828 i::Isolate* isolate = i::Isolate::Current();
5829 IsDeadCheck(isolate, "v8::CpuProfiler::FindProfile");
5830 return reinterpret_cast<const CpuProfile*>(
5831 i::CpuProfiler::FindProfile(
5832 security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
5837 void CpuProfiler::StartProfiling(Handle<String> title) {
5838 i::Isolate* isolate = i::Isolate::Current();
5839 IsDeadCheck(isolate, "v8::CpuProfiler::StartProfiling");
5840 i::CpuProfiler::StartProfiling(*Utils::OpenHandle(*title));
5844 const CpuProfile* CpuProfiler::StopProfiling(Handle<String> title,
5845 Handle<Value> security_token) {
5846 i::Isolate* isolate = i::Isolate::Current();
5847 IsDeadCheck(isolate, "v8::CpuProfiler::StopProfiling");
5848 return reinterpret_cast<const CpuProfile*>(
5849 i::CpuProfiler::StopProfiling(
5850 security_token.IsEmpty() ? NULL : *Utils::OpenHandle(*security_token),
5851 *Utils::OpenHandle(*title)));
5855 void CpuProfiler::DeleteAllProfiles() {
5856 i::Isolate* isolate = i::Isolate::Current();
5857 IsDeadCheck(isolate, "v8::CpuProfiler::DeleteAllProfiles");
5858 i::CpuProfiler::DeleteAllProfiles();
5862 static i::HeapGraphEdge* ToInternal(const HeapGraphEdge* edge) {
5863 return const_cast<i::HeapGraphEdge*>(
5864 reinterpret_cast<const i::HeapGraphEdge*>(edge));
5868 HeapGraphEdge::Type HeapGraphEdge::GetType() const {
5869 i::Isolate* isolate = i::Isolate::Current();
5870 IsDeadCheck(isolate, "v8::HeapGraphEdge::GetType");
5871 return static_cast<HeapGraphEdge::Type>(ToInternal(this)->type());
5875 Handle<Value> HeapGraphEdge::GetName() const {
5876 i::Isolate* isolate = i::Isolate::Current();
5877 IsDeadCheck(isolate, "v8::HeapGraphEdge::GetName");
5878 i::HeapGraphEdge* edge = ToInternal(this);
5879 switch (edge->type()) {
5880 case i::HeapGraphEdge::kContextVariable:
5881 case i::HeapGraphEdge::kInternal:
5882 case i::HeapGraphEdge::kProperty:
5883 case i::HeapGraphEdge::kShortcut:
5884 return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol(
5886 case i::HeapGraphEdge::kElement:
5887 case i::HeapGraphEdge::kHidden:
5888 return Handle<Number>(ToApi<Number>(isolate->factory()->NewNumberFromInt(
5890 default: UNREACHABLE();
5892 return v8::Undefined();
5896 const HeapGraphNode* HeapGraphEdge::GetFromNode() const {
5897 i::Isolate* isolate = i::Isolate::Current();
5898 IsDeadCheck(isolate, "v8::HeapGraphEdge::GetFromNode");
5899 const i::HeapEntry* from = ToInternal(this)->From();
5900 return reinterpret_cast<const HeapGraphNode*>(from);
5904 const HeapGraphNode* HeapGraphEdge::GetToNode() const {
5905 i::Isolate* isolate = i::Isolate::Current();
5906 IsDeadCheck(isolate, "v8::HeapGraphEdge::GetToNode");
5907 const i::HeapEntry* to = ToInternal(this)->to();
5908 return reinterpret_cast<const HeapGraphNode*>(to);
5912 static i::HeapEntry* ToInternal(const HeapGraphNode* entry) {
5913 return const_cast<i::HeapEntry*>(
5914 reinterpret_cast<const i::HeapEntry*>(entry));
5918 HeapGraphNode::Type HeapGraphNode::GetType() const {
5919 i::Isolate* isolate = i::Isolate::Current();
5920 IsDeadCheck(isolate, "v8::HeapGraphNode::GetType");
5921 return static_cast<HeapGraphNode::Type>(ToInternal(this)->type());
5925 Handle<String> HeapGraphNode::GetName() const {
5926 i::Isolate* isolate = i::Isolate::Current();
5927 IsDeadCheck(isolate, "v8::HeapGraphNode::GetName");
5928 return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol(
5929 ToInternal(this)->name())));
5933 uint64_t HeapGraphNode::GetId() const {
5934 i::Isolate* isolate = i::Isolate::Current();
5935 IsDeadCheck(isolate, "v8::HeapGraphNode::GetId");
5936 return ToInternal(this)->id();
5940 int HeapGraphNode::GetSelfSize() const {
5941 i::Isolate* isolate = i::Isolate::Current();
5942 IsDeadCheck(isolate, "v8::HeapGraphNode::GetSelfSize");
5943 return ToInternal(this)->self_size();
5947 int HeapGraphNode::GetRetainedSize(bool exact) const {
5948 i::Isolate* isolate = i::Isolate::Current();
5949 IsDeadCheck(isolate, "v8::HeapSnapshot::GetRetainedSize");
5950 return ToInternal(this)->RetainedSize(exact);
5954 int HeapGraphNode::GetChildrenCount() const {
5955 i::Isolate* isolate = i::Isolate::Current();
5956 IsDeadCheck(isolate, "v8::HeapSnapshot::GetChildrenCount");
5957 return ToInternal(this)->children().length();
5961 const HeapGraphEdge* HeapGraphNode::GetChild(int index) const {
5962 i::Isolate* isolate = i::Isolate::Current();
5963 IsDeadCheck(isolate, "v8::HeapSnapshot::GetChild");
5964 return reinterpret_cast<const HeapGraphEdge*>(
5965 &ToInternal(this)->children()[index]);
5969 int HeapGraphNode::GetRetainersCount() const {
5970 i::Isolate* isolate = i::Isolate::Current();
5971 IsDeadCheck(isolate, "v8::HeapSnapshot::GetRetainersCount");
5972 return ToInternal(this)->retainers().length();
5976 const HeapGraphEdge* HeapGraphNode::GetRetainer(int index) const {
5977 i::Isolate* isolate = i::Isolate::Current();
5978 IsDeadCheck(isolate, "v8::HeapSnapshot::GetRetainer");
5979 return reinterpret_cast<const HeapGraphEdge*>(
5980 ToInternal(this)->retainers()[index]);
5984 const HeapGraphNode* HeapGraphNode::GetDominatorNode() const {
5985 i::Isolate* isolate = i::Isolate::Current();
5986 IsDeadCheck(isolate, "v8::HeapSnapshot::GetDominatorNode");
5987 return reinterpret_cast<const HeapGraphNode*>(ToInternal(this)->dominator());
5991 v8::Handle<v8::Value> HeapGraphNode::GetHeapValue() const {
5992 i::Isolate* isolate = i::Isolate::Current();
5993 IsDeadCheck(isolate, "v8::HeapGraphNode::GetHeapValue");
5994 i::Handle<i::HeapObject> object = ToInternal(this)->GetHeapObject();
5995 return v8::Handle<Value>(!object.is_null() ?
5996 ToApi<Value>(object) : ToApi<Value>(
5997 isolate->factory()->undefined_value()));
6001 static i::HeapSnapshot* ToInternal(const HeapSnapshot* snapshot) {
6002 return const_cast<i::HeapSnapshot*>(
6003 reinterpret_cast<const i::HeapSnapshot*>(snapshot));
6007 void HeapSnapshot::Delete() {
6008 i::Isolate* isolate = i::Isolate::Current();
6009 IsDeadCheck(isolate, "v8::HeapSnapshot::Delete");
6010 if (i::HeapProfiler::GetSnapshotsCount() > 1) {
6011 ToInternal(this)->Delete();
6013 // If this is the last snapshot, clean up all accessory data as well.
6014 i::HeapProfiler::DeleteAllSnapshots();
6019 HeapSnapshot::Type HeapSnapshot::GetType() const {
6020 i::Isolate* isolate = i::Isolate::Current();
6021 IsDeadCheck(isolate, "v8::HeapSnapshot::GetType");
6022 return static_cast<HeapSnapshot::Type>(ToInternal(this)->type());
6026 unsigned HeapSnapshot::GetUid() const {
6027 i::Isolate* isolate = i::Isolate::Current();
6028 IsDeadCheck(isolate, "v8::HeapSnapshot::GetUid");
6029 return ToInternal(this)->uid();
6033 Handle<String> HeapSnapshot::GetTitle() const {
6034 i::Isolate* isolate = i::Isolate::Current();
6035 IsDeadCheck(isolate, "v8::HeapSnapshot::GetTitle");
6036 return Handle<String>(ToApi<String>(isolate->factory()->LookupAsciiSymbol(
6037 ToInternal(this)->title())));
6041 const HeapGraphNode* HeapSnapshot::GetRoot() const {
6042 i::Isolate* isolate = i::Isolate::Current();
6043 IsDeadCheck(isolate, "v8::HeapSnapshot::GetHead");
6044 return reinterpret_cast<const HeapGraphNode*>(ToInternal(this)->root());
6048 const HeapGraphNode* HeapSnapshot::GetNodeById(uint64_t id) const {
6049 i::Isolate* isolate = i::Isolate::Current();
6050 IsDeadCheck(isolate, "v8::HeapSnapshot::GetNodeById");
6051 return reinterpret_cast<const HeapGraphNode*>(
6052 ToInternal(this)->GetEntryById(id));
6056 int HeapSnapshot::GetNodesCount() const {
6057 i::Isolate* isolate = i::Isolate::Current();
6058 IsDeadCheck(isolate, "v8::HeapSnapshot::GetNodesCount");
6059 return ToInternal(this)->entries()->length();
6063 const HeapGraphNode* HeapSnapshot::GetNode(int index) const {
6064 i::Isolate* isolate = i::Isolate::Current();
6065 IsDeadCheck(isolate, "v8::HeapSnapshot::GetNode");
6066 return reinterpret_cast<const HeapGraphNode*>(
6067 ToInternal(this)->entries()->at(index));
6071 void HeapSnapshot::Serialize(OutputStream* stream,
6072 HeapSnapshot::SerializationFormat format) const {
6073 i::Isolate* isolate = i::Isolate::Current();
6074 IsDeadCheck(isolate, "v8::HeapSnapshot::Serialize");
6075 ApiCheck(format == kJSON,
6076 "v8::HeapSnapshot::Serialize",
6077 "Unknown serialization format");
6078 ApiCheck(stream->GetOutputEncoding() == OutputStream::kAscii,
6079 "v8::HeapSnapshot::Serialize",
6080 "Unsupported output encoding");
6081 ApiCheck(stream->GetChunkSize() > 0,
6082 "v8::HeapSnapshot::Serialize",
6083 "Invalid stream chunk size");
6084 i::HeapSnapshotJSONSerializer serializer(ToInternal(this));
6085 serializer.Serialize(stream);
6089 int HeapProfiler::GetSnapshotsCount() {
6090 i::Isolate* isolate = i::Isolate::Current();
6091 IsDeadCheck(isolate, "v8::HeapProfiler::GetSnapshotsCount");
6092 return i::HeapProfiler::GetSnapshotsCount();
6096 const HeapSnapshot* HeapProfiler::GetSnapshot(int index) {
6097 i::Isolate* isolate = i::Isolate::Current();
6098 IsDeadCheck(isolate, "v8::HeapProfiler::GetSnapshot");
6099 return reinterpret_cast<const HeapSnapshot*>(
6100 i::HeapProfiler::GetSnapshot(index));
6104 const HeapSnapshot* HeapProfiler::FindSnapshot(unsigned uid) {
6105 i::Isolate* isolate = i::Isolate::Current();
6106 IsDeadCheck(isolate, "v8::HeapProfiler::FindSnapshot");
6107 return reinterpret_cast<const HeapSnapshot*>(
6108 i::HeapProfiler::FindSnapshot(uid));
6112 const HeapSnapshot* HeapProfiler::TakeSnapshot(Handle<String> title,
6113 HeapSnapshot::Type type,
6114 ActivityControl* control) {
6115 i::Isolate* isolate = i::Isolate::Current();
6116 IsDeadCheck(isolate, "v8::HeapProfiler::TakeSnapshot");
6117 i::HeapSnapshot::Type internal_type = i::HeapSnapshot::kFull;
6119 case HeapSnapshot::kFull:
6120 internal_type = i::HeapSnapshot::kFull;
6125 return reinterpret_cast<const HeapSnapshot*>(
6126 i::HeapProfiler::TakeSnapshot(
6127 *Utils::OpenHandle(*title), internal_type, control));
6131 void HeapProfiler::DeleteAllSnapshots() {
6132 i::Isolate* isolate = i::Isolate::Current();
6133 IsDeadCheck(isolate, "v8::HeapProfiler::DeleteAllSnapshots");
6134 i::HeapProfiler::DeleteAllSnapshots();
6138 void HeapProfiler::DefineWrapperClass(uint16_t class_id,
6139 WrapperInfoCallback callback) {
6140 i::Isolate::Current()->heap_profiler()->DefineWrapperClass(class_id,
6146 v8::Testing::StressType internal::Testing::stress_type_ =
6147 v8::Testing::kStressTypeOpt;
6150 void Testing::SetStressRunType(Testing::StressType type) {
6151 internal::Testing::set_stress_type(type);
6154 int Testing::GetStressRuns() {
6155 if (internal::FLAG_stress_runs != 0) return internal::FLAG_stress_runs;
6157 // In debug mode the code runs much slower so stressing will only make two
6166 static void SetFlagsFromString(const char* flags) {
6167 V8::SetFlagsFromString(flags, i::StrLength(flags));
6171 void Testing::PrepareStressRun(int run) {
6172 static const char* kLazyOptimizations =
6173 "--prepare-always-opt --nolimit-inlining "
6174 "--noalways-opt --noopt-eagerly";
6175 static const char* kEagerOptimizations = "--opt-eagerly";
6176 static const char* kForcedOptimizations = "--always-opt";
6178 // If deoptimization stressed turn on frequent deoptimization. If no value
6179 // is spefified through --deopt-every-n-times use a default default value.
6180 static const char* kDeoptEvery13Times = "--deopt-every-n-times=13";
6181 if (internal::Testing::stress_type() == Testing::kStressTypeDeopt &&
6182 internal::FLAG_deopt_every_n_times == 0) {
6183 SetFlagsFromString(kDeoptEvery13Times);
6187 // As stressing in debug mode only make two runs skip the deopt stressing
6189 if (run == GetStressRuns() - 1) {
6190 SetFlagsFromString(kForcedOptimizations);
6192 SetFlagsFromString(kEagerOptimizations);
6193 SetFlagsFromString(kLazyOptimizations);
6196 if (run == GetStressRuns() - 1) {
6197 SetFlagsFromString(kForcedOptimizations);
6198 } else if (run == GetStressRuns() - 2) {
6199 SetFlagsFromString(kEagerOptimizations);
6201 SetFlagsFromString(kLazyOptimizations);
6207 void Testing::DeoptimizeAll() {
6208 internal::Deoptimizer::DeoptimizeAll();
6212 namespace internal {
6215 void HandleScopeImplementer::FreeThreadResources() {
6220 char* HandleScopeImplementer::ArchiveThread(char* storage) {
6221 v8::ImplementationUtilities::HandleScopeData* current =
6222 isolate_->handle_scope_data();
6223 handle_scope_data_ = *current;
6224 memcpy(storage, this, sizeof(*this));
6226 ResetAfterArchive();
6227 current->Initialize();
6229 return storage + ArchiveSpacePerThread();
6233 int HandleScopeImplementer::ArchiveSpacePerThread() {
6234 return sizeof(HandleScopeImplementer);
6238 char* HandleScopeImplementer::RestoreThread(char* storage) {
6239 memcpy(this, storage, sizeof(*this));
6240 *isolate_->handle_scope_data() = handle_scope_data_;
6241 return storage + ArchiveSpacePerThread();
6245 void HandleScopeImplementer::IterateThis(ObjectVisitor* v) {
6246 // Iterate over all handles in the blocks except for the last.
6247 for (int i = blocks()->length() - 2; i >= 0; --i) {
6248 Object** block = blocks()->at(i);
6249 v->VisitPointers(block, &block[kHandleBlockSize]);
6252 // Iterate over live handles in the last block (if any).
6253 if (!blocks()->is_empty()) {
6254 v->VisitPointers(blocks()->last(), handle_scope_data_.next);
6257 if (!saved_contexts_.is_empty()) {
6258 Object** start = reinterpret_cast<Object**>(&saved_contexts_.first());
6259 v->VisitPointers(start, start + saved_contexts_.length());
6264 void HandleScopeImplementer::Iterate(ObjectVisitor* v) {
6265 v8::ImplementationUtilities::HandleScopeData* current =
6266 isolate_->handle_scope_data();
6267 handle_scope_data_ = *current;
6272 char* HandleScopeImplementer::Iterate(ObjectVisitor* v, char* storage) {
6273 HandleScopeImplementer* scope_implementer =
6274 reinterpret_cast<HandleScopeImplementer*>(storage);
6275 scope_implementer->IterateThis(v);
6276 return storage + ArchiveSpacePerThread();
6279 } } // namespace v8::internal