blob: 8274a735689ce3332670d0abc25cb832408366c4 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/api.h"
Ben Murdochf87a2032010-10-22 12:50:53 +01006
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007#include <string.h> // For memcpy, strlen.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#ifdef V8_USE_ADDRESS_SANITIZER
9#include <sanitizer/asan_interface.h>
10#endif // V8_USE_ADDRESS_SANITIZER
11#include <cmath> // For isnan.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012#include <limits>
13#include <vector>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014#include "include/v8-debug.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015#include "include/v8-experimental.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016#include "include/v8-profiler.h"
17#include "include/v8-testing.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018#include "src/api-experimental.h"
19#include "src/api-natives.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000020#include "src/assert-scope.h"
21#include "src/background-parsing-task.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040022#include "src/base/functional.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023#include "src/base/platform/platform.h"
24#include "src/base/platform/time.h"
25#include "src/base/utils/random-number-generator.h"
26#include "src/bootstrapper.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000027#include "src/char-predicates-inl.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028#include "src/code-stubs.h"
29#include "src/compiler.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000030#include "src/context-measure.h"
31#include "src/contexts.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032#include "src/conversions-inl.h"
33#include "src/counters.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034#include "src/debug/debug.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035#include "src/deoptimizer.h"
36#include "src/execution.h"
37#include "src/global-handles.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038#include "src/icu_util.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000039#include "src/isolate-inl.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040#include "src/messages.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041#include "src/parsing/json-parser.h"
42#include "src/parsing/parser.h"
43#include "src/parsing/scanner-character-streams.h"
44#include "src/pending-compilation-error-handler.h"
45#include "src/profiler/cpu-profiler.h"
46#include "src/profiler/heap-profiler.h"
47#include "src/profiler/heap-snapshot-generator-inl.h"
48#include "src/profiler/profile-generator-inl.h"
49#include "src/profiler/sampler.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050#include "src/property.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000051#include "src/property-descriptor.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052#include "src/property-details.h"
53#include "src/prototype.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040054#include "src/runtime/runtime.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055#include "src/runtime-profiler.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056#include "src/simulator.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000057#include "src/snapshot/natives.h"
58#include "src/snapshot/snapshot.h"
59#include "src/startup-data-util.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060#include "src/unicode-inl.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000061#include "src/v8.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062#include "src/v8threads.h"
63#include "src/version.h"
64#include "src/vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000065
66
Steve Blocka7e24c12009-10-30 11:49:00 +000067namespace v8 {
68
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000069#define LOG_API(isolate, expr) LOG(isolate, ApiEntryCall(expr))
Steve Blocka7e24c12009-10-30 11:49:00 +000070
71
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000072#define ENTER_V8(isolate) i::VMState<v8::OTHER> __state__((isolate))
73
74
75#define PREPARE_FOR_EXECUTION_GENERIC(isolate, context, function_name, \
76 bailout_value, HandleScopeClass, \
77 do_callback) \
78 if (IsExecutionTerminatingCheck(isolate)) { \
79 return bailout_value; \
80 } \
81 HandleScopeClass handle_scope(isolate); \
82 CallDepthScope call_depth_scope(isolate, context, do_callback); \
83 LOG_API(isolate, function_name); \
84 ENTER_V8(isolate); \
Steve Blocka7e24c12009-10-30 11:49:00 +000085 bool has_pending_exception = false
86
87
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000088#define PREPARE_FOR_EXECUTION_WITH_CONTEXT( \
89 context, function_name, bailout_value, HandleScopeClass, do_callback) \
90 auto isolate = context.IsEmpty() \
91 ? i::Isolate::Current() \
92 : reinterpret_cast<i::Isolate*>(context->GetIsolate()); \
93 PREPARE_FOR_EXECUTION_GENERIC(isolate, context, function_name, \
94 bailout_value, HandleScopeClass, do_callback);
95
96
97#define PREPARE_FOR_EXECUTION_WITH_ISOLATE(isolate, function_name, T) \
98 PREPARE_FOR_EXECUTION_GENERIC(isolate, Local<Context>(), function_name, \
99 MaybeLocal<T>(), InternalEscapableScope, \
100 false);
101
102
103#define PREPARE_FOR_EXECUTION(context, function_name, T) \
104 PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, function_name, MaybeLocal<T>(), \
105 InternalEscapableScope, false)
106
107
108#define PREPARE_FOR_EXECUTION_WITH_CALLBACK(context, function_name, T) \
109 PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, function_name, MaybeLocal<T>(), \
110 InternalEscapableScope, true)
111
112
113#define PREPARE_FOR_EXECUTION_PRIMITIVE(context, function_name, T) \
114 PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, function_name, Nothing<T>(), \
115 i::HandleScope, false)
116
117
118#define EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, value) \
119 do { \
120 if (has_pending_exception) { \
121 call_depth_scope.Escape(); \
122 return value; \
123 } \
Steve Blocka7e24c12009-10-30 11:49:00 +0000124 } while (false)
125
126
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000127#define RETURN_ON_FAILED_EXECUTION(T) \
128 EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, MaybeLocal<T>())
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100129
130
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000131#define RETURN_ON_FAILED_EXECUTION_PRIMITIVE(T) \
132 EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, Nothing<T>())
133
134
135#define RETURN_TO_LOCAL_UNCHECKED(maybe_local, T) \
136 return maybe_local.FromMaybe(Local<T>());
137
138
139#define RETURN_ESCAPED(value) return handle_scope.Escape(value);
140
141
142namespace {
143
144Local<Context> ContextFromHeapObject(i::Handle<i::Object> obj) {
145 return reinterpret_cast<v8::Isolate*>(i::HeapObject::cast(*obj)->GetIsolate())
146 ->GetCurrentContext();
147}
148
149class InternalEscapableScope : public v8::EscapableHandleScope {
150 public:
151 explicit inline InternalEscapableScope(i::Isolate* isolate)
152 : v8::EscapableHandleScope(reinterpret_cast<v8::Isolate*>(isolate)) {}
153};
154
155
156class CallDepthScope {
157 public:
158 explicit CallDepthScope(i::Isolate* isolate, Local<Context> context,
159 bool do_callback)
160 : isolate_(isolate),
161 context_(context),
162 escaped_(false),
163 do_callback_(do_callback) {
164 // TODO(dcarney): remove this when blink stops crashing.
165 DCHECK(!isolate_->external_caught_exception());
166 isolate_->IncrementJsCallsFromApiCounter();
167 isolate_->handle_scope_implementer()->IncrementCallDepth();
168 if (!context_.IsEmpty()) context_->Enter();
169 }
170 ~CallDepthScope() {
171 if (!context_.IsEmpty()) context_->Exit();
172 if (!escaped_) isolate_->handle_scope_implementer()->DecrementCallDepth();
173 if (do_callback_) isolate_->FireCallCompletedCallback();
174 }
175
176 void Escape() {
177 DCHECK(!escaped_);
178 escaped_ = true;
179 auto handle_scope_implementer = isolate_->handle_scope_implementer();
180 handle_scope_implementer->DecrementCallDepth();
181 bool call_depth_is_zero = handle_scope_implementer->CallDepthIsZero();
182 isolate_->OptionalRescheduleException(call_depth_is_zero);
183 }
184
185 private:
186 i::Isolate* const isolate_;
187 Local<Context> context_;
188 bool escaped_;
189 bool do_callback_;
190};
191
192} // namespace
193
194
195static ScriptOrigin GetScriptOriginForScript(i::Isolate* isolate,
196 i::Handle<i::Script> script) {
197 i::Handle<i::Object> scriptName(i::Script::GetNameOrSourceURL(script));
198 i::Handle<i::Object> source_map_url(script->source_mapping_url(), isolate);
199 v8::Isolate* v8_isolate =
200 reinterpret_cast<v8::Isolate*>(script->GetIsolate());
201 ScriptOriginOptions options(script->origin_options());
202 v8::ScriptOrigin origin(
203 Utils::ToLocal(scriptName),
204 v8::Integer::New(v8_isolate, script->line_offset()),
205 v8::Integer::New(v8_isolate, script->column_offset()),
206 v8::Boolean::New(v8_isolate, options.IsSharedCrossOrigin()),
207 v8::Integer::New(v8_isolate, script->id()),
208 v8::Boolean::New(v8_isolate, options.IsEmbedderDebugScript()),
209 Utils::ToLocal(source_map_url),
210 v8::Boolean::New(v8_isolate, options.IsOpaque()));
211 return origin;
212}
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100213
214
Steve Blocka7e24c12009-10-30 11:49:00 +0000215// --- E x c e p t i o n B e h a v i o r ---
216
217
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800218void i::FatalProcessOutOfMemory(const char* location) {
219 i::V8::FatalProcessOutOfMemory(location, false);
220}
221
Steve Blocka7e24c12009-10-30 11:49:00 +0000222
223// When V8 cannot allocated memory FatalProcessOutOfMemory is called.
224// The default fatal error handler is called and execution is stopped.
Ben Murdochbb769b22010-08-11 14:56:33 +0100225void i::V8::FatalProcessOutOfMemory(const char* location, bool take_snapshot) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000226 i::Isolate* isolate = i::Isolate::Current();
227 char last_few_messages[Heap::kTraceRingBufferSize + 1];
228 char js_stacktrace[Heap::kStacktraceBufferSize + 1];
229 memset(last_few_messages, 0, Heap::kTraceRingBufferSize + 1);
230 memset(js_stacktrace, 0, Heap::kStacktraceBufferSize + 1);
231
Steve Blockd0582a62009-12-15 09:54:21 +0000232 i::HeapStats heap_stats;
233 int start_marker;
234 heap_stats.start_marker = &start_marker;
235 int new_space_size;
236 heap_stats.new_space_size = &new_space_size;
237 int new_space_capacity;
238 heap_stats.new_space_capacity = &new_space_capacity;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000239 intptr_t old_space_size;
240 heap_stats.old_space_size = &old_space_size;
241 intptr_t old_space_capacity;
242 heap_stats.old_space_capacity = &old_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100243 intptr_t code_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000244 heap_stats.code_space_size = &code_space_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100245 intptr_t code_space_capacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000246 heap_stats.code_space_capacity = &code_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100247 intptr_t map_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000248 heap_stats.map_space_size = &map_space_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100249 intptr_t map_space_capacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000250 heap_stats.map_space_capacity = &map_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100251 intptr_t lo_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000252 heap_stats.lo_space_size = &lo_space_size;
253 int global_handle_count;
254 heap_stats.global_handle_count = &global_handle_count;
255 int weak_global_handle_count;
256 heap_stats.weak_global_handle_count = &weak_global_handle_count;
257 int pending_global_handle_count;
258 heap_stats.pending_global_handle_count = &pending_global_handle_count;
259 int near_death_global_handle_count;
260 heap_stats.near_death_global_handle_count = &near_death_global_handle_count;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000261 int free_global_handle_count;
262 heap_stats.free_global_handle_count = &free_global_handle_count;
Ben Murdochf87a2032010-10-22 12:50:53 +0100263 intptr_t memory_allocator_size;
Ben Murdochbb769b22010-08-11 14:56:33 +0100264 heap_stats.memory_allocator_size = &memory_allocator_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100265 intptr_t memory_allocator_capacity;
Ben Murdochbb769b22010-08-11 14:56:33 +0100266 heap_stats.memory_allocator_capacity = &memory_allocator_capacity;
267 int objects_per_type[LAST_TYPE + 1] = {0};
268 heap_stats.objects_per_type = objects_per_type;
269 int size_per_type[LAST_TYPE + 1] = {0};
270 heap_stats.size_per_type = size_per_type;
Iain Merrick75681382010-08-19 15:07:18 +0100271 int os_error;
272 heap_stats.os_error = &os_error;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000273 heap_stats.last_few_messages = last_few_messages;
274 heap_stats.js_stacktrace = js_stacktrace;
Steve Blockd0582a62009-12-15 09:54:21 +0000275 int end_marker;
276 heap_stats.end_marker = &end_marker;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277 if (isolate->heap()->HasBeenSetUp()) {
278 // BUG(1718): Don't use the take_snapshot since we don't support
279 // HeapIterator here without doing a special GC.
280 isolate->heap()->RecordStats(&heap_stats, false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000281 char* first_newline = strchr(last_few_messages, '\n');
282 if (first_newline == NULL || first_newline[1] == '\0')
283 first_newline = last_few_messages;
284 PrintF("\n<--- Last few GCs --->\n%s\n", first_newline);
285 PrintF("\n<--- JS stacktrace --->\n%s\n", js_stacktrace);
Steve Blocka7e24c12009-10-30 11:49:00 +0000286 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000287 Utils::ApiCheck(false, location, "Allocation failed - process out of memory");
288 // If the fatal error handler returns, we stop execution.
289 FATAL("API fatal error handler returned after process out of memory");
Steve Blocka7e24c12009-10-30 11:49:00 +0000290}
291
292
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000293void Utils::ReportApiFailure(const char* location, const char* message) {
294 i::Isolate* isolate = i::Isolate::Current();
295 FatalErrorCallback callback = isolate->exception_behavior();
296 if (callback == NULL) {
297 base::OS::PrintError("\n#\n# Fatal error in %s\n# %s\n#\n\n", location,
298 message);
299 base::OS::Abort();
300 } else {
301 callback(location, message);
302 }
303 isolate->SignalFatalError();
Steve Blocka7e24c12009-10-30 11:49:00 +0000304}
305
306
Steve Block44f0eee2011-05-26 01:26:41 +0100307static inline bool IsExecutionTerminatingCheck(i::Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100308 if (isolate->has_scheduled_exception()) {
309 return isolate->scheduled_exception() ==
310 isolate->heap()->termination_exception();
311 }
312 return false;
313}
314
315
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000316void V8::SetNativesDataBlob(StartupData* natives_blob) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400317 i::V8::SetNativesBlob(natives_blob);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000318}
319
320
321void V8::SetSnapshotDataBlob(StartupData* snapshot_blob) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400322 i::V8::SetSnapshotBlob(snapshot_blob);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000323}
324
325
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000326bool RunExtraCode(Isolate* isolate, Local<Context> context,
327 const char* utf8_source) {
328 // Run custom script if provided.
329 base::ElapsedTimer timer;
330 timer.Start();
331 TryCatch try_catch(isolate);
332 Local<String> source_string;
333 if (!String::NewFromUtf8(isolate, utf8_source, NewStringType::kNormal)
334 .ToLocal(&source_string)) {
335 return false;
336 }
337 Local<String> resource_name =
338 String::NewFromUtf8(isolate, "<embedded script>", NewStringType::kNormal)
339 .ToLocalChecked();
340 ScriptOrigin origin(resource_name);
341 ScriptCompiler::Source source(source_string, origin);
342 Local<Script> script;
343 if (!ScriptCompiler::Compile(context, &source).ToLocal(&script)) return false;
344 if (script->Run(context).IsEmpty()) return false;
345 if (i::FLAG_profile_deserialization) {
346 i::PrintF("Executing custom snapshot script took %0.3f ms\n",
347 timer.Elapsed().InMillisecondsF());
348 }
349 timer.Stop();
350 CHECK(!try_catch.HasCaught());
351 return true;
352}
353
354
355namespace {
356
357class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
358 public:
359 virtual void* Allocate(size_t length) {
360 void* data = AllocateUninitialized(length);
361 return data == NULL ? data : memset(data, 0, length);
362 }
363 virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
364 virtual void Free(void* data, size_t) { free(data); }
365};
366
367} // namespace
368
369
370StartupData V8::CreateSnapshotDataBlob(const char* custom_source) {
371 i::Isolate* internal_isolate = new i::Isolate(true);
372 ArrayBufferAllocator allocator;
373 internal_isolate->set_array_buffer_allocator(&allocator);
374 Isolate* isolate = reinterpret_cast<Isolate*>(internal_isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400375 StartupData result = {NULL, 0};
376 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000377 base::ElapsedTimer timer;
378 timer.Start();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400379 Isolate::Scope isolate_scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000380 internal_isolate->Init(NULL);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400381 Persistent<Context> context;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000382 i::Snapshot::Metadata metadata;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400383 {
384 HandleScope handle_scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000385 Local<Context> new_context = Context::New(isolate);
386 context.Reset(isolate, new_context);
387 if (custom_source != NULL) {
388 metadata.set_embeds_script(true);
389 Context::Scope context_scope(new_context);
390 if (!RunExtraCode(isolate, new_context, custom_source)) context.Reset();
391 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400392 }
393 if (!context.IsEmpty()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400394 // If we don't do this then we end up with a stray root pointing at the
395 // context even after we have disposed of the context.
396 internal_isolate->heap()->CollectAllAvailableGarbage("mksnapshot");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000397
398 // GC may have cleared weak cells, so compact any WeakFixedArrays
399 // found on the heap.
400 i::HeapIterator iterator(internal_isolate->heap(),
401 i::HeapIterator::kFilterUnreachable);
402 for (i::HeapObject* o = iterator.next(); o != NULL; o = iterator.next()) {
403 if (o->IsPrototypeInfo()) {
404 i::Object* prototype_users =
405 i::PrototypeInfo::cast(o)->prototype_users();
406 if (prototype_users->IsWeakFixedArray()) {
407 i::WeakFixedArray* array = i::WeakFixedArray::cast(prototype_users);
408 array->Compact<i::JSObject::PrototypeRegistryCompactionCallback>();
409 }
410 } else if (o->IsScript()) {
411 i::Object* shared_list = i::Script::cast(o)->shared_function_infos();
412 if (shared_list->IsWeakFixedArray()) {
413 i::WeakFixedArray* array = i::WeakFixedArray::cast(shared_list);
414 array->Compact<i::WeakFixedArray::NullCallback>();
415 }
416 }
417 }
418
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400419 i::Object* raw_context = *v8::Utils::OpenPersistent(context);
420 context.Reset();
Steve Blocka7e24c12009-10-30 11:49:00 +0000421
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400422 i::SnapshotByteSink snapshot_sink;
423 i::StartupSerializer ser(internal_isolate, &snapshot_sink);
424 ser.SerializeStrongReferences();
Steve Blocka7e24c12009-10-30 11:49:00 +0000425
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400426 i::SnapshotByteSink context_sink;
427 i::PartialSerializer context_ser(internal_isolate, &ser, &context_sink);
428 context_ser.Serialize(&raw_context);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000429 ser.SerializeWeakReferencesAndDeferred();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400430
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000431 result = i::Snapshot::CreateSnapshotBlob(ser, context_ser, metadata);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400432 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000433 if (i::FLAG_profile_deserialization) {
434 i::PrintF("Creating snapshot took %0.3f ms\n",
435 timer.Elapsed().InMillisecondsF());
436 }
437 timer.Stop();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400438 }
439 isolate->Dispose();
440 return result;
Ben Murdoch257744e2011-11-30 15:57:28 +0000441}
442
443
Steve Blocka7e24c12009-10-30 11:49:00 +0000444void V8::SetFlagsFromString(const char* str, int length) {
445 i::FlagList::SetFlagsFromString(str, length);
446}
447
448
449void V8::SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags) {
450 i::FlagList::SetFlagsFromCommandLine(argc, argv, remove_flags);
451}
452
453
Steve Blocka7e24c12009-10-30 11:49:00 +0000454RegisteredExtension* RegisteredExtension::first_extension_ = NULL;
455
456
457RegisteredExtension::RegisteredExtension(Extension* extension)
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100458 : extension_(extension) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000459
460
461void RegisteredExtension::Register(RegisteredExtension* that) {
Steve Block44f0eee2011-05-26 01:26:41 +0100462 that->next_ = first_extension_;
463 first_extension_ = that;
Steve Blocka7e24c12009-10-30 11:49:00 +0000464}
465
466
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000467void RegisteredExtension::UnregisterAll() {
468 RegisteredExtension* re = first_extension_;
469 while (re != NULL) {
470 RegisteredExtension* next = re->next();
471 delete re;
472 re = next;
473 }
474 first_extension_ = NULL;
475}
476
477
Steve Blocka7e24c12009-10-30 11:49:00 +0000478void RegisterExtension(Extension* that) {
479 RegisteredExtension* extension = new RegisteredExtension(that);
480 RegisteredExtension::Register(extension);
481}
482
483
484Extension::Extension(const char* name,
485 const char* source,
486 int dep_count,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100487 const char** deps,
488 int source_length)
Steve Blocka7e24c12009-10-30 11:49:00 +0000489 : name_(name),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100490 source_length_(source_length >= 0 ?
491 source_length :
492 (source ? static_cast<int>(strlen(source)) : 0)),
493 source_(source, source_length_),
Steve Blocka7e24c12009-10-30 11:49:00 +0000494 dep_count_(dep_count),
495 deps_(deps),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000496 auto_enable_(false) {
497 CHECK(source != NULL || source_length_ == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000498}
499
500
501ResourceConstraints::ResourceConstraints()
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000502 : max_semi_space_size_(0),
503 max_old_space_size_(0),
504 max_executable_size_(0),
505 stack_limit_(NULL),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000506 code_range_size_(0) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000507
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000508void ResourceConstraints::ConfigureDefaults(uint64_t physical_memory,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000509 uint64_t virtual_memory_limit) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000510#if V8_OS_ANDROID
511 // Android has higher physical memory requirements before raising the maximum
512 // heap size limits since it has no swap space.
513 const uint64_t low_limit = 512ul * i::MB;
514 const uint64_t medium_limit = 1ul * i::GB;
515 const uint64_t high_limit = 2ul * i::GB;
516#else
517 const uint64_t low_limit = 512ul * i::MB;
518 const uint64_t medium_limit = 768ul * i::MB;
519 const uint64_t high_limit = 1ul * i::GB;
520#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000521
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000522 if (physical_memory <= low_limit) {
523 set_max_semi_space_size(i::Heap::kMaxSemiSpaceSizeLowMemoryDevice);
524 set_max_old_space_size(i::Heap::kMaxOldSpaceSizeLowMemoryDevice);
525 set_max_executable_size(i::Heap::kMaxExecutableSizeLowMemoryDevice);
526 } else if (physical_memory <= medium_limit) {
527 set_max_semi_space_size(i::Heap::kMaxSemiSpaceSizeMediumMemoryDevice);
528 set_max_old_space_size(i::Heap::kMaxOldSpaceSizeMediumMemoryDevice);
529 set_max_executable_size(i::Heap::kMaxExecutableSizeMediumMemoryDevice);
530 } else if (physical_memory <= high_limit) {
531 set_max_semi_space_size(i::Heap::kMaxSemiSpaceSizeHighMemoryDevice);
532 set_max_old_space_size(i::Heap::kMaxOldSpaceSizeHighMemoryDevice);
533 set_max_executable_size(i::Heap::kMaxExecutableSizeHighMemoryDevice);
534 } else {
535 set_max_semi_space_size(i::Heap::kMaxSemiSpaceSizeHugeMemoryDevice);
536 set_max_old_space_size(i::Heap::kMaxOldSpaceSizeHugeMemoryDevice);
537 set_max_executable_size(i::Heap::kMaxExecutableSizeHugeMemoryDevice);
Steve Blocka7e24c12009-10-30 11:49:00 +0000538 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000539
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000540 if (virtual_memory_limit > 0 && i::kRequiresCodeRange) {
541 // Reserve no more than 1/8 of the memory for the code range, but at most
542 // kMaximalCodeRangeSize.
543 set_code_range_size(
544 i::Min(i::kMaximalCodeRangeSize / i::MB,
545 static_cast<size_t>((virtual_memory_limit >> 3) / i::MB)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000546 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000547}
548
549
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000550void SetResourceConstraints(i::Isolate* isolate,
551 const ResourceConstraints& constraints) {
552 int semi_space_size = constraints.max_semi_space_size();
553 int old_space_size = constraints.max_old_space_size();
554 int max_executable_size = constraints.max_executable_size();
555 size_t code_range_size = constraints.code_range_size();
556 if (semi_space_size != 0 || old_space_size != 0 ||
557 max_executable_size != 0 || code_range_size != 0) {
558 isolate->heap()->ConfigureHeap(semi_space_size, old_space_size,
559 max_executable_size, code_range_size);
560 }
561 if (constraints.stack_limit() != NULL) {
562 uintptr_t limit = reinterpret_cast<uintptr_t>(constraints.stack_limit());
563 isolate->stack_guard()->SetStackLimit(limit);
564 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000565}
566
567
568i::Object** V8::GlobalizeReference(i::Isolate* isolate, i::Object** obj) {
Steve Block44f0eee2011-05-26 01:26:41 +0100569 LOG_API(isolate, "Persistent::New");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000570 i::Handle<i::Object> result = isolate->global_handles()->Create(*obj);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400571#ifdef VERIFY_HEAP
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000572 if (i::FLAG_verify_heap) {
573 (*obj)->ObjectVerify();
574 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400575#endif // VERIFY_HEAP
Steve Blocka7e24c12009-10-30 11:49:00 +0000576 return result.location();
577}
578
579
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000580i::Object** V8::CopyPersistent(i::Object** obj) {
581 i::Handle<i::Object> result = i::GlobalHandles::CopyGlobal(obj);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400582#ifdef VERIFY_HEAP
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000583 if (i::FLAG_verify_heap) {
584 (*obj)->ObjectVerify();
585 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400586#endif // VERIFY_HEAP
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000587 return result.location();
Steve Blocka7e24c12009-10-30 11:49:00 +0000588}
589
590
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400591void V8::MakeWeak(i::Object** object, void* parameter,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000592 WeakCallback weak_callback) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400593 i::GlobalHandles::MakeWeak(object, parameter, weak_callback);
594}
595
596
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000597void V8::MakeWeak(i::Object** object, void* parameter,
598 int internal_field_index1, int internal_field_index2,
599 WeakCallbackInfo<void>::Callback weak_callback) {
600 WeakCallbackType type = WeakCallbackType::kParameter;
601 if (internal_field_index1 == 0) {
602 if (internal_field_index2 == 1) {
603 type = WeakCallbackType::kInternalFields;
604 } else {
605 DCHECK_EQ(internal_field_index2, -1);
606 type = WeakCallbackType::kInternalFields;
607 }
608 } else {
609 DCHECK_EQ(internal_field_index1, -1);
610 DCHECK_EQ(internal_field_index2, -1);
611 }
612 i::GlobalHandles::MakeWeak(object, parameter, weak_callback, type);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400613}
614
615
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000616void V8::MakeWeak(i::Object** object, void* parameter,
617 WeakCallbackInfo<void>::Callback weak_callback,
618 WeakCallbackType type) {
619 i::GlobalHandles::MakeWeak(object, parameter, weak_callback, type);
Steve Blocka7e24c12009-10-30 11:49:00 +0000620}
621
622
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000623void* V8::ClearWeak(i::Object** obj) {
624 return i::GlobalHandles::ClearWeakness(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +0000625}
626
627
628void V8::DisposeGlobal(i::Object** obj) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000629 i::GlobalHandles::Destroy(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +0000630}
631
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000632
633void V8::Eternalize(Isolate* v8_isolate, Value* value, int* index) {
634 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
635 i::Object* object = *Utils::OpenHandle(value);
636 isolate->eternal_handles()->Create(isolate, object, index);
637}
638
639
640Local<Value> V8::GetEternal(Isolate* v8_isolate, int index) {
641 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
642 return Utils::ToLocal(isolate->eternal_handles()->Get(index));
643}
644
645
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000646void V8::FromJustIsNothing() {
647 Utils::ApiCheck(false, "v8::FromJust", "Maybe value is Nothing.");
648}
649
650
651void V8::ToLocalEmpty() {
652 Utils::ApiCheck(false, "v8::ToLocalChecked", "Empty MaybeLocal.");
653}
654
655
656void V8::InternalFieldOutOfBounds(int index) {
657 Utils::ApiCheck(0 <= index && index < kInternalFieldsInWeakCallback,
658 "WeakCallbackInfo::GetInternalField",
659 "Internal field out of bounds.");
660}
661
662
Steve Blocka7e24c12009-10-30 11:49:00 +0000663// --- H a n d l e s ---
664
665
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000666HandleScope::HandleScope(Isolate* isolate) {
667 Initialize(isolate);
668}
669
670
671void HandleScope::Initialize(Isolate* isolate) {
672 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
673 // We do not want to check the correct usage of the Locker class all over the
674 // place, so we do it only here: Without a HandleScope, an embedder can do
675 // almost nothing, so it is enough to check in this central place.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000676 // We make an exception if the serializer is enabled, which means that the
677 // Isolate is exclusively used to create a snapshot.
678 Utils::ApiCheck(
679 !v8::Locker::IsActive() ||
680 internal_isolate->thread_manager()->IsLockedByCurrentThread() ||
681 internal_isolate->serializer_enabled(),
682 "HandleScope::HandleScope",
683 "Entering the V8 API without proper locking in place");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000684 i::HandleScopeData* current = internal_isolate->handle_scope_data();
685 isolate_ = internal_isolate;
Steve Block44f0eee2011-05-26 01:26:41 +0100686 prev_next_ = current->next;
687 prev_limit_ = current->limit;
Steve Block44f0eee2011-05-26 01:26:41 +0100688 current->level++;
Steve Blocka7e24c12009-10-30 11:49:00 +0000689}
690
691
692HandleScope::~HandleScope() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000693 i::HandleScope::CloseScope(isolate_, prev_next_, prev_limit_);
694}
695
696
697int HandleScope::NumberOfHandles(Isolate* isolate) {
698 return i::HandleScope::NumberOfHandles(
699 reinterpret_cast<i::Isolate*>(isolate));
700}
701
702
703i::Object** HandleScope::CreateHandle(i::Isolate* isolate, i::Object* value) {
704 return i::HandleScope::CreateHandle(isolate, value);
705}
706
707
708i::Object** HandleScope::CreateHandle(i::HeapObject* heap_object,
709 i::Object* value) {
710 DCHECK(heap_object->IsHeapObject());
711 return i::HandleScope::CreateHandle(heap_object->GetIsolate(), value);
712}
713
714
715EscapableHandleScope::EscapableHandleScope(Isolate* v8_isolate) {
716 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
717 escape_slot_ = CreateHandle(isolate, isolate->heap()->the_hole_value());
718 Initialize(v8_isolate);
719}
720
721
722i::Object** EscapableHandleScope::Escape(i::Object** escape_value) {
723 i::Heap* heap = reinterpret_cast<i::Isolate*>(GetIsolate())->heap();
724 Utils::ApiCheck(*escape_slot_ == heap->the_hole_value(),
725 "EscapeableHandleScope::Escape",
726 "Escape value set twice");
727 if (escape_value == NULL) {
728 *escape_slot_ = heap->undefined_value();
729 return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000730 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000731 *escape_slot_ = *escape_value;
732 return escape_slot_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000733}
734
735
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000736SealHandleScope::SealHandleScope(Isolate* isolate) {
737 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
738
739 isolate_ = internal_isolate;
740 i::HandleScopeData* current = internal_isolate->handle_scope_data();
741 prev_limit_ = current->limit;
742 current->limit = current->next;
743 prev_sealed_level_ = current->sealed_level;
744 current->sealed_level = current->level;
745}
746
747
748SealHandleScope::~SealHandleScope() {
749 i::HandleScopeData* current = isolate_->handle_scope_data();
750 DCHECK_EQ(current->next, current->limit);
751 current->limit = prev_limit_;
752 DCHECK_EQ(current->level, current->sealed_level);
753 current->sealed_level = prev_sealed_level_;
754}
755
756
Steve Blocka7e24c12009-10-30 11:49:00 +0000757void Context::Enter() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000758 i::Handle<i::Context> env = Utils::OpenHandle(this);
759 i::Isolate* isolate = env->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100760 ENTER_V8(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000761 i::HandleScopeImplementer* impl = isolate->handle_scope_implementer();
762 impl->EnterContext(env);
763 impl->SaveContext(isolate->context());
Steve Block44f0eee2011-05-26 01:26:41 +0100764 isolate->set_context(*env);
Steve Blocka7e24c12009-10-30 11:49:00 +0000765}
766
767
768void Context::Exit() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000769 i::Handle<i::Context> env = Utils::OpenHandle(this);
770 i::Isolate* isolate = env->GetIsolate();
771 ENTER_V8(isolate);
772 i::HandleScopeImplementer* impl = isolate->handle_scope_implementer();
773 if (!Utils::ApiCheck(impl->LastEnteredContextWas(env),
774 "v8::Context::Exit()",
775 "Cannot exit non-entered context")) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000776 return;
777 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000778 impl->LeaveContext();
779 isolate->set_context(impl->RestoreContext());
Steve Blocka7e24c12009-10-30 11:49:00 +0000780}
781
782
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000783static void* DecodeSmiToAligned(i::Object* value, const char* location) {
784 Utils::ApiCheck(value->IsSmi(), location, "Not a Smi");
785 return reinterpret_cast<void*>(value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000786}
787
788
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000789static i::Smi* EncodeAlignedAsSmi(void* value, const char* location) {
790 i::Smi* smi = reinterpret_cast<i::Smi*>(value);
791 Utils::ApiCheck(smi->IsSmi(), location, "Pointer is not aligned");
792 return smi;
793}
794
795
796static i::Handle<i::FixedArray> EmbedderDataFor(Context* context,
797 int index,
798 bool can_grow,
799 const char* location) {
800 i::Handle<i::Context> env = Utils::OpenHandle(context);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000801 i::Isolate* isolate = env->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000802 bool ok =
803 Utils::ApiCheck(env->IsNativeContext(),
804 location,
805 "Not a native context") &&
806 Utils::ApiCheck(index >= 0, location, "Negative index");
807 if (!ok) return i::Handle<i::FixedArray>();
808 i::Handle<i::FixedArray> data(env->embedder_data());
809 if (index < data->length()) return data;
810 if (!Utils::ApiCheck(can_grow, location, "Index too large")) {
811 return i::Handle<i::FixedArray>();
Steve Block44f0eee2011-05-26 01:26:41 +0100812 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000813 int new_size = i::Max(index, data->length() << 1) + 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000814 int grow_by = new_size - data->length();
815 data = isolate->factory()->CopyFixedArrayAndGrow(data, grow_by);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000816 env->set_embedder_data(*data);
817 return data;
818}
819
820
821v8::Local<v8::Value> Context::SlowGetEmbedderData(int index) {
822 const char* location = "v8::Context::GetEmbedderData()";
823 i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, false, location);
824 if (data.is_null()) return Local<Value>();
825 i::Handle<i::Object> result(data->get(index), data->GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000826 return Utils::ToLocal(result);
827}
828
829
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000830void Context::SetEmbedderData(int index, v8::Local<Value> value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000831 const char* location = "v8::Context::SetEmbedderData()";
832 i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, true, location);
833 if (data.is_null()) return;
834 i::Handle<i::Object> val = Utils::OpenHandle(*value);
835 data->set(index, *val);
836 DCHECK_EQ(*Utils::OpenHandle(*value),
837 *Utils::OpenHandle(*GetEmbedderData(index)));
838}
Steve Blocka7e24c12009-10-30 11:49:00 +0000839
Steve Blocka7e24c12009-10-30 11:49:00 +0000840
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000841void* Context::SlowGetAlignedPointerFromEmbedderData(int index) {
842 const char* location = "v8::Context::GetAlignedPointerFromEmbedderData()";
843 i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, false, location);
844 if (data.is_null()) return NULL;
845 return DecodeSmiToAligned(data->get(index), location);
846}
Steve Block6ded16b2010-05-10 14:33:55 +0100847
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000848
849void Context::SetAlignedPointerInEmbedderData(int index, void* value) {
850 const char* location = "v8::Context::SetAlignedPointerInEmbedderData()";
851 i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, true, location);
852 data->set(index, EncodeAlignedAsSmi(value, location));
853 DCHECK_EQ(value, GetAlignedPointerFromEmbedderData(index));
Steve Blocka7e24c12009-10-30 11:49:00 +0000854}
855
856
857// --- N e a n d e r ---
858
859
860// A constructor cannot easily return an error value, therefore it is necessary
861// to check for a dead VM with ON_BAILOUT before constructing any Neander
862// objects. To remind you about this there is no HandleScope in the
863// NeanderObject constructor. When you add one to the site calling the
864// constructor you should check that you ensured the VM was not dead first.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000865NeanderObject::NeanderObject(v8::internal::Isolate* isolate, int size) {
Steve Block44f0eee2011-05-26 01:26:41 +0100866 ENTER_V8(isolate);
867 value_ = isolate->factory()->NewNeanderObject();
868 i::Handle<i::FixedArray> elements = isolate->factory()->NewFixedArray(size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000869 value_->set_elements(*elements);
870}
871
872
873int NeanderObject::size() {
874 return i::FixedArray::cast(value_->elements())->length();
875}
876
877
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000878NeanderArray::NeanderArray(v8::internal::Isolate* isolate) : obj_(isolate, 2) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000879 obj_.set(0, i::Smi::FromInt(0));
880}
881
882
883int NeanderArray::length() {
884 return i::Smi::cast(obj_.get(0))->value();
885}
886
887
888i::Object* NeanderArray::get(int offset) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000889 DCHECK_LE(0, offset);
890 DCHECK_LT(offset, length());
Steve Blocka7e24c12009-10-30 11:49:00 +0000891 return obj_.get(offset + 1);
892}
893
894
895// This method cannot easily return an error value, therefore it is necessary
896// to check for a dead VM with ON_BAILOUT before calling it. To remind you
897// about this there is no HandleScope in this method. When you add one to the
898// site calling this method you should check that you ensured the VM was not
899// dead first.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400900void NeanderArray::add(i::Isolate* isolate, i::Handle<i::Object> value) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000901 int length = this->length();
902 int size = obj_.size();
903 if (length == size - 1) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400904 i::Factory* factory = isolate->factory();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000905 i::Handle<i::FixedArray> new_elms = factory->NewFixedArray(2 * size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000906 for (int i = 0; i < length; i++)
907 new_elms->set(i + 1, get(i));
908 obj_.value()->set_elements(*new_elms);
909 }
910 obj_.set(length + 1, *value);
911 obj_.set(0, i::Smi::FromInt(length + 1));
912}
913
914
915void NeanderArray::set(int index, i::Object* value) {
916 if (index < 0 || index >= this->length()) return;
917 obj_.set(index + 1, value);
918}
919
920
921// --- T e m p l a t e ---
922
923
924static void InitializeTemplate(i::Handle<i::TemplateInfo> that, int type) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000925 that->set_number_of_properties(0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000926 that->set_tag(i::Smi::FromInt(type));
927}
928
929
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000930void Template::Set(v8::Local<Name> name, v8::Local<Data> value,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000931 v8::PropertyAttribute attribute) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000932 auto templ = Utils::OpenHandle(this);
933 i::Isolate* isolate = templ->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000934 ENTER_V8(isolate);
935 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000936 // TODO(dcarney): split api to allow values of v8::Value or v8::TemplateInfo.
937 i::ApiNatives::AddDataProperty(isolate, templ, Utils::OpenHandle(*name),
938 Utils::OpenHandle(*value),
939 static_cast<i::PropertyAttributes>(attribute));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000940}
941
942
943void Template::SetAccessorProperty(
944 v8::Local<v8::Name> name,
945 v8::Local<FunctionTemplate> getter,
946 v8::Local<FunctionTemplate> setter,
947 v8::PropertyAttribute attribute,
948 v8::AccessControl access_control) {
949 // TODO(verwaest): Remove |access_control|.
950 DCHECK_EQ(v8::DEFAULT, access_control);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000951 auto templ = Utils::OpenHandle(this);
952 auto isolate = templ->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000953 ENTER_V8(isolate);
954 DCHECK(!name.IsEmpty());
955 DCHECK(!getter.IsEmpty() || !setter.IsEmpty());
956 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000957 i::ApiNatives::AddAccessorProperty(
958 isolate, templ, Utils::OpenHandle(*name),
959 Utils::OpenHandle(*getter, true), Utils::OpenHandle(*setter, true),
960 static_cast<i::PropertyAttributes>(attribute));
Steve Blocka7e24c12009-10-30 11:49:00 +0000961}
962
963
964// --- F u n c t i o n T e m p l a t e ---
965static void InitializeFunctionTemplate(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000966 i::Handle<i::FunctionTemplateInfo> info) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000967 InitializeTemplate(info, Consts::FUNCTION_TEMPLATE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000968 info->set_flag(0);
969}
970
971
972Local<ObjectTemplate> FunctionTemplate::PrototypeTemplate() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000973 i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate();
974 ENTER_V8(i_isolate);
975 i::Handle<i::Object> result(Utils::OpenHandle(this)->prototype_template(),
976 i_isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000977 if (result->IsUndefined()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000978 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(i_isolate);
979 result = Utils::OpenHandle(*ObjectTemplate::New(isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +0000980 Utils::OpenHandle(this)->set_prototype_template(*result);
981 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000982 return ToApiHandle<ObjectTemplate>(result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000983}
984
985
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000986static void EnsureNotInstantiated(i::Handle<i::FunctionTemplateInfo> info,
987 const char* func) {
988 Utils::ApiCheck(!info->instantiated(), func,
989 "FunctionTemplate already instantiated");
990}
991
992
993void FunctionTemplate::Inherit(v8::Local<FunctionTemplate> value) {
994 auto info = Utils::OpenHandle(this);
995 EnsureNotInstantiated(info, "v8::FunctionTemplate::Inherit");
996 i::Isolate* isolate = info->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100997 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000998 info->set_parent_template(*Utils::OpenHandle(*value));
Steve Blocka7e24c12009-10-30 11:49:00 +0000999}
1000
1001
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001002static Local<FunctionTemplate> FunctionTemplateNew(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001003 i::Isolate* isolate, FunctionCallback callback,
1004 experimental::FastAccessorBuilder* fast_handler, v8::Local<Value> data,
1005 v8::Local<Signature> signature, int length, bool do_not_cache) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001006 i::Handle<i::Struct> struct_obj =
Steve Block44f0eee2011-05-26 01:26:41 +01001007 isolate->factory()->NewStruct(i::FUNCTION_TEMPLATE_INFO_TYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001008 i::Handle<i::FunctionTemplateInfo> obj =
1009 i::Handle<i::FunctionTemplateInfo>::cast(struct_obj);
1010 InitializeFunctionTemplate(obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001011 obj->set_do_not_cache(do_not_cache);
1012 int next_serial_number = 0;
1013 if (!do_not_cache) {
1014 next_serial_number = isolate->next_serial_number() + 1;
1015 isolate->set_next_serial_number(next_serial_number);
1016 }
Steve Block44f0eee2011-05-26 01:26:41 +01001017 obj->set_serial_number(i::Smi::FromInt(next_serial_number));
Steve Blocka7e24c12009-10-30 11:49:00 +00001018 if (callback != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001019 if (data.IsEmpty()) {
1020 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1021 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001022 Utils::ToLocal(obj)->SetCallHandler(callback, data, fast_handler);
Steve Blocka7e24c12009-10-30 11:49:00 +00001023 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001024 obj->set_length(length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001025 obj->set_undetectable(false);
1026 obj->set_needs_access_check(false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001027 obj->set_accept_any_receiver(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001028 if (!signature.IsEmpty())
1029 obj->set_signature(*Utils::OpenHandle(*signature));
1030 return Utils::ToLocal(obj);
1031}
1032
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001033
1034Local<FunctionTemplate> FunctionTemplate::New(Isolate* isolate,
1035 FunctionCallback callback,
1036 v8::Local<Value> data,
1037 v8::Local<Signature> signature,
1038 int length) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001039 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001040 // Changes to the environment cannot be captured in the snapshot. Expect no
1041 // function templates when the isolate is created for serialization.
1042 DCHECK(!i_isolate->serializer_enabled());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001043 LOG_API(i_isolate, "FunctionTemplate::New");
1044 ENTER_V8(i_isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001045 return FunctionTemplateNew(i_isolate, callback, nullptr, data, signature,
1046 length, false);
1047}
1048
1049
1050Local<FunctionTemplate> FunctionTemplate::NewWithFastHandler(
1051 Isolate* isolate, FunctionCallback callback,
1052 experimental::FastAccessorBuilder* fast_handler, v8::Local<Value> data,
1053 v8::Local<Signature> signature, int length) {
1054 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
1055 DCHECK(!i_isolate->serializer_enabled());
1056 LOG_API(i_isolate, "FunctionTemplate::NewWithFastHandler");
1057 ENTER_V8(i_isolate);
1058 return FunctionTemplateNew(i_isolate, callback, fast_handler, data, signature,
1059 length, false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001060}
Steve Blocka7e24c12009-10-30 11:49:00 +00001061
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001062
1063Local<Signature> Signature::New(Isolate* isolate,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001064 Local<FunctionTemplate> receiver) {
1065 return Utils::SignatureToLocal(Utils::OpenHandle(*receiver));
Steve Blocka7e24c12009-10-30 11:49:00 +00001066}
1067
1068
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001069Local<AccessorSignature> AccessorSignature::New(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001070 Isolate* isolate, Local<FunctionTemplate> receiver) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001071 return Utils::AccessorSignatureToLocal(Utils::OpenHandle(*receiver));
1072}
1073
1074
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001075#define SET_FIELD_WRAPPED(obj, setter, cdata) do { \
1076 i::Handle<i::Object> foreign = FromCData(obj->GetIsolate(), cdata); \
1077 (obj)->setter(*foreign); \
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001078 } while (false)
1079
1080
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001081void FunctionTemplate::SetCallHandler(
1082 FunctionCallback callback, v8::Local<Value> data,
1083 experimental::FastAccessorBuilder* fast_handler) {
1084 auto info = Utils::OpenHandle(this);
1085 EnsureNotInstantiated(info, "v8::FunctionTemplate::SetCallHandler");
1086 i::Isolate* isolate = info->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001087 ENTER_V8(isolate);
1088 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001089 i::Handle<i::Struct> struct_obj =
Steve Block44f0eee2011-05-26 01:26:41 +01001090 isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001091 i::Handle<i::CallHandlerInfo> obj =
1092 i::Handle<i::CallHandlerInfo>::cast(struct_obj);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001093 SET_FIELD_WRAPPED(obj, set_callback, callback);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001094 i::MaybeHandle<i::Code> code =
1095 i::experimental::BuildCodeFromFastAccessorBuilder(fast_handler);
1096 if (!code.is_null()) {
1097 obj->set_fast_handler(*code.ToHandleChecked());
1098 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001099 if (data.IsEmpty()) {
1100 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1101 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001102 obj->set_data(*Utils::OpenHandle(*data));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001103 info->set_call_code(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001104}
1105
1106
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001107static i::Handle<i::AccessorInfo> SetAccessorInfoProperties(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001108 i::Handle<i::AccessorInfo> obj, v8::Local<Name> name,
1109 v8::AccessControl settings, v8::PropertyAttribute attributes,
1110 v8::Local<AccessorSignature> signature) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001111 obj->set_name(*Utils::OpenHandle(*name));
1112 if (settings & ALL_CAN_READ) obj->set_all_can_read(true);
1113 if (settings & ALL_CAN_WRITE) obj->set_all_can_write(true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001114 obj->set_property_attributes(static_cast<i::PropertyAttributes>(attributes));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001115 if (!signature.IsEmpty()) {
1116 obj->set_expected_receiver_type(*Utils::OpenHandle(*signature));
1117 }
Leon Clarkef7060e22010-06-03 12:02:55 +01001118 return obj;
1119}
1120
1121
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001122template <typename Getter, typename Setter>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001123static i::Handle<i::AccessorInfo> MakeAccessorInfo(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001124 v8::Local<Name> name, Getter getter, Setter setter, v8::Local<Value> data,
1125 v8::AccessControl settings, v8::PropertyAttribute attributes,
1126 v8::Local<AccessorSignature> signature) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001127 i::Isolate* isolate = Utils::OpenHandle(*name)->GetIsolate();
1128 i::Handle<i::ExecutableAccessorInfo> obj =
1129 isolate->factory()->NewExecutableAccessorInfo();
1130 SET_FIELD_WRAPPED(obj, set_getter, getter);
1131 SET_FIELD_WRAPPED(obj, set_setter, setter);
1132 if (data.IsEmpty()) {
1133 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +00001134 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001135 obj->set_data(*Utils::OpenHandle(*data));
1136 return SetAccessorInfoProperties(obj, name, settings, attributes, signature);
1137}
Steve Blocka7e24c12009-10-30 11:49:00 +00001138
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001139
Steve Blocka7e24c12009-10-30 11:49:00 +00001140Local<ObjectTemplate> FunctionTemplate::InstanceTemplate() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001141 i::Handle<i::FunctionTemplateInfo> handle = Utils::OpenHandle(this, true);
1142 if (!Utils::ApiCheck(!handle.is_null(),
1143 "v8::FunctionTemplate::InstanceTemplate()",
1144 "Reading from empty handle")) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001145 return Local<ObjectTemplate>();
Steve Blocka7e24c12009-10-30 11:49:00 +00001146 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001147 i::Isolate* isolate = handle->GetIsolate();
1148 ENTER_V8(isolate);
1149 if (handle->instance_template()->IsUndefined()) {
1150 Local<ObjectTemplate> templ =
1151 ObjectTemplate::New(isolate, ToApiHandle<FunctionTemplate>(handle));
1152 handle->set_instance_template(*Utils::OpenHandle(*templ));
1153 }
1154 i::Handle<i::ObjectTemplateInfo> result(
1155 i::ObjectTemplateInfo::cast(handle->instance_template()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001156 return Utils::ToLocal(result);
1157}
1158
1159
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001160void FunctionTemplate::SetLength(int length) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001161 auto info = Utils::OpenHandle(this);
1162 EnsureNotInstantiated(info, "v8::FunctionTemplate::SetLength");
1163 auto isolate = info->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001164 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001165 info->set_length(length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001166}
1167
1168
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001169void FunctionTemplate::SetClassName(Local<String> name) {
1170 auto info = Utils::OpenHandle(this);
1171 EnsureNotInstantiated(info, "v8::FunctionTemplate::SetClassName");
1172 auto isolate = info->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001173 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001174 info->set_class_name(*Utils::OpenHandle(*name));
1175}
1176
1177
1178void FunctionTemplate::SetAcceptAnyReceiver(bool value) {
1179 auto info = Utils::OpenHandle(this);
1180 EnsureNotInstantiated(info, "v8::FunctionTemplate::SetAcceptAnyReceiver");
1181 auto isolate = info->GetIsolate();
1182 ENTER_V8(isolate);
1183 info->set_accept_any_receiver(value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001184}
1185
1186
1187void FunctionTemplate::SetHiddenPrototype(bool value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001188 auto info = Utils::OpenHandle(this);
1189 EnsureNotInstantiated(info, "v8::FunctionTemplate::SetHiddenPrototype");
1190 auto isolate = info->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001191 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001192 info->set_hidden_prototype(value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001193}
1194
1195
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001196void FunctionTemplate::ReadOnlyPrototype() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001197 auto info = Utils::OpenHandle(this);
1198 EnsureNotInstantiated(info, "v8::FunctionTemplate::ReadOnlyPrototype");
1199 auto isolate = info->GetIsolate();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001200 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001201 info->set_read_only_prototype(true);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001202}
1203
1204
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001205void FunctionTemplate::RemovePrototype() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001206 auto info = Utils::OpenHandle(this);
1207 EnsureNotInstantiated(info, "v8::FunctionTemplate::RemovePrototype");
1208 auto isolate = info->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001209 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001210 info->set_remove_prototype(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001211}
1212
1213
1214// --- O b j e c t T e m p l a t e ---
1215
1216
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001217Local<ObjectTemplate> ObjectTemplate::New(
1218 Isolate* isolate, v8::Local<FunctionTemplate> constructor) {
1219 return New(reinterpret_cast<i::Isolate*>(isolate), constructor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001220}
1221
1222
Steve Blocka7e24c12009-10-30 11:49:00 +00001223Local<ObjectTemplate> ObjectTemplate::New() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001224 return New(i::Isolate::Current(), Local<FunctionTemplate>());
Steve Blocka7e24c12009-10-30 11:49:00 +00001225}
1226
1227
1228Local<ObjectTemplate> ObjectTemplate::New(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001229 i::Isolate* isolate, v8::Local<FunctionTemplate> constructor) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001230 // Changes to the environment cannot be captured in the snapshot. Expect no
1231 // object templates when the isolate is created for serialization.
1232 DCHECK(!isolate->serializer_enabled());
Steve Block44f0eee2011-05-26 01:26:41 +01001233 LOG_API(isolate, "ObjectTemplate::New");
1234 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001235 i::Handle<i::Struct> struct_obj =
Steve Block44f0eee2011-05-26 01:26:41 +01001236 isolate->factory()->NewStruct(i::OBJECT_TEMPLATE_INFO_TYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001237 i::Handle<i::ObjectTemplateInfo> obj =
1238 i::Handle<i::ObjectTemplateInfo>::cast(struct_obj);
1239 InitializeTemplate(obj, Consts::OBJECT_TEMPLATE);
1240 if (!constructor.IsEmpty())
1241 obj->set_constructor(*Utils::OpenHandle(*constructor));
1242 obj->set_internal_field_count(i::Smi::FromInt(0));
1243 return Utils::ToLocal(obj);
1244}
1245
1246
1247// Ensure that the object template has a constructor. If no
1248// constructor is available we create one.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001249static i::Handle<i::FunctionTemplateInfo> EnsureConstructor(
1250 i::Isolate* isolate,
1251 ObjectTemplate* object_template) {
1252 i::Object* obj = Utils::OpenHandle(object_template)->constructor();
1253 if (!obj ->IsUndefined()) {
1254 i::FunctionTemplateInfo* info = i::FunctionTemplateInfo::cast(obj);
1255 return i::Handle<i::FunctionTemplateInfo>(info, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001256 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001257 Local<FunctionTemplate> templ =
1258 FunctionTemplate::New(reinterpret_cast<Isolate*>(isolate));
1259 i::Handle<i::FunctionTemplateInfo> constructor = Utils::OpenHandle(*templ);
1260 constructor->set_instance_template(*Utils::OpenHandle(object_template));
1261 Utils::OpenHandle(object_template)->set_constructor(*constructor);
1262 return constructor;
1263}
1264
1265
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001266static inline i::Handle<i::TemplateInfo> GetTemplateInfo(
1267 i::Isolate* isolate,
1268 Template* template_obj) {
1269 return Utils::OpenHandle(template_obj);
1270}
1271
1272
1273// TODO(dcarney): remove this with ObjectTemplate::SetAccessor
1274static inline i::Handle<i::TemplateInfo> GetTemplateInfo(
1275 i::Isolate* isolate,
1276 ObjectTemplate* object_template) {
1277 EnsureConstructor(isolate, object_template);
1278 return Utils::OpenHandle(object_template);
1279}
1280
1281
1282template<typename Getter, typename Setter, typename Data, typename Template>
1283static bool TemplateSetAccessor(
1284 Template* template_obj,
1285 v8::Local<Name> name,
1286 Getter getter,
1287 Setter setter,
1288 Data data,
1289 AccessControl settings,
1290 PropertyAttribute attribute,
1291 v8::Local<AccessorSignature> signature) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001292 auto isolate = Utils::OpenHandle(template_obj)->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001293 ENTER_V8(isolate);
1294 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001295 auto obj = MakeAccessorInfo(name, getter, setter, data, settings, attribute,
1296 signature);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001297 if (obj.is_null()) return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001298 auto info = GetTemplateInfo(isolate, template_obj);
1299 i::ApiNatives::AddNativeDataProperty(isolate, info, obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001300 return true;
1301}
1302
1303
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001304void Template::SetNativeDataProperty(v8::Local<String> name,
1305 AccessorGetterCallback getter,
1306 AccessorSetterCallback setter,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001307 v8::Local<Value> data,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001308 PropertyAttribute attribute,
1309 v8::Local<AccessorSignature> signature,
1310 AccessControl settings) {
1311 TemplateSetAccessor(
1312 this, name, getter, setter, data, settings, attribute, signature);
1313}
1314
1315
1316void Template::SetNativeDataProperty(v8::Local<Name> name,
1317 AccessorNameGetterCallback getter,
1318 AccessorNameSetterCallback setter,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001319 v8::Local<Value> data,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001320 PropertyAttribute attribute,
1321 v8::Local<AccessorSignature> signature,
1322 AccessControl settings) {
1323 TemplateSetAccessor(
1324 this, name, getter, setter, data, settings, attribute, signature);
Steve Blocka7e24c12009-10-30 11:49:00 +00001325}
1326
1327
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001328void Template::SetIntrinsicDataProperty(Local<Name> name, Intrinsic intrinsic,
1329 PropertyAttribute attribute) {
1330 auto templ = Utils::OpenHandle(this);
1331 i::Isolate* isolate = templ->GetIsolate();
1332 ENTER_V8(isolate);
1333 i::HandleScope scope(isolate);
1334 i::ApiNatives::AddDataProperty(isolate, templ, Utils::OpenHandle(*name),
1335 intrinsic,
1336 static_cast<i::PropertyAttributes>(attribute));
1337}
1338
1339
1340void ObjectTemplate::SetAccessor(v8::Local<String> name,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001341 AccessorGetterCallback getter,
1342 AccessorSetterCallback setter,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001343 v8::Local<Value> data, AccessControl settings,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001344 PropertyAttribute attribute,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001345 v8::Local<AccessorSignature> signature) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001346 TemplateSetAccessor(
1347 this, name, getter, setter, data, settings, attribute, signature);
Steve Blocka7e24c12009-10-30 11:49:00 +00001348}
1349
1350
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001351void ObjectTemplate::SetAccessor(v8::Local<Name> name,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001352 AccessorNameGetterCallback getter,
1353 AccessorNameSetterCallback setter,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001354 v8::Local<Value> data, AccessControl settings,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001355 PropertyAttribute attribute,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001356 v8::Local<AccessorSignature> signature) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001357 TemplateSetAccessor(
1358 this, name, getter, setter, data, settings, attribute, signature);
1359}
1360
1361
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001362template <typename Getter, typename Setter, typename Query, typename Deleter,
1363 typename Enumerator>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001364static void ObjectTemplateSetNamedPropertyHandler(ObjectTemplate* templ,
1365 Getter getter, Setter setter,
1366 Query query, Deleter remover,
1367 Enumerator enumerator,
1368 Local<Value> data,
1369 PropertyHandlerFlags flags) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001370 i::Isolate* isolate = Utils::OpenHandle(templ)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001371 ENTER_V8(isolate);
1372 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001373 auto cons = EnsureConstructor(isolate, templ);
1374 EnsureNotInstantiated(cons, "ObjectTemplateSetNamedPropertyHandler");
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001375 auto obj = i::Handle<i::InterceptorInfo>::cast(
1376 isolate->factory()->NewStruct(i::INTERCEPTOR_INFO_TYPE));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001377 obj->set_flags(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001378
1379 if (getter != 0) SET_FIELD_WRAPPED(obj, set_getter, getter);
1380 if (setter != 0) SET_FIELD_WRAPPED(obj, set_setter, setter);
1381 if (query != 0) SET_FIELD_WRAPPED(obj, set_query, query);
1382 if (remover != 0) SET_FIELD_WRAPPED(obj, set_deleter, remover);
1383 if (enumerator != 0) SET_FIELD_WRAPPED(obj, set_enumerator, enumerator);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001384 obj->set_can_intercept_symbols(
1385 !(static_cast<int>(flags) &
1386 static_cast<int>(PropertyHandlerFlags::kOnlyInterceptStrings)));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001387 obj->set_all_can_read(static_cast<int>(flags) &
1388 static_cast<int>(PropertyHandlerFlags::kAllCanRead));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001389 obj->set_non_masking(static_cast<int>(flags) &
1390 static_cast<int>(PropertyHandlerFlags::kNonMasking));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001391
1392 if (data.IsEmpty()) {
1393 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1394 }
1395 obj->set_data(*Utils::OpenHandle(*data));
1396 cons->set_named_property_handler(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001397}
1398
1399
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001400void ObjectTemplate::SetNamedPropertyHandler(
1401 NamedPropertyGetterCallback getter, NamedPropertySetterCallback setter,
1402 NamedPropertyQueryCallback query, NamedPropertyDeleterCallback remover,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001403 NamedPropertyEnumeratorCallback enumerator, Local<Value> data) {
1404 ObjectTemplateSetNamedPropertyHandler(
1405 this, getter, setter, query, remover, enumerator, data,
1406 PropertyHandlerFlags::kOnlyInterceptStrings);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001407}
1408
1409
1410void ObjectTemplate::SetHandler(
1411 const NamedPropertyHandlerConfiguration& config) {
1412 ObjectTemplateSetNamedPropertyHandler(
1413 this, config.getter, config.setter, config.query, config.deleter,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001414 config.enumerator, config.data, config.flags);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001415}
1416
1417
Steve Blocka7e24c12009-10-30 11:49:00 +00001418void ObjectTemplate::MarkAsUndetectable() {
Steve Block44f0eee2011-05-26 01:26:41 +01001419 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001420 ENTER_V8(isolate);
1421 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001422 auto cons = EnsureConstructor(isolate, this);
1423 EnsureNotInstantiated(cons, "v8::ObjectTemplate::MarkAsUndetectable");
Steve Blocka7e24c12009-10-30 11:49:00 +00001424 cons->set_undetectable(true);
1425}
1426
1427
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001428void ObjectTemplate::SetAccessCheckCallback(AccessCheckCallback callback,
1429 Local<Value> data) {
Steve Block44f0eee2011-05-26 01:26:41 +01001430 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001431 ENTER_V8(isolate);
1432 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001433 auto cons = EnsureConstructor(isolate, this);
1434 EnsureNotInstantiated(cons, "v8::ObjectTemplate::SetAccessCheckCallback");
Steve Blocka7e24c12009-10-30 11:49:00 +00001435
1436 i::Handle<i::Struct> struct_info =
Steve Block44f0eee2011-05-26 01:26:41 +01001437 isolate->factory()->NewStruct(i::ACCESS_CHECK_INFO_TYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001438 i::Handle<i::AccessCheckInfo> info =
1439 i::Handle<i::AccessCheckInfo>::cast(struct_info);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001440
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001441 SET_FIELD_WRAPPED(info, set_callback, callback);
1442 SET_FIELD_WRAPPED(info, set_named_callback, nullptr);
1443 SET_FIELD_WRAPPED(info, set_indexed_callback, nullptr);
1444
1445 if (data.IsEmpty()) {
1446 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1447 }
1448 info->set_data(*Utils::OpenHandle(*data));
1449
1450 cons->set_access_check_info(*info);
1451 cons->set_needs_access_check(true);
1452}
1453
1454
1455void ObjectTemplate::SetAccessCheckCallbacks(
1456 NamedSecurityCallback named_callback,
1457 IndexedSecurityCallback indexed_callback, Local<Value> data) {
1458 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1459 ENTER_V8(isolate);
1460 i::HandleScope scope(isolate);
1461 auto cons = EnsureConstructor(isolate, this);
1462 EnsureNotInstantiated(cons, "v8::ObjectTemplate::SetAccessCheckCallbacks");
1463
1464 i::Handle<i::Struct> struct_info =
1465 isolate->factory()->NewStruct(i::ACCESS_CHECK_INFO_TYPE);
1466 i::Handle<i::AccessCheckInfo> info =
1467 i::Handle<i::AccessCheckInfo>::cast(struct_info);
1468
1469 SET_FIELD_WRAPPED(info, set_callback, nullptr);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001470 SET_FIELD_WRAPPED(info, set_named_callback, named_callback);
1471 SET_FIELD_WRAPPED(info, set_indexed_callback, indexed_callback);
1472
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001473 if (data.IsEmpty()) {
1474 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1475 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001476 info->set_data(*Utils::OpenHandle(*data));
1477
Steve Blocka7e24c12009-10-30 11:49:00 +00001478 cons->set_access_check_info(*info);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001479 cons->set_needs_access_check(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001480}
1481
1482
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001483void ObjectTemplate::SetHandler(
1484 const IndexedPropertyHandlerConfiguration& config) {
Steve Block44f0eee2011-05-26 01:26:41 +01001485 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001486 ENTER_V8(isolate);
1487 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001488 auto cons = EnsureConstructor(isolate, this);
1489 EnsureNotInstantiated(cons, "v8::ObjectTemplate::SetHandler");
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001490 auto obj = i::Handle<i::InterceptorInfo>::cast(
1491 isolate->factory()->NewStruct(i::INTERCEPTOR_INFO_TYPE));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001492 obj->set_flags(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001493
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001494 if (config.getter != 0) SET_FIELD_WRAPPED(obj, set_getter, config.getter);
1495 if (config.setter != 0) SET_FIELD_WRAPPED(obj, set_setter, config.setter);
1496 if (config.query != 0) SET_FIELD_WRAPPED(obj, set_query, config.query);
1497 if (config.deleter != 0) SET_FIELD_WRAPPED(obj, set_deleter, config.deleter);
1498 if (config.enumerator != 0) {
1499 SET_FIELD_WRAPPED(obj, set_enumerator, config.enumerator);
1500 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001501 obj->set_all_can_read(static_cast<int>(config.flags) &
1502 static_cast<int>(PropertyHandlerFlags::kAllCanRead));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001503
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001504 v8::Local<v8::Value> data = config.data;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001505 if (data.IsEmpty()) {
1506 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1507 }
1508 obj->set_data(*Utils::OpenHandle(*data));
1509 cons->set_indexed_property_handler(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001510}
1511
1512
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001513void ObjectTemplate::SetCallAsFunctionHandler(FunctionCallback callback,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001514 Local<Value> data) {
Steve Block44f0eee2011-05-26 01:26:41 +01001515 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001516 ENTER_V8(isolate);
1517 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001518 auto cons = EnsureConstructor(isolate, this);
1519 EnsureNotInstantiated(cons, "v8::ObjectTemplate::SetCallAsFunctionHandler");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001520 i::Handle<i::Struct> struct_obj =
1521 isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE);
1522 i::Handle<i::CallHandlerInfo> obj =
1523 i::Handle<i::CallHandlerInfo>::cast(struct_obj);
1524 SET_FIELD_WRAPPED(obj, set_callback, callback);
1525 if (data.IsEmpty()) {
1526 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1527 }
1528 obj->set_data(*Utils::OpenHandle(*data));
1529 cons->set_instance_call_handler(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001530}
1531
1532
1533int ObjectTemplate::InternalFieldCount() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001534 return i::Smi::cast(Utils::OpenHandle(this)->internal_field_count())->value();
1535}
1536
1537
1538void ObjectTemplate::SetInternalFieldCount(int value) {
Steve Block44f0eee2011-05-26 01:26:41 +01001539 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001540 if (!Utils::ApiCheck(i::Smi::IsValid(value),
1541 "v8::ObjectTemplate::SetInternalFieldCount()",
1542 "Invalid internal field count")) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001543 return;
1544 }
Steve Block44f0eee2011-05-26 01:26:41 +01001545 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001546 if (value > 0) {
1547 // The internal field count is set by the constructor function's
1548 // construct code, so we ensure that there is a constructor
1549 // function to do the setting.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001550 EnsureConstructor(isolate, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001551 }
1552 Utils::OpenHandle(this)->set_internal_field_count(i::Smi::FromInt(value));
1553}
1554
1555
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001556// --- S c r i p t s ---
Steve Blocka7e24c12009-10-30 11:49:00 +00001557
1558
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001559// Internally, UnboundScript is a SharedFunctionInfo, and Script is a
1560// JSFunction.
1561
1562ScriptCompiler::CachedData::CachedData(const uint8_t* data_, int length_,
1563 BufferPolicy buffer_policy_)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001564 : data(data_),
1565 length(length_),
1566 rejected(false),
1567 buffer_policy(buffer_policy_) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001568
1569
1570ScriptCompiler::CachedData::~CachedData() {
1571 if (buffer_policy == BufferOwned) {
1572 delete[] data;
1573 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001574}
1575
1576
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001577bool ScriptCompiler::ExternalSourceStream::SetBookmark() { return false; }
1578
1579
1580void ScriptCompiler::ExternalSourceStream::ResetToBookmark() { UNREACHABLE(); }
1581
1582
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001583ScriptCompiler::StreamedSource::StreamedSource(ExternalSourceStream* stream,
1584 Encoding encoding)
1585 : impl_(new i::StreamedSource(stream, encoding)) {}
1586
1587
1588ScriptCompiler::StreamedSource::~StreamedSource() { delete impl_; }
1589
1590
1591const ScriptCompiler::CachedData*
1592ScriptCompiler::StreamedSource::GetCachedData() const {
1593 return impl_->cached_data.get();
1594}
1595
1596
1597Local<Script> UnboundScript::BindToCurrentContext() {
1598 i::Handle<i::HeapObject> obj =
1599 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this));
1600 i::Handle<i::SharedFunctionInfo>
1601 function_info(i::SharedFunctionInfo::cast(*obj), obj->GetIsolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001602 i::Isolate* isolate = obj->GetIsolate();
1603
1604 i::ScopeInfo* scope_info = function_info->scope_info();
1605 i::Handle<i::JSReceiver> global(isolate->native_context()->global_object());
1606 for (int i = 0; i < scope_info->StrongModeFreeVariableCount(); ++i) {
1607 i::Handle<i::String> name_string(scope_info->StrongModeFreeVariableName(i));
1608 i::ScriptContextTable::LookupResult result;
1609 i::Handle<i::ScriptContextTable> script_context_table(
1610 isolate->native_context()->script_context_table());
1611 if (!i::ScriptContextTable::Lookup(script_context_table, name_string,
1612 &result)) {
1613 i::Handle<i::Name> name(scope_info->StrongModeFreeVariableName(i));
1614 Maybe<bool> has = i::JSReceiver::HasProperty(global, name);
1615 if (has.IsJust() && !has.FromJust()) {
1616 i::PendingCompilationErrorHandler pending_error_handler_;
1617 pending_error_handler_.ReportMessageAt(
1618 scope_info->StrongModeFreeVariableStartPosition(i),
1619 scope_info->StrongModeFreeVariableEndPosition(i),
1620 i::MessageTemplate::kStrongUnboundGlobal, name_string,
1621 i::kReferenceError);
1622 i::Handle<i::Script> script(i::Script::cast(function_info->script()));
1623 pending_error_handler_.ThrowPendingError(isolate, script);
1624 isolate->ReportPendingMessages();
1625 isolate->OptionalRescheduleException(true);
1626 return Local<Script>();
1627 }
1628 }
1629 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001630 i::Handle<i::JSFunction> function =
1631 obj->GetIsolate()->factory()->NewFunctionFromSharedFunctionInfo(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001632 function_info, isolate->native_context());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001633 return ToApiHandle<Script>(function);
1634}
1635
1636
1637int UnboundScript::GetId() {
1638 i::Handle<i::HeapObject> obj =
1639 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this));
1640 i::Isolate* isolate = obj->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001641 LOG_API(isolate, "v8::UnboundScript::GetId");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001642 i::HandleScope scope(isolate);
1643 i::Handle<i::SharedFunctionInfo> function_info(
1644 i::SharedFunctionInfo::cast(*obj));
1645 i::Handle<i::Script> script(i::Script::cast(function_info->script()));
1646 return script->id();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001647}
1648
1649
1650int UnboundScript::GetLineNumber(int code_pos) {
1651 i::Handle<i::SharedFunctionInfo> obj =
1652 i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
1653 i::Isolate* isolate = obj->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001654 LOG_API(isolate, "UnboundScript::GetLineNumber");
1655 if (obj->script()->IsScript()) {
1656 i::Handle<i::Script> script(i::Script::cast(obj->script()));
1657 return i::Script::GetLineNumber(script, code_pos);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001658 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001659 return -1;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001660 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001661}
1662
1663
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001664Local<Value> UnboundScript::GetScriptName() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001665 i::Handle<i::SharedFunctionInfo> obj =
1666 i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
1667 i::Isolate* isolate = obj->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001668 LOG_API(isolate, "UnboundScript::GetName");
1669 if (obj->script()->IsScript()) {
1670 i::Object* name = i::Script::cast(obj->script())->name();
1671 return Utils::ToLocal(i::Handle<i::Object>(name, isolate));
1672 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001673 return Local<String>();
Leon Clarkef7060e22010-06-03 12:02:55 +01001674 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001675}
Leon Clarkef7060e22010-06-03 12:02:55 +01001676
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001677
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001678Local<Value> UnboundScript::GetSourceURL() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001679 i::Handle<i::SharedFunctionInfo> obj =
1680 i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
1681 i::Isolate* isolate = obj->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001682 LOG_API(isolate, "UnboundScript::GetSourceURL");
1683 if (obj->script()->IsScript()) {
1684 i::Object* url = i::Script::cast(obj->script())->source_url();
1685 return Utils::ToLocal(i::Handle<i::Object>(url, isolate));
1686 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001687 return Local<String>();
Iain Merrick9ac36c92010-09-13 15:29:50 +01001688 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001689}
1690
1691
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001692Local<Value> UnboundScript::GetSourceMappingURL() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001693 i::Handle<i::SharedFunctionInfo> obj =
1694 i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
1695 i::Isolate* isolate = obj->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001696 LOG_API(isolate, "UnboundScript::GetSourceMappingURL");
1697 if (obj->script()->IsScript()) {
1698 i::Object* url = i::Script::cast(obj->script())->source_mapping_url();
1699 return Utils::ToLocal(i::Handle<i::Object>(url, isolate));
1700 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001701 return Local<String>();
Ben Murdoch692be652012-01-10 18:47:50 +00001702 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001703}
1704
1705
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001706MaybeLocal<Value> Script::Run(Local<Context> context) {
1707 PREPARE_FOR_EXECUTION_WITH_CALLBACK(context, "v8::Script::Run()", Value)
1708 i::AggregatingHistogramTimerScope timer(isolate->counters()->compile_lazy());
1709 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
1710 auto fun = i::Handle<i::JSFunction>::cast(Utils::OpenHandle(this));
1711 i::Handle<i::Object> receiver(isolate->global_proxy(), isolate);
1712 Local<Value> result;
1713 has_pending_exception =
1714 !ToLocal<Value>(i::Execution::Call(isolate, fun, receiver, 0, NULL),
1715 &result);
1716 RETURN_ON_FAILED_EXECUTION(Value);
1717 RETURN_ESCAPED(result);
1718}
1719
1720
Steve Blocka7e24c12009-10-30 11:49:00 +00001721Local<Value> Script::Run() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001722 auto self = Utils::OpenHandle(this, true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001723 // If execution is terminating, Compile(..)->Run() requires this
1724 // check.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001725 if (self.is_null()) return Local<Value>();
1726 auto context = ContextFromHeapObject(self);
1727 RETURN_TO_LOCAL_UNCHECKED(Run(context), Value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001728}
1729
1730
1731Local<UnboundScript> Script::GetUnboundScript() {
1732 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1733 return ToApiHandle<UnboundScript>(
1734 i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared()));
1735}
1736
1737
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001738MaybeLocal<UnboundScript> ScriptCompiler::CompileUnboundInternal(
1739 Isolate* v8_isolate, Source* source, CompileOptions options,
1740 bool is_module) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001741 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001742 PREPARE_FOR_EXECUTION_WITH_ISOLATE(
1743 isolate, "v8::ScriptCompiler::CompileUnbound()", UnboundScript);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001744
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001745 // Don't try to produce any kind of cache when the debugger is loaded.
1746 if (isolate->debug()->is_loaded() &&
1747 (options == kProduceParserCache || options == kProduceCodeCache)) {
1748 options = kNoCompileOptions;
1749 }
1750
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001751 i::ScriptData* script_data = NULL;
1752 if (options == kConsumeParserCache || options == kConsumeCodeCache) {
1753 DCHECK(source->cached_data);
1754 // ScriptData takes care of pointer-aligning the data.
1755 script_data = new i::ScriptData(source->cached_data->data,
1756 source->cached_data->length);
1757 }
1758
1759 i::Handle<i::String> str = Utils::OpenHandle(*(source->source_string));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001760 i::Handle<i::SharedFunctionInfo> result;
1761 {
1762 i::HistogramTimerScope total(isolate->counters()->compile_script(), true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001763 i::Handle<i::Object> name_obj;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001764 i::Handle<i::Object> source_map_url;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001765 int line_offset = 0;
1766 int column_offset = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001767 if (!source->resource_name.IsEmpty()) {
1768 name_obj = Utils::OpenHandle(*(source->resource_name));
1769 }
1770 if (!source->resource_line_offset.IsEmpty()) {
1771 line_offset = static_cast<int>(source->resource_line_offset->Value());
1772 }
1773 if (!source->resource_column_offset.IsEmpty()) {
1774 column_offset =
1775 static_cast<int>(source->resource_column_offset->Value());
1776 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001777 if (!source->source_map_url.IsEmpty()) {
1778 source_map_url = Utils::OpenHandle(*(source->source_map_url));
Steve Blocka7e24c12009-10-30 11:49:00 +00001779 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001780 result = i::Compiler::CompileScript(
1781 str, name_obj, line_offset, column_offset, source->resource_options,
1782 source_map_url, isolate->native_context(), NULL, &script_data, options,
1783 i::NOT_NATIVES_CODE, is_module);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001784 has_pending_exception = result.is_null();
1785 if (has_pending_exception && script_data != NULL) {
1786 // This case won't happen during normal operation; we have compiled
1787 // successfully and produced cached data, and but the second compilation
1788 // of the same source code fails.
1789 delete script_data;
1790 script_data = NULL;
1791 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001792 RETURN_ON_FAILED_EXECUTION(UnboundScript);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001793
1794 if ((options == kProduceParserCache || options == kProduceCodeCache) &&
1795 script_data != NULL) {
1796 // script_data now contains the data that was generated. source will
1797 // take the ownership.
1798 source->cached_data = new CachedData(
1799 script_data->data(), script_data->length(), CachedData::BufferOwned);
1800 script_data->ReleaseDataOwnership();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001801 } else if (options == kConsumeParserCache || options == kConsumeCodeCache) {
1802 source->cached_data->rejected = script_data->rejected();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001803 }
1804 delete script_data;
Steve Blocka7e24c12009-10-30 11:49:00 +00001805 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001806 RETURN_ESCAPED(ToApiHandle<UnboundScript>(result));
1807}
1808
1809
1810MaybeLocal<UnboundScript> ScriptCompiler::CompileUnboundScript(
1811 Isolate* v8_isolate, Source* source, CompileOptions options) {
1812 return CompileUnboundInternal(v8_isolate, source, options, false);
1813}
1814
1815
1816Local<UnboundScript> ScriptCompiler::CompileUnbound(Isolate* v8_isolate,
1817 Source* source,
1818 CompileOptions options) {
1819 RETURN_TO_LOCAL_UNCHECKED(
1820 CompileUnboundInternal(v8_isolate, source, options, false),
1821 UnboundScript);
1822}
1823
1824
1825MaybeLocal<Script> ScriptCompiler::Compile(Local<Context> context,
1826 Source* source,
1827 CompileOptions options) {
1828 auto isolate = context->GetIsolate();
1829 auto maybe = CompileUnboundInternal(isolate, source, options, false);
1830 Local<UnboundScript> result;
1831 if (!maybe.ToLocal(&result)) return MaybeLocal<Script>();
1832 v8::Context::Scope scope(context);
1833 return result->BindToCurrentContext();
Steve Blocka7e24c12009-10-30 11:49:00 +00001834}
1835
1836
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001837Local<Script> ScriptCompiler::Compile(
1838 Isolate* v8_isolate,
1839 Source* source,
1840 CompileOptions options) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001841 auto context = v8_isolate->GetCurrentContext();
1842 RETURN_TO_LOCAL_UNCHECKED(Compile(context, source, options), Script);
1843}
1844
1845
1846MaybeLocal<Script> ScriptCompiler::CompileModule(Local<Context> context,
1847 Source* source,
1848 CompileOptions options) {
1849 CHECK(i::FLAG_harmony_modules);
1850 auto isolate = context->GetIsolate();
1851 auto maybe = CompileUnboundInternal(isolate, source, options, true);
1852 Local<UnboundScript> generic;
1853 if (!maybe.ToLocal(&generic)) return MaybeLocal<Script>();
1854 v8::Context::Scope scope(context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001855 return generic->BindToCurrentContext();
Steve Block6ded16b2010-05-10 14:33:55 +01001856}
1857
1858
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001859class IsIdentifierHelper {
1860 public:
1861 IsIdentifierHelper() : is_identifier_(false), first_char_(true) {}
1862
1863 bool Check(i::String* string) {
1864 i::ConsString* cons_string = i::String::VisitFlat(this, string, 0);
1865 if (cons_string == NULL) return is_identifier_;
1866 // We don't support cons strings here.
1867 return false;
1868 }
1869 void VisitOneByteString(const uint8_t* chars, int length) {
1870 for (int i = 0; i < length; ++i) {
1871 if (first_char_) {
1872 first_char_ = false;
1873 is_identifier_ = unicode_cache_.IsIdentifierStart(chars[0]);
1874 } else {
1875 is_identifier_ &= unicode_cache_.IsIdentifierPart(chars[i]);
1876 }
1877 }
1878 }
1879 void VisitTwoByteString(const uint16_t* chars, int length) {
1880 for (int i = 0; i < length; ++i) {
1881 if (first_char_) {
1882 first_char_ = false;
1883 is_identifier_ = unicode_cache_.IsIdentifierStart(chars[0]);
1884 } else {
1885 is_identifier_ &= unicode_cache_.IsIdentifierPart(chars[i]);
1886 }
1887 }
1888 }
1889
1890 private:
1891 bool is_identifier_;
1892 bool first_char_;
1893 i::UnicodeCache unicode_cache_;
1894 DISALLOW_COPY_AND_ASSIGN(IsIdentifierHelper);
1895};
1896
1897
1898MaybeLocal<Function> ScriptCompiler::CompileFunctionInContext(
1899 Local<Context> v8_context, Source* source, size_t arguments_count,
1900 Local<String> arguments[], size_t context_extension_count,
1901 Local<Object> context_extensions[]) {
1902 PREPARE_FOR_EXECUTION(
1903 v8_context, "v8::ScriptCompiler::CompileFunctionInContext()", Function);
1904 i::Handle<i::String> source_string;
1905 auto factory = isolate->factory();
1906 if (arguments_count) {
1907 source_string = factory->NewStringFromStaticChars("(function(");
1908 for (size_t i = 0; i < arguments_count; ++i) {
1909 IsIdentifierHelper helper;
1910 if (!helper.Check(*Utils::OpenHandle(*arguments[i]))) {
1911 return Local<Function>();
1912 }
1913 has_pending_exception =
1914 !factory->NewConsString(source_string,
1915 Utils::OpenHandle(*arguments[i]))
1916 .ToHandle(&source_string);
1917 RETURN_ON_FAILED_EXECUTION(Function);
1918 if (i + 1 == arguments_count) continue;
1919 has_pending_exception =
1920 !factory->NewConsString(source_string,
1921 factory->LookupSingleCharacterStringFromCode(
1922 ',')).ToHandle(&source_string);
1923 RETURN_ON_FAILED_EXECUTION(Function);
1924 }
1925 auto brackets = factory->NewStringFromStaticChars("){");
1926 has_pending_exception = !factory->NewConsString(source_string, brackets)
1927 .ToHandle(&source_string);
1928 RETURN_ON_FAILED_EXECUTION(Function);
1929 } else {
1930 source_string = factory->NewStringFromStaticChars("(function(){");
1931 }
1932
1933 int scope_position = source_string->length();
1934 has_pending_exception =
1935 !factory->NewConsString(source_string,
1936 Utils::OpenHandle(*source->source_string))
1937 .ToHandle(&source_string);
1938 RETURN_ON_FAILED_EXECUTION(Function);
1939 // Include \n in case the source contains a line end comment.
1940 auto brackets = factory->NewStringFromStaticChars("\n})");
1941 has_pending_exception =
1942 !factory->NewConsString(source_string, brackets).ToHandle(&source_string);
1943 RETURN_ON_FAILED_EXECUTION(Function);
1944
1945 i::Handle<i::Context> context = Utils::OpenHandle(*v8_context);
1946 i::Handle<i::SharedFunctionInfo> outer_info(context->closure()->shared(),
1947 isolate);
1948 for (size_t i = 0; i < context_extension_count; ++i) {
1949 i::Handle<i::JSReceiver> extension =
1950 Utils::OpenHandle(*context_extensions[i]);
1951 if (!extension->IsJSObject()) return Local<Function>();
1952 i::Handle<i::JSFunction> closure(context->closure(), isolate);
1953 context = factory->NewWithContext(closure, context, extension);
1954 }
1955
1956 i::Handle<i::Object> name_obj;
1957 int line_offset = 0;
1958 int column_offset = 0;
1959 if (!source->resource_name.IsEmpty()) {
1960 name_obj = Utils::OpenHandle(*(source->resource_name));
1961 }
1962 if (!source->resource_line_offset.IsEmpty()) {
1963 line_offset = static_cast<int>(source->resource_line_offset->Value());
1964 }
1965 if (!source->resource_column_offset.IsEmpty()) {
1966 column_offset = static_cast<int>(source->resource_column_offset->Value());
1967 }
1968 i::Handle<i::JSFunction> fun;
1969 has_pending_exception = !i::Compiler::GetFunctionFromEval(
1970 source_string, outer_info, context, i::SLOPPY,
1971 i::ONLY_SINGLE_FUNCTION_LITERAL, line_offset,
1972 column_offset - scope_position, name_obj,
1973 source->resource_options).ToHandle(&fun);
1974 if (has_pending_exception) {
1975 isolate->ReportPendingMessages();
1976 }
1977 RETURN_ON_FAILED_EXECUTION(Function);
1978
1979 i::Handle<i::Object> result;
1980 has_pending_exception =
1981 !i::Execution::Call(isolate, fun,
1982 Utils::OpenHandle(*v8_context->Global()), 0,
1983 nullptr).ToHandle(&result);
1984 RETURN_ON_FAILED_EXECUTION(Function);
1985 RETURN_ESCAPED(
1986 Utils::CallableToLocal(i::Handle<i::JSFunction>::cast(result)));
1987}
1988
1989
1990Local<Function> ScriptCompiler::CompileFunctionInContext(
1991 Isolate* v8_isolate, Source* source, Local<Context> v8_context,
1992 size_t arguments_count, Local<String> arguments[],
1993 size_t context_extension_count, Local<Object> context_extensions[]) {
1994 RETURN_TO_LOCAL_UNCHECKED(
1995 CompileFunctionInContext(v8_context, source, arguments_count, arguments,
1996 context_extension_count, context_extensions),
1997 Function);
1998}
1999
2000
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002001ScriptCompiler::ScriptStreamingTask* ScriptCompiler::StartStreamingScript(
2002 Isolate* v8_isolate, StreamedSource* source, CompileOptions options) {
2003 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002004 return new i::BackgroundParsingTask(source->impl(), options,
2005 i::FLAG_stack_size, isolate);
2006}
2007
2008
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002009MaybeLocal<Script> ScriptCompiler::Compile(Local<Context> context,
2010 StreamedSource* v8_source,
2011 Local<String> full_source_string,
2012 const ScriptOrigin& origin) {
2013 PREPARE_FOR_EXECUTION(context, "v8::ScriptCompiler::Compile()", Script);
2014 i::StreamedSource* source = v8_source->impl();
2015 i::Handle<i::String> str = Utils::OpenHandle(*(full_source_string));
2016 i::Handle<i::Script> script = isolate->factory()->NewScript(str);
2017 if (!origin.ResourceName().IsEmpty()) {
2018 script->set_name(*Utils::OpenHandle(*(origin.ResourceName())));
2019 }
2020 if (!origin.ResourceLineOffset().IsEmpty()) {
2021 script->set_line_offset(
2022 static_cast<int>(origin.ResourceLineOffset()->Value()));
2023 }
2024 if (!origin.ResourceColumnOffset().IsEmpty()) {
2025 script->set_column_offset(
2026 static_cast<int>(origin.ResourceColumnOffset()->Value()));
2027 }
2028 script->set_origin_options(origin.Options());
2029 if (!origin.SourceMapUrl().IsEmpty()) {
2030 script->set_source_mapping_url(
2031 *Utils::OpenHandle(*(origin.SourceMapUrl())));
2032 }
2033
2034 source->info->set_script(script);
2035 source->info->set_context(isolate->native_context());
2036
2037 // Do the parsing tasks which need to be done on the main thread. This will
2038 // also handle parse errors.
2039 source->parser->Internalize(isolate, script,
2040 source->info->literal() == nullptr);
2041 source->parser->HandleSourceURLComments(isolate, script);
2042
2043 i::Handle<i::SharedFunctionInfo> result;
2044 if (source->info->literal() != nullptr) {
2045 // Parsing has succeeded.
2046 result = i::Compiler::CompileStreamedScript(script, source->info.get(),
2047 str->length());
2048 }
2049 has_pending_exception = result.is_null();
2050 if (has_pending_exception) isolate->ReportPendingMessages();
2051 RETURN_ON_FAILED_EXECUTION(Script);
2052
2053 source->info->clear_script(); // because script goes out of scope.
2054
2055 Local<UnboundScript> generic = ToApiHandle<UnboundScript>(result);
2056 if (generic.IsEmpty()) return Local<Script>();
2057 Local<Script> bound = generic->BindToCurrentContext();
2058 if (bound.IsEmpty()) return Local<Script>();
2059 RETURN_ESCAPED(bound);
2060}
2061
2062
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002063Local<Script> ScriptCompiler::Compile(Isolate* v8_isolate,
2064 StreamedSource* v8_source,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002065 Local<String> full_source_string,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002066 const ScriptOrigin& origin) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002067 auto context = v8_isolate->GetCurrentContext();
2068 RETURN_TO_LOCAL_UNCHECKED(
2069 Compile(context, v8_source, full_source_string, origin), Script);
Steve Blocka7e24c12009-10-30 11:49:00 +00002070}
2071
2072
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002073uint32_t ScriptCompiler::CachedDataVersionTag() {
2074 return static_cast<uint32_t>(base::hash_combine(
2075 internal::Version::Hash(), internal::FlagList::Hash(),
2076 static_cast<uint32_t>(internal::CpuFeatures::SupportedFeatures())));
2077}
2078
2079
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002080MaybeLocal<Script> Script::Compile(Local<Context> context, Local<String> source,
2081 ScriptOrigin* origin) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002082 if (origin) {
2083 ScriptCompiler::Source script_source(source, *origin);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002084 return ScriptCompiler::Compile(context, &script_source);
Steve Blocka7e24c12009-10-30 11:49:00 +00002085 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002086 ScriptCompiler::Source script_source(source);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002087 return ScriptCompiler::Compile(context, &script_source);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002088}
2089
2090
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002091Local<Script> Script::Compile(v8::Local<String> source,
2092 v8::ScriptOrigin* origin) {
2093 auto str = Utils::OpenHandle(*source);
2094 auto context = ContextFromHeapObject(str);
2095 RETURN_TO_LOCAL_UNCHECKED(Compile(context, source, origin), Script);
2096}
2097
2098
2099Local<Script> Script::Compile(v8::Local<String> source,
2100 v8::Local<String> file_name) {
2101 auto str = Utils::OpenHandle(*source);
2102 auto context = ContextFromHeapObject(str);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002103 ScriptOrigin origin(file_name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002104 return Compile(context, source, &origin).FromMaybe(Local<Script>());
Steve Blocka7e24c12009-10-30 11:49:00 +00002105}
2106
2107
2108// --- E x c e p t i o n s ---
2109
2110
2111v8::TryCatch::TryCatch()
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002112 : isolate_(i::Isolate::Current()),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002113 next_(isolate_->try_catch_handler()),
Steve Blocka7e24c12009-10-30 11:49:00 +00002114 is_verbose_(false),
2115 can_continue_(true),
2116 capture_message_(true),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002117 rethrow_(false),
2118 has_terminated_(false) {
2119 ResetInternal();
2120 // Special handling for simulators which have a separate JS stack.
2121 js_stack_comparable_address_ =
2122 reinterpret_cast<void*>(v8::internal::SimulatorStack::RegisterCTryCatch(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002123 isolate_, v8::internal::GetCurrentStackPosition()));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002124 isolate_->RegisterTryCatchHandler(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00002125}
2126
2127
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002128v8::TryCatch::TryCatch(v8::Isolate* isolate)
2129 : isolate_(reinterpret_cast<i::Isolate*>(isolate)),
2130 next_(isolate_->try_catch_handler()),
2131 is_verbose_(false),
2132 can_continue_(true),
2133 capture_message_(true),
2134 rethrow_(false),
2135 has_terminated_(false) {
2136 ResetInternal();
2137 // Special handling for simulators which have a separate JS stack.
2138 js_stack_comparable_address_ =
2139 reinterpret_cast<void*>(v8::internal::SimulatorStack::RegisterCTryCatch(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002140 isolate_, v8::internal::GetCurrentStackPosition()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002141 isolate_->RegisterTryCatchHandler(this);
2142}
2143
2144
Steve Blocka7e24c12009-10-30 11:49:00 +00002145v8::TryCatch::~TryCatch() {
Steve Blockd0582a62009-12-15 09:54:21 +00002146 if (rethrow_) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002147 v8::Isolate* isolate = reinterpret_cast<Isolate*>(isolate_);
2148 v8::HandleScope scope(isolate);
2149 v8::Local<v8::Value> exc = v8::Local<v8::Value>::New(isolate, Exception());
2150 if (HasCaught() && capture_message_) {
2151 // If an exception was caught and rethrow_ is indicated, the saved
2152 // message, script, and location need to be restored to Isolate TLS
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002153 // for reuse. capture_message_ needs to be disabled so that Throw()
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002154 // does not create a new message.
2155 isolate_->thread_local_top()->rethrowing_message_ = true;
2156 isolate_->RestorePendingMessageFromTryCatch(this);
2157 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002158 isolate_->UnregisterTryCatchHandler(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002159 v8::internal::SimulatorStack::UnregisterCTryCatch(isolate_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002160 reinterpret_cast<Isolate*>(isolate_)->ThrowException(exc);
2161 DCHECK(!isolate_->thread_local_top()->rethrowing_message_);
Steve Blockd0582a62009-12-15 09:54:21 +00002162 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002163 if (HasCaught() && isolate_->has_scheduled_exception()) {
2164 // If an exception was caught but is still scheduled because no API call
2165 // promoted it, then it is canceled to prevent it from being propagated.
2166 // Note that this will not cancel termination exceptions.
2167 isolate_->CancelScheduledExceptionFromTryCatch(this);
2168 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002169 isolate_->UnregisterTryCatchHandler(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002170 v8::internal::SimulatorStack::UnregisterCTryCatch(isolate_);
Steve Blockd0582a62009-12-15 09:54:21 +00002171 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002172}
2173
2174
2175bool v8::TryCatch::HasCaught() const {
2176 return !reinterpret_cast<i::Object*>(exception_)->IsTheHole();
2177}
2178
2179
2180bool v8::TryCatch::CanContinue() const {
2181 return can_continue_;
2182}
2183
2184
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002185bool v8::TryCatch::HasTerminated() const {
2186 return has_terminated_;
2187}
2188
2189
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002190v8::Local<v8::Value> v8::TryCatch::ReThrow() {
Steve Blockd0582a62009-12-15 09:54:21 +00002191 if (!HasCaught()) return v8::Local<v8::Value>();
2192 rethrow_ = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002193 return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate_));
Steve Blockd0582a62009-12-15 09:54:21 +00002194}
2195
2196
Steve Blocka7e24c12009-10-30 11:49:00 +00002197v8::Local<Value> v8::TryCatch::Exception() const {
2198 if (HasCaught()) {
2199 // Check for out of memory exception.
2200 i::Object* exception = reinterpret_cast<i::Object*>(exception_);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002201 return v8::Utils::ToLocal(i::Handle<i::Object>(exception, isolate_));
Steve Blocka7e24c12009-10-30 11:49:00 +00002202 } else {
2203 return v8::Local<Value>();
2204 }
2205}
2206
2207
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002208MaybeLocal<Value> v8::TryCatch::StackTrace(Local<Context> context) const {
2209 if (!HasCaught()) return v8::Local<Value>();
2210 i::Object* raw_obj = reinterpret_cast<i::Object*>(exception_);
2211 if (!raw_obj->IsJSObject()) return v8::Local<Value>();
2212 PREPARE_FOR_EXECUTION(context, "v8::TryCatch::StackTrace", Value);
2213 i::Handle<i::JSObject> obj(i::JSObject::cast(raw_obj), isolate_);
2214 i::Handle<i::String> name = isolate->factory()->stack_string();
2215 Maybe<bool> maybe = i::JSReceiver::HasProperty(obj, name);
2216 has_pending_exception = !maybe.IsJust();
2217 RETURN_ON_FAILED_EXECUTION(Value);
2218 if (!maybe.FromJust()) return v8::Local<Value>();
2219 Local<Value> result;
2220 has_pending_exception =
2221 !ToLocal<Value>(i::Object::GetProperty(obj, name), &result);
2222 RETURN_ON_FAILED_EXECUTION(Value);
2223 RETURN_ESCAPED(result);
2224}
2225
2226
Steve Blocka7e24c12009-10-30 11:49:00 +00002227v8::Local<Value> v8::TryCatch::StackTrace() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002228 auto context = reinterpret_cast<v8::Isolate*>(isolate_)->GetCurrentContext();
2229 RETURN_TO_LOCAL_UNCHECKED(StackTrace(context), Value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002230}
2231
2232
2233v8::Local<v8::Message> v8::TryCatch::Message() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002234 i::Object* message = reinterpret_cast<i::Object*>(message_obj_);
2235 DCHECK(message->IsJSMessageObject() || message->IsTheHole());
2236 if (HasCaught() && !message->IsTheHole()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002237 return v8::Utils::MessageToLocal(i::Handle<i::Object>(message, isolate_));
Steve Blocka7e24c12009-10-30 11:49:00 +00002238 } else {
2239 return v8::Local<v8::Message>();
2240 }
2241}
2242
2243
2244void v8::TryCatch::Reset() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002245 if (!rethrow_ && HasCaught() && isolate_->has_scheduled_exception()) {
2246 // If an exception was caught but is still scheduled because no API call
2247 // promoted it, then it is canceled to prevent it from being propagated.
2248 // Note that this will not cancel termination exceptions.
2249 isolate_->CancelScheduledExceptionFromTryCatch(this);
2250 }
2251 ResetInternal();
2252}
2253
2254
2255void v8::TryCatch::ResetInternal() {
2256 i::Object* the_hole = isolate_->heap()->the_hole_value();
2257 exception_ = the_hole;
2258 message_obj_ = the_hole;
Steve Blocka7e24c12009-10-30 11:49:00 +00002259}
2260
2261
2262void v8::TryCatch::SetVerbose(bool value) {
2263 is_verbose_ = value;
2264}
2265
2266
2267void v8::TryCatch::SetCaptureMessage(bool value) {
2268 capture_message_ = value;
2269}
2270
2271
2272// --- M e s s a g e ---
2273
2274
2275Local<String> Message::Get() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002276 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01002277 ENTER_V8(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002278 EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +00002279 i::Handle<i::Object> obj = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002280 i::Handle<i::String> raw_result = i::MessageHandler::GetMessage(isolate, obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002281 Local<String> result = Utils::ToLocal(raw_result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002282 return scope.Escape(result);
2283}
2284
2285
2286ScriptOrigin Message::GetScriptOrigin() const {
2287 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002288 auto message = i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
2289 auto script_wraper = i::Handle<i::Object>(message->script(), isolate);
2290 auto script_value = i::Handle<i::JSValue>::cast(script_wraper);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002291 i::Handle<i::Script> script(i::Script::cast(script_value->value()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002292 return GetScriptOriginForScript(isolate, script);
Steve Blocka7e24c12009-10-30 11:49:00 +00002293}
2294
2295
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002296v8::Local<Value> Message::GetScriptResourceName() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002297 return GetScriptOrigin().ResourceName();
Steve Blocka7e24c12009-10-30 11:49:00 +00002298}
2299
2300
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002301v8::Local<v8::StackTrace> Message::GetStackTrace() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002302 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01002303 ENTER_V8(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002304 EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002305 auto message = i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002306 i::Handle<i::Object> stackFramesObj(message->stack_frames(), isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002307 if (!stackFramesObj->IsJSArray()) return v8::Local<v8::StackTrace>();
2308 auto stackTrace = i::Handle<i::JSArray>::cast(stackFramesObj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002309 return scope.Escape(Utils::StackTraceToLocal(stackTrace));
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002310}
2311
2312
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002313Maybe<int> Message::GetLineNumber(Local<Context> context) const {
2314 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Message::GetLineNumber()", int);
2315 i::Handle<i::JSFunction> fun = isolate->message_get_line_number();
2316 i::Handle<i::Object> undefined = isolate->factory()->undefined_value();
2317 i::Handle<i::Object> args[] = {Utils::OpenHandle(this)};
2318 i::Handle<i::Object> result;
2319 has_pending_exception =
2320 !i::Execution::Call(isolate, fun, undefined, arraysize(args), args)
2321 .ToHandle(&result);
2322 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(int);
2323 return Just(static_cast<int>(result->Number()));
Steve Blocka7e24c12009-10-30 11:49:00 +00002324}
2325
2326
2327int Message::GetLineNumber() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002328 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
2329 return GetLineNumber(context).FromMaybe(0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002330}
2331
2332
2333int Message::GetStartPosition() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002334 auto self = Utils::OpenHandle(this);
2335 return self->start_position();
Steve Blocka7e24c12009-10-30 11:49:00 +00002336}
2337
2338
2339int Message::GetEndPosition() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002340 auto self = Utils::OpenHandle(this);
2341 return self->end_position();
2342}
2343
2344
2345Maybe<int> Message::GetStartColumn(Local<Context> context) const {
2346 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Message::GetStartColumn()",
2347 int);
2348 i::Handle<i::JSFunction> fun = isolate->message_get_column_number();
2349 i::Handle<i::Object> undefined = isolate->factory()->undefined_value();
2350 i::Handle<i::Object> args[] = {Utils::OpenHandle(this)};
2351 i::Handle<i::Object> result;
2352 has_pending_exception =
2353 !i::Execution::Call(isolate, fun, undefined, arraysize(args), args)
2354 .ToHandle(&result);
2355 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(int);
2356 return Just(static_cast<int>(result->Number()));
Steve Blocka7e24c12009-10-30 11:49:00 +00002357}
2358
2359
2360int Message::GetStartColumn() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002361 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
2362 const int default_value = kNoColumnInfo;
2363 return GetStartColumn(context).FromMaybe(default_value);
2364}
2365
2366
2367Maybe<int> Message::GetEndColumn(Local<Context> context) const {
2368 auto self = Utils::OpenHandle(this);
2369 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Message::GetEndColumn()", int);
2370 i::Handle<i::JSFunction> fun = isolate->message_get_column_number();
2371 i::Handle<i::Object> undefined = isolate->factory()->undefined_value();
2372 i::Handle<i::Object> args[] = {self};
2373 i::Handle<i::Object> result;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002374 has_pending_exception =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002375 !i::Execution::Call(isolate, fun, undefined, arraysize(args), args)
2376 .ToHandle(&result);
2377 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(int);
2378 int start = self->start_position();
2379 int end = self->end_position();
2380 return Just(static_cast<int>(result->Number()) + (end - start));
Steve Blocka7e24c12009-10-30 11:49:00 +00002381}
2382
2383
2384int Message::GetEndColumn() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002385 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
2386 const int default_value = kNoColumnInfo;
2387 return GetEndColumn(context).FromMaybe(default_value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002388}
2389
2390
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002391bool Message::IsSharedCrossOrigin() const {
2392 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2393 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002394 auto self = Utils::OpenHandle(this);
2395 auto script = i::Handle<i::JSValue>::cast(
2396 i::Handle<i::Object>(self->script(), isolate));
2397 return i::Script::cast(script->value())
2398 ->origin_options()
2399 .IsSharedCrossOrigin();
2400}
2401
2402bool Message::IsOpaque() const {
2403 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2404 ENTER_V8(isolate);
2405 auto self = Utils::OpenHandle(this);
2406 auto script = i::Handle<i::JSValue>::cast(
2407 i::Handle<i::Object>(self->script(), isolate));
2408 return i::Script::cast(script->value())->origin_options().IsOpaque();
2409}
2410
2411
2412MaybeLocal<String> Message::GetSourceLine(Local<Context> context) const {
2413 PREPARE_FOR_EXECUTION(context, "v8::Message::GetSourceLine()", String);
2414 i::Handle<i::JSFunction> fun = isolate->message_get_source_line();
2415 i::Handle<i::Object> undefined = isolate->factory()->undefined_value();
2416 i::Handle<i::Object> args[] = {Utils::OpenHandle(this)};
2417 i::Handle<i::Object> result;
2418 has_pending_exception =
2419 !i::Execution::Call(isolate, fun, undefined, arraysize(args), args)
2420 .ToHandle(&result);
2421 RETURN_ON_FAILED_EXECUTION(String);
2422 Local<String> str;
2423 if (result->IsString()) {
2424 str = Utils::ToLocal(i::Handle<i::String>::cast(result));
2425 }
2426 RETURN_ESCAPED(str);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002427}
2428
2429
Steve Blocka7e24c12009-10-30 11:49:00 +00002430Local<String> Message::GetSourceLine() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002431 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
2432 RETURN_TO_LOCAL_UNCHECKED(GetSourceLine(context), String)
Steve Blocka7e24c12009-10-30 11:49:00 +00002433}
2434
2435
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002436void Message::PrintCurrentStackTrace(Isolate* isolate, FILE* out) {
2437 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2438 ENTER_V8(i_isolate);
2439 i_isolate->PrintCurrentStackTrace(out);
Steve Blocka7e24c12009-10-30 11:49:00 +00002440}
2441
2442
Kristian Monsen25f61362010-05-21 11:50:48 +01002443// --- S t a c k T r a c e ---
2444
2445Local<StackFrame> StackTrace::GetFrame(uint32_t index) const {
Steve Block44f0eee2011-05-26 01:26:41 +01002446 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01002447 ENTER_V8(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002448 EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002449 auto self = Utils::OpenHandle(this);
2450 auto obj = i::Object::GetElement(isolate, self, index).ToHandleChecked();
2451 auto jsobj = i::Handle<i::JSObject>::cast(obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002452 return scope.Escape(Utils::StackFrameToLocal(jsobj));
Kristian Monsen25f61362010-05-21 11:50:48 +01002453}
2454
2455
2456int StackTrace::GetFrameCount() const {
Kristian Monsen25f61362010-05-21 11:50:48 +01002457 return i::Smi::cast(Utils::OpenHandle(this)->length())->value();
2458}
2459
2460
2461Local<Array> StackTrace::AsArray() {
Kristian Monsen25f61362010-05-21 11:50:48 +01002462 return Utils::ToLocal(Utils::OpenHandle(this));
2463}
2464
2465
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002466Local<StackTrace> StackTrace::CurrentStackTrace(
2467 Isolate* isolate,
2468 int frame_limit,
Kristian Monsen25f61362010-05-21 11:50:48 +01002469 StackTraceOptions options) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002470 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2471 ENTER_V8(i_isolate);
2472 // TODO(dcarney): remove when ScriptDebugServer is fixed.
2473 options = static_cast<StackTraceOptions>(
2474 static_cast<int>(options) | kExposeFramesAcrossSecurityOrigins);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002475 i::Handle<i::JSArray> stackTrace =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002476 i_isolate->CaptureCurrentStackTrace(frame_limit, options);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002477 return Utils::StackTraceToLocal(stackTrace);
Kristian Monsen25f61362010-05-21 11:50:48 +01002478}
2479
2480
2481// --- S t a c k F r a m e ---
2482
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002483static int getIntProperty(const StackFrame* f, const char* propertyName,
2484 int defaultValue) {
2485 i::Isolate* isolate = Utils::OpenHandle(f)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01002486 ENTER_V8(isolate);
2487 i::HandleScope scope(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002488 i::Handle<i::JSObject> self = Utils::OpenHandle(f);
2489 i::Handle<i::Object> obj =
2490 i::Object::GetProperty(isolate, self, propertyName).ToHandleChecked();
2491 return obj->IsSmi() ? i::Smi::cast(*obj)->value() : defaultValue;
2492}
2493
2494
2495int StackFrame::GetLineNumber() const {
2496 return getIntProperty(this, "lineNumber", Message::kNoLineNumberInfo);
Kristian Monsen25f61362010-05-21 11:50:48 +01002497}
2498
2499
2500int StackFrame::GetColumn() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002501 return getIntProperty(this, "column", Message::kNoColumnInfo);
2502}
2503
2504
2505int StackFrame::GetScriptId() const {
2506 return getIntProperty(this, "scriptId", Message::kNoScriptIdInfo);
2507}
2508
2509
2510static Local<String> getStringProperty(const StackFrame* f,
2511 const char* propertyName) {
2512 i::Isolate* isolate = Utils::OpenHandle(f)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01002513 ENTER_V8(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002514 EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
2515 i::Handle<i::JSObject> self = Utils::OpenHandle(f);
2516 i::Handle<i::Object> obj =
2517 i::Object::GetProperty(isolate, self, propertyName).ToHandleChecked();
2518 return obj->IsString()
2519 ? scope.Escape(Local<String>::Cast(Utils::ToLocal(obj)))
2520 : Local<String>();
Kristian Monsen25f61362010-05-21 11:50:48 +01002521}
2522
2523
2524Local<String> StackFrame::GetScriptName() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002525 return getStringProperty(this, "scriptName");
Kristian Monsen25f61362010-05-21 11:50:48 +01002526}
2527
2528
Ben Murdochf87a2032010-10-22 12:50:53 +01002529Local<String> StackFrame::GetScriptNameOrSourceURL() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002530 return getStringProperty(this, "scriptNameOrSourceURL");
Ben Murdochf87a2032010-10-22 12:50:53 +01002531}
2532
2533
Kristian Monsen25f61362010-05-21 11:50:48 +01002534Local<String> StackFrame::GetFunctionName() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002535 return getStringProperty(this, "functionName");
Kristian Monsen25f61362010-05-21 11:50:48 +01002536}
2537
2538
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002539static bool getBoolProperty(const StackFrame* f, const char* propertyName) {
2540 i::Isolate* isolate = Utils::OpenHandle(f)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01002541 ENTER_V8(isolate);
2542 i::HandleScope scope(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002543 i::Handle<i::JSObject> self = Utils::OpenHandle(f);
2544 i::Handle<i::Object> obj =
2545 i::Object::GetProperty(isolate, self, propertyName).ToHandleChecked();
2546 return obj->IsTrue();
Kristian Monsen25f61362010-05-21 11:50:48 +01002547}
2548
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002549bool StackFrame::IsEval() const { return getBoolProperty(this, "isEval"); }
2550
Kristian Monsen25f61362010-05-21 11:50:48 +01002551
2552bool StackFrame::IsConstructor() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002553 return getBoolProperty(this, "isConstructor");
2554}
2555
2556
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002557// --- N a t i v e W e a k M a p ---
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002558
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002559Local<NativeWeakMap> NativeWeakMap::New(Isolate* v8_isolate) {
2560 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
2561 ENTER_V8(isolate);
2562 i::Handle<i::JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
2563 i::JSWeakCollection::Initialize(weakmap, isolate);
2564 return Utils::NativeWeakMapToLocal(weakmap);
2565}
2566
2567
2568void NativeWeakMap::Set(Local<Value> v8_key, Local<Value> v8_value) {
2569 i::Handle<i::JSWeakMap> weak_collection = Utils::OpenHandle(this);
2570 i::Isolate* isolate = weak_collection->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01002571 ENTER_V8(isolate);
2572 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002573 i::Handle<i::Object> key = Utils::OpenHandle(*v8_key);
2574 i::Handle<i::Object> value = Utils::OpenHandle(*v8_value);
2575 if (!key->IsJSReceiver() && !key->IsSymbol()) {
2576 DCHECK(false);
2577 return;
2578 }
2579 i::Handle<i::ObjectHashTable> table(
2580 i::ObjectHashTable::cast(weak_collection->table()));
2581 if (!table->IsKey(*key)) {
2582 DCHECK(false);
2583 return;
2584 }
2585 int32_t hash = i::Object::GetOrCreateHash(isolate, key)->value();
2586 i::JSWeakCollection::Set(weak_collection, key, value, hash);
2587}
2588
2589
2590Local<Value> NativeWeakMap::Get(Local<Value> v8_key) {
2591 i::Handle<i::JSWeakMap> weak_collection = Utils::OpenHandle(this);
2592 i::Isolate* isolate = weak_collection->GetIsolate();
2593 ENTER_V8(isolate);
2594 i::Handle<i::Object> key = Utils::OpenHandle(*v8_key);
2595 if (!key->IsJSReceiver() && !key->IsSymbol()) {
2596 DCHECK(false);
2597 return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
2598 }
2599 i::Handle<i::ObjectHashTable> table(
2600 i::ObjectHashTable::cast(weak_collection->table()));
2601 if (!table->IsKey(*key)) {
2602 DCHECK(false);
2603 return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
2604 }
2605 i::Handle<i::Object> lookup(table->Lookup(key), isolate);
2606 if (lookup->IsTheHole())
2607 return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
2608 return Utils::ToLocal(lookup);
2609}
2610
2611
2612bool NativeWeakMap::Has(Local<Value> v8_key) {
2613 i::Handle<i::JSWeakMap> weak_collection = Utils::OpenHandle(this);
2614 i::Isolate* isolate = weak_collection->GetIsolate();
2615 ENTER_V8(isolate);
2616 i::HandleScope scope(isolate);
2617 i::Handle<i::Object> key = Utils::OpenHandle(*v8_key);
2618 if (!key->IsJSReceiver() && !key->IsSymbol()) {
2619 DCHECK(false);
2620 return false;
2621 }
2622 i::Handle<i::ObjectHashTable> table(
2623 i::ObjectHashTable::cast(weak_collection->table()));
2624 if (!table->IsKey(*key)) {
2625 DCHECK(false);
2626 return false;
2627 }
2628 i::Handle<i::Object> lookup(table->Lookup(key), isolate);
2629 return !lookup->IsTheHole();
2630}
2631
2632
2633bool NativeWeakMap::Delete(Local<Value> v8_key) {
2634 i::Handle<i::JSWeakMap> weak_collection = Utils::OpenHandle(this);
2635 i::Isolate* isolate = weak_collection->GetIsolate();
2636 ENTER_V8(isolate);
2637 i::HandleScope scope(isolate);
2638 i::Handle<i::Object> key = Utils::OpenHandle(*v8_key);
2639 if (!key->IsJSReceiver() && !key->IsSymbol()) {
2640 DCHECK(false);
2641 return false;
2642 }
2643 i::Handle<i::ObjectHashTable> table(
2644 i::ObjectHashTable::cast(weak_collection->table()));
2645 if (!table->IsKey(*key)) {
2646 DCHECK(false);
2647 return false;
2648 }
2649 int32_t hash = i::Object::GetOrCreateHash(isolate, key)->value();
2650 return i::JSWeakCollection::Delete(weak_collection, key, hash);
2651}
2652
2653
2654// --- J S O N ---
2655
2656MaybeLocal<Value> JSON::Parse(Isolate* v8_isolate, Local<String> json_string) {
2657 auto isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
2658 PREPARE_FOR_EXECUTION_WITH_ISOLATE(isolate, "JSON::Parse", Value);
2659 i::Handle<i::String> string = Utils::OpenHandle(*json_string);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002660 i::Handle<i::String> source = i::String::Flatten(string);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002661 auto maybe = source->IsSeqOneByteString()
2662 ? i::JsonParser<true>::Parse(source)
2663 : i::JsonParser<false>::Parse(source);
2664 Local<Value> result;
2665 has_pending_exception = !ToLocal<Value>(maybe, &result);
2666 RETURN_ON_FAILED_EXECUTION(Value);
2667 RETURN_ESCAPED(result);
2668}
2669
2670
2671Local<Value> JSON::Parse(Local<String> json_string) {
2672 auto isolate = reinterpret_cast<v8::Isolate*>(
2673 Utils::OpenHandle(*json_string)->GetIsolate());
2674 RETURN_TO_LOCAL_UNCHECKED(Parse(isolate, json_string), Value);
Kristian Monsen25f61362010-05-21 11:50:48 +01002675}
2676
2677
Steve Blocka7e24c12009-10-30 11:49:00 +00002678// --- D a t a ---
2679
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002680bool Value::FullIsUndefined() const {
2681 bool result = Utils::OpenHandle(this)->IsUndefined();
2682 DCHECK_EQ(result, QuickIsUndefined());
2683 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00002684}
2685
2686
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002687bool Value::FullIsNull() const {
2688 bool result = Utils::OpenHandle(this)->IsNull();
2689 DCHECK_EQ(result, QuickIsNull());
2690 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00002691}
2692
2693
2694bool Value::IsTrue() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002695 return Utils::OpenHandle(this)->IsTrue();
2696}
2697
2698
2699bool Value::IsFalse() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002700 return Utils::OpenHandle(this)->IsFalse();
2701}
2702
2703
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002704bool Value::IsFunction() const { return Utils::OpenHandle(this)->IsCallable(); }
Steve Blocka7e24c12009-10-30 11:49:00 +00002705
2706
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002707bool Value::IsName() const {
2708 return Utils::OpenHandle(this)->IsName();
2709}
2710
2711
Steve Blocka7e24c12009-10-30 11:49:00 +00002712bool Value::FullIsString() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002713 bool result = Utils::OpenHandle(this)->IsString();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002714 DCHECK_EQ(result, QuickIsString());
Steve Blocka7e24c12009-10-30 11:49:00 +00002715 return result;
2716}
2717
2718
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002719bool Value::IsSymbol() const {
2720 return Utils::OpenHandle(this)->IsSymbol();
2721}
2722
2723
Steve Blocka7e24c12009-10-30 11:49:00 +00002724bool Value::IsArray() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002725 return Utils::OpenHandle(this)->IsJSArray();
2726}
2727
2728
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002729bool Value::IsArrayBuffer() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002730 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2731 return obj->IsJSArrayBuffer() && !i::JSArrayBuffer::cast(*obj)->is_shared();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002732}
2733
2734
2735bool Value::IsArrayBufferView() const {
2736 return Utils::OpenHandle(this)->IsJSArrayBufferView();
2737}
2738
2739
2740bool Value::IsTypedArray() const {
2741 return Utils::OpenHandle(this)->IsJSTypedArray();
2742}
2743
2744
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002745#define VALUE_IS_TYPED_ARRAY(Type, typeName, TYPE, ctype, size) \
2746 bool Value::Is##Type##Array() const { \
2747 i::Handle<i::Object> obj = Utils::OpenHandle(this); \
2748 return obj->IsJSTypedArray() && \
2749 i::JSTypedArray::cast(*obj)->type() == i::kExternal##Type##Array; \
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002750 }
2751
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002752
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002753TYPED_ARRAYS(VALUE_IS_TYPED_ARRAY)
2754
2755#undef VALUE_IS_TYPED_ARRAY
2756
2757
2758bool Value::IsDataView() const {
2759 return Utils::OpenHandle(this)->IsJSDataView();
2760}
2761
2762
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002763bool Value::IsSharedArrayBuffer() const {
2764 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2765 return obj->IsJSArrayBuffer() && i::JSArrayBuffer::cast(*obj)->is_shared();
Steve Blocka7e24c12009-10-30 11:49:00 +00002766}
2767
2768
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002769bool Value::IsObject() const { return Utils::OpenHandle(this)->IsJSReceiver(); }
2770
2771
Steve Blocka7e24c12009-10-30 11:49:00 +00002772bool Value::IsNumber() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002773 return Utils::OpenHandle(this)->IsNumber();
2774}
2775
2776
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002777bool Value::IsProxy() const { return Utils::OpenHandle(this)->IsJSProxy(); }
2778
2779
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002780#define VALUE_IS_SPECIFIC_TYPE(Type, Class) \
2781 bool Value::Is##Type() const { \
2782 i::Handle<i::Object> obj = Utils::OpenHandle(this); \
2783 if (!obj->IsHeapObject()) return false; \
2784 i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate(); \
2785 return obj->HasSpecificClassOf(isolate->heap()->Class##_string()); \
Steve Block44f0eee2011-05-26 01:26:41 +01002786 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002787
2788VALUE_IS_SPECIFIC_TYPE(ArgumentsObject, Arguments)
2789VALUE_IS_SPECIFIC_TYPE(BooleanObject, Boolean)
2790VALUE_IS_SPECIFIC_TYPE(NumberObject, Number)
2791VALUE_IS_SPECIFIC_TYPE(StringObject, String)
2792VALUE_IS_SPECIFIC_TYPE(SymbolObject, Symbol)
2793VALUE_IS_SPECIFIC_TYPE(Date, Date)
2794VALUE_IS_SPECIFIC_TYPE(Map, Map)
2795VALUE_IS_SPECIFIC_TYPE(Set, Set)
2796VALUE_IS_SPECIFIC_TYPE(WeakMap, WeakMap)
2797VALUE_IS_SPECIFIC_TYPE(WeakSet, WeakSet)
2798
2799#undef VALUE_IS_SPECIFIC_TYPE
2800
2801
2802bool Value::IsBoolean() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002803 return Utils::OpenHandle(this)->IsBoolean();
2804}
2805
2806
2807bool Value::IsExternal() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002808 return Utils::OpenHandle(this)->IsExternal();
Steve Blocka7e24c12009-10-30 11:49:00 +00002809}
2810
2811
2812bool Value::IsInt32() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002813 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2814 if (obj->IsSmi()) return true;
2815 if (obj->IsNumber()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002816 return i::IsInt32Double(obj->Number());
Steve Blocka7e24c12009-10-30 11:49:00 +00002817 }
2818 return false;
2819}
2820
2821
Steve Block6ded16b2010-05-10 14:33:55 +01002822bool Value::IsUint32() const {
Steve Block6ded16b2010-05-10 14:33:55 +01002823 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2824 if (obj->IsSmi()) return i::Smi::cast(*obj)->value() >= 0;
2825 if (obj->IsNumber()) {
2826 double value = obj->Number();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002827 return !i::IsMinusZero(value) &&
2828 value >= 0 &&
2829 value <= i::kMaxUInt32 &&
2830 value == i::FastUI2D(i::FastD2UI(value));
Steve Block6ded16b2010-05-10 14:33:55 +01002831 }
2832 return false;
2833}
2834
2835
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002836bool Value::IsNativeError() const {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002837 i::Handle<i::Object> obj = Utils::OpenHandle(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002838 if (!obj->IsJSObject()) return false;
2839 i::Handle<i::JSObject> js_obj = i::Handle<i::JSObject>::cast(obj);
2840 i::Isolate* isolate = js_obj->GetIsolate();
2841 i::Handle<i::Object> constructor(js_obj->map()->GetConstructor(), isolate);
2842 if (!constructor->IsJSFunction()) return false;
2843 i::Handle<i::JSFunction> function =
2844 i::Handle<i::JSFunction>::cast(constructor);
2845 if (!function->shared()->native()) return false;
2846 return function.is_identical_to(isolate->error_function()) ||
2847 function.is_identical_to(isolate->eval_error_function()) ||
2848 function.is_identical_to(isolate->range_error_function()) ||
2849 function.is_identical_to(isolate->reference_error_function()) ||
2850 function.is_identical_to(isolate->syntax_error_function()) ||
2851 function.is_identical_to(isolate->type_error_function()) ||
2852 function.is_identical_to(isolate->uri_error_function());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002853}
2854
2855
Iain Merrick75681382010-08-19 15:07:18 +01002856bool Value::IsRegExp() const {
Iain Merrick75681382010-08-19 15:07:18 +01002857 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2858 return obj->IsJSRegExp();
2859}
2860
2861
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002862bool Value::IsGeneratorFunction() const {
2863 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2864 if (!obj->IsJSFunction()) return false;
2865 i::Handle<i::JSFunction> func = i::Handle<i::JSFunction>::cast(obj);
2866 return func->shared()->is_generator();
2867}
2868
2869
2870bool Value::IsGeneratorObject() const {
2871 return Utils::OpenHandle(this)->IsJSGeneratorObject();
2872}
2873
2874
2875bool Value::IsMapIterator() const {
2876 return Utils::OpenHandle(this)->IsJSMapIterator();
2877}
2878
2879
2880bool Value::IsSetIterator() const {
2881 return Utils::OpenHandle(this)->IsJSSetIterator();
2882}
2883
2884
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002885bool Value::IsPromise() const {
2886 auto self = Utils::OpenHandle(this);
2887 return i::Object::IsPromise(self);
Steve Blocka7e24c12009-10-30 11:49:00 +00002888}
2889
2890
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002891MaybeLocal<String> Value::ToString(Local<Context> context) const {
2892 auto obj = Utils::OpenHandle(this);
2893 if (obj->IsString()) return ToApiHandle<String>(obj);
2894 PREPARE_FOR_EXECUTION(context, "ToString", String);
2895 Local<String> result;
2896 has_pending_exception =
2897 !ToLocal<String>(i::Object::ToString(isolate, obj), &result);
2898 RETURN_ON_FAILED_EXECUTION(String);
2899 RETURN_ESCAPED(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00002900}
2901
2902
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002903Local<String> Value::ToString(Isolate* isolate) const {
2904 RETURN_TO_LOCAL_UNCHECKED(ToString(isolate->GetCurrentContext()), String);
2905}
2906
2907
2908MaybeLocal<String> Value::ToDetailString(Local<Context> context) const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002909 i::Handle<i::Object> obj = Utils::OpenHandle(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002910 if (obj->IsString()) return ToApiHandle<String>(obj);
2911 PREPARE_FOR_EXECUTION(context, "ToDetailString", String);
2912 Local<String> result;
2913 i::Handle<i::Object> args[] = {obj};
2914 has_pending_exception = !ToLocal<String>(
2915 i::Execution::TryCall(isolate, isolate->no_side_effects_to_string_fun(),
2916 isolate->factory()->undefined_value(),
2917 arraysize(args), args),
2918 &result);
2919 RETURN_ON_FAILED_EXECUTION(String);
2920 RETURN_ESCAPED(result);
2921}
2922
2923
2924Local<String> Value::ToDetailString(Isolate* isolate) const {
2925 RETURN_TO_LOCAL_UNCHECKED(ToDetailString(isolate->GetCurrentContext()),
2926 String);
2927}
2928
2929
2930MaybeLocal<Object> Value::ToObject(Local<Context> context) const {
2931 auto obj = Utils::OpenHandle(this);
2932 if (obj->IsJSObject()) return ToApiHandle<Object>(obj);
2933 PREPARE_FOR_EXECUTION(context, "ToObject", Object);
2934 Local<Object> result;
2935 has_pending_exception =
2936 !ToLocal<Object>(i::Execution::ToObject(isolate, obj), &result);
2937 RETURN_ON_FAILED_EXECUTION(Object);
2938 RETURN_ESCAPED(result);
2939}
2940
2941
2942Local<v8::Object> Value::ToObject(Isolate* isolate) const {
2943 RETURN_TO_LOCAL_UNCHECKED(ToObject(isolate->GetCurrentContext()), Object);
2944}
2945
2946
2947MaybeLocal<Boolean> Value::ToBoolean(Local<Context> context) const {
2948 auto obj = Utils::OpenHandle(this);
2949 if (obj->IsBoolean()) return ToApiHandle<Boolean>(obj);
2950 auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
2951 auto val = isolate->factory()->ToBoolean(obj->BooleanValue());
2952 return ToApiHandle<Boolean>(val);
Steve Blocka7e24c12009-10-30 11:49:00 +00002953}
2954
2955
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002956Local<Boolean> Value::ToBoolean(Isolate* v8_isolate) const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002957 return ToBoolean(v8_isolate->GetCurrentContext()).ToLocalChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +00002958}
2959
2960
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002961MaybeLocal<Number> Value::ToNumber(Local<Context> context) const {
2962 auto obj = Utils::OpenHandle(this);
2963 if (obj->IsNumber()) return ToApiHandle<Number>(obj);
2964 PREPARE_FOR_EXECUTION(context, "ToNumber", Number);
2965 Local<Number> result;
2966 has_pending_exception = !ToLocal<Number>(i::Object::ToNumber(obj), &result);
2967 RETURN_ON_FAILED_EXECUTION(Number);
2968 RETURN_ESCAPED(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00002969}
2970
2971
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002972Local<Number> Value::ToNumber(Isolate* isolate) const {
2973 RETURN_TO_LOCAL_UNCHECKED(ToNumber(isolate->GetCurrentContext()), Number);
2974}
2975
2976
2977MaybeLocal<Integer> Value::ToInteger(Local<Context> context) const {
2978 auto obj = Utils::OpenHandle(this);
2979 if (obj->IsSmi()) return ToApiHandle<Integer>(obj);
2980 PREPARE_FOR_EXECUTION(context, "ToInteger", Integer);
2981 Local<Integer> result;
2982 has_pending_exception =
2983 !ToLocal<Integer>(i::Object::ToInteger(isolate, obj), &result);
2984 RETURN_ON_FAILED_EXECUTION(Integer);
2985 RETURN_ESCAPED(result);
2986}
2987
2988
2989Local<Integer> Value::ToInteger(Isolate* isolate) const {
2990 RETURN_TO_LOCAL_UNCHECKED(ToInteger(isolate->GetCurrentContext()), Integer);
2991}
2992
2993
2994MaybeLocal<Int32> Value::ToInt32(Local<Context> context) const {
2995 auto obj = Utils::OpenHandle(this);
2996 if (obj->IsSmi()) return ToApiHandle<Int32>(obj);
2997 Local<Int32> result;
2998 PREPARE_FOR_EXECUTION(context, "ToInt32", Int32);
2999 has_pending_exception =
3000 !ToLocal<Int32>(i::Object::ToInt32(isolate, obj), &result);
3001 RETURN_ON_FAILED_EXECUTION(Int32);
3002 RETURN_ESCAPED(result);
3003}
3004
3005
3006Local<Int32> Value::ToInt32(Isolate* isolate) const {
3007 RETURN_TO_LOCAL_UNCHECKED(ToInt32(isolate->GetCurrentContext()), Int32);
3008}
3009
3010
3011MaybeLocal<Uint32> Value::ToUint32(Local<Context> context) const {
3012 auto obj = Utils::OpenHandle(this);
3013 if (obj->IsSmi()) return ToApiHandle<Uint32>(obj);
3014 Local<Uint32> result;
3015 PREPARE_FOR_EXECUTION(context, "ToUint32", Uint32);
3016 has_pending_exception =
3017 !ToLocal<Uint32>(i::Object::ToUint32(isolate, obj), &result);
3018 RETURN_ON_FAILED_EXECUTION(Uint32);
3019 RETURN_ESCAPED(result);
3020}
3021
3022
3023Local<Uint32> Value::ToUint32(Isolate* isolate) const {
3024 RETURN_TO_LOCAL_UNCHECKED(ToUint32(isolate->GetCurrentContext()), Uint32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003025}
3026
3027
3028void i::Internals::CheckInitializedImpl(v8::Isolate* external_isolate) {
3029 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
3030 Utils::ApiCheck(isolate != NULL &&
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003031 !isolate->IsDead(),
3032 "v8::internal::Internals::CheckInitialized()",
3033 "Isolate is not initialized or V8 has died");
Steve Blocka7e24c12009-10-30 11:49:00 +00003034}
3035
3036
3037void External::CheckCast(v8::Value* that) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003038 Utils::ApiCheck(Utils::OpenHandle(that)->IsExternal(),
3039 "v8::External::Cast()",
3040 "Could not convert to external");
Steve Blocka7e24c12009-10-30 11:49:00 +00003041}
3042
3043
3044void v8::Object::CheckCast(Value* that) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003045 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003046 Utils::ApiCheck(obj->IsJSReceiver(), "v8::Object::Cast()",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003047 "Could not convert to object");
Steve Blocka7e24c12009-10-30 11:49:00 +00003048}
3049
3050
3051void v8::Function::CheckCast(Value* that) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003052 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003053 Utils::ApiCheck(obj->IsCallable(), "v8::Function::Cast()",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003054 "Could not convert to function");
3055}
3056
3057
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003058void v8::Boolean::CheckCast(v8::Value* that) {
3059 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3060 Utils::ApiCheck(obj->IsBoolean(),
3061 "v8::Boolean::Cast()",
3062 "Could not convert to boolean");
3063}
3064
3065
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003066void v8::Name::CheckCast(v8::Value* that) {
3067 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3068 Utils::ApiCheck(obj->IsName(),
3069 "v8::Name::Cast()",
3070 "Could not convert to name");
Steve Blocka7e24c12009-10-30 11:49:00 +00003071}
3072
3073
3074void v8::String::CheckCast(v8::Value* that) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003075 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003076 Utils::ApiCheck(obj->IsString(),
3077 "v8::String::Cast()",
3078 "Could not convert to string");
3079}
3080
3081
3082void v8::Symbol::CheckCast(v8::Value* that) {
3083 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3084 Utils::ApiCheck(obj->IsSymbol(),
3085 "v8::Symbol::Cast()",
3086 "Could not convert to symbol");
Steve Blocka7e24c12009-10-30 11:49:00 +00003087}
3088
3089
3090void v8::Number::CheckCast(v8::Value* that) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003091 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003092 Utils::ApiCheck(obj->IsNumber(),
3093 "v8::Number::Cast()",
3094 "Could not convert to number");
Steve Blocka7e24c12009-10-30 11:49:00 +00003095}
3096
3097
3098void v8::Integer::CheckCast(v8::Value* that) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003099 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003100 Utils::ApiCheck(obj->IsNumber(),
3101 "v8::Integer::Cast()",
3102 "Could not convert to number");
Steve Blocka7e24c12009-10-30 11:49:00 +00003103}
3104
3105
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003106void v8::Int32::CheckCast(v8::Value* that) {
3107 Utils::ApiCheck(that->IsInt32(), "v8::Int32::Cast()",
3108 "Could not convert to 32-bit signed integer");
3109}
3110
3111
3112void v8::Uint32::CheckCast(v8::Value* that) {
3113 Utils::ApiCheck(that->IsUint32(), "v8::Uint32::Cast()",
3114 "Could not convert to 32-bit unsigned integer");
3115}
3116
3117
Steve Blocka7e24c12009-10-30 11:49:00 +00003118void v8::Array::CheckCast(Value* that) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003119 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003120 Utils::ApiCheck(obj->IsJSArray(),
3121 "v8::Array::Cast()",
3122 "Could not convert to array");
3123}
3124
3125
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003126void v8::Map::CheckCast(Value* that) {
3127 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3128 Utils::ApiCheck(obj->IsJSMap(), "v8::Map::Cast()",
3129 "Could not convert to Map");
3130}
3131
3132
3133void v8::Set::CheckCast(Value* that) {
3134 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3135 Utils::ApiCheck(obj->IsJSSet(), "v8::Set::Cast()",
3136 "Could not convert to Set");
3137}
3138
3139
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003140void v8::Promise::CheckCast(Value* that) {
3141 Utils::ApiCheck(that->IsPromise(),
3142 "v8::Promise::Cast()",
3143 "Could not convert to promise");
3144}
3145
3146
3147void v8::Promise::Resolver::CheckCast(Value* that) {
3148 Utils::ApiCheck(that->IsPromise(),
3149 "v8::Promise::Resolver::Cast()",
3150 "Could not convert to promise resolver");
3151}
3152
3153
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003154void v8::Proxy::CheckCast(Value* that) {
3155 Utils::ApiCheck(that->IsProxy(), "v8::Proxy::Cast()",
3156 "Could not convert to proxy");
3157}
3158
3159
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003160void v8::ArrayBuffer::CheckCast(Value* that) {
3161 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003162 Utils::ApiCheck(
3163 obj->IsJSArrayBuffer() && !i::JSArrayBuffer::cast(*obj)->is_shared(),
3164 "v8::ArrayBuffer::Cast()", "Could not convert to ArrayBuffer");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003165}
3166
3167
3168void v8::ArrayBufferView::CheckCast(Value* that) {
3169 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3170 Utils::ApiCheck(obj->IsJSArrayBufferView(),
3171 "v8::ArrayBufferView::Cast()",
3172 "Could not convert to ArrayBufferView");
3173}
3174
3175
3176void v8::TypedArray::CheckCast(Value* that) {
3177 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3178 Utils::ApiCheck(obj->IsJSTypedArray(),
3179 "v8::TypedArray::Cast()",
3180 "Could not convert to TypedArray");
3181}
3182
3183
3184#define CHECK_TYPED_ARRAY_CAST(Type, typeName, TYPE, ctype, size) \
3185 void v8::Type##Array::CheckCast(Value* that) { \
3186 i::Handle<i::Object> obj = Utils::OpenHandle(that); \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003187 Utils::ApiCheck( \
3188 obj->IsJSTypedArray() && \
3189 i::JSTypedArray::cast(*obj)->type() == i::kExternal##Type##Array, \
3190 "v8::" #Type "Array::Cast()", "Could not convert to " #Type "Array"); \
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003191 }
3192
3193
3194TYPED_ARRAYS(CHECK_TYPED_ARRAY_CAST)
3195
3196#undef CHECK_TYPED_ARRAY_CAST
3197
3198
3199void v8::DataView::CheckCast(Value* that) {
3200 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3201 Utils::ApiCheck(obj->IsJSDataView(),
3202 "v8::DataView::Cast()",
3203 "Could not convert to DataView");
Steve Blocka7e24c12009-10-30 11:49:00 +00003204}
3205
3206
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003207void v8::SharedArrayBuffer::CheckCast(Value* that) {
3208 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3209 Utils::ApiCheck(
3210 obj->IsJSArrayBuffer() && i::JSArrayBuffer::cast(*obj)->is_shared(),
3211 "v8::SharedArrayBuffer::Cast()",
3212 "Could not convert to SharedArrayBuffer");
3213}
3214
3215
Steve Blocka7e24c12009-10-30 11:49:00 +00003216void v8::Date::CheckCast(v8::Value* that) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003217 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003218 i::Isolate* isolate = NULL;
3219 if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
3220 Utils::ApiCheck(isolate != NULL &&
3221 obj->HasSpecificClassOf(isolate->heap()->Date_string()),
3222 "v8::Date::Cast()",
3223 "Could not convert to date");
Steve Blocka7e24c12009-10-30 11:49:00 +00003224}
3225
3226
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003227void v8::StringObject::CheckCast(v8::Value* that) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003228 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003229 i::Isolate* isolate = NULL;
3230 if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
3231 Utils::ApiCheck(isolate != NULL &&
3232 obj->HasSpecificClassOf(isolate->heap()->String_string()),
3233 "v8::StringObject::Cast()",
3234 "Could not convert to StringObject");
3235}
3236
3237
3238void v8::SymbolObject::CheckCast(v8::Value* that) {
3239 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3240 i::Isolate* isolate = NULL;
3241 if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
3242 Utils::ApiCheck(isolate != NULL &&
3243 obj->HasSpecificClassOf(isolate->heap()->Symbol_string()),
3244 "v8::SymbolObject::Cast()",
3245 "Could not convert to SymbolObject");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003246}
3247
3248
3249void v8::NumberObject::CheckCast(v8::Value* that) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003250 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003251 i::Isolate* isolate = NULL;
3252 if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
3253 Utils::ApiCheck(isolate != NULL &&
3254 obj->HasSpecificClassOf(isolate->heap()->Number_string()),
3255 "v8::NumberObject::Cast()",
3256 "Could not convert to NumberObject");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003257}
3258
3259
3260void v8::BooleanObject::CheckCast(v8::Value* that) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003261 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003262 i::Isolate* isolate = NULL;
3263 if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
3264 Utils::ApiCheck(isolate != NULL &&
3265 obj->HasSpecificClassOf(isolate->heap()->Boolean_string()),
3266 "v8::BooleanObject::Cast()",
3267 "Could not convert to BooleanObject");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003268}
3269
3270
Ben Murdochf87a2032010-10-22 12:50:53 +01003271void v8::RegExp::CheckCast(v8::Value* that) {
Ben Murdochf87a2032010-10-22 12:50:53 +01003272 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003273 Utils::ApiCheck(obj->IsJSRegExp(),
3274 "v8::RegExp::Cast()",
3275 "Could not convert to regular expression");
Ben Murdochf87a2032010-10-22 12:50:53 +01003276}
3277
3278
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003279Maybe<bool> Value::BooleanValue(Local<Context> context) const {
3280 return Just(Utils::OpenHandle(this)->BooleanValue());
3281}
3282
3283
Steve Blocka7e24c12009-10-30 11:49:00 +00003284bool Value::BooleanValue() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003285 return Utils::OpenHandle(this)->BooleanValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00003286}
3287
3288
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003289Maybe<double> Value::NumberValue(Local<Context> context) const {
3290 auto obj = Utils::OpenHandle(this);
3291 if (obj->IsNumber()) return Just(obj->Number());
3292 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "NumberValue", double);
3293 i::Handle<i::Object> num;
3294 has_pending_exception = !i::Object::ToNumber(obj).ToHandle(&num);
3295 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(double);
3296 return Just(num->Number());
3297}
3298
3299
Steve Blocka7e24c12009-10-30 11:49:00 +00003300double Value::NumberValue() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003301 auto obj = Utils::OpenHandle(this);
3302 if (obj->IsNumber()) return obj->Number();
3303 return NumberValue(ContextFromHeapObject(obj))
3304 .FromMaybe(std::numeric_limits<double>::quiet_NaN());
3305}
3306
3307
3308Maybe<int64_t> Value::IntegerValue(Local<Context> context) const {
3309 auto obj = Utils::OpenHandle(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00003310 i::Handle<i::Object> num;
3311 if (obj->IsNumber()) {
3312 num = obj;
3313 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003314 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "IntegerValue", int64_t);
3315 has_pending_exception = !i::Object::ToInteger(isolate, obj).ToHandle(&num);
3316 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(int64_t);
Steve Blocka7e24c12009-10-30 11:49:00 +00003317 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003318 return Just(num->IsSmi() ? static_cast<int64_t>(i::Smi::cast(*num)->value())
3319 : static_cast<int64_t>(num->Number()));
Steve Blocka7e24c12009-10-30 11:49:00 +00003320}
3321
3322
3323int64_t Value::IntegerValue() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003324 auto obj = Utils::OpenHandle(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00003325 if (obj->IsNumber()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003326 if (obj->IsSmi()) {
3327 return i::Smi::cast(*obj)->value();
3328 } else {
3329 return static_cast<int64_t>(obj->Number());
3330 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003331 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003332 return IntegerValue(ContextFromHeapObject(obj)).FromMaybe(0);
Steve Blocka7e24c12009-10-30 11:49:00 +00003333}
3334
3335
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003336Maybe<int32_t> Value::Int32Value(Local<Context> context) const {
3337 auto obj = Utils::OpenHandle(this);
3338 if (obj->IsNumber()) return Just(NumberToInt32(*obj));
3339 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "Int32Value", int32_t);
Steve Blocka7e24c12009-10-30 11:49:00 +00003340 i::Handle<i::Object> num;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003341 has_pending_exception = !i::Object::ToInt32(isolate, obj).ToHandle(&num);
3342 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(int32_t);
3343 return Just(num->IsSmi() ? i::Smi::cast(*num)->value()
3344 : static_cast<int32_t>(num->Number()));
Steve Blocka7e24c12009-10-30 11:49:00 +00003345}
3346
3347
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003348int32_t Value::Int32Value() const {
3349 auto obj = Utils::OpenHandle(this);
3350 if (obj->IsNumber()) return NumberToInt32(*obj);
3351 return Int32Value(ContextFromHeapObject(obj)).FromMaybe(0);
3352}
3353
3354
3355Maybe<uint32_t> Value::Uint32Value(Local<Context> context) const {
3356 auto obj = Utils::OpenHandle(this);
3357 if (obj->IsNumber()) return Just(NumberToUint32(*obj));
3358 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "Uint32Value", uint32_t);
Steve Blocka7e24c12009-10-30 11:49:00 +00003359 i::Handle<i::Object> num;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003360 has_pending_exception = !i::Object::ToUint32(isolate, obj).ToHandle(&num);
3361 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(uint32_t);
3362 return Just(num->IsSmi() ? static_cast<uint32_t>(i::Smi::cast(*num)->value())
3363 : static_cast<uint32_t>(num->Number()));
Steve Blocka7e24c12009-10-30 11:49:00 +00003364}
3365
3366
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003367uint32_t Value::Uint32Value() const {
3368 auto obj = Utils::OpenHandle(this);
3369 if (obj->IsNumber()) return NumberToUint32(*obj);
3370 return Uint32Value(ContextFromHeapObject(obj)).FromMaybe(0);
3371}
3372
3373
3374MaybeLocal<Uint32> Value::ToArrayIndex(Local<Context> context) const {
3375 auto self = Utils::OpenHandle(this);
3376 if (self->IsSmi()) {
3377 if (i::Smi::cast(*self)->value() >= 0) return Utils::Uint32ToLocal(self);
Steve Blocka7e24c12009-10-30 11:49:00 +00003378 return Local<Uint32>();
3379 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003380 PREPARE_FOR_EXECUTION(context, "ToArrayIndex", Uint32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003381 i::Handle<i::Object> string_obj;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003382 has_pending_exception =
3383 !i::Object::ToString(isolate, self).ToHandle(&string_obj);
3384 RETURN_ON_FAILED_EXECUTION(Uint32);
Steve Blocka7e24c12009-10-30 11:49:00 +00003385 i::Handle<i::String> str = i::Handle<i::String>::cast(string_obj);
3386 uint32_t index;
3387 if (str->AsArrayIndex(&index)) {
3388 i::Handle<i::Object> value;
3389 if (index <= static_cast<uint32_t>(i::Smi::kMaxValue)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003390 value = i::Handle<i::Object>(i::Smi::FromInt(index), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003391 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01003392 value = isolate->factory()->NewNumber(index);
Steve Blocka7e24c12009-10-30 11:49:00 +00003393 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003394 RETURN_ESCAPED(Utils::Uint32ToLocal(value));
Steve Blocka7e24c12009-10-30 11:49:00 +00003395 }
3396 return Local<Uint32>();
3397}
3398
3399
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003400Local<Uint32> Value::ToArrayIndex() const {
3401 auto self = Utils::OpenHandle(this);
3402 if (self->IsSmi()) {
3403 if (i::Smi::cast(*self)->value() >= 0) return Utils::Uint32ToLocal(self);
3404 return Local<Uint32>();
Steve Blocka7e24c12009-10-30 11:49:00 +00003405 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003406 auto context = ContextFromHeapObject(self);
3407 RETURN_TO_LOCAL_UNCHECKED(ToArrayIndex(context), Uint32);
Steve Blocka7e24c12009-10-30 11:49:00 +00003408}
3409
3410
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003411Maybe<bool> Value::Equals(Local<Context> context, Local<Value> that) const {
3412 auto self = Utils::OpenHandle(this);
3413 auto other = Utils::OpenHandle(*that);
3414 return i::Object::Equals(self, other);
Steve Blocka7e24c12009-10-30 11:49:00 +00003415}
3416
3417
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003418bool Value::Equals(Local<Value> that) const {
3419 auto self = Utils::OpenHandle(this);
3420 auto other = Utils::OpenHandle(*that);
3421 if (self->IsSmi() && other->IsSmi()) {
3422 return self->Number() == other->Number();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003423 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003424 if (self->IsJSObject() && other->IsJSObject()) {
3425 return *self == *other;
Steve Blocka7e24c12009-10-30 11:49:00 +00003426 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003427 auto heap_object = self->IsSmi() ? other : self;
3428 auto context = ContextFromHeapObject(heap_object);
3429 return Equals(context, that).FromMaybe(false);
Steve Blocka7e24c12009-10-30 11:49:00 +00003430}
3431
3432
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003433bool Value::StrictEquals(Local<Value> that) const {
3434 auto self = Utils::OpenHandle(this);
3435 auto other = Utils::OpenHandle(*that);
3436 return self->StrictEquals(*other);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003437}
3438
3439
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003440bool Value::SameValue(Local<Value> that) const {
3441 auto self = Utils::OpenHandle(this);
3442 auto other = Utils::OpenHandle(*that);
3443 return self->SameValue(*other);
Steve Blocka7e24c12009-10-30 11:49:00 +00003444}
3445
3446
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003447Maybe<bool> v8::Object::Set(v8::Local<v8::Context> context,
3448 v8::Local<Value> key, v8::Local<Value> value) {
3449 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Object::Set()", bool);
3450 auto self = Utils::OpenHandle(this);
3451 auto key_obj = Utils::OpenHandle(*key);
3452 auto value_obj = Utils::OpenHandle(*value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003453 has_pending_exception =
3454 i::Runtime::SetObjectProperty(isolate, self, key_obj, value_obj,
3455 i::SLOPPY).is_null();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003456 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3457 return Just(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00003458}
3459
3460
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003461bool v8::Object::Set(v8::Local<Value> key, v8::Local<Value> value) {
3462 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3463 return Set(context, key, value).FromMaybe(false);
3464}
3465
3466
3467Maybe<bool> v8::Object::Set(v8::Local<v8::Context> context, uint32_t index,
3468 v8::Local<Value> value) {
3469 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Object::Set()", bool);
3470 auto self = Utils::OpenHandle(this);
3471 auto value_obj = Utils::OpenHandle(*value);
3472 has_pending_exception = i::Object::SetElement(isolate, self, index, value_obj,
3473 i::SLOPPY).is_null();
3474 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3475 return Just(true);
3476}
3477
3478
3479bool v8::Object::Set(uint32_t index, v8::Local<Value> value) {
3480 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3481 return Set(context, index, value).FromMaybe(false);
3482}
3483
3484
3485Maybe<bool> v8::Object::CreateDataProperty(v8::Local<v8::Context> context,
3486 v8::Local<Name> key,
3487 v8::Local<Value> value) {
3488 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Object::CreateDataProperty()",
3489 bool);
3490 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
3491 i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
Steve Block6ded16b2010-05-10 14:33:55 +01003492 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003493
3494 i::LookupIterator it = i::LookupIterator::PropertyOrElement(
3495 isolate, self, key_obj, i::LookupIterator::OWN);
3496 Maybe<bool> result =
3497 i::JSReceiver::CreateDataProperty(&it, value_obj, i::Object::DONT_THROW);
3498 has_pending_exception = result.IsNothing();
3499 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3500 return result;
Steve Block6ded16b2010-05-10 14:33:55 +01003501}
3502
3503
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003504Maybe<bool> v8::Object::CreateDataProperty(v8::Local<v8::Context> context,
3505 uint32_t index,
3506 v8::Local<Value> value) {
3507 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Object::CreateDataProperty()",
3508 bool);
3509 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
3510 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
3511
3512 i::LookupIterator it(isolate, self, index, i::LookupIterator::OWN);
3513 Maybe<bool> result =
3514 i::JSReceiver::CreateDataProperty(&it, value_obj, i::Object::DONT_THROW);
3515 has_pending_exception = result.IsNothing();
3516 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3517 return result;
3518}
3519
3520
3521Maybe<bool> v8::Object::DefineOwnProperty(v8::Local<v8::Context> context,
3522 v8::Local<Name> key,
3523 v8::Local<Value> value,
3524 v8::PropertyAttribute attributes) {
3525 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Object::DefineOwnProperty()",
3526 bool);
3527 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
3528 i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
3529 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
3530
3531 if (self->IsAccessCheckNeeded() &&
3532 !isolate->MayAccess(handle(isolate->context()),
3533 i::Handle<i::JSObject>::cast(self))) {
3534 isolate->ReportFailedAccessCheck(i::Handle<i::JSObject>::cast(self));
3535 return Nothing<bool>();
3536 }
3537
3538 i::PropertyDescriptor desc;
3539 desc.set_writable(!(attributes & v8::ReadOnly));
3540 desc.set_enumerable(!(attributes & v8::DontEnum));
3541 desc.set_configurable(!(attributes & v8::DontDelete));
3542 desc.set_value(value_obj);
3543 Maybe<bool> success = i::JSReceiver::DefineOwnProperty(
3544 isolate, self, key_obj, &desc, i::Object::DONT_THROW);
3545 // Even though we said DONT_THROW, there might be accessors that do throw.
3546 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3547 return success;
3548}
3549
3550
3551MUST_USE_RESULT
3552static i::MaybeHandle<i::Object> DefineObjectProperty(
3553 i::Handle<i::JSObject> js_object, i::Handle<i::Object> key,
3554 i::Handle<i::Object> value, i::PropertyAttributes attrs) {
3555 i::Isolate* isolate = js_object->GetIsolate();
3556 bool success = false;
3557 i::LookupIterator it = i::LookupIterator::PropertyOrElement(
3558 isolate, js_object, key, &success, i::LookupIterator::OWN);
3559 if (!success) return i::MaybeHandle<i::Object>();
3560
3561 return i::JSObject::DefineOwnPropertyIgnoreAttributes(&it, value, attrs);
3562}
3563
3564
3565Maybe<bool> v8::Object::ForceSet(v8::Local<v8::Context> context,
3566 v8::Local<Value> key, v8::Local<Value> value,
3567 v8::PropertyAttribute attribs) {
3568 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Object::ForceSet()", bool);
3569 auto self = i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
3570 auto key_obj = Utils::OpenHandle(*key);
3571 auto value_obj = Utils::OpenHandle(*value);
3572 has_pending_exception =
3573 DefineObjectProperty(self, key_obj, value_obj,
3574 static_cast<i::PropertyAttributes>(attribs))
3575 .is_null();
3576 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3577 return Just(true);
3578}
3579
3580
3581bool v8::Object::ForceSet(v8::Local<Value> key, v8::Local<Value> value,
Steve Blocka7e24c12009-10-30 11:49:00 +00003582 v8::PropertyAttribute attribs) {
Steve Block44f0eee2011-05-26 01:26:41 +01003583 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003584 PREPARE_FOR_EXECUTION_GENERIC(isolate, Local<Context>(),
3585 "v8::Object::ForceSet", false, i::HandleScope,
3586 false);
3587 i::Handle<i::JSObject> self =
3588 i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
Steve Blocka7e24c12009-10-30 11:49:00 +00003589 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
3590 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003591 has_pending_exception =
3592 DefineObjectProperty(self, key_obj, value_obj,
3593 static_cast<i::PropertyAttributes>(attribs))
3594 .is_null();
3595 EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00003596 return true;
3597}
3598
3599
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003600Maybe<bool> v8::Object::SetPrivate(Local<Context> context, Local<Private> key,
3601 Local<Value> value) {
3602 return DefineOwnProperty(context, Local<Name>(reinterpret_cast<Name*>(*key)),
3603 value, DontEnum);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003604}
3605
3606
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003607MaybeLocal<Value> v8::Object::Get(Local<v8::Context> context,
3608 Local<Value> key) {
3609 PREPARE_FOR_EXECUTION(context, "v8::Object::Get()", Value);
3610 auto self = Utils::OpenHandle(this);
3611 auto key_obj = Utils::OpenHandle(*key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003612 i::Handle<i::Object> result;
3613 has_pending_exception =
3614 !i::Runtime::GetObjectProperty(isolate, self, key_obj).ToHandle(&result);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003615 RETURN_ON_FAILED_EXECUTION(Value);
3616 RETURN_ESCAPED(Utils::ToLocal(result));
3617}
3618
3619
3620Local<Value> v8::Object::Get(v8::Local<Value> key) {
3621 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3622 RETURN_TO_LOCAL_UNCHECKED(Get(context, key), Value);
3623}
3624
3625
3626MaybeLocal<Value> v8::Object::Get(Local<Context> context, uint32_t index) {
3627 PREPARE_FOR_EXECUTION(context, "v8::Object::Get()", Value);
3628 auto self = Utils::OpenHandle(this);
3629 i::Handle<i::Object> result;
3630 has_pending_exception =
3631 !i::Object::GetElement(isolate, self, index).ToHandle(&result);
3632 RETURN_ON_FAILED_EXECUTION(Value);
3633 RETURN_ESCAPED(Utils::ToLocal(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00003634}
3635
3636
Steve Block6ded16b2010-05-10 14:33:55 +01003637Local<Value> v8::Object::Get(uint32_t index) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003638 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3639 RETURN_TO_LOCAL_UNCHECKED(Get(context, index), Value);
Steve Block6ded16b2010-05-10 14:33:55 +01003640}
3641
3642
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003643MaybeLocal<Value> v8::Object::GetPrivate(Local<Context> context,
3644 Local<Private> key) {
3645 return Get(context, Local<Value>(reinterpret_cast<Value*>(*key)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003646}
3647
3648
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003649Maybe<PropertyAttribute> v8::Object::GetPropertyAttributes(
3650 Local<Context> context, Local<Value> key) {
3651 PREPARE_FOR_EXECUTION_PRIMITIVE(
3652 context, "v8::Object::GetPropertyAttributes()", PropertyAttribute);
3653 auto self = Utils::OpenHandle(this);
3654 auto key_obj = Utils::OpenHandle(*key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003655 if (!key_obj->IsName()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003656 has_pending_exception =
3657 !i::Object::ToString(isolate, key_obj).ToHandle(&key_obj);
3658 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(PropertyAttribute);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003659 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003660 auto key_name = i::Handle<i::Name>::cast(key_obj);
3661 auto result = i::JSReceiver::GetPropertyAttributes(self, key_name);
3662 has_pending_exception = result.IsNothing();
3663 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(PropertyAttribute);
3664 if (result.FromJust() == i::ABSENT) {
3665 return Just(static_cast<PropertyAttribute>(i::NONE));
3666 }
3667 return Just(static_cast<PropertyAttribute>(result.FromJust()));
3668}
3669
3670
3671PropertyAttribute v8::Object::GetPropertyAttributes(v8::Local<Value> key) {
3672 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3673 return GetPropertyAttributes(context, key)
3674 .FromMaybe(static_cast<PropertyAttribute>(i::NONE));
3675}
3676
3677
3678MaybeLocal<Value> v8::Object::GetOwnPropertyDescriptor(Local<Context> context,
3679 Local<String> key) {
3680 PREPARE_FOR_EXECUTION(context, "v8::Object::GetOwnPropertyDescriptor()",
3681 Value);
3682 i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
3683 i::Handle<i::String> key_name = Utils::OpenHandle(*key);
3684
3685 i::PropertyDescriptor desc;
3686 Maybe<bool> found =
3687 i::JSReceiver::GetOwnPropertyDescriptor(isolate, obj, key_name, &desc);
3688 has_pending_exception = found.IsNothing();
3689 RETURN_ON_FAILED_EXECUTION(Value);
3690 if (!found.FromJust()) {
3691 return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
3692 }
3693 RETURN_ESCAPED(Utils::ToLocal(desc.ToObject(isolate)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003694}
3695
3696
3697Local<Value> v8::Object::GetOwnPropertyDescriptor(Local<String> key) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003698 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3699 RETURN_TO_LOCAL_UNCHECKED(GetOwnPropertyDescriptor(context, key), Value);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003700}
3701
3702
Steve Blocka7e24c12009-10-30 11:49:00 +00003703Local<Value> v8::Object::GetPrototype() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003704 auto isolate = Utils::OpenHandle(this)->GetIsolate();
3705 auto self = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003706 i::PrototypeIterator iter(isolate, self);
3707 return Utils::ToLocal(i::PrototypeIterator::GetCurrent(iter));
Steve Blocka7e24c12009-10-30 11:49:00 +00003708}
3709
3710
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003711Maybe<bool> v8::Object::SetPrototype(Local<Context> context,
3712 Local<Value> value) {
3713 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Object::SetPrototype()", bool);
3714 auto self = Utils::OpenHandle(this);
3715 auto value_obj = Utils::OpenHandle(*value);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003716 // We do not allow exceptions thrown while setting the prototype
3717 // to propagate outside.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003718 TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
3719 auto result = i::JSReceiver::SetPrototype(self, value_obj, false,
3720 i::Object::THROW_ON_ERROR);
3721 has_pending_exception = result.IsNothing();
3722 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3723 return Just(true);
3724}
3725
3726
3727bool v8::Object::SetPrototype(Local<Value> value) {
3728 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3729 return SetPrototype(context, value).FromMaybe(false);
Andrei Popescu402d9372010-02-26 13:31:12 +00003730}
3731
3732
Steve Blocka7e24c12009-10-30 11:49:00 +00003733Local<Object> v8::Object::FindInstanceInPrototypeChain(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003734 v8::Local<FunctionTemplate> tmpl) {
3735 auto isolate = Utils::OpenHandle(this)->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003736 i::PrototypeIterator iter(isolate, *Utils::OpenHandle(this),
3737 i::PrototypeIterator::START_AT_RECEIVER);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003738 auto tmpl_info = *Utils::OpenHandle(*tmpl);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003739 while (!tmpl_info->IsTemplateFor(iter.GetCurrent())) {
3740 iter.Advance();
3741 if (iter.IsAtEnd()) {
3742 return Local<Object>();
3743 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003744 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003745 // IsTemplateFor() ensures that iter.GetCurrent() can't be a Proxy here.
3746 return Utils::ToLocal(i::handle(iter.GetCurrent<i::JSObject>(), isolate));
3747}
3748
3749
3750MaybeLocal<Array> v8::Object::GetPropertyNames(Local<Context> context) {
3751 PREPARE_FOR_EXECUTION(context, "v8::Object::GetPropertyNames()", Array);
3752 auto self = Utils::OpenHandle(this);
3753 i::Handle<i::FixedArray> value;
3754 has_pending_exception =
3755 !i::JSReceiver::GetKeys(self, i::JSReceiver::INCLUDE_PROTOS,
3756 i::ENUMERABLE_STRINGS)
3757 .ToHandle(&value);
3758 RETURN_ON_FAILED_EXECUTION(Array);
3759 // Because we use caching to speed up enumeration it is important
3760 // to never change the result of the basic enumeration function so
3761 // we clone the result.
3762 auto elms = isolate->factory()->CopyFixedArray(value);
3763 auto result = isolate->factory()->NewJSArrayWithElements(elms);
3764 RETURN_ESCAPED(Utils::ToLocal(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00003765}
3766
3767
3768Local<Array> v8::Object::GetPropertyNames() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003769 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3770 RETURN_TO_LOCAL_UNCHECKED(GetPropertyNames(context), Array);
3771}
3772
3773
3774MaybeLocal<Array> v8::Object::GetOwnPropertyNames(Local<Context> context) {
3775 PREPARE_FOR_EXECUTION(context, "v8::Object::GetOwnPropertyNames()", Array);
3776 auto self = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003777 i::Handle<i::FixedArray> value;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003778 has_pending_exception = !i::JSReceiver::GetKeys(self, i::JSReceiver::OWN_ONLY,
3779 i::ENUMERABLE_STRINGS)
3780 .ToHandle(&value);
3781 RETURN_ON_FAILED_EXECUTION(Array);
Steve Blocka7e24c12009-10-30 11:49:00 +00003782 // Because we use caching to speed up enumeration it is important
3783 // to never change the result of the basic enumeration function so
3784 // we clone the result.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003785 auto elms = isolate->factory()->CopyFixedArray(value);
3786 auto result = isolate->factory()->NewJSArrayWithElements(elms);
3787 RETURN_ESCAPED(Utils::ToLocal(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00003788}
3789
3790
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003791Local<Array> v8::Object::GetOwnPropertyNames() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003792 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3793 RETURN_TO_LOCAL_UNCHECKED(GetOwnPropertyNames(context), Array);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003794}
3795
3796
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003797MaybeLocal<String> v8::Object::ObjectProtoToString(Local<Context> context) {
3798 PREPARE_FOR_EXECUTION(context, "v8::Object::ObjectProtoToString", String);
3799 auto obj = Utils::OpenHandle(this);
3800 Local<String> result;
3801 has_pending_exception =
3802 !ToLocal<String>(i::JSObject::ObjectProtoToString(isolate, obj), &result);
3803 RETURN_ON_FAILED_EXECUTION(String);
3804 RETURN_ESCAPED(result);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003805}
3806
3807
Steve Blocka7e24c12009-10-30 11:49:00 +00003808Local<String> v8::Object::ObjectProtoToString() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003809 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3810 RETURN_TO_LOCAL_UNCHECKED(ObjectProtoToString(context), String);
Steve Blocka7e24c12009-10-30 11:49:00 +00003811}
3812
3813
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003814Local<String> v8::Object::GetConstructorName() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003815 auto self = Utils::OpenHandle(this);
3816 i::Handle<i::String> name = i::JSReceiver::GetConstructorName(self);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003817 return Utils::ToLocal(name);
3818}
3819
3820
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003821Maybe<bool> v8::Object::Delete(Local<Context> context, Local<Value> key) {
3822 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Object::Delete()", bool);
3823 auto self = Utils::OpenHandle(this);
3824 auto key_obj = Utils::OpenHandle(*key);
3825 Maybe<bool> result =
3826 i::Runtime::DeleteObjectProperty(isolate, self, key_obj, i::SLOPPY);
3827 has_pending_exception = result.IsNothing();
3828 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3829 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00003830}
3831
3832
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003833bool v8::Object::Delete(v8::Local<Value> key) {
3834 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3835 return Delete(context, key).FromMaybe(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003836}
3837
3838
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003839Maybe<bool> v8::Object::DeletePrivate(Local<Context> context,
3840 Local<Private> key) {
3841 return Delete(context, Local<Value>(reinterpret_cast<Value*>(*key)));
3842}
3843
3844
3845Maybe<bool> v8::Object::Has(Local<Context> context, Local<Value> key) {
3846 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Object::Get()", bool);
3847 auto self = Utils::OpenHandle(this);
3848 auto key_obj = Utils::OpenHandle(*key);
3849 Maybe<bool> maybe = Nothing<bool>();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003850 // Check if the given key is an array index.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003851 uint32_t index = 0;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003852 if (key_obj->ToArrayIndex(&index)) {
3853 maybe = i::JSReceiver::HasElement(self, index);
3854 } else {
3855 // Convert the key to a name - possibly by calling back into JavaScript.
3856 i::Handle<i::Name> name;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003857 if (i::Object::ToName(isolate, key_obj).ToHandle(&name)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003858 maybe = i::JSReceiver::HasProperty(self, name);
3859 }
3860 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003861 has_pending_exception = maybe.IsNothing();
3862 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3863 return maybe;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003864}
3865
3866
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003867bool v8::Object::Has(v8::Local<Value> key) {
3868 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3869 return Has(context, key).FromMaybe(false);
3870}
3871
3872
3873Maybe<bool> v8::Object::HasPrivate(Local<Context> context, Local<Private> key) {
3874 return HasOwnProperty(context, Local<Name>(reinterpret_cast<Name*>(*key)));
3875}
3876
3877
3878Maybe<bool> v8::Object::Delete(Local<Context> context, uint32_t index) {
3879 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Object::DeleteProperty()",
3880 bool);
3881 auto self = Utils::OpenHandle(this);
3882 Maybe<bool> result = i::JSReceiver::DeleteElement(self, index);
3883 has_pending_exception = result.IsNothing();
3884 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3885 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00003886}
3887
3888
3889bool v8::Object::Delete(uint32_t index) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003890 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3891 return Delete(context, index).FromMaybe(false);
3892}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003893
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003894
3895Maybe<bool> v8::Object::Has(Local<Context> context, uint32_t index) {
3896 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Object::Get()", bool);
3897 auto self = Utils::OpenHandle(this);
3898 auto maybe = i::JSReceiver::HasElement(self, index);
3899 has_pending_exception = maybe.IsNothing();
3900 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3901 return maybe;
Steve Blocka7e24c12009-10-30 11:49:00 +00003902}
3903
3904
3905bool v8::Object::Has(uint32_t index) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003906 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3907 return Has(context, index).FromMaybe(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003908}
3909
3910
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003911template <typename Getter, typename Setter, typename Data>
3912static Maybe<bool> ObjectSetAccessor(Local<Context> context, Object* self,
3913 Local<Name> name, Getter getter,
3914 Setter setter, Data data,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003915 AccessControl settings,
3916 PropertyAttribute attributes) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003917 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Object::SetAccessor()", bool);
3918 if (!Utils::OpenHandle(self)->IsJSObject()) return Just(false);
3919 i::Handle<i::JSObject> obj =
3920 i::Handle<i::JSObject>::cast(Utils::OpenHandle(self));
3921 v8::Local<AccessorSignature> signature;
3922 auto info = MakeAccessorInfo(name, getter, setter, data, settings, attributes,
3923 signature);
3924 if (info.is_null()) return Nothing<bool>();
3925 bool fast = obj->HasFastProperties();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003926 i::Handle<i::Object> result;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003927 has_pending_exception =
3928 !i::JSObject::SetAccessor(obj, info).ToHandle(&result);
3929 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3930 if (result->IsUndefined()) return Nothing<bool>();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003931 if (fast) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003932 i::JSObject::MigrateSlowToFast(obj, 0, "APISetAccessor");
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003933 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003934 return Just(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00003935}
3936
3937
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003938Maybe<bool> Object::SetAccessor(Local<Context> context, Local<Name> name,
3939 AccessorNameGetterCallback getter,
3940 AccessorNameSetterCallback setter,
3941 MaybeLocal<Value> data, AccessControl settings,
3942 PropertyAttribute attribute) {
3943 return ObjectSetAccessor(context, this, name, getter, setter,
3944 data.FromMaybe(Local<Value>()), settings, attribute);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003945}
3946
3947
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003948bool Object::SetAccessor(Local<String> name, AccessorGetterCallback getter,
3949 AccessorSetterCallback setter, v8::Local<Value> data,
3950 AccessControl settings, PropertyAttribute attributes) {
3951 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3952 return ObjectSetAccessor(context, this, name, getter, setter, data, settings,
3953 attributes).FromMaybe(false);
3954}
3955
3956
3957bool Object::SetAccessor(Local<Name> name, AccessorNameGetterCallback getter,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003958 AccessorNameSetterCallback setter,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003959 v8::Local<Value> data, AccessControl settings,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003960 PropertyAttribute attributes) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003961 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3962 return ObjectSetAccessor(context, this, name, getter, setter, data, settings,
3963 attributes).FromMaybe(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003964}
3965
3966
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003967void Object::SetAccessorProperty(Local<Name> name, Local<Function> getter,
3968 Local<Function> setter,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003969 PropertyAttribute attribute,
3970 AccessControl settings) {
3971 // TODO(verwaest): Remove |settings|.
3972 DCHECK_EQ(v8::DEFAULT, settings);
Steve Block44f0eee2011-05-26 01:26:41 +01003973 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01003974 ENTER_V8(isolate);
3975 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003976 auto self = Utils::OpenHandle(this);
3977 if (!self->IsJSObject()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003978 i::Handle<i::Object> getter_i = v8::Utils::OpenHandle(*getter);
3979 i::Handle<i::Object> setter_i = v8::Utils::OpenHandle(*setter, true);
3980 if (setter_i.is_null()) setter_i = isolate->factory()->null_value();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003981 i::JSObject::DefineAccessor(i::Handle<i::JSObject>::cast(self),
3982 v8::Utils::OpenHandle(*name), getter_i, setter_i,
3983 static_cast<i::PropertyAttributes>(attribute));
Leon Clarkef7060e22010-06-03 12:02:55 +01003984}
3985
3986
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003987Maybe<bool> v8::Object::HasOwnProperty(Local<Context> context,
3988 Local<Name> key) {
3989 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Object::HasOwnProperty()",
3990 bool);
3991 auto self = Utils::OpenHandle(this);
3992 auto key_val = Utils::OpenHandle(*key);
3993 auto result = i::JSReceiver::HasOwnProperty(self, key_val);
3994 has_pending_exception = result.IsNothing();
3995 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3996 return result;
Ben Murdoch257744e2011-11-30 15:57:28 +00003997}
3998
3999
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004000bool v8::Object::HasOwnProperty(Local<String> key) {
4001 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4002 return HasOwnProperty(context, key).FromMaybe(false);
4003}
4004
4005
4006Maybe<bool> v8::Object::HasRealNamedProperty(Local<Context> context,
4007 Local<Name> key) {
4008 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Object::HasRealNamedProperty()",
4009 bool);
4010 auto self = Utils::OpenHandle(this);
4011 if (!self->IsJSObject()) return Just(false);
4012 auto key_val = Utils::OpenHandle(*key);
4013 auto result = i::JSObject::HasRealNamedProperty(
4014 i::Handle<i::JSObject>::cast(self), key_val);
4015 has_pending_exception = result.IsNothing();
4016 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4017 return result;
4018}
4019
4020
4021bool v8::Object::HasRealNamedProperty(Local<String> key) {
4022 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4023 return HasRealNamedProperty(context, key).FromMaybe(false);
4024}
4025
4026
4027Maybe<bool> v8::Object::HasRealIndexedProperty(Local<Context> context,
4028 uint32_t index) {
4029 PREPARE_FOR_EXECUTION_PRIMITIVE(context,
4030 "v8::Object::HasRealIndexedProperty()", bool);
4031 auto self = Utils::OpenHandle(this);
4032 if (!self->IsJSObject()) return Just(false);
4033 auto result = i::JSObject::HasRealElementProperty(
4034 i::Handle<i::JSObject>::cast(self), index);
4035 has_pending_exception = result.IsNothing();
4036 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4037 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00004038}
4039
4040
4041bool v8::Object::HasRealIndexedProperty(uint32_t index) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004042 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4043 return HasRealIndexedProperty(context, index).FromMaybe(false);
Steve Blocka7e24c12009-10-30 11:49:00 +00004044}
4045
4046
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004047Maybe<bool> v8::Object::HasRealNamedCallbackProperty(Local<Context> context,
4048 Local<Name> key) {
4049 PREPARE_FOR_EXECUTION_PRIMITIVE(
4050 context, "v8::Object::HasRealNamedCallbackProperty()", bool);
4051 auto self = Utils::OpenHandle(this);
4052 if (!self->IsJSObject()) return Just(false);
4053 auto key_val = Utils::OpenHandle(*key);
4054 auto result = i::JSObject::HasRealNamedCallbackProperty(
4055 i::Handle<i::JSObject>::cast(self), key_val);
4056 has_pending_exception = result.IsNothing();
4057 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4058 return result;
4059}
4060
4061
4062bool v8::Object::HasRealNamedCallbackProperty(Local<String> key) {
4063 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4064 return HasRealNamedCallbackProperty(context, key).FromMaybe(false);
Steve Blocka7e24c12009-10-30 11:49:00 +00004065}
4066
4067
4068bool v8::Object::HasNamedLookupInterceptor() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004069 auto self = Utils::OpenHandle(this);
4070 return self->IsJSObject() &&
4071 i::Handle<i::JSObject>::cast(self)->HasNamedInterceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00004072}
4073
4074
4075bool v8::Object::HasIndexedLookupInterceptor() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004076 auto self = Utils::OpenHandle(this);
4077 return self->IsJSObject() &&
4078 i::Handle<i::JSObject>::cast(self)->HasIndexedInterceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00004079}
4080
4081
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004082MaybeLocal<Value> v8::Object::GetRealNamedPropertyInPrototypeChain(
4083 Local<Context> context, Local<Name> key) {
4084 PREPARE_FOR_EXECUTION(
4085 context, "v8::Object::GetRealNamedPropertyInPrototypeChain()", Value);
4086 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4087 if (!self->IsJSObject()) return MaybeLocal<Value>();
4088 i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
4089 i::PrototypeIterator iter(isolate, self);
4090 if (iter.IsAtEnd()) return MaybeLocal<Value>();
4091 i::Handle<i::JSReceiver> proto =
4092 i::PrototypeIterator::GetCurrent<i::JSReceiver>(iter);
4093 i::LookupIterator it = i::LookupIterator::PropertyOrElement(
4094 isolate, self, key_obj, proto,
4095 i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
4096 Local<Value> result;
4097 has_pending_exception = !ToLocal<Value>(i::Object::GetProperty(&it), &result);
4098 RETURN_ON_FAILED_EXECUTION(Value);
4099 if (!it.IsFound()) return MaybeLocal<Value>();
4100 RETURN_ESCAPED(result);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004101}
4102
4103
Steve Blocka7e24c12009-10-30 11:49:00 +00004104Local<Value> v8::Object::GetRealNamedPropertyInPrototypeChain(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004105 Local<String> key) {
4106 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4107 RETURN_TO_LOCAL_UNCHECKED(GetRealNamedPropertyInPrototypeChain(context, key),
4108 Value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004109}
4110
4111
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004112Maybe<PropertyAttribute>
4113v8::Object::GetRealNamedPropertyAttributesInPrototypeChain(
4114 Local<Context> context, Local<Name> key) {
4115 PREPARE_FOR_EXECUTION_PRIMITIVE(
4116 context, "v8::Object::GetRealNamedPropertyAttributesInPrototypeChain()",
4117 PropertyAttribute);
4118 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4119 if (!self->IsJSObject()) return Nothing<PropertyAttribute>();
4120 i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
4121 i::PrototypeIterator iter(isolate, self);
4122 if (iter.IsAtEnd()) return Nothing<PropertyAttribute>();
4123 i::Handle<i::JSReceiver> proto =
4124 i::PrototypeIterator::GetCurrent<i::JSReceiver>(iter);
4125 i::LookupIterator it = i::LookupIterator::PropertyOrElement(
4126 isolate, self, key_obj, proto,
4127 i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
4128 Maybe<i::PropertyAttributes> result =
4129 i::JSReceiver::GetPropertyAttributes(&it);
4130 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(PropertyAttribute);
4131 if (!it.IsFound()) return Nothing<PropertyAttribute>();
4132 if (result.FromJust() == i::ABSENT) return Just(None);
4133 return Just(static_cast<PropertyAttribute>(result.FromJust()));
Steve Blocka7e24c12009-10-30 11:49:00 +00004134}
4135
4136
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004137Maybe<PropertyAttribute>
4138v8::Object::GetRealNamedPropertyAttributesInPrototypeChain(Local<String> key) {
4139 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4140 return GetRealNamedPropertyAttributesInPrototypeChain(context, key);
4141}
Steve Blocka7e24c12009-10-30 11:49:00 +00004142
Ben Murdochb0fe1622011-05-05 13:52:32 +01004143
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004144MaybeLocal<Value> v8::Object::GetRealNamedProperty(Local<Context> context,
4145 Local<Name> key) {
4146 PREPARE_FOR_EXECUTION(context, "v8::Object::GetRealNamedProperty()", Value);
4147 auto self = Utils::OpenHandle(this);
4148 auto key_obj = Utils::OpenHandle(*key);
4149 i::LookupIterator it = i::LookupIterator::PropertyOrElement(
4150 isolate, self, key_obj,
4151 i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
4152 Local<Value> result;
4153 has_pending_exception = !ToLocal<Value>(i::Object::GetProperty(&it), &result);
4154 RETURN_ON_FAILED_EXECUTION(Value);
4155 if (!it.IsFound()) return MaybeLocal<Value>();
4156 RETURN_ESCAPED(result);
4157}
4158
4159
4160Local<Value> v8::Object::GetRealNamedProperty(Local<String> key) {
4161 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4162 RETURN_TO_LOCAL_UNCHECKED(GetRealNamedProperty(context, key), Value);
4163}
4164
4165
4166Maybe<PropertyAttribute> v8::Object::GetRealNamedPropertyAttributes(
4167 Local<Context> context, Local<Name> key) {
4168 PREPARE_FOR_EXECUTION_PRIMITIVE(
4169 context, "v8::Object::GetRealNamedPropertyAttributes()",
4170 PropertyAttribute);
4171 auto self = Utils::OpenHandle(this);
4172 auto key_obj = Utils::OpenHandle(*key);
4173 i::LookupIterator it = i::LookupIterator::PropertyOrElement(
4174 isolate, self, key_obj,
4175 i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
4176 auto result = i::JSReceiver::GetPropertyAttributes(&it);
4177 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(PropertyAttribute);
4178 if (!it.IsFound()) return Nothing<PropertyAttribute>();
4179 if (result.FromJust() == i::ABSENT) {
4180 return Just(static_cast<PropertyAttribute>(i::NONE));
4181 }
4182 return Just<PropertyAttribute>(
4183 static_cast<PropertyAttribute>(result.FromJust()));
4184}
4185
4186
4187Maybe<PropertyAttribute> v8::Object::GetRealNamedPropertyAttributes(
4188 Local<String> key) {
4189 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4190 return GetRealNamedPropertyAttributes(context, key);
Steve Blocka7e24c12009-10-30 11:49:00 +00004191}
4192
4193
Steve Blocka7e24c12009-10-30 11:49:00 +00004194Local<v8::Object> v8::Object::Clone() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004195 auto self = i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
4196 auto isolate = self->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01004197 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004198 auto result = isolate->factory()->CopyJSObject(self);
4199 CHECK(!result.is_null());
Steve Blocka7e24c12009-10-30 11:49:00 +00004200 return Utils::ToLocal(result);
4201}
4202
4203
Ben Murdoch8b112d22011-06-08 16:22:53 +01004204Local<v8::Context> v8::Object::CreationContext() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004205 auto self = Utils::OpenHandle(this);
4206 auto context = handle(self->GetCreationContext());
4207 return Utils::ToLocal(context);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004208}
4209
4210
Steve Blocka7e24c12009-10-30 11:49:00 +00004211int v8::Object::GetIdentityHash() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004212 auto isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01004213 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004214 auto self = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004215 return i::JSReceiver::GetOrCreateIdentityHash(self)->value();
Steve Blocka7e24c12009-10-30 11:49:00 +00004216}
4217
4218
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004219bool v8::Object::SetHiddenValue(v8::Local<v8::String> key,
4220 v8::Local<v8::Value> value) {
Steve Block44f0eee2011-05-26 01:26:41 +01004221 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01004222 ENTER_V8(isolate);
4223 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004224 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4225 if (!self->IsJSObject()) return false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004226 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004227 i::Handle<i::String> key_string =
4228 isolate->factory()->InternalizeString(key_obj);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004229 if (value.IsEmpty()) {
4230 i::JSObject::DeleteHiddenProperty(i::Handle<i::JSObject>::cast(self),
4231 key_string);
4232 return true;
4233 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004234 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004235 i::Handle<i::Object> result = i::JSObject::SetHiddenProperty(
4236 i::Handle<i::JSObject>::cast(self), key_string, value_obj);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004237 return *result == *self;
Steve Blocka7e24c12009-10-30 11:49:00 +00004238}
4239
4240
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004241v8::Local<v8::Value> v8::Object::GetHiddenValue(v8::Local<v8::String> key) {
Steve Block44f0eee2011-05-26 01:26:41 +01004242 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01004243 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004244 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4245 if (!self->IsJSObject()) return v8::Local<v8::Value>();
Steve Blocka7e24c12009-10-30 11:49:00 +00004246 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004247 i::Handle<i::String> key_string =
4248 isolate->factory()->InternalizeString(key_obj);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004249 i::Handle<i::Object> result(
4250 i::Handle<i::JSObject>::cast(self)->GetHiddenProperty(key_string),
4251 isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004252 if (result->IsTheHole()) return v8::Local<v8::Value>();
Steve Blocka7e24c12009-10-30 11:49:00 +00004253 return Utils::ToLocal(result);
4254}
4255
4256
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004257bool v8::Object::DeleteHiddenValue(v8::Local<v8::String> key) {
Steve Block44f0eee2011-05-26 01:26:41 +01004258 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01004259 ENTER_V8(isolate);
4260 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004261 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4262 if (!self->IsJSObject()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00004263 i::Handle<i::String> key_obj = Utils::OpenHandle(*key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004264 i::Handle<i::String> key_string =
4265 isolate->factory()->InternalizeString(key_obj);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004266 i::JSObject::DeleteHiddenProperty(i::Handle<i::JSObject>::cast(self),
4267 key_string);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004268 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00004269}
4270
4271
Ben Murdoch257744e2011-11-30 15:57:28 +00004272bool v8::Object::IsCallable() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004273 auto self = Utils::OpenHandle(this);
4274 return self->IsCallable();
Ben Murdoch257744e2011-11-30 15:57:28 +00004275}
4276
4277
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004278MaybeLocal<Value> Object::CallAsFunction(Local<Context> context,
4279 Local<Value> recv, int argc,
4280 Local<Value> argv[]) {
4281 PREPARE_FOR_EXECUTION_WITH_CALLBACK(context, "v8::Object::CallAsFunction()",
4282 Value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004283 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004284 auto self = Utils::OpenHandle(this);
4285 auto recv_obj = Utils::OpenHandle(*recv);
4286 STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Object**));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004287 i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004288 Local<Value> result;
4289 has_pending_exception = !ToLocal<Value>(
4290 i::Execution::Call(isolate, self, recv_obj, argc, args), &result);
4291 RETURN_ON_FAILED_EXECUTION(Value);
4292 RETURN_ESCAPED(result);
4293}
4294
4295
4296Local<v8::Value> Object::CallAsFunction(v8::Local<v8::Value> recv, int argc,
4297 v8::Local<v8::Value> argv[]) {
4298 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4299 Local<Value>* argv_cast = reinterpret_cast<Local<Value>*>(argv);
4300 RETURN_TO_LOCAL_UNCHECKED(CallAsFunction(context, recv, argc, argv_cast),
4301 Value);
4302}
4303
4304
4305MaybeLocal<Value> Object::CallAsConstructor(Local<Context> context, int argc,
4306 Local<Value> argv[]) {
4307 PREPARE_FOR_EXECUTION_WITH_CALLBACK(context,
4308 "v8::Object::CallAsConstructor()", Value);
4309 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
4310 auto self = Utils::OpenHandle(this);
4311 STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Object**));
4312 i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
4313 Local<Value> result;
4314 has_pending_exception = !ToLocal<Value>(
4315 i::Execution::New(isolate, self, self, argc, args), &result);
4316 RETURN_ON_FAILED_EXECUTION(Value);
4317 RETURN_ESCAPED(result);
Ben Murdoch257744e2011-11-30 15:57:28 +00004318}
4319
4320
4321Local<v8::Value> Object::CallAsConstructor(int argc,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004322 v8::Local<v8::Value> argv[]) {
4323 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4324 Local<Value>* argv_cast = reinterpret_cast<Local<Value>*>(argv);
4325 RETURN_TO_LOCAL_UNCHECKED(CallAsConstructor(context, argc, argv_cast), Value);
Ben Murdoch257744e2011-11-30 15:57:28 +00004326}
4327
4328
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004329MaybeLocal<Function> Function::New(Local<Context> context,
4330 FunctionCallback callback, Local<Value> data,
4331 int length) {
4332 i::Isolate* isolate = Utils::OpenHandle(*context)->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004333 LOG_API(isolate, "Function::New");
4334 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004335 return FunctionTemplateNew(isolate, callback, nullptr, data,
4336 Local<Signature>(), length, true)
4337 ->GetFunction(context);
4338}
4339
4340
4341Local<Function> Function::New(Isolate* v8_isolate, FunctionCallback callback,
4342 Local<Value> data, int length) {
4343 return Function::New(v8_isolate->GetCurrentContext(), callback, data, length)
4344 .FromMaybe(Local<Function>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004345}
4346
4347
Steve Blocka7e24c12009-10-30 11:49:00 +00004348Local<v8::Object> Function::NewInstance() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004349 return NewInstance(Isolate::GetCurrent()->GetCurrentContext(), 0, NULL)
4350 .FromMaybe(Local<Object>());
4351}
4352
4353
4354MaybeLocal<Object> Function::NewInstance(Local<Context> context, int argc,
4355 v8::Local<v8::Value> argv[]) const {
4356 PREPARE_FOR_EXECUTION_WITH_CALLBACK(context, "v8::Function::NewInstance()",
4357 Object);
4358 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
4359 auto self = Utils::OpenHandle(this);
4360 STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Object**));
4361 i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
4362 Local<Object> result;
4363 has_pending_exception = !ToLocal<Object>(
4364 i::Execution::New(isolate, self, self, argc, args), &result);
4365 RETURN_ON_FAILED_EXECUTION(Object);
4366 RETURN_ESCAPED(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00004367}
4368
4369
4370Local<v8::Object> Function::NewInstance(int argc,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004371 v8::Local<v8::Value> argv[]) const {
4372 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4373 RETURN_TO_LOCAL_UNCHECKED(NewInstance(context, argc, argv), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +00004374}
4375
4376
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004377MaybeLocal<v8::Value> Function::Call(Local<Context> context,
4378 v8::Local<v8::Value> recv, int argc,
4379 v8::Local<v8::Value> argv[]) {
4380 PREPARE_FOR_EXECUTION_WITH_CALLBACK(context, "v8::Function::Call()", Value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004381 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004382 auto self = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004383 i::Handle<i::Object> recv_obj = Utils::OpenHandle(*recv);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004384 STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Object**));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004385 i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004386 Local<Value> result;
4387 has_pending_exception = !ToLocal<Value>(
4388 i::Execution::Call(isolate, self, recv_obj, argc, args), &result);
4389 RETURN_ON_FAILED_EXECUTION(Value);
4390 RETURN_ESCAPED(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00004391}
4392
4393
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004394Local<v8::Value> Function::Call(v8::Local<v8::Value> recv, int argc,
4395 v8::Local<v8::Value> argv[]) {
4396 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4397 RETURN_TO_LOCAL_UNCHECKED(Call(context, recv, argc, argv), Value);
4398}
4399
4400
4401void Function::SetName(v8::Local<v8::String> name) {
4402 auto self = Utils::OpenHandle(this);
4403 if (!self->IsJSFunction()) return;
4404 auto func = i::Handle<i::JSFunction>::cast(self);
Steve Blocka7e24c12009-10-30 11:49:00 +00004405 func->shared()->set_name(*Utils::OpenHandle(*name));
4406}
4407
4408
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004409Local<Value> Function::GetName() const {
4410 auto self = Utils::OpenHandle(this);
4411 if (self->IsJSBoundFunction()) {
4412 auto func = i::Handle<i::JSBoundFunction>::cast(self);
4413 return Utils::ToLocal(handle(func->name(), func->GetIsolate()));
4414 }
4415 if (self->IsJSFunction()) {
4416 auto func = i::Handle<i::JSFunction>::cast(self);
4417 return Utils::ToLocal(handle(func->shared()->name(), func->GetIsolate()));
4418 }
4419 return ToApiHandle<Primitive>(
4420 self->GetIsolate()->factory()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00004421}
4422
4423
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004424Local<Value> Function::GetInferredName() const {
4425 auto self = Utils::OpenHandle(this);
4426 if (!self->IsJSFunction()) {
4427 return ToApiHandle<Primitive>(
4428 self->GetIsolate()->factory()->undefined_value());
4429 }
4430 auto func = i::Handle<i::JSFunction>::cast(self);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004431 return Utils::ToLocal(i::Handle<i::Object>(func->shared()->inferred_name(),
4432 func->GetIsolate()));
4433}
4434
4435
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004436Local<Value> Function::GetDebugName() const {
4437 auto self = Utils::OpenHandle(this);
4438 if (!self->IsJSFunction()) {
4439 return ToApiHandle<Primitive>(
4440 self->GetIsolate()->factory()->undefined_value());
4441 }
4442 auto func = i::Handle<i::JSFunction>::cast(self);
4443 i::Handle<i::String> name = i::JSFunction::GetDebugName(func);
4444 return Utils::ToLocal(i::Handle<i::Object>(*name, name->GetIsolate()));
4445}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004446
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004447
4448Local<Value> Function::GetDisplayName() const {
4449 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4450 ENTER_V8(isolate);
4451 auto self = Utils::OpenHandle(this);
4452 if (!self->IsJSFunction()) {
4453 return ToApiHandle<Primitive>(isolate->factory()->undefined_value());
4454 }
4455 auto func = i::Handle<i::JSFunction>::cast(self);
4456 i::Handle<i::String> property_name =
4457 isolate->factory()->NewStringFromStaticChars("displayName");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004458 i::Handle<i::Object> value =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004459 i::JSReceiver::GetDataProperty(func, property_name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004460 if (value->IsString()) {
4461 i::Handle<i::String> name = i::Handle<i::String>::cast(value);
4462 if (name->length() > 0) return Utils::ToLocal(name);
4463 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004464 return ToApiHandle<Primitive>(isolate->factory()->undefined_value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004465}
4466
4467
Andrei Popescu402d9372010-02-26 13:31:12 +00004468ScriptOrigin Function::GetScriptOrigin() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004469 auto self = Utils::OpenHandle(this);
4470 if (!self->IsJSFunction()) {
4471 return v8::ScriptOrigin(Local<Value>());
4472 }
4473 auto func = i::Handle<i::JSFunction>::cast(self);
Andrei Popescu402d9372010-02-26 13:31:12 +00004474 if (func->shared()->script()->IsScript()) {
4475 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004476 return GetScriptOriginForScript(func->GetIsolate(), script);
Andrei Popescu402d9372010-02-26 13:31:12 +00004477 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004478 return v8::ScriptOrigin(Local<Value>());
Andrei Popescu402d9372010-02-26 13:31:12 +00004479}
4480
4481
4482const int Function::kLineOffsetNotFound = -1;
4483
4484
4485int Function::GetScriptLineNumber() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004486 auto self = Utils::OpenHandle(this);
4487 if (!self->IsJSFunction()) {
4488 return kLineOffsetNotFound;
4489 }
4490 auto func = i::Handle<i::JSFunction>::cast(self);
Andrei Popescu402d9372010-02-26 13:31:12 +00004491 if (func->shared()->script()->IsScript()) {
4492 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004493 return i::Script::GetLineNumber(script, func->shared()->start_position());
Andrei Popescu402d9372010-02-26 13:31:12 +00004494 }
4495 return kLineOffsetNotFound;
4496}
4497
4498
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004499int Function::GetScriptColumnNumber() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004500 auto self = Utils::OpenHandle(this);
4501 if (!self->IsJSFunction()) {
4502 return kLineOffsetNotFound;
4503 }
4504 auto func = i::Handle<i::JSFunction>::cast(self);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004505 if (func->shared()->script()->IsScript()) {
4506 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004507 return i::Script::GetColumnNumber(script, func->shared()->start_position());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004508 }
4509 return kLineOffsetNotFound;
4510}
4511
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004512
4513bool Function::IsBuiltin() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004514 auto self = Utils::OpenHandle(this);
4515 if (!self->IsJSFunction()) {
4516 return false;
4517 }
4518 auto func = i::Handle<i::JSFunction>::cast(self);
4519 return func->shared()->IsBuiltin();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004520}
4521
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004522
4523int Function::ScriptId() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004524 auto self = Utils::OpenHandle(this);
4525 if (!self->IsJSFunction()) {
4526 return v8::UnboundScript::kNoScriptId;
4527 }
4528 auto func = i::Handle<i::JSFunction>::cast(self);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004529 if (!func->shared()->script()->IsScript()) {
4530 return v8::UnboundScript::kNoScriptId;
4531 }
4532 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004533 return script->id();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004534}
4535
4536
4537Local<v8::Value> Function::GetBoundFunction() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004538 auto self = Utils::OpenHandle(this);
4539 if (self->IsJSBoundFunction()) {
4540 auto bound_function = i::Handle<i::JSBoundFunction>::cast(self);
4541 auto bound_target_function = i::handle(
4542 bound_function->bound_target_function(), bound_function->GetIsolate());
4543 return Utils::CallableToLocal(bound_target_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004544 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004545 return v8::Undefined(reinterpret_cast<v8::Isolate*>(self->GetIsolate()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004546}
4547
4548
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004549int Name::GetIdentityHash() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004550 auto self = Utils::OpenHandle(this);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004551 return static_cast<int>(self->Hash());
4552}
4553
4554
Steve Blocka7e24c12009-10-30 11:49:00 +00004555int String::Length() const {
Steve Block44f0eee2011-05-26 01:26:41 +01004556 i::Handle<i::String> str = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01004557 return str->length();
Steve Blocka7e24c12009-10-30 11:49:00 +00004558}
4559
4560
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004561bool String::IsOneByte() const {
4562 i::Handle<i::String> str = Utils::OpenHandle(this);
4563 return str->HasOnlyOneByteChars();
4564}
4565
4566
4567// Helpers for ContainsOnlyOneByteHelper
4568template<size_t size> struct OneByteMask;
4569template<> struct OneByteMask<4> {
4570 static const uint32_t value = 0xFF00FF00;
4571};
4572template<> struct OneByteMask<8> {
4573 static const uint64_t value = V8_2PART_UINT64_C(0xFF00FF00, FF00FF00);
4574};
4575static const uintptr_t kOneByteMask = OneByteMask<sizeof(uintptr_t)>::value;
4576static const uintptr_t kAlignmentMask = sizeof(uintptr_t) - 1;
4577static inline bool Unaligned(const uint16_t* chars) {
4578 return reinterpret_cast<const uintptr_t>(chars) & kAlignmentMask;
4579}
4580
4581
4582static inline const uint16_t* Align(const uint16_t* chars) {
4583 return reinterpret_cast<uint16_t*>(
4584 reinterpret_cast<uintptr_t>(chars) & ~kAlignmentMask);
4585}
4586
4587class ContainsOnlyOneByteHelper {
4588 public:
4589 ContainsOnlyOneByteHelper() : is_one_byte_(true) {}
4590 bool Check(i::String* string) {
4591 i::ConsString* cons_string = i::String::VisitFlat(this, string, 0);
4592 if (cons_string == NULL) return is_one_byte_;
4593 return CheckCons(cons_string);
4594 }
4595 void VisitOneByteString(const uint8_t* chars, int length) {
4596 // Nothing to do.
4597 }
4598 void VisitTwoByteString(const uint16_t* chars, int length) {
4599 // Accumulated bits.
4600 uintptr_t acc = 0;
4601 // Align to uintptr_t.
4602 const uint16_t* end = chars + length;
4603 while (Unaligned(chars) && chars != end) {
4604 acc |= *chars++;
4605 }
4606 // Read word aligned in blocks,
4607 // checking the return value at the end of each block.
4608 const uint16_t* aligned_end = Align(end);
4609 const int increment = sizeof(uintptr_t)/sizeof(uint16_t);
4610 const int inner_loops = 16;
4611 while (chars + inner_loops*increment < aligned_end) {
4612 for (int i = 0; i < inner_loops; i++) {
4613 acc |= *reinterpret_cast<const uintptr_t*>(chars);
4614 chars += increment;
4615 }
4616 // Check for early return.
4617 if ((acc & kOneByteMask) != 0) {
4618 is_one_byte_ = false;
4619 return;
4620 }
4621 }
4622 // Read the rest.
4623 while (chars != end) {
4624 acc |= *chars++;
4625 }
4626 // Check result.
4627 if ((acc & kOneByteMask) != 0) is_one_byte_ = false;
4628 }
4629
4630 private:
4631 bool CheckCons(i::ConsString* cons_string) {
4632 while (true) {
4633 // Check left side if flat.
4634 i::String* left = cons_string->first();
4635 i::ConsString* left_as_cons =
4636 i::String::VisitFlat(this, left, 0);
4637 if (!is_one_byte_) return false;
4638 // Check right side if flat.
4639 i::String* right = cons_string->second();
4640 i::ConsString* right_as_cons =
4641 i::String::VisitFlat(this, right, 0);
4642 if (!is_one_byte_) return false;
4643 // Standard recurse/iterate trick.
4644 if (left_as_cons != NULL && right_as_cons != NULL) {
4645 if (left->length() < right->length()) {
4646 CheckCons(left_as_cons);
4647 cons_string = right_as_cons;
4648 } else {
4649 CheckCons(right_as_cons);
4650 cons_string = left_as_cons;
4651 }
4652 // Check fast return.
4653 if (!is_one_byte_) return false;
4654 continue;
4655 }
4656 // Descend left in place.
4657 if (left_as_cons != NULL) {
4658 cons_string = left_as_cons;
4659 continue;
4660 }
4661 // Descend right in place.
4662 if (right_as_cons != NULL) {
4663 cons_string = right_as_cons;
4664 continue;
4665 }
4666 // Terminate.
4667 break;
4668 }
4669 return is_one_byte_;
4670 }
4671 bool is_one_byte_;
4672 DISALLOW_COPY_AND_ASSIGN(ContainsOnlyOneByteHelper);
4673};
4674
4675
4676bool String::ContainsOnlyOneByte() const {
4677 i::Handle<i::String> str = Utils::OpenHandle(this);
4678 if (str->HasOnlyOneByteChars()) return true;
4679 ContainsOnlyOneByteHelper helper;
4680 return helper.Check(*str);
4681}
4682
4683
4684class Utf8LengthHelper : public i::AllStatic {
4685 public:
4686 enum State {
4687 kEndsWithLeadingSurrogate = 1 << 0,
4688 kStartsWithTrailingSurrogate = 1 << 1,
4689 kLeftmostEdgeIsCalculated = 1 << 2,
4690 kRightmostEdgeIsCalculated = 1 << 3,
4691 kLeftmostEdgeIsSurrogate = 1 << 4,
4692 kRightmostEdgeIsSurrogate = 1 << 5
4693 };
4694
4695 static const uint8_t kInitialState = 0;
4696
4697 static inline bool EndsWithSurrogate(uint8_t state) {
4698 return state & kEndsWithLeadingSurrogate;
4699 }
4700
4701 static inline bool StartsWithSurrogate(uint8_t state) {
4702 return state & kStartsWithTrailingSurrogate;
4703 }
4704
4705 class Visitor {
4706 public:
4707 Visitor() : utf8_length_(0), state_(kInitialState) {}
4708
4709 void VisitOneByteString(const uint8_t* chars, int length) {
4710 int utf8_length = 0;
4711 // Add in length 1 for each non-Latin1 character.
4712 for (int i = 0; i < length; i++) {
4713 utf8_length += *chars++ >> 7;
4714 }
4715 // Add in length 1 for each character.
4716 utf8_length_ = utf8_length + length;
4717 state_ = kInitialState;
4718 }
4719
4720 void VisitTwoByteString(const uint16_t* chars, int length) {
4721 int utf8_length = 0;
4722 int last_character = unibrow::Utf16::kNoPreviousCharacter;
4723 for (int i = 0; i < length; i++) {
4724 uint16_t c = chars[i];
4725 utf8_length += unibrow::Utf8::Length(c, last_character);
4726 last_character = c;
4727 }
4728 utf8_length_ = utf8_length;
4729 uint8_t state = 0;
4730 if (unibrow::Utf16::IsTrailSurrogate(chars[0])) {
4731 state |= kStartsWithTrailingSurrogate;
4732 }
4733 if (unibrow::Utf16::IsLeadSurrogate(chars[length-1])) {
4734 state |= kEndsWithLeadingSurrogate;
4735 }
4736 state_ = state;
4737 }
4738
4739 static i::ConsString* VisitFlat(i::String* string,
4740 int* length,
4741 uint8_t* state) {
4742 Visitor visitor;
4743 i::ConsString* cons_string = i::String::VisitFlat(&visitor, string);
4744 *length = visitor.utf8_length_;
4745 *state = visitor.state_;
4746 return cons_string;
4747 }
4748
4749 private:
4750 int utf8_length_;
4751 uint8_t state_;
4752 DISALLOW_COPY_AND_ASSIGN(Visitor);
4753 };
4754
4755 static inline void MergeLeafLeft(int* length,
4756 uint8_t* state,
4757 uint8_t leaf_state) {
4758 bool edge_surrogate = StartsWithSurrogate(leaf_state);
4759 if (!(*state & kLeftmostEdgeIsCalculated)) {
4760 DCHECK(!(*state & kLeftmostEdgeIsSurrogate));
4761 *state |= kLeftmostEdgeIsCalculated
4762 | (edge_surrogate ? kLeftmostEdgeIsSurrogate : 0);
4763 } else if (EndsWithSurrogate(*state) && edge_surrogate) {
4764 *length -= unibrow::Utf8::kBytesSavedByCombiningSurrogates;
4765 }
4766 if (EndsWithSurrogate(leaf_state)) {
4767 *state |= kEndsWithLeadingSurrogate;
4768 } else {
4769 *state &= ~kEndsWithLeadingSurrogate;
4770 }
4771 }
4772
4773 static inline void MergeLeafRight(int* length,
4774 uint8_t* state,
4775 uint8_t leaf_state) {
4776 bool edge_surrogate = EndsWithSurrogate(leaf_state);
4777 if (!(*state & kRightmostEdgeIsCalculated)) {
4778 DCHECK(!(*state & kRightmostEdgeIsSurrogate));
4779 *state |= (kRightmostEdgeIsCalculated
4780 | (edge_surrogate ? kRightmostEdgeIsSurrogate : 0));
4781 } else if (edge_surrogate && StartsWithSurrogate(*state)) {
4782 *length -= unibrow::Utf8::kBytesSavedByCombiningSurrogates;
4783 }
4784 if (StartsWithSurrogate(leaf_state)) {
4785 *state |= kStartsWithTrailingSurrogate;
4786 } else {
4787 *state &= ~kStartsWithTrailingSurrogate;
4788 }
4789 }
4790
4791 static inline void MergeTerminal(int* length,
4792 uint8_t state,
4793 uint8_t* state_out) {
4794 DCHECK((state & kLeftmostEdgeIsCalculated) &&
4795 (state & kRightmostEdgeIsCalculated));
4796 if (EndsWithSurrogate(state) && StartsWithSurrogate(state)) {
4797 *length -= unibrow::Utf8::kBytesSavedByCombiningSurrogates;
4798 }
4799 *state_out = kInitialState |
4800 (state & kLeftmostEdgeIsSurrogate ? kStartsWithTrailingSurrogate : 0) |
4801 (state & kRightmostEdgeIsSurrogate ? kEndsWithLeadingSurrogate : 0);
4802 }
4803
4804 static int Calculate(i::ConsString* current, uint8_t* state_out) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004805 using internal::ConsString;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004806 int total_length = 0;
4807 uint8_t state = kInitialState;
4808 while (true) {
4809 i::String* left = current->first();
4810 i::String* right = current->second();
4811 uint8_t right_leaf_state;
4812 uint8_t left_leaf_state;
4813 int leaf_length;
4814 ConsString* left_as_cons =
4815 Visitor::VisitFlat(left, &leaf_length, &left_leaf_state);
4816 if (left_as_cons == NULL) {
4817 total_length += leaf_length;
4818 MergeLeafLeft(&total_length, &state, left_leaf_state);
4819 }
4820 ConsString* right_as_cons =
4821 Visitor::VisitFlat(right, &leaf_length, &right_leaf_state);
4822 if (right_as_cons == NULL) {
4823 total_length += leaf_length;
4824 MergeLeafRight(&total_length, &state, right_leaf_state);
4825 if (left_as_cons != NULL) {
4826 // 1 Leaf node. Descend in place.
4827 current = left_as_cons;
4828 continue;
4829 } else {
4830 // Terminal node.
4831 MergeTerminal(&total_length, state, state_out);
4832 return total_length;
4833 }
4834 } else if (left_as_cons == NULL) {
4835 // 1 Leaf node. Descend in place.
4836 current = right_as_cons;
4837 continue;
4838 }
4839 // Both strings are ConsStrings.
4840 // Recurse on smallest.
4841 if (left->length() < right->length()) {
4842 total_length += Calculate(left_as_cons, &left_leaf_state);
4843 MergeLeafLeft(&total_length, &state, left_leaf_state);
4844 current = right_as_cons;
4845 } else {
4846 total_length += Calculate(right_as_cons, &right_leaf_state);
4847 MergeLeafRight(&total_length, &state, right_leaf_state);
4848 current = left_as_cons;
4849 }
4850 }
4851 UNREACHABLE();
4852 return 0;
4853 }
4854
4855 static inline int Calculate(i::ConsString* current) {
4856 uint8_t state = kInitialState;
4857 return Calculate(current, &state);
4858 }
4859
4860 private:
4861 DISALLOW_IMPLICIT_CONSTRUCTORS(Utf8LengthHelper);
4862};
4863
4864
4865static int Utf8Length(i::String* str, i::Isolate* isolate) {
4866 int length = str->length();
4867 if (length == 0) return 0;
4868 uint8_t state;
4869 i::ConsString* cons_string =
4870 Utf8LengthHelper::Visitor::VisitFlat(str, &length, &state);
4871 if (cons_string == NULL) return length;
4872 return Utf8LengthHelper::Calculate(cons_string);
4873}
4874
4875
Steve Blocka7e24c12009-10-30 11:49:00 +00004876int String::Utf8Length() const {
Steve Block44f0eee2011-05-26 01:26:41 +01004877 i::Handle<i::String> str = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004878 i::Isolate* isolate = str->GetIsolate();
4879 return v8::Utf8Length(*str, isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004880}
4881
4882
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004883class Utf8WriterVisitor {
4884 public:
4885 Utf8WriterVisitor(
4886 char* buffer,
4887 int capacity,
4888 bool skip_capacity_check,
4889 bool replace_invalid_utf8)
4890 : early_termination_(false),
4891 last_character_(unibrow::Utf16::kNoPreviousCharacter),
4892 buffer_(buffer),
4893 start_(buffer),
4894 capacity_(capacity),
4895 skip_capacity_check_(capacity == -1 || skip_capacity_check),
4896 replace_invalid_utf8_(replace_invalid_utf8),
4897 utf16_chars_read_(0) {
4898 }
4899
4900 static int WriteEndCharacter(uint16_t character,
4901 int last_character,
4902 int remaining,
4903 char* const buffer,
4904 bool replace_invalid_utf8) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004905 DCHECK_GT(remaining, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004906 // We can't use a local buffer here because Encode needs to modify
4907 // previous characters in the stream. We know, however, that
4908 // exactly one character will be advanced.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004909 if (unibrow::Utf16::IsSurrogatePair(last_character, character)) {
4910 int written = unibrow::Utf8::Encode(buffer, character, last_character,
4911 replace_invalid_utf8);
4912 DCHECK_EQ(written, 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004913 return written;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004914 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004915 // Use a scratch buffer to check the required characters.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004916 char temp_buffer[unibrow::Utf8::kMaxEncodedSize];
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004917 // Can't encode using last_character as gcc has array bounds issues.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004918 int written = unibrow::Utf8::Encode(temp_buffer, character,
4919 unibrow::Utf16::kNoPreviousCharacter,
4920 replace_invalid_utf8);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004921 // Won't fit.
4922 if (written > remaining) return 0;
4923 // Copy over the character from temp_buffer.
4924 for (int j = 0; j < written; j++) {
4925 buffer[j] = temp_buffer[j];
4926 }
4927 return written;
4928 }
4929
4930 // Visit writes out a group of code units (chars) of a v8::String to the
4931 // internal buffer_. This is done in two phases. The first phase calculates a
4932 // pesimistic estimate (writable_length) on how many code units can be safely
4933 // written without exceeding the buffer capacity and without writing the last
4934 // code unit (it could be a lead surrogate). The estimated number of code
4935 // units is then written out in one go, and the reported byte usage is used
4936 // to correct the estimate. This is repeated until the estimate becomes <= 0
4937 // or all code units have been written out. The second phase writes out code
4938 // units until the buffer capacity is reached, would be exceeded by the next
4939 // unit, or all units have been written out.
4940 template<typename Char>
4941 void Visit(const Char* chars, const int length) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004942 DCHECK(!early_termination_);
4943 if (length == 0) return;
4944 // Copy state to stack.
4945 char* buffer = buffer_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004946 int last_character = sizeof(Char) == 1
4947 ? unibrow::Utf16::kNoPreviousCharacter
4948 : last_character_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004949 int i = 0;
4950 // Do a fast loop where there is no exit capacity check.
4951 while (true) {
4952 int fast_length;
4953 if (skip_capacity_check_) {
4954 fast_length = length;
4955 } else {
4956 int remaining_capacity = capacity_ - static_cast<int>(buffer - start_);
4957 // Need enough space to write everything but one character.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004958 STATIC_ASSERT(unibrow::Utf16::kMaxExtraUtf8BytesForOneUtf16CodeUnit ==
4959 3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004960 int max_size_per_char = sizeof(Char) == 1 ? 2 : 3;
4961 int writable_length =
4962 (remaining_capacity - max_size_per_char)/max_size_per_char;
4963 // Need to drop into slow loop.
4964 if (writable_length <= 0) break;
4965 fast_length = i + writable_length;
4966 if (fast_length > length) fast_length = length;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004967 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004968 // Write the characters to the stream.
4969 if (sizeof(Char) == 1) {
4970 for (; i < fast_length; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004971 buffer += unibrow::Utf8::EncodeOneByte(
4972 buffer, static_cast<uint8_t>(*chars++));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004973 DCHECK(capacity_ == -1 || (buffer - start_) <= capacity_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004974 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004975 } else {
4976 for (; i < fast_length; i++) {
4977 uint16_t character = *chars++;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004978 buffer += unibrow::Utf8::Encode(buffer, character, last_character,
4979 replace_invalid_utf8_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004980 last_character = character;
4981 DCHECK(capacity_ == -1 || (buffer - start_) <= capacity_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004982 }
4983 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004984 // Array is fully written. Exit.
4985 if (fast_length == length) {
4986 // Write state back out to object.
4987 last_character_ = last_character;
4988 buffer_ = buffer;
4989 utf16_chars_read_ += length;
4990 return;
4991 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004992 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004993 DCHECK(!skip_capacity_check_);
4994 // Slow loop. Must check capacity on each iteration.
4995 int remaining_capacity = capacity_ - static_cast<int>(buffer - start_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004996 DCHECK_GE(remaining_capacity, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004997 for (; i < length && remaining_capacity > 0; i++) {
4998 uint16_t character = *chars++;
4999 // remaining_capacity is <= 3 bytes at this point, so we do not write out
5000 // an umatched lead surrogate.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005001 if (replace_invalid_utf8_ && unibrow::Utf16::IsLeadSurrogate(character)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005002 early_termination_ = true;
5003 break;
5004 }
5005 int written = WriteEndCharacter(character,
5006 last_character,
5007 remaining_capacity,
5008 buffer,
5009 replace_invalid_utf8_);
5010 if (written == 0) {
5011 early_termination_ = true;
5012 break;
5013 }
5014 buffer += written;
5015 remaining_capacity -= written;
5016 last_character = character;
5017 }
5018 // Write state back out to object.
5019 last_character_ = last_character;
5020 buffer_ = buffer;
5021 utf16_chars_read_ += i;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005022 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005023
5024 inline bool IsDone() {
5025 return early_termination_;
5026 }
5027
5028 inline void VisitOneByteString(const uint8_t* chars, int length) {
5029 Visit(chars, length);
5030 }
5031
5032 inline void VisitTwoByteString(const uint16_t* chars, int length) {
5033 Visit(chars, length);
5034 }
5035
5036 int CompleteWrite(bool write_null, int* utf16_chars_read_out) {
5037 // Write out number of utf16 characters written to the stream.
5038 if (utf16_chars_read_out != NULL) {
5039 *utf16_chars_read_out = utf16_chars_read_;
5040 }
5041 // Only null terminate if all of the string was written and there's space.
5042 if (write_null &&
5043 !early_termination_ &&
5044 (capacity_ == -1 || (buffer_ - start_) < capacity_)) {
5045 *buffer_++ = '\0';
5046 }
5047 return static_cast<int>(buffer_ - start_);
5048 }
5049
5050 private:
5051 bool early_termination_;
5052 int last_character_;
5053 char* buffer_;
5054 char* const start_;
5055 int capacity_;
5056 bool const skip_capacity_check_;
5057 bool const replace_invalid_utf8_;
5058 int utf16_chars_read_;
5059 DISALLOW_IMPLICIT_CONSTRUCTORS(Utf8WriterVisitor);
5060};
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005061
5062
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005063static bool RecursivelySerializeToUtf8(i::String* current,
5064 Utf8WriterVisitor* writer,
5065 int recursion_budget) {
5066 while (!writer->IsDone()) {
5067 i::ConsString* cons_string = i::String::VisitFlat(writer, current);
5068 if (cons_string == NULL) return true; // Leaf node.
5069 if (recursion_budget <= 0) return false;
5070 // Must write the left branch first.
5071 i::String* first = cons_string->first();
5072 bool success = RecursivelySerializeToUtf8(first,
5073 writer,
5074 recursion_budget - 1);
5075 if (!success) return false;
5076 // Inline tail recurse for right branch.
5077 current = cons_string->second();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005078 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005079 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00005080}
5081
5082
Steve Block6ded16b2010-05-10 14:33:55 +01005083int String::WriteUtf8(char* buffer,
5084 int capacity,
5085 int* nchars_ref,
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005086 int options) const {
Steve Block44f0eee2011-05-26 01:26:41 +01005087 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01005088 LOG_API(isolate, "String::WriteUtf8");
5089 ENTER_V8(isolate);
Ben Murdoch85b71792012-04-11 18:30:58 +01005090 i::Handle<i::String> str = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005091 if (options & HINT_MANY_WRITES_EXPECTED) {
5092 str = i::String::Flatten(str); // Flatten the string for efficiency.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005093 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005094 const int string_length = str->length();
5095 bool write_null = !(options & NO_NULL_TERMINATION);
5096 bool replace_invalid_utf8 = (options & REPLACE_INVALID_UTF8);
5097 int max16BitCodeUnitSize = unibrow::Utf8::kMax16BitCodeUnitSize;
5098 // First check if we can just write the string without checking capacity.
5099 if (capacity == -1 || capacity / max16BitCodeUnitSize >= string_length) {
5100 Utf8WriterVisitor writer(buffer, capacity, true, replace_invalid_utf8);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005101 const int kMaxRecursion = 100;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005102 bool success = RecursivelySerializeToUtf8(*str, &writer, kMaxRecursion);
5103 if (success) return writer.CompleteWrite(write_null, nchars_ref);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005104 } else if (capacity >= string_length) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005105 // First check that the buffer is large enough.
5106 int utf8_bytes = v8::Utf8Length(*str, str->GetIsolate());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005107 if (utf8_bytes <= capacity) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005108 // one-byte fast path.
5109 if (utf8_bytes == string_length) {
5110 WriteOneByte(reinterpret_cast<uint8_t*>(buffer), 0, capacity, options);
5111 if (nchars_ref != NULL) *nchars_ref = string_length;
5112 if (write_null && (utf8_bytes+1 <= capacity)) {
5113 return string_length + 1;
5114 }
5115 return string_length;
5116 }
5117 if (write_null && (utf8_bytes+1 > capacity)) {
5118 options |= NO_NULL_TERMINATION;
5119 }
5120 // Recurse once without a capacity limit.
5121 // This will get into the first branch above.
5122 // TODO(dcarney) Check max left rec. in Utf8Length and fall through.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005123 return WriteUtf8(buffer, -1, nchars_ref, options);
5124 }
5125 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005126 // Recursive slow path can potentially be unreasonable slow. Flatten.
5127 str = i::String::Flatten(str);
5128 Utf8WriterVisitor writer(buffer, capacity, false, replace_invalid_utf8);
5129 i::String::VisitFlat(&writer, *str);
5130 return writer.CompleteWrite(write_null, nchars_ref);
Steve Blocka7e24c12009-10-30 11:49:00 +00005131}
5132
5133
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005134template<typename CharType>
5135static inline int WriteHelper(const String* string,
5136 CharType* buffer,
5137 int start,
5138 int length,
5139 int options) {
5140 i::Isolate* isolate = Utils::OpenHandle(string)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01005141 LOG_API(isolate, "String::Write");
5142 ENTER_V8(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005143 DCHECK(start >= 0 && length >= -1);
5144 i::Handle<i::String> str = Utils::OpenHandle(string);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005145 if (options & String::HINT_MANY_WRITES_EXPECTED) {
Steve Block6ded16b2010-05-10 14:33:55 +01005146 // Flatten the string for efficiency. This applies whether we are
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005147 // using StringCharacterStream or Get(i) to access the characters.
5148 str = i::String::Flatten(str);
Steve Block6ded16b2010-05-10 14:33:55 +01005149 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01005150 int end = start + length;
5151 if ((length == -1) || (length > str->length() - start) )
5152 end = str->length();
Steve Blocka7e24c12009-10-30 11:49:00 +00005153 if (end < 0) return 0;
5154 i::String::WriteToFlat(*str, buffer, start, end);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005155 if (!(options & String::NO_NULL_TERMINATION) &&
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005156 (length == -1 || end - start < length)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01005157 buffer[end - start] = '\0';
5158 }
5159 return end - start;
Steve Blocka7e24c12009-10-30 11:49:00 +00005160}
5161
5162
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005163int String::WriteOneByte(uint8_t* buffer,
5164 int start,
5165 int length,
5166 int options) const {
5167 return WriteHelper(this, buffer, start, length, options);
5168}
5169
5170
5171int String::Write(uint16_t* buffer,
5172 int start,
5173 int length,
5174 int options) const {
5175 return WriteHelper(this, buffer, start, length, options);
5176}
5177
5178
Steve Blocka7e24c12009-10-30 11:49:00 +00005179bool v8::String::IsExternal() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00005180 i::Handle<i::String> str = Utils::OpenHandle(this);
5181 return i::StringShape(*str).IsExternalTwoByte();
5182}
5183
5184
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005185bool v8::String::IsExternalOneByte() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00005186 i::Handle<i::String> str = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005187 return i::StringShape(*str).IsExternalOneByte();
Steve Blocka7e24c12009-10-30 11:49:00 +00005188}
5189
5190
5191void v8::String::VerifyExternalStringResource(
5192 v8::String::ExternalStringResource* value) const {
5193 i::Handle<i::String> str = Utils::OpenHandle(this);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005194 const v8::String::ExternalStringResource* expected;
Steve Blocka7e24c12009-10-30 11:49:00 +00005195 if (i::StringShape(*str).IsExternalTwoByte()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005196 const void* resource =
5197 i::Handle<i::ExternalTwoByteString>::cast(str)->resource();
5198 expected = reinterpret_cast<const ExternalStringResource*>(resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00005199 } else {
5200 expected = NULL;
5201 }
5202 CHECK_EQ(expected, value);
5203}
5204
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005205void v8::String::VerifyExternalStringResourceBase(
5206 v8::String::ExternalStringResourceBase* value, Encoding encoding) const {
Steve Blocka7e24c12009-10-30 11:49:00 +00005207 i::Handle<i::String> str = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005208 const v8::String::ExternalStringResourceBase* expected;
5209 Encoding expectedEncoding;
5210 if (i::StringShape(*str).IsExternalOneByte()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005211 const void* resource =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005212 i::Handle<i::ExternalOneByteString>::cast(str)->resource();
5213 expected = reinterpret_cast<const ExternalStringResourceBase*>(resource);
5214 expectedEncoding = ONE_BYTE_ENCODING;
5215 } else if (i::StringShape(*str).IsExternalTwoByte()) {
5216 const void* resource =
5217 i::Handle<i::ExternalTwoByteString>::cast(str)->resource();
5218 expected = reinterpret_cast<const ExternalStringResourceBase*>(resource);
5219 expectedEncoding = TWO_BYTE_ENCODING;
5220 } else {
5221 expected = NULL;
5222 expectedEncoding =
5223 str->IsOneByteRepresentation() ? ONE_BYTE_ENCODING : TWO_BYTE_ENCODING;
5224 }
5225 CHECK_EQ(expected, value);
5226 CHECK_EQ(expectedEncoding, encoding);
5227}
5228
5229const v8::String::ExternalOneByteStringResource*
5230v8::String::GetExternalOneByteStringResource() const {
5231 i::Handle<i::String> str = Utils::OpenHandle(this);
5232 if (i::StringShape(*str).IsExternalOneByte()) {
5233 const void* resource =
5234 i::Handle<i::ExternalOneByteString>::cast(str)->resource();
5235 return reinterpret_cast<const ExternalOneByteStringResource*>(resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00005236 } else {
5237 return NULL;
5238 }
5239}
5240
5241
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005242Local<Value> Symbol::Name() const {
5243 i::Handle<i::Symbol> sym = Utils::OpenHandle(this);
5244 i::Handle<i::Object> name(sym->name(), sym->GetIsolate());
5245 return Utils::ToLocal(name);
5246}
5247
5248
5249Local<Value> Private::Name() const {
5250 return reinterpret_cast<const Symbol*>(this)->Name();
5251}
5252
5253
Steve Blocka7e24c12009-10-30 11:49:00 +00005254double Number::Value() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00005255 i::Handle<i::Object> obj = Utils::OpenHandle(this);
5256 return obj->Number();
5257}
5258
5259
5260bool Boolean::Value() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00005261 i::Handle<i::Object> obj = Utils::OpenHandle(this);
5262 return obj->IsTrue();
5263}
5264
5265
5266int64_t Integer::Value() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00005267 i::Handle<i::Object> obj = Utils::OpenHandle(this);
5268 if (obj->IsSmi()) {
5269 return i::Smi::cast(*obj)->value();
5270 } else {
5271 return static_cast<int64_t>(obj->Number());
5272 }
5273}
5274
5275
5276int32_t Int32::Value() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00005277 i::Handle<i::Object> obj = Utils::OpenHandle(this);
5278 if (obj->IsSmi()) {
5279 return i::Smi::cast(*obj)->value();
5280 } else {
5281 return static_cast<int32_t>(obj->Number());
5282 }
5283}
5284
5285
Steve Block6ded16b2010-05-10 14:33:55 +01005286uint32_t Uint32::Value() const {
Steve Block6ded16b2010-05-10 14:33:55 +01005287 i::Handle<i::Object> obj = Utils::OpenHandle(this);
5288 if (obj->IsSmi()) {
5289 return i::Smi::cast(*obj)->value();
5290 } else {
5291 return static_cast<uint32_t>(obj->Number());
5292 }
5293}
5294
5295
Steve Blocka7e24c12009-10-30 11:49:00 +00005296int v8::Object::InternalFieldCount() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005297 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
5298 if (!self->IsJSObject()) return 0;
5299 return i::Handle<i::JSObject>::cast(self)->GetInternalFieldCount();
Steve Blocka7e24c12009-10-30 11:49:00 +00005300}
5301
5302
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005303static bool InternalFieldOK(i::Handle<i::JSReceiver> obj, int index,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005304 const char* location) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005305 return Utils::ApiCheck(
5306 obj->IsJSObject() &&
5307 (index < i::Handle<i::JSObject>::cast(obj)->GetInternalFieldCount()),
5308 location, "Internal field out of bounds");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005309}
5310
5311
5312Local<Value> v8::Object::SlowGetInternalField(int index) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005313 i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005314 const char* location = "v8::Object::GetInternalField()";
5315 if (!InternalFieldOK(obj, index, location)) return Local<Value>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005316 i::Handle<i::Object> value(
5317 i::Handle<i::JSObject>::cast(obj)->GetInternalField(index),
5318 obj->GetIsolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005319 return Utils::ToLocal(value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005320}
5321
5322
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005323void v8::Object::SetInternalField(int index, v8::Local<Value> value) {
5324 i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005325 const char* location = "v8::Object::SetInternalField()";
5326 if (!InternalFieldOK(obj, index, location)) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00005327 i::Handle<i::Object> val = Utils::OpenHandle(*value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005328 i::Handle<i::JSObject>::cast(obj)->SetInternalField(index, *val);
Steve Blocka7e24c12009-10-30 11:49:00 +00005329}
5330
5331
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005332void* v8::Object::SlowGetAlignedPointerFromInternalField(int index) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005333 i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005334 const char* location = "v8::Object::GetAlignedPointerFromInternalField()";
5335 if (!InternalFieldOK(obj, index, location)) return NULL;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005336 return DecodeSmiToAligned(
5337 i::Handle<i::JSObject>::cast(obj)->GetInternalField(index), location);
Ben Murdochb8e0da22011-05-16 14:20:40 +01005338}
5339
5340
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005341void v8::Object::SetAlignedPointerInInternalField(int index, void* value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005342 i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005343 const char* location = "v8::Object::SetAlignedPointerInInternalField()";
5344 if (!InternalFieldOK(obj, index, location)) return;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005345 i::Handle<i::JSObject>::cast(obj)
5346 ->SetInternalField(index, EncodeAlignedAsSmi(value, location));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005347 DCHECK_EQ(value, GetAlignedPointerFromInternalField(index));
Ben Murdochb8e0da22011-05-16 14:20:40 +01005348}
5349
5350
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005351static void* ExternalValue(i::Object* obj) {
5352 // Obscure semantics for undefined, but somehow checked in our unit tests...
5353 if (obj->IsUndefined()) return NULL;
5354 i::Object* foreign = i::JSObject::cast(obj)->GetInternalField(0);
5355 return i::Foreign::cast(foreign)->foreign_address();
Steve Blocka7e24c12009-10-30 11:49:00 +00005356}
5357
5358
5359// --- E n v i r o n m e n t ---
5360
Steve Block44f0eee2011-05-26 01:26:41 +01005361
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005362void v8::V8::InitializePlatform(Platform* platform) {
5363 i::V8::InitializePlatform(platform);
Steve Blocka7e24c12009-10-30 11:49:00 +00005364}
5365
5366
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005367void v8::V8::ShutdownPlatform() {
5368 i::V8::ShutdownPlatform();
5369}
5370
5371
5372bool v8::V8::Initialize() {
5373 i::V8::Initialize();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005374#ifdef V8_USE_EXTERNAL_STARTUP_DATA
5375 i::ReadNatives();
5376#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005377 return true;
5378}
5379
5380
5381void v8::V8::SetEntropySource(EntropySource entropy_source) {
5382 base::RandomNumberGenerator::SetEntropySource(entropy_source);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005383}
5384
5385
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005386void v8::V8::SetReturnAddressLocationResolver(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005387 ReturnAddressLocationResolver return_address_resolver) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005388 i::StackFrame::SetReturnAddressLocationResolver(return_address_resolver);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005389}
5390
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005391
Steve Blocka7e24c12009-10-30 11:49:00 +00005392bool v8::V8::Dispose() {
5393 i::V8::TearDown();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005394#ifdef V8_USE_EXTERNAL_STARTUP_DATA
5395 i::DisposeNatives();
5396#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005397 return true;
5398}
5399
5400
Russell Brenner90bac252010-11-18 13:33:46 -08005401HeapStatistics::HeapStatistics(): total_heap_size_(0),
5402 total_heap_size_executable_(0),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005403 total_physical_size_(0),
Ben Murdochb8e0da22011-05-16 14:20:40 +01005404 used_heap_size_(0),
5405 heap_size_limit_(0) { }
Steve Block3ce2e202009-11-05 08:53:23 +00005406
5407
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005408HeapSpaceStatistics::HeapSpaceStatistics(): space_name_(0),
5409 space_size_(0),
5410 space_used_size_(0),
5411 space_available_size_(0),
5412 physical_space_size_(0) { }
5413
5414
5415HeapObjectStatistics::HeapObjectStatistics()
5416 : object_type_(nullptr),
5417 object_sub_type_(nullptr),
5418 object_count_(0),
5419 object_size_(0) {}
5420
5421
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005422bool v8::V8::InitializeICU(const char* icu_data_file) {
5423 return i::InitializeICU(icu_data_file);
Steve Block6ded16b2010-05-10 14:33:55 +01005424}
5425
5426
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005427void v8::V8::InitializeExternalStartupData(const char* directory_path) {
5428 i::InitializeExternalStartupData(directory_path);
5429}
5430
5431
5432void v8::V8::InitializeExternalStartupData(const char* natives_blob,
5433 const char* snapshot_blob) {
5434 i::InitializeExternalStartupData(natives_blob, snapshot_blob);
5435}
5436
5437
Steve Blocka7e24c12009-10-30 11:49:00 +00005438const char* v8::V8::GetVersion() {
Steve Block44f0eee2011-05-26 01:26:41 +01005439 return i::Version::GetVersion();
Steve Blocka7e24c12009-10-30 11:49:00 +00005440}
5441
5442
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005443static i::Handle<i::Context> CreateEnvironment(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005444 i::Isolate* isolate, v8::ExtensionConfiguration* extensions,
5445 v8::Local<ObjectTemplate> global_template,
5446 v8::Local<Value> maybe_global_proxy) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005447 i::Handle<i::Context> env;
Steve Blocka7e24c12009-10-30 11:49:00 +00005448
5449 // Enter V8 via an ENTER_V8 scope.
Steve Blocka7e24c12009-10-30 11:49:00 +00005450 {
Steve Block44f0eee2011-05-26 01:26:41 +01005451 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005452 v8::Local<ObjectTemplate> proxy_template = global_template;
Steve Blocka7e24c12009-10-30 11:49:00 +00005453 i::Handle<i::FunctionTemplateInfo> proxy_constructor;
5454 i::Handle<i::FunctionTemplateInfo> global_constructor;
5455
5456 if (!global_template.IsEmpty()) {
5457 // Make sure that the global_template has a constructor.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005458 global_constructor = EnsureConstructor(isolate, *global_template);
Steve Blocka7e24c12009-10-30 11:49:00 +00005459
5460 // Create a fresh template for the global proxy object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005461 proxy_template = ObjectTemplate::New(
5462 reinterpret_cast<v8::Isolate*>(isolate));
5463 proxy_constructor = EnsureConstructor(isolate, *proxy_template);
Steve Blocka7e24c12009-10-30 11:49:00 +00005464
5465 // Set the global template to be the prototype template of
5466 // global proxy template.
5467 proxy_constructor->set_prototype_template(
5468 *Utils::OpenHandle(*global_template));
5469
5470 // Migrate security handlers from global_template to
5471 // proxy_template. Temporarily removing access check
5472 // information from the global template.
5473 if (!global_constructor->access_check_info()->IsUndefined()) {
5474 proxy_constructor->set_access_check_info(
5475 global_constructor->access_check_info());
5476 proxy_constructor->set_needs_access_check(
5477 global_constructor->needs_access_check());
5478 global_constructor->set_needs_access_check(false);
Steve Block44f0eee2011-05-26 01:26:41 +01005479 global_constructor->set_access_check_info(
5480 isolate->heap()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00005481 }
5482 }
5483
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005484 i::Handle<i::Object> proxy = Utils::OpenHandle(*maybe_global_proxy, true);
5485 i::MaybeHandle<i::JSGlobalProxy> maybe_proxy;
5486 if (!proxy.is_null()) {
5487 maybe_proxy = i::Handle<i::JSGlobalProxy>::cast(proxy);
5488 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005489 // Create the environment.
Steve Block44f0eee2011-05-26 01:26:41 +01005490 env = isolate->bootstrapper()->CreateEnvironment(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005491 maybe_proxy, proxy_template, extensions);
Steve Blocka7e24c12009-10-30 11:49:00 +00005492
5493 // Restore the access check info on the global template.
5494 if (!global_template.IsEmpty()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005495 DCHECK(!global_constructor.is_null());
5496 DCHECK(!proxy_constructor.is_null());
Steve Blocka7e24c12009-10-30 11:49:00 +00005497 global_constructor->set_access_check_info(
5498 proxy_constructor->access_check_info());
5499 global_constructor->set_needs_access_check(
5500 proxy_constructor->needs_access_check());
5501 }
5502 }
5503 // Leave V8.
5504
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005505 return env;
5506}
5507
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005508Local<Context> v8::Context::New(v8::Isolate* external_isolate,
5509 v8::ExtensionConfiguration* extensions,
5510 v8::Local<ObjectTemplate> global_template,
5511 v8::Local<Value> global_object) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005512 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
5513 LOG_API(isolate, "Context::New");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005514 i::HandleScope scope(isolate);
5515 ExtensionConfiguration no_extensions;
5516 if (extensions == NULL) extensions = &no_extensions;
5517 i::Handle<i::Context> env =
5518 CreateEnvironment(isolate, extensions, global_template, global_object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005519 if (env.is_null()) {
5520 if (isolate->has_pending_exception()) {
5521 isolate->OptionalRescheduleException(true);
5522 }
5523 return Local<Context>();
5524 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005525 return Utils::ToLocal(scope.CloseAndEscape(env));
Steve Blocka7e24c12009-10-30 11:49:00 +00005526}
5527
5528
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005529void v8::Context::SetSecurityToken(Local<Value> token) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005530 i::Handle<i::Context> env = Utils::OpenHandle(this);
5531 i::Handle<i::Object> token_handle = Utils::OpenHandle(*token);
5532 env->set_security_token(*token_handle);
5533}
5534
5535
5536void v8::Context::UseDefaultSecurityToken() {
Steve Blocka7e24c12009-10-30 11:49:00 +00005537 i::Handle<i::Context> env = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005538 env->set_security_token(env->global_object());
Steve Blocka7e24c12009-10-30 11:49:00 +00005539}
5540
5541
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005542Local<Value> v8::Context::GetSecurityToken() {
Steve Blocka7e24c12009-10-30 11:49:00 +00005543 i::Handle<i::Context> env = Utils::OpenHandle(this);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005544 i::Isolate* isolate = env->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00005545 i::Object* security_token = env->security_token();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005546 i::Handle<i::Object> token_handle(security_token, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005547 return Utils::ToLocal(token_handle);
5548}
5549
5550
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005551v8::Isolate* Context::GetIsolate() {
Steve Blocka7e24c12009-10-30 11:49:00 +00005552 i::Handle<i::Context> env = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005553 return reinterpret_cast<Isolate*>(env->GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00005554}
5555
5556
5557v8::Local<v8::Object> Context::Global() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005558 i::Handle<i::Context> context = Utils::OpenHandle(this);
5559 i::Isolate* isolate = context->GetIsolate();
5560 i::Handle<i::Object> global(context->global_proxy(), isolate);
5561 // TODO(dcarney): This should always return the global proxy
5562 // but can't presently as calls to GetProtoype will return the wrong result.
5563 if (i::Handle<i::JSGlobalProxy>::cast(
5564 global)->IsDetachedFrom(context->global_object())) {
5565 global = i::Handle<i::Object>(context->global_object(), isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01005566 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005567 return Utils::ToLocal(i::Handle<i::JSObject>::cast(global));
5568}
5569
5570
5571void Context::DetachGlobal() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005572 i::Handle<i::Context> context = Utils::OpenHandle(this);
5573 i::Isolate* isolate = context->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01005574 ENTER_V8(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01005575 isolate->bootstrapper()->DetachGlobal(context);
Steve Blocka7e24c12009-10-30 11:49:00 +00005576}
5577
5578
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005579Local<v8::Object> Context::GetExtrasBindingObject() {
5580 i::Handle<i::Context> context = Utils::OpenHandle(this);
5581 i::Isolate* isolate = context->GetIsolate();
5582 i::Handle<i::JSObject> binding(context->extras_binding_object(), isolate);
5583 return Utils::ToLocal(binding);
5584}
5585
5586
Ben Murdoch257744e2011-11-30 15:57:28 +00005587void Context::AllowCodeGenerationFromStrings(bool allow) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005588 i::Handle<i::Context> context = Utils::OpenHandle(this);
5589 i::Isolate* isolate = context->GetIsolate();
Ben Murdoch257744e2011-11-30 15:57:28 +00005590 ENTER_V8(isolate);
Ben Murdoch257744e2011-11-30 15:57:28 +00005591 context->set_allow_code_gen_from_strings(
5592 allow ? isolate->heap()->true_value() : isolate->heap()->false_value());
5593}
5594
5595
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005596bool Context::IsCodeGenerationFromStringsAllowed() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005597 i::Handle<i::Context> context = Utils::OpenHandle(this);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005598 return !context->allow_code_gen_from_strings()->IsFalse();
5599}
5600
5601
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005602void Context::SetErrorMessageForCodeGenerationFromStrings(Local<String> error) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005603 i::Handle<i::Context> context = Utils::OpenHandle(this);
5604 i::Handle<i::String> error_handle = Utils::OpenHandle(*error);
5605 context->set_error_message_for_code_gen_from_strings(*error_handle);
Andrei Popescu74b3c142010-03-29 12:03:09 +01005606}
5607
5608
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005609size_t Context::EstimatedSize() {
5610 return static_cast<size_t>(
5611 i::ContextMeasure(*Utils::OpenHandle(this)).Size());
5612}
5613
5614
5615MaybeLocal<v8::Object> ObjectTemplate::NewInstance(Local<Context> context) {
5616 PREPARE_FOR_EXECUTION(context, "v8::ObjectTemplate::NewInstance()", Object);
5617 auto self = Utils::OpenHandle(this);
5618 Local<Object> result;
5619 has_pending_exception =
5620 !ToLocal<Object>(i::ApiNatives::InstantiateObject(self), &result);
5621 RETURN_ON_FAILED_EXECUTION(Object);
5622 RETURN_ESCAPED(result);
5623}
5624
5625
Steve Blocka7e24c12009-10-30 11:49:00 +00005626Local<v8::Object> ObjectTemplate::NewInstance() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005627 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
5628 RETURN_TO_LOCAL_UNCHECKED(NewInstance(context), Object);
5629}
5630
5631
5632MaybeLocal<v8::Function> FunctionTemplate::GetFunction(Local<Context> context) {
5633 PREPARE_FOR_EXECUTION(context, "v8::FunctionTemplate::GetFunction()",
5634 Function);
5635 auto self = Utils::OpenHandle(this);
5636 Local<Function> result;
5637 has_pending_exception =
5638 !ToLocal<Function>(i::ApiNatives::InstantiateFunction(self), &result);
5639 RETURN_ON_FAILED_EXECUTION(Function);
5640 RETURN_ESCAPED(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00005641}
5642
5643
5644Local<v8::Function> FunctionTemplate::GetFunction() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005645 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
5646 RETURN_TO_LOCAL_UNCHECKED(GetFunction(context), Function);
Steve Blocka7e24c12009-10-30 11:49:00 +00005647}
5648
5649
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005650bool FunctionTemplate::HasInstance(v8::Local<v8::Value> value) {
5651 auto self = Utils::OpenHandle(this);
5652 auto obj = Utils::OpenHandle(*value);
5653 return self->IsTemplateFor(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00005654}
5655
5656
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005657Local<External> v8::External::New(Isolate* isolate, void* value) {
5658 STATIC_ASSERT(sizeof(value) == sizeof(i::Address));
5659 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
5660 LOG_API(i_isolate, "External::New");
5661 ENTER_V8(i_isolate);
5662 i::Handle<i::JSObject> external = i_isolate->factory()->NewExternal(value);
5663 return Utils::ExternalToLocal(external);
Steve Blocka7e24c12009-10-30 11:49:00 +00005664}
5665
5666
5667void* External::Value() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005668 return ExternalValue(*Utils::OpenHandle(this));
Steve Blocka7e24c12009-10-30 11:49:00 +00005669}
5670
5671
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005672// anonymous namespace for string creation helper functions
5673namespace {
5674
5675inline int StringLength(const char* string) {
5676 return i::StrLength(string);
Steve Blocka7e24c12009-10-30 11:49:00 +00005677}
5678
5679
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005680inline int StringLength(const uint8_t* string) {
5681 return i::StrLength(reinterpret_cast<const char*>(string));
5682}
5683
5684
5685inline int StringLength(const uint16_t* string) {
5686 int length = 0;
5687 while (string[length] != '\0')
5688 length++;
5689 return length;
5690}
5691
5692
5693MUST_USE_RESULT
5694inline i::MaybeHandle<i::String> NewString(i::Factory* factory,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005695 v8::NewStringType type,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005696 i::Vector<const char> string) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005697 if (type == v8::NewStringType::kInternalized) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005698 return factory->InternalizeUtf8String(string);
5699 }
5700 return factory->NewStringFromUtf8(string);
5701}
5702
5703
5704MUST_USE_RESULT
5705inline i::MaybeHandle<i::String> NewString(i::Factory* factory,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005706 v8::NewStringType type,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005707 i::Vector<const uint8_t> string) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005708 if (type == v8::NewStringType::kInternalized) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005709 return factory->InternalizeOneByteString(string);
5710 }
5711 return factory->NewStringFromOneByte(string);
5712}
5713
5714
5715MUST_USE_RESULT
5716inline i::MaybeHandle<i::String> NewString(i::Factory* factory,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005717 v8::NewStringType type,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005718 i::Vector<const uint16_t> string) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005719 if (type == v8::NewStringType::kInternalized) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005720 return factory->InternalizeTwoByteString(string);
5721 }
5722 return factory->NewStringFromTwoByte(string);
5723}
5724
5725
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005726STATIC_ASSERT(v8::String::kMaxLength == i::String::kMaxLength);
5727
5728
5729template <typename Char>
5730inline MaybeLocal<String> NewString(Isolate* v8_isolate, const char* location,
5731 const char* env, const Char* data,
5732 v8::NewStringType type, int length) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005733 i::Isolate* isolate = reinterpret_cast<internal::Isolate*>(v8_isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005734 if (length == 0) return String::Empty(v8_isolate);
5735 // TODO(dcarney): throw a context free exception.
5736 if (length > i::String::kMaxLength) return MaybeLocal<String>();
Steve Block44f0eee2011-05-26 01:26:41 +01005737 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005738 LOG_API(isolate, env);
5739 if (length < 0) length = StringLength(data);
5740 i::Handle<i::String> result =
5741 NewString(isolate->factory(), type, i::Vector<const Char>(data, length))
5742 .ToHandleChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +00005743 return Utils::ToLocal(result);
5744}
5745
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005746} // anonymous namespace
5747
5748
5749Local<String> String::NewFromUtf8(Isolate* isolate,
5750 const char* data,
5751 NewStringType type,
5752 int length) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005753 RETURN_TO_LOCAL_UNCHECKED(
5754 NewString(isolate, "v8::String::NewFromUtf8()", "String::NewFromUtf8",
5755 data, static_cast<v8::NewStringType>(type), length),
5756 String);
5757}
5758
5759
5760MaybeLocal<String> String::NewFromUtf8(Isolate* isolate, const char* data,
5761 v8::NewStringType type, int length) {
5762 return NewString(isolate, "v8::String::NewFromUtf8()", "String::NewFromUtf8",
5763 data, type, length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005764}
5765
5766
5767Local<String> String::NewFromOneByte(Isolate* isolate,
5768 const uint8_t* data,
5769 NewStringType type,
5770 int length) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005771 RETURN_TO_LOCAL_UNCHECKED(
5772 NewString(isolate, "v8::String::NewFromOneByte()",
5773 "String::NewFromOneByte", data,
5774 static_cast<v8::NewStringType>(type), length),
5775 String);
5776}
5777
5778
5779MaybeLocal<String> String::NewFromOneByte(Isolate* isolate, const uint8_t* data,
5780 v8::NewStringType type, int length) {
5781 return NewString(isolate, "v8::String::NewFromOneByte()",
5782 "String::NewFromOneByte", data, type, length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005783}
5784
5785
5786Local<String> String::NewFromTwoByte(Isolate* isolate,
5787 const uint16_t* data,
5788 NewStringType type,
5789 int length) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005790 RETURN_TO_LOCAL_UNCHECKED(
5791 NewString(isolate, "v8::String::NewFromTwoByte()",
5792 "String::NewFromTwoByte", data,
5793 static_cast<v8::NewStringType>(type), length),
5794 String);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005795}
5796
Steve Blocka7e24c12009-10-30 11:49:00 +00005797
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005798MaybeLocal<String> String::NewFromTwoByte(Isolate* isolate,
5799 const uint16_t* data,
5800 v8::NewStringType type, int length) {
5801 return NewString(isolate, "v8::String::NewFromTwoByte()",
5802 "String::NewFromTwoByte", data, type, length);
5803}
5804
5805
5806Local<String> v8::String::Concat(Local<String> left, Local<String> right) {
Steve Block3ce2e202009-11-05 08:53:23 +00005807 i::Handle<i::String> left_string = Utils::OpenHandle(*left);
Steve Block44f0eee2011-05-26 01:26:41 +01005808 i::Isolate* isolate = left_string->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01005809 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005810 LOG_API(isolate, "v8::String::Concat");
Steve Block3ce2e202009-11-05 08:53:23 +00005811 i::Handle<i::String> right_string = Utils::OpenHandle(*right);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005812 // If we are steering towards a range error, do not wait for the error to be
5813 // thrown, and return the null handle instead.
5814 if (left_string->length() + right_string->length() > i::String::kMaxLength) {
5815 return Local<String>();
5816 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005817 i::Handle<i::String> result = isolate->factory()->NewConsString(
5818 left_string, right_string).ToHandleChecked();
Steve Block3ce2e202009-11-05 08:53:23 +00005819 return Utils::ToLocal(result);
5820}
5821
5822
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005823MaybeLocal<String> v8::String::NewExternalTwoByte(
5824 Isolate* isolate, v8::String::ExternalStringResource* resource) {
5825 CHECK(resource && resource->data());
5826 // TODO(dcarney): throw a context free exception.
5827 if (resource->length() > static_cast<size_t>(i::String::kMaxLength)) {
5828 return MaybeLocal<String>();
5829 }
5830 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
5831 ENTER_V8(i_isolate);
5832 LOG_API(i_isolate, "String::NewExternalTwoByte");
5833 i::Handle<i::String> string = i_isolate->factory()
5834 ->NewExternalStringFromTwoByte(resource)
5835 .ToHandleChecked();
5836 i_isolate->heap()->RegisterExternalString(*string);
5837 return Utils::ToLocal(string);
Steve Blocka7e24c12009-10-30 11:49:00 +00005838}
5839
5840
Steve Blocka7e24c12009-10-30 11:49:00 +00005841Local<String> v8::String::NewExternal(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005842 Isolate* isolate, v8::String::ExternalStringResource* resource) {
5843 RETURN_TO_LOCAL_UNCHECKED(NewExternalTwoByte(isolate, resource), String);
5844}
5845
5846
5847MaybeLocal<String> v8::String::NewExternalOneByte(
5848 Isolate* isolate, v8::String::ExternalOneByteStringResource* resource) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005849 CHECK(resource && resource->data());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005850 // TODO(dcarney): throw a context free exception.
5851 if (resource->length() > static_cast<size_t>(i::String::kMaxLength)) {
5852 return MaybeLocal<String>();
5853 }
5854 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
5855 ENTER_V8(i_isolate);
5856 LOG_API(i_isolate, "String::NewExternalOneByte");
5857 i::Handle<i::String> string = i_isolate->factory()
5858 ->NewExternalStringFromOneByte(resource)
5859 .ToHandleChecked();
5860 i_isolate->heap()->RegisterExternalString(*string);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005861 return Utils::ToLocal(string);
Steve Blocka7e24c12009-10-30 11:49:00 +00005862}
5863
5864
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005865Local<String> v8::String::NewExternal(
5866 Isolate* isolate, v8::String::ExternalOneByteStringResource* resource) {
5867 RETURN_TO_LOCAL_UNCHECKED(NewExternalOneByte(isolate, resource), String);
5868}
5869
5870
Steve Blocka7e24c12009-10-30 11:49:00 +00005871bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005872 i::Handle<i::String> obj = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01005873 i::Isolate* isolate = obj->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005874 if (i::StringShape(*obj).IsExternal()) {
Steve Block44f0eee2011-05-26 01:26:41 +01005875 return false; // Already an external string.
5876 }
5877 ENTER_V8(isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005878 if (isolate->heap()->IsInGCPostProcessing()) {
5879 return false;
5880 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005881 CHECK(resource && resource->data());
5882
Steve Blocka7e24c12009-10-30 11:49:00 +00005883 bool result = obj->MakeExternal(resource);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005884 // Assert that if CanMakeExternal(), then externalizing actually succeeds.
5885 DCHECK(!CanMakeExternal() || result);
5886 if (result) {
5887 DCHECK(obj->IsExternalString());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005888 isolate->heap()->RegisterExternalString(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00005889 }
5890 return result;
5891}
5892
5893
Steve Blocka7e24c12009-10-30 11:49:00 +00005894bool v8::String::MakeExternal(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005895 v8::String::ExternalOneByteStringResource* resource) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005896 i::Handle<i::String> obj = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01005897 i::Isolate* isolate = obj->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005898 if (i::StringShape(*obj).IsExternal()) {
Steve Block44f0eee2011-05-26 01:26:41 +01005899 return false; // Already an external string.
5900 }
5901 ENTER_V8(isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005902 if (isolate->heap()->IsInGCPostProcessing()) {
5903 return false;
5904 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005905 CHECK(resource && resource->data());
5906
Steve Blocka7e24c12009-10-30 11:49:00 +00005907 bool result = obj->MakeExternal(resource);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005908 // Assert that if CanMakeExternal(), then externalizing actually succeeds.
5909 DCHECK(!CanMakeExternal() || result);
5910 if (result) {
5911 DCHECK(obj->IsExternalString());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005912 isolate->heap()->RegisterExternalString(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00005913 }
5914 return result;
5915}
5916
5917
5918bool v8::String::CanMakeExternal() {
Steve Blocka7e24c12009-10-30 11:49:00 +00005919 i::Handle<i::String> obj = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01005920 i::Isolate* isolate = obj->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005921
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005922 // Old space strings should be externalized.
5923 if (!isolate->heap()->new_space()->Contains(*obj)) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00005924 int size = obj->Size(); // Byte size of the original string.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005925 if (size <= i::ExternalString::kShortSize) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00005926 i::StringShape shape(*obj);
5927 return !shape.IsExternal();
5928}
5929
5930
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005931Isolate* v8::Object::GetIsolate() {
5932 i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate();
5933 return reinterpret_cast<Isolate*>(i_isolate);
5934}
5935
5936
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005937Local<v8::Object> v8::Object::New(Isolate* isolate) {
5938 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
5939 LOG_API(i_isolate, "Object::New");
5940 ENTER_V8(i_isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005941 i::Handle<i::JSObject> obj =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005942 i_isolate->factory()->NewJSObject(i_isolate->object_function());
Steve Blocka7e24c12009-10-30 11:49:00 +00005943 return Utils::ToLocal(obj);
5944}
5945
5946
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005947Local<v8::Value> v8::NumberObject::New(Isolate* isolate, double value) {
5948 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
5949 LOG_API(i_isolate, "NumberObject::New");
5950 ENTER_V8(i_isolate);
5951 i::Handle<i::Object> number = i_isolate->factory()->NewNumber(value);
5952 i::Handle<i::Object> obj =
5953 i::Object::ToObject(i_isolate, number).ToHandleChecked();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005954 return Utils::ToLocal(obj);
5955}
5956
5957
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005958double v8::NumberObject::ValueOf() const {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005959 i::Handle<i::Object> obj = Utils::OpenHandle(this);
5960 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005961 i::Isolate* isolate = jsvalue->GetIsolate();
5962 LOG_API(isolate, "NumberObject::NumberValue");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005963 return jsvalue->value()->Number();
5964}
5965
5966
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005967Local<v8::Value> v8::BooleanObject::New(Isolate* isolate, bool value) {
5968 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
5969 LOG_API(i_isolate, "BooleanObject::New");
5970 ENTER_V8(i_isolate);
5971 i::Handle<i::Object> boolean(value ? i_isolate->heap()->true_value()
5972 : i_isolate->heap()->false_value(),
5973 i_isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005974 i::Handle<i::Object> obj =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005975 i::Object::ToObject(i_isolate, boolean).ToHandleChecked();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005976 return Utils::ToLocal(obj);
5977}
5978
5979
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005980Local<v8::Value> v8::BooleanObject::New(bool value) {
5981 return New(Isolate::GetCurrent(), value);
5982}
5983
5984
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005985bool v8::BooleanObject::ValueOf() const {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005986 i::Handle<i::Object> obj = Utils::OpenHandle(this);
5987 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005988 i::Isolate* isolate = jsvalue->GetIsolate();
5989 LOG_API(isolate, "BooleanObject::BooleanValue");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005990 return jsvalue->value()->IsTrue();
5991}
5992
5993
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005994Local<v8::Value> v8::StringObject::New(Local<String> value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005995 i::Handle<i::String> string = Utils::OpenHandle(*value);
5996 i::Isolate* isolate = string->GetIsolate();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005997 LOG_API(isolate, "StringObject::New");
5998 ENTER_V8(isolate);
5999 i::Handle<i::Object> obj =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006000 i::Object::ToObject(isolate, string).ToHandleChecked();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006001 return Utils::ToLocal(obj);
6002}
6003
6004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006005Local<v8::String> v8::StringObject::ValueOf() const {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006006 i::Handle<i::Object> obj = Utils::OpenHandle(this);
6007 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006008 i::Isolate* isolate = jsvalue->GetIsolate();
6009 LOG_API(isolate, "StringObject::StringValue");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006010 return Utils::ToLocal(
6011 i::Handle<i::String>(i::String::cast(jsvalue->value())));
6012}
6013
6014
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006015Local<v8::Value> v8::SymbolObject::New(Isolate* isolate, Local<Symbol> value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006016 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6017 LOG_API(i_isolate, "SymbolObject::New");
6018 ENTER_V8(i_isolate);
6019 i::Handle<i::Object> obj = i::Object::ToObject(
6020 i_isolate, Utils::OpenHandle(*value)).ToHandleChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +00006021 return Utils::ToLocal(obj);
6022}
6023
6024
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006025Local<v8::Symbol> v8::SymbolObject::ValueOf() const {
6026 i::Handle<i::Object> obj = Utils::OpenHandle(this);
6027 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
6028 i::Isolate* isolate = jsvalue->GetIsolate();
6029 LOG_API(isolate, "SymbolObject::SymbolValue");
6030 return Utils::ToLocal(
6031 i::Handle<i::Symbol>(i::Symbol::cast(jsvalue->value())));
6032}
6033
6034
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006035MaybeLocal<v8::Value> v8::Date::New(Local<Context> context, double time) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006036 if (std::isnan(time)) {
6037 // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006038 time = std::numeric_limits<double>::quiet_NaN();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006039 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006040 PREPARE_FOR_EXECUTION(context, "Date::New", Value);
6041 Local<Value> result;
6042 has_pending_exception = !ToLocal<Value>(
6043 i::JSDate::New(isolate->date_function(), isolate->date_function(), time),
6044 &result);
6045 RETURN_ON_FAILED_EXECUTION(Value);
6046 RETURN_ESCAPED(result);
6047}
6048
6049
6050Local<v8::Value> v8::Date::New(Isolate* isolate, double time) {
6051 auto context = isolate->GetCurrentContext();
6052 RETURN_TO_LOCAL_UNCHECKED(New(context, time), Value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006053}
6054
6055
6056double v8::Date::ValueOf() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00006057 i::Handle<i::Object> obj = Utils::OpenHandle(this);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006058 i::Handle<i::JSDate> jsdate = i::Handle<i::JSDate>::cast(obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006059 i::Isolate* isolate = jsdate->GetIsolate();
6060 LOG_API(isolate, "Date::NumberValue");
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006061 return jsdate->value()->Number();
Steve Blocka7e24c12009-10-30 11:49:00 +00006062}
6063
6064
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006065void v8::Date::DateTimeConfigurationChangeNotification(Isolate* isolate) {
6066 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006067 LOG_API(i_isolate, "Date::DateTimeConfigurationChangeNotification");
6068 ENTER_V8(i_isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006069 i_isolate->date_cache()->ResetDateCache();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006070 if (!i_isolate->eternal_handles()->Exists(
6071 i::EternalHandles::DATE_CACHE_VERSION)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01006072 return;
6073 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006074 i::Handle<i::FixedArray> date_cache_version =
6075 i::Handle<i::FixedArray>::cast(i_isolate->eternal_handles()->GetSingleton(
6076 i::EternalHandles::DATE_CACHE_VERSION));
6077 DCHECK_EQ(1, date_cache_version->length());
6078 CHECK(date_cache_version->get(0)->IsSmi());
6079 date_cache_version->set(
6080 0,
6081 i::Smi::FromInt(i::Smi::cast(date_cache_version->get(0))->value() + 1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01006082}
6083
6084
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006085MaybeLocal<v8::RegExp> v8::RegExp::New(Local<Context> context,
6086 Local<String> pattern, Flags flags) {
6087 PREPARE_FOR_EXECUTION(context, "RegExp::New", RegExp);
6088 Local<v8::RegExp> result;
6089 has_pending_exception =
6090 !ToLocal<RegExp>(i::JSRegExp::New(Utils::OpenHandle(*pattern),
6091 static_cast<i::JSRegExp::Flags>(flags)),
6092 &result);
6093 RETURN_ON_FAILED_EXECUTION(RegExp);
6094 RETURN_ESCAPED(result);
Ben Murdochf87a2032010-10-22 12:50:53 +01006095}
6096
6097
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006098Local<v8::RegExp> v8::RegExp::New(Local<String> pattern, Flags flags) {
6099 auto isolate =
6100 reinterpret_cast<Isolate*>(Utils::OpenHandle(*pattern)->GetIsolate());
6101 auto context = isolate->GetCurrentContext();
6102 RETURN_TO_LOCAL_UNCHECKED(New(context, pattern, flags), RegExp);
Ben Murdochf87a2032010-10-22 12:50:53 +01006103}
6104
6105
6106Local<v8::String> v8::RegExp::GetSource() const {
Ben Murdochf87a2032010-10-22 12:50:53 +01006107 i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
6108 return Utils::ToLocal(i::Handle<i::String>(obj->Pattern()));
6109}
6110
6111
6112// Assert that the static flags cast in GetFlags is valid.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006113#define REGEXP_FLAG_ASSERT_EQ(flag) \
6114 STATIC_ASSERT(static_cast<int>(v8::RegExp::flag) == \
6115 static_cast<int>(i::JSRegExp::flag))
6116REGEXP_FLAG_ASSERT_EQ(kNone);
6117REGEXP_FLAG_ASSERT_EQ(kGlobal);
6118REGEXP_FLAG_ASSERT_EQ(kIgnoreCase);
6119REGEXP_FLAG_ASSERT_EQ(kMultiline);
6120REGEXP_FLAG_ASSERT_EQ(kSticky);
6121REGEXP_FLAG_ASSERT_EQ(kUnicode);
Ben Murdochf87a2032010-10-22 12:50:53 +01006122#undef REGEXP_FLAG_ASSERT_EQ
6123
6124v8::RegExp::Flags v8::RegExp::GetFlags() const {
Ben Murdochf87a2032010-10-22 12:50:53 +01006125 i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006126 return RegExp::Flags(static_cast<int>(obj->GetFlags()));
Ben Murdochf87a2032010-10-22 12:50:53 +01006127}
6128
6129
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006130Local<v8::Array> v8::Array::New(Isolate* isolate, int length) {
6131 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6132 LOG_API(i_isolate, "Array::New");
6133 ENTER_V8(i_isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01006134 int real_length = length > 0 ? length : 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006135 i::Handle<i::JSArray> obj = i_isolate->factory()->NewJSArray(real_length);
Ben Murdoch8b112d22011-06-08 16:22:53 +01006136 i::Handle<i::Object> length_obj =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006137 i_isolate->factory()->NewNumberFromInt(real_length);
Ben Murdoch8b112d22011-06-08 16:22:53 +01006138 obj->set_length(*length_obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00006139 return Utils::ToLocal(obj);
6140}
6141
6142
6143uint32_t v8::Array::Length() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00006144 i::Handle<i::JSArray> obj = Utils::OpenHandle(this);
6145 i::Object* length = obj->length();
6146 if (length->IsSmi()) {
6147 return i::Smi::cast(length)->value();
6148 } else {
6149 return static_cast<uint32_t>(length->Number());
6150 }
6151}
6152
6153
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006154MaybeLocal<Object> Array::CloneElementAt(Local<Context> context,
6155 uint32_t index) {
6156 PREPARE_FOR_EXECUTION(context, "v8::Array::CloneElementAt()", Object);
6157 auto self = Utils::OpenHandle(this);
6158 if (!self->HasFastObjectElements()) return Local<Object>();
Steve Blocka7e24c12009-10-30 11:49:00 +00006159 i::FixedArray* elms = i::FixedArray::cast(self->elements());
6160 i::Object* paragon = elms->get(index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006161 if (!paragon->IsJSObject()) return Local<Object>();
Steve Blocka7e24c12009-10-30 11:49:00 +00006162 i::Handle<i::JSObject> paragon_handle(i::JSObject::cast(paragon));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006163 Local<Object> result;
6164 has_pending_exception =
6165 !ToLocal<Object>(isolate->factory()->CopyJSObject(paragon_handle),
6166 &result);
6167 RETURN_ON_FAILED_EXECUTION(Object);
6168 RETURN_ESCAPED(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00006169}
6170
6171
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006172Local<Object> Array::CloneElementAt(uint32_t index) { return Local<Object>(); }
6173
6174
6175Local<v8::Map> v8::Map::New(Isolate* isolate) {
6176 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6177 LOG_API(i_isolate, "Map::New");
6178 ENTER_V8(i_isolate);
6179 i::Handle<i::JSMap> obj = i_isolate->factory()->NewJSMap();
6180 return Utils::ToLocal(obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006181}
6182
6183
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006184size_t v8::Map::Size() const {
6185 i::Handle<i::JSMap> obj = Utils::OpenHandle(this);
6186 return i::OrderedHashMap::cast(obj->table())->NumberOfElements();
6187}
6188
6189
6190void Map::Clear() {
6191 auto self = Utils::OpenHandle(this);
6192 i::Isolate* isolate = self->GetIsolate();
6193 LOG_API(isolate, "Map::Clear");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006194 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006195 i::JSMap::Clear(self);
6196}
6197
6198
6199MaybeLocal<Value> Map::Get(Local<Context> context, Local<Value> key) {
6200 PREPARE_FOR_EXECUTION(context, "Map::Get", Value);
6201 auto self = Utils::OpenHandle(this);
6202 Local<Value> result;
6203 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
6204 has_pending_exception =
6205 !ToLocal<Value>(i::Execution::Call(isolate, isolate->map_get(), self,
6206 arraysize(argv), argv),
6207 &result);
6208 RETURN_ON_FAILED_EXECUTION(Value);
6209 RETURN_ESCAPED(result);
6210}
6211
6212
6213MaybeLocal<Map> Map::Set(Local<Context> context, Local<Value> key,
6214 Local<Value> value) {
6215 PREPARE_FOR_EXECUTION(context, "Map::Set", Map);
6216 auto self = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006217 i::Handle<i::Object> result;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006218 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key),
6219 Utils::OpenHandle(*value)};
6220 has_pending_exception = !i::Execution::Call(isolate, isolate->map_set(), self,
6221 arraysize(argv), argv)
6222 .ToHandle(&result);
6223 RETURN_ON_FAILED_EXECUTION(Map);
6224 RETURN_ESCAPED(Local<Map>::Cast(Utils::ToLocal(result)));
6225}
6226
6227
6228Maybe<bool> Map::Has(Local<Context> context, Local<Value> key) {
6229 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "Map::Has", bool);
6230 auto self = Utils::OpenHandle(this);
6231 i::Handle<i::Object> result;
6232 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
6233 has_pending_exception = !i::Execution::Call(isolate, isolate->map_has(), self,
6234 arraysize(argv), argv)
6235 .ToHandle(&result);
6236 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
6237 return Just(result->IsTrue());
6238}
6239
6240
6241Maybe<bool> Map::Delete(Local<Context> context, Local<Value> key) {
6242 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "Map::Delete", bool);
6243 auto self = Utils::OpenHandle(this);
6244 i::Handle<i::Object> result;
6245 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
6246 has_pending_exception = !i::Execution::Call(isolate, isolate->map_delete(),
6247 self, arraysize(argv), argv)
6248 .ToHandle(&result);
6249 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
6250 return Just(result->IsTrue());
6251}
6252
6253
6254Local<Array> Map::AsArray() const {
6255 i::Handle<i::JSMap> obj = Utils::OpenHandle(this);
6256 i::Isolate* isolate = obj->GetIsolate();
6257 i::Factory* factory = isolate->factory();
6258 LOG_API(isolate, "Map::AsArray");
6259 ENTER_V8(isolate);
6260 i::Handle<i::OrderedHashMap> table(i::OrderedHashMap::cast(obj->table()));
6261 int size = table->NumberOfElements();
6262 int length = size * 2;
6263 i::Handle<i::FixedArray> result = factory->NewFixedArray(length);
6264 for (int i = 0; i < size; ++i) {
6265 if (table->KeyAt(i)->IsTheHole()) continue;
6266 result->set(i * 2, table->KeyAt(i));
6267 result->set(i * 2 + 1, table->ValueAt(i));
6268 }
6269 i::Handle<i::JSArray> result_array =
6270 factory->NewJSArrayWithElements(result, i::FAST_ELEMENTS, length);
6271 return Utils::ToLocal(result_array);
6272}
6273
6274
6275Local<v8::Set> v8::Set::New(Isolate* isolate) {
6276 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6277 LOG_API(i_isolate, "Set::New");
6278 ENTER_V8(i_isolate);
6279 i::Handle<i::JSSet> obj = i_isolate->factory()->NewJSSet();
6280 return Utils::ToLocal(obj);
6281}
6282
6283
6284size_t v8::Set::Size() const {
6285 i::Handle<i::JSSet> obj = Utils::OpenHandle(this);
6286 return i::OrderedHashSet::cast(obj->table())->NumberOfElements();
6287}
6288
6289
6290void Set::Clear() {
6291 auto self = Utils::OpenHandle(this);
6292 i::Isolate* isolate = self->GetIsolate();
6293 LOG_API(isolate, "Set::Clear");
6294 ENTER_V8(isolate);
6295 i::JSSet::Clear(self);
6296}
6297
6298
6299MaybeLocal<Set> Set::Add(Local<Context> context, Local<Value> key) {
6300 PREPARE_FOR_EXECUTION(context, "Set::Add", Set);
6301 auto self = Utils::OpenHandle(this);
6302 i::Handle<i::Object> result;
6303 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
6304 has_pending_exception = !i::Execution::Call(isolate, isolate->set_add(), self,
6305 arraysize(argv), argv)
6306 .ToHandle(&result);
6307 RETURN_ON_FAILED_EXECUTION(Set);
6308 RETURN_ESCAPED(Local<Set>::Cast(Utils::ToLocal(result)));
6309}
6310
6311
6312Maybe<bool> Set::Has(Local<Context> context, Local<Value> key) {
6313 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "Set::Has", bool);
6314 auto self = Utils::OpenHandle(this);
6315 i::Handle<i::Object> result;
6316 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
6317 has_pending_exception = !i::Execution::Call(isolate, isolate->set_has(), self,
6318 arraysize(argv), argv)
6319 .ToHandle(&result);
6320 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
6321 return Just(result->IsTrue());
6322}
6323
6324
6325Maybe<bool> Set::Delete(Local<Context> context, Local<Value> key) {
6326 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "Set::Delete", bool);
6327 auto self = Utils::OpenHandle(this);
6328 i::Handle<i::Object> result;
6329 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
6330 has_pending_exception = !i::Execution::Call(isolate, isolate->set_delete(),
6331 self, arraysize(argv), argv)
6332 .ToHandle(&result);
6333 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
6334 return Just(result->IsTrue());
6335}
6336
6337
6338Local<Array> Set::AsArray() const {
6339 i::Handle<i::JSSet> obj = Utils::OpenHandle(this);
6340 i::Isolate* isolate = obj->GetIsolate();
6341 i::Factory* factory = isolate->factory();
6342 LOG_API(isolate, "Set::AsArray");
6343 ENTER_V8(isolate);
6344 i::Handle<i::OrderedHashSet> table(i::OrderedHashSet::cast(obj->table()));
6345 int length = table->NumberOfElements();
6346 i::Handle<i::FixedArray> result = factory->NewFixedArray(length);
6347 for (int i = 0; i < length; ++i) {
6348 i::Object* key = table->KeyAt(i);
6349 if (!key->IsTheHole()) {
6350 result->set(i, key);
6351 }
6352 }
6353 i::Handle<i::JSArray> result_array =
6354 factory->NewJSArrayWithElements(result, i::FAST_ELEMENTS, length);
6355 return Utils::ToLocal(result_array);
6356}
6357
6358
6359MaybeLocal<Promise::Resolver> Promise::Resolver::New(Local<Context> context) {
6360 PREPARE_FOR_EXECUTION(context, "Promise::Resolver::New", Resolver);
6361 i::Handle<i::Object> result;
6362 has_pending_exception =
6363 !i::Execution::Call(isolate, isolate->promise_create(),
6364 isolate->factory()->undefined_value(), 0, NULL)
6365 .ToHandle(&result);
6366 RETURN_ON_FAILED_EXECUTION(Promise::Resolver);
6367 RETURN_ESCAPED(Local<Promise::Resolver>::Cast(Utils::ToLocal(result)));
6368}
6369
6370
6371Local<Promise::Resolver> Promise::Resolver::New(Isolate* isolate) {
6372 RETURN_TO_LOCAL_UNCHECKED(New(isolate->GetCurrentContext()),
6373 Promise::Resolver);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006374}
6375
6376
6377Local<Promise> Promise::Resolver::GetPromise() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006378 i::Handle<i::JSReceiver> promise = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006379 return Local<Promise>::Cast(Utils::ToLocal(promise));
6380}
6381
6382
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006383Maybe<bool> Promise::Resolver::Resolve(Local<Context> context,
6384 Local<Value> value) {
6385 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "Promise::Resolver::Resolve", bool);
6386 auto self = Utils::OpenHandle(this);
6387 i::Handle<i::Object> argv[] = {self, Utils::OpenHandle(*value)};
6388 has_pending_exception =
6389 i::Execution::Call(isolate, isolate->promise_resolve(),
6390 isolate->factory()->undefined_value(), arraysize(argv),
6391 argv)
6392 .is_null();
6393 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
6394 return Just(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006395}
6396
6397
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006398void Promise::Resolver::Resolve(Local<Value> value) {
6399 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
6400 USE(Resolve(context, value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006401}
6402
6403
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006404Maybe<bool> Promise::Resolver::Reject(Local<Context> context,
6405 Local<Value> value) {
6406 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "Promise::Resolver::Resolve", bool);
6407 auto self = Utils::OpenHandle(this);
6408 i::Handle<i::Object> argv[] = {self, Utils::OpenHandle(*value)};
6409 has_pending_exception =
6410 i::Execution::Call(isolate, isolate->promise_reject(),
6411 isolate->factory()->undefined_value(), arraysize(argv),
6412 argv)
6413 .is_null();
6414 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
6415 return Just(true);
6416}
6417
6418
6419void Promise::Resolver::Reject(Local<Value> value) {
6420 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
6421 USE(Reject(context, value));
6422}
6423
6424
6425namespace {
6426
6427MaybeLocal<Promise> DoChain(Value* value, Local<Context> context,
6428 Local<Function> handler) {
6429 PREPARE_FOR_EXECUTION(context, "Promise::Chain", Promise);
6430 auto self = Utils::OpenHandle(value);
6431 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*handler)};
6432 i::Handle<i::Object> result;
6433 has_pending_exception = !i::Execution::Call(isolate, isolate->promise_chain(),
6434 self, arraysize(argv), argv)
6435 .ToHandle(&result);
6436 RETURN_ON_FAILED_EXECUTION(Promise);
6437 RETURN_ESCAPED(Local<Promise>::Cast(Utils::ToLocal(result)));
6438}
6439
6440} // namespace
6441
6442
6443MaybeLocal<Promise> Promise::Chain(Local<Context> context,
6444 Local<Function> handler) {
6445 return DoChain(this, context, handler);
6446}
6447
6448
6449Local<Promise> Promise::Chain(Local<Function> handler) {
6450 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
6451 RETURN_TO_LOCAL_UNCHECKED(DoChain(this, context, handler), Promise);
6452}
6453
6454
6455MaybeLocal<Promise> Promise::Catch(Local<Context> context,
6456 Local<Function> handler) {
6457 PREPARE_FOR_EXECUTION(context, "Promise::Catch", Promise);
6458 auto self = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006459 i::Handle<i::Object> argv[] = { Utils::OpenHandle(*handler) };
6460 i::Handle<i::Object> result;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006461 has_pending_exception = !i::Execution::Call(isolate, isolate->promise_catch(),
6462 self, arraysize(argv), argv)
6463 .ToHandle(&result);
6464 RETURN_ON_FAILED_EXECUTION(Promise);
6465 RETURN_ESCAPED(Local<Promise>::Cast(Utils::ToLocal(result)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006466}
6467
6468
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006469Local<Promise> Promise::Catch(Local<Function> handler) {
6470 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
6471 RETURN_TO_LOCAL_UNCHECKED(Catch(context, handler), Promise);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006472}
6473
6474
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006475MaybeLocal<Promise> Promise::Then(Local<Context> context,
6476 Local<Function> handler) {
6477 PREPARE_FOR_EXECUTION(context, "Promise::Then", Promise);
6478 auto self = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006479 i::Handle<i::Object> argv[] = { Utils::OpenHandle(*handler) };
6480 i::Handle<i::Object> result;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006481 has_pending_exception = !i::Execution::Call(isolate, isolate->promise_then(),
6482 self, arraysize(argv), argv)
6483 .ToHandle(&result);
6484 RETURN_ON_FAILED_EXECUTION(Promise);
6485 RETURN_ESCAPED(Local<Promise>::Cast(Utils::ToLocal(result)));
6486}
6487
6488
6489Local<Promise> Promise::Then(Local<Function> handler) {
6490 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
6491 RETURN_TO_LOCAL_UNCHECKED(Then(context, handler), Promise);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006492}
6493
6494
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006495bool Promise::HasHandler() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006496 i::Handle<i::JSReceiver> promise = Utils::OpenHandle(this);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006497 i::Isolate* isolate = promise->GetIsolate();
6498 LOG_API(isolate, "Promise::HasRejectHandler");
6499 ENTER_V8(isolate);
6500 i::Handle<i::Symbol> key = isolate->factory()->promise_has_handler_symbol();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006501 return i::JSReceiver::GetDataProperty(promise, key)->IsTrue();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006502}
6503
6504
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006505Local<Object> Proxy::GetTarget() {
6506 i::Handle<i::JSProxy> self = Utils::OpenHandle(this);
6507 i::Handle<i::JSReceiver> target(self->target());
6508 return Utils::ToLocal(target);
6509}
6510
6511
6512Local<Value> Proxy::GetHandler() {
6513 i::Handle<i::JSProxy> self = Utils::OpenHandle(this);
6514 i::Handle<i::Object> handler(self->handler(), self->GetIsolate());
6515 return Utils::ToLocal(handler);
6516}
6517
6518
6519bool Proxy::IsRevoked() {
6520 i::Handle<i::JSProxy> self = Utils::OpenHandle(this);
6521 return self->IsRevoked();
6522}
6523
6524
6525void Proxy::Revoke() {
6526 i::Handle<i::JSProxy> self = Utils::OpenHandle(this);
6527 i::JSProxy::Revoke(self);
6528}
6529
6530
6531MaybeLocal<Proxy> Proxy::New(Local<Context> context, Local<Object> local_target,
6532 Local<Object> local_handler) {
6533 PREPARE_FOR_EXECUTION(context, "Proxy::New", Proxy);
6534 i::Handle<i::JSReceiver> target = Utils::OpenHandle(*local_target);
6535 i::Handle<i::JSReceiver> handler = Utils::OpenHandle(*local_handler);
6536 Local<Proxy> result;
6537 has_pending_exception =
6538 !ToLocal<Proxy>(i::JSProxy::New(isolate, target, handler), &result);
6539 RETURN_ON_FAILED_EXECUTION(Proxy);
6540 RETURN_ESCAPED(result);
6541}
6542
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006543bool v8::ArrayBuffer::IsExternal() const {
6544 return Utils::OpenHandle(this)->is_external();
6545}
6546
6547
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006548bool v8::ArrayBuffer::IsNeuterable() const {
6549 return Utils::OpenHandle(this)->is_neuterable();
6550}
6551
6552
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006553v8::ArrayBuffer::Contents v8::ArrayBuffer::Externalize() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006554 i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
6555 i::Isolate* isolate = self->GetIsolate();
6556 Utils::ApiCheck(!self->is_external(), "v8::ArrayBuffer::Externalize",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006557 "ArrayBuffer already externalized");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006558 self->set_is_external(true);
6559 isolate->heap()->UnregisterArrayBuffer(*self);
6560
6561 return GetContents();
6562}
6563
6564
6565v8::ArrayBuffer::Contents v8::ArrayBuffer::GetContents() {
6566 i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
6567 size_t byte_length = static_cast<size_t>(self->byte_length()->Number());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006568 Contents contents;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006569 contents.data_ = self->backing_store();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006570 contents.byte_length_ = byte_length;
6571 return contents;
6572}
6573
6574
6575void v8::ArrayBuffer::Neuter() {
6576 i::Handle<i::JSArrayBuffer> obj = Utils::OpenHandle(this);
6577 i::Isolate* isolate = obj->GetIsolate();
6578 Utils::ApiCheck(obj->is_external(),
6579 "v8::ArrayBuffer::Neuter",
6580 "Only externalized ArrayBuffers can be neutered");
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006581 Utils::ApiCheck(obj->is_neuterable(), "v8::ArrayBuffer::Neuter",
6582 "Only neuterable ArrayBuffers can be neutered");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006583 LOG_API(obj->GetIsolate(), "v8::ArrayBuffer::Neuter()");
6584 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006585 obj->Neuter();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006586}
6587
6588
6589size_t v8::ArrayBuffer::ByteLength() const {
6590 i::Handle<i::JSArrayBuffer> obj = Utils::OpenHandle(this);
6591 return static_cast<size_t>(obj->byte_length()->Number());
6592}
6593
6594
6595Local<ArrayBuffer> v8::ArrayBuffer::New(Isolate* isolate, size_t byte_length) {
6596 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6597 LOG_API(i_isolate, "v8::ArrayBuffer::New(size_t)");
6598 ENTER_V8(i_isolate);
6599 i::Handle<i::JSArrayBuffer> obj =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006600 i_isolate->factory()->NewJSArrayBuffer(i::SharedFlag::kNotShared);
6601 i::JSArrayBuffer::SetupAllocatingData(obj, i_isolate, byte_length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006602 return Utils::ToLocal(obj);
6603}
6604
6605
6606Local<ArrayBuffer> v8::ArrayBuffer::New(Isolate* isolate, void* data,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006607 size_t byte_length,
6608 ArrayBufferCreationMode mode) {
6609 // Embedders must guarantee that the external backing store is valid.
6610 CHECK(byte_length == 0 || data != NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006611 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6612 LOG_API(i_isolate, "v8::ArrayBuffer::New(void*, size_t)");
6613 ENTER_V8(i_isolate);
6614 i::Handle<i::JSArrayBuffer> obj =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006615 i_isolate->factory()->NewJSArrayBuffer(i::SharedFlag::kNotShared);
6616 i::JSArrayBuffer::Setup(obj, i_isolate,
6617 mode == ArrayBufferCreationMode::kExternalized, data,
6618 byte_length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006619 return Utils::ToLocal(obj);
6620}
6621
6622
6623Local<ArrayBuffer> v8::ArrayBufferView::Buffer() {
6624 i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this);
6625 i::Handle<i::JSArrayBuffer> buffer;
6626 if (obj->IsJSDataView()) {
6627 i::Handle<i::JSDataView> data_view(i::JSDataView::cast(*obj));
6628 DCHECK(data_view->buffer()->IsJSArrayBuffer());
6629 buffer = i::handle(i::JSArrayBuffer::cast(data_view->buffer()));
6630 } else {
6631 DCHECK(obj->IsJSTypedArray());
6632 buffer = i::JSTypedArray::cast(*obj)->GetBuffer();
6633 }
6634 return Utils::ToLocal(buffer);
6635}
6636
6637
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006638size_t v8::ArrayBufferView::CopyContents(void* dest, size_t byte_length) {
6639 i::Handle<i::JSArrayBufferView> self = Utils::OpenHandle(this);
6640 i::Isolate* isolate = self->GetIsolate();
6641 size_t byte_offset = i::NumberToSize(isolate, self->byte_offset());
6642 size_t bytes_to_copy =
6643 i::Min(byte_length, i::NumberToSize(isolate, self->byte_length()));
6644 if (bytes_to_copy) {
6645 i::DisallowHeapAllocation no_gc;
6646 i::Handle<i::JSArrayBuffer> buffer(i::JSArrayBuffer::cast(self->buffer()));
6647 const char* source = reinterpret_cast<char*>(buffer->backing_store());
6648 if (source == nullptr) {
6649 DCHECK(self->IsJSTypedArray());
6650 i::Handle<i::JSTypedArray> typed_array(i::JSTypedArray::cast(*self));
6651 i::Handle<i::FixedTypedArrayBase> fixed_array(
6652 i::FixedTypedArrayBase::cast(typed_array->elements()));
6653 source = reinterpret_cast<char*>(fixed_array->DataPtr());
6654 }
6655 memcpy(dest, source + byte_offset, bytes_to_copy);
6656 }
6657 return bytes_to_copy;
6658}
6659
6660
6661bool v8::ArrayBufferView::HasBuffer() const {
6662 i::Handle<i::JSArrayBufferView> self = Utils::OpenHandle(this);
6663 i::Handle<i::JSArrayBuffer> buffer(i::JSArrayBuffer::cast(self->buffer()));
6664 return buffer->backing_store() != nullptr;
6665}
6666
6667
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006668size_t v8::ArrayBufferView::ByteOffset() {
6669 i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this);
6670 return static_cast<size_t>(obj->byte_offset()->Number());
6671}
6672
6673
6674size_t v8::ArrayBufferView::ByteLength() {
6675 i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this);
6676 return static_cast<size_t>(obj->byte_length()->Number());
6677}
6678
6679
6680size_t v8::TypedArray::Length() {
6681 i::Handle<i::JSTypedArray> obj = Utils::OpenHandle(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006682 return static_cast<size_t>(obj->length_value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006683}
6684
6685
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006686#define TYPED_ARRAY_NEW(Type, type, TYPE, ctype, size) \
6687 Local<Type##Array> Type##Array::New(Local<ArrayBuffer> array_buffer, \
6688 size_t byte_offset, size_t length) { \
6689 i::Isolate* isolate = Utils::OpenHandle(*array_buffer)->GetIsolate(); \
6690 LOG_API(isolate, \
6691 "v8::" #Type "Array::New(Local<ArrayBuffer>, size_t, size_t)"); \
6692 ENTER_V8(isolate); \
6693 if (!Utils::ApiCheck(length <= static_cast<size_t>(i::Smi::kMaxValue), \
6694 "v8::" #Type \
6695 "Array::New(Local<ArrayBuffer>, size_t, size_t)", \
6696 "length exceeds max allowed value")) { \
6697 return Local<Type##Array>(); \
6698 } \
6699 i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*array_buffer); \
6700 i::Handle<i::JSTypedArray> obj = isolate->factory()->NewJSTypedArray( \
6701 i::kExternal##Type##Array, buffer, byte_offset, length); \
6702 return Utils::ToLocal##Type##Array(obj); \
6703 } \
6704 Local<Type##Array> Type##Array::New( \
6705 Local<SharedArrayBuffer> shared_array_buffer, size_t byte_offset, \
6706 size_t length) { \
6707 CHECK(i::FLAG_harmony_sharedarraybuffer); \
6708 i::Isolate* isolate = \
6709 Utils::OpenHandle(*shared_array_buffer)->GetIsolate(); \
6710 LOG_API(isolate, "v8::" #Type \
6711 "Array::New(Local<SharedArrayBuffer>, size_t, size_t)"); \
6712 ENTER_V8(isolate); \
6713 if (!Utils::ApiCheck( \
6714 length <= static_cast<size_t>(i::Smi::kMaxValue), \
6715 "v8::" #Type \
6716 "Array::New(Local<SharedArrayBuffer>, size_t, size_t)", \
6717 "length exceeds max allowed value")) { \
6718 return Local<Type##Array>(); \
6719 } \
6720 i::Handle<i::JSArrayBuffer> buffer = \
6721 Utils::OpenHandle(*shared_array_buffer); \
6722 i::Handle<i::JSTypedArray> obj = isolate->factory()->NewJSTypedArray( \
6723 i::kExternal##Type##Array, buffer, byte_offset, length); \
6724 return Utils::ToLocal##Type##Array(obj); \
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006725 }
6726
6727
6728TYPED_ARRAYS(TYPED_ARRAY_NEW)
6729#undef TYPED_ARRAY_NEW
6730
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006731Local<DataView> DataView::New(Local<ArrayBuffer> array_buffer,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006732 size_t byte_offset, size_t byte_length) {
6733 i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*array_buffer);
6734 i::Isolate* isolate = buffer->GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006735 LOG_API(isolate, "v8::DataView::New(Local<ArrayBuffer>, size_t, size_t)");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006736 ENTER_V8(isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006737 i::Handle<i::JSDataView> obj =
6738 isolate->factory()->NewJSDataView(buffer, byte_offset, byte_length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006739 return Utils::ToLocal(obj);
6740}
6741
6742
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006743Local<DataView> DataView::New(Local<SharedArrayBuffer> shared_array_buffer,
6744 size_t byte_offset, size_t byte_length) {
6745 CHECK(i::FLAG_harmony_sharedarraybuffer);
6746 i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*shared_array_buffer);
6747 i::Isolate* isolate = buffer->GetIsolate();
6748 LOG_API(isolate,
6749 "v8::DataView::New(Local<SharedArrayBuffer>, size_t, size_t)");
6750 ENTER_V8(isolate);
6751 i::Handle<i::JSDataView> obj =
6752 isolate->factory()->NewJSDataView(buffer, byte_offset, byte_length);
6753 return Utils::ToLocal(obj);
6754}
6755
6756
6757bool v8::SharedArrayBuffer::IsExternal() const {
6758 return Utils::OpenHandle(this)->is_external();
6759}
6760
6761
6762v8::SharedArrayBuffer::Contents v8::SharedArrayBuffer::Externalize() {
6763 i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
6764 i::Isolate* isolate = self->GetIsolate();
6765 Utils::ApiCheck(!self->is_external(), "v8::SharedArrayBuffer::Externalize",
6766 "SharedArrayBuffer already externalized");
6767 self->set_is_external(true);
6768 isolate->heap()->UnregisterArrayBuffer(*self);
6769 return GetContents();
6770}
6771
6772
6773v8::SharedArrayBuffer::Contents v8::SharedArrayBuffer::GetContents() {
6774 i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
6775 size_t byte_length = static_cast<size_t>(self->byte_length()->Number());
6776 Contents contents;
6777 contents.data_ = self->backing_store();
6778 contents.byte_length_ = byte_length;
6779 return contents;
6780}
6781
6782
6783size_t v8::SharedArrayBuffer::ByteLength() const {
6784 i::Handle<i::JSArrayBuffer> obj = Utils::OpenHandle(this);
6785 return static_cast<size_t>(obj->byte_length()->Number());
6786}
6787
6788
6789Local<SharedArrayBuffer> v8::SharedArrayBuffer::New(Isolate* isolate,
6790 size_t byte_length) {
6791 CHECK(i::FLAG_harmony_sharedarraybuffer);
6792 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6793 LOG_API(i_isolate, "v8::SharedArrayBuffer::New(size_t)");
6794 ENTER_V8(i_isolate);
6795 i::Handle<i::JSArrayBuffer> obj =
6796 i_isolate->factory()->NewJSArrayBuffer(i::SharedFlag::kShared);
6797 i::JSArrayBuffer::SetupAllocatingData(obj, i_isolate, byte_length, true,
6798 i::SharedFlag::kShared);
6799 return Utils::ToLocalShared(obj);
6800}
6801
6802
6803Local<SharedArrayBuffer> v8::SharedArrayBuffer::New(
6804 Isolate* isolate, void* data, size_t byte_length,
6805 ArrayBufferCreationMode mode) {
6806 CHECK(i::FLAG_harmony_sharedarraybuffer);
6807 // Embedders must guarantee that the external backing store is valid.
6808 CHECK(byte_length == 0 || data != NULL);
6809 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6810 LOG_API(i_isolate, "v8::SharedArrayBuffer::New(void*, size_t)");
6811 ENTER_V8(i_isolate);
6812 i::Handle<i::JSArrayBuffer> obj =
6813 i_isolate->factory()->NewJSArrayBuffer(i::SharedFlag::kShared);
6814 i::JSArrayBuffer::Setup(obj, i_isolate,
6815 mode == ArrayBufferCreationMode::kExternalized, data,
6816 byte_length, i::SharedFlag::kShared);
6817 return Utils::ToLocalShared(obj);
6818}
6819
6820
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006821Local<Symbol> v8::Symbol::New(Isolate* isolate, Local<String> name) {
6822 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6823 LOG_API(i_isolate, "Symbol::New()");
6824 ENTER_V8(i_isolate);
6825 i::Handle<i::Symbol> result = i_isolate->factory()->NewSymbol();
6826 if (!name.IsEmpty()) result->set_name(*Utils::OpenHandle(*name));
Steve Blocka7e24c12009-10-30 11:49:00 +00006827 return Utils::ToLocal(result);
6828}
6829
6830
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006831static i::Handle<i::Symbol> SymbolFor(i::Isolate* isolate,
6832 i::Handle<i::String> name,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006833 i::Handle<i::String> part,
6834 bool private_symbol) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006835 i::Handle<i::JSObject> registry = isolate->GetSymbolRegistry();
6836 i::Handle<i::JSObject> symbols =
6837 i::Handle<i::JSObject>::cast(
6838 i::Object::GetPropertyOrElement(registry, part).ToHandleChecked());
6839 i::Handle<i::Object> symbol =
6840 i::Object::GetPropertyOrElement(symbols, name).ToHandleChecked();
6841 if (!symbol->IsSymbol()) {
6842 DCHECK(symbol->IsUndefined());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006843 if (private_symbol)
6844 symbol = isolate->factory()->NewPrivateSymbol();
6845 else
6846 symbol = isolate->factory()->NewSymbol();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006847 i::Handle<i::Symbol>::cast(symbol)->set_name(*name);
6848 i::JSObject::SetProperty(symbols, name, symbol, i::STRICT).Assert();
Steve Blockd0582a62009-12-15 09:54:21 +00006849 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006850 return i::Handle<i::Symbol>::cast(symbol);
6851}
6852
6853
6854Local<Symbol> v8::Symbol::For(Isolate* isolate, Local<String> name) {
6855 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6856 i::Handle<i::String> i_name = Utils::OpenHandle(*name);
6857 i::Handle<i::String> part = i_isolate->factory()->for_string();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006858 return Utils::ToLocal(SymbolFor(i_isolate, i_name, part, false));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006859}
6860
6861
6862Local<Symbol> v8::Symbol::ForApi(Isolate* isolate, Local<String> name) {
6863 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6864 i::Handle<i::String> i_name = Utils::OpenHandle(*name);
6865 i::Handle<i::String> part = i_isolate->factory()->for_api_string();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006866 return Utils::ToLocal(SymbolFor(i_isolate, i_name, part, false));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006867}
6868
6869
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006870Local<Symbol> v8::Symbol::GetIterator(Isolate* isolate) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006871 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6872 return Utils::ToLocal(i_isolate->factory()->iterator_symbol());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006873}
6874
6875
6876Local<Symbol> v8::Symbol::GetUnscopables(Isolate* isolate) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006877 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6878 return Utils::ToLocal(i_isolate->factory()->unscopables_symbol());
6879}
6880
6881
6882Local<Symbol> v8::Symbol::GetToStringTag(Isolate* isolate) {
6883 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6884 return Utils::ToLocal(i_isolate->factory()->to_string_tag_symbol());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006885}
6886
6887
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006888Local<Symbol> v8::Symbol::GetIsConcatSpreadable(Isolate* isolate) {
6889 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6890 return Utils::ToLocal(i_isolate->factory()->is_concat_spreadable_symbol());
6891}
6892
6893
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006894Local<Private> v8::Private::New(Isolate* isolate, Local<String> name) {
6895 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6896 LOG_API(i_isolate, "Private::New()");
6897 ENTER_V8(i_isolate);
6898 i::Handle<i::Symbol> symbol = i_isolate->factory()->NewPrivateSymbol();
6899 if (!name.IsEmpty()) symbol->set_name(*Utils::OpenHandle(*name));
6900 Local<Symbol> result = Utils::ToLocal(symbol);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006901 return v8::Local<Private>(reinterpret_cast<Private*>(*result));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006902}
6903
6904
6905Local<Private> v8::Private::ForApi(Isolate* isolate, Local<String> name) {
6906 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6907 i::Handle<i::String> i_name = Utils::OpenHandle(*name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006908 i::Handle<i::String> part = i_isolate->factory()->private_api_string();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006909 Local<Symbol> result =
6910 Utils::ToLocal(SymbolFor(i_isolate, i_name, part, true));
6911 return v8::Local<Private>(reinterpret_cast<Private*>(*result));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006912}
6913
6914
6915Local<Number> v8::Number::New(Isolate* isolate, double value) {
6916 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006917 if (std::isnan(value)) {
6918 // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006919 value = std::numeric_limits<double>::quiet_NaN();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006920 }
6921 ENTER_V8(internal_isolate);
6922 i::Handle<i::Object> result = internal_isolate->factory()->NewNumber(value);
Steve Blocka7e24c12009-10-30 11:49:00 +00006923 return Utils::NumberToLocal(result);
6924}
6925
6926
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006927Local<Integer> v8::Integer::New(Isolate* isolate, int32_t value) {
6928 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00006929 if (i::Smi::IsValid(value)) {
Steve Block44f0eee2011-05-26 01:26:41 +01006930 return Utils::IntegerToLocal(i::Handle<i::Object>(i::Smi::FromInt(value),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006931 internal_isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +00006932 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006933 ENTER_V8(internal_isolate);
6934 i::Handle<i::Object> result = internal_isolate->factory()->NewNumber(value);
Steve Blocka7e24c12009-10-30 11:49:00 +00006935 return Utils::IntegerToLocal(result);
6936}
6937
6938
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006939Local<Integer> v8::Integer::NewFromUnsigned(Isolate* isolate, uint32_t value) {
6940 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
Steve Block3ce2e202009-11-05 08:53:23 +00006941 bool fits_into_int32_t = (value & (1 << 31)) == 0;
6942 if (fits_into_int32_t) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006943 return Integer::New(isolate, static_cast<int32_t>(value));
Steve Block3ce2e202009-11-05 08:53:23 +00006944 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006945 ENTER_V8(internal_isolate);
6946 i::Handle<i::Object> result = internal_isolate->factory()->NewNumber(value);
Steve Block3ce2e202009-11-05 08:53:23 +00006947 return Utils::IntegerToLocal(result);
6948}
6949
6950
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006951void Isolate::ReportExternalAllocationLimitReached() {
6952 i::Heap* heap = reinterpret_cast<i::Isolate*>(this)->heap();
6953 if (heap->gc_state() != i::Heap::NOT_IN_GC) return;
6954 heap->ReportExternalMemoryPressure(
6955 "external memory allocation limit reached.");
Steve Blocka7e24c12009-10-30 11:49:00 +00006956}
6957
6958
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006959HeapProfiler* Isolate::GetHeapProfiler() {
6960 i::HeapProfiler* heap_profiler =
6961 reinterpret_cast<i::Isolate*>(this)->heap_profiler();
6962 return reinterpret_cast<HeapProfiler*>(heap_profiler);
Steve Blocka7e24c12009-10-30 11:49:00 +00006963}
6964
6965
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006966CpuProfiler* Isolate::GetCpuProfiler() {
6967 i::CpuProfiler* cpu_profiler =
6968 reinterpret_cast<i::Isolate*>(this)->cpu_profiler();
6969 return reinterpret_cast<CpuProfiler*>(cpu_profiler);
6970}
6971
6972
6973bool Isolate::InContext() {
6974 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
6975 return isolate->context() != NULL;
6976}
6977
6978
6979v8::Local<v8::Context> Isolate::GetCurrentContext() {
6980 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
6981 i::Context* context = isolate->context();
6982 if (context == NULL) return Local<Context>();
6983 i::Context* native_context = context->native_context();
6984 if (native_context == NULL) return Local<Context>();
6985 return Utils::ToLocal(i::Handle<i::Context>(native_context));
6986}
6987
6988
6989v8::Local<v8::Context> Isolate::GetCallingContext() {
6990 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
6991 i::Handle<i::Object> calling = isolate->GetCallingNativeContext();
6992 if (calling.is_null()) return Local<Context>();
6993 return Utils::ToLocal(i::Handle<i::Context>::cast(calling));
6994}
6995
6996
6997v8::Local<v8::Context> Isolate::GetEnteredContext() {
6998 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
6999 i::Handle<i::Object> last =
7000 isolate->handle_scope_implementer()->LastEnteredContext();
7001 if (last.is_null()) return Local<Context>();
7002 return Utils::ToLocal(i::Handle<i::Context>::cast(last));
7003}
7004
7005
7006v8::Local<Value> Isolate::ThrowException(v8::Local<v8::Value> value) {
7007 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7008 ENTER_V8(isolate);
7009 // If we're passed an empty handle, we throw an undefined exception
7010 // to deal more gracefully with out of memory situations.
7011 if (value.IsEmpty()) {
7012 isolate->ScheduleThrow(isolate->heap()->undefined_value());
7013 } else {
7014 isolate->ScheduleThrow(*Utils::OpenHandle(*value));
Steve Block44f0eee2011-05-26 01:26:41 +01007015 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007016 return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +00007017}
7018
7019
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007020void Isolate::SetObjectGroupId(internal::Object** object, UniqueId id) {
7021 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(this);
7022 internal_isolate->global_handles()->SetObjectGroupId(
7023 v8::internal::Handle<v8::internal::Object>(object).location(),
7024 id);
Steve Blocka7e24c12009-10-30 11:49:00 +00007025}
7026
7027
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007028void Isolate::SetReferenceFromGroup(UniqueId id, internal::Object** object) {
7029 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(this);
7030 internal_isolate->global_handles()->SetReferenceFromGroup(
7031 id,
7032 v8::internal::Handle<v8::internal::Object>(object).location());
7033}
7034
7035
7036void Isolate::SetReference(internal::Object** parent,
7037 internal::Object** child) {
7038 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(this);
7039 i::Object** parent_location =
7040 v8::internal::Handle<v8::internal::Object>(parent).location();
7041 internal_isolate->global_handles()->SetReference(
7042 reinterpret_cast<i::HeapObject**>(parent_location),
7043 v8::internal::Handle<v8::internal::Object>(child).location());
7044}
7045
7046
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007047void Isolate::AddGCPrologueCallback(GCCallback callback, GCType gc_type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007048 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7049 isolate->heap()->AddGCPrologueCallback(callback, gc_type);
7050}
7051
7052
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007053void Isolate::RemoveGCPrologueCallback(GCCallback callback) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007054 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7055 isolate->heap()->RemoveGCPrologueCallback(callback);
7056}
7057
7058
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007059void Isolate::AddGCEpilogueCallback(GCCallback callback, GCType gc_type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007060 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7061 isolate->heap()->AddGCEpilogueCallback(callback, gc_type);
7062}
7063
7064
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007065void Isolate::RemoveGCEpilogueCallback(GCCallback callback) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007066 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7067 isolate->heap()->RemoveGCEpilogueCallback(callback);
Steve Blocka7e24c12009-10-30 11:49:00 +00007068}
7069
7070
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007071void V8::AddGCPrologueCallback(GCCallback callback, GCType gc_type) {
Steve Block44f0eee2011-05-26 01:26:41 +01007072 i::Isolate* isolate = i::Isolate::Current();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007073 isolate->heap()->AddGCPrologueCallback(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007074 reinterpret_cast<v8::Isolate::GCCallback>(callback), gc_type, false);
Steve Block6ded16b2010-05-10 14:33:55 +01007075}
7076
7077
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007078void V8::AddGCEpilogueCallback(GCCallback callback, GCType gc_type) {
Steve Block44f0eee2011-05-26 01:26:41 +01007079 i::Isolate* isolate = i::Isolate::Current();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007080 isolate->heap()->AddGCEpilogueCallback(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007081 reinterpret_cast<v8::Isolate::GCCallback>(callback), gc_type, false);
Steve Block6ded16b2010-05-10 14:33:55 +01007082}
7083
7084
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007085void Isolate::AddMemoryAllocationCallback(MemoryAllocationCallback callback,
7086 ObjectSpace space,
7087 AllocationAction action) {
7088 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
Steve Block44f0eee2011-05-26 01:26:41 +01007089 isolate->memory_allocator()->AddMemoryAllocationCallback(
7090 callback, space, action);
Iain Merrick9ac36c92010-09-13 15:29:50 +01007091}
7092
7093
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007094void Isolate::RemoveMemoryAllocationCallback(
7095 MemoryAllocationCallback callback) {
7096 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
Steve Block44f0eee2011-05-26 01:26:41 +01007097 isolate->memory_allocator()->RemoveMemoryAllocationCallback(
7098 callback);
Iain Merrick9ac36c92010-09-13 15:29:50 +01007099}
7100
7101
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007102void Isolate::TerminateExecution() {
7103 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7104 isolate->stack_guard()->RequestTerminateExecution();
Steve Blocka7e24c12009-10-30 11:49:00 +00007105}
7106
7107
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007108bool Isolate::IsExecutionTerminating() {
7109 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7110 return IsExecutionTerminatingCheck(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01007111}
7112
7113
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007114void Isolate::CancelTerminateExecution() {
7115 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7116 isolate->stack_guard()->ClearTerminateExecution();
7117 isolate->CancelTerminateExecution();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007118}
7119
7120
7121void Isolate::RequestInterrupt(InterruptCallback callback, void* data) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007122 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7123 isolate->RequestInterrupt(callback, data);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007124}
7125
7126
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007127void Isolate::RequestGarbageCollectionForTesting(GarbageCollectionType type) {
7128 CHECK(i::FLAG_expose_gc);
7129 if (type == kMinorGarbageCollection) {
7130 reinterpret_cast<i::Isolate*>(this)->heap()->CollectGarbage(
7131 i::NEW_SPACE, "Isolate::RequestGarbageCollection",
7132 kGCCallbackFlagForced);
7133 } else {
7134 DCHECK_EQ(kFullGarbageCollection, type);
7135 reinterpret_cast<i::Isolate*>(this)->heap()->CollectAllGarbage(
7136 i::Heap::kAbortIncrementalMarkingMask,
7137 "Isolate::RequestGarbageCollection", kGCCallbackFlagForced);
7138 }
7139}
7140
7141
Steve Block44f0eee2011-05-26 01:26:41 +01007142Isolate* Isolate::GetCurrent() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007143 i::Isolate* isolate = i::Isolate::Current();
Steve Block44f0eee2011-05-26 01:26:41 +01007144 return reinterpret_cast<Isolate*>(isolate);
7145}
7146
7147
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007148Isolate* Isolate::New(const Isolate::CreateParams& params) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007149 i::Isolate* isolate = new i::Isolate(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007150 Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007151 CHECK(params.array_buffer_allocator != NULL);
7152 isolate->set_array_buffer_allocator(params.array_buffer_allocator);
7153 if (params.snapshot_blob != NULL) {
7154 isolate->set_snapshot_blob(params.snapshot_blob);
7155 } else {
7156 isolate->set_snapshot_blob(i::Snapshot::DefaultSnapshotBlob());
7157 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007158 if (params.entry_hook) {
7159 isolate->set_function_entry_hook(params.entry_hook);
7160 }
7161 if (params.code_event_handler) {
7162 isolate->InitializeLoggingAndCounters();
7163 isolate->logger()->SetCodeEventHandler(kJitCodeEventDefault,
7164 params.code_event_handler);
7165 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007166 if (params.counter_lookup_callback) {
7167 v8_isolate->SetCounterFunction(params.counter_lookup_callback);
7168 }
7169
7170 if (params.create_histogram_callback) {
7171 v8_isolate->SetCreateHistogramFunction(params.create_histogram_callback);
7172 }
7173
7174 if (params.add_histogram_sample_callback) {
7175 v8_isolate->SetAddHistogramSampleFunction(
7176 params.add_histogram_sample_callback);
7177 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007178 SetResourceConstraints(isolate, params.constraints);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007179 // TODO(jochen): Once we got rid of Isolate::Current(), we can remove this.
7180 Isolate::Scope isolate_scope(v8_isolate);
7181 if (params.entry_hook || !i::Snapshot::Initialize(isolate)) {
7182 // If the isolate has a function entry hook, it needs to re-build all its
7183 // code stubs with entry hooks embedded, so don't deserialize a snapshot.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007184 if (i::Snapshot::EmbedsScript(isolate)) {
7185 // If the snapshot embeds a script, we cannot initialize the isolate
7186 // without the snapshot as a fallback. This is unlikely to happen though.
7187 V8_Fatal(__FILE__, __LINE__,
7188 "Initializing isolate from custom startup snapshot failed");
7189 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007190 isolate->Init(NULL);
7191 }
7192 return v8_isolate;
Steve Block44f0eee2011-05-26 01:26:41 +01007193}
7194
7195
7196void Isolate::Dispose() {
7197 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007198 if (!Utils::ApiCheck(!isolate->IsInUse(),
7199 "v8::Isolate::Dispose()",
7200 "Disposing the isolate that is entered by a thread.")) {
Steve Block44f0eee2011-05-26 01:26:41 +01007201 return;
Steve Block6ded16b2010-05-10 14:33:55 +01007202 }
Steve Block44f0eee2011-05-26 01:26:41 +01007203 isolate->TearDown();
7204}
7205
7206
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007207void Isolate::DiscardThreadSpecificMetadata() {
7208 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7209 isolate->DiscardPerThreadDataForThisThread();
7210}
7211
7212
Steve Block44f0eee2011-05-26 01:26:41 +01007213void Isolate::Enter() {
7214 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7215 isolate->Enter();
7216}
7217
7218
7219void Isolate::Exit() {
7220 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7221 isolate->Exit();
Steve Block6ded16b2010-05-10 14:33:55 +01007222}
7223
7224
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007225void Isolate::SetAbortOnUncaughtExceptionCallback(
7226 AbortOnUncaughtExceptionCallback callback) {
7227 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7228 isolate->SetAbortOnUncaughtExceptionCallback(callback);
7229}
7230
7231
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007232Isolate::DisallowJavascriptExecutionScope::DisallowJavascriptExecutionScope(
7233 Isolate* isolate,
7234 Isolate::DisallowJavascriptExecutionScope::OnFailure on_failure)
7235 : on_failure_(on_failure) {
7236 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7237 if (on_failure_ == CRASH_ON_FAILURE) {
7238 internal_ = reinterpret_cast<void*>(
7239 new i::DisallowJavascriptExecution(i_isolate));
7240 } else {
7241 DCHECK_EQ(THROW_ON_FAILURE, on_failure);
7242 internal_ = reinterpret_cast<void*>(
7243 new i::ThrowOnJavascriptExecution(i_isolate));
7244 }
Ben Murdoch257744e2011-11-30 15:57:28 +00007245}
7246
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007247
7248Isolate::DisallowJavascriptExecutionScope::~DisallowJavascriptExecutionScope() {
7249 if (on_failure_ == CRASH_ON_FAILURE) {
7250 delete reinterpret_cast<i::DisallowJavascriptExecution*>(internal_);
7251 } else {
7252 delete reinterpret_cast<i::ThrowOnJavascriptExecution*>(internal_);
7253 }
7254}
7255
7256
7257Isolate::AllowJavascriptExecutionScope::AllowJavascriptExecutionScope(
7258 Isolate* isolate) {
7259 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7260 internal_assert_ = reinterpret_cast<void*>(
7261 new i::AllowJavascriptExecution(i_isolate));
7262 internal_throws_ = reinterpret_cast<void*>(
7263 new i::NoThrowOnJavascriptExecution(i_isolate));
7264}
7265
7266
7267Isolate::AllowJavascriptExecutionScope::~AllowJavascriptExecutionScope() {
7268 delete reinterpret_cast<i::AllowJavascriptExecution*>(internal_assert_);
7269 delete reinterpret_cast<i::NoThrowOnJavascriptExecution*>(internal_throws_);
7270}
7271
7272
7273Isolate::SuppressMicrotaskExecutionScope::SuppressMicrotaskExecutionScope(
7274 Isolate* isolate)
7275 : isolate_(reinterpret_cast<i::Isolate*>(isolate)) {
7276 isolate_->handle_scope_implementer()->IncrementCallDepth();
7277}
7278
7279
7280Isolate::SuppressMicrotaskExecutionScope::~SuppressMicrotaskExecutionScope() {
7281 isolate_->handle_scope_implementer()->DecrementCallDepth();
7282}
7283
7284
7285void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) {
Ben Murdoch257744e2011-11-30 15:57:28 +00007286 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007287 i::Heap* heap = isolate->heap();
7288 heap_statistics->total_heap_size_ = heap->CommittedMemory();
7289 heap_statistics->total_heap_size_executable_ =
7290 heap->CommittedMemoryExecutable();
7291 heap_statistics->total_physical_size_ = heap->CommittedPhysicalMemory();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007292 heap_statistics->total_available_size_ = heap->Available();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007293 heap_statistics->used_heap_size_ = heap->SizeOfObjects();
7294 heap_statistics->heap_size_limit_ = heap->MaxReserved();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007295 heap_statistics->does_zap_garbage_ = heap->ShouldZapGarbage();
7296}
7297
7298
7299size_t Isolate::NumberOfHeapSpaces() {
7300 return i::LAST_SPACE - i::FIRST_SPACE + 1;
7301}
7302
7303
7304bool Isolate::GetHeapSpaceStatistics(HeapSpaceStatistics* space_statistics,
7305 size_t index) {
7306 if (!space_statistics) return false;
7307 if (!i::Heap::IsValidAllocationSpace(static_cast<i::AllocationSpace>(index)))
7308 return false;
7309
7310 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7311 i::Heap* heap = isolate->heap();
7312 i::Space* space = heap->space(static_cast<int>(index));
7313
7314 space_statistics->space_name_ = heap->GetSpaceName(static_cast<int>(index));
7315 space_statistics->space_size_ = space->CommittedMemory();
7316 space_statistics->space_used_size_ = space->SizeOfObjects();
7317 space_statistics->space_available_size_ = space->Available();
7318 space_statistics->physical_space_size_ = space->CommittedPhysicalMemory();
7319 return true;
7320}
7321
7322
7323size_t Isolate::NumberOfTrackedHeapObjectTypes() {
7324 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7325 i::Heap* heap = isolate->heap();
7326 return heap->NumberOfTrackedHeapObjectTypes();
7327}
7328
7329
7330bool Isolate::GetHeapObjectStatisticsAtLastGC(
7331 HeapObjectStatistics* object_statistics, size_t type_index) {
7332 if (!object_statistics) return false;
7333 if (!i::FLAG_track_gc_object_stats) return false;
7334
7335 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7336 i::Heap* heap = isolate->heap();
7337 if (type_index >= heap->NumberOfTrackedHeapObjectTypes()) return false;
7338
7339 const char* object_type;
7340 const char* object_sub_type;
7341 size_t object_count = heap->ObjectCountAtLastGC(type_index);
7342 size_t object_size = heap->ObjectSizeAtLastGC(type_index);
7343 if (!heap->GetObjectTypeName(type_index, &object_type, &object_sub_type)) {
7344 // There should be no objects counted when the type is unknown.
7345 DCHECK_EQ(object_count, 0U);
7346 DCHECK_EQ(object_size, 0U);
7347 return false;
7348 }
7349
7350 object_statistics->object_type_ = object_type;
7351 object_statistics->object_sub_type_ = object_sub_type;
7352 object_statistics->object_count_ = object_count;
7353 object_statistics->object_size_ = object_size;
7354 return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007355}
7356
7357
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007358void Isolate::GetStackSample(const RegisterState& state, void** frames,
7359 size_t frames_limit, SampleInfo* sample_info) {
7360 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7361 i::TickSample::GetStackSample(isolate, state, i::TickSample::kSkipCEntryFrame,
7362 frames, frames_limit, sample_info);
7363}
7364
7365
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007366void Isolate::SetEventLogger(LogEventCallback that) {
7367 // Do not overwrite the event logger if we want to log explicitly.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007368 if (i::FLAG_log_internal_timer_events) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007369 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7370 isolate->set_event_logger(that);
7371}
7372
7373
7374void Isolate::AddCallCompletedCallback(CallCompletedCallback callback) {
7375 if (callback == NULL) return;
7376 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7377 isolate->AddCallCompletedCallback(callback);
7378}
7379
7380
7381void Isolate::RemoveCallCompletedCallback(CallCompletedCallback callback) {
7382 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7383 isolate->RemoveCallCompletedCallback(callback);
7384}
7385
7386
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007387void Isolate::SetPromiseRejectCallback(PromiseRejectCallback callback) {
7388 if (callback == NULL) return;
7389 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7390 isolate->SetPromiseRejectCallback(callback);
7391}
7392
7393
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007394void Isolate::RunMicrotasks() {
7395 reinterpret_cast<i::Isolate*>(this)->RunMicrotasks();
7396}
7397
7398
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007399void Isolate::EnqueueMicrotask(Local<Function> microtask) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007400 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7401 isolate->EnqueueMicrotask(Utils::OpenHandle(*microtask));
7402}
7403
7404
7405void Isolate::EnqueueMicrotask(MicrotaskCallback microtask, void* data) {
7406 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7407 i::HandleScope scope(isolate);
7408 i::Handle<i::CallHandlerInfo> callback_info =
7409 i::Handle<i::CallHandlerInfo>::cast(
7410 isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE));
7411 SET_FIELD_WRAPPED(callback_info, set_callback, microtask);
7412 SET_FIELD_WRAPPED(callback_info, set_data, data);
7413 isolate->EnqueueMicrotask(callback_info);
7414}
7415
7416
7417void Isolate::SetAutorunMicrotasks(bool autorun) {
7418 reinterpret_cast<i::Isolate*>(this)->set_autorun_microtasks(autorun);
7419}
7420
7421
7422bool Isolate::WillAutorunMicrotasks() const {
7423 return reinterpret_cast<const i::Isolate*>(this)->autorun_microtasks();
7424}
7425
7426
7427void Isolate::SetUseCounterCallback(UseCounterCallback callback) {
7428 reinterpret_cast<i::Isolate*>(this)->SetUseCounterCallback(callback);
7429}
7430
7431
7432void Isolate::SetCounterFunction(CounterLookupCallback callback) {
7433 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7434 isolate->stats_table()->SetCounterFunction(callback);
7435 isolate->InitializeLoggingAndCounters();
7436 isolate->counters()->ResetCounters();
7437}
7438
7439
7440void Isolate::SetCreateHistogramFunction(CreateHistogramCallback callback) {
7441 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7442 isolate->stats_table()->SetCreateHistogramFunction(callback);
7443 isolate->InitializeLoggingAndCounters();
7444 isolate->counters()->ResetHistograms();
7445}
7446
7447
7448void Isolate::SetAddHistogramSampleFunction(
7449 AddHistogramSampleCallback callback) {
7450 reinterpret_cast<i::Isolate*>(this)
7451 ->stats_table()
7452 ->SetAddHistogramSampleFunction(callback);
7453}
7454
7455
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007456bool Isolate::IdleNotification(int idle_time_in_ms) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007457 // Returning true tells the caller that it need not
7458 // continue to call IdleNotification.
7459 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7460 if (!i::FLAG_use_idle_notification) return true;
7461 return isolate->heap()->IdleNotification(idle_time_in_ms);
7462}
7463
7464
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007465bool Isolate::IdleNotificationDeadline(double deadline_in_seconds) {
7466 // Returning true tells the caller that it need not
7467 // continue to call IdleNotification.
7468 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7469 if (!i::FLAG_use_idle_notification) return true;
7470 return isolate->heap()->IdleNotification(deadline_in_seconds);
7471}
7472
7473
7474void Isolate::LowMemoryNotification() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007475 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7476 {
7477 i::HistogramTimerScope idle_notification_scope(
7478 isolate->counters()->gc_low_memory_notification());
7479 isolate->heap()->CollectAllAvailableGarbage("low memory notification");
7480 }
7481}
7482
7483
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007484int Isolate::ContextDisposedNotification(bool dependant_context) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007485 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007486 return isolate->heap()->NotifyContextDisposed(dependant_context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007487}
7488
7489
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007490void Isolate::IsolateInForegroundNotification() {
7491 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7492 return isolate->heap()->SetOptimizeForLatency();
7493}
7494
7495
7496void Isolate::IsolateInBackgroundNotification() {
7497 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7498 return isolate->heap()->SetOptimizeForMemoryUsage();
7499}
7500
7501
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007502void Isolate::SetJitCodeEventHandler(JitCodeEventOptions options,
7503 JitCodeEventHandler event_handler) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007504 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7505 // Ensure that logging is initialized for our isolate.
7506 isolate->InitializeLoggingAndCounters();
7507 isolate->logger()->SetCodeEventHandler(options, event_handler);
7508}
7509
7510
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007511void Isolate::SetStackLimit(uintptr_t stack_limit) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007512 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7513 CHECK(stack_limit);
7514 isolate->stack_guard()->SetStackLimit(stack_limit);
Ben Murdoch257744e2011-11-30 15:57:28 +00007515}
7516
7517
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007518void Isolate::GetCodeRange(void** start, size_t* length_in_bytes) {
7519 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7520 if (isolate->code_range()->valid()) {
7521 *start = isolate->code_range()->start();
7522 *length_in_bytes = isolate->code_range()->size();
7523 } else {
7524 *start = NULL;
7525 *length_in_bytes = 0;
7526 }
7527}
7528
7529
7530void Isolate::SetFatalErrorHandler(FatalErrorCallback that) {
7531 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7532 isolate->set_exception_behavior(that);
7533}
7534
7535
7536void Isolate::SetAllowCodeGenerationFromStringsCallback(
7537 AllowCodeGenerationFromStringsCallback callback) {
7538 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7539 isolate->set_allow_code_gen_callback(callback);
7540}
7541
7542
7543bool Isolate::IsDead() {
7544 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7545 return isolate->IsDead();
7546}
7547
7548
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007549bool Isolate::AddMessageListener(MessageCallback that, Local<Value> data) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007550 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007551 ENTER_V8(isolate);
7552 i::HandleScope scope(isolate);
7553 NeanderArray listeners(isolate->factory()->message_listeners());
7554 NeanderObject obj(isolate, 2);
7555 obj.set(0, *isolate->factory()->NewForeign(FUNCTION_ADDR(that)));
7556 obj.set(1, data.IsEmpty() ? isolate->heap()->undefined_value()
7557 : *Utils::OpenHandle(*data));
7558 listeners.add(isolate, obj.value());
7559 return true;
7560}
7561
7562
7563void Isolate::RemoveMessageListeners(MessageCallback that) {
7564 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007565 ENTER_V8(isolate);
7566 i::HandleScope scope(isolate);
7567 NeanderArray listeners(isolate->factory()->message_listeners());
7568 for (int i = 0; i < listeners.length(); i++) {
7569 if (listeners.get(i)->IsUndefined()) continue; // skip deleted ones
7570
7571 NeanderObject listener(i::JSObject::cast(listeners.get(i)));
7572 i::Handle<i::Foreign> callback_obj(i::Foreign::cast(listener.get(0)));
7573 if (callback_obj->foreign_address() == FUNCTION_ADDR(that)) {
7574 listeners.set(i, isolate->heap()->undefined_value());
7575 }
7576 }
7577}
7578
7579
7580void Isolate::SetFailedAccessCheckCallbackFunction(
7581 FailedAccessCheckCallback callback) {
7582 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7583 isolate->SetFailedAccessCheckCallback(callback);
7584}
7585
7586
7587void Isolate::SetCaptureStackTraceForUncaughtExceptions(
7588 bool capture, int frame_limit, StackTrace::StackTraceOptions options) {
7589 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7590 isolate->SetCaptureStackTraceForUncaughtExceptions(capture, frame_limit,
7591 options);
7592}
7593
7594
7595void Isolate::VisitExternalResources(ExternalResourceVisitor* visitor) {
7596 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7597 isolate->heap()->VisitExternalResources(visitor);
7598}
7599
7600
7601class VisitorAdapter : public i::ObjectVisitor {
7602 public:
7603 explicit VisitorAdapter(PersistentHandleVisitor* visitor)
7604 : visitor_(visitor) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007605 void VisitPointers(i::Object** start, i::Object** end) override {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007606 UNREACHABLE();
7607 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007608 void VisitEmbedderReference(i::Object** p, uint16_t class_id) override {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007609 Value* value = ToApi<Value>(i::Handle<i::Object>(p));
7610 visitor_->VisitPersistentHandle(
7611 reinterpret_cast<Persistent<Value>*>(&value), class_id);
7612 }
7613
7614 private:
7615 PersistentHandleVisitor* visitor_;
7616};
7617
7618
7619void Isolate::VisitHandlesWithClassIds(PersistentHandleVisitor* visitor) {
7620 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7621 i::DisallowHeapAllocation no_allocation;
7622 VisitorAdapter visitor_adapter(visitor);
7623 isolate->global_handles()->IterateAllRootsWithClassIds(&visitor_adapter);
7624}
7625
7626
7627void Isolate::VisitHandlesForPartialDependence(
7628 PersistentHandleVisitor* visitor) {
7629 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7630 i::DisallowHeapAllocation no_allocation;
7631 VisitorAdapter visitor_adapter(visitor);
7632 isolate->global_handles()->IterateAllRootsInNewSpaceWithClassIds(
7633 &visitor_adapter);
7634}
7635
7636
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007637void Isolate::VisitWeakHandles(PersistentHandleVisitor* visitor) {
7638 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7639 i::DisallowHeapAllocation no_allocation;
7640 VisitorAdapter visitor_adapter(visitor);
7641 isolate->global_handles()->IterateWeakRootsInNewSpaceWithClassIds(
7642 &visitor_adapter);
7643}
7644
7645
7646String::Utf8Value::Utf8Value(v8::Local<v8::Value> obj)
Ben Murdoch257744e2011-11-30 15:57:28 +00007647 : str_(NULL), length_(0) {
Ben Murdoch257744e2011-11-30 15:57:28 +00007648 if (obj.IsEmpty()) return;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007649 i::Isolate* isolate = i::Isolate::Current();
7650 Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01007651 ENTER_V8(isolate);
7652 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007653 Local<Context> context = v8_isolate->GetCurrentContext();
7654 TryCatch try_catch(v8_isolate);
7655 Local<String> str;
7656 if (!obj->ToString(context).ToLocal(&str)) return;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007657 i::Handle<i::String> i_str = Utils::OpenHandle(*str);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007658 length_ = v8::Utf8Length(*i_str, isolate);
Ben Murdoch257744e2011-11-30 15:57:28 +00007659 str_ = i::NewArray<char>(length_ + 1);
7660 str->WriteUtf8(str_);
Steve Blocka7e24c12009-10-30 11:49:00 +00007661}
7662
7663
7664String::Utf8Value::~Utf8Value() {
7665 i::DeleteArray(str_);
7666}
7667
7668
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007669String::Value::Value(v8::Local<v8::Value> obj) : str_(NULL), length_(0) {
Ben Murdoch257744e2011-11-30 15:57:28 +00007670 if (obj.IsEmpty()) return;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007671 i::Isolate* isolate = i::Isolate::Current();
7672 Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01007673 ENTER_V8(isolate);
7674 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007675 Local<Context> context = v8_isolate->GetCurrentContext();
7676 TryCatch try_catch(v8_isolate);
7677 Local<String> str;
7678 if (!obj->ToString(context).ToLocal(&str)) return;
Ben Murdoch257744e2011-11-30 15:57:28 +00007679 length_ = str->Length();
7680 str_ = i::NewArray<uint16_t>(length_ + 1);
7681 str->Write(str_);
Steve Blocka7e24c12009-10-30 11:49:00 +00007682}
7683
7684
7685String::Value::~Value() {
7686 i::DeleteArray(str_);
7687}
7688
Steve Blocka7e24c12009-10-30 11:49:00 +00007689
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007690#define DEFINE_ERROR(NAME, name) \
7691 Local<Value> Exception::NAME(v8::Local<v8::String> raw_message) { \
7692 i::Isolate* isolate = i::Isolate::Current(); \
7693 LOG_API(isolate, #NAME); \
7694 ENTER_V8(isolate); \
7695 i::Object* error; \
7696 { \
7697 i::HandleScope scope(isolate); \
7698 i::Handle<i::String> message = Utils::OpenHandle(*raw_message); \
7699 i::Handle<i::JSFunction> constructor = isolate->name##_function(); \
7700 error = *isolate->factory()->NewError(constructor, message); \
7701 } \
7702 i::Handle<i::Object> result(error, isolate); \
7703 return Utils::ToLocal(result); \
Steve Blocka7e24c12009-10-30 11:49:00 +00007704 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007705
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007706DEFINE_ERROR(RangeError, range_error)
7707DEFINE_ERROR(ReferenceError, reference_error)
7708DEFINE_ERROR(SyntaxError, syntax_error)
7709DEFINE_ERROR(TypeError, type_error)
7710DEFINE_ERROR(Error, error)
Steve Blocka7e24c12009-10-30 11:49:00 +00007711
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007712#undef DEFINE_ERROR
Steve Blocka7e24c12009-10-30 11:49:00 +00007713
7714
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007715Local<Message> Exception::CreateMessage(Isolate* isolate,
7716 Local<Value> exception) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007717 i::Handle<i::Object> obj = Utils::OpenHandle(*exception);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007718 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7719 ENTER_V8(i_isolate);
7720 i::HandleScope scope(i_isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007721 return Utils::MessageToLocal(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007722 scope.CloseAndEscape(i_isolate->CreateMessage(obj, NULL)));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007723}
7724
7725
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007726Local<Message> Exception::CreateMessage(Local<Value> exception) {
7727 i::Handle<i::Object> obj = Utils::OpenHandle(*exception);
7728 if (!obj->IsHeapObject()) return Local<Message>();
7729 i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate();
7730 return CreateMessage(reinterpret_cast<Isolate*>(isolate), exception);
7731}
7732
7733
7734Local<StackTrace> Exception::GetStackTrace(Local<Value> exception) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007735 i::Handle<i::Object> obj = Utils::OpenHandle(*exception);
7736 if (!obj->IsJSObject()) return Local<StackTrace>();
7737 i::Handle<i::JSObject> js_obj = i::Handle<i::JSObject>::cast(obj);
7738 i::Isolate* isolate = js_obj->GetIsolate();
7739 ENTER_V8(isolate);
7740 return Utils::StackTraceToLocal(isolate->GetDetailedStackTrace(js_obj));
7741}
7742
7743
Steve Blocka7e24c12009-10-30 11:49:00 +00007744// --- D e b u g S u p p o r t ---
7745
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007746bool Debug::SetDebugEventListener(Isolate* isolate, EventCallback that,
7747 Local<Value> data) {
7748 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7749 ENTER_V8(i_isolate);
7750 i::HandleScope scope(i_isolate);
7751 i::Handle<i::Object> foreign = i_isolate->factory()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00007752 if (that != NULL) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007753 foreign = i_isolate->factory()->NewForeign(FUNCTION_ADDR(that));
Steve Blocka7e24c12009-10-30 11:49:00 +00007754 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007755 i_isolate->debug()->SetEventListener(foreign, Utils::OpenHandle(*data, true));
Steve Blocka7e24c12009-10-30 11:49:00 +00007756 return true;
7757}
7758
7759
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007760bool Debug::SetDebugEventListener(EventCallback that, Local<Value> data) {
7761 return SetDebugEventListener(
7762 reinterpret_cast<Isolate*>(i::Isolate::Current()), that, data);
7763}
7764
7765
Steve Block44f0eee2011-05-26 01:26:41 +01007766void Debug::DebugBreak(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007767 reinterpret_cast<i::Isolate*>(isolate)->stack_guard()->RequestDebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +00007768}
7769
7770
Steve Block44f0eee2011-05-26 01:26:41 +01007771void Debug::CancelDebugBreak(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007772 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
7773 internal_isolate->stack_guard()->ClearDebugBreak();
Ben Murdochf87a2032010-10-22 12:50:53 +01007774}
7775
7776
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007777bool Debug::CheckDebugBreak(Isolate* isolate) {
7778 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
7779 return internal_isolate->stack_guard()->CheckDebugBreak();
Ben Murdoch3bec4d22010-07-22 14:51:16 +01007780}
7781
7782
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007783void Debug::SetMessageHandler(Isolate* isolate,
7784 v8::Debug::MessageHandler handler) {
7785 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7786 ENTER_V8(i_isolate);
7787 i_isolate->debug()->SetMessageHandler(handler);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007788}
7789
7790
7791void Debug::SetMessageHandler(v8::Debug::MessageHandler handler) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007792 SetMessageHandler(reinterpret_cast<Isolate*>(i::Isolate::Current()), handler);
Steve Blocka7e24c12009-10-30 11:49:00 +00007793}
7794
7795
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007796void Debug::SendCommand(Isolate* isolate,
7797 const uint16_t* command,
7798 int length,
7799 ClientData* client_data) {
7800 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
7801 internal_isolate->debug()->EnqueueCommandMessage(
7802 i::Vector<const uint16_t>(command, length), client_data);
Steve Blockd0582a62009-12-15 09:54:21 +00007803}
7804
7805
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007806MaybeLocal<Value> Debug::Call(Local<Context> context,
7807 v8::Local<v8::Function> fun,
7808 v8::Local<v8::Value> data) {
7809 PREPARE_FOR_EXECUTION(context, "v8::Debug::Call()", Value);
7810 i::Handle<i::Object> data_obj;
Steve Blocka7e24c12009-10-30 11:49:00 +00007811 if (data.IsEmpty()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007812 data_obj = isolate->factory()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00007813 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007814 data_obj = Utils::OpenHandle(*data);
Steve Blocka7e24c12009-10-30 11:49:00 +00007815 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007816 Local<Value> result;
7817 has_pending_exception =
7818 !ToLocal<Value>(isolate->debug()->Call(Utils::OpenHandle(*fun), data_obj),
7819 &result);
7820 RETURN_ON_FAILED_EXECUTION(Value);
7821 RETURN_ESCAPED(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00007822}
7823
7824
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007825Local<Value> Debug::Call(v8::Local<v8::Function> fun,
7826 v8::Local<v8::Value> data) {
7827 auto context = ContextFromHeapObject(Utils::OpenHandle(*fun));
7828 RETURN_TO_LOCAL_UNCHECKED(Call(context, fun, data), Value);
7829}
7830
7831
7832MaybeLocal<Value> Debug::GetMirror(Local<Context> context,
7833 v8::Local<v8::Value> obj) {
7834 PREPARE_FOR_EXECUTION(context, "v8::Debug::GetMirror()", Value);
Steve Block44f0eee2011-05-26 01:26:41 +01007835 i::Debug* isolate_debug = isolate->debug();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007836 has_pending_exception = !isolate_debug->Load();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007837 RETURN_ON_FAILED_EXECUTION(Value);
7838 i::Handle<i::JSObject> debug(isolate_debug->debug_context()->global_object());
7839 auto name = isolate->factory()->NewStringFromStaticChars("MakeMirror");
7840 auto fun_obj = i::Object::GetProperty(debug, name).ToHandleChecked();
7841 auto v8_fun = Utils::CallableToLocal(i::Handle<i::JSFunction>::cast(fun_obj));
7842 const int kArgc = 1;
7843 v8::Local<v8::Value> argv[kArgc] = {obj};
7844 Local<Value> result;
7845 has_pending_exception =
7846 !v8_fun->Call(context, Utils::ToLocal(debug), kArgc, argv)
7847 .ToLocal(&result);
7848 RETURN_ON_FAILED_EXECUTION(Value);
7849 RETURN_ESCAPED(result);
7850}
7851
7852
7853Local<Value> Debug::GetMirror(v8::Local<v8::Value> obj) {
7854 RETURN_TO_LOCAL_UNCHECKED(GetMirror(Local<Context>(), obj), Value);
7855}
7856
7857
7858void Debug::ProcessDebugMessages(Isolate* isolate) {
7859 reinterpret_cast<i::Isolate*>(isolate)->debug()->ProcessDebugMessages(true);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007860}
7861
7862
Leon Clarkee46be812010-01-19 14:06:41 +00007863void Debug::ProcessDebugMessages() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007864 ProcessDebugMessages(reinterpret_cast<Isolate*>(i::Isolate::Current()));
7865}
7866
7867
7868Local<Context> Debug::GetDebugContext(Isolate* isolate) {
7869 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7870 ENTER_V8(i_isolate);
7871 return Utils::ToLocal(i_isolate->debug()->GetDebugContext());
Leon Clarkee46be812010-01-19 14:06:41 +00007872}
7873
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007874
Steve Block6ded16b2010-05-10 14:33:55 +01007875Local<Context> Debug::GetDebugContext() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007876 return GetDebugContext(reinterpret_cast<Isolate*>(i::Isolate::Current()));
Steve Block6ded16b2010-05-10 14:33:55 +01007877}
7878
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007879
7880void Debug::SetLiveEditEnabled(Isolate* isolate, bool enable) {
7881 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
7882 internal_isolate->debug()->set_live_edit_enabled(enable);
7883}
Steve Blocka7e24c12009-10-30 11:49:00 +00007884
Steve Block6ded16b2010-05-10 14:33:55 +01007885
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007886MaybeLocal<Array> Debug::GetInternalProperties(Isolate* v8_isolate,
7887 Local<Value> value) {
7888 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
7889 ENTER_V8(isolate);
7890 i::Handle<i::Object> val = Utils::OpenHandle(*value);
7891 i::Handle<i::JSArray> result;
7892 if (!i::Runtime::GetInternalProperties(isolate, val).ToHandle(&result))
7893 return MaybeLocal<Array>();
7894 return Utils::ToLocal(result);
7895}
7896
7897
7898Local<String> CpuProfileNode::GetFunctionName() const {
Steve Block6ded16b2010-05-10 14:33:55 +01007899 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007900 i::Isolate* isolate = node->isolate();
Steve Block6ded16b2010-05-10 14:33:55 +01007901 const i::CodeEntry* entry = node->entry();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007902 i::Handle<i::String> name =
7903 isolate->factory()->InternalizeUtf8String(entry->name());
Steve Block6ded16b2010-05-10 14:33:55 +01007904 if (!entry->has_name_prefix()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007905 return ToApiHandle<String>(name);
Steve Block6ded16b2010-05-10 14:33:55 +01007906 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007907 // We do not expect this to fail. Change this if it does.
7908 i::Handle<i::String> cons = isolate->factory()->NewConsString(
7909 isolate->factory()->InternalizeUtf8String(entry->name_prefix()),
7910 name).ToHandleChecked();
7911 return ToApiHandle<String>(cons);
Steve Block6ded16b2010-05-10 14:33:55 +01007912 }
7913}
7914
7915
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007916int CpuProfileNode::GetScriptId() const {
7917 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
7918 const i::CodeEntry* entry = node->entry();
7919 return entry->script_id();
7920}
7921
7922
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007923Local<String> CpuProfileNode::GetScriptResourceName() const {
Steve Block6ded16b2010-05-10 14:33:55 +01007924 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007925 i::Isolate* isolate = node->isolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007926 return ToApiHandle<String>(isolate->factory()->InternalizeUtf8String(
7927 node->entry()->resource_name()));
Steve Block6ded16b2010-05-10 14:33:55 +01007928}
7929
7930
7931int CpuProfileNode::GetLineNumber() const {
Steve Block6ded16b2010-05-10 14:33:55 +01007932 return reinterpret_cast<const i::ProfileNode*>(this)->entry()->line_number();
7933}
7934
7935
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007936int CpuProfileNode::GetColumnNumber() const {
7937 return reinterpret_cast<const i::ProfileNode*>(this)->
7938 entry()->column_number();
Steve Block6ded16b2010-05-10 14:33:55 +01007939}
7940
7941
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007942unsigned int CpuProfileNode::GetHitLineCount() const {
7943 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
7944 return node->GetHitLineCount();
7945}
7946
7947
7948bool CpuProfileNode::GetLineTicks(LineTick* entries,
7949 unsigned int length) const {
7950 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
7951 return node->GetLineTicks(entries, length);
7952}
7953
7954
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007955const char* CpuProfileNode::GetBailoutReason() const {
7956 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
7957 return node->entry()->bailout_reason();
Steve Block6ded16b2010-05-10 14:33:55 +01007958}
7959
7960
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007961unsigned CpuProfileNode::GetHitCount() const {
Steve Block6ded16b2010-05-10 14:33:55 +01007962 return reinterpret_cast<const i::ProfileNode*>(this)->self_ticks();
7963}
7964
7965
7966unsigned CpuProfileNode::GetCallUid() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007967 return reinterpret_cast<const i::ProfileNode*>(this)->function_id();
Steve Block6ded16b2010-05-10 14:33:55 +01007968}
7969
7970
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007971unsigned CpuProfileNode::GetNodeId() const {
7972 return reinterpret_cast<const i::ProfileNode*>(this)->id();
7973}
7974
7975
Steve Block6ded16b2010-05-10 14:33:55 +01007976int CpuProfileNode::GetChildrenCount() const {
Steve Block6ded16b2010-05-10 14:33:55 +01007977 return reinterpret_cast<const i::ProfileNode*>(this)->children()->length();
7978}
7979
7980
7981const CpuProfileNode* CpuProfileNode::GetChild(int index) const {
Steve Block6ded16b2010-05-10 14:33:55 +01007982 const i::ProfileNode* child =
7983 reinterpret_cast<const i::ProfileNode*>(this)->children()->at(index);
7984 return reinterpret_cast<const CpuProfileNode*>(child);
7985}
7986
7987
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007988const std::vector<CpuProfileDeoptInfo>& CpuProfileNode::GetDeoptInfos() const {
7989 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
7990 return node->deopt_infos();
Steve Block6ded16b2010-05-10 14:33:55 +01007991}
7992
7993
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007994void CpuProfile::Delete() {
7995 i::CpuProfile* profile = reinterpret_cast<i::CpuProfile*>(this);
7996 i::Isolate* isolate = profile->top_down()->isolate();
7997 i::CpuProfiler* profiler = isolate->cpu_profiler();
7998 DCHECK(profiler != NULL);
7999 profiler->DeleteProfile(profile);
8000}
8001
8002
8003Local<String> CpuProfile::GetTitle() const {
Steve Block6ded16b2010-05-10 14:33:55 +01008004 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008005 i::Isolate* isolate = profile->top_down()->isolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008006 return ToApiHandle<String>(isolate->factory()->InternalizeUtf8String(
8007 profile->title()));
Steve Block6ded16b2010-05-10 14:33:55 +01008008}
8009
8010
8011const CpuProfileNode* CpuProfile::GetTopDownRoot() const {
Steve Block6ded16b2010-05-10 14:33:55 +01008012 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
8013 return reinterpret_cast<const CpuProfileNode*>(profile->top_down()->root());
8014}
8015
8016
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008017const CpuProfileNode* CpuProfile::GetSample(int index) const {
8018 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
8019 return reinterpret_cast<const CpuProfileNode*>(profile->sample(index));
Steve Block6ded16b2010-05-10 14:33:55 +01008020}
8021
8022
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008023int64_t CpuProfile::GetSampleTimestamp(int index) const {
8024 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
8025 return (profile->sample_timestamp(index) - base::TimeTicks())
8026 .InMicroseconds();
Steve Block6ded16b2010-05-10 14:33:55 +01008027}
8028
8029
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008030int64_t CpuProfile::GetStartTime() const {
8031 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
8032 return (profile->start_time() - base::TimeTicks()).InMicroseconds();
Steve Block6ded16b2010-05-10 14:33:55 +01008033}
8034
8035
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008036int64_t CpuProfile::GetEndTime() const {
8037 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
8038 return (profile->end_time() - base::TimeTicks()).InMicroseconds();
Steve Block6ded16b2010-05-10 14:33:55 +01008039}
8040
8041
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008042int CpuProfile::GetSamplesCount() const {
8043 return reinterpret_cast<const i::CpuProfile*>(this)->samples_count();
8044}
8045
8046
8047void CpuProfiler::SetSamplingInterval(int us) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008048 DCHECK_GE(us, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008049 return reinterpret_cast<i::CpuProfiler*>(this)->set_sampling_interval(
8050 base::TimeDelta::FromMicroseconds(us));
8051}
8052
8053
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008054void CpuProfiler::StartProfiling(Local<String> title, bool record_samples) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008055 reinterpret_cast<i::CpuProfiler*>(this)->StartProfiling(
8056 *Utils::OpenHandle(*title), record_samples);
8057}
8058
8059
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008060CpuProfile* CpuProfiler::StopProfiling(Local<String> title) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008061 return reinterpret_cast<CpuProfile*>(
8062 reinterpret_cast<i::CpuProfiler*>(this)->StopProfiling(
Leon Clarkef7060e22010-06-03 12:02:55 +01008063 *Utils::OpenHandle(*title)));
Steve Block6ded16b2010-05-10 14:33:55 +01008064}
8065
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008066
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008067void CpuProfiler::SetIdle(bool is_idle) {
8068 i::Isolate* isolate = reinterpret_cast<i::CpuProfiler*>(this)->isolate();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008069 v8::StateTag state = isolate->current_vm_state();
8070 DCHECK(state == v8::EXTERNAL || state == v8::IDLE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008071 if (isolate->js_entry_sp() != NULL) return;
8072 if (is_idle) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008073 isolate->set_current_vm_state(v8::IDLE);
8074 } else if (state == v8::IDLE) {
8075 isolate->set_current_vm_state(v8::EXTERNAL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008076 }
Steve Block44f0eee2011-05-26 01:26:41 +01008077}
8078
8079
Iain Merrick75681382010-08-19 15:07:18 +01008080static i::HeapGraphEdge* ToInternal(const HeapGraphEdge* edge) {
8081 return const_cast<i::HeapGraphEdge*>(
8082 reinterpret_cast<const i::HeapGraphEdge*>(edge));
8083}
8084
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008085
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008086HeapGraphEdge::Type HeapGraphEdge::GetType() const {
Iain Merrick75681382010-08-19 15:07:18 +01008087 return static_cast<HeapGraphEdge::Type>(ToInternal(this)->type());
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008088}
8089
8090
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008091Local<Value> HeapGraphEdge::GetName() const {
Iain Merrick75681382010-08-19 15:07:18 +01008092 i::HeapGraphEdge* edge = ToInternal(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008093 i::Isolate* isolate = edge->isolate();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008094 switch (edge->type()) {
Iain Merrick75681382010-08-19 15:07:18 +01008095 case i::HeapGraphEdge::kContextVariable:
8096 case i::HeapGraphEdge::kInternal:
8097 case i::HeapGraphEdge::kProperty:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08008098 case i::HeapGraphEdge::kShortcut:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008099 case i::HeapGraphEdge::kWeak:
8100 return ToApiHandle<String>(
8101 isolate->factory()->InternalizeUtf8String(edge->name()));
Iain Merrick75681382010-08-19 15:07:18 +01008102 case i::HeapGraphEdge::kElement:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08008103 case i::HeapGraphEdge::kHidden:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008104 return ToApiHandle<Number>(
8105 isolate->factory()->NewNumberFromInt(edge->index()));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008106 default: UNREACHABLE();
8107 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008108 return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008109}
8110
8111
8112const HeapGraphNode* HeapGraphEdge::GetFromNode() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008113 const i::HeapEntry* from = ToInternal(this)->from();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008114 return reinterpret_cast<const HeapGraphNode*>(from);
8115}
8116
8117
8118const HeapGraphNode* HeapGraphEdge::GetToNode() const {
Iain Merrick75681382010-08-19 15:07:18 +01008119 const i::HeapEntry* to = ToInternal(this)->to();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008120 return reinterpret_cast<const HeapGraphNode*>(to);
8121}
8122
8123
Iain Merrick75681382010-08-19 15:07:18 +01008124static i::HeapEntry* ToInternal(const HeapGraphNode* entry) {
8125 return const_cast<i::HeapEntry*>(
8126 reinterpret_cast<const i::HeapEntry*>(entry));
8127}
8128
8129
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008130HeapGraphNode::Type HeapGraphNode::GetType() const {
Iain Merrick75681382010-08-19 15:07:18 +01008131 return static_cast<HeapGraphNode::Type>(ToInternal(this)->type());
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008132}
8133
8134
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008135Local<String> HeapGraphNode::GetName() const {
8136 i::Isolate* isolate = ToInternal(this)->isolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008137 return ToApiHandle<String>(
8138 isolate->factory()->InternalizeUtf8String(ToInternal(this)->name()));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008139}
8140
8141
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008142SnapshotObjectId HeapGraphNode::GetId() const {
Iain Merrick75681382010-08-19 15:07:18 +01008143 return ToInternal(this)->id();
Ben Murdoch3bec4d22010-07-22 14:51:16 +01008144}
8145
8146
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008147size_t HeapGraphNode::GetShallowSize() const {
Iain Merrick75681382010-08-19 15:07:18 +01008148 return ToInternal(this)->self_size();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008149}
8150
8151
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008152int HeapGraphNode::GetChildrenCount() const {
Iain Merrick75681382010-08-19 15:07:18 +01008153 return ToInternal(this)->children().length();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008154}
8155
8156
8157const HeapGraphEdge* HeapGraphNode::GetChild(int index) const {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008158 return reinterpret_cast<const HeapGraphEdge*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008159 ToInternal(this)->children()[index]);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008160}
8161
8162
Iain Merrick75681382010-08-19 15:07:18 +01008163static i::HeapSnapshot* ToInternal(const HeapSnapshot* snapshot) {
8164 return const_cast<i::HeapSnapshot*>(
8165 reinterpret_cast<const i::HeapSnapshot*>(snapshot));
8166}
8167
8168
Steve Block44f0eee2011-05-26 01:26:41 +01008169void HeapSnapshot::Delete() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008170 i::Isolate* isolate = ToInternal(this)->profiler()->isolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008171 if (isolate->heap_profiler()->GetSnapshotsCount() > 1) {
Steve Block44f0eee2011-05-26 01:26:41 +01008172 ToInternal(this)->Delete();
8173 } else {
8174 // If this is the last snapshot, clean up all accessory data as well.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008175 isolate->heap_profiler()->DeleteAllSnapshots();
Steve Block44f0eee2011-05-26 01:26:41 +01008176 }
8177}
8178
8179
Ben Murdoch3bec4d22010-07-22 14:51:16 +01008180const HeapGraphNode* HeapSnapshot::GetRoot() const {
Iain Merrick75681382010-08-19 15:07:18 +01008181 return reinterpret_cast<const HeapGraphNode*>(ToInternal(this)->root());
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008182}
8183
8184
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008185const HeapGraphNode* HeapSnapshot::GetNodeById(SnapshotObjectId id) const {
Ben Murdochb0fe1622011-05-05 13:52:32 +01008186 return reinterpret_cast<const HeapGraphNode*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008187 ToInternal(this)->GetEntryById(id));
Ben Murdochb0fe1622011-05-05 13:52:32 +01008188}
8189
8190
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008191int HeapSnapshot::GetNodesCount() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008192 return ToInternal(this)->entries().length();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008193}
8194
8195
8196const HeapGraphNode* HeapSnapshot::GetNode(int index) const {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008197 return reinterpret_cast<const HeapGraphNode*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008198 &ToInternal(this)->entries().at(index));
8199}
8200
8201
8202SnapshotObjectId HeapSnapshot::GetMaxSnapshotJSObjectId() const {
8203 return ToInternal(this)->max_snapshot_js_object_id();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008204}
8205
8206
Kristian Monsen0d5e1162010-09-30 15:31:59 +01008207void HeapSnapshot::Serialize(OutputStream* stream,
8208 HeapSnapshot::SerializationFormat format) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008209 Utils::ApiCheck(format == kJSON,
8210 "v8::HeapSnapshot::Serialize",
8211 "Unknown serialization format");
8212 Utils::ApiCheck(stream->GetChunkSize() > 0,
8213 "v8::HeapSnapshot::Serialize",
8214 "Invalid stream chunk size");
Kristian Monsen0d5e1162010-09-30 15:31:59 +01008215 i::HeapSnapshotJSONSerializer serializer(ToInternal(this));
8216 serializer.Serialize(stream);
8217}
8218
8219
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008220// static
8221STATIC_CONST_MEMBER_DEFINITION const SnapshotObjectId
8222 HeapProfiler::kUnknownObjectId;
8223
8224
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008225int HeapProfiler::GetSnapshotCount() {
8226 return reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshotsCount();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008227}
8228
8229
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008230const HeapSnapshot* HeapProfiler::GetHeapSnapshot(int index) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008231 return reinterpret_cast<const HeapSnapshot*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008232 reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshot(index));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008233}
8234
8235
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008236SnapshotObjectId HeapProfiler::GetObjectId(Local<Value> value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008237 i::Handle<i::Object> obj = Utils::OpenHandle(*value);
8238 return reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshotObjectId(obj);
8239}
8240
8241
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008242Local<Value> HeapProfiler::FindObjectById(SnapshotObjectId id) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008243 i::Handle<i::Object> obj =
8244 reinterpret_cast<i::HeapProfiler*>(this)->FindHeapObjectById(id);
8245 if (obj.is_null()) return Local<Value>();
8246 return Utils::ToLocal(obj);
8247}
8248
8249
8250void HeapProfiler::ClearObjectIds() {
8251 reinterpret_cast<i::HeapProfiler*>(this)->ClearHeapObjectMap();
8252}
8253
8254
8255const HeapSnapshot* HeapProfiler::TakeHeapSnapshot(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008256 ActivityControl* control, ObjectNameResolver* resolver) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008257 return reinterpret_cast<const HeapSnapshot*>(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008258 reinterpret_cast<i::HeapProfiler*>(this)
8259 ->TakeSnapshot(control, resolver));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008260}
8261
8262
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008263void HeapProfiler::StartTrackingHeapObjects(bool track_allocations) {
8264 reinterpret_cast<i::HeapProfiler*>(this)->StartHeapObjectsTracking(
8265 track_allocations);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008266}
8267
Steve Block44f0eee2011-05-26 01:26:41 +01008268
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008269void HeapProfiler::StopTrackingHeapObjects() {
8270 reinterpret_cast<i::HeapProfiler*>(this)->StopHeapObjectsTracking();
Steve Block44f0eee2011-05-26 01:26:41 +01008271}
8272
8273
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008274SnapshotObjectId HeapProfiler::GetHeapStats(OutputStream* stream,
8275 int64_t* timestamp_us) {
8276 i::HeapProfiler* heap_profiler = reinterpret_cast<i::HeapProfiler*>(this);
8277 return heap_profiler->PushHeapObjectsStats(stream, timestamp_us);
Steve Block44f0eee2011-05-26 01:26:41 +01008278}
8279
Steve Block6ded16b2010-05-10 14:33:55 +01008280
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008281void HeapProfiler::DeleteAllHeapSnapshots() {
8282 reinterpret_cast<i::HeapProfiler*>(this)->DeleteAllSnapshots();
8283}
8284
8285
8286void HeapProfiler::SetWrapperClassInfoProvider(uint16_t class_id,
8287 WrapperInfoCallback callback) {
8288 reinterpret_cast<i::HeapProfiler*>(this)->DefineWrapperClass(class_id,
8289 callback);
8290}
8291
8292
8293size_t HeapProfiler::GetProfilerMemorySize() {
8294 return reinterpret_cast<i::HeapProfiler*>(this)->
8295 GetMemorySizeUsedByProfiler();
8296}
8297
8298
8299void HeapProfiler::SetRetainedObjectInfo(UniqueId id,
8300 RetainedObjectInfo* info) {
8301 reinterpret_cast<i::HeapProfiler*>(this)->SetRetainedObjectInfo(id, info);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008302}
8303
Steve Block6ded16b2010-05-10 14:33:55 +01008304
Ben Murdochb0fe1622011-05-05 13:52:32 +01008305v8::Testing::StressType internal::Testing::stress_type_ =
8306 v8::Testing::kStressTypeOpt;
8307
8308
8309void Testing::SetStressRunType(Testing::StressType type) {
8310 internal::Testing::set_stress_type(type);
8311}
8312
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008313
Ben Murdochb0fe1622011-05-05 13:52:32 +01008314int Testing::GetStressRuns() {
8315 if (internal::FLAG_stress_runs != 0) return internal::FLAG_stress_runs;
8316#ifdef DEBUG
8317 // In debug mode the code runs much slower so stressing will only make two
8318 // runs.
8319 return 2;
8320#else
8321 return 5;
8322#endif
8323}
8324
8325
8326static void SetFlagsFromString(const char* flags) {
8327 V8::SetFlagsFromString(flags, i::StrLength(flags));
8328}
8329
8330
8331void Testing::PrepareStressRun(int run) {
8332 static const char* kLazyOptimizations =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008333 "--prepare-always-opt "
8334 "--max-inlined-source-size=999999 "
8335 "--max-inlined-nodes=999999 "
8336 "--max-inlined-nodes-cumulative=999999 "
8337 "--noalways-opt";
Ben Murdochb0fe1622011-05-05 13:52:32 +01008338 static const char* kForcedOptimizations = "--always-opt";
8339
8340 // If deoptimization stressed turn on frequent deoptimization. If no value
8341 // is spefified through --deopt-every-n-times use a default default value.
8342 static const char* kDeoptEvery13Times = "--deopt-every-n-times=13";
8343 if (internal::Testing::stress_type() == Testing::kStressTypeDeopt &&
8344 internal::FLAG_deopt_every_n_times == 0) {
8345 SetFlagsFromString(kDeoptEvery13Times);
8346 }
8347
8348#ifdef DEBUG
8349 // As stressing in debug mode only make two runs skip the deopt stressing
8350 // here.
8351 if (run == GetStressRuns() - 1) {
8352 SetFlagsFromString(kForcedOptimizations);
8353 } else {
Ben Murdochb0fe1622011-05-05 13:52:32 +01008354 SetFlagsFromString(kLazyOptimizations);
8355 }
8356#else
8357 if (run == GetStressRuns() - 1) {
8358 SetFlagsFromString(kForcedOptimizations);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008359 } else if (run != GetStressRuns() - 2) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01008360 SetFlagsFromString(kLazyOptimizations);
8361 }
8362#endif
8363}
8364
8365
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008366void Testing::DeoptimizeAll(Isolate* isolate) {
8367 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8368 i::HandleScope scope(i_isolate);
8369 internal::Deoptimizer::DeoptimizeAll(i_isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00008370}
8371
8372
Steve Block44f0eee2011-05-26 01:26:41 +01008373namespace internal {
8374
8375
Steve Blocka7e24c12009-10-30 11:49:00 +00008376void HandleScopeImplementer::FreeThreadResources() {
Steve Block44f0eee2011-05-26 01:26:41 +01008377 Free();
Steve Blocka7e24c12009-10-30 11:49:00 +00008378}
8379
8380
8381char* HandleScopeImplementer::ArchiveThread(char* storage) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008382 HandleScopeData* current = isolate_->handle_scope_data();
Steve Blocka7e24c12009-10-30 11:49:00 +00008383 handle_scope_data_ = *current;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008384 MemCopy(storage, this, sizeof(*this));
Steve Blocka7e24c12009-10-30 11:49:00 +00008385
8386 ResetAfterArchive();
8387 current->Initialize();
8388
8389 return storage + ArchiveSpacePerThread();
8390}
8391
8392
8393int HandleScopeImplementer::ArchiveSpacePerThread() {
Steve Block44f0eee2011-05-26 01:26:41 +01008394 return sizeof(HandleScopeImplementer);
Steve Blocka7e24c12009-10-30 11:49:00 +00008395}
8396
8397
8398char* HandleScopeImplementer::RestoreThread(char* storage) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008399 MemCopy(this, storage, sizeof(*this));
Ben Murdoch257744e2011-11-30 15:57:28 +00008400 *isolate_->handle_scope_data() = handle_scope_data_;
Steve Blocka7e24c12009-10-30 11:49:00 +00008401 return storage + ArchiveSpacePerThread();
8402}
8403
8404
8405void HandleScopeImplementer::IterateThis(ObjectVisitor* v) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008406#ifdef DEBUG
8407 bool found_block_before_deferred = false;
8408#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00008409 // Iterate over all handles in the blocks except for the last.
8410 for (int i = blocks()->length() - 2; i >= 0; --i) {
8411 Object** block = blocks()->at(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008412 if (last_handle_before_deferred_block_ != NULL &&
8413 (last_handle_before_deferred_block_ <= &block[kHandleBlockSize]) &&
8414 (last_handle_before_deferred_block_ >= block)) {
8415 v->VisitPointers(block, last_handle_before_deferred_block_);
8416 DCHECK(!found_block_before_deferred);
8417#ifdef DEBUG
8418 found_block_before_deferred = true;
8419#endif
8420 } else {
8421 v->VisitPointers(block, &block[kHandleBlockSize]);
8422 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008423 }
8424
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008425 DCHECK(last_handle_before_deferred_block_ == NULL ||
8426 found_block_before_deferred);
8427
Steve Blocka7e24c12009-10-30 11:49:00 +00008428 // Iterate over live handles in the last block (if any).
8429 if (!blocks()->is_empty()) {
8430 v->VisitPointers(blocks()->last(), handle_scope_data_.next);
8431 }
8432
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008433 List<Context*>* context_lists[2] = { &saved_contexts_, &entered_contexts_};
8434 for (unsigned i = 0; i < arraysize(context_lists); i++) {
8435 if (context_lists[i]->is_empty()) continue;
8436 Object** start = reinterpret_cast<Object**>(&context_lists[i]->first());
8437 v->VisitPointers(start, start + context_lists[i]->length());
Steve Blocka7e24c12009-10-30 11:49:00 +00008438 }
8439}
8440
8441
8442void HandleScopeImplementer::Iterate(ObjectVisitor* v) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008443 HandleScopeData* current = isolate_->handle_scope_data();
Steve Block44f0eee2011-05-26 01:26:41 +01008444 handle_scope_data_ = *current;
8445 IterateThis(v);
Steve Blocka7e24c12009-10-30 11:49:00 +00008446}
8447
8448
8449char* HandleScopeImplementer::Iterate(ObjectVisitor* v, char* storage) {
Steve Block44f0eee2011-05-26 01:26:41 +01008450 HandleScopeImplementer* scope_implementer =
Steve Blocka7e24c12009-10-30 11:49:00 +00008451 reinterpret_cast<HandleScopeImplementer*>(storage);
Steve Block44f0eee2011-05-26 01:26:41 +01008452 scope_implementer->IterateThis(v);
Steve Blocka7e24c12009-10-30 11:49:00 +00008453 return storage + ArchiveSpacePerThread();
8454}
8455
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008456
8457DeferredHandles* HandleScopeImplementer::Detach(Object** prev_limit) {
8458 DeferredHandles* deferred =
8459 new DeferredHandles(isolate()->handle_scope_data()->next, isolate());
8460
8461 while (!blocks_.is_empty()) {
8462 Object** block_start = blocks_.last();
8463 Object** block_limit = &block_start[kHandleBlockSize];
8464 // We should not need to check for SealHandleScope here. Assert this.
8465 DCHECK(prev_limit == block_limit ||
8466 !(block_start <= prev_limit && prev_limit <= block_limit));
8467 if (prev_limit == block_limit) break;
8468 deferred->blocks_.Add(blocks_.last());
8469 blocks_.RemoveLast();
8470 }
8471
8472 // deferred->blocks_ now contains the blocks installed on the
8473 // HandleScope stack since BeginDeferredScope was called, but in
8474 // reverse order.
8475
8476 DCHECK(prev_limit == NULL || !blocks_.is_empty());
8477
8478 DCHECK(!blocks_.is_empty() && prev_limit != NULL);
8479 DCHECK(last_handle_before_deferred_block_ != NULL);
8480 last_handle_before_deferred_block_ = NULL;
8481 return deferred;
8482}
8483
8484
8485void HandleScopeImplementer::BeginDeferredScope() {
8486 DCHECK(last_handle_before_deferred_block_ == NULL);
8487 last_handle_before_deferred_block_ = isolate()->handle_scope_data()->next;
8488}
8489
8490
8491DeferredHandles::~DeferredHandles() {
8492 isolate_->UnlinkDeferredHandles(this);
8493
8494 for (int i = 0; i < blocks_.length(); i++) {
8495#ifdef ENABLE_HANDLE_ZAPPING
8496 HandleScope::ZapRange(blocks_[i], &blocks_[i][kHandleBlockSize]);
8497#endif
8498 isolate_->handle_scope_implementer()->ReturnBlock(blocks_[i]);
8499 }
8500}
8501
8502
8503void DeferredHandles::Iterate(ObjectVisitor* v) {
8504 DCHECK(!blocks_.is_empty());
8505
8506 DCHECK((first_block_limit_ >= blocks_.first()) &&
8507 (first_block_limit_ <= &(blocks_.first())[kHandleBlockSize]));
8508
8509 v->VisitPointers(blocks_.first(), first_block_limit_);
8510
8511 for (int i = 1; i < blocks_.length(); i++) {
8512 v->VisitPointers(blocks_[i], &blocks_[i][kHandleBlockSize]);
8513 }
8514}
8515
8516
8517void InvokeAccessorGetterCallback(
8518 v8::Local<v8::Name> property,
8519 const v8::PropertyCallbackInfo<v8::Value>& info,
8520 v8::AccessorNameGetterCallback getter) {
8521 // Leaving JavaScript.
8522 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
8523 Address getter_address = reinterpret_cast<Address>(reinterpret_cast<intptr_t>(
8524 getter));
8525 VMState<EXTERNAL> state(isolate);
8526 ExternalCallbackScope call_scope(isolate, getter_address);
8527 getter(property, info);
8528}
8529
8530
8531void InvokeFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info,
8532 v8::FunctionCallback callback) {
8533 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
8534 Address callback_address =
8535 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
8536 VMState<EXTERNAL> state(isolate);
8537 ExternalCallbackScope call_scope(isolate, callback_address);
8538 callback(info);
8539}
8540
8541
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008542} // namespace internal
8543} // namespace v8