1// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/api.h"
6
7#include <string.h>  // For memcpy, strlen.
8#ifdef V8_USE_ADDRESS_SANITIZER
9#include <sanitizer/asan_interface.h>
10#endif  // V8_USE_ADDRESS_SANITIZER
11#include <cmath>  // For isnan.
12#include "include/v8-debug.h"
13#include "include/v8-profiler.h"
14#include "include/v8-testing.h"
15#include "src/assert-scope.h"
16#include "src/background-parsing-task.h"
17#include "src/base/platform/platform.h"
18#include "src/base/platform/time.h"
19#include "src/base/utils/random-number-generator.h"
20#include "src/bootstrapper.h"
21#include "src/code-stubs.h"
22#include "src/compiler.h"
23#include "src/conversions-inl.h"
24#include "src/counters.h"
25#include "src/cpu-profiler.h"
26#include "src/debug.h"
27#include "src/deoptimizer.h"
28#include "src/execution.h"
29#include "src/global-handles.h"
30#include "src/heap-profiler.h"
31#include "src/heap-snapshot-generator-inl.h"
32#include "src/icu_util.h"
33#include "src/json-parser.h"
34#include "src/messages.h"
35#include "src/natives.h"
36#include "src/parser.h"
37#include "src/profile-generator-inl.h"
38#include "src/property.h"
39#include "src/property-details.h"
40#include "src/prototype.h"
41#include "src/runtime.h"
42#include "src/runtime-profiler.h"
43#include "src/scanner-character-streams.h"
44#include "src/simulator.h"
45#include "src/snapshot.h"
46#include "src/unicode-inl.h"
47#include "src/v8threads.h"
48#include "src/version.h"
49#include "src/vm-state-inl.h"
50
51
52#define LOG_API(isolate, expr) LOG(isolate, ApiEntryCall(expr))
53
54#define ENTER_V8(isolate)                                          \
55  DCHECK((isolate)->IsInitialized());                              \
56  i::VMState<i::OTHER> __state__((isolate))
57
58namespace v8 {
59
60#define ON_BAILOUT(isolate, location, code)                        \
61  if (IsExecutionTerminatingCheck(isolate)) {                      \
62    code;                                                          \
63    UNREACHABLE();                                                 \
64  }
65
66
67#define EXCEPTION_PREAMBLE(isolate)                                         \
68  (isolate)->handle_scope_implementer()->IncrementCallDepth();              \
69  DCHECK(!(isolate)->external_caught_exception());                          \
70  bool has_pending_exception = false
71
72
73#define EXCEPTION_BAILOUT_CHECK_GENERIC(isolate, value, do_callback)           \
74  do {                                                                         \
75    i::HandleScopeImplementer* handle_scope_implementer =                      \
76        (isolate)->handle_scope_implementer();                                 \
77    handle_scope_implementer->DecrementCallDepth();                            \
78    if (has_pending_exception) {                                               \
79      bool call_depth_is_zero = handle_scope_implementer->CallDepthIsZero();   \
80      (isolate)->OptionalRescheduleException(call_depth_is_zero);              \
81      do_callback                                                              \
82      return value;                                                            \
83    }                                                                          \
84    do_callback                                                                \
85  } while (false)
86
87
88#define EXCEPTION_BAILOUT_CHECK_DO_CALLBACK(isolate, value)                    \
89  EXCEPTION_BAILOUT_CHECK_GENERIC(                                             \
90      isolate, value, isolate->FireCallCompletedCallback();)
91
92
93#define EXCEPTION_BAILOUT_CHECK(isolate, value)                                \
94  EXCEPTION_BAILOUT_CHECK_GENERIC(isolate, value, ;)
95
96
97// --- E x c e p t i o n   B e h a v i o r ---
98
99
100void i::FatalProcessOutOfMemory(const char* location) {
101  i::V8::FatalProcessOutOfMemory(location, false);
102}
103
104
105// When V8 cannot allocated memory FatalProcessOutOfMemory is called.
106// The default fatal error handler is called and execution is stopped.
107void i::V8::FatalProcessOutOfMemory(const char* location, bool take_snapshot) {
108  i::HeapStats heap_stats;
109  int start_marker;
110  heap_stats.start_marker = &start_marker;
111  int new_space_size;
112  heap_stats.new_space_size = &new_space_size;
113  int new_space_capacity;
114  heap_stats.new_space_capacity = &new_space_capacity;
115  intptr_t old_pointer_space_size;
116  heap_stats.old_pointer_space_size = &old_pointer_space_size;
117  intptr_t old_pointer_space_capacity;
118  heap_stats.old_pointer_space_capacity = &old_pointer_space_capacity;
119  intptr_t old_data_space_size;
120  heap_stats.old_data_space_size = &old_data_space_size;
121  intptr_t old_data_space_capacity;
122  heap_stats.old_data_space_capacity = &old_data_space_capacity;
123  intptr_t code_space_size;
124  heap_stats.code_space_size = &code_space_size;
125  intptr_t code_space_capacity;
126  heap_stats.code_space_capacity = &code_space_capacity;
127  intptr_t map_space_size;
128  heap_stats.map_space_size = &map_space_size;
129  intptr_t map_space_capacity;
130  heap_stats.map_space_capacity = &map_space_capacity;
131  intptr_t cell_space_size;
132  heap_stats.cell_space_size = &cell_space_size;
133  intptr_t cell_space_capacity;
134  heap_stats.cell_space_capacity = &cell_space_capacity;
135  intptr_t property_cell_space_size;
136  heap_stats.property_cell_space_size = &property_cell_space_size;
137  intptr_t property_cell_space_capacity;
138  heap_stats.property_cell_space_capacity = &property_cell_space_capacity;
139  intptr_t lo_space_size;
140  heap_stats.lo_space_size = &lo_space_size;
141  int global_handle_count;
142  heap_stats.global_handle_count = &global_handle_count;
143  int weak_global_handle_count;
144  heap_stats.weak_global_handle_count = &weak_global_handle_count;
145  int pending_global_handle_count;
146  heap_stats.pending_global_handle_count = &pending_global_handle_count;
147  int near_death_global_handle_count;
148  heap_stats.near_death_global_handle_count = &near_death_global_handle_count;
149  int free_global_handle_count;
150  heap_stats.free_global_handle_count = &free_global_handle_count;
151  intptr_t memory_allocator_size;
152  heap_stats.memory_allocator_size = &memory_allocator_size;
153  intptr_t memory_allocator_capacity;
154  heap_stats.memory_allocator_capacity = &memory_allocator_capacity;
155  int objects_per_type[LAST_TYPE + 1] = {0};
156  heap_stats.objects_per_type = objects_per_type;
157  int size_per_type[LAST_TYPE + 1] = {0};
158  heap_stats.size_per_type = size_per_type;
159  int os_error;
160  heap_stats.os_error = &os_error;
161  int end_marker;
162  heap_stats.end_marker = &end_marker;
163  i::Isolate* isolate = i::Isolate::Current();
164  if (isolate->heap()->HasBeenSetUp()) {
165    // BUG(1718): Don't use the take_snapshot since we don't support
166    // HeapIterator here without doing a special GC.
167    isolate->heap()->RecordStats(&heap_stats, false);
168  }
169  Utils::ApiCheck(false, location, "Allocation failed - process out of memory");
170  // If the fatal error handler returns, we stop execution.
171  FATAL("API fatal error handler returned after process out of memory");
172}
173
174
175void Utils::ReportApiFailure(const char* location, const char* message) {
176  i::Isolate* isolate = i::Isolate::Current();
177  FatalErrorCallback callback = isolate->exception_behavior();
178  if (callback == NULL) {
179    base::OS::PrintError("\n#\n# Fatal error in %s\n# %s\n#\n\n", location,
180                         message);
181    base::OS::Abort();
182  } else {
183    callback(location, message);
184  }
185  isolate->SignalFatalError();
186}
187
188
189bool V8::IsDead() {
190  i::Isolate* isolate = i::Isolate::Current();
191  return isolate->IsDead();
192}
193
194
195static inline bool IsExecutionTerminatingCheck(i::Isolate* isolate) {
196  if (!isolate->IsInitialized()) return false;
197  if (isolate->has_scheduled_exception()) {
198    return isolate->scheduled_exception() ==
199        isolate->heap()->termination_exception();
200  }
201  return false;
202}
203
204
205StartupDataDecompressor::StartupDataDecompressor()
206    : raw_data(i::NewArray<char*>(V8::GetCompressedStartupDataCount())) {
207  for (int i = 0; i < V8::GetCompressedStartupDataCount(); ++i) {
208    raw_data[i] = NULL;
209  }
210}
211
212
213StartupDataDecompressor::~StartupDataDecompressor() {
214  for (int i = 0; i < V8::GetCompressedStartupDataCount(); ++i) {
215    i::DeleteArray(raw_data[i]);
216  }
217  i::DeleteArray(raw_data);
218}
219
220
221int StartupDataDecompressor::Decompress() {
222  int compressed_data_count = V8::GetCompressedStartupDataCount();
223  StartupData* compressed_data =
224      i::NewArray<StartupData>(compressed_data_count);
225  V8::GetCompressedStartupData(compressed_data);
226  for (int i = 0; i < compressed_data_count; ++i) {
227    char* decompressed = raw_data[i] =
228        i::NewArray<char>(compressed_data[i].raw_size);
229    if (compressed_data[i].compressed_size != 0) {
230      int result = DecompressData(decompressed,
231                                  &compressed_data[i].raw_size,
232                                  compressed_data[i].data,
233                                  compressed_data[i].compressed_size);
234      if (result != 0) return result;
235    } else {
236      DCHECK_EQ(0, compressed_data[i].raw_size);
237    }
238    compressed_data[i].data = decompressed;
239  }
240  V8::SetDecompressedStartupData(compressed_data);
241  i::DeleteArray(compressed_data);
242  return 0;
243}
244
245
246StartupData::CompressionAlgorithm V8::GetCompressedStartupDataAlgorithm() {
247#ifdef COMPRESS_STARTUP_DATA_BZ2
248  return StartupData::kBZip2;
249#else
250  return StartupData::kUncompressed;
251#endif
252}
253
254
255enum CompressedStartupDataItems {
256  kSnapshot = 0,
257  kSnapshotContext,
258  kLibraries,
259  kExperimentalLibraries,
260  kCompressedStartupDataCount
261};
262
263
264int V8::GetCompressedStartupDataCount() {
265#ifdef COMPRESS_STARTUP_DATA_BZ2
266  return kCompressedStartupDataCount;
267#else
268  return 0;
269#endif
270}
271
272
273void V8::GetCompressedStartupData(StartupData* compressed_data) {
274#ifdef COMPRESS_STARTUP_DATA_BZ2
275  compressed_data[kSnapshot].data =
276      reinterpret_cast<const char*>(i::Snapshot::data());
277  compressed_data[kSnapshot].compressed_size = i::Snapshot::size();
278  compressed_data[kSnapshot].raw_size = i::Snapshot::raw_size();
279
280  compressed_data[kSnapshotContext].data =
281      reinterpret_cast<const char*>(i::Snapshot::context_data());
282  compressed_data[kSnapshotContext].compressed_size =
283      i::Snapshot::context_size();
284  compressed_data[kSnapshotContext].raw_size = i::Snapshot::context_raw_size();
285
286  i::Vector<const i::byte> libraries_source = i::Natives::GetScriptsSource();
287  compressed_data[kLibraries].data =
288      reinterpret_cast<const char*>(libraries_source.start());
289  compressed_data[kLibraries].compressed_size = libraries_source.length();
290  compressed_data[kLibraries].raw_size = i::Natives::GetRawScriptsSize();
291
292  i::Vector<const i::byte> exp_libraries_source =
293      i::ExperimentalNatives::GetScriptsSource();
294  compressed_data[kExperimentalLibraries].data =
295      reinterpret_cast<const char*>(exp_libraries_source.start());
296  compressed_data[kExperimentalLibraries].compressed_size =
297      exp_libraries_source.length();
298  compressed_data[kExperimentalLibraries].raw_size =
299      i::ExperimentalNatives::GetRawScriptsSize();
300#endif
301}
302
303
304void V8::SetDecompressedStartupData(StartupData* decompressed_data) {
305#ifdef COMPRESS_STARTUP_DATA_BZ2
306  DCHECK_EQ(i::Snapshot::raw_size(), decompressed_data[kSnapshot].raw_size);
307  i::Snapshot::set_raw_data(
308      reinterpret_cast<const i::byte*>(decompressed_data[kSnapshot].data));
309
310  DCHECK_EQ(i::Snapshot::context_raw_size(),
311            decompressed_data[kSnapshotContext].raw_size);
312  i::Snapshot::set_context_raw_data(
313      reinterpret_cast<const i::byte*>(
314          decompressed_data[kSnapshotContext].data));
315
316  DCHECK_EQ(i::Natives::GetRawScriptsSize(),
317            decompressed_data[kLibraries].raw_size);
318  i::Vector<const char> libraries_source(
319      decompressed_data[kLibraries].data,
320      decompressed_data[kLibraries].raw_size);
321  i::Natives::SetRawScriptsSource(libraries_source);
322
323  DCHECK_EQ(i::ExperimentalNatives::GetRawScriptsSize(),
324            decompressed_data[kExperimentalLibraries].raw_size);
325  i::Vector<const char> exp_libraries_source(
326      decompressed_data[kExperimentalLibraries].data,
327      decompressed_data[kExperimentalLibraries].raw_size);
328  i::ExperimentalNatives::SetRawScriptsSource(exp_libraries_source);
329#endif
330}
331
332
333void V8::SetNativesDataBlob(StartupData* natives_blob) {
334#ifdef V8_USE_EXTERNAL_STARTUP_DATA
335  i::SetNativesFromFile(natives_blob);
336#else
337  CHECK(false);
338#endif
339}
340
341
342void V8::SetSnapshotDataBlob(StartupData* snapshot_blob) {
343#ifdef V8_USE_EXTERNAL_STARTUP_DATA
344  i::SetSnapshotFromFile(snapshot_blob);
345#else
346  CHECK(false);
347#endif
348}
349
350
351void V8::SetFatalErrorHandler(FatalErrorCallback that) {
352  i::Isolate* isolate = i::Isolate::Current();
353  isolate->set_exception_behavior(that);
354}
355
356
357void V8::SetAllowCodeGenerationFromStringsCallback(
358    AllowCodeGenerationFromStringsCallback callback) {
359  i::Isolate* isolate = i::Isolate::Current();
360  isolate->set_allow_code_gen_callback(callback);
361}
362
363
364void V8::SetFlagsFromString(const char* str, int length) {
365  i::FlagList::SetFlagsFromString(str, length);
366}
367
368
369void V8::SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags) {
370  i::FlagList::SetFlagsFromCommandLine(argc, argv, remove_flags);
371}
372
373
374RegisteredExtension* RegisteredExtension::first_extension_ = NULL;
375
376
377RegisteredExtension::RegisteredExtension(Extension* extension)
378    : extension_(extension) { }
379
380
381void RegisteredExtension::Register(RegisteredExtension* that) {
382  that->next_ = first_extension_;
383  first_extension_ = that;
384}
385
386
387void RegisteredExtension::UnregisterAll() {
388  RegisteredExtension* re = first_extension_;
389  while (re != NULL) {
390    RegisteredExtension* next = re->next();
391    delete re;
392    re = next;
393  }
394  first_extension_ = NULL;
395}
396
397
398void RegisterExtension(Extension* that) {
399  RegisteredExtension* extension = new RegisteredExtension(that);
400  RegisteredExtension::Register(extension);
401}
402
403
404Extension::Extension(const char* name,
405                     const char* source,
406                     int dep_count,
407                     const char** deps,
408                     int source_length)
409    : name_(name),
410      source_length_(source_length >= 0 ?
411                     source_length :
412                     (source ? static_cast<int>(strlen(source)) : 0)),
413      source_(source, source_length_),
414      dep_count_(dep_count),
415      deps_(deps),
416      auto_enable_(false) {
417  CHECK(source != NULL || source_length_ == 0);
418}
419
420
421ResourceConstraints::ResourceConstraints()
422    : max_semi_space_size_(0),
423      max_old_space_size_(0),
424      max_executable_size_(0),
425      stack_limit_(NULL),
426      max_available_threads_(0),
427      code_range_size_(0) { }
428
429void ResourceConstraints::ConfigureDefaults(uint64_t physical_memory,
430                                            uint64_t virtual_memory_limit,
431                                            uint32_t number_of_processors) {
432#if V8_OS_ANDROID
433  // Android has higher physical memory requirements before raising the maximum
434  // heap size limits since it has no swap space.
435  const uint64_t low_limit = 512ul * i::MB;
436  const uint64_t medium_limit = 1ul * i::GB;
437  const uint64_t high_limit = 2ul * i::GB;
438#else
439  const uint64_t low_limit = 512ul * i::MB;
440  const uint64_t medium_limit = 768ul * i::MB;
441  const uint64_t high_limit = 1ul  * i::GB;
442#endif
443
444  if (physical_memory <= low_limit) {
445    set_max_semi_space_size(i::Heap::kMaxSemiSpaceSizeLowMemoryDevice);
446    set_max_old_space_size(i::Heap::kMaxOldSpaceSizeLowMemoryDevice);
447    set_max_executable_size(i::Heap::kMaxExecutableSizeLowMemoryDevice);
448  } else if (physical_memory <= medium_limit) {
449    set_max_semi_space_size(i::Heap::kMaxSemiSpaceSizeMediumMemoryDevice);
450    set_max_old_space_size(i::Heap::kMaxOldSpaceSizeMediumMemoryDevice);
451    set_max_executable_size(i::Heap::kMaxExecutableSizeMediumMemoryDevice);
452  } else if (physical_memory <= high_limit) {
453    set_max_semi_space_size(i::Heap::kMaxSemiSpaceSizeHighMemoryDevice);
454    set_max_old_space_size(i::Heap::kMaxOldSpaceSizeHighMemoryDevice);
455    set_max_executable_size(i::Heap::kMaxExecutableSizeHighMemoryDevice);
456  } else {
457    set_max_semi_space_size(i::Heap::kMaxSemiSpaceSizeHugeMemoryDevice);
458    set_max_old_space_size(i::Heap::kMaxOldSpaceSizeHugeMemoryDevice);
459    set_max_executable_size(i::Heap::kMaxExecutableSizeHugeMemoryDevice);
460  }
461
462  set_max_available_threads(i::Max(i::Min(number_of_processors, 4u), 1u));
463
464  if (virtual_memory_limit > 0 && i::kRequiresCodeRange) {
465    // Reserve no more than 1/8 of the memory for the code range, but at most
466    // kMaximalCodeRangeSize.
467    set_code_range_size(
468        i::Min(i::kMaximalCodeRangeSize / i::MB,
469               static_cast<size_t>((virtual_memory_limit >> 3) / i::MB)));
470  }
471}
472
473
474void SetResourceConstraints(i::Isolate* isolate,
475                            const ResourceConstraints& constraints) {
476  int semi_space_size = constraints.max_semi_space_size();
477  int old_space_size = constraints.max_old_space_size();
478  int max_executable_size = constraints.max_executable_size();
479  size_t code_range_size = constraints.code_range_size();
480  if (semi_space_size != 0 || old_space_size != 0 ||
481      max_executable_size != 0 || code_range_size != 0) {
482    isolate->heap()->ConfigureHeap(semi_space_size, old_space_size,
483                                   max_executable_size, code_range_size);
484  }
485  if (constraints.stack_limit() != NULL) {
486    uintptr_t limit = reinterpret_cast<uintptr_t>(constraints.stack_limit());
487    isolate->stack_guard()->SetStackLimit(limit);
488  }
489
490  isolate->set_max_available_threads(constraints.max_available_threads());
491}
492
493
494i::Object** V8::GlobalizeReference(i::Isolate* isolate, i::Object** obj) {
495  LOG_API(isolate, "Persistent::New");
496  i::Handle<i::Object> result = isolate->global_handles()->Create(*obj);
497#ifdef DEBUG
498  (*obj)->ObjectVerify();
499#endif  // DEBUG
500  return result.location();
501}
502
503
504i::Object** V8::CopyPersistent(i::Object** obj) {
505  i::Handle<i::Object> result = i::GlobalHandles::CopyGlobal(obj);
506#ifdef DEBUG
507  (*obj)->ObjectVerify();
508#endif  // DEBUG
509  return result.location();
510}
511
512
513void V8::MakeWeak(i::Object** object,
514                  void* parameters,
515                  WeakCallback weak_callback) {
516  i::GlobalHandles::MakeWeak(object, parameters, weak_callback);
517}
518
519
520void* V8::ClearWeak(i::Object** obj) {
521  return i::GlobalHandles::ClearWeakness(obj);
522}
523
524
525void V8::DisposeGlobal(i::Object** obj) {
526  i::GlobalHandles::Destroy(obj);
527}
528
529
530void V8::Eternalize(Isolate* v8_isolate, Value* value, int* index) {
531  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
532  i::Object* object = *Utils::OpenHandle(value);
533  isolate->eternal_handles()->Create(isolate, object, index);
534}
535
536
537Local<Value> V8::GetEternal(Isolate* v8_isolate, int index) {
538  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
539  return Utils::ToLocal(isolate->eternal_handles()->Get(index));
540}
541
542
543// --- H a n d l e s ---
544
545
546HandleScope::HandleScope(Isolate* isolate) {
547  Initialize(isolate);
548}
549
550
551void HandleScope::Initialize(Isolate* isolate) {
552  i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
553  // We do not want to check the correct usage of the Locker class all over the
554  // place, so we do it only here: Without a HandleScope, an embedder can do
555  // almost nothing, so it is enough to check in this central place.
556  Utils::ApiCheck(!v8::Locker::IsActive() ||
557                  internal_isolate->thread_manager()->IsLockedByCurrentThread(),
558                  "HandleScope::HandleScope",
559                  "Entering the V8 API without proper locking in place");
560  i::HandleScopeData* current = internal_isolate->handle_scope_data();
561  isolate_ = internal_isolate;
562  prev_next_ = current->next;
563  prev_limit_ = current->limit;
564  current->level++;
565}
566
567
568HandleScope::~HandleScope() {
569  i::HandleScope::CloseScope(isolate_, prev_next_, prev_limit_);
570}
571
572
573int HandleScope::NumberOfHandles(Isolate* isolate) {
574  return i::HandleScope::NumberOfHandles(
575      reinterpret_cast<i::Isolate*>(isolate));
576}
577
578
579i::Object** HandleScope::CreateHandle(i::Isolate* isolate, i::Object* value) {
580  return i::HandleScope::CreateHandle(isolate, value);
581}
582
583
584i::Object** HandleScope::CreateHandle(i::HeapObject* heap_object,
585                                      i::Object* value) {
586  DCHECK(heap_object->IsHeapObject());
587  return i::HandleScope::CreateHandle(heap_object->GetIsolate(), value);
588}
589
590
591EscapableHandleScope::EscapableHandleScope(Isolate* v8_isolate) {
592  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
593  escape_slot_ = CreateHandle(isolate, isolate->heap()->the_hole_value());
594  Initialize(v8_isolate);
595}
596
597
598i::Object** EscapableHandleScope::Escape(i::Object** escape_value) {
599  i::Heap* heap = reinterpret_cast<i::Isolate*>(GetIsolate())->heap();
600  Utils::ApiCheck(*escape_slot_ == heap->the_hole_value(),
601                  "EscapeableHandleScope::Escape",
602                  "Escape value set twice");
603  if (escape_value == NULL) {
604    *escape_slot_ = heap->undefined_value();
605    return NULL;
606  }
607  *escape_slot_ = *escape_value;
608  return escape_slot_;
609}
610
611
612void Context::Enter() {
613  i::Handle<i::Context> env = Utils::OpenHandle(this);
614  i::Isolate* isolate = env->GetIsolate();
615  ENTER_V8(isolate);
616  i::HandleScopeImplementer* impl = isolate->handle_scope_implementer();
617  impl->EnterContext(env);
618  impl->SaveContext(isolate->context());
619  isolate->set_context(*env);
620}
621
622
623void Context::Exit() {
624  i::Handle<i::Context> env = Utils::OpenHandle(this);
625  i::Isolate* isolate = env->GetIsolate();
626  ENTER_V8(isolate);
627  i::HandleScopeImplementer* impl = isolate->handle_scope_implementer();
628  if (!Utils::ApiCheck(impl->LastEnteredContextWas(env),
629                       "v8::Context::Exit()",
630                       "Cannot exit non-entered context")) {
631    return;
632  }
633  impl->LeaveContext();
634  isolate->set_context(impl->RestoreContext());
635}
636
637
638static void* DecodeSmiToAligned(i::Object* value, const char* location) {
639  Utils::ApiCheck(value->IsSmi(), location, "Not a Smi");
640  return reinterpret_cast<void*>(value);
641}
642
643
644static i::Smi* EncodeAlignedAsSmi(void* value, const char* location) {
645  i::Smi* smi = reinterpret_cast<i::Smi*>(value);
646  Utils::ApiCheck(smi->IsSmi(), location, "Pointer is not aligned");
647  return smi;
648}
649
650
651static i::Handle<i::FixedArray> EmbedderDataFor(Context* context,
652                                                int index,
653                                                bool can_grow,
654                                                const char* location) {
655  i::Handle<i::Context> env = Utils::OpenHandle(context);
656  bool ok =
657      Utils::ApiCheck(env->IsNativeContext(),
658                      location,
659                      "Not a native context") &&
660      Utils::ApiCheck(index >= 0, location, "Negative index");
661  if (!ok) return i::Handle<i::FixedArray>();
662  i::Handle<i::FixedArray> data(env->embedder_data());
663  if (index < data->length()) return data;
664  if (!Utils::ApiCheck(can_grow, location, "Index too large")) {
665    return i::Handle<i::FixedArray>();
666  }
667  int new_size = i::Max(index, data->length() << 1) + 1;
668  data = i::FixedArray::CopySize(data, new_size);
669  env->set_embedder_data(*data);
670  return data;
671}
672
673
674v8::Local<v8::Value> Context::SlowGetEmbedderData(int index) {
675  const char* location = "v8::Context::GetEmbedderData()";
676  i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, false, location);
677  if (data.is_null()) return Local<Value>();
678  i::Handle<i::Object> result(data->get(index), data->GetIsolate());
679  return Utils::ToLocal(result);
680}
681
682
683void Context::SetEmbedderData(int index, v8::Handle<Value> value) {
684  const char* location = "v8::Context::SetEmbedderData()";
685  i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, true, location);
686  if (data.is_null()) return;
687  i::Handle<i::Object> val = Utils::OpenHandle(*value);
688  data->set(index, *val);
689  DCHECK_EQ(*Utils::OpenHandle(*value),
690            *Utils::OpenHandle(*GetEmbedderData(index)));
691}
692
693
694void* Context::SlowGetAlignedPointerFromEmbedderData(int index) {
695  const char* location = "v8::Context::GetAlignedPointerFromEmbedderData()";
696  i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, false, location);
697  if (data.is_null()) return NULL;
698  return DecodeSmiToAligned(data->get(index), location);
699}
700
701
702void Context::SetAlignedPointerInEmbedderData(int index, void* value) {
703  const char* location = "v8::Context::SetAlignedPointerInEmbedderData()";
704  i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, true, location);
705  data->set(index, EncodeAlignedAsSmi(value, location));
706  DCHECK_EQ(value, GetAlignedPointerFromEmbedderData(index));
707}
708
709
710// --- N e a n d e r ---
711
712
713// A constructor cannot easily return an error value, therefore it is necessary
714// to check for a dead VM with ON_BAILOUT before constructing any Neander
715// objects.  To remind you about this there is no HandleScope in the
716// NeanderObject constructor.  When you add one to the site calling the
717// constructor you should check that you ensured the VM was not dead first.
718NeanderObject::NeanderObject(v8::internal::Isolate* isolate, int size) {
719  ENTER_V8(isolate);
720  value_ = isolate->factory()->NewNeanderObject();
721  i::Handle<i::FixedArray> elements = isolate->factory()->NewFixedArray(size);
722  value_->set_elements(*elements);
723}
724
725
726int NeanderObject::size() {
727  return i::FixedArray::cast(value_->elements())->length();
728}
729
730
731NeanderArray::NeanderArray(v8::internal::Isolate* isolate) : obj_(isolate, 2) {
732  obj_.set(0, i::Smi::FromInt(0));
733}
734
735
736int NeanderArray::length() {
737  return i::Smi::cast(obj_.get(0))->value();
738}
739
740
741i::Object* NeanderArray::get(int offset) {
742  DCHECK(0 <= offset);
743  DCHECK(offset < length());
744  return obj_.get(offset + 1);
745}
746
747
748// This method cannot easily return an error value, therefore it is necessary
749// to check for a dead VM with ON_BAILOUT before calling it.  To remind you
750// about this there is no HandleScope in this method.  When you add one to the
751// site calling this method you should check that you ensured the VM was not
752// dead first.
753void NeanderArray::add(i::Handle<i::Object> value) {
754  int length = this->length();
755  int size = obj_.size();
756  if (length == size - 1) {
757    i::Factory* factory = i::Isolate::Current()->factory();
758    i::Handle<i::FixedArray> new_elms = factory->NewFixedArray(2 * size);
759    for (int i = 0; i < length; i++)
760      new_elms->set(i + 1, get(i));
761    obj_.value()->set_elements(*new_elms);
762  }
763  obj_.set(length + 1, *value);
764  obj_.set(0, i::Smi::FromInt(length + 1));
765}
766
767
768void NeanderArray::set(int index, i::Object* value) {
769  if (index < 0 || index >= this->length()) return;
770  obj_.set(index + 1, value);
771}
772
773
774// --- T e m p l a t e ---
775
776
777static void InitializeTemplate(i::Handle<i::TemplateInfo> that, int type) {
778  that->set_tag(i::Smi::FromInt(type));
779}
780
781
782static void TemplateSet(i::Isolate* isolate,
783                        v8::Template* templ,
784                        int length,
785                        v8::Handle<v8::Data>* data) {
786  i::Handle<i::Object> list(Utils::OpenHandle(templ)->property_list(), isolate);
787  if (list->IsUndefined()) {
788    list = NeanderArray(isolate).value();
789    Utils::OpenHandle(templ)->set_property_list(*list);
790  }
791  NeanderArray array(list);
792  array.add(isolate->factory()->NewNumberFromInt(length));
793  for (int i = 0; i < length; i++) {
794    i::Handle<i::Object> value = data[i].IsEmpty() ?
795        i::Handle<i::Object>(isolate->factory()->undefined_value()) :
796        Utils::OpenHandle(*data[i]);
797    array.add(value);
798  }
799}
800
801
802void Template::Set(v8::Handle<Name> name,
803                   v8::Handle<Data> value,
804                   v8::PropertyAttribute attribute) {
805  i::Isolate* isolate = i::Isolate::Current();
806  ENTER_V8(isolate);
807  i::HandleScope scope(isolate);
808  const int kSize = 3;
809  v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
810  v8::Handle<v8::Data> data[kSize] = {
811    name,
812    value,
813    v8::Integer::New(v8_isolate, attribute)};
814  TemplateSet(isolate, this, kSize, data);
815}
816
817
818void Template::SetAccessorProperty(
819    v8::Local<v8::Name> name,
820    v8::Local<FunctionTemplate> getter,
821    v8::Local<FunctionTemplate> setter,
822    v8::PropertyAttribute attribute,
823    v8::AccessControl access_control) {
824  // TODO(verwaest): Remove |access_control|.
825  DCHECK_EQ(v8::DEFAULT, access_control);
826  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
827  ENTER_V8(isolate);
828  DCHECK(!name.IsEmpty());
829  DCHECK(!getter.IsEmpty() || !setter.IsEmpty());
830  i::HandleScope scope(isolate);
831  const int kSize = 5;
832  v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
833  v8::Handle<v8::Data> data[kSize] = {
834    name,
835    getter,
836    setter,
837    v8::Integer::New(v8_isolate, attribute)};
838  TemplateSet(isolate, this, kSize, data);
839}
840
841
842// --- F u n c t i o n   T e m p l a t e ---
843static void InitializeFunctionTemplate(
844    i::Handle<i::FunctionTemplateInfo> info) {
845  info->set_tag(i::Smi::FromInt(Consts::FUNCTION_TEMPLATE));
846  info->set_flag(0);
847}
848
849
850Local<ObjectTemplate> FunctionTemplate::PrototypeTemplate() {
851  i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate();
852  ENTER_V8(i_isolate);
853  i::Handle<i::Object> result(Utils::OpenHandle(this)->prototype_template(),
854                              i_isolate);
855  if (result->IsUndefined()) {
856    v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(i_isolate);
857    result = Utils::OpenHandle(*ObjectTemplate::New(isolate));
858    Utils::OpenHandle(this)->set_prototype_template(*result);
859  }
860  return ToApiHandle<ObjectTemplate>(result);
861}
862
863
864void FunctionTemplate::Inherit(v8::Handle<FunctionTemplate> value) {
865  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
866  ENTER_V8(isolate);
867  Utils::OpenHandle(this)->set_parent_template(*Utils::OpenHandle(*value));
868}
869
870
871static Local<FunctionTemplate> FunctionTemplateNew(
872    i::Isolate* isolate,
873    FunctionCallback callback,
874    v8::Handle<Value> data,
875    v8::Handle<Signature> signature,
876    int length,
877    bool do_not_cache) {
878  i::Handle<i::Struct> struct_obj =
879      isolate->factory()->NewStruct(i::FUNCTION_TEMPLATE_INFO_TYPE);
880  i::Handle<i::FunctionTemplateInfo> obj =
881      i::Handle<i::FunctionTemplateInfo>::cast(struct_obj);
882  InitializeFunctionTemplate(obj);
883  obj->set_do_not_cache(do_not_cache);
884  int next_serial_number = 0;
885  if (!do_not_cache) {
886    next_serial_number = isolate->next_serial_number() + 1;
887    isolate->set_next_serial_number(next_serial_number);
888  }
889  obj->set_serial_number(i::Smi::FromInt(next_serial_number));
890  if (callback != 0) {
891    if (data.IsEmpty()) {
892      data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
893    }
894    Utils::ToLocal(obj)->SetCallHandler(callback, data);
895  }
896  obj->set_length(length);
897  obj->set_undetectable(false);
898  obj->set_needs_access_check(false);
899  if (!signature.IsEmpty())
900    obj->set_signature(*Utils::OpenHandle(*signature));
901  return Utils::ToLocal(obj);
902}
903
904Local<FunctionTemplate> FunctionTemplate::New(
905    Isolate* isolate,
906    FunctionCallback callback,
907    v8::Handle<Value> data,
908    v8::Handle<Signature> signature,
909    int length) {
910  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
911  LOG_API(i_isolate, "FunctionTemplate::New");
912  ENTER_V8(i_isolate);
913  return FunctionTemplateNew(
914      i_isolate, callback, data, signature, length, false);
915}
916
917
918Local<Signature> Signature::New(Isolate* isolate,
919                                Handle<FunctionTemplate> receiver, int argc,
920                                Handle<FunctionTemplate> argv[]) {
921  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
922  LOG_API(i_isolate, "Signature::New");
923  ENTER_V8(i_isolate);
924  i::Handle<i::Struct> struct_obj =
925      i_isolate->factory()->NewStruct(i::SIGNATURE_INFO_TYPE);
926  i::Handle<i::SignatureInfo> obj =
927      i::Handle<i::SignatureInfo>::cast(struct_obj);
928  if (!receiver.IsEmpty()) obj->set_receiver(*Utils::OpenHandle(*receiver));
929  if (argc > 0) {
930    i::Handle<i::FixedArray> args = i_isolate->factory()->NewFixedArray(argc);
931    for (int i = 0; i < argc; i++) {
932      if (!argv[i].IsEmpty())
933        args->set(i, *Utils::OpenHandle(*argv[i]));
934    }
935    obj->set_args(*args);
936  }
937  return Utils::ToLocal(obj);
938}
939
940
941Local<AccessorSignature> AccessorSignature::New(
942    Isolate* isolate,
943    Handle<FunctionTemplate> receiver) {
944  return Utils::AccessorSignatureToLocal(Utils::OpenHandle(*receiver));
945}
946
947
948template<typename Operation>
949static Local<Operation> NewDescriptor(
950    Isolate* isolate,
951    const i::DeclaredAccessorDescriptorData& data,
952    Data* previous_descriptor) {
953  i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
954  i::Handle<i::DeclaredAccessorDescriptor> previous =
955      i::Handle<i::DeclaredAccessorDescriptor>();
956  if (previous_descriptor != NULL) {
957    previous = Utils::OpenHandle(
958        static_cast<DeclaredAccessorDescriptor*>(previous_descriptor));
959  }
960  i::Handle<i::DeclaredAccessorDescriptor> descriptor =
961      i::DeclaredAccessorDescriptor::Create(internal_isolate, data, previous);
962  return Utils::Convert<i::DeclaredAccessorDescriptor, Operation>(descriptor);
963}
964
965
966Local<RawOperationDescriptor>
967ObjectOperationDescriptor::NewInternalFieldDereference(
968    Isolate* isolate,
969    int internal_field) {
970  i::DeclaredAccessorDescriptorData data;
971  data.type = i::kDescriptorObjectDereference;
972  data.object_dereference_descriptor.internal_field = internal_field;
973  return NewDescriptor<RawOperationDescriptor>(isolate, data, NULL);
974}
975
976
977Local<RawOperationDescriptor> RawOperationDescriptor::NewRawShift(
978    Isolate* isolate,
979    int16_t byte_offset) {
980  i::DeclaredAccessorDescriptorData data;
981  data.type = i::kDescriptorPointerShift;
982  data.pointer_shift_descriptor.byte_offset = byte_offset;
983  return NewDescriptor<RawOperationDescriptor>(isolate, data, this);
984}
985
986
987Local<DeclaredAccessorDescriptor> RawOperationDescriptor::NewHandleDereference(
988    Isolate* isolate) {
989  i::DeclaredAccessorDescriptorData data;
990  data.type = i::kDescriptorReturnObject;
991  return NewDescriptor<DeclaredAccessorDescriptor>(isolate, data, this);
992}
993
994
995Local<RawOperationDescriptor> RawOperationDescriptor::NewRawDereference(
996    Isolate* isolate) {
997  i::DeclaredAccessorDescriptorData data;
998  data.type = i::kDescriptorPointerDereference;
999  return NewDescriptor<RawOperationDescriptor>(isolate, data, this);
1000}
1001
1002
1003Local<DeclaredAccessorDescriptor> RawOperationDescriptor::NewPointerCompare(
1004    Isolate* isolate,
1005    void* compare_value) {
1006  i::DeclaredAccessorDescriptorData data;
1007  data.type = i::kDescriptorPointerCompare;
1008  data.pointer_compare_descriptor.compare_value = compare_value;
1009  return NewDescriptor<DeclaredAccessorDescriptor>(isolate, data, this);
1010}
1011
1012
1013Local<DeclaredAccessorDescriptor> RawOperationDescriptor::NewPrimitiveValue(
1014    Isolate* isolate,
1015    DeclaredAccessorDescriptorDataType data_type,
1016    uint8_t bool_offset) {
1017  i::DeclaredAccessorDescriptorData data;
1018  data.type = i::kDescriptorPrimitiveValue;
1019  data.primitive_value_descriptor.data_type = data_type;
1020  data.primitive_value_descriptor.bool_offset = bool_offset;
1021  return NewDescriptor<DeclaredAccessorDescriptor>(isolate, data, this);
1022}
1023
1024
1025template<typename T>
1026static Local<DeclaredAccessorDescriptor> NewBitmaskCompare(
1027    Isolate* isolate,
1028    T bitmask,
1029    T compare_value,
1030    RawOperationDescriptor* operation) {
1031  i::DeclaredAccessorDescriptorData data;
1032  data.type = i::kDescriptorBitmaskCompare;
1033  data.bitmask_compare_descriptor.bitmask = bitmask;
1034  data.bitmask_compare_descriptor.compare_value = compare_value;
1035  data.bitmask_compare_descriptor.size = sizeof(T);
1036  return NewDescriptor<DeclaredAccessorDescriptor>(isolate, data, operation);
1037}
1038
1039
1040Local<DeclaredAccessorDescriptor> RawOperationDescriptor::NewBitmaskCompare8(
1041    Isolate* isolate,
1042    uint8_t bitmask,
1043    uint8_t compare_value) {
1044  return NewBitmaskCompare(isolate, bitmask, compare_value, this);
1045}
1046
1047
1048Local<DeclaredAccessorDescriptor> RawOperationDescriptor::NewBitmaskCompare16(
1049    Isolate* isolate,
1050    uint16_t bitmask,
1051    uint16_t compare_value) {
1052  return NewBitmaskCompare(isolate, bitmask, compare_value, this);
1053}
1054
1055
1056Local<DeclaredAccessorDescriptor> RawOperationDescriptor::NewBitmaskCompare32(
1057    Isolate* isolate,
1058    uint32_t bitmask,
1059    uint32_t compare_value) {
1060  return NewBitmaskCompare(isolate, bitmask, compare_value, this);
1061}
1062
1063
1064Local<TypeSwitch> TypeSwitch::New(Handle<FunctionTemplate> type) {
1065  Handle<FunctionTemplate> types[1] = { type };
1066  return TypeSwitch::New(1, types);
1067}
1068
1069
1070Local<TypeSwitch> TypeSwitch::New(int argc, Handle<FunctionTemplate> types[]) {
1071  i::Isolate* isolate = i::Isolate::Current();
1072  LOG_API(isolate, "TypeSwitch::New");
1073  ENTER_V8(isolate);
1074  i::Handle<i::FixedArray> vector = isolate->factory()->NewFixedArray(argc);
1075  for (int i = 0; i < argc; i++)
1076    vector->set(i, *Utils::OpenHandle(*types[i]));
1077  i::Handle<i::Struct> struct_obj =
1078      isolate->factory()->NewStruct(i::TYPE_SWITCH_INFO_TYPE);
1079  i::Handle<i::TypeSwitchInfo> obj =
1080      i::Handle<i::TypeSwitchInfo>::cast(struct_obj);
1081  obj->set_types(*vector);
1082  return Utils::ToLocal(obj);
1083}
1084
1085
1086int TypeSwitch::match(v8::Handle<Value> value) {
1087  i::Isolate* isolate = i::Isolate::Current();
1088  LOG_API(isolate, "TypeSwitch::match");
1089  USE(isolate);
1090  i::Handle<i::Object> obj = Utils::OpenHandle(*value);
1091  i::Handle<i::TypeSwitchInfo> info = Utils::OpenHandle(this);
1092  i::FixedArray* types = i::FixedArray::cast(info->types());
1093  for (int i = 0; i < types->length(); i++) {
1094    if (i::FunctionTemplateInfo::cast(types->get(i))->IsTemplateFor(*obj))
1095      return i + 1;
1096  }
1097  return 0;
1098}
1099
1100
1101#define SET_FIELD_WRAPPED(obj, setter, cdata) do {                      \
1102    i::Handle<i::Object> foreign = FromCData(obj->GetIsolate(), cdata); \
1103    (obj)->setter(*foreign);                                            \
1104  } while (false)
1105
1106
1107void FunctionTemplate::SetCallHandler(FunctionCallback callback,
1108                                      v8::Handle<Value> data) {
1109  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1110  ENTER_V8(isolate);
1111  i::HandleScope scope(isolate);
1112  i::Handle<i::Struct> struct_obj =
1113      isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE);
1114  i::Handle<i::CallHandlerInfo> obj =
1115      i::Handle<i::CallHandlerInfo>::cast(struct_obj);
1116  SET_FIELD_WRAPPED(obj, set_callback, callback);
1117  if (data.IsEmpty()) {
1118    data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1119  }
1120  obj->set_data(*Utils::OpenHandle(*data));
1121  Utils::OpenHandle(this)->set_call_code(*obj);
1122}
1123
1124
1125static i::Handle<i::AccessorInfo> SetAccessorInfoProperties(
1126    i::Handle<i::AccessorInfo> obj,
1127    v8::Handle<Name> name,
1128    v8::AccessControl settings,
1129    v8::PropertyAttribute attributes,
1130    v8::Handle<AccessorSignature> signature) {
1131  obj->set_name(*Utils::OpenHandle(*name));
1132  if (settings & ALL_CAN_READ) obj->set_all_can_read(true);
1133  if (settings & ALL_CAN_WRITE) obj->set_all_can_write(true);
1134  obj->set_property_attributes(static_cast<PropertyAttributes>(attributes));
1135  if (!signature.IsEmpty()) {
1136    obj->set_expected_receiver_type(*Utils::OpenHandle(*signature));
1137  }
1138  return obj;
1139}
1140
1141
1142template<typename Getter, typename Setter>
1143static i::Handle<i::AccessorInfo> MakeAccessorInfo(
1144    v8::Handle<Name> name,
1145    Getter getter,
1146    Setter setter,
1147    v8::Handle<Value> data,
1148    v8::AccessControl settings,
1149    v8::PropertyAttribute attributes,
1150    v8::Handle<AccessorSignature> signature) {
1151  i::Isolate* isolate = Utils::OpenHandle(*name)->GetIsolate();
1152  i::Handle<i::ExecutableAccessorInfo> obj =
1153      isolate->factory()->NewExecutableAccessorInfo();
1154  SET_FIELD_WRAPPED(obj, set_getter, getter);
1155  SET_FIELD_WRAPPED(obj, set_setter, setter);
1156  if (data.IsEmpty()) {
1157    data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1158  }
1159  obj->set_data(*Utils::OpenHandle(*data));
1160  return SetAccessorInfoProperties(obj, name, settings, attributes, signature);
1161}
1162
1163
1164static i::Handle<i::AccessorInfo> MakeAccessorInfo(
1165    v8::Handle<Name> name,
1166    v8::Handle<v8::DeclaredAccessorDescriptor> descriptor,
1167    void* setter_ignored,
1168    void* data_ignored,
1169    v8::AccessControl settings,
1170    v8::PropertyAttribute attributes,
1171    v8::Handle<AccessorSignature> signature) {
1172  i::Isolate* isolate = Utils::OpenHandle(*name)->GetIsolate();
1173  if (descriptor.IsEmpty()) return i::Handle<i::DeclaredAccessorInfo>();
1174  i::Handle<i::DeclaredAccessorInfo> obj =
1175      isolate->factory()->NewDeclaredAccessorInfo();
1176  obj->set_descriptor(*Utils::OpenHandle(*descriptor));
1177  return SetAccessorInfoProperties(obj, name, settings, attributes, signature);
1178}
1179
1180
1181Local<ObjectTemplate> FunctionTemplate::InstanceTemplate() {
1182  i::Handle<i::FunctionTemplateInfo> handle = Utils::OpenHandle(this, true);
1183  if (!Utils::ApiCheck(!handle.is_null(),
1184                       "v8::FunctionTemplate::InstanceTemplate()",
1185                       "Reading from empty handle")) {
1186    return Local<ObjectTemplate>();
1187  }
1188  i::Isolate* isolate = handle->GetIsolate();
1189  ENTER_V8(isolate);
1190  if (handle->instance_template()->IsUndefined()) {
1191    Local<ObjectTemplate> templ =
1192        ObjectTemplate::New(isolate, ToApiHandle<FunctionTemplate>(handle));
1193    handle->set_instance_template(*Utils::OpenHandle(*templ));
1194  }
1195  i::Handle<i::ObjectTemplateInfo> result(
1196      i::ObjectTemplateInfo::cast(handle->instance_template()));
1197  return Utils::ToLocal(result);
1198}
1199
1200
1201void FunctionTemplate::SetLength(int length) {
1202  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1203  ENTER_V8(isolate);
1204  Utils::OpenHandle(this)->set_length(length);
1205}
1206
1207
1208void FunctionTemplate::SetClassName(Handle<String> name) {
1209  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1210  ENTER_V8(isolate);
1211  Utils::OpenHandle(this)->set_class_name(*Utils::OpenHandle(*name));
1212}
1213
1214
1215void FunctionTemplate::SetHiddenPrototype(bool value) {
1216  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1217  ENTER_V8(isolate);
1218  Utils::OpenHandle(this)->set_hidden_prototype(value);
1219}
1220
1221
1222void FunctionTemplate::ReadOnlyPrototype() {
1223  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1224  ENTER_V8(isolate);
1225  Utils::OpenHandle(this)->set_read_only_prototype(true);
1226}
1227
1228
1229void FunctionTemplate::RemovePrototype() {
1230  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1231  ENTER_V8(isolate);
1232  Utils::OpenHandle(this)->set_remove_prototype(true);
1233}
1234
1235
1236// --- O b j e c t T e m p l a t e ---
1237
1238
1239Local<ObjectTemplate> ObjectTemplate::New(Isolate* isolate) {
1240  return New(reinterpret_cast<i::Isolate*>(isolate), Local<FunctionTemplate>());
1241}
1242
1243
1244Local<ObjectTemplate> ObjectTemplate::New() {
1245  return New(i::Isolate::Current(), Local<FunctionTemplate>());
1246}
1247
1248
1249Local<ObjectTemplate> ObjectTemplate::New(
1250    i::Isolate* isolate,
1251    v8::Handle<FunctionTemplate> constructor) {
1252  LOG_API(isolate, "ObjectTemplate::New");
1253  ENTER_V8(isolate);
1254  i::Handle<i::Struct> struct_obj =
1255      isolate->factory()->NewStruct(i::OBJECT_TEMPLATE_INFO_TYPE);
1256  i::Handle<i::ObjectTemplateInfo> obj =
1257      i::Handle<i::ObjectTemplateInfo>::cast(struct_obj);
1258  InitializeTemplate(obj, Consts::OBJECT_TEMPLATE);
1259  if (!constructor.IsEmpty())
1260    obj->set_constructor(*Utils::OpenHandle(*constructor));
1261  obj->set_internal_field_count(i::Smi::FromInt(0));
1262  return Utils::ToLocal(obj);
1263}
1264
1265
1266// Ensure that the object template has a constructor.  If no
1267// constructor is available we create one.
1268static i::Handle<i::FunctionTemplateInfo> EnsureConstructor(
1269    i::Isolate* isolate,
1270    ObjectTemplate* object_template) {
1271  i::Object* obj = Utils::OpenHandle(object_template)->constructor();
1272  if (!obj ->IsUndefined()) {
1273    i::FunctionTemplateInfo* info = i::FunctionTemplateInfo::cast(obj);
1274    return i::Handle<i::FunctionTemplateInfo>(info, isolate);
1275  }
1276  Local<FunctionTemplate> templ =
1277      FunctionTemplate::New(reinterpret_cast<Isolate*>(isolate));
1278  i::Handle<i::FunctionTemplateInfo> constructor = Utils::OpenHandle(*templ);
1279  constructor->set_instance_template(*Utils::OpenHandle(object_template));
1280  Utils::OpenHandle(object_template)->set_constructor(*constructor);
1281  return constructor;
1282}
1283
1284
1285static inline void AddPropertyToTemplate(
1286    i::Handle<i::TemplateInfo> info,
1287    i::Handle<i::AccessorInfo> obj) {
1288  i::Isolate* isolate = info->GetIsolate();
1289  i::Handle<i::Object> list(info->property_accessors(), isolate);
1290  if (list->IsUndefined()) {
1291    list = NeanderArray(isolate).value();
1292    info->set_property_accessors(*list);
1293  }
1294  NeanderArray array(list);
1295  array.add(obj);
1296}
1297
1298
1299static inline i::Handle<i::TemplateInfo> GetTemplateInfo(
1300    i::Isolate* isolate,
1301    Template* template_obj) {
1302  return Utils::OpenHandle(template_obj);
1303}
1304
1305
1306// TODO(dcarney): remove this with ObjectTemplate::SetAccessor
1307static inline i::Handle<i::TemplateInfo> GetTemplateInfo(
1308    i::Isolate* isolate,
1309    ObjectTemplate* object_template) {
1310  EnsureConstructor(isolate, object_template);
1311  return Utils::OpenHandle(object_template);
1312}
1313
1314
1315template<typename Getter, typename Setter, typename Data, typename Template>
1316static bool TemplateSetAccessor(
1317    Template* template_obj,
1318    v8::Local<Name> name,
1319    Getter getter,
1320    Setter setter,
1321    Data data,
1322    AccessControl settings,
1323    PropertyAttribute attribute,
1324    v8::Local<AccessorSignature> signature) {
1325  i::Isolate* isolate = Utils::OpenHandle(template_obj)->GetIsolate();
1326  ENTER_V8(isolate);
1327  i::HandleScope scope(isolate);
1328  i::Handle<i::AccessorInfo> obj = MakeAccessorInfo(
1329      name, getter, setter, data, settings, attribute, signature);
1330  if (obj.is_null()) return false;
1331  i::Handle<i::TemplateInfo> info = GetTemplateInfo(isolate, template_obj);
1332  AddPropertyToTemplate(info, obj);
1333  return true;
1334}
1335
1336
1337bool Template::SetDeclaredAccessor(
1338    Local<Name> name,
1339    Local<DeclaredAccessorDescriptor> descriptor,
1340    PropertyAttribute attribute,
1341    Local<AccessorSignature> signature,
1342    AccessControl settings) {
1343  void* null = NULL;
1344  return TemplateSetAccessor(
1345      this, name, descriptor, null, null, settings, attribute, signature);
1346}
1347
1348
1349void Template::SetNativeDataProperty(v8::Local<String> name,
1350                                     AccessorGetterCallback getter,
1351                                     AccessorSetterCallback setter,
1352                                     v8::Handle<Value> data,
1353                                     PropertyAttribute attribute,
1354                                     v8::Local<AccessorSignature> signature,
1355                                     AccessControl settings) {
1356  TemplateSetAccessor(
1357      this, name, getter, setter, data, settings, attribute, signature);
1358}
1359
1360
1361void Template::SetNativeDataProperty(v8::Local<Name> name,
1362                                     AccessorNameGetterCallback getter,
1363                                     AccessorNameSetterCallback setter,
1364                                     v8::Handle<Value> data,
1365                                     PropertyAttribute attribute,
1366                                     v8::Local<AccessorSignature> signature,
1367                                     AccessControl settings) {
1368  TemplateSetAccessor(
1369      this, name, getter, setter, data, settings, attribute, signature);
1370}
1371
1372
1373void ObjectTemplate::SetAccessor(v8::Handle<String> name,
1374                                 AccessorGetterCallback getter,
1375                                 AccessorSetterCallback setter,
1376                                 v8::Handle<Value> data,
1377                                 AccessControl settings,
1378                                 PropertyAttribute attribute,
1379                                 v8::Handle<AccessorSignature> signature) {
1380  TemplateSetAccessor(
1381      this, name, getter, setter, data, settings, attribute, signature);
1382}
1383
1384
1385void ObjectTemplate::SetAccessor(v8::Handle<Name> name,
1386                                 AccessorNameGetterCallback getter,
1387                                 AccessorNameSetterCallback setter,
1388                                 v8::Handle<Value> data,
1389                                 AccessControl settings,
1390                                 PropertyAttribute attribute,
1391                                 v8::Handle<AccessorSignature> signature) {
1392  TemplateSetAccessor(
1393      this, name, getter, setter, data, settings, attribute, signature);
1394}
1395
1396
1397void ObjectTemplate::SetNamedPropertyHandler(
1398    NamedPropertyGetterCallback getter,
1399    NamedPropertySetterCallback setter,
1400    NamedPropertyQueryCallback query,
1401    NamedPropertyDeleterCallback remover,
1402    NamedPropertyEnumeratorCallback enumerator,
1403    Handle<Value> data) {
1404  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1405  ENTER_V8(isolate);
1406  i::HandleScope scope(isolate);
1407  EnsureConstructor(isolate, this);
1408  i::FunctionTemplateInfo* constructor = i::FunctionTemplateInfo::cast(
1409      Utils::OpenHandle(this)->constructor());
1410  i::Handle<i::FunctionTemplateInfo> cons(constructor);
1411  i::Handle<i::Struct> struct_obj =
1412      isolate->factory()->NewStruct(i::INTERCEPTOR_INFO_TYPE);
1413  i::Handle<i::InterceptorInfo> obj =
1414      i::Handle<i::InterceptorInfo>::cast(struct_obj);
1415
1416  if (getter != 0) SET_FIELD_WRAPPED(obj, set_getter, getter);
1417  if (setter != 0) SET_FIELD_WRAPPED(obj, set_setter, setter);
1418  if (query != 0) SET_FIELD_WRAPPED(obj, set_query, query);
1419  if (remover != 0) SET_FIELD_WRAPPED(obj, set_deleter, remover);
1420  if (enumerator != 0) SET_FIELD_WRAPPED(obj, set_enumerator, enumerator);
1421
1422  if (data.IsEmpty()) {
1423    data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1424  }
1425  obj->set_data(*Utils::OpenHandle(*data));
1426  cons->set_named_property_handler(*obj);
1427}
1428
1429
1430void ObjectTemplate::MarkAsUndetectable() {
1431  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1432  ENTER_V8(isolate);
1433  i::HandleScope scope(isolate);
1434  EnsureConstructor(isolate, this);
1435  i::FunctionTemplateInfo* constructor =
1436      i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1437  i::Handle<i::FunctionTemplateInfo> cons(constructor);
1438  cons->set_undetectable(true);
1439}
1440
1441
1442void ObjectTemplate::SetAccessCheckCallbacks(
1443    NamedSecurityCallback named_callback,
1444    IndexedSecurityCallback indexed_callback,
1445    Handle<Value> data,
1446    bool turned_on_by_default) {
1447  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1448  ENTER_V8(isolate);
1449  i::HandleScope scope(isolate);
1450  EnsureConstructor(isolate, this);
1451
1452  i::Handle<i::Struct> struct_info =
1453      isolate->factory()->NewStruct(i::ACCESS_CHECK_INFO_TYPE);
1454  i::Handle<i::AccessCheckInfo> info =
1455      i::Handle<i::AccessCheckInfo>::cast(struct_info);
1456
1457  SET_FIELD_WRAPPED(info, set_named_callback, named_callback);
1458  SET_FIELD_WRAPPED(info, set_indexed_callback, indexed_callback);
1459
1460  if (data.IsEmpty()) {
1461    data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1462  }
1463  info->set_data(*Utils::OpenHandle(*data));
1464
1465  i::FunctionTemplateInfo* constructor =
1466      i::FunctionTemplateInfo::cast(Utils::OpenHandle(this)->constructor());
1467  i::Handle<i::FunctionTemplateInfo> cons(constructor);
1468  cons->set_access_check_info(*info);
1469  cons->set_needs_access_check(turned_on_by_default);
1470}
1471
1472
1473void ObjectTemplate::SetIndexedPropertyHandler(
1474    IndexedPropertyGetterCallback getter,
1475    IndexedPropertySetterCallback setter,
1476    IndexedPropertyQueryCallback query,
1477    IndexedPropertyDeleterCallback remover,
1478    IndexedPropertyEnumeratorCallback enumerator,
1479    Handle<Value> data) {
1480  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1481  ENTER_V8(isolate);
1482  i::HandleScope scope(isolate);
1483  EnsureConstructor(isolate, this);
1484  i::FunctionTemplateInfo* constructor = i::FunctionTemplateInfo::cast(
1485      Utils::OpenHandle(this)->constructor());
1486  i::Handle<i::FunctionTemplateInfo> cons(constructor);
1487  i::Handle<i::Struct> struct_obj =
1488      isolate->factory()->NewStruct(i::INTERCEPTOR_INFO_TYPE);
1489  i::Handle<i::InterceptorInfo> obj =
1490      i::Handle<i::InterceptorInfo>::cast(struct_obj);
1491
1492  if (getter != 0) SET_FIELD_WRAPPED(obj, set_getter, getter);
1493  if (setter != 0) SET_FIELD_WRAPPED(obj, set_setter, setter);
1494  if (query != 0) SET_FIELD_WRAPPED(obj, set_query, query);
1495  if (remover != 0) SET_FIELD_WRAPPED(obj, set_deleter, remover);
1496  if (enumerator != 0) SET_FIELD_WRAPPED(obj, set_enumerator, enumerator);
1497
1498  if (data.IsEmpty()) {
1499    data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1500  }
1501  obj->set_data(*Utils::OpenHandle(*data));
1502  cons->set_indexed_property_handler(*obj);
1503}
1504
1505
1506void ObjectTemplate::SetCallAsFunctionHandler(FunctionCallback callback,
1507                                              Handle<Value> data) {
1508  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1509  ENTER_V8(isolate);
1510  i::HandleScope scope(isolate);
1511  EnsureConstructor(isolate, this);
1512  i::FunctionTemplateInfo* constructor = i::FunctionTemplateInfo::cast(
1513      Utils::OpenHandle(this)->constructor());
1514  i::Handle<i::FunctionTemplateInfo> cons(constructor);
1515  i::Handle<i::Struct> struct_obj =
1516      isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE);
1517  i::Handle<i::CallHandlerInfo> obj =
1518      i::Handle<i::CallHandlerInfo>::cast(struct_obj);
1519  SET_FIELD_WRAPPED(obj, set_callback, callback);
1520  if (data.IsEmpty()) {
1521    data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1522  }
1523  obj->set_data(*Utils::OpenHandle(*data));
1524  cons->set_instance_call_handler(*obj);
1525}
1526
1527
1528int ObjectTemplate::InternalFieldCount() {
1529  return i::Smi::cast(Utils::OpenHandle(this)->internal_field_count())->value();
1530}
1531
1532
1533void ObjectTemplate::SetInternalFieldCount(int value) {
1534  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1535  if (!Utils::ApiCheck(i::Smi::IsValid(value),
1536                       "v8::ObjectTemplate::SetInternalFieldCount()",
1537                       "Invalid internal field count")) {
1538    return;
1539  }
1540  ENTER_V8(isolate);
1541  if (value > 0) {
1542    // The internal field count is set by the constructor function's
1543    // construct code, so we ensure that there is a constructor
1544    // function to do the setting.
1545    EnsureConstructor(isolate, this);
1546  }
1547  Utils::OpenHandle(this)->set_internal_field_count(i::Smi::FromInt(value));
1548}
1549
1550
1551// --- S c r i p t s ---
1552
1553
1554// Internally, UnboundScript is a SharedFunctionInfo, and Script is a
1555// JSFunction.
1556
1557ScriptCompiler::CachedData::CachedData(const uint8_t* data_, int length_,
1558                                       BufferPolicy buffer_policy_)
1559    : data(data_), length(length_), buffer_policy(buffer_policy_) {}
1560
1561
1562ScriptCompiler::CachedData::~CachedData() {
1563  if (buffer_policy == BufferOwned) {
1564    delete[] data;
1565  }
1566}
1567
1568
1569ScriptCompiler::StreamedSource::StreamedSource(ExternalSourceStream* stream,
1570                                               Encoding encoding)
1571    : impl_(new i::StreamedSource(stream, encoding)) {}
1572
1573
1574ScriptCompiler::StreamedSource::~StreamedSource() { delete impl_; }
1575
1576
1577const ScriptCompiler::CachedData*
1578ScriptCompiler::StreamedSource::GetCachedData() const {
1579  return impl_->cached_data.get();
1580}
1581
1582
1583Local<Script> UnboundScript::BindToCurrentContext() {
1584  i::Handle<i::HeapObject> obj =
1585      i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this));
1586  i::Handle<i::SharedFunctionInfo>
1587      function_info(i::SharedFunctionInfo::cast(*obj), obj->GetIsolate());
1588  i::Handle<i::JSFunction> function =
1589      obj->GetIsolate()->factory()->NewFunctionFromSharedFunctionInfo(
1590          function_info, obj->GetIsolate()->global_context());
1591  return ToApiHandle<Script>(function);
1592}
1593
1594
1595int UnboundScript::GetId() {
1596  i::Handle<i::HeapObject> obj =
1597      i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this));
1598  i::Isolate* isolate = obj->GetIsolate();
1599  ON_BAILOUT(isolate, "v8::UnboundScript::GetId()", return -1);
1600  LOG_API(isolate, "v8::UnboundScript::GetId");
1601  {
1602    i::HandleScope scope(isolate);
1603    i::Handle<i::SharedFunctionInfo> function_info(
1604        i::SharedFunctionInfo::cast(*obj));
1605    i::Handle<i::Script> script(i::Script::cast(function_info->script()));
1606    return script->id()->value();
1607  }
1608}
1609
1610
1611int UnboundScript::GetLineNumber(int code_pos) {
1612  i::Handle<i::SharedFunctionInfo> obj =
1613      i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
1614  i::Isolate* isolate = obj->GetIsolate();
1615  ON_BAILOUT(isolate, "v8::UnboundScript::GetLineNumber()", return -1);
1616  LOG_API(isolate, "UnboundScript::GetLineNumber");
1617  if (obj->script()->IsScript()) {
1618    i::Handle<i::Script> script(i::Script::cast(obj->script()));
1619    return i::Script::GetLineNumber(script, code_pos);
1620  } else {
1621    return -1;
1622  }
1623}
1624
1625
1626Handle<Value> UnboundScript::GetScriptName() {
1627  i::Handle<i::SharedFunctionInfo> obj =
1628      i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
1629  i::Isolate* isolate = obj->GetIsolate();
1630  ON_BAILOUT(isolate, "v8::UnboundScript::GetName()",
1631             return Handle<String>());
1632  LOG_API(isolate, "UnboundScript::GetName");
1633  if (obj->script()->IsScript()) {
1634    i::Object* name = i::Script::cast(obj->script())->name();
1635    return Utils::ToLocal(i::Handle<i::Object>(name, isolate));
1636  } else {
1637    return Handle<String>();
1638  }
1639}
1640
1641
1642Handle<Value> UnboundScript::GetSourceURL() {
1643  i::Handle<i::SharedFunctionInfo> obj =
1644      i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
1645  i::Isolate* isolate = obj->GetIsolate();
1646  ON_BAILOUT(isolate, "v8::UnboundScript::GetSourceURL()",
1647             return Handle<String>());
1648  LOG_API(isolate, "UnboundScript::GetSourceURL");
1649  if (obj->script()->IsScript()) {
1650    i::Object* url = i::Script::cast(obj->script())->source_url();
1651    return Utils::ToLocal(i::Handle<i::Object>(url, isolate));
1652  } else {
1653    return Handle<String>();
1654  }
1655}
1656
1657
1658Handle<Value> UnboundScript::GetSourceMappingURL() {
1659  i::Handle<i::SharedFunctionInfo> obj =
1660      i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
1661  i::Isolate* isolate = obj->GetIsolate();
1662  ON_BAILOUT(isolate, "v8::UnboundScript::GetSourceMappingURL()",
1663             return Handle<String>());
1664  LOG_API(isolate, "UnboundScript::GetSourceMappingURL");
1665  if (obj->script()->IsScript()) {
1666    i::Object* url = i::Script::cast(obj->script())->source_mapping_url();
1667    return Utils::ToLocal(i::Handle<i::Object>(url, isolate));
1668  } else {
1669    return Handle<String>();
1670  }
1671}
1672
1673
1674Local<Value> Script::Run() {
1675  i::Handle<i::Object> obj = Utils::OpenHandle(this, true);
1676  // If execution is terminating, Compile(..)->Run() requires this
1677  // check.
1678  if (obj.is_null()) return Local<Value>();
1679  i::Isolate* isolate = i::Handle<i::HeapObject>::cast(obj)->GetIsolate();
1680  ON_BAILOUT(isolate, "v8::Script::Run()", return Local<Value>());
1681  LOG_API(isolate, "Script::Run");
1682  ENTER_V8(isolate);
1683  i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
1684  i::HandleScope scope(isolate);
1685  i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(obj);
1686  EXCEPTION_PREAMBLE(isolate);
1687  i::Handle<i::Object> receiver(isolate->global_proxy(), isolate);
1688  i::Handle<i::Object> result;
1689  has_pending_exception = !i::Execution::Call(
1690      isolate, fun, receiver, 0, NULL).ToHandle(&result);
1691  EXCEPTION_BAILOUT_CHECK_DO_CALLBACK(isolate, Local<Value>());
1692  return Utils::ToLocal(scope.CloseAndEscape(result));
1693}
1694
1695
1696Local<UnboundScript> Script::GetUnboundScript() {
1697  i::Handle<i::Object> obj = Utils::OpenHandle(this);
1698  return ToApiHandle<UnboundScript>(
1699      i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared()));
1700}
1701
1702
1703Local<UnboundScript> ScriptCompiler::CompileUnbound(
1704    Isolate* v8_isolate,
1705    Source* source,
1706    CompileOptions options) {
1707  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
1708  ON_BAILOUT(isolate, "v8::ScriptCompiler::CompileUnbound()",
1709             return Local<UnboundScript>());
1710
1711  // Support the old API for a transition period:
1712  // - kProduceToCache -> kProduceParserCache
1713  // - kNoCompileOptions + cached_data != NULL -> kConsumeParserCache
1714  if (options == kProduceDataToCache) {
1715    options = kProduceParserCache;
1716  } else if (options == kNoCompileOptions && source->cached_data) {
1717    options = kConsumeParserCache;
1718  }
1719
1720  i::ScriptData* script_data = NULL;
1721  if (options == kConsumeParserCache || options == kConsumeCodeCache) {
1722    DCHECK(source->cached_data);
1723    // ScriptData takes care of pointer-aligning the data.
1724    script_data = new i::ScriptData(source->cached_data->data,
1725                                    source->cached_data->length);
1726  }
1727
1728  i::Handle<i::String> str = Utils::OpenHandle(*(source->source_string));
1729  LOG_API(isolate, "ScriptCompiler::CompileUnbound");
1730  ENTER_V8(isolate);
1731  i::SharedFunctionInfo* raw_result = NULL;
1732  { i::HandleScope scope(isolate);
1733    i::Handle<i::Object> name_obj;
1734    int line_offset = 0;
1735    int column_offset = 0;
1736    bool is_shared_cross_origin = false;
1737    if (!source->resource_name.IsEmpty()) {
1738      name_obj = Utils::OpenHandle(*(source->resource_name));
1739    }
1740    if (!source->resource_line_offset.IsEmpty()) {
1741      line_offset = static_cast<int>(source->resource_line_offset->Value());
1742    }
1743    if (!source->resource_column_offset.IsEmpty()) {
1744      column_offset =
1745          static_cast<int>(source->resource_column_offset->Value());
1746    }
1747    if (!source->resource_is_shared_cross_origin.IsEmpty()) {
1748      v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
1749      is_shared_cross_origin =
1750          source->resource_is_shared_cross_origin == v8::True(v8_isolate);
1751    }
1752    EXCEPTION_PREAMBLE(isolate);
1753    i::Handle<i::SharedFunctionInfo> result = i::Compiler::CompileScript(
1754        str, name_obj, line_offset, column_offset, is_shared_cross_origin,
1755        isolate->global_context(), NULL, &script_data, options,
1756        i::NOT_NATIVES_CODE);
1757    has_pending_exception = result.is_null();
1758    if (has_pending_exception && script_data != NULL) {
1759      // This case won't happen during normal operation; we have compiled
1760      // successfully and produced cached data, and but the second compilation
1761      // of the same source code fails.
1762      delete script_data;
1763      script_data = NULL;
1764    }
1765    EXCEPTION_BAILOUT_CHECK(isolate, Local<UnboundScript>());
1766    raw_result = *result;
1767
1768    if ((options == kProduceParserCache || options == kProduceCodeCache) &&
1769        script_data != NULL) {
1770      // script_data now contains the data that was generated. source will
1771      // take the ownership.
1772      source->cached_data = new CachedData(
1773          script_data->data(), script_data->length(), CachedData::BufferOwned);
1774      script_data->ReleaseDataOwnership();
1775    }
1776    delete script_data;
1777  }
1778  i::Handle<i::SharedFunctionInfo> result(raw_result, isolate);
1779  return ToApiHandle<UnboundScript>(result);
1780}
1781
1782
1783Local<Script> ScriptCompiler::Compile(
1784    Isolate* v8_isolate,
1785    Source* source,
1786    CompileOptions options) {
1787  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
1788  ON_BAILOUT(isolate, "v8::ScriptCompiler::Compile()", return Local<Script>());
1789  LOG_API(isolate, "ScriptCompiler::CompiletBound()");
1790  ENTER_V8(isolate);
1791  Local<UnboundScript> generic = CompileUnbound(v8_isolate, source, options);
1792  if (generic.IsEmpty()) return Local<Script>();
1793  return generic->BindToCurrentContext();
1794}
1795
1796
1797ScriptCompiler::ScriptStreamingTask* ScriptCompiler::StartStreamingScript(
1798    Isolate* v8_isolate, StreamedSource* source, CompileOptions options) {
1799  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
1800  if (!isolate->global_context().is_null() &&
1801      !isolate->global_context()->IsNativeContext()) {
1802    // The context chain is non-trivial, and constructing the corresponding
1803    // non-trivial Scope chain outside the V8 heap is not implemented. Don't
1804    // stream the script. This will only occur if Harmony scoping is enabled and
1805    // a previous script has introduced "let" or "const" variables. TODO(marja):
1806    // Implement externalizing ScopeInfos and constructing non-trivial Scope
1807    // chains independent of the V8 heap so that we can stream also in this
1808    // case.
1809    return NULL;
1810  }
1811  return new i::BackgroundParsingTask(source->impl(), options,
1812                                      i::FLAG_stack_size, isolate);
1813}
1814
1815
1816Local<Script> ScriptCompiler::Compile(Isolate* v8_isolate,
1817                                      StreamedSource* v8_source,
1818                                      Handle<String> full_source_string,
1819                                      const ScriptOrigin& origin) {
1820  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
1821  i::StreamedSource* source = v8_source->impl();
1822  ON_BAILOUT(isolate, "v8::ScriptCompiler::Compile()", return Local<Script>());
1823  LOG_API(isolate, "ScriptCompiler::Compile()");
1824  ENTER_V8(isolate);
1825  i::SharedFunctionInfo* raw_result = NULL;
1826
1827  {
1828    i::HandleScope scope(isolate);
1829    i::Handle<i::String> str = Utils::OpenHandle(*(full_source_string));
1830    i::Handle<i::Script> script = isolate->factory()->NewScript(str);
1831    if (!origin.ResourceName().IsEmpty()) {
1832      script->set_name(*Utils::OpenHandle(*(origin.ResourceName())));
1833    }
1834    if (!origin.ResourceLineOffset().IsEmpty()) {
1835      script->set_line_offset(i::Smi::FromInt(
1836          static_cast<int>(origin.ResourceLineOffset()->Value())));
1837    }
1838    if (!origin.ResourceColumnOffset().IsEmpty()) {
1839      script->set_column_offset(i::Smi::FromInt(
1840          static_cast<int>(origin.ResourceColumnOffset()->Value())));
1841    }
1842    if (!origin.ResourceIsSharedCrossOrigin().IsEmpty()) {
1843      script->set_is_shared_cross_origin(origin.ResourceIsSharedCrossOrigin() ==
1844                                         v8::True(v8_isolate));
1845    }
1846    source->info->set_script(script);
1847    source->info->SetContext(isolate->global_context());
1848
1849    EXCEPTION_PREAMBLE(isolate);
1850
1851    // Do the parsing tasks which need to be done on the main thread. This will
1852    // also handle parse errors.
1853    source->parser->Internalize();
1854
1855    i::Handle<i::SharedFunctionInfo> result =
1856        i::Handle<i::SharedFunctionInfo>::null();
1857    if (source->info->function() != NULL) {
1858      // Parsing has succeeded.
1859      result =
1860          i::Compiler::CompileStreamedScript(source->info.get(), str->length());
1861    }
1862    has_pending_exception = result.is_null();
1863    if (has_pending_exception) isolate->ReportPendingMessages();
1864    EXCEPTION_BAILOUT_CHECK(isolate, Local<Script>());
1865
1866    raw_result = *result;
1867    // The Handle<Script> will go out of scope soon; make sure CompilationInfo
1868    // doesn't point to it.
1869    source->info->set_script(i::Handle<i::Script>());
1870  }  // HandleScope goes out of scope.
1871  i::Handle<i::SharedFunctionInfo> result(raw_result, isolate);
1872  Local<UnboundScript> generic = ToApiHandle<UnboundScript>(result);
1873  if (generic.IsEmpty()) {
1874    return Local<Script>();
1875  }
1876  return generic->BindToCurrentContext();
1877}
1878
1879
1880Local<Script> Script::Compile(v8::Handle<String> source,
1881                              v8::ScriptOrigin* origin) {
1882  i::Handle<i::String> str = Utils::OpenHandle(*source);
1883  if (origin) {
1884    ScriptCompiler::Source script_source(source, *origin);
1885    return ScriptCompiler::Compile(
1886        reinterpret_cast<v8::Isolate*>(str->GetIsolate()),
1887        &script_source);
1888  }
1889  ScriptCompiler::Source script_source(source);
1890  return ScriptCompiler::Compile(
1891      reinterpret_cast<v8::Isolate*>(str->GetIsolate()),
1892      &script_source);
1893}
1894
1895
1896Local<Script> Script::Compile(v8::Handle<String> source,
1897                              v8::Handle<String> file_name) {
1898  ScriptOrigin origin(file_name);
1899  return Compile(source, &origin);
1900}
1901
1902
1903// --- E x c e p t i o n s ---
1904
1905
1906v8::TryCatch::TryCatch()
1907    : isolate_(i::Isolate::Current()),
1908      next_(isolate_->try_catch_handler()),
1909      is_verbose_(false),
1910      can_continue_(true),
1911      capture_message_(true),
1912      rethrow_(false),
1913      has_terminated_(false) {
1914  ResetInternal();
1915  // Special handling for simulators which have a separate JS stack.
1916  js_stack_comparable_address_ =
1917      reinterpret_cast<void*>(v8::internal::SimulatorStack::RegisterCTryCatch(
1918          v8::internal::GetCurrentStackPosition()));
1919  isolate_->RegisterTryCatchHandler(this);
1920}
1921
1922
1923v8::TryCatch::~TryCatch() {
1924  DCHECK(isolate_ == i::Isolate::Current());
1925  if (rethrow_) {
1926    v8::Isolate* isolate = reinterpret_cast<Isolate*>(isolate_);
1927    v8::HandleScope scope(isolate);
1928    v8::Local<v8::Value> exc = v8::Local<v8::Value>::New(isolate, Exception());
1929    if (HasCaught() && capture_message_) {
1930      // If an exception was caught and rethrow_ is indicated, the saved
1931      // message, script, and location need to be restored to Isolate TLS
1932      // for reuse.  capture_message_ needs to be disabled so that DoThrow()
1933      // does not create a new message.
1934      isolate_->thread_local_top()->rethrowing_message_ = true;
1935      isolate_->RestorePendingMessageFromTryCatch(this);
1936    }
1937    isolate_->UnregisterTryCatchHandler(this);
1938    v8::internal::SimulatorStack::UnregisterCTryCatch();
1939    reinterpret_cast<Isolate*>(isolate_)->ThrowException(exc);
1940    DCHECK(!isolate_->thread_local_top()->rethrowing_message_);
1941  } else {
1942    if (HasCaught() && isolate_->has_scheduled_exception()) {
1943      // If an exception was caught but is still scheduled because no API call
1944      // promoted it, then it is canceled to prevent it from being propagated.
1945      // Note that this will not cancel termination exceptions.
1946      isolate_->CancelScheduledExceptionFromTryCatch(this);
1947    }
1948    isolate_->UnregisterTryCatchHandler(this);
1949    v8::internal::SimulatorStack::UnregisterCTryCatch();
1950  }
1951}
1952
1953
1954bool v8::TryCatch::HasCaught() const {
1955  return !reinterpret_cast<i::Object*>(exception_)->IsTheHole();
1956}
1957
1958
1959bool v8::TryCatch::CanContinue() const {
1960  return can_continue_;
1961}
1962
1963
1964bool v8::TryCatch::HasTerminated() const {
1965  return has_terminated_;
1966}
1967
1968
1969v8::Handle<v8::Value> v8::TryCatch::ReThrow() {
1970  if (!HasCaught()) return v8::Local<v8::Value>();
1971  rethrow_ = true;
1972  return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate_));
1973}
1974
1975
1976v8::Local<Value> v8::TryCatch::Exception() const {
1977  DCHECK(isolate_ == i::Isolate::Current());
1978  if (HasCaught()) {
1979    // Check for out of memory exception.
1980    i::Object* exception = reinterpret_cast<i::Object*>(exception_);
1981    return v8::Utils::ToLocal(i::Handle<i::Object>(exception, isolate_));
1982  } else {
1983    return v8::Local<Value>();
1984  }
1985}
1986
1987
1988v8::Local<Value> v8::TryCatch::StackTrace() const {
1989  DCHECK(isolate_ == i::Isolate::Current());
1990  if (HasCaught()) {
1991    i::Object* raw_obj = reinterpret_cast<i::Object*>(exception_);
1992    if (!raw_obj->IsJSObject()) return v8::Local<Value>();
1993    i::HandleScope scope(isolate_);
1994    i::Handle<i::JSObject> obj(i::JSObject::cast(raw_obj), isolate_);
1995    i::Handle<i::String> name = isolate_->factory()->stack_string();
1996    EXCEPTION_PREAMBLE(isolate_);
1997    Maybe<bool> maybe = i::JSReceiver::HasProperty(obj, name);
1998    has_pending_exception = !maybe.has_value;
1999    EXCEPTION_BAILOUT_CHECK(isolate_, v8::Local<Value>());
2000    if (!maybe.value) return v8::Local<Value>();
2001    i::Handle<i::Object> value;
2002    if (!i::Object::GetProperty(obj, name).ToHandle(&value)) {
2003      return v8::Local<Value>();
2004    }
2005    return v8::Utils::ToLocal(scope.CloseAndEscape(value));
2006  } else {
2007    return v8::Local<Value>();
2008  }
2009}
2010
2011
2012v8::Local<v8::Message> v8::TryCatch::Message() const {
2013  DCHECK(isolate_ == i::Isolate::Current());
2014  i::Object* message = reinterpret_cast<i::Object*>(message_obj_);
2015  DCHECK(message->IsJSMessageObject() || message->IsTheHole());
2016  if (HasCaught() && !message->IsTheHole()) {
2017    return v8::Utils::MessageToLocal(i::Handle<i::Object>(message, isolate_));
2018  } else {
2019    return v8::Local<v8::Message>();
2020  }
2021}
2022
2023
2024void v8::TryCatch::Reset() {
2025  DCHECK(isolate_ == i::Isolate::Current());
2026  if (!rethrow_ && HasCaught() && isolate_->has_scheduled_exception()) {
2027    // If an exception was caught but is still scheduled because no API call
2028    // promoted it, then it is canceled to prevent it from being propagated.
2029    // Note that this will not cancel termination exceptions.
2030    isolate_->CancelScheduledExceptionFromTryCatch(this);
2031  }
2032  ResetInternal();
2033}
2034
2035
2036void v8::TryCatch::ResetInternal() {
2037  i::Object* the_hole = isolate_->heap()->the_hole_value();
2038  exception_ = the_hole;
2039  message_obj_ = the_hole;
2040  message_script_ = the_hole;
2041  message_start_pos_ = 0;
2042  message_end_pos_ = 0;
2043}
2044
2045
2046void v8::TryCatch::SetVerbose(bool value) {
2047  is_verbose_ = value;
2048}
2049
2050
2051void v8::TryCatch::SetCaptureMessage(bool value) {
2052  capture_message_ = value;
2053}
2054
2055
2056// --- M e s s a g e ---
2057
2058
2059Local<String> Message::Get() const {
2060  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2061  ON_BAILOUT(isolate, "v8::Message::Get()", return Local<String>());
2062  ENTER_V8(isolate);
2063  EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
2064  i::Handle<i::Object> obj = Utils::OpenHandle(this);
2065  i::Handle<i::String> raw_result = i::MessageHandler::GetMessage(isolate, obj);
2066  Local<String> result = Utils::ToLocal(raw_result);
2067  return scope.Escape(result);
2068}
2069
2070
2071ScriptOrigin Message::GetScriptOrigin() const {
2072  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2073  i::Handle<i::JSMessageObject> message =
2074      i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
2075  i::Handle<i::Object> script_wraper =
2076      i::Handle<i::Object>(message->script(), isolate);
2077  i::Handle<i::JSValue> script_value =
2078      i::Handle<i::JSValue>::cast(script_wraper);
2079  i::Handle<i::Script> script(i::Script::cast(script_value->value()));
2080  i::Handle<i::Object> scriptName(i::Script::GetNameOrSourceURL(script));
2081  v8::Isolate* v8_isolate =
2082      reinterpret_cast<v8::Isolate*>(script->GetIsolate());
2083  v8::ScriptOrigin origin(
2084      Utils::ToLocal(scriptName),
2085      v8::Integer::New(v8_isolate, script->line_offset()->value()),
2086      v8::Integer::New(v8_isolate, script->column_offset()->value()),
2087      Handle<Boolean>(),
2088      v8::Integer::New(v8_isolate, script->id()->value()));
2089  return origin;
2090}
2091
2092
2093v8::Handle<Value> Message::GetScriptResourceName() const {
2094  return GetScriptOrigin().ResourceName();
2095}
2096
2097
2098v8::Handle<v8::StackTrace> Message::GetStackTrace() const {
2099  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2100  ENTER_V8(isolate);
2101  EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
2102  i::Handle<i::JSMessageObject> message =
2103      i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
2104  i::Handle<i::Object> stackFramesObj(message->stack_frames(), isolate);
2105  if (!stackFramesObj->IsJSArray()) return v8::Handle<v8::StackTrace>();
2106  i::Handle<i::JSArray> stackTrace =
2107      i::Handle<i::JSArray>::cast(stackFramesObj);
2108  return scope.Escape(Utils::StackTraceToLocal(stackTrace));
2109}
2110
2111
2112MUST_USE_RESULT static i::MaybeHandle<i::Object> CallV8HeapFunction(
2113    const char* name,
2114    i::Handle<i::Object> recv,
2115    int argc,
2116    i::Handle<i::Object> argv[]) {
2117  i::Isolate* isolate = i::Isolate::Current();
2118  i::Handle<i::Object> object_fun =
2119      i::Object::GetProperty(
2120          isolate, isolate->js_builtins_object(), name).ToHandleChecked();
2121  i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(object_fun);
2122  return i::Execution::Call(isolate, fun, recv, argc, argv);
2123}
2124
2125
2126MUST_USE_RESULT static i::MaybeHandle<i::Object> CallV8HeapFunction(
2127    const char* name,
2128    i::Handle<i::Object> data) {
2129  i::Handle<i::Object> argv[] = { data };
2130  return CallV8HeapFunction(name,
2131                            i::Isolate::Current()->js_builtins_object(),
2132                            arraysize(argv),
2133                            argv);
2134}
2135
2136
2137int Message::GetLineNumber() const {
2138  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2139  ON_BAILOUT(isolate, "v8::Message::GetLineNumber()", return kNoLineNumberInfo);
2140  ENTER_V8(isolate);
2141  i::HandleScope scope(isolate);
2142
2143  EXCEPTION_PREAMBLE(isolate);
2144  i::Handle<i::Object> result;
2145  has_pending_exception = !CallV8HeapFunction(
2146      "GetLineNumber", Utils::OpenHandle(this)).ToHandle(&result);
2147  EXCEPTION_BAILOUT_CHECK(isolate, 0);
2148  return static_cast<int>(result->Number());
2149}
2150
2151
2152int Message::GetStartPosition() const {
2153  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2154  ENTER_V8(isolate);
2155  i::HandleScope scope(isolate);
2156  i::Handle<i::JSMessageObject> message =
2157      i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
2158  return message->start_position();
2159}
2160
2161
2162int Message::GetEndPosition() const {
2163  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2164  ENTER_V8(isolate);
2165  i::HandleScope scope(isolate);
2166  i::Handle<i::JSMessageObject> message =
2167      i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
2168  return message->end_position();
2169}
2170
2171
2172int Message::GetStartColumn() const {
2173  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2174  ON_BAILOUT(isolate, "v8::Message::GetStartColumn()", return kNoColumnInfo);
2175  ENTER_V8(isolate);
2176  i::HandleScope scope(isolate);
2177  i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this);
2178  EXCEPTION_PREAMBLE(isolate);
2179  i::Handle<i::Object> start_col_obj;
2180  has_pending_exception = !CallV8HeapFunction(
2181      "GetPositionInLine", data_obj).ToHandle(&start_col_obj);
2182  EXCEPTION_BAILOUT_CHECK(isolate, 0);
2183  return static_cast<int>(start_col_obj->Number());
2184}
2185
2186
2187int Message::GetEndColumn() const {
2188  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2189  ON_BAILOUT(isolate, "v8::Message::GetEndColumn()", return kNoColumnInfo);
2190  ENTER_V8(isolate);
2191  i::HandleScope scope(isolate);
2192  i::Handle<i::JSObject> data_obj = Utils::OpenHandle(this);
2193  EXCEPTION_PREAMBLE(isolate);
2194  i::Handle<i::Object> start_col_obj;
2195  has_pending_exception = !CallV8HeapFunction(
2196      "GetPositionInLine", data_obj).ToHandle(&start_col_obj);
2197  EXCEPTION_BAILOUT_CHECK(isolate, 0);
2198  i::Handle<i::JSMessageObject> message =
2199      i::Handle<i::JSMessageObject>::cast(data_obj);
2200  int start = message->start_position();
2201  int end = message->end_position();
2202  return static_cast<int>(start_col_obj->Number()) + (end - start);
2203}
2204
2205
2206bool Message::IsSharedCrossOrigin() const {
2207  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2208  ENTER_V8(isolate);
2209  i::HandleScope scope(isolate);
2210  i::Handle<i::JSMessageObject> message =
2211      i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
2212  i::Handle<i::JSValue> script =
2213      i::Handle<i::JSValue>::cast(i::Handle<i::Object>(message->script(),
2214                                                       isolate));
2215  return i::Script::cast(script->value())->is_shared_cross_origin();
2216}
2217
2218
2219Local<String> Message::GetSourceLine() const {
2220  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2221  ON_BAILOUT(isolate, "v8::Message::GetSourceLine()", return Local<String>());
2222  ENTER_V8(isolate);
2223  EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
2224  EXCEPTION_PREAMBLE(isolate);
2225  i::Handle<i::Object> result;
2226  has_pending_exception = !CallV8HeapFunction(
2227      "GetSourceLine", Utils::OpenHandle(this)).ToHandle(&result);
2228  EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::String>());
2229  if (result->IsString()) {
2230    return scope.Escape(Utils::ToLocal(i::Handle<i::String>::cast(result)));
2231  } else {
2232    return Local<String>();
2233  }
2234}
2235
2236
2237void Message::PrintCurrentStackTrace(Isolate* isolate, FILE* out) {
2238  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2239  ENTER_V8(i_isolate);
2240  i_isolate->PrintCurrentStackTrace(out);
2241}
2242
2243
2244// --- S t a c k T r a c e ---
2245
2246Local<StackFrame> StackTrace::GetFrame(uint32_t index) const {
2247  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2248  ENTER_V8(isolate);
2249  EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
2250  i::Handle<i::JSArray> self = Utils::OpenHandle(this);
2251  i::Handle<i::Object> obj =
2252      i::Object::GetElement(isolate, self, index).ToHandleChecked();
2253  i::Handle<i::JSObject> jsobj = i::Handle<i::JSObject>::cast(obj);
2254  return scope.Escape(Utils::StackFrameToLocal(jsobj));
2255}
2256
2257
2258int StackTrace::GetFrameCount() const {
2259  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2260  ENTER_V8(isolate);
2261  return i::Smi::cast(Utils::OpenHandle(this)->length())->value();
2262}
2263
2264
2265Local<Array> StackTrace::AsArray() {
2266  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2267  ENTER_V8(isolate);
2268  return Utils::ToLocal(Utils::OpenHandle(this));
2269}
2270
2271
2272Local<StackTrace> StackTrace::CurrentStackTrace(
2273    Isolate* isolate,
2274    int frame_limit,
2275    StackTraceOptions options) {
2276  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2277  ENTER_V8(i_isolate);
2278  // TODO(dcarney): remove when ScriptDebugServer is fixed.
2279  options = static_cast<StackTraceOptions>(
2280      static_cast<int>(options) | kExposeFramesAcrossSecurityOrigins);
2281  i::Handle<i::JSArray> stackTrace =
2282      i_isolate->CaptureCurrentStackTrace(frame_limit, options);
2283  return Utils::StackTraceToLocal(stackTrace);
2284}
2285
2286
2287// --- S t a c k F r a m e ---
2288
2289static int getIntProperty(const StackFrame* f, const char* propertyName,
2290                          int defaultValue) {
2291  i::Isolate* isolate = Utils::OpenHandle(f)->GetIsolate();
2292  ENTER_V8(isolate);
2293  i::HandleScope scope(isolate);
2294  i::Handle<i::JSObject> self = Utils::OpenHandle(f);
2295  i::Handle<i::Object> obj =
2296      i::Object::GetProperty(isolate, self, propertyName).ToHandleChecked();
2297  return obj->IsSmi() ? i::Smi::cast(*obj)->value() : defaultValue;
2298}
2299
2300
2301int StackFrame::GetLineNumber() const {
2302  return getIntProperty(this, "lineNumber", Message::kNoLineNumberInfo);
2303}
2304
2305
2306int StackFrame::GetColumn() const {
2307  return getIntProperty(this, "column", Message::kNoColumnInfo);
2308}
2309
2310
2311int StackFrame::GetScriptId() const {
2312  return getIntProperty(this, "scriptId", Message::kNoScriptIdInfo);
2313}
2314
2315
2316static Local<String> getStringProperty(const StackFrame* f,
2317                                       const char* propertyName) {
2318  i::Isolate* isolate = Utils::OpenHandle(f)->GetIsolate();
2319  ENTER_V8(isolate);
2320  EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
2321  i::Handle<i::JSObject> self = Utils::OpenHandle(f);
2322  i::Handle<i::Object> obj =
2323      i::Object::GetProperty(isolate, self, propertyName).ToHandleChecked();
2324  return obj->IsString()
2325             ? scope.Escape(Local<String>::Cast(Utils::ToLocal(obj)))
2326             : Local<String>();
2327}
2328
2329
2330Local<String> StackFrame::GetScriptName() const {
2331  return getStringProperty(this, "scriptName");
2332}
2333
2334
2335Local<String> StackFrame::GetScriptNameOrSourceURL() const {
2336  return getStringProperty(this, "scriptNameOrSourceURL");
2337}
2338
2339
2340Local<String> StackFrame::GetFunctionName() const {
2341  return getStringProperty(this, "functionName");
2342}
2343
2344
2345static bool getBoolProperty(const StackFrame* f, const char* propertyName) {
2346  i::Isolate* isolate = Utils::OpenHandle(f)->GetIsolate();
2347  ENTER_V8(isolate);
2348  i::HandleScope scope(isolate);
2349  i::Handle<i::JSObject> self = Utils::OpenHandle(f);
2350  i::Handle<i::Object> obj =
2351      i::Object::GetProperty(isolate, self, propertyName).ToHandleChecked();
2352  return obj->IsTrue();
2353}
2354
2355bool StackFrame::IsEval() const { return getBoolProperty(this, "isEval"); }
2356
2357
2358bool StackFrame::IsConstructor() const {
2359  return getBoolProperty(this, "isConstructor");
2360}
2361
2362
2363// --- J S O N ---
2364
2365Local<Value> JSON::Parse(Local<String> json_string) {
2366  i::Handle<i::String> string = Utils::OpenHandle(*json_string);
2367  i::Isolate* isolate = string->GetIsolate();
2368  ENTER_V8(isolate);
2369  i::HandleScope scope(isolate);
2370  i::Handle<i::String> source = i::String::Flatten(string);
2371  EXCEPTION_PREAMBLE(isolate);
2372  i::MaybeHandle<i::Object> maybe_result =
2373      source->IsSeqOneByteString() ? i::JsonParser<true>::Parse(source)
2374                                   : i::JsonParser<false>::Parse(source);
2375  i::Handle<i::Object> result;
2376  has_pending_exception = !maybe_result.ToHandle(&result);
2377  EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>());
2378  return Utils::ToLocal(
2379      i::Handle<i::Object>::cast(scope.CloseAndEscape(result)));
2380}
2381
2382
2383// --- D a t a ---
2384
2385bool Value::FullIsUndefined() const {
2386  bool result = Utils::OpenHandle(this)->IsUndefined();
2387  DCHECK_EQ(result, QuickIsUndefined());
2388  return result;
2389}
2390
2391
2392bool Value::FullIsNull() const {
2393  bool result = Utils::OpenHandle(this)->IsNull();
2394  DCHECK_EQ(result, QuickIsNull());
2395  return result;
2396}
2397
2398
2399bool Value::IsTrue() const {
2400  return Utils::OpenHandle(this)->IsTrue();
2401}
2402
2403
2404bool Value::IsFalse() const {
2405  return Utils::OpenHandle(this)->IsFalse();
2406}
2407
2408
2409bool Value::IsFunction() const {
2410  return Utils::OpenHandle(this)->IsJSFunction();
2411}
2412
2413
2414bool Value::IsName() const {
2415  return Utils::OpenHandle(this)->IsName();
2416}
2417
2418
2419bool Value::FullIsString() const {
2420  bool result = Utils::OpenHandle(this)->IsString();
2421  DCHECK_EQ(result, QuickIsString());
2422  return result;
2423}
2424
2425
2426bool Value::IsSymbol() const {
2427  return Utils::OpenHandle(this)->IsSymbol();
2428}
2429
2430
2431bool Value::IsArray() const {
2432  return Utils::OpenHandle(this)->IsJSArray();
2433}
2434
2435
2436bool Value::IsArrayBuffer() const {
2437  return Utils::OpenHandle(this)->IsJSArrayBuffer();
2438}
2439
2440
2441bool Value::IsArrayBufferView() const {
2442  return Utils::OpenHandle(this)->IsJSArrayBufferView();
2443}
2444
2445
2446bool Value::IsTypedArray() const {
2447  return Utils::OpenHandle(this)->IsJSTypedArray();
2448}
2449
2450
2451#define VALUE_IS_TYPED_ARRAY(Type, typeName, TYPE, ctype, size)            \
2452  bool Value::Is##Type##Array() const {                                    \
2453    i::Handle<i::Object> obj = Utils::OpenHandle(this);                    \
2454    return obj->IsJSTypedArray() &&                                        \
2455           i::JSTypedArray::cast(*obj)->type() == kExternal##Type##Array;  \
2456  }
2457
2458TYPED_ARRAYS(VALUE_IS_TYPED_ARRAY)
2459
2460#undef VALUE_IS_TYPED_ARRAY
2461
2462
2463bool Value::IsDataView() const {
2464  return Utils::OpenHandle(this)->IsJSDataView();
2465}
2466
2467
2468bool Value::IsObject() const {
2469  return Utils::OpenHandle(this)->IsJSObject();
2470}
2471
2472
2473bool Value::IsNumber() const {
2474  return Utils::OpenHandle(this)->IsNumber();
2475}
2476
2477
2478#define VALUE_IS_SPECIFIC_TYPE(Type, Class)                            \
2479  bool Value::Is##Type() const {                                       \
2480    i::Handle<i::Object> obj = Utils::OpenHandle(this);                \
2481    if (!obj->IsHeapObject()) return false;                            \
2482    i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate();     \
2483    return obj->HasSpecificClassOf(isolate->heap()->Class##_string()); \
2484  }
2485
2486VALUE_IS_SPECIFIC_TYPE(ArgumentsObject, Arguments)
2487VALUE_IS_SPECIFIC_TYPE(BooleanObject, Boolean)
2488VALUE_IS_SPECIFIC_TYPE(NumberObject, Number)
2489VALUE_IS_SPECIFIC_TYPE(StringObject, String)
2490VALUE_IS_SPECIFIC_TYPE(SymbolObject, Symbol)
2491VALUE_IS_SPECIFIC_TYPE(Date, Date)
2492VALUE_IS_SPECIFIC_TYPE(Map, Map)
2493VALUE_IS_SPECIFIC_TYPE(Set, Set)
2494VALUE_IS_SPECIFIC_TYPE(WeakMap, WeakMap)
2495VALUE_IS_SPECIFIC_TYPE(WeakSet, WeakSet)
2496
2497#undef VALUE_IS_SPECIFIC_TYPE
2498
2499
2500bool Value::IsBoolean() const {
2501  return Utils::OpenHandle(this)->IsBoolean();
2502}
2503
2504
2505bool Value::IsExternal() const {
2506  return Utils::OpenHandle(this)->IsExternal();
2507}
2508
2509
2510bool Value::IsInt32() const {
2511  i::Handle<i::Object> obj = Utils::OpenHandle(this);
2512  if (obj->IsSmi()) return true;
2513  if (obj->IsNumber()) {
2514    return i::IsInt32Double(obj->Number());
2515  }
2516  return false;
2517}
2518
2519
2520bool Value::IsUint32() const {
2521  i::Handle<i::Object> obj = Utils::OpenHandle(this);
2522  if (obj->IsSmi()) return i::Smi::cast(*obj)->value() >= 0;
2523  if (obj->IsNumber()) {
2524    double value = obj->Number();
2525    return !i::IsMinusZero(value) &&
2526        value >= 0 &&
2527        value <= i::kMaxUInt32 &&
2528        value == i::FastUI2D(i::FastD2UI(value));
2529  }
2530  return false;
2531}
2532
2533
2534static bool CheckConstructor(i::Isolate* isolate,
2535                             i::Handle<i::JSObject> obj,
2536                             const char* class_name) {
2537  i::Handle<i::Object> constr(obj->map()->constructor(), isolate);
2538  if (!constr->IsJSFunction()) return false;
2539  i::Handle<i::JSFunction> func = i::Handle<i::JSFunction>::cast(constr);
2540  return func->shared()->native() && constr.is_identical_to(
2541      i::Object::GetProperty(isolate,
2542                             isolate->js_builtins_object(),
2543                             class_name).ToHandleChecked());
2544}
2545
2546
2547bool Value::IsNativeError() const {
2548  i::Handle<i::Object> obj = Utils::OpenHandle(this);
2549  if (obj->IsJSObject()) {
2550    i::Handle<i::JSObject> js_obj(i::JSObject::cast(*obj));
2551    i::Isolate* isolate = js_obj->GetIsolate();
2552    return CheckConstructor(isolate, js_obj, "$Error") ||
2553        CheckConstructor(isolate, js_obj, "$EvalError") ||
2554        CheckConstructor(isolate, js_obj, "$RangeError") ||
2555        CheckConstructor(isolate, js_obj, "$ReferenceError") ||
2556        CheckConstructor(isolate, js_obj, "$SyntaxError") ||
2557        CheckConstructor(isolate, js_obj, "$TypeError") ||
2558        CheckConstructor(isolate, js_obj, "$URIError");
2559  } else {
2560    return false;
2561  }
2562}
2563
2564
2565bool Value::IsRegExp() const {
2566  i::Handle<i::Object> obj = Utils::OpenHandle(this);
2567  return obj->IsJSRegExp();
2568}
2569
2570
2571Local<String> Value::ToString() const {
2572  i::Handle<i::Object> obj = Utils::OpenHandle(this);
2573  i::Handle<i::Object> str;
2574  if (obj->IsString()) {
2575    str = obj;
2576  } else {
2577    i::Isolate* isolate = i::Isolate::Current();
2578    LOG_API(isolate, "ToString");
2579    ENTER_V8(isolate);
2580    EXCEPTION_PREAMBLE(isolate);
2581    has_pending_exception = !i::Execution::ToString(
2582        isolate, obj).ToHandle(&str);
2583    EXCEPTION_BAILOUT_CHECK(isolate, Local<String>());
2584  }
2585  return ToApiHandle<String>(str);
2586}
2587
2588
2589Local<String> Value::ToDetailString() const {
2590  i::Handle<i::Object> obj = Utils::OpenHandle(this);
2591  i::Handle<i::Object> str;
2592  if (obj->IsString()) {
2593    str = obj;
2594  } else {
2595    i::Isolate* isolate = i::Isolate::Current();
2596    LOG_API(isolate, "ToDetailString");
2597    ENTER_V8(isolate);
2598    EXCEPTION_PREAMBLE(isolate);
2599    has_pending_exception = !i::Execution::ToDetailString(
2600        isolate, obj).ToHandle(&str);
2601    EXCEPTION_BAILOUT_CHECK(isolate, Local<String>());
2602  }
2603  return ToApiHandle<String>(str);
2604}
2605
2606
2607Local<v8::Object> Value::ToObject() const {
2608  i::Handle<i::Object> obj = Utils::OpenHandle(this);
2609  i::Handle<i::Object> val;
2610  if (obj->IsJSObject()) {
2611    val = obj;
2612  } else {
2613    i::Isolate* isolate = i::Isolate::Current();
2614    LOG_API(isolate, "ToObject");
2615    ENTER_V8(isolate);
2616    EXCEPTION_PREAMBLE(isolate);
2617    has_pending_exception = !i::Execution::ToObject(
2618        isolate, obj).ToHandle(&val);
2619    EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>());
2620  }
2621  return ToApiHandle<Object>(val);
2622}
2623
2624
2625Local<Boolean> Value::ToBoolean() const {
2626  i::Handle<i::Object> obj = Utils::OpenHandle(this);
2627  if (obj->IsBoolean()) {
2628    return ToApiHandle<Boolean>(obj);
2629  } else {
2630    i::Isolate* isolate = i::Isolate::Current();
2631    LOG_API(isolate, "ToBoolean");
2632    ENTER_V8(isolate);
2633    i::Handle<i::Object> val =
2634        isolate->factory()->ToBoolean(obj->BooleanValue());
2635    return ToApiHandle<Boolean>(val);
2636  }
2637}
2638
2639
2640Local<Number> Value::ToNumber() const {
2641  i::Handle<i::Object> obj = Utils::OpenHandle(this);
2642  i::Handle<i::Object> num;
2643  if (obj->IsNumber()) {
2644    num = obj;
2645  } else {
2646    i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate();
2647    LOG_API(isolate, "ToNumber");
2648    ENTER_V8(isolate);
2649    EXCEPTION_PREAMBLE(isolate);
2650    has_pending_exception = !i::Execution::ToNumber(
2651        isolate, obj).ToHandle(&num);
2652    EXCEPTION_BAILOUT_CHECK(isolate, Local<Number>());
2653  }
2654  return ToApiHandle<Number>(num);
2655}
2656
2657
2658Local<Integer> Value::ToInteger() const {
2659  i::Handle<i::Object> obj = Utils::OpenHandle(this);
2660  i::Handle<i::Object> num;
2661  if (obj->IsSmi()) {
2662    num = obj;
2663  } else {
2664    i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate();
2665    LOG_API(isolate, "ToInteger");
2666    ENTER_V8(isolate);
2667    EXCEPTION_PREAMBLE(isolate);
2668    has_pending_exception = !i::Execution::ToInteger(
2669        isolate, obj).ToHandle(&num);
2670    EXCEPTION_BAILOUT_CHECK(isolate, Local<Integer>());
2671  }
2672  return ToApiHandle<Integer>(num);
2673}
2674
2675
2676void i::Internals::CheckInitializedImpl(v8::Isolate* external_isolate) {
2677  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
2678  Utils::ApiCheck(isolate != NULL &&
2679                  isolate->IsInitialized() &&
2680                  !isolate->IsDead(),
2681                  "v8::internal::Internals::CheckInitialized()",
2682                  "Isolate is not initialized or V8 has died");
2683}
2684
2685
2686void External::CheckCast(v8::Value* that) {
2687  Utils::ApiCheck(Utils::OpenHandle(that)->IsExternal(),
2688                  "v8::External::Cast()",
2689                  "Could not convert to external");
2690}
2691
2692
2693void v8::Object::CheckCast(Value* that) {
2694  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2695  Utils::ApiCheck(obj->IsJSObject(),
2696                  "v8::Object::Cast()",
2697                  "Could not convert to object");
2698}
2699
2700
2701void v8::Function::CheckCast(Value* that) {
2702  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2703  Utils::ApiCheck(obj->IsJSFunction(),
2704                  "v8::Function::Cast()",
2705                  "Could not convert to function");
2706}
2707
2708
2709void v8::Name::CheckCast(v8::Value* that) {
2710  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2711  Utils::ApiCheck(obj->IsName(),
2712                  "v8::Name::Cast()",
2713                  "Could not convert to name");
2714}
2715
2716
2717void v8::String::CheckCast(v8::Value* that) {
2718  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2719  Utils::ApiCheck(obj->IsString(),
2720                  "v8::String::Cast()",
2721                  "Could not convert to string");
2722}
2723
2724
2725void v8::Symbol::CheckCast(v8::Value* that) {
2726  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2727  Utils::ApiCheck(obj->IsSymbol(),
2728                  "v8::Symbol::Cast()",
2729                  "Could not convert to symbol");
2730}
2731
2732
2733void v8::Number::CheckCast(v8::Value* that) {
2734  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2735  Utils::ApiCheck(obj->IsNumber(),
2736                  "v8::Number::Cast()",
2737                  "Could not convert to number");
2738}
2739
2740
2741void v8::Integer::CheckCast(v8::Value* that) {
2742  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2743  Utils::ApiCheck(obj->IsNumber(),
2744                  "v8::Integer::Cast()",
2745                  "Could not convert to number");
2746}
2747
2748
2749void v8::Array::CheckCast(Value* that) {
2750  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2751  Utils::ApiCheck(obj->IsJSArray(),
2752                  "v8::Array::Cast()",
2753                  "Could not convert to array");
2754}
2755
2756
2757void v8::Promise::CheckCast(Value* that) {
2758  Utils::ApiCheck(that->IsPromise(),
2759                  "v8::Promise::Cast()",
2760                  "Could not convert to promise");
2761}
2762
2763
2764void v8::Promise::Resolver::CheckCast(Value* that) {
2765  Utils::ApiCheck(that->IsPromise(),
2766                  "v8::Promise::Resolver::Cast()",
2767                  "Could not convert to promise resolver");
2768}
2769
2770
2771void v8::ArrayBuffer::CheckCast(Value* that) {
2772  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2773  Utils::ApiCheck(obj->IsJSArrayBuffer(),
2774                  "v8::ArrayBuffer::Cast()",
2775                  "Could not convert to ArrayBuffer");
2776}
2777
2778
2779void v8::ArrayBufferView::CheckCast(Value* that) {
2780  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2781  Utils::ApiCheck(obj->IsJSArrayBufferView(),
2782                  "v8::ArrayBufferView::Cast()",
2783                  "Could not convert to ArrayBufferView");
2784}
2785
2786
2787void v8::TypedArray::CheckCast(Value* that) {
2788  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2789  Utils::ApiCheck(obj->IsJSTypedArray(),
2790                  "v8::TypedArray::Cast()",
2791                  "Could not convert to TypedArray");
2792}
2793
2794
2795#define CHECK_TYPED_ARRAY_CAST(Type, typeName, TYPE, ctype, size)             \
2796  void v8::Type##Array::CheckCast(Value* that) {                              \
2797    i::Handle<i::Object> obj = Utils::OpenHandle(that);                       \
2798    Utils::ApiCheck(obj->IsJSTypedArray() &&                                  \
2799                    i::JSTypedArray::cast(*obj)->type() ==                    \
2800                        kExternal##Type##Array,                               \
2801                    "v8::" #Type "Array::Cast()",                             \
2802                    "Could not convert to " #Type "Array");                   \
2803  }
2804
2805
2806TYPED_ARRAYS(CHECK_TYPED_ARRAY_CAST)
2807
2808#undef CHECK_TYPED_ARRAY_CAST
2809
2810
2811void v8::DataView::CheckCast(Value* that) {
2812  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2813  Utils::ApiCheck(obj->IsJSDataView(),
2814                  "v8::DataView::Cast()",
2815                  "Could not convert to DataView");
2816}
2817
2818
2819void v8::Date::CheckCast(v8::Value* that) {
2820  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2821  i::Isolate* isolate = NULL;
2822  if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
2823  Utils::ApiCheck(isolate != NULL &&
2824                  obj->HasSpecificClassOf(isolate->heap()->Date_string()),
2825                  "v8::Date::Cast()",
2826                  "Could not convert to date");
2827}
2828
2829
2830void v8::StringObject::CheckCast(v8::Value* that) {
2831  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2832  i::Isolate* isolate = NULL;
2833  if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
2834  Utils::ApiCheck(isolate != NULL &&
2835                  obj->HasSpecificClassOf(isolate->heap()->String_string()),
2836                  "v8::StringObject::Cast()",
2837                  "Could not convert to StringObject");
2838}
2839
2840
2841void v8::SymbolObject::CheckCast(v8::Value* that) {
2842  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2843  i::Isolate* isolate = NULL;
2844  if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
2845  Utils::ApiCheck(isolate != NULL &&
2846                  obj->HasSpecificClassOf(isolate->heap()->Symbol_string()),
2847                  "v8::SymbolObject::Cast()",
2848                  "Could not convert to SymbolObject");
2849}
2850
2851
2852void v8::NumberObject::CheckCast(v8::Value* that) {
2853  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2854  i::Isolate* isolate = NULL;
2855  if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
2856  Utils::ApiCheck(isolate != NULL &&
2857                  obj->HasSpecificClassOf(isolate->heap()->Number_string()),
2858                  "v8::NumberObject::Cast()",
2859                  "Could not convert to NumberObject");
2860}
2861
2862
2863void v8::BooleanObject::CheckCast(v8::Value* that) {
2864  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2865  i::Isolate* isolate = NULL;
2866  if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
2867  Utils::ApiCheck(isolate != NULL &&
2868                  obj->HasSpecificClassOf(isolate->heap()->Boolean_string()),
2869                  "v8::BooleanObject::Cast()",
2870                  "Could not convert to BooleanObject");
2871}
2872
2873
2874void v8::RegExp::CheckCast(v8::Value* that) {
2875  i::Handle<i::Object> obj = Utils::OpenHandle(that);
2876  Utils::ApiCheck(obj->IsJSRegExp(),
2877                  "v8::RegExp::Cast()",
2878                  "Could not convert to regular expression");
2879}
2880
2881
2882bool Value::BooleanValue() const {
2883  return Utils::OpenHandle(this)->BooleanValue();
2884}
2885
2886
2887double Value::NumberValue() const {
2888  i::Handle<i::Object> obj = Utils::OpenHandle(this);
2889  i::Handle<i::Object> num;
2890  if (obj->IsNumber()) {
2891    num = obj;
2892  } else {
2893    i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate();
2894    LOG_API(isolate, "NumberValue");
2895    ENTER_V8(isolate);
2896    EXCEPTION_PREAMBLE(isolate);
2897    has_pending_exception = !i::Execution::ToNumber(
2898        isolate, obj).ToHandle(&num);
2899    EXCEPTION_BAILOUT_CHECK(isolate, base::OS::nan_value());
2900  }
2901  return num->Number();
2902}
2903
2904
2905int64_t Value::IntegerValue() const {
2906  i::Handle<i::Object> obj = Utils::OpenHandle(this);
2907  i::Handle<i::Object> num;
2908  if (obj->IsNumber()) {
2909    num = obj;
2910  } else {
2911    i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate();
2912    LOG_API(isolate, "IntegerValue");
2913    ENTER_V8(isolate);
2914    EXCEPTION_PREAMBLE(isolate);
2915    has_pending_exception = !i::Execution::ToInteger(
2916        isolate, obj).ToHandle(&num);
2917    EXCEPTION_BAILOUT_CHECK(isolate, 0);
2918  }
2919  if (num->IsSmi()) {
2920    return i::Smi::cast(*num)->value();
2921  } else {
2922    return static_cast<int64_t>(num->Number());
2923  }
2924}
2925
2926
2927Local<Int32> Value::ToInt32() const {
2928  i::Handle<i::Object> obj = Utils::OpenHandle(this);
2929  i::Handle<i::Object> num;
2930  if (obj->IsSmi()) {
2931    num = obj;
2932  } else {
2933    i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate();
2934    LOG_API(isolate, "ToInt32");
2935    ENTER_V8(isolate);
2936    EXCEPTION_PREAMBLE(isolate);
2937    has_pending_exception = !i::Execution::ToInt32(isolate, obj).ToHandle(&num);
2938    EXCEPTION_BAILOUT_CHECK(isolate, Local<Int32>());
2939  }
2940  return ToApiHandle<Int32>(num);
2941}
2942
2943
2944Local<Uint32> Value::ToUint32() const {
2945  i::Handle<i::Object> obj = Utils::OpenHandle(this);
2946  i::Handle<i::Object> num;
2947  if (obj->IsSmi()) {
2948    num = obj;
2949  } else {
2950    i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate();
2951    LOG_API(isolate, "ToUInt32");
2952    ENTER_V8(isolate);
2953    EXCEPTION_PREAMBLE(isolate);
2954    has_pending_exception = !i::Execution::ToUint32(
2955        isolate, obj).ToHandle(&num);
2956    EXCEPTION_BAILOUT_CHECK(isolate, Local<Uint32>());
2957  }
2958  return ToApiHandle<Uint32>(num);
2959}
2960
2961
2962Local<Uint32> Value::ToArrayIndex() const {
2963  i::Handle<i::Object> obj = Utils::OpenHandle(this);
2964  if (obj->IsSmi()) {
2965    if (i::Smi::cast(*obj)->value() >= 0) return Utils::Uint32ToLocal(obj);
2966    return Local<Uint32>();
2967  }
2968  i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate();
2969  LOG_API(isolate, "ToArrayIndex");
2970  ENTER_V8(isolate);
2971  EXCEPTION_PREAMBLE(isolate);
2972  i::Handle<i::Object> string_obj;
2973  has_pending_exception = !i::Execution::ToString(
2974      isolate, obj).ToHandle(&string_obj);
2975  EXCEPTION_BAILOUT_CHECK(isolate, Local<Uint32>());
2976  i::Handle<i::String> str = i::Handle<i::String>::cast(string_obj);
2977  uint32_t index;
2978  if (str->AsArrayIndex(&index)) {
2979    i::Handle<i::Object> value;
2980    if (index <= static_cast<uint32_t>(i::Smi::kMaxValue)) {
2981      value = i::Handle<i::Object>(i::Smi::FromInt(index), isolate);
2982    } else {
2983      value = isolate->factory()->NewNumber(index);
2984    }
2985    return Utils::Uint32ToLocal(value);
2986  }
2987  return Local<Uint32>();
2988}
2989
2990
2991int32_t Value::Int32Value() const {
2992  i::Handle<i::Object> obj = Utils::OpenHandle(this);
2993  if (obj->IsSmi()) {
2994    return i::Smi::cast(*obj)->value();
2995  } else {
2996    i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate();
2997    LOG_API(isolate, "Int32Value (slow)");
2998    ENTER_V8(isolate);
2999    EXCEPTION_PREAMBLE(isolate);
3000    i::Handle<i::Object> num;
3001    has_pending_exception = !i::Execution::ToInt32(isolate, obj).ToHandle(&num);
3002    EXCEPTION_BAILOUT_CHECK(isolate, 0);
3003    if (num->IsSmi()) {
3004      return i::Smi::cast(*num)->value();
3005    } else {
3006      return static_cast<int32_t>(num->Number());
3007    }
3008  }
3009}
3010
3011
3012bool Value::Equals(Handle<Value> that) const {
3013  i::Isolate* isolate = i::Isolate::Current();
3014  i::Handle<i::Object> obj = Utils::OpenHandle(this, true);
3015  if (!Utils::ApiCheck(!obj.is_null() && !that.IsEmpty(),
3016                       "v8::Value::Equals()",
3017                       "Reading from empty handle")) {
3018    return false;
3019  }
3020  LOG_API(isolate, "Equals");
3021  ENTER_V8(isolate);
3022  i::Handle<i::Object> other = Utils::OpenHandle(*that);
3023  // If both obj and other are JSObjects, we'd better compare by identity
3024  // immediately when going into JS builtin.  The reason is Invoke
3025  // would overwrite global object receiver with global proxy.
3026  if (obj->IsJSObject() && other->IsJSObject()) {
3027    return *obj == *other;
3028  }
3029  i::Handle<i::Object> args[] = { other };
3030  EXCEPTION_PREAMBLE(isolate);
3031  i::Handle<i::Object> result;
3032  has_pending_exception = !CallV8HeapFunction(
3033      "EQUALS", obj, arraysize(args), args).ToHandle(&result);
3034  EXCEPTION_BAILOUT_CHECK(isolate, false);
3035  return *result == i::Smi::FromInt(i::EQUAL);
3036}
3037
3038
3039bool Value::StrictEquals(Handle<Value> that) const {
3040  i::Isolate* isolate = i::Isolate::Current();
3041  i::Handle<i::Object> obj = Utils::OpenHandle(this, true);
3042  if (!Utils::ApiCheck(!obj.is_null() && !that.IsEmpty(),
3043                       "v8::Value::StrictEquals()",
3044                       "Reading from empty handle")) {
3045    return false;
3046  }
3047  LOG_API(isolate, "StrictEquals");
3048  i::Handle<i::Object> other = Utils::OpenHandle(*that);
3049  // Must check HeapNumber first, since NaN !== NaN.
3050  if (obj->IsHeapNumber()) {
3051    if (!other->IsNumber()) return false;
3052    double x = obj->Number();
3053    double y = other->Number();
3054    // Must check explicitly for NaN:s on Windows, but -0 works fine.
3055    return x == y && !std::isnan(x) && !std::isnan(y);
3056  } else if (*obj == *other) {  // Also covers Booleans.
3057    return true;
3058  } else if (obj->IsSmi()) {
3059    return other->IsNumber() && obj->Number() == other->Number();
3060  } else if (obj->IsString()) {
3061    return other->IsString() &&
3062        i::String::Equals(i::Handle<i::String>::cast(obj),
3063                          i::Handle<i::String>::cast(other));
3064  } else if (obj->IsUndefined() || obj->IsUndetectableObject()) {
3065    return other->IsUndefined() || other->IsUndetectableObject();
3066  } else {
3067    return false;
3068  }
3069}
3070
3071
3072bool Value::SameValue(Handle<Value> that) const {
3073  i::Handle<i::Object> obj = Utils::OpenHandle(this, true);
3074  if (!Utils::ApiCheck(!obj.is_null() && !that.IsEmpty(),
3075                       "v8::Value::SameValue()",
3076                       "Reading from empty handle")) {
3077    return false;
3078  }
3079  i::Handle<i::Object> other = Utils::OpenHandle(*that);
3080  return obj->SameValue(*other);
3081}
3082
3083
3084uint32_t Value::Uint32Value() const {
3085  i::Handle<i::Object> obj = Utils::OpenHandle(this);
3086  if (obj->IsSmi()) {
3087    return i::Smi::cast(*obj)->value();
3088  } else {
3089    i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate();
3090    LOG_API(isolate, "Uint32Value");
3091    ENTER_V8(isolate);
3092    EXCEPTION_PREAMBLE(isolate);
3093    i::Handle<i::Object> num;
3094    has_pending_exception = !i::Execution::ToUint32(
3095        isolate, obj).ToHandle(&num);
3096    EXCEPTION_BAILOUT_CHECK(isolate, 0);
3097    if (num->IsSmi()) {
3098      return i::Smi::cast(*num)->value();
3099    } else {
3100      return static_cast<uint32_t>(num->Number());
3101    }
3102  }
3103}
3104
3105
3106bool v8::Object::Set(v8::Handle<Value> key, v8::Handle<Value> value) {
3107  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3108  ON_BAILOUT(isolate, "v8::Object::Set()", return false);
3109  ENTER_V8(isolate);
3110  i::HandleScope scope(isolate);
3111  i::Handle<i::Object> self = Utils::OpenHandle(this);
3112  i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
3113  i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
3114  EXCEPTION_PREAMBLE(isolate);
3115  has_pending_exception =
3116      i::Runtime::SetObjectProperty(isolate, self, key_obj, value_obj,
3117                                    i::SLOPPY).is_null();
3118  EXCEPTION_BAILOUT_CHECK(isolate, false);
3119  return true;
3120}
3121
3122
3123bool v8::Object::Set(uint32_t index, v8::Handle<Value> value) {
3124  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3125  ON_BAILOUT(isolate, "v8::Object::Set()", return false);
3126  ENTER_V8(isolate);
3127  i::HandleScope scope(isolate);
3128  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3129  i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
3130  EXCEPTION_PREAMBLE(isolate);
3131  has_pending_exception = i::JSObject::SetElement(
3132      self, index, value_obj, NONE, i::SLOPPY).is_null();
3133  EXCEPTION_BAILOUT_CHECK(isolate, false);
3134  return true;
3135}
3136
3137
3138bool v8::Object::ForceSet(v8::Handle<Value> key,
3139                          v8::Handle<Value> value,
3140                          v8::PropertyAttribute attribs) {
3141  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3142  ON_BAILOUT(isolate, "v8::Object::ForceSet()", return false);
3143  ENTER_V8(isolate);
3144  i::HandleScope scope(isolate);
3145  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3146  i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
3147  i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
3148  EXCEPTION_PREAMBLE(isolate);
3149  has_pending_exception = i::Runtime::DefineObjectProperty(
3150      self,
3151      key_obj,
3152      value_obj,
3153      static_cast<PropertyAttributes>(attribs)).is_null();
3154  EXCEPTION_BAILOUT_CHECK(isolate, false);
3155  return true;
3156}
3157
3158
3159bool v8::Object::SetPrivate(v8::Handle<Private> key, v8::Handle<Value> value) {
3160  return ForceSet(v8::Handle<Value>(reinterpret_cast<Value*>(*key)),
3161                  value, DontEnum);
3162}
3163
3164
3165bool v8::Object::ForceDelete(v8::Handle<Value> key) {
3166  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3167  ON_BAILOUT(isolate, "v8::Object::ForceDelete()", return false);
3168  ENTER_V8(isolate);
3169  i::HandleScope scope(isolate);
3170  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3171  i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
3172
3173  // When deleting a property on the global object using ForceDelete
3174  // deoptimize all functions as optimized code does not check for the hole
3175  // value with DontDelete properties.  We have to deoptimize all contexts
3176  // because of possible cross-context inlined functions.
3177  if (self->IsJSGlobalProxy() || self->IsGlobalObject()) {
3178    i::Deoptimizer::DeoptimizeAll(isolate);
3179  }
3180
3181  EXCEPTION_PREAMBLE(isolate);
3182  i::Handle<i::Object> obj;
3183  has_pending_exception = !i::Runtime::DeleteObjectProperty(
3184      isolate, self, key_obj, i::JSReceiver::FORCE_DELETION).ToHandle(&obj);
3185  EXCEPTION_BAILOUT_CHECK(isolate, false);
3186  return obj->IsTrue();
3187}
3188
3189
3190Local<Value> v8::Object::Get(v8::Handle<Value> key) {
3191  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3192  ON_BAILOUT(isolate, "v8::Object::Get()", return Local<v8::Value>());
3193  ENTER_V8(isolate);
3194  i::Handle<i::Object> self = Utils::OpenHandle(this);
3195  i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
3196  EXCEPTION_PREAMBLE(isolate);
3197  i::Handle<i::Object> result;
3198  has_pending_exception =
3199      !i::Runtime::GetObjectProperty(isolate, self, key_obj).ToHandle(&result);
3200  EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
3201  return Utils::ToLocal(result);
3202}
3203
3204
3205Local<Value> v8::Object::Get(uint32_t index) {
3206  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3207  ON_BAILOUT(isolate, "v8::Object::Get()", return Local<v8::Value>());
3208  ENTER_V8(isolate);
3209  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3210  EXCEPTION_PREAMBLE(isolate);
3211  i::Handle<i::Object> result;
3212  has_pending_exception =
3213      !i::Object::GetElement(isolate, self, index).ToHandle(&result);
3214  EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
3215  return Utils::ToLocal(result);
3216}
3217
3218
3219Local<Value> v8::Object::GetPrivate(v8::Handle<Private> key) {
3220  return Get(v8::Handle<Value>(reinterpret_cast<Value*>(*key)));
3221}
3222
3223
3224PropertyAttribute v8::Object::GetPropertyAttributes(v8::Handle<Value> key) {
3225  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3226  ON_BAILOUT(isolate, "v8::Object::GetPropertyAttributes()",
3227             return static_cast<PropertyAttribute>(NONE));
3228  ENTER_V8(isolate);
3229  i::HandleScope scope(isolate);
3230  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3231  i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
3232  if (!key_obj->IsName()) {
3233    EXCEPTION_PREAMBLE(isolate);
3234    has_pending_exception = !i::Execution::ToString(
3235        isolate, key_obj).ToHandle(&key_obj);
3236    EXCEPTION_BAILOUT_CHECK(isolate, static_cast<PropertyAttribute>(NONE));
3237  }
3238  i::Handle<i::Name> key_name = i::Handle<i::Name>::cast(key_obj);
3239  EXCEPTION_PREAMBLE(isolate);
3240  Maybe<PropertyAttributes> result =
3241      i::JSReceiver::GetPropertyAttributes(self, key_name);
3242  has_pending_exception = !result.has_value;
3243  EXCEPTION_BAILOUT_CHECK(isolate, static_cast<PropertyAttribute>(NONE));
3244  if (result.value == ABSENT) return static_cast<PropertyAttribute>(NONE);
3245  return static_cast<PropertyAttribute>(result.value);
3246}
3247
3248
3249Local<Value> v8::Object::GetOwnPropertyDescriptor(Local<String> key) {
3250  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3251  ON_BAILOUT(isolate, "v8::Object::GetOwnPropertyDescriptor()",
3252             return Local<Value>());
3253  ENTER_V8(isolate);
3254  i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3255  i::Handle<i::Name> key_name = Utils::OpenHandle(*key);
3256  i::Handle<i::Object> args[] = { obj, key_name };
3257  EXCEPTION_PREAMBLE(isolate);
3258  i::Handle<i::Object> result;
3259  has_pending_exception = !CallV8HeapFunction(
3260      "ObjectGetOwnPropertyDescriptor",
3261      isolate->factory()->undefined_value(),
3262      arraysize(args),
3263      args).ToHandle(&result);
3264  EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
3265  return Utils::ToLocal(result);
3266}
3267
3268
3269Local<Value> v8::Object::GetPrototype() {
3270  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3271  ON_BAILOUT(isolate, "v8::Object::GetPrototype()", return Local<v8::Value>());
3272  ENTER_V8(isolate);
3273  i::Handle<i::Object> self = Utils::OpenHandle(this);
3274  i::PrototypeIterator iter(isolate, self);
3275  return Utils::ToLocal(i::PrototypeIterator::GetCurrent(iter));
3276}
3277
3278
3279bool v8::Object::SetPrototype(Handle<Value> value) {
3280  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3281  ON_BAILOUT(isolate, "v8::Object::SetPrototype()", return false);
3282  ENTER_V8(isolate);
3283  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3284  i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
3285  // We do not allow exceptions thrown while setting the prototype
3286  // to propagate outside.
3287  TryCatch try_catch;
3288  EXCEPTION_PREAMBLE(isolate);
3289  i::MaybeHandle<i::Object> result =
3290      i::JSObject::SetPrototype(self, value_obj, false);
3291  has_pending_exception = result.is_null();
3292  EXCEPTION_BAILOUT_CHECK(isolate, false);
3293  return true;
3294}
3295
3296
3297Local<Object> v8::Object::FindInstanceInPrototypeChain(
3298    v8::Handle<FunctionTemplate> tmpl) {
3299  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3300  ON_BAILOUT(isolate,
3301             "v8::Object::FindInstanceInPrototypeChain()",
3302             return Local<v8::Object>());
3303  ENTER_V8(isolate);
3304  i::PrototypeIterator iter(isolate, *Utils::OpenHandle(this),
3305                            i::PrototypeIterator::START_AT_RECEIVER);
3306  i::FunctionTemplateInfo* tmpl_info = *Utils::OpenHandle(*tmpl);
3307  while (!tmpl_info->IsTemplateFor(iter.GetCurrent())) {
3308    iter.Advance();
3309    if (iter.IsAtEnd()) {
3310      return Local<Object>();
3311    }
3312  }
3313  return Utils::ToLocal(
3314      i::handle(i::JSObject::cast(iter.GetCurrent()), isolate));
3315}
3316
3317
3318Local<Array> v8::Object::GetPropertyNames() {
3319  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3320  ON_BAILOUT(isolate, "v8::Object::GetPropertyNames()",
3321             return Local<v8::Array>());
3322  ENTER_V8(isolate);
3323  i::HandleScope scope(isolate);
3324  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3325  EXCEPTION_PREAMBLE(isolate);
3326  i::Handle<i::FixedArray> value;
3327  has_pending_exception = !i::JSReceiver::GetKeys(
3328      self, i::JSReceiver::INCLUDE_PROTOS).ToHandle(&value);
3329  EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Array>());
3330  // Because we use caching to speed up enumeration it is important
3331  // to never change the result of the basic enumeration function so
3332  // we clone the result.
3333  i::Handle<i::FixedArray> elms = isolate->factory()->CopyFixedArray(value);
3334  i::Handle<i::JSArray> result =
3335      isolate->factory()->NewJSArrayWithElements(elms);
3336  return Utils::ToLocal(scope.CloseAndEscape(result));
3337}
3338
3339
3340Local<Array> v8::Object::GetOwnPropertyNames() {
3341  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3342  ON_BAILOUT(isolate, "v8::Object::GetOwnPropertyNames()",
3343             return Local<v8::Array>());
3344  ENTER_V8(isolate);
3345  i::HandleScope scope(isolate);
3346  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3347  EXCEPTION_PREAMBLE(isolate);
3348  i::Handle<i::FixedArray> value;
3349  has_pending_exception = !i::JSReceiver::GetKeys(
3350      self, i::JSReceiver::OWN_ONLY).ToHandle(&value);
3351  EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Array>());
3352  // Because we use caching to speed up enumeration it is important
3353  // to never change the result of the basic enumeration function so
3354  // we clone the result.
3355  i::Handle<i::FixedArray> elms = isolate->factory()->CopyFixedArray(value);
3356  i::Handle<i::JSArray> result =
3357      isolate->factory()->NewJSArrayWithElements(elms);
3358  return Utils::ToLocal(scope.CloseAndEscape(result));
3359}
3360
3361
3362Local<String> v8::Object::ObjectProtoToString() {
3363  i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate();
3364  Isolate* isolate = reinterpret_cast<Isolate*>(i_isolate);
3365  ON_BAILOUT(i_isolate, "v8::Object::ObjectProtoToString()",
3366             return Local<v8::String>());
3367  ENTER_V8(i_isolate);
3368  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3369
3370  i::Handle<i::Object> name(self->class_name(), i_isolate);
3371
3372  // Native implementation of Object.prototype.toString (v8natives.js):
3373  //   var c = %_ClassOf(this);
3374  //   if (c === 'Arguments') c  = 'Object';
3375  //   return "[object " + c + "]";
3376
3377  if (!name->IsString()) {
3378    return v8::String::NewFromUtf8(isolate, "[object ]");
3379  } else {
3380    i::Handle<i::String> class_name = i::Handle<i::String>::cast(name);
3381    if (i::String::Equals(class_name,
3382                          i_isolate->factory()->Arguments_string())) {
3383      return v8::String::NewFromUtf8(isolate, "[object Object]");
3384    } else {
3385      const char* prefix = "[object ";
3386      Local<String> str = Utils::ToLocal(class_name);
3387      const char* postfix = "]";
3388
3389      int prefix_len = i::StrLength(prefix);
3390      int str_len = str->Utf8Length();
3391      int postfix_len = i::StrLength(postfix);
3392
3393      int buf_len = prefix_len + str_len + postfix_len;
3394      i::ScopedVector<char> buf(buf_len);
3395
3396      // Write prefix.
3397      char* ptr = buf.start();
3398      i::MemCopy(ptr, prefix, prefix_len * v8::internal::kCharSize);
3399      ptr += prefix_len;
3400
3401      // Write real content.
3402      str->WriteUtf8(ptr, str_len);
3403      ptr += str_len;
3404
3405      // Write postfix.
3406      i::MemCopy(ptr, postfix, postfix_len * v8::internal::kCharSize);
3407
3408      // Copy the buffer into a heap-allocated string and return it.
3409      Local<String> result = v8::String::NewFromUtf8(
3410          isolate, buf.start(), String::kNormalString, buf_len);
3411      return result;
3412    }
3413  }
3414}
3415
3416
3417Local<String> v8::Object::GetConstructorName() {
3418  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3419  ON_BAILOUT(isolate, "v8::Object::GetConstructorName()",
3420             return Local<v8::String>());
3421  ENTER_V8(isolate);
3422  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3423  i::Handle<i::String> name(self->constructor_name());
3424  return Utils::ToLocal(name);
3425}
3426
3427
3428bool v8::Object::Delete(v8::Handle<Value> key) {
3429  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3430  ON_BAILOUT(isolate, "v8::Object::Delete()", return false);
3431  ENTER_V8(isolate);
3432  i::HandleScope scope(isolate);
3433  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3434  i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
3435  EXCEPTION_PREAMBLE(isolate);
3436  i::Handle<i::Object> obj;
3437  has_pending_exception = !i::Runtime::DeleteObjectProperty(
3438      isolate, self, key_obj, i::JSReceiver::NORMAL_DELETION).ToHandle(&obj);
3439  EXCEPTION_BAILOUT_CHECK(isolate, false);
3440  return obj->IsTrue();
3441}
3442
3443
3444bool v8::Object::DeletePrivate(v8::Handle<Private> key) {
3445  return Delete(v8::Handle<Value>(reinterpret_cast<Value*>(*key)));
3446}
3447
3448
3449bool v8::Object::Has(v8::Handle<Value> key) {
3450  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3451  ON_BAILOUT(isolate, "v8::Object::Has()", return false);
3452  ENTER_V8(isolate);
3453  i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
3454  i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
3455  EXCEPTION_PREAMBLE(isolate);
3456  i::Handle<i::Object> obj;
3457  has_pending_exception = !i::Runtime::HasObjectProperty(
3458      isolate, self, key_obj).ToHandle(&obj);
3459  EXCEPTION_BAILOUT_CHECK(isolate, false);
3460  return obj->IsTrue();
3461}
3462
3463
3464bool v8::Object::HasPrivate(v8::Handle<Private> key) {
3465  // TODO(rossberg): this should use HasOwnProperty, but we'd need to
3466  // generalise that to a (noy yet existant) Name argument first.
3467  return Has(v8::Handle<Value>(reinterpret_cast<Value*>(*key)));
3468}
3469
3470
3471bool v8::Object::Delete(uint32_t index) {
3472  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3473  ON_BAILOUT(isolate, "v8::Object::DeleteProperty()",
3474             return false);
3475  ENTER_V8(isolate);
3476  HandleScope scope(reinterpret_cast<Isolate*>(isolate));
3477  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3478
3479  EXCEPTION_PREAMBLE(isolate);
3480  i::Handle<i::Object> obj;
3481  has_pending_exception =
3482      !i::JSReceiver::DeleteElement(self, index).ToHandle(&obj);
3483  EXCEPTION_BAILOUT_CHECK(isolate, false);
3484  return obj->IsTrue();
3485}
3486
3487
3488bool v8::Object::Has(uint32_t index) {
3489  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3490  ON_BAILOUT(isolate, "v8::Object::HasProperty()", return false);
3491  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3492  EXCEPTION_PREAMBLE(isolate);
3493  Maybe<bool> maybe = i::JSReceiver::HasElement(self, index);
3494  has_pending_exception = !maybe.has_value;
3495  EXCEPTION_BAILOUT_CHECK(isolate, false);
3496  return maybe.value;
3497}
3498
3499
3500template<typename Getter, typename Setter, typename Data>
3501static inline bool ObjectSetAccessor(Object* obj,
3502                                     Handle<Name> name,
3503                                     Getter getter,
3504                                     Setter setter,
3505                                     Data data,
3506                                     AccessControl settings,
3507                                     PropertyAttribute attributes) {
3508  i::Isolate* isolate = Utils::OpenHandle(obj)->GetIsolate();
3509  ON_BAILOUT(isolate, "v8::Object::SetAccessor()", return false);
3510  ENTER_V8(isolate);
3511  i::HandleScope scope(isolate);
3512  v8::Handle<AccessorSignature> signature;
3513  i::Handle<i::AccessorInfo> info = MakeAccessorInfo(
3514      name, getter, setter, data, settings, attributes, signature);
3515  if (info.is_null()) return false;
3516  bool fast = Utils::OpenHandle(obj)->HasFastProperties();
3517  i::Handle<i::Object> result;
3518  ASSIGN_RETURN_ON_EXCEPTION_VALUE(
3519      isolate, result,
3520      i::JSObject::SetAccessor(Utils::OpenHandle(obj), info),
3521      false);
3522  if (result->IsUndefined()) return false;
3523  if (fast) i::JSObject::MigrateSlowToFast(Utils::OpenHandle(obj), 0);
3524  return true;
3525}
3526
3527
3528bool Object::SetAccessor(Handle<String> name,
3529                         AccessorGetterCallback getter,
3530                         AccessorSetterCallback setter,
3531                         v8::Handle<Value> data,
3532                         AccessControl settings,
3533                         PropertyAttribute attributes) {
3534  return ObjectSetAccessor(
3535      this, name, getter, setter, data, settings, attributes);
3536}
3537
3538
3539bool Object::SetAccessor(Handle<Name> name,
3540                         AccessorNameGetterCallback getter,
3541                         AccessorNameSetterCallback setter,
3542                         v8::Handle<Value> data,
3543                         AccessControl settings,
3544                         PropertyAttribute attributes) {
3545  return ObjectSetAccessor(
3546      this, name, getter, setter, data, settings, attributes);
3547}
3548
3549
3550bool Object::SetDeclaredAccessor(Local<Name> name,
3551                                 Local<DeclaredAccessorDescriptor> descriptor,
3552                                 PropertyAttribute attributes,
3553                                 AccessControl settings) {
3554  void* null = NULL;
3555  return ObjectSetAccessor(
3556      this, name, descriptor, null, null, settings, attributes);
3557}
3558
3559
3560void Object::SetAccessorProperty(Local<Name> name,
3561                                 Local<Function> getter,
3562                                 Handle<Function> setter,
3563                                 PropertyAttribute attribute,
3564                                 AccessControl settings) {
3565  // TODO(verwaest): Remove |settings|.
3566  DCHECK_EQ(v8::DEFAULT, settings);
3567  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3568  ON_BAILOUT(isolate, "v8::Object::SetAccessorProperty()", return);
3569  ENTER_V8(isolate);
3570  i::HandleScope scope(isolate);
3571  i::Handle<i::Object> getter_i = v8::Utils::OpenHandle(*getter);
3572  i::Handle<i::Object> setter_i = v8::Utils::OpenHandle(*setter, true);
3573  if (setter_i.is_null()) setter_i = isolate->factory()->null_value();
3574  i::JSObject::DefineAccessor(v8::Utils::OpenHandle(this),
3575                              v8::Utils::OpenHandle(*name),
3576                              getter_i,
3577                              setter_i,
3578                              static_cast<PropertyAttributes>(attribute));
3579}
3580
3581
3582bool v8::Object::HasOwnProperty(Handle<String> key) {
3583  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3584  ON_BAILOUT(isolate, "v8::Object::HasOwnProperty()",
3585             return false);
3586  EXCEPTION_PREAMBLE(isolate);
3587  Maybe<bool> maybe = i::JSReceiver::HasOwnProperty(Utils::OpenHandle(this),
3588                                                    Utils::OpenHandle(*key));
3589  has_pending_exception = !maybe.has_value;
3590  EXCEPTION_BAILOUT_CHECK(isolate, false);
3591  return maybe.value;
3592}
3593
3594
3595bool v8::Object::HasRealNamedProperty(Handle<String> key) {
3596  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3597  ON_BAILOUT(isolate, "v8::Object::HasRealNamedProperty()",
3598             return false);
3599  EXCEPTION_PREAMBLE(isolate);
3600  Maybe<bool> maybe = i::JSObject::HasRealNamedProperty(
3601      Utils::OpenHandle(this), Utils::OpenHandle(*key));
3602  has_pending_exception = !maybe.has_value;
3603  EXCEPTION_BAILOUT_CHECK(isolate, false);
3604  return maybe.value;
3605}
3606
3607
3608bool v8::Object::HasRealIndexedProperty(uint32_t index) {
3609  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3610  ON_BAILOUT(isolate, "v8::Object::HasRealIndexedProperty()",
3611             return false);
3612  EXCEPTION_PREAMBLE(isolate);
3613  Maybe<bool> maybe =
3614      i::JSObject::HasRealElementProperty(Utils::OpenHandle(this), index);
3615  has_pending_exception = !maybe.has_value;
3616  EXCEPTION_BAILOUT_CHECK(isolate, false);
3617  return maybe.value;
3618}
3619
3620
3621bool v8::Object::HasRealNamedCallbackProperty(Handle<String> key) {
3622  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3623  ON_BAILOUT(isolate,
3624             "v8::Object::HasRealNamedCallbackProperty()",
3625             return false);
3626  ENTER_V8(isolate);
3627  EXCEPTION_PREAMBLE(isolate);
3628  Maybe<bool> maybe = i::JSObject::HasRealNamedCallbackProperty(
3629      Utils::OpenHandle(this), Utils::OpenHandle(*key));
3630  has_pending_exception = !maybe.has_value;
3631  EXCEPTION_BAILOUT_CHECK(isolate, false);
3632  return maybe.value;
3633}
3634
3635
3636bool v8::Object::HasNamedLookupInterceptor() {
3637  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3638  ON_BAILOUT(isolate, "v8::Object::HasNamedLookupInterceptor()",
3639             return false);
3640  return Utils::OpenHandle(this)->HasNamedInterceptor();
3641}
3642
3643
3644bool v8::Object::HasIndexedLookupInterceptor() {
3645  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3646  ON_BAILOUT(isolate, "v8::Object::HasIndexedLookupInterceptor()",
3647             return false);
3648  return Utils::OpenHandle(this)->HasIndexedInterceptor();
3649}
3650
3651
3652static Local<Value> GetPropertyByLookup(i::LookupIterator* it) {
3653  // If the property being looked up is a callback, it can throw an exception.
3654  EXCEPTION_PREAMBLE(it->isolate());
3655  i::Handle<i::Object> result;
3656  has_pending_exception = !i::Object::GetProperty(it).ToHandle(&result);
3657  EXCEPTION_BAILOUT_CHECK(it->isolate(), Local<Value>());
3658
3659  if (it->IsFound()) return Utils::ToLocal(result);
3660  return Local<Value>();
3661}
3662
3663
3664Local<Value> v8::Object::GetRealNamedPropertyInPrototypeChain(
3665    Handle<String> key) {
3666  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3667  ON_BAILOUT(isolate,
3668             "v8::Object::GetRealNamedPropertyInPrototypeChain()",
3669             return Local<Value>());
3670  ENTER_V8(isolate);
3671  i::Handle<i::JSObject> self_obj = Utils::OpenHandle(this);
3672  i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
3673  i::PrototypeIterator iter(isolate, self_obj);
3674  if (iter.IsAtEnd()) return Local<Value>();
3675  i::Handle<i::Object> proto = i::PrototypeIterator::GetCurrent(iter);
3676  i::LookupIterator it(self_obj, key_obj, i::Handle<i::JSReceiver>::cast(proto),
3677                       i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
3678  return GetPropertyByLookup(&it);
3679}
3680
3681
3682Local<Value> v8::Object::GetRealNamedProperty(Handle<String> key) {
3683  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3684  ON_BAILOUT(isolate, "v8::Object::GetRealNamedProperty()",
3685             return Local<Value>());
3686  ENTER_V8(isolate);
3687  i::Handle<i::JSObject> self_obj = Utils::OpenHandle(this);
3688  i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
3689  i::LookupIterator it(self_obj, key_obj,
3690                       i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
3691  return GetPropertyByLookup(&it);
3692}
3693
3694
3695// Turns on access checks by copying the map and setting the check flag.
3696// Because the object gets a new map, existing inline cache caching
3697// the old map of this object will fail.
3698void v8::Object::TurnOnAccessCheck() {
3699  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3700  ON_BAILOUT(isolate, "v8::Object::TurnOnAccessCheck()", return);
3701  ENTER_V8(isolate);
3702  i::HandleScope scope(isolate);
3703  i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3704
3705  // When turning on access checks for a global object deoptimize all functions
3706  // as optimized code does not always handle access checks.
3707  i::Deoptimizer::DeoptimizeGlobalObject(*obj);
3708
3709  i::Handle<i::Map> new_map = i::Map::Copy(i::Handle<i::Map>(obj->map()));
3710  new_map->set_is_access_check_needed(true);
3711  i::JSObject::MigrateToMap(obj, new_map);
3712}
3713
3714
3715bool v8::Object::IsDirty() {
3716  return Utils::OpenHandle(this)->IsDirty();
3717}
3718
3719
3720Local<v8::Object> v8::Object::Clone() {
3721  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3722  ON_BAILOUT(isolate, "v8::Object::Clone()", return Local<Object>());
3723  ENTER_V8(isolate);
3724  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3725  EXCEPTION_PREAMBLE(isolate);
3726  i::Handle<i::JSObject> result = isolate->factory()->CopyJSObject(self);
3727  has_pending_exception = result.is_null();
3728  EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>());
3729  return Utils::ToLocal(result);
3730}
3731
3732
3733Local<v8::Context> v8::Object::CreationContext() {
3734  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3735  ON_BAILOUT(isolate,
3736             "v8::Object::CreationContext()", return Local<v8::Context>());
3737  ENTER_V8(isolate);
3738  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3739  i::Context* context = self->GetCreationContext();
3740  return Utils::ToLocal(i::Handle<i::Context>(context));
3741}
3742
3743
3744int v8::Object::GetIdentityHash() {
3745  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3746  ON_BAILOUT(isolate, "v8::Object::GetIdentityHash()", return 0);
3747  ENTER_V8(isolate);
3748  i::HandleScope scope(isolate);
3749  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3750  return i::JSReceiver::GetOrCreateIdentityHash(self)->value();
3751}
3752
3753
3754bool v8::Object::SetHiddenValue(v8::Handle<v8::String> key,
3755                                v8::Handle<v8::Value> value) {
3756  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3757  ON_BAILOUT(isolate, "v8::Object::SetHiddenValue()", return false);
3758  if (value.IsEmpty()) return DeleteHiddenValue(key);
3759  ENTER_V8(isolate);
3760  i::HandleScope scope(isolate);
3761  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3762  i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
3763  i::Handle<i::String> key_string =
3764      isolate->factory()->InternalizeString(key_obj);
3765  i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
3766  i::Handle<i::Object> result =
3767      i::JSObject::SetHiddenProperty(self, key_string, value_obj);
3768  return *result == *self;
3769}
3770
3771
3772v8::Local<v8::Value> v8::Object::GetHiddenValue(v8::Handle<v8::String> key) {
3773  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3774  ON_BAILOUT(isolate, "v8::Object::GetHiddenValue()",
3775             return Local<v8::Value>());
3776  ENTER_V8(isolate);
3777  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3778  i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
3779  i::Handle<i::String> key_string =
3780      isolate->factory()->InternalizeString(key_obj);
3781  i::Handle<i::Object> result(self->GetHiddenProperty(key_string), isolate);
3782  if (result->IsTheHole()) return v8::Local<v8::Value>();
3783  return Utils::ToLocal(result);
3784}
3785
3786
3787bool v8::Object::DeleteHiddenValue(v8::Handle<v8::String> key) {
3788  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3789  ON_BAILOUT(isolate, "v8::DeleteHiddenValue()", return false);
3790  ENTER_V8(isolate);
3791  i::HandleScope scope(isolate);
3792  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3793  i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
3794  i::Handle<i::String> key_string =
3795      isolate->factory()->InternalizeString(key_obj);
3796  i::JSObject::DeleteHiddenProperty(self, key_string);
3797  return true;
3798}
3799
3800
3801namespace {
3802
3803static i::ElementsKind GetElementsKindFromExternalArrayType(
3804    ExternalArrayType array_type) {
3805  switch (array_type) {
3806#define ARRAY_TYPE_TO_ELEMENTS_KIND(Type, type, TYPE, ctype, size)            \
3807    case kExternal##Type##Array:                                              \
3808      return i::EXTERNAL_##TYPE##_ELEMENTS;
3809
3810    TYPED_ARRAYS(ARRAY_TYPE_TO_ELEMENTS_KIND)
3811#undef ARRAY_TYPE_TO_ELEMENTS_KIND
3812  }
3813  UNREACHABLE();
3814  return i::DICTIONARY_ELEMENTS;
3815}
3816
3817
3818void PrepareExternalArrayElements(i::Handle<i::JSObject> object,
3819                                  void* data,
3820                                  ExternalArrayType array_type,
3821                                  int length) {
3822  i::Isolate* isolate = object->GetIsolate();
3823  i::Handle<i::ExternalArray> array =
3824      isolate->factory()->NewExternalArray(length, array_type, data);
3825
3826  i::Handle<i::Map> external_array_map =
3827      i::JSObject::GetElementsTransitionMap(
3828          object,
3829          GetElementsKindFromExternalArrayType(array_type));
3830
3831  i::JSObject::SetMapAndElements(object, external_array_map, array);
3832}
3833
3834}  // namespace
3835
3836
3837void v8::Object::SetIndexedPropertiesToPixelData(uint8_t* data, int length) {
3838  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3839  ON_BAILOUT(isolate, "v8::SetElementsToPixelData()", return);
3840  ENTER_V8(isolate);
3841  i::HandleScope scope(isolate);
3842  if (!Utils::ApiCheck(length >= 0 &&
3843                       length <= i::ExternalUint8ClampedArray::kMaxLength,
3844                       "v8::Object::SetIndexedPropertiesToPixelData()",
3845                       "length exceeds max acceptable value")) {
3846    return;
3847  }
3848  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3849  if (!Utils::ApiCheck(!self->IsJSArray(),
3850                       "v8::Object::SetIndexedPropertiesToPixelData()",
3851                       "JSArray is not supported")) {
3852    return;
3853  }
3854  PrepareExternalArrayElements(self, data, kExternalUint8ClampedArray, length);
3855}
3856
3857
3858bool v8::Object::HasIndexedPropertiesInPixelData() {
3859  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3860  ON_BAILOUT(self->GetIsolate(), "v8::HasIndexedPropertiesInPixelData()",
3861             return false);
3862  return self->HasExternalUint8ClampedElements();
3863}
3864
3865
3866uint8_t* v8::Object::GetIndexedPropertiesPixelData() {
3867  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3868  ON_BAILOUT(self->GetIsolate(), "v8::GetIndexedPropertiesPixelData()",
3869             return NULL);
3870  if (self->HasExternalUint8ClampedElements()) {
3871    return i::ExternalUint8ClampedArray::cast(self->elements())->
3872        external_uint8_clamped_pointer();
3873  } else {
3874    return NULL;
3875  }
3876}
3877
3878
3879int v8::Object::GetIndexedPropertiesPixelDataLength() {
3880  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3881  ON_BAILOUT(self->GetIsolate(), "v8::GetIndexedPropertiesPixelDataLength()",
3882             return -1);
3883  if (self->HasExternalUint8ClampedElements()) {
3884    return i::ExternalUint8ClampedArray::cast(self->elements())->length();
3885  } else {
3886    return -1;
3887  }
3888}
3889
3890
3891void v8::Object::SetIndexedPropertiesToExternalArrayData(
3892    void* data,
3893    ExternalArrayType array_type,
3894    int length) {
3895  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3896  ON_BAILOUT(isolate, "v8::SetIndexedPropertiesToExternalArrayData()", return);
3897  ENTER_V8(isolate);
3898  i::HandleScope scope(isolate);
3899  if (!Utils::ApiCheck(length >= 0 && length <= i::ExternalArray::kMaxLength,
3900                       "v8::Object::SetIndexedPropertiesToExternalArrayData()",
3901                       "length exceeds max acceptable value")) {
3902    return;
3903  }
3904  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3905  if (!Utils::ApiCheck(!self->IsJSArray(),
3906                       "v8::Object::SetIndexedPropertiesToExternalArrayData()",
3907                       "JSArray is not supported")) {
3908    return;
3909  }
3910  PrepareExternalArrayElements(self, data, array_type, length);
3911}
3912
3913
3914bool v8::Object::HasIndexedPropertiesInExternalArrayData() {
3915  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3916  ON_BAILOUT(self->GetIsolate(),
3917             "v8::HasIndexedPropertiesInExternalArrayData()",
3918             return false);
3919  return self->HasExternalArrayElements();
3920}
3921
3922
3923void* v8::Object::GetIndexedPropertiesExternalArrayData() {
3924  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3925  ON_BAILOUT(self->GetIsolate(),
3926             "v8::GetIndexedPropertiesExternalArrayData()",
3927             return NULL);
3928  if (self->HasExternalArrayElements()) {
3929    return i::ExternalArray::cast(self->elements())->external_pointer();
3930  } else {
3931    return NULL;
3932  }
3933}
3934
3935
3936ExternalArrayType v8::Object::GetIndexedPropertiesExternalArrayDataType() {
3937  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3938  ON_BAILOUT(self->GetIsolate(),
3939             "v8::GetIndexedPropertiesExternalArrayDataType()",
3940             return static_cast<ExternalArrayType>(-1));
3941  switch (self->elements()->map()->instance_type()) {
3942#define INSTANCE_TYPE_TO_ARRAY_TYPE(Type, type, TYPE, ctype, size)            \
3943    case i::EXTERNAL_##TYPE##_ARRAY_TYPE:                                     \
3944      return kExternal##Type##Array;
3945    TYPED_ARRAYS(INSTANCE_TYPE_TO_ARRAY_TYPE)
3946#undef INSTANCE_TYPE_TO_ARRAY_TYPE
3947    default:
3948      return static_cast<ExternalArrayType>(-1);
3949  }
3950}
3951
3952
3953int v8::Object::GetIndexedPropertiesExternalArrayDataLength() {
3954  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
3955  ON_BAILOUT(self->GetIsolate(),
3956             "v8::GetIndexedPropertiesExternalArrayDataLength()",
3957             return 0);
3958  if (self->HasExternalArrayElements()) {
3959    return i::ExternalArray::cast(self->elements())->length();
3960  } else {
3961    return -1;
3962  }
3963}
3964
3965
3966bool v8::Object::IsCallable() {
3967  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3968  ON_BAILOUT(isolate, "v8::Object::IsCallable()", return false);
3969  ENTER_V8(isolate);
3970  i::HandleScope scope(isolate);
3971  i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3972  return obj->IsCallable();
3973}
3974
3975
3976Local<v8::Value> Object::CallAsFunction(v8::Handle<v8::Value> recv,
3977                                        int argc,
3978                                        v8::Handle<v8::Value> argv[]) {
3979  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3980  ON_BAILOUT(isolate, "v8::Object::CallAsFunction()",
3981             return Local<v8::Value>());
3982  LOG_API(isolate, "Object::CallAsFunction");
3983  ENTER_V8(isolate);
3984  i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
3985  i::HandleScope scope(isolate);
3986  i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3987  i::Handle<i::Object> recv_obj = Utils::OpenHandle(*recv);
3988  STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**));
3989  i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
3990  i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>();
3991  if (obj->IsJSFunction()) {
3992    fun = i::Handle<i::JSFunction>::cast(obj);
3993  } else {
3994    EXCEPTION_PREAMBLE(isolate);
3995    i::Handle<i::Object> delegate;
3996    has_pending_exception = !i::Execution::TryGetFunctionDelegate(
3997        isolate, obj).ToHandle(&delegate);
3998    EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
3999    fun = i::Handle<i::JSFunction>::cast(delegate);
4000    recv_obj = obj;
4001  }
4002  EXCEPTION_PREAMBLE(isolate);
4003  i::Handle<i::Object> returned;
4004  has_pending_exception = !i::Execution::Call(
4005      isolate, fun, recv_obj, argc, args, true).ToHandle(&returned);
4006  EXCEPTION_BAILOUT_CHECK_DO_CALLBACK(isolate, Local<Value>());
4007  return Utils::ToLocal(scope.CloseAndEscape(returned));
4008}
4009
4010
4011Local<v8::Value> Object::CallAsConstructor(int argc,
4012                                           v8::Handle<v8::Value> argv[]) {
4013  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4014  ON_BAILOUT(isolate, "v8::Object::CallAsConstructor()",
4015             return Local<v8::Object>());
4016  LOG_API(isolate, "Object::CallAsConstructor");
4017  ENTER_V8(isolate);
4018  i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
4019  i::HandleScope scope(isolate);
4020  i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
4021  STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**));
4022  i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
4023  if (obj->IsJSFunction()) {
4024    i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(obj);
4025    EXCEPTION_PREAMBLE(isolate);
4026    i::Handle<i::Object> returned;
4027    has_pending_exception = !i::Execution::New(
4028        fun, argc, args).ToHandle(&returned);
4029    EXCEPTION_BAILOUT_CHECK_DO_CALLBACK(isolate, Local<v8::Object>());
4030    return Utils::ToLocal(scope.CloseAndEscape(
4031        i::Handle<i::JSObject>::cast(returned)));
4032  }
4033  EXCEPTION_PREAMBLE(isolate);
4034  i::Handle<i::Object> delegate;
4035  has_pending_exception = !i::Execution::TryGetConstructorDelegate(
4036      isolate, obj).ToHandle(&delegate);
4037  EXCEPTION_BAILOUT_CHECK(isolate, Local<v8::Object>());
4038  if (!delegate->IsUndefined()) {
4039    i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(delegate);
4040    EXCEPTION_PREAMBLE(isolate);
4041    i::Handle<i::Object> returned;
4042    has_pending_exception = !i::Execution::Call(
4043        isolate, fun, obj, argc, args).ToHandle(&returned);
4044    EXCEPTION_BAILOUT_CHECK_DO_CALLBACK(isolate, Local<v8::Object>());
4045    DCHECK(!delegate->IsUndefined());
4046    return Utils::ToLocal(scope.CloseAndEscape(returned));
4047  }
4048  return Local<v8::Object>();
4049}
4050
4051
4052Local<Function> Function::New(Isolate* v8_isolate,
4053                              FunctionCallback callback,
4054                              Local<Value> data,
4055                              int length) {
4056  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
4057  LOG_API(isolate, "Function::New");
4058  ENTER_V8(isolate);
4059  return FunctionTemplateNew(
4060      isolate, callback, data, Local<Signature>(), length, true)->
4061      GetFunction();
4062}
4063
4064
4065Local<v8::Object> Function::NewInstance() const {
4066  return NewInstance(0, NULL);
4067}
4068
4069
4070Local<v8::Object> Function::NewInstance(int argc,
4071                                        v8::Handle<v8::Value> argv[]) const {
4072  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4073  ON_BAILOUT(isolate, "v8::Function::NewInstance()",
4074             return Local<v8::Object>());
4075  LOG_API(isolate, "Function::NewInstance");
4076  ENTER_V8(isolate);
4077  i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
4078  EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
4079  i::Handle<i::JSFunction> function = Utils::OpenHandle(this);
4080  STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**));
4081  i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
4082  EXCEPTION_PREAMBLE(isolate);
4083  i::Handle<i::Object> returned;
4084  has_pending_exception = !i::Execution::New(
4085      function, argc, args).ToHandle(&returned);
4086  EXCEPTION_BAILOUT_CHECK_DO_CALLBACK(isolate, Local<v8::Object>());
4087  return scope.Escape(Utils::ToLocal(i::Handle<i::JSObject>::cast(returned)));
4088}
4089
4090
4091Local<v8::Value> Function::Call(v8::Handle<v8::Value> recv, int argc,
4092                                v8::Handle<v8::Value> argv[]) {
4093  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4094  ON_BAILOUT(isolate, "v8::Function::Call()", return Local<v8::Value>());
4095  LOG_API(isolate, "Function::Call");
4096  ENTER_V8(isolate);
4097  i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
4098  i::HandleScope scope(isolate);
4099  i::Handle<i::JSFunction> fun = Utils::OpenHandle(this);
4100  i::Handle<i::Object> recv_obj = Utils::OpenHandle(*recv);
4101  STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**));
4102  i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
4103  EXCEPTION_PREAMBLE(isolate);
4104  i::Handle<i::Object> returned;
4105  has_pending_exception = !i::Execution::Call(
4106      isolate, fun, recv_obj, argc, args, true).ToHandle(&returned);
4107  EXCEPTION_BAILOUT_CHECK_DO_CALLBACK(isolate, Local<Object>());
4108  return Utils::ToLocal(scope.CloseAndEscape(returned));
4109}
4110
4111
4112void Function::SetName(v8::Handle<v8::String> name) {
4113  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4114  ENTER_V8(isolate);
4115  USE(isolate);
4116  i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
4117  func->shared()->set_name(*Utils::OpenHandle(*name));
4118}
4119
4120
4121Handle<Value> Function::GetName() const {
4122  i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
4123  return Utils::ToLocal(i::Handle<i::Object>(func->shared()->name(),
4124                                             func->GetIsolate()));
4125}
4126
4127
4128Handle<Value> Function::GetInferredName() const {
4129  i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
4130  return Utils::ToLocal(i::Handle<i::Object>(func->shared()->inferred_name(),
4131                                             func->GetIsolate()));
4132}
4133
4134
4135Handle<Value> Function::GetDisplayName() const {
4136  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4137  ON_BAILOUT(isolate, "v8::Function::GetDisplayName()",
4138             return ToApiHandle<Primitive>(
4139                 isolate->factory()->undefined_value()));
4140  ENTER_V8(isolate);
4141  i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
4142  i::Handle<i::String> property_name =
4143      isolate->factory()->InternalizeOneByteString(
4144          STATIC_CHAR_VECTOR("displayName"));
4145
4146  i::Handle<i::Object> value =
4147      i::JSObject::GetDataProperty(func, property_name);
4148  if (value->IsString()) {
4149    i::Handle<i::String> name = i::Handle<i::String>::cast(value);
4150    if (name->length() > 0) return Utils::ToLocal(name);
4151  }
4152
4153  return ToApiHandle<Primitive>(isolate->factory()->undefined_value());
4154}
4155
4156
4157ScriptOrigin Function::GetScriptOrigin() const {
4158  i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
4159  if (func->shared()->script()->IsScript()) {
4160    i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
4161    i::Handle<i::Object> scriptName = i::Script::GetNameOrSourceURL(script);
4162    v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(func->GetIsolate());
4163    v8::ScriptOrigin origin(
4164        Utils::ToLocal(scriptName),
4165        v8::Integer::New(isolate, script->line_offset()->value()),
4166        v8::Integer::New(isolate, script->column_offset()->value()));
4167    return origin;
4168  }
4169  return v8::ScriptOrigin(Handle<Value>());
4170}
4171
4172
4173const int Function::kLineOffsetNotFound = -1;
4174
4175
4176int Function::GetScriptLineNumber() const {
4177  i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
4178  if (func->shared()->script()->IsScript()) {
4179    i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
4180    return i::Script::GetLineNumber(script, func->shared()->start_position());
4181  }
4182  return kLineOffsetNotFound;
4183}
4184
4185
4186int Function::GetScriptColumnNumber() const {
4187  i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
4188  if (func->shared()->script()->IsScript()) {
4189    i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
4190    return i::Script::GetColumnNumber(script, func->shared()->start_position());
4191  }
4192  return kLineOffsetNotFound;
4193}
4194
4195
4196bool Function::IsBuiltin() const {
4197  i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
4198  return func->IsBuiltin();
4199}
4200
4201
4202int Function::ScriptId() const {
4203  i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
4204  if (!func->shared()->script()->IsScript()) {
4205    return v8::UnboundScript::kNoScriptId;
4206  }
4207  i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
4208  return script->id()->value();
4209}
4210
4211
4212Local<v8::Value> Function::GetBoundFunction() const {
4213  i::Handle<i::JSFunction> func = Utils::OpenHandle(this);
4214  if (!func->shared()->bound()) {
4215    return v8::Undefined(reinterpret_cast<v8::Isolate*>(func->GetIsolate()));
4216  }
4217  i::Handle<i::FixedArray> bound_args = i::Handle<i::FixedArray>(
4218      i::FixedArray::cast(func->function_bindings()));
4219  i::Handle<i::Object> original(
4220      bound_args->get(i::JSFunction::kBoundFunctionIndex),
4221      func->GetIsolate());
4222  return Utils::ToLocal(i::Handle<i::JSFunction>::cast(original));
4223}
4224
4225
4226int String::Length() const {
4227  i::Handle<i::String> str = Utils::OpenHandle(this);
4228  return str->length();
4229}
4230
4231
4232bool String::IsOneByte() const {
4233  i::Handle<i::String> str = Utils::OpenHandle(this);
4234  return str->HasOnlyOneByteChars();
4235}
4236
4237
4238// Helpers for ContainsOnlyOneByteHelper
4239template<size_t size> struct OneByteMask;
4240template<> struct OneByteMask<4> {
4241  static const uint32_t value = 0xFF00FF00;
4242};
4243template<> struct OneByteMask<8> {
4244  static const uint64_t value = V8_2PART_UINT64_C(0xFF00FF00, FF00FF00);
4245};
4246static const uintptr_t kOneByteMask = OneByteMask<sizeof(uintptr_t)>::value;
4247static const uintptr_t kAlignmentMask = sizeof(uintptr_t) - 1;
4248static inline bool Unaligned(const uint16_t* chars) {
4249  return reinterpret_cast<const uintptr_t>(chars) & kAlignmentMask;
4250}
4251
4252
4253static inline const uint16_t* Align(const uint16_t* chars) {
4254  return reinterpret_cast<uint16_t*>(
4255      reinterpret_cast<uintptr_t>(chars) & ~kAlignmentMask);
4256}
4257
4258class ContainsOnlyOneByteHelper {
4259 public:
4260  ContainsOnlyOneByteHelper() : is_one_byte_(true) {}
4261  bool Check(i::String* string) {
4262    i::ConsString* cons_string = i::String::VisitFlat(this, string, 0);
4263    if (cons_string == NULL) return is_one_byte_;
4264    return CheckCons(cons_string);
4265  }
4266  void VisitOneByteString(const uint8_t* chars, int length) {
4267    // Nothing to do.
4268  }
4269  void VisitTwoByteString(const uint16_t* chars, int length) {
4270    // Accumulated bits.
4271    uintptr_t acc = 0;
4272    // Align to uintptr_t.
4273    const uint16_t* end = chars + length;
4274    while (Unaligned(chars) && chars != end) {
4275      acc |= *chars++;
4276    }
4277    // Read word aligned in blocks,
4278    // checking the return value at the end of each block.
4279    const uint16_t* aligned_end = Align(end);
4280    const int increment = sizeof(uintptr_t)/sizeof(uint16_t);
4281    const int inner_loops = 16;
4282    while (chars + inner_loops*increment < aligned_end) {
4283      for (int i = 0; i < inner_loops; i++) {
4284        acc |= *reinterpret_cast<const uintptr_t*>(chars);
4285        chars += increment;
4286      }
4287      // Check for early return.
4288      if ((acc & kOneByteMask) != 0) {
4289        is_one_byte_ = false;
4290        return;
4291      }
4292    }
4293    // Read the rest.
4294    while (chars != end) {
4295      acc |= *chars++;
4296    }
4297    // Check result.
4298    if ((acc & kOneByteMask) != 0) is_one_byte_ = false;
4299  }
4300
4301 private:
4302  bool CheckCons(i::ConsString* cons_string) {
4303    while (true) {
4304      // Check left side if flat.
4305      i::String* left = cons_string->first();
4306      i::ConsString* left_as_cons =
4307          i::String::VisitFlat(this, left, 0);
4308      if (!is_one_byte_) return false;
4309      // Check right side if flat.
4310      i::String* right = cons_string->second();
4311      i::ConsString* right_as_cons =
4312          i::String::VisitFlat(this, right, 0);
4313      if (!is_one_byte_) return false;
4314      // Standard recurse/iterate trick.
4315      if (left_as_cons != NULL && right_as_cons != NULL) {
4316        if (left->length() < right->length()) {
4317          CheckCons(left_as_cons);
4318          cons_string = right_as_cons;
4319        } else {
4320          CheckCons(right_as_cons);
4321          cons_string = left_as_cons;
4322        }
4323        // Check fast return.
4324        if (!is_one_byte_) return false;
4325        continue;
4326      }
4327      // Descend left in place.
4328      if (left_as_cons != NULL) {
4329        cons_string = left_as_cons;
4330        continue;
4331      }
4332      // Descend right in place.
4333      if (right_as_cons != NULL) {
4334        cons_string = right_as_cons;
4335        continue;
4336      }
4337      // Terminate.
4338      break;
4339    }
4340    return is_one_byte_;
4341  }
4342  bool is_one_byte_;
4343  DISALLOW_COPY_AND_ASSIGN(ContainsOnlyOneByteHelper);
4344};
4345
4346
4347bool String::ContainsOnlyOneByte() const {
4348  i::Handle<i::String> str = Utils::OpenHandle(this);
4349  if (str->HasOnlyOneByteChars()) return true;
4350  ContainsOnlyOneByteHelper helper;
4351  return helper.Check(*str);
4352}
4353
4354
4355class Utf8LengthHelper : public i::AllStatic {
4356 public:
4357  enum State {
4358    kEndsWithLeadingSurrogate = 1 << 0,
4359    kStartsWithTrailingSurrogate = 1 << 1,
4360    kLeftmostEdgeIsCalculated = 1 << 2,
4361    kRightmostEdgeIsCalculated = 1 << 3,
4362    kLeftmostEdgeIsSurrogate = 1 << 4,
4363    kRightmostEdgeIsSurrogate = 1 << 5
4364  };
4365
4366  static const uint8_t kInitialState = 0;
4367
4368  static inline bool EndsWithSurrogate(uint8_t state) {
4369    return state & kEndsWithLeadingSurrogate;
4370  }
4371
4372  static inline bool StartsWithSurrogate(uint8_t state) {
4373    return state & kStartsWithTrailingSurrogate;
4374  }
4375
4376  class Visitor {
4377   public:
4378    Visitor() : utf8_length_(0), state_(kInitialState) {}
4379
4380    void VisitOneByteString(const uint8_t* chars, int length) {
4381      int utf8_length = 0;
4382      // Add in length 1 for each non-Latin1 character.
4383      for (int i = 0; i < length; i++) {
4384        utf8_length += *chars++ >> 7;
4385      }
4386      // Add in length 1 for each character.
4387      utf8_length_ = utf8_length + length;
4388      state_ = kInitialState;
4389    }
4390
4391    void VisitTwoByteString(const uint16_t* chars, int length) {
4392      int utf8_length = 0;
4393      int last_character = unibrow::Utf16::kNoPreviousCharacter;
4394      for (int i = 0; i < length; i++) {
4395        uint16_t c = chars[i];
4396        utf8_length += unibrow::Utf8::Length(c, last_character);
4397        last_character = c;
4398      }
4399      utf8_length_ = utf8_length;
4400      uint8_t state = 0;
4401      if (unibrow::Utf16::IsTrailSurrogate(chars[0])) {
4402        state |= kStartsWithTrailingSurrogate;
4403      }
4404      if (unibrow::Utf16::IsLeadSurrogate(chars[length-1])) {
4405        state |= kEndsWithLeadingSurrogate;
4406      }
4407      state_ = state;
4408    }
4409
4410    static i::ConsString* VisitFlat(i::String* string,
4411                                    int* length,
4412                                    uint8_t* state) {
4413      Visitor visitor;
4414      i::ConsString* cons_string = i::String::VisitFlat(&visitor, string);
4415      *length = visitor.utf8_length_;
4416      *state = visitor.state_;
4417      return cons_string;
4418    }
4419
4420   private:
4421    int utf8_length_;
4422    uint8_t state_;
4423    DISALLOW_COPY_AND_ASSIGN(Visitor);
4424  };
4425
4426  static inline void MergeLeafLeft(int* length,
4427                                   uint8_t* state,
4428                                   uint8_t leaf_state) {
4429    bool edge_surrogate = StartsWithSurrogate(leaf_state);
4430    if (!(*state & kLeftmostEdgeIsCalculated)) {
4431      DCHECK(!(*state & kLeftmostEdgeIsSurrogate));
4432      *state |= kLeftmostEdgeIsCalculated
4433          | (edge_surrogate ? kLeftmostEdgeIsSurrogate : 0);
4434    } else if (EndsWithSurrogate(*state) && edge_surrogate) {
4435      *length -= unibrow::Utf8::kBytesSavedByCombiningSurrogates;
4436    }
4437    if (EndsWithSurrogate(leaf_state)) {
4438      *state |= kEndsWithLeadingSurrogate;
4439    } else {
4440      *state &= ~kEndsWithLeadingSurrogate;
4441    }
4442  }
4443
4444  static inline void MergeLeafRight(int* length,
4445                                    uint8_t* state,
4446                                    uint8_t leaf_state) {
4447    bool edge_surrogate = EndsWithSurrogate(leaf_state);
4448    if (!(*state & kRightmostEdgeIsCalculated)) {
4449      DCHECK(!(*state & kRightmostEdgeIsSurrogate));
4450      *state |= (kRightmostEdgeIsCalculated
4451                 | (edge_surrogate ? kRightmostEdgeIsSurrogate : 0));
4452    } else if (edge_surrogate && StartsWithSurrogate(*state)) {
4453      *length -= unibrow::Utf8::kBytesSavedByCombiningSurrogates;
4454    }
4455    if (StartsWithSurrogate(leaf_state)) {
4456      *state |= kStartsWithTrailingSurrogate;
4457    } else {
4458      *state &= ~kStartsWithTrailingSurrogate;
4459    }
4460  }
4461
4462  static inline void MergeTerminal(int* length,
4463                                   uint8_t state,
4464                                   uint8_t* state_out) {
4465    DCHECK((state & kLeftmostEdgeIsCalculated) &&
4466           (state & kRightmostEdgeIsCalculated));
4467    if (EndsWithSurrogate(state) && StartsWithSurrogate(state)) {
4468      *length -= unibrow::Utf8::kBytesSavedByCombiningSurrogates;
4469    }
4470    *state_out = kInitialState |
4471        (state & kLeftmostEdgeIsSurrogate ? kStartsWithTrailingSurrogate : 0) |
4472        (state & kRightmostEdgeIsSurrogate ? kEndsWithLeadingSurrogate : 0);
4473  }
4474
4475  static int Calculate(i::ConsString* current, uint8_t* state_out) {
4476    using namespace internal;
4477    int total_length = 0;
4478    uint8_t state = kInitialState;
4479    while (true) {
4480      i::String* left = current->first();
4481      i::String* right = current->second();
4482      uint8_t right_leaf_state;
4483      uint8_t left_leaf_state;
4484      int leaf_length;
4485      ConsString* left_as_cons =
4486          Visitor::VisitFlat(left, &leaf_length, &left_leaf_state);
4487      if (left_as_cons == NULL) {
4488        total_length += leaf_length;
4489        MergeLeafLeft(&total_length, &state, left_leaf_state);
4490      }
4491      ConsString* right_as_cons =
4492          Visitor::VisitFlat(right, &leaf_length, &right_leaf_state);
4493      if (right_as_cons == NULL) {
4494        total_length += leaf_length;
4495        MergeLeafRight(&total_length, &state, right_leaf_state);
4496        if (left_as_cons != NULL) {
4497          // 1 Leaf node. Descend in place.
4498          current = left_as_cons;
4499          continue;
4500        } else {
4501          // Terminal node.
4502          MergeTerminal(&total_length, state, state_out);
4503          return total_length;
4504        }
4505      } else if (left_as_cons == NULL) {
4506        // 1 Leaf node. Descend in place.
4507        current = right_as_cons;
4508        continue;
4509      }
4510      // Both strings are ConsStrings.
4511      // Recurse on smallest.
4512      if (left->length() < right->length()) {
4513        total_length += Calculate(left_as_cons, &left_leaf_state);
4514        MergeLeafLeft(&total_length, &state, left_leaf_state);
4515        current = right_as_cons;
4516      } else {
4517        total_length += Calculate(right_as_cons, &right_leaf_state);
4518        MergeLeafRight(&total_length, &state, right_leaf_state);
4519        current = left_as_cons;
4520      }
4521    }
4522    UNREACHABLE();
4523    return 0;
4524  }
4525
4526  static inline int Calculate(i::ConsString* current) {
4527    uint8_t state = kInitialState;
4528    return Calculate(current, &state);
4529  }
4530
4531 private:
4532  DISALLOW_IMPLICIT_CONSTRUCTORS(Utf8LengthHelper);
4533};
4534
4535
4536static int Utf8Length(i::String* str, i::Isolate* isolate) {
4537  int length = str->length();
4538  if (length == 0) return 0;
4539  uint8_t state;
4540  i::ConsString* cons_string =
4541      Utf8LengthHelper::Visitor::VisitFlat(str, &length, &state);
4542  if (cons_string == NULL) return length;
4543  return Utf8LengthHelper::Calculate(cons_string);
4544}
4545
4546
4547int String::Utf8Length() const {
4548  i::Handle<i::String> str = Utils::OpenHandle(this);
4549  i::Isolate* isolate = str->GetIsolate();
4550  return v8::Utf8Length(*str, isolate);
4551}
4552
4553
4554class Utf8WriterVisitor {
4555 public:
4556  Utf8WriterVisitor(
4557      char* buffer,
4558      int capacity,
4559      bool skip_capacity_check,
4560      bool replace_invalid_utf8)
4561    : early_termination_(false),
4562      last_character_(unibrow::Utf16::kNoPreviousCharacter),
4563      buffer_(buffer),
4564      start_(buffer),
4565      capacity_(capacity),
4566      skip_capacity_check_(capacity == -1 || skip_capacity_check),
4567      replace_invalid_utf8_(replace_invalid_utf8),
4568      utf16_chars_read_(0) {
4569  }
4570
4571  static int WriteEndCharacter(uint16_t character,
4572                               int last_character,
4573                               int remaining,
4574                               char* const buffer,
4575                               bool replace_invalid_utf8) {
4576    using namespace unibrow;
4577    DCHECK(remaining > 0);
4578    // We can't use a local buffer here because Encode needs to modify
4579    // previous characters in the stream.  We know, however, that
4580    // exactly one character will be advanced.
4581    if (Utf16::IsSurrogatePair(last_character, character)) {
4582      int written = Utf8::Encode(buffer,
4583                                 character,
4584                                 last_character,
4585                                 replace_invalid_utf8);
4586      DCHECK(written == 1);
4587      return written;
4588    }
4589    // Use a scratch buffer to check the required characters.
4590    char temp_buffer[Utf8::kMaxEncodedSize];
4591    // Can't encode using last_character as gcc has array bounds issues.
4592    int written = Utf8::Encode(temp_buffer,
4593                               character,
4594                               Utf16::kNoPreviousCharacter,
4595                               replace_invalid_utf8);
4596    // Won't fit.
4597    if (written > remaining) return 0;
4598    // Copy over the character from temp_buffer.
4599    for (int j = 0; j < written; j++) {
4600      buffer[j] = temp_buffer[j];
4601    }
4602    return written;
4603  }
4604
4605  // Visit writes out a group of code units (chars) of a v8::String to the
4606  // internal buffer_. This is done in two phases. The first phase calculates a
4607  // pesimistic estimate (writable_length) on how many code units can be safely
4608  // written without exceeding the buffer capacity and without writing the last
4609  // code unit (it could be a lead surrogate). The estimated number of code
4610  // units is then written out in one go, and the reported byte usage is used
4611  // to correct the estimate. This is repeated until the estimate becomes <= 0
4612  // or all code units have been written out. The second phase writes out code
4613  // units until the buffer capacity is reached, would be exceeded by the next
4614  // unit, or all units have been written out.
4615  template<typename Char>
4616  void Visit(const Char* chars, const int length) {
4617    using namespace unibrow;
4618    DCHECK(!early_termination_);
4619    if (length == 0) return;
4620    // Copy state to stack.
4621    char* buffer = buffer_;
4622    int last_character =
4623        sizeof(Char) == 1 ? Utf16::kNoPreviousCharacter : last_character_;
4624    int i = 0;
4625    // Do a fast loop where there is no exit capacity check.
4626    while (true) {
4627      int fast_length;
4628      if (skip_capacity_check_) {
4629        fast_length = length;
4630      } else {