Line data Source code
1 : // Copyright 2012 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/api.h"
6 :
7 : #include <string.h> // For memcpy, strlen.
8 : #ifdef V8_USE_ADDRESS_SANITIZER
9 : #include <sanitizer/asan_interface.h>
10 : #endif // V8_USE_ADDRESS_SANITIZER
11 : #include <cmath> // For isnan.
12 : #include <limits>
13 : #include <vector>
14 : #include "include/v8-debug.h"
15 : #include "include/v8-profiler.h"
16 : #include "include/v8-testing.h"
17 : #include "include/v8-util.h"
18 : #include "src/accessors.h"
19 : #include "src/api-natives.h"
20 : #include "src/assert-scope.h"
21 : #include "src/background-parsing-task.h"
22 : #include "src/base/functional.h"
23 : #include "src/base/platform/platform.h"
24 : #include "src/base/platform/time.h"
25 : #include "src/base/safe_conversions.h"
26 : #include "src/base/utils/random-number-generator.h"
27 : #include "src/bootstrapper.h"
28 : #include "src/builtins/builtins-utils.h"
29 : #include "src/char-predicates-inl.h"
30 : #include "src/code-stubs.h"
31 : #include "src/compiler-dispatcher/compiler-dispatcher.h"
32 : #include "src/compiler.h"
33 : #include "src/contexts.h"
34 : #include "src/conversions-inl.h"
35 : #include "src/counters.h"
36 : #include "src/debug/debug-coverage.h"
37 : #include "src/debug/debug.h"
38 : #include "src/deoptimizer.h"
39 : #include "src/execution.h"
40 : #include "src/frames-inl.h"
41 : #include "src/gdb-jit.h"
42 : #include "src/global-handles.h"
43 : #include "src/globals.h"
44 : #include "src/icu_util.h"
45 : #include "src/isolate-inl.h"
46 : #include "src/json-parser.h"
47 : #include "src/json-stringifier.h"
48 : #include "src/messages.h"
49 : #include "src/objects-inl.h"
50 : #include "src/parsing/parser.h"
51 : #include "src/parsing/scanner-character-streams.h"
52 : #include "src/pending-compilation-error-handler.h"
53 : #include "src/profiler/cpu-profiler.h"
54 : #include "src/profiler/heap-profiler.h"
55 : #include "src/profiler/heap-snapshot-generator-inl.h"
56 : #include "src/profiler/profile-generator-inl.h"
57 : #include "src/profiler/tick-sample.h"
58 : #include "src/property-descriptor.h"
59 : #include "src/property-details.h"
60 : #include "src/property.h"
61 : #include "src/prototype.h"
62 : #include "src/runtime-profiler.h"
63 : #include "src/runtime/runtime.h"
64 : #include "src/simulator.h"
65 : #include "src/snapshot/code-serializer.h"
66 : #include "src/snapshot/natives.h"
67 : #include "src/snapshot/snapshot.h"
68 : #include "src/startup-data-util.h"
69 : #include "src/tracing/trace-event.h"
70 : #include "src/trap-handler/trap-handler.h"
71 : #include "src/unicode-inl.h"
72 : #include "src/v8.h"
73 : #include "src/v8threads.h"
74 : #include "src/value-serializer.h"
75 : #include "src/version.h"
76 : #include "src/vm-state-inl.h"
77 : #include "src/wasm/wasm-module.h"
78 : #include "src/wasm/wasm-objects.h"
79 : #include "src/wasm/wasm-result.h"
80 :
81 : namespace v8 {
82 :
83 : #define LOG_API(isolate, class_name, function_name) \
84 : i::RuntimeCallTimerScope _runtime_timer( \
85 : isolate, &i::RuntimeCallStats::API_##class_name##_##function_name); \
86 : LOG(isolate, ApiEntryCall("v8::" #class_name "::" #function_name))
87 :
88 : #define ENTER_V8(isolate) i::VMState<v8::OTHER> __state__((isolate))
89 :
90 : #define PREPARE_FOR_EXECUTION_GENERIC(isolate, context, class_name, \
91 : function_name, bailout_value, \
92 : HandleScopeClass, do_callback) \
93 : if (IsExecutionTerminatingCheck(isolate)) { \
94 : return bailout_value; \
95 : } \
96 : HandleScopeClass handle_scope(isolate); \
97 : CallDepthScope<do_callback> call_depth_scope(isolate, context); \
98 : LOG_API(isolate, class_name, function_name); \
99 : ENTER_V8(isolate); \
100 : bool has_pending_exception = false
101 :
102 : #define PREPARE_FOR_DEBUG_INTERFACE_EXECUTION_WITH_ISOLATE(isolate, T) \
103 : if (IsExecutionTerminatingCheck(isolate)) { \
104 : return MaybeLocal<T>(); \
105 : } \
106 : InternalEscapableScope handle_scope(isolate); \
107 : CallDepthScope<false> call_depth_scope(isolate, v8::Local<v8::Context>()); \
108 : ENTER_V8(isolate); \
109 : bool has_pending_exception = false
110 :
111 : #define PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \
112 : bailout_value, HandleScopeClass, \
113 : do_callback) \
114 : auto isolate = context.IsEmpty() \
115 : ? i::Isolate::Current() \
116 : : reinterpret_cast<i::Isolate*>(context->GetIsolate()); \
117 : PREPARE_FOR_EXECUTION_GENERIC(isolate, context, class_name, function_name, \
118 : bailout_value, HandleScopeClass, do_callback);
119 :
120 : #define PREPARE_FOR_EXECUTION_WITH_CONTEXT_IN_RUNTIME_CALL_STATS_SCOPE( \
121 : category, name, context, class_name, function_name, bailout_value, \
122 : HandleScopeClass, do_callback) \
123 : auto isolate = context.IsEmpty() \
124 : ? i::Isolate::Current() \
125 : : reinterpret_cast<i::Isolate*>(context->GetIsolate()); \
126 : TRACE_EVENT_CALL_STATS_SCOPED(isolate, category, name); \
127 : PREPARE_FOR_EXECUTION_GENERIC(isolate, context, class_name, function_name, \
128 : bailout_value, HandleScopeClass, do_callback);
129 :
130 : #define PREPARE_FOR_EXECUTION_WITH_ISOLATE(isolate, class_name, function_name, \
131 : T) \
132 : PREPARE_FOR_EXECUTION_GENERIC(isolate, Local<Context>(), class_name, \
133 : function_name, MaybeLocal<T>(), \
134 : InternalEscapableScope, false);
135 :
136 : #define PREPARE_FOR_EXECUTION(context, class_name, function_name, T) \
137 : PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \
138 : MaybeLocal<T>(), InternalEscapableScope, \
139 : false)
140 :
141 : #define PREPARE_FOR_EXECUTION_WITH_CALLBACK(context, class_name, \
142 : function_name, T) \
143 : PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \
144 : MaybeLocal<T>(), InternalEscapableScope, \
145 : true)
146 :
147 : #define PREPARE_FOR_EXECUTION_PRIMITIVE(context, class_name, function_name, T) \
148 : PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \
149 : Nothing<T>(), i::HandleScope, false)
150 :
151 : #define PREPARE_FOR_EXECUTION_BOOL(context, class_name, function_name) \
152 : PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \
153 : false, i::HandleScope, false)
154 :
155 : #ifdef DEBUG
156 : #define ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate) \
157 : i::VMState<v8::OTHER> __state__((isolate)); \
158 : i::DisallowJavascriptExecutionDebugOnly __no_script__((isolate)); \
159 : i::DisallowExceptions __no_exceptions__((isolate))
160 :
161 : #define ENTER_V8_FOR_NEW_CONTEXT(isolate) \
162 : i::VMState<v8::OTHER> __state__((isolate)); \
163 : i::DisallowExceptions __no_exceptions__((isolate))
164 : #else
165 : #define ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate) \
166 : i::VMState<v8::OTHER> __state__((isolate));
167 :
168 : #define ENTER_V8_FOR_NEW_CONTEXT(isolate) \
169 : i::VMState<v8::OTHER> __state__((isolate));
170 : #endif // DEBUG
171 :
172 : #define EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, value) \
173 : do { \
174 : if (has_pending_exception) { \
175 : call_depth_scope.Escape(); \
176 : return value; \
177 : } \
178 : } while (false)
179 :
180 :
181 : #define RETURN_ON_FAILED_EXECUTION(T) \
182 : EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, MaybeLocal<T>())
183 :
184 :
185 : #define RETURN_ON_FAILED_EXECUTION_PRIMITIVE(T) \
186 : EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, Nothing<T>())
187 :
188 : #define RETURN_ON_FAILED_EXECUTION_BOOL() \
189 : EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, false)
190 :
191 : #define RETURN_TO_LOCAL_UNCHECKED(maybe_local, T) \
192 : return maybe_local.FromMaybe(Local<T>());
193 :
194 :
195 : #define RETURN_ESCAPED(value) return handle_scope.Escape(value);
196 :
197 :
198 : namespace {
199 :
200 : Local<Context> ContextFromHeapObject(i::Handle<i::Object> obj) {
201 : return reinterpret_cast<v8::Isolate*>(i::HeapObject::cast(*obj)->GetIsolate())
202 0 : ->GetCurrentContext();
203 : }
204 :
205 : class InternalEscapableScope : public v8::EscapableHandleScope {
206 : public:
207 : explicit inline InternalEscapableScope(i::Isolate* isolate)
208 160335829 : : v8::EscapableHandleScope(reinterpret_cast<v8::Isolate*>(isolate)) {}
209 : };
210 :
211 :
212 : #ifdef DEBUG
213 : void CheckMicrotasksScopesConsistency(i::Isolate* isolate) {
214 : auto handle_scope_implementer = isolate->handle_scope_implementer();
215 : if (handle_scope_implementer->microtasks_policy() ==
216 : v8::MicrotasksPolicy::kScoped) {
217 : DCHECK(handle_scope_implementer->GetMicrotasksScopeDepth() ||
218 : !handle_scope_implementer->DebugMicrotasksScopeDepthIsZero());
219 : }
220 : }
221 : #endif
222 :
223 : template <bool do_callback>
224 : class CallDepthScope {
225 : public:
226 484589483 : explicit CallDepthScope(i::Isolate* isolate, Local<Context> context)
227 242457749 : : isolate_(isolate), context_(context), escaped_(false) {
228 : // TODO(dcarney): remove this when blink stops crashing.
229 : DCHECK(!isolate_->external_caught_exception());
230 242457749 : isolate_->handle_scope_implementer()->IncrementCallDepth();
231 242457749 : if (!context.IsEmpty()) {
232 : i::Handle<i::Context> env = Utils::OpenHandle(*context);
233 : i::HandleScopeImplementer* impl = isolate->handle_scope_implementer();
234 724526146 : if (isolate->context() != nullptr &&
235 : isolate->context()->native_context() == env->native_context() &&
236 : impl->LastEnteredContextWas(env)) {
237 239892901 : context_ = Local<Context>();
238 : } else {
239 2238833 : context_->Enter();
240 : }
241 : }
242 30038486 : if (do_callback) isolate_->FireBeforeCallEnteredCallback();
243 242457751 : }
244 242457721 : ~CallDepthScope() {
245 242457721 : if (!context_.IsEmpty()) context_->Exit();
246 242457727 : if (!escaped_) isolate_->handle_scope_implementer()->DecrementCallDepth();
247 30038485 : if (do_callback) isolate_->FireCallCompletedCallback();
248 : #ifdef DEBUG
249 : if (do_callback) CheckMicrotasksScopesConsistency(isolate_);
250 : #endif
251 242457728 : }
252 :
253 : void Escape() {
254 : DCHECK(!escaped_);
255 19710 : escaped_ = true;
256 19710 : auto handle_scope_implementer = isolate_->handle_scope_implementer();
257 : handle_scope_implementer->DecrementCallDepth();
258 : bool call_depth_is_zero = handle_scope_implementer->CallDepthIsZero();
259 19710 : isolate_->OptionalRescheduleException(call_depth_is_zero);
260 : }
261 :
262 : private:
263 : i::Isolate* const isolate_;
264 : Local<Context> context_;
265 : bool escaped_;
266 : bool do_callback_;
267 : };
268 :
269 : } // namespace
270 :
271 :
272 25576 : static ScriptOrigin GetScriptOriginForScript(i::Isolate* isolate,
273 : i::Handle<i::Script> script) {
274 25576 : i::Handle<i::Object> scriptName(script->GetNameOrSourceURL(), isolate);
275 : i::Handle<i::Object> source_map_url(script->source_mapping_url(), isolate);
276 : v8::Isolate* v8_isolate =
277 : reinterpret_cast<v8::Isolate*>(script->GetIsolate());
278 : ScriptOriginOptions options(script->origin_options());
279 : v8::ScriptOrigin origin(
280 : Utils::ToLocal(scriptName),
281 : v8::Integer::New(v8_isolate, script->line_offset()),
282 : v8::Integer::New(v8_isolate, script->column_offset()),
283 : v8::Boolean::New(v8_isolate, options.IsSharedCrossOrigin()),
284 : v8::Integer::New(v8_isolate, script->id()),
285 : Utils::ToLocal(source_map_url),
286 : v8::Boolean::New(v8_isolate, options.IsOpaque()),
287 : v8::Boolean::New(v8_isolate, script->type() == i::Script::TYPE_WASM),
288 76728 : v8::Boolean::New(v8_isolate, options.IsModule()));
289 25576 : return origin;
290 : }
291 :
292 :
293 : // --- E x c e p t i o n B e h a v i o r ---
294 :
295 :
296 0 : void i::FatalProcessOutOfMemory(const char* location) {
297 0 : i::V8::FatalProcessOutOfMemory(location, false);
298 : }
299 :
300 : // When V8 cannot allocate memory FatalProcessOutOfMemory is called. The default
301 : // OOM error handler is called and execution is stopped.
302 0 : void i::V8::FatalProcessOutOfMemory(const char* location, bool is_heap_oom) {
303 : i::Isolate* isolate = i::Isolate::Current();
304 : char last_few_messages[Heap::kTraceRingBufferSize + 1];
305 : char js_stacktrace[Heap::kStacktraceBufferSize + 1];
306 : i::HeapStats heap_stats;
307 :
308 0 : if (isolate == nullptr) {
309 : // On a background thread -> we cannot retrieve memory information from the
310 : // Isolate. Write easy-to-recognize values on the stack.
311 : memset(last_few_messages, 0x0badc0de, Heap::kTraceRingBufferSize + 1);
312 : memset(js_stacktrace, 0x0badc0de, Heap::kStacktraceBufferSize + 1);
313 : memset(&heap_stats, 0xbadc0de, sizeof(heap_stats));
314 : // Note that the embedder's oom handler won't be called in this case. We
315 : // just crash.
316 0 : FATAL("API fatal error handler returned after process out of memory");
317 : return;
318 : }
319 :
320 : memset(last_few_messages, 0, Heap::kTraceRingBufferSize + 1);
321 : memset(js_stacktrace, 0, Heap::kStacktraceBufferSize + 1);
322 :
323 : intptr_t start_marker;
324 0 : heap_stats.start_marker = &start_marker;
325 : size_t new_space_size;
326 0 : heap_stats.new_space_size = &new_space_size;
327 : size_t new_space_capacity;
328 0 : heap_stats.new_space_capacity = &new_space_capacity;
329 : size_t old_space_size;
330 0 : heap_stats.old_space_size = &old_space_size;
331 : size_t old_space_capacity;
332 0 : heap_stats.old_space_capacity = &old_space_capacity;
333 : size_t code_space_size;
334 0 : heap_stats.code_space_size = &code_space_size;
335 : size_t code_space_capacity;
336 0 : heap_stats.code_space_capacity = &code_space_capacity;
337 : size_t map_space_size;
338 0 : heap_stats.map_space_size = &map_space_size;
339 : size_t map_space_capacity;
340 0 : heap_stats.map_space_capacity = &map_space_capacity;
341 : size_t lo_space_size;
342 0 : heap_stats.lo_space_size = &lo_space_size;
343 : size_t global_handle_count;
344 0 : heap_stats.global_handle_count = &global_handle_count;
345 : size_t weak_global_handle_count;
346 0 : heap_stats.weak_global_handle_count = &weak_global_handle_count;
347 : size_t pending_global_handle_count;
348 0 : heap_stats.pending_global_handle_count = &pending_global_handle_count;
349 : size_t near_death_global_handle_count;
350 0 : heap_stats.near_death_global_handle_count = &near_death_global_handle_count;
351 : size_t free_global_handle_count;
352 0 : heap_stats.free_global_handle_count = &free_global_handle_count;
353 : size_t memory_allocator_size;
354 0 : heap_stats.memory_allocator_size = &memory_allocator_size;
355 : size_t memory_allocator_capacity;
356 0 : heap_stats.memory_allocator_capacity = &memory_allocator_capacity;
357 : size_t malloced_memory;
358 0 : heap_stats.malloced_memory = &malloced_memory;
359 : size_t malloced_peak_memory;
360 0 : heap_stats.malloced_peak_memory = &malloced_peak_memory;
361 0 : size_t objects_per_type[LAST_TYPE + 1] = {0};
362 0 : heap_stats.objects_per_type = objects_per_type;
363 0 : size_t size_per_type[LAST_TYPE + 1] = {0};
364 0 : heap_stats.size_per_type = size_per_type;
365 : int os_error;
366 0 : heap_stats.os_error = &os_error;
367 0 : heap_stats.last_few_messages = last_few_messages;
368 0 : heap_stats.js_stacktrace = js_stacktrace;
369 : intptr_t end_marker;
370 0 : heap_stats.end_marker = &end_marker;
371 0 : if (isolate->heap()->HasBeenSetUp()) {
372 : // BUG(1718): Don't use the take_snapshot since we don't support
373 : // HeapIterator here without doing a special GC.
374 0 : isolate->heap()->RecordStats(&heap_stats, false);
375 : char* first_newline = strchr(last_few_messages, '\n');
376 0 : if (first_newline == NULL || first_newline[1] == '\0')
377 : first_newline = last_few_messages;
378 0 : PrintF("\n<--- Last few GCs --->\n%s\n", first_newline);
379 0 : PrintF("\n<--- JS stacktrace --->\n%s\n", js_stacktrace);
380 : }
381 0 : Utils::ReportOOMFailure(location, is_heap_oom);
382 : // If the fatal error handler returns, we stop execution.
383 0 : FATAL("API fatal error handler returned after process out of memory");
384 : }
385 :
386 :
387 12 : void Utils::ReportApiFailure(const char* location, const char* message) {
388 12 : i::Isolate* isolate = i::Isolate::Current();
389 : FatalErrorCallback callback = isolate->exception_behavior();
390 12 : if (callback == nullptr) {
391 : base::OS::PrintError("\n#\n# Fatal error in %s\n# %s\n#\n\n", location,
392 0 : message);
393 0 : base::OS::Abort();
394 : } else {
395 12 : callback(location, message);
396 : }
397 : isolate->SignalFatalError();
398 12 : }
399 :
400 0 : void Utils::ReportOOMFailure(const char* location, bool is_heap_oom) {
401 0 : i::Isolate* isolate = i::Isolate::Current();
402 : OOMErrorCallback oom_callback = isolate->oom_behavior();
403 0 : if (oom_callback == nullptr) {
404 : // TODO(wfh): Remove this fallback once Blink is setting OOM handler. See
405 : // crbug.com/614440.
406 : FatalErrorCallback fatal_callback = isolate->exception_behavior();
407 0 : if (fatal_callback == nullptr) {
408 : base::OS::PrintError("\n#\n# Fatal %s OOM in %s\n#\n\n",
409 0 : is_heap_oom ? "javascript" : "process", location);
410 0 : base::OS::Abort();
411 : } else {
412 : fatal_callback(location,
413 : is_heap_oom
414 : ? "Allocation failed - JavaScript heap out of memory"
415 0 : : "Allocation failed - process out of memory");
416 : }
417 : } else {
418 0 : oom_callback(location, is_heap_oom);
419 : }
420 : isolate->SignalFatalError();
421 0 : }
422 :
423 : static inline bool IsExecutionTerminatingCheck(i::Isolate* isolate) {
424 242507927 : if (isolate->has_scheduled_exception()) {
425 : return isolate->scheduled_exception() ==
426 357 : isolate->heap()->termination_exception();
427 : }
428 : return false;
429 : }
430 :
431 :
432 59466 : void V8::SetNativesDataBlob(StartupData* natives_blob) {
433 59466 : i::V8::SetNativesBlob(natives_blob);
434 59466 : }
435 :
436 :
437 59466 : void V8::SetSnapshotDataBlob(StartupData* snapshot_blob) {
438 59466 : i::V8::SetSnapshotBlob(snapshot_blob);
439 59466 : }
440 :
441 : namespace {
442 :
443 8716 : class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
444 : public:
445 706 : virtual void* Allocate(size_t length) {
446 706 : void* data = AllocateUninitialized(length);
447 1412 : return data == NULL ? data : memset(data, 0, length);
448 : }
449 755 : virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
450 683 : virtual void Free(void* data, size_t) { free(data); }
451 : };
452 :
453 60 : bool RunExtraCode(Isolate* isolate, Local<Context> context,
454 : const char* utf8_source, const char* name) {
455 : base::ElapsedTimer timer;
456 : timer.Start();
457 : Context::Scope context_scope(context);
458 120 : TryCatch try_catch(isolate);
459 : Local<String> source_string;
460 60 : if (!String::NewFromUtf8(isolate, utf8_source, NewStringType::kNormal)
461 120 : .ToLocal(&source_string)) {
462 : return false;
463 : }
464 : Local<String> resource_name =
465 : String::NewFromUtf8(isolate, name, NewStringType::kNormal)
466 60 : .ToLocalChecked();
467 : ScriptOrigin origin(resource_name);
468 : ScriptCompiler::Source source(source_string, origin);
469 : Local<Script> script;
470 120 : if (!ScriptCompiler::Compile(context, &source).ToLocal(&script)) return false;
471 120 : if (script->Run(context).IsEmpty()) return false;
472 60 : if (i::FLAG_profile_deserialization) {
473 : i::PrintF("Executing custom snapshot script %s took %0.3f ms\n", name,
474 0 : timer.Elapsed().InMillisecondsF());
475 : }
476 : timer.Stop();
477 60 : CHECK(!try_catch.HasCaught());
478 : return true;
479 : }
480 :
481 291 : struct SnapshotCreatorData {
482 : explicit SnapshotCreatorData(Isolate* isolate)
483 : : isolate_(isolate),
484 : default_context_(),
485 : contexts_(isolate),
486 : templates_(isolate),
487 194 : created_(false) {}
488 :
489 : static SnapshotCreatorData* cast(void* data) {
490 : return reinterpret_cast<SnapshotCreatorData*>(data);
491 : }
492 :
493 : ArrayBufferAllocator allocator_;
494 : Isolate* isolate_;
495 : Persistent<Context> default_context_;
496 : PersistentValueVector<Context> contexts_;
497 : PersistentValueVector<Template> templates_;
498 : std::vector<SerializeInternalFieldsCallback> embedder_fields_serializers_;
499 : bool created_;
500 : };
501 :
502 : } // namespace
503 :
504 97 : SnapshotCreator::SnapshotCreator(intptr_t* external_references,
505 : StartupData* existing_snapshot) {
506 97 : i::Isolate* internal_isolate = new i::Isolate(true);
507 : Isolate* isolate = reinterpret_cast<Isolate*>(internal_isolate);
508 97 : SnapshotCreatorData* data = new SnapshotCreatorData(isolate);
509 : data->isolate_ = isolate;
510 97 : internal_isolate->set_array_buffer_allocator(&data->allocator_);
511 : internal_isolate->set_api_external_references(external_references);
512 : isolate->Enter();
513 : const StartupData* blob = existing_snapshot
514 : ? existing_snapshot
515 97 : : i::Snapshot::DefaultSnapshotBlob();
516 97 : if (blob && blob->raw_size > 0) {
517 : internal_isolate->set_snapshot_blob(blob);
518 96 : i::Snapshot::Initialize(internal_isolate);
519 : } else {
520 1 : internal_isolate->Init(nullptr);
521 : }
522 97 : data_ = data;
523 97 : }
524 :
525 97 : SnapshotCreator::~SnapshotCreator() {
526 97 : SnapshotCreatorData* data = SnapshotCreatorData::cast(data_);
527 : DCHECK(data->created_);
528 97 : Isolate* isolate = data->isolate_;
529 : isolate->Exit();
530 97 : isolate->Dispose();
531 97 : delete data;
532 97 : }
533 :
534 24 : Isolate* SnapshotCreator::GetIsolate() {
535 97 : return SnapshotCreatorData::cast(data_)->isolate_;
536 : }
537 :
538 97 : void SnapshotCreator::SetDefaultContext(Local<Context> context) {
539 : DCHECK(!context.IsEmpty());
540 97 : SnapshotCreatorData* data = SnapshotCreatorData::cast(data_);
541 : DCHECK(!data->created_);
542 : DCHECK(data->default_context_.IsEmpty());
543 97 : Isolate* isolate = data->isolate_;
544 97 : CHECK_EQ(isolate, context->GetIsolate());
545 : data->default_context_.Reset(isolate, context);
546 97 : }
547 :
548 24 : size_t SnapshotCreator::AddContext(Local<Context> context,
549 : SerializeInternalFieldsCallback callback) {
550 : DCHECK(!context.IsEmpty());
551 24 : SnapshotCreatorData* data = SnapshotCreatorData::cast(data_);
552 : DCHECK(!data->created_);
553 24 : Isolate* isolate = data->isolate_;
554 24 : CHECK_EQ(isolate, context->GetIsolate());
555 24 : size_t index = static_cast<int>(data->contexts_.Size());
556 24 : data->contexts_.Append(context);
557 24 : data->embedder_fields_serializers_.push_back(callback);
558 24 : return index;
559 : }
560 :
561 12 : size_t SnapshotCreator::AddTemplate(Local<Template> template_obj) {
562 : DCHECK(!template_obj.IsEmpty());
563 12 : SnapshotCreatorData* data = SnapshotCreatorData::cast(data_);
564 : DCHECK(!data->created_);
565 : DCHECK_EQ(reinterpret_cast<i::Isolate*>(data->isolate_),
566 : Utils::OpenHandle(*template_obj)->GetIsolate());
567 12 : size_t index = static_cast<int>(data->templates_.Size());
568 12 : data->templates_.Append(template_obj);
569 12 : return index;
570 : }
571 :
572 97 : StartupData SnapshotCreator::CreateBlob(
573 : SnapshotCreator::FunctionCodeHandling function_code_handling) {
574 97 : SnapshotCreatorData* data = SnapshotCreatorData::cast(data_);
575 97 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(data->isolate_);
576 : DCHECK(!data->created_);
577 : DCHECK(!data->default_context_.IsEmpty());
578 :
579 97 : int num_additional_contexts = static_cast<int>(data->contexts_.Size());
580 :
581 : {
582 97 : int num_templates = static_cast<int>(data->templates_.Size());
583 : i::HandleScope scope(isolate);
584 : i::Handle<i::FixedArray> templates =
585 97 : isolate->factory()->NewFixedArray(num_templates, i::TENURED);
586 109 : for (int i = 0; i < num_templates; i++) {
587 24 : templates->set(i, *v8::Utils::OpenHandle(*data->templates_.Get(i)));
588 : }
589 : isolate->heap()->SetSerializedTemplates(*templates);
590 97 : data->templates_.Clear();
591 :
592 : // We need to store the global proxy size upfront in case we need the
593 : // bootstrapper to create a global proxy before we deserialize the context.
594 : i::Handle<i::FixedArray> global_proxy_sizes =
595 97 : isolate->factory()->NewFixedArray(num_additional_contexts, i::TENURED);
596 121 : for (int i = 0; i < num_additional_contexts; i++) {
597 : i::Handle<i::Context> context =
598 48 : v8::Utils::OpenHandle(*data->contexts_.Get(i));
599 : global_proxy_sizes->set(i,
600 24 : i::Smi::FromInt(context->global_proxy()->Size()));
601 : }
602 : isolate->heap()->SetSerializedGlobalProxySizes(*global_proxy_sizes);
603 : }
604 :
605 : // If we don't do this then we end up with a stray root pointing at the
606 : // context even after we have disposed of the context.
607 : isolate->heap()->CollectAllAvailableGarbage(
608 97 : i::GarbageCollectionReason::kSnapshotCreator);
609 97 : isolate->heap()->CompactWeakFixedArrays();
610 :
611 : i::DisallowHeapAllocation no_gc_from_here_on;
612 :
613 : i::List<i::Object*> contexts(num_additional_contexts);
614 : i::Object* default_context;
615 : {
616 : i::HandleScope scope(isolate);
617 : default_context =
618 194 : *v8::Utils::OpenHandle(*data->default_context_.Get(data->isolate_));
619 : data->default_context_.Reset();
620 24 : for (int i = 0; i < num_additional_contexts; i++) {
621 : i::Handle<i::Context> context =
622 48 : v8::Utils::OpenHandle(*data->contexts_.Get(i));
623 24 : contexts.Add(*context);
624 : }
625 97 : data->contexts_.Clear();
626 : }
627 :
628 : // Complete in-object slack tracking for all functions.
629 194 : i::HeapIterator heap_iterator(isolate->heap());
630 1273116 : while (i::HeapObject* current_obj = heap_iterator.next()) {
631 1273019 : if (!current_obj->IsJSFunction()) continue;
632 : i::JSFunction* fun = i::JSFunction::cast(current_obj);
633 151075 : fun->CompleteInobjectSlackTrackingIfActive();
634 : }
635 :
636 : #ifdef DEBUG
637 : i::ExternalReferenceTable::instance(isolate)->ResetCount();
638 : #endif // DEBUG
639 :
640 194 : i::StartupSerializer startup_serializer(isolate, function_code_handling);
641 97 : startup_serializer.SerializeStrongReferences();
642 :
643 : // Serialize each context with a new partial serializer.
644 97 : i::List<i::SnapshotData*> context_snapshots(num_additional_contexts + 1);
645 :
646 : {
647 : // The default snapshot does not support embedder fields.
648 : i::PartialSerializer partial_serializer(
649 97 : isolate, &startup_serializer, v8::SerializeInternalFieldsCallback());
650 97 : partial_serializer.Serialize(&default_context, false);
651 97 : context_snapshots.Add(new i::SnapshotData(&partial_serializer));
652 : }
653 :
654 121 : for (int i = 0; i < num_additional_contexts; i++) {
655 : i::PartialSerializer partial_serializer(
656 48 : isolate, &startup_serializer, data->embedder_fields_serializers_[i]);
657 48 : partial_serializer.Serialize(&contexts[i], true);
658 24 : context_snapshots.Add(new i::SnapshotData(&partial_serializer));
659 24 : }
660 :
661 97 : startup_serializer.SerializeWeakReferencesAndDeferred();
662 :
663 : #ifdef DEBUG
664 : if (i::FLAG_external_reference_stats) {
665 : i::ExternalReferenceTable::instance(isolate)->PrintCount();
666 : }
667 : #endif // DEBUG
668 :
669 97 : i::SnapshotData startup_snapshot(&startup_serializer);
670 : StartupData result =
671 97 : i::Snapshot::CreateSnapshotBlob(&startup_snapshot, &context_snapshots);
672 :
673 : // Delete heap-allocated context snapshot instances.
674 315 : for (const auto& context_snapshot : context_snapshots) {
675 242 : delete context_snapshot;
676 : }
677 97 : data->created_ = true;
678 194 : return result;
679 : }
680 :
681 61 : StartupData V8::CreateSnapshotDataBlob(const char* embedded_source) {
682 : // Create a new isolate and a new context from scratch, optionally run
683 : // a script to embed, and serialize to create a snapshot blob.
684 : StartupData result = {nullptr, 0};
685 : base::ElapsedTimer timer;
686 : timer.Start();
687 : {
688 61 : SnapshotCreator snapshot_creator;
689 : Isolate* isolate = snapshot_creator.GetIsolate();
690 : {
691 : HandleScope scope(isolate);
692 61 : Local<Context> context = Context::New(isolate);
693 109 : if (embedded_source != NULL &&
694 48 : !RunExtraCode(isolate, context, embedded_source, "<embedded>")) {
695 0 : return result;
696 : }
697 61 : snapshot_creator.SetDefaultContext(context);
698 : }
699 : result = snapshot_creator.CreateBlob(
700 61 : SnapshotCreator::FunctionCodeHandling::kClear);
701 : }
702 :
703 61 : if (i::FLAG_profile_deserialization) {
704 : i::PrintF("Creating snapshot took %0.3f ms\n",
705 6 : timer.Elapsed().InMillisecondsF());
706 : }
707 : timer.Stop();
708 61 : return result;
709 : }
710 :
711 12 : StartupData V8::WarmUpSnapshotDataBlob(StartupData cold_snapshot_blob,
712 : const char* warmup_source) {
713 12 : CHECK(cold_snapshot_blob.raw_size > 0 && cold_snapshot_blob.data != NULL);
714 12 : CHECK(warmup_source != NULL);
715 : // Use following steps to create a warmed up snapshot blob from a cold one:
716 : // - Create a new isolate from the cold snapshot.
717 : // - Create a new context to run the warmup script. This will trigger
718 : // compilation of executed functions.
719 : // - Create a new context. This context will be unpolluted.
720 : // - Serialize the isolate and the second context into a new snapshot blob.
721 : StartupData result = {nullptr, 0};
722 : base::ElapsedTimer timer;
723 : timer.Start();
724 : {
725 12 : SnapshotCreator snapshot_creator(nullptr, &cold_snapshot_blob);
726 : Isolate* isolate = snapshot_creator.GetIsolate();
727 : {
728 : HandleScope scope(isolate);
729 12 : Local<Context> context = Context::New(isolate);
730 12 : if (!RunExtraCode(isolate, context, warmup_source, "<warm-up>")) {
731 0 : return result;
732 : }
733 : }
734 : {
735 : HandleScope handle_scope(isolate);
736 : isolate->ContextDisposedNotification(false);
737 12 : Local<Context> context = Context::New(isolate);
738 12 : snapshot_creator.SetDefaultContext(context);
739 : }
740 : result = snapshot_creator.CreateBlob(
741 12 : SnapshotCreator::FunctionCodeHandling::kKeep);
742 : }
743 :
744 12 : if (i::FLAG_profile_deserialization) {
745 : i::PrintF("Warming up snapshot took %0.3f ms\n",
746 0 : timer.Elapsed().InMillisecondsF());
747 : }
748 : timer.Stop();
749 12 : return result;
750 : }
751 :
752 :
753 114472 : void V8::SetFlagsFromString(const char* str, int length) {
754 146710 : i::FlagList::SetFlagsFromString(str, length);
755 146710 : i::FlagList::EnforceFlagImplications();
756 114472 : }
757 :
758 :
759 33158 : void V8::SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags) {
760 33158 : i::FlagList::SetFlagsFromCommandLine(argc, argv, remove_flags);
761 33158 : }
762 :
763 :
764 : RegisteredExtension* RegisteredExtension::first_extension_ = NULL;
765 :
766 :
767 0 : RegisteredExtension::RegisteredExtension(Extension* extension)
768 435922 : : extension_(extension) { }
769 :
770 :
771 0 : void RegisteredExtension::Register(RegisteredExtension* that) {
772 435922 : that->next_ = first_extension_;
773 435922 : first_extension_ = that;
774 0 : }
775 :
776 :
777 32496 : void RegisteredExtension::UnregisterAll() {
778 227220 : RegisteredExtension* re = first_extension_;
779 259716 : while (re != NULL) {
780 : RegisteredExtension* next = re->next();
781 194724 : delete re;
782 : re = next;
783 : }
784 32496 : first_extension_ = NULL;
785 32496 : }
786 :
787 :
788 435922 : void RegisterExtension(Extension* that) {
789 435922 : RegisteredExtension* extension = new RegisteredExtension(that);
790 : RegisteredExtension::Register(extension);
791 435922 : }
792 :
793 :
794 435923 : Extension::Extension(const char* name,
795 : const char* source,
796 : int dep_count,
797 : const char** deps,
798 : int source_length)
799 : : name_(name),
800 : source_length_(source_length >= 0 ?
801 : source_length :
802 435899 : (source ? static_cast<int>(strlen(source)) : 0)),
803 : source_(source, source_length_),
804 : dep_count_(dep_count),
805 : deps_(deps),
806 1307745 : auto_enable_(false) {
807 435923 : CHECK(source != NULL || source_length_ == 0);
808 435923 : }
809 :
810 60565 : ResourceConstraints::ResourceConstraints()
811 : : max_semi_space_size_(0),
812 : max_old_space_size_(0),
813 : max_executable_size_(0),
814 : stack_limit_(NULL),
815 : code_range_size_(0),
816 60565 : max_zone_pool_size_(0) {}
817 :
818 28618 : void ResourceConstraints::ConfigureDefaults(uint64_t physical_memory,
819 : uint64_t virtual_memory_limit) {
820 : #if V8_OS_ANDROID
821 : // Android has higher physical memory requirements before raising the maximum
822 : // heap size limits since it has no swap space.
823 : const uint64_t low_limit = 512ul * i::MB;
824 : const uint64_t medium_limit = 1ul * i::GB;
825 : const uint64_t high_limit = 2ul * i::GB;
826 : #else
827 : const uint64_t low_limit = 512ul * i::MB;
828 : const uint64_t medium_limit = 768ul * i::MB;
829 : const uint64_t high_limit = 1ul * i::GB;
830 : #endif
831 :
832 28618 : if (physical_memory <= low_limit) {
833 : set_max_semi_space_size(i::Heap::kMaxSemiSpaceSizeLowMemoryDevice);
834 : set_max_old_space_size(i::Heap::kMaxOldSpaceSizeLowMemoryDevice);
835 : set_max_executable_size(i::Heap::kMaxExecutableSizeLowMemoryDevice);
836 : set_max_zone_pool_size(i::AccountingAllocator::kMaxPoolSizeLowMemoryDevice);
837 28618 : } else if (physical_memory <= medium_limit) {
838 : set_max_semi_space_size(i::Heap::kMaxSemiSpaceSizeMediumMemoryDevice);
839 : set_max_old_space_size(i::Heap::kMaxOldSpaceSizeMediumMemoryDevice);
840 : set_max_executable_size(i::Heap::kMaxExecutableSizeMediumMemoryDevice);
841 : set_max_zone_pool_size(
842 : i::AccountingAllocator::kMaxPoolSizeMediumMemoryDevice);
843 28618 : } else if (physical_memory <= high_limit) {
844 : set_max_semi_space_size(i::Heap::kMaxSemiSpaceSizeHighMemoryDevice);
845 : set_max_old_space_size(i::Heap::kMaxOldSpaceSizeHighMemoryDevice);
846 : set_max_executable_size(i::Heap::kMaxExecutableSizeHighMemoryDevice);
847 : set_max_zone_pool_size(
848 : i::AccountingAllocator::kMaxPoolSizeHighMemoryDevice);
849 : } else {
850 : set_max_semi_space_size(i::Heap::kMaxSemiSpaceSizeHugeMemoryDevice);
851 : set_max_old_space_size(i::Heap::kMaxOldSpaceSizeHugeMemoryDevice);
852 : set_max_executable_size(i::Heap::kMaxExecutableSizeHugeMemoryDevice);
853 : set_max_zone_pool_size(
854 : i::AccountingAllocator::kMaxPoolSizeHugeMemoryDevice);
855 : }
856 :
857 28618 : if (virtual_memory_limit > 0 && i::kRequiresCodeRange) {
858 : // Reserve no more than 1/8 of the memory for the code range, but at most
859 : // kMaximalCodeRangeSize.
860 : set_code_range_size(
861 : i::Min(i::kMaximalCodeRangeSize / i::MB,
862 0 : static_cast<size_t>((virtual_memory_limit >> 3) / i::MB)));
863 : }
864 28618 : }
865 :
866 :
867 121202 : void SetResourceConstraints(i::Isolate* isolate,
868 121202 : const ResourceConstraints& constraints) {
869 : int semi_space_size = constraints.max_semi_space_size();
870 : int old_space_size = constraints.max_old_space_size();
871 : int max_executable_size = constraints.max_executable_size();
872 : size_t code_range_size = constraints.code_range_size();
873 : size_t max_pool_size = constraints.max_zone_pool_size();
874 60601 : if (semi_space_size != 0 || old_space_size != 0 ||
875 31953 : max_executable_size != 0 || code_range_size != 0) {
876 : isolate->heap()->ConfigureHeap(semi_space_size, old_space_size,
877 28648 : max_executable_size, code_range_size);
878 : }
879 60601 : isolate->allocator()->ConfigureSegmentPool(max_pool_size);
880 :
881 60601 : if (constraints.stack_limit() != NULL) {
882 0 : uintptr_t limit = reinterpret_cast<uintptr_t>(constraints.stack_limit());
883 0 : isolate->stack_guard()->SetStackLimit(limit);
884 : }
885 60601 : }
886 :
887 :
888 13126536 : i::Object** V8::GlobalizeReference(i::Isolate* isolate, i::Object** obj) {
889 8751024 : LOG_API(isolate, Persistent, New);
890 8751024 : i::Handle<i::Object> result = isolate->global_handles()->Create(*obj);
891 : #ifdef VERIFY_HEAP
892 : if (i::FLAG_verify_heap) {
893 : (*obj)->ObjectVerify();
894 : }
895 : #endif // VERIFY_HEAP
896 4375512 : return result.location();
897 : }
898 :
899 :
900 12 : i::Object** V8::CopyPersistent(i::Object** obj) {
901 12 : i::Handle<i::Object> result = i::GlobalHandles::CopyGlobal(obj);
902 : #ifdef VERIFY_HEAP
903 : if (i::FLAG_verify_heap) {
904 : (*obj)->ObjectVerify();
905 : }
906 : #endif // VERIFY_HEAP
907 12 : return result.location();
908 : }
909 :
910 0 : void V8::RegisterExternallyReferencedObject(i::Object** object,
911 : i::Isolate* isolate) {
912 0 : isolate->heap()->RegisterExternallyReferencedObject(object);
913 0 : }
914 :
915 0 : void V8::MakeWeak(i::Object** location, void* parameter,
916 : int embedder_field_index1, int embedder_field_index2,
917 : WeakCallbackInfo<void>::Callback weak_callback) {
918 : WeakCallbackType type = WeakCallbackType::kParameter;
919 0 : if (embedder_field_index1 == 0) {
920 : if (embedder_field_index2 == 1) {
921 : type = WeakCallbackType::kInternalFields;
922 : } else {
923 : DCHECK_EQ(embedder_field_index2, -1);
924 : type = WeakCallbackType::kInternalFields;
925 : }
926 : } else {
927 : DCHECK_EQ(embedder_field_index1, -1);
928 : DCHECK_EQ(embedder_field_index2, -1);
929 : }
930 0 : i::GlobalHandles::MakeWeak(location, parameter, weak_callback, type);
931 0 : }
932 :
933 41665 : void V8::MakeWeak(i::Object** location, void* parameter,
934 : WeakCallbackInfo<void>::Callback weak_callback,
935 : WeakCallbackType type) {
936 41665 : i::GlobalHandles::MakeWeak(location, parameter, weak_callback, type);
937 41665 : }
938 :
939 13 : void V8::MakeWeak(i::Object*** location_addr) {
940 13 : i::GlobalHandles::MakeWeak(location_addr);
941 13 : }
942 :
943 30 : void* V8::ClearWeak(i::Object** location) {
944 30 : return i::GlobalHandles::ClearWeakness(location);
945 : }
946 :
947 4372648 : void V8::DisposeGlobal(i::Object** location) {
948 4372648 : i::GlobalHandles::Destroy(location);
949 4372648 : }
950 :
951 12288 : Value* V8::Eternalize(Isolate* v8_isolate, Value* value) {
952 24576 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
953 : i::Object* object = *Utils::OpenHandle(value);
954 12288 : int index = -1;
955 12288 : isolate->eternal_handles()->Create(isolate, object, &index);
956 : return reinterpret_cast<Value*>(
957 24576 : isolate->eternal_handles()->Get(index).location());
958 : }
959 :
960 :
961 0 : void V8::FromJustIsNothing() {
962 : Utils::ApiCheck(false, "v8::FromJust", "Maybe value is Nothing.");
963 0 : }
964 :
965 :
966 0 : void V8::ToLocalEmpty() {
967 : Utils::ApiCheck(false, "v8::ToLocalChecked", "Empty MaybeLocal.");
968 0 : }
969 :
970 0 : void V8::InternalFieldOutOfBounds(int index) {
971 : Utils::ApiCheck(0 <= index && index < kInternalFieldsInWeakCallback,
972 : "WeakCallbackInfo::GetInternalField",
973 0 : "Internal field out of bounds.");
974 0 : }
975 :
976 :
977 : // --- H a n d l e s ---
978 :
979 :
980 584756236 : HandleScope::HandleScope(Isolate* isolate) {
981 584756464 : Initialize(isolate);
982 584756230 : }
983 :
984 :
985 745348388 : void HandleScope::Initialize(Isolate* isolate) {
986 41349 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
987 : // We do not want to check the correct usage of the Locker class all over the
988 : // place, so we do it only here: Without a HandleScope, an embedder can do
989 : // almost nothing, so it is enough to check in this central place.
990 : // We make an exception if the serializer is enabled, which means that the
991 : // Isolate is exclusively used to create a snapshot.
992 : Utils::ApiCheck(
993 745389719 : !v8::Locker::IsActive() ||
994 745389733 : internal_isolate->thread_manager()->IsLockedByCurrentThread() ||
995 : internal_isolate->serializer_enabled(),
996 : "HandleScope::HandleScope",
997 : "Entering the V8 API without proper locking in place");
998 : i::HandleScopeData* current = internal_isolate->handle_scope_data();
999 745348384 : isolate_ = internal_isolate;
1000 745348384 : prev_next_ = current->next;
1001 745348384 : prev_limit_ = current->limit;
1002 745348384 : current->level++;
1003 745348384 : }
1004 :
1005 :
1006 745348134 : HandleScope::~HandleScope() {
1007 745348350 : i::HandleScope::CloseScope(isolate_, prev_next_, prev_limit_);
1008 745348148 : }
1009 :
1010 0 : V8_NORETURN void* HandleScope::operator new(size_t) {
1011 0 : base::OS::Abort();
1012 : abort();
1013 : }
1014 :
1015 0 : void HandleScope::operator delete(void*, size_t) { base::OS::Abort(); }
1016 :
1017 7042 : int HandleScope::NumberOfHandles(Isolate* isolate) {
1018 : return i::HandleScope::NumberOfHandles(
1019 7042 : reinterpret_cast<i::Isolate*>(isolate));
1020 : }
1021 :
1022 :
1023 164451669 : i::Object** HandleScope::CreateHandle(i::Isolate* isolate, i::Object* value) {
1024 164451666 : return i::HandleScope::CreateHandle(isolate, value);
1025 : }
1026 :
1027 :
1028 190 : i::Object** HandleScope::CreateHandle(i::HeapObject* heap_object,
1029 : i::Object* value) {
1030 : DCHECK(heap_object->IsHeapObject());
1031 190 : return i::HandleScope::CreateHandle(heap_object->GetIsolate(), value);
1032 : }
1033 :
1034 :
1035 160591991 : EscapableHandleScope::EscapableHandleScope(Isolate* v8_isolate) {
1036 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
1037 160591991 : escape_slot_ = CreateHandle(isolate, isolate->heap()->the_hole_value());
1038 160591984 : Initialize(v8_isolate);
1039 160591977 : }
1040 :
1041 :
1042 160531632 : i::Object** EscapableHandleScope::Escape(i::Object** escape_value) {
1043 160531638 : i::Heap* heap = reinterpret_cast<i::Isolate*>(GetIsolate())->heap();
1044 160531632 : Utils::ApiCheck((*escape_slot_)->IsTheHole(heap->isolate()),
1045 : "EscapableHandleScope::Escape", "Escape value set twice");
1046 160531631 : if (escape_value == NULL) {
1047 12 : *escape_slot_ = heap->undefined_value();
1048 6 : return NULL;
1049 : }
1050 160531625 : *escape_slot_ = *escape_value;
1051 160531625 : return escape_slot_;
1052 : }
1053 :
1054 0 : V8_NORETURN void* EscapableHandleScope::operator new(size_t) {
1055 0 : base::OS::Abort();
1056 : abort();
1057 : }
1058 :
1059 0 : void EscapableHandleScope::operator delete(void*, size_t) { base::OS::Abort(); }
1060 :
1061 6 : SealHandleScope::SealHandleScope(Isolate* isolate)
1062 6 : : isolate_(reinterpret_cast<i::Isolate*>(isolate)) {
1063 : i::HandleScopeData* current = isolate_->handle_scope_data();
1064 6 : prev_limit_ = current->limit;
1065 6 : current->limit = current->next;
1066 6 : prev_sealed_level_ = current->sealed_level;
1067 6 : current->sealed_level = current->level;
1068 6 : }
1069 :
1070 :
1071 6 : SealHandleScope::~SealHandleScope() {
1072 6 : i::HandleScopeData* current = isolate_->handle_scope_data();
1073 : DCHECK_EQ(current->next, current->limit);
1074 6 : current->limit = prev_limit_;
1075 : DCHECK_EQ(current->level, current->sealed_level);
1076 6 : current->sealed_level = prev_sealed_level_;
1077 6 : }
1078 :
1079 0 : V8_NORETURN void* SealHandleScope::operator new(size_t) {
1080 0 : base::OS::Abort();
1081 : abort();
1082 : }
1083 :
1084 0 : void SealHandleScope::operator delete(void*, size_t) { base::OS::Abort(); }
1085 :
1086 3232139 : void Context::Enter() {
1087 : i::Handle<i::Context> env = Utils::OpenHandle(this);
1088 6464290 : i::Isolate* isolate = env->GetIsolate();
1089 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1090 : i::HandleScopeImplementer* impl = isolate->handle_scope_implementer();
1091 : impl->EnterContext(env);
1092 : impl->SaveContext(isolate->context());
1093 : isolate->set_context(*env);
1094 3232141 : }
1095 :
1096 :
1097 3215952 : void Context::Exit() {
1098 : i::Handle<i::Context> env = Utils::OpenHandle(this);
1099 3215952 : i::Isolate* isolate = env->GetIsolate();
1100 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1101 : i::HandleScopeImplementer* impl = isolate->handle_scope_implementer();
1102 3215959 : if (!Utils::ApiCheck(impl->LastEnteredContextWas(env),
1103 : "v8::Context::Exit()",
1104 : "Cannot exit non-entered context")) {
1105 3215968 : return;
1106 : }
1107 : impl->LeaveContext();
1108 : isolate->set_context(impl->RestoreContext());
1109 : }
1110 :
1111 :
1112 : static void* DecodeSmiToAligned(i::Object* value, const char* location) {
1113 : Utils::ApiCheck(value->IsSmi(), location, "Not a Smi");
1114 : return reinterpret_cast<void*>(value);
1115 : }
1116 :
1117 :
1118 : static i::Smi* EncodeAlignedAsSmi(void* value, const char* location) {
1119 : i::Smi* smi = reinterpret_cast<i::Smi*>(value);
1120 : Utils::ApiCheck(smi->IsSmi(), location, "Pointer is not aligned");
1121 : return smi;
1122 : }
1123 :
1124 :
1125 133191 : static i::Handle<i::FixedArray> EmbedderDataFor(Context* context,
1126 : int index,
1127 : bool can_grow,
1128 : const char* location) {
1129 : i::Handle<i::Context> env = Utils::OpenHandle(context);
1130 : i::Isolate* isolate = env->GetIsolate();
1131 : bool ok =
1132 133191 : Utils::ApiCheck(env->IsNativeContext(),
1133 : location,
1134 266382 : "Not a native context") &&
1135 133191 : Utils::ApiCheck(index >= 0, location, "Negative index");
1136 133191 : if (!ok) return i::Handle<i::FixedArray>();
1137 : i::Handle<i::FixedArray> data(env->embedder_data());
1138 133191 : if (index < data->length()) return data;
1139 1500 : if (!Utils::ApiCheck(can_grow, location, "Index too large")) {
1140 0 : return i::Handle<i::FixedArray>();
1141 : }
1142 3000 : int new_size = i::Max(index, data->length() << 1) + 1;
1143 1500 : int grow_by = new_size - data->length();
1144 1500 : data = isolate->factory()->CopyFixedArrayAndGrow(data, grow_by);
1145 : env->set_embedder_data(*data);
1146 1500 : return data;
1147 : }
1148 :
1149 :
1150 0 : v8::Local<v8::Value> Context::SlowGetEmbedderData(int index) {
1151 : const char* location = "v8::Context::GetEmbedderData()";
1152 0 : i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, false, location);
1153 0 : if (data.is_null()) return Local<Value>();
1154 : i::Handle<i::Object> result(data->get(index), data->GetIsolate());
1155 : return Utils::ToLocal(result);
1156 : }
1157 :
1158 :
1159 52 : void Context::SetEmbedderData(int index, v8::Local<Value> value) {
1160 : const char* location = "v8::Context::SetEmbedderData()";
1161 52 : i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, true, location);
1162 104 : if (data.is_null()) return;
1163 : i::Handle<i::Object> val = Utils::OpenHandle(*value);
1164 52 : data->set(index, *val);
1165 : DCHECK_EQ(*Utils::OpenHandle(*value),
1166 : *Utils::OpenHandle(*GetEmbedderData(index)));
1167 : }
1168 :
1169 :
1170 0 : void* Context::SlowGetAlignedPointerFromEmbedderData(int index) {
1171 : const char* location = "v8::Context::GetAlignedPointerFromEmbedderData()";
1172 0 : i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, false, location);
1173 0 : if (data.is_null()) return NULL;
1174 0 : return DecodeSmiToAligned(data->get(index), location);
1175 : }
1176 :
1177 :
1178 133139 : void Context::SetAlignedPointerInEmbedderData(int index, void* value) {
1179 : const char* location = "v8::Context::SetAlignedPointerInEmbedderData()";
1180 133139 : i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, true, location);
1181 : data->set(index, EncodeAlignedAsSmi(value, location));
1182 : DCHECK_EQ(value, GetAlignedPointerFromEmbedderData(index));
1183 133139 : }
1184 :
1185 :
1186 : // --- T e m p l a t e ---
1187 :
1188 :
1189 : static void InitializeTemplate(i::Handle<i::TemplateInfo> that, int type) {
1190 : that->set_number_of_properties(0);
1191 4212637 : that->set_tag(i::Smi::FromInt(type));
1192 : }
1193 :
1194 :
1195 2160766 : void Template::Set(v8::Local<Name> name, v8::Local<Data> value,
1196 : v8::PropertyAttribute attribute) {
1197 : auto templ = Utils::OpenHandle(this);
1198 : i::Isolate* isolate = templ->GetIsolate();
1199 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1200 : i::HandleScope scope(isolate);
1201 : auto value_obj = Utils::OpenHandle(*value);
1202 2160766 : CHECK(!value_obj->IsJSReceiver() || value_obj->IsTemplateInfo());
1203 2160766 : if (value_obj->IsObjectTemplateInfo()) {
1204 188951 : templ->set_serial_number(i::Smi::kZero);
1205 188951 : if (templ->IsFunctionTemplateInfo()) {
1206 : i::Handle<i::FunctionTemplateInfo>::cast(templ)->set_do_not_cache(true);
1207 : }
1208 : }
1209 : i::ApiNatives::AddDataProperty(isolate, templ, Utils::OpenHandle(*name),
1210 : value_obj,
1211 2160766 : static_cast<i::PropertyAttributes>(attribute));
1212 2160766 : }
1213 :
1214 0 : void Template::SetPrivate(v8::Local<Private> name, v8::Local<Data> value,
1215 : v8::PropertyAttribute attribute) {
1216 : Set(Utils::ToLocal(Utils::OpenHandle(reinterpret_cast<Name*>(*name))), value,
1217 0 : attribute);
1218 0 : }
1219 :
1220 107 : void Template::SetAccessorProperty(
1221 : v8::Local<v8::Name> name,
1222 : v8::Local<FunctionTemplate> getter,
1223 : v8::Local<FunctionTemplate> setter,
1224 : v8::PropertyAttribute attribute,
1225 : v8::AccessControl access_control) {
1226 : // TODO(verwaest): Remove |access_control|.
1227 : DCHECK_EQ(v8::DEFAULT, access_control);
1228 : auto templ = Utils::OpenHandle(this);
1229 : auto isolate = templ->GetIsolate();
1230 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1231 : DCHECK(!name.IsEmpty());
1232 : DCHECK(!getter.IsEmpty() || !setter.IsEmpty());
1233 : i::HandleScope scope(isolate);
1234 : i::ApiNatives::AddAccessorProperty(
1235 : isolate, templ, Utils::OpenHandle(*name),
1236 : Utils::OpenHandle(*getter, true), Utils::OpenHandle(*setter, true),
1237 107 : static_cast<i::PropertyAttributes>(attribute));
1238 107 : }
1239 :
1240 :
1241 : // --- F u n c t i o n T e m p l a t e ---
1242 3763083 : static void InitializeFunctionTemplate(
1243 : i::Handle<i::FunctionTemplateInfo> info) {
1244 : InitializeTemplate(info, Consts::FUNCTION_TEMPLATE);
1245 : info->set_flag(0);
1246 3763085 : }
1247 :
1248 : static Local<ObjectTemplate> ObjectTemplateNew(
1249 : i::Isolate* isolate, v8::Local<FunctionTemplate> constructor,
1250 : bool do_not_cache);
1251 :
1252 187898 : Local<ObjectTemplate> FunctionTemplate::PrototypeTemplate() {
1253 : i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate();
1254 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
1255 : i::Handle<i::Object> result(Utils::OpenHandle(this)->prototype_template(),
1256 : i_isolate);
1257 187898 : if (result->IsUndefined(i_isolate)) {
1258 : // Do not cache prototype objects.
1259 : result = Utils::OpenHandle(
1260 125818 : *ObjectTemplateNew(i_isolate, Local<FunctionTemplate>(), true));
1261 62909 : Utils::OpenHandle(this)->set_prototype_template(*result);
1262 : }
1263 187898 : return ToApiHandle<ObjectTemplate>(result);
1264 : }
1265 :
1266 6 : void FunctionTemplate::SetPrototypeProviderTemplate(
1267 : Local<FunctionTemplate> prototype_provider) {
1268 : i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate();
1269 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
1270 : i::Handle<i::Object> result = Utils::OpenHandle(*prototype_provider);
1271 : auto info = Utils::OpenHandle(this);
1272 6 : CHECK(info->prototype_template()->IsUndefined(i_isolate));
1273 6 : CHECK(info->parent_template()->IsUndefined(i_isolate));
1274 6 : info->set_prototype_provider_template(*result);
1275 6 : }
1276 :
1277 5486241 : static void EnsureNotInstantiated(i::Handle<i::FunctionTemplateInfo> info,
1278 : const char* func) {
1279 5486241 : Utils::ApiCheck(!info->instantiated(), func,
1280 : "FunctionTemplate already instantiated");
1281 5486244 : }
1282 :
1283 :
1284 275 : void FunctionTemplate::Inherit(v8::Local<FunctionTemplate> value) {
1285 : auto info = Utils::OpenHandle(this);
1286 275 : EnsureNotInstantiated(info, "v8::FunctionTemplate::Inherit");
1287 : i::Isolate* i_isolate = info->GetIsolate();
1288 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
1289 275 : CHECK(info->prototype_provider_template()->IsUndefined(i_isolate));
1290 275 : info->set_parent_template(*Utils::OpenHandle(*value));
1291 275 : }
1292 :
1293 3763086 : static Local<FunctionTemplate> FunctionTemplateNew(
1294 : i::Isolate* isolate, FunctionCallback callback, v8::Local<Value> data,
1295 : v8::Local<Signature> signature, int length, bool do_not_cache,
1296 : v8::Local<Private> cached_property_name = v8::Local<Private>()) {
1297 : i::Handle<i::Struct> struct_obj =
1298 3763086 : isolate->factory()->NewStruct(i::FUNCTION_TEMPLATE_INFO_TYPE);
1299 : i::Handle<i::FunctionTemplateInfo> obj =
1300 : i::Handle<i::FunctionTemplateInfo>::cast(struct_obj);
1301 3763083 : InitializeFunctionTemplate(obj);
1302 : obj->set_do_not_cache(do_not_cache);
1303 : int next_serial_number = i::FunctionTemplateInfo::kInvalidSerialNumber;
1304 3763086 : if (!do_not_cache) {
1305 : next_serial_number = isolate->heap()->GetNextTemplateSerialNumber();
1306 : }
1307 3763086 : obj->set_serial_number(i::Smi::FromInt(next_serial_number));
1308 3763087 : if (callback != 0) {
1309 3631058 : if (data.IsEmpty()) {
1310 : data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1311 : }
1312 3631058 : Utils::ToLocal(obj)->SetCallHandler(callback, data);
1313 : }
1314 : obj->set_length(length);
1315 : obj->set_undetectable(false);
1316 : obj->set_needs_access_check(false);
1317 : obj->set_accept_any_receiver(true);
1318 3763089 : if (!signature.IsEmpty())
1319 187670 : obj->set_signature(*Utils::OpenHandle(*signature));
1320 : obj->set_cached_property_name(
1321 : cached_property_name.IsEmpty()
1322 3763062 : ? isolate->heap()->the_hole_value()
1323 7526178 : : *Utils::OpenHandle(*cached_property_name));
1324 3763088 : return Utils::ToLocal(obj);
1325 : }
1326 :
1327 3720162 : Local<FunctionTemplate> FunctionTemplate::New(
1328 : Isolate* isolate, FunctionCallback callback, v8::Local<Value> data,
1329 : v8::Local<Signature> signature, int length, ConstructorBehavior behavior) {
1330 3720167 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
1331 : // Changes to the environment cannot be captured in the snapshot. Expect no
1332 : // function templates when the isolate is created for serialization.
1333 7440329 : LOG_API(i_isolate, FunctionTemplate, New);
1334 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
1335 : auto templ =
1336 3720167 : FunctionTemplateNew(i_isolate, callback, data, signature, length, false);
1337 3720167 : if (behavior == ConstructorBehavior::kThrow) templ->RemovePrototype();
1338 7440336 : return templ;
1339 : }
1340 :
1341 12 : MaybeLocal<FunctionTemplate> FunctionTemplate::FromSnapshot(Isolate* isolate,
1342 : size_t index) {
1343 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
1344 12 : i::FixedArray* templates = i_isolate->heap()->serialized_templates();
1345 12 : int int_index = static_cast<int>(index);
1346 12 : if (int_index < templates->length()) {
1347 : i::Object* info = templates->get(int_index);
1348 6 : if (info->IsFunctionTemplateInfo()) {
1349 : return Utils::ToLocal(i::Handle<i::FunctionTemplateInfo>(
1350 6 : i::FunctionTemplateInfo::cast(info)));
1351 : }
1352 : }
1353 6 : return Local<FunctionTemplate>();
1354 : }
1355 :
1356 27 : Local<FunctionTemplate> FunctionTemplate::NewWithCache(
1357 : Isolate* isolate, FunctionCallback callback, Local<Private> cache_property,
1358 : Local<Value> data, Local<Signature> signature, int length) {
1359 27 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
1360 54 : LOG_API(i_isolate, FunctionTemplate, NewWithCache);
1361 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
1362 : return FunctionTemplateNew(i_isolate, callback, data, signature, length,
1363 54 : false, cache_property);
1364 : }
1365 :
1366 62688 : Local<Signature> Signature::New(Isolate* isolate,
1367 : Local<FunctionTemplate> receiver) {
1368 62688 : return Utils::SignatureToLocal(Utils::OpenHandle(*receiver));
1369 : }
1370 :
1371 :
1372 21 : Local<AccessorSignature> AccessorSignature::New(
1373 : Isolate* isolate, Local<FunctionTemplate> receiver) {
1374 21 : return Utils::AccessorSignatureToLocal(Utils::OpenHandle(*receiver));
1375 : }
1376 :
1377 :
1378 : #define SET_FIELD_WRAPPED(obj, setter, cdata) do { \
1379 : i::Handle<i::Object> foreign = FromCData(obj->GetIsolate(), cdata); \
1380 : (obj)->setter(*foreign); \
1381 : } while (false)
1382 :
1383 3631083 : void FunctionTemplate::SetCallHandler(FunctionCallback callback,
1384 : v8::Local<Value> data) {
1385 : auto info = Utils::OpenHandle(this);
1386 3631083 : EnsureNotInstantiated(info, "v8::FunctionTemplate::SetCallHandler");
1387 : i::Isolate* isolate = info->GetIsolate();
1388 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1389 : i::HandleScope scope(isolate);
1390 : i::Handle<i::Struct> struct_obj =
1391 3631086 : isolate->factory()->NewStruct(i::TUPLE2_TYPE);
1392 : i::Handle<i::CallHandlerInfo> obj =
1393 : i::Handle<i::CallHandlerInfo>::cast(struct_obj);
1394 7262171 : SET_FIELD_WRAPPED(obj, set_callback, callback);
1395 3631086 : if (data.IsEmpty()) {
1396 : data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1397 : }
1398 3631086 : obj->set_data(*Utils::OpenHandle(*data));
1399 3631087 : info->set_call_code(*obj);
1400 3631086 : }
1401 :
1402 :
1403 70546 : static i::Handle<i::AccessorInfo> SetAccessorInfoProperties(
1404 : i::Handle<i::AccessorInfo> obj, v8::Local<Name> name,
1405 : v8::AccessControl settings, v8::PropertyAttribute attributes,
1406 : v8::Local<AccessorSignature> signature) {
1407 70546 : obj->set_name(*Utils::OpenHandle(*name));
1408 70546 : if (settings & ALL_CAN_READ) obj->set_all_can_read(true);
1409 70546 : if (settings & ALL_CAN_WRITE) obj->set_all_can_write(true);
1410 : obj->set_property_attributes(static_cast<i::PropertyAttributes>(attributes));
1411 70546 : if (!signature.IsEmpty()) {
1412 21 : obj->set_expected_receiver_type(*Utils::OpenHandle(*signature));
1413 : }
1414 70546 : return obj;
1415 : }
1416 :
1417 : namespace {
1418 :
1419 : template <typename Getter, typename Setter>
1420 70546 : i::Handle<i::AccessorInfo> MakeAccessorInfo(
1421 : v8::Local<Name> name, Getter getter, Setter setter, v8::Local<Value> data,
1422 : v8::AccessControl settings, v8::PropertyAttribute attributes,
1423 : v8::Local<AccessorSignature> signature, bool is_special_data_property,
1424 : bool replace_on_access) {
1425 : i::Isolate* isolate = Utils::OpenHandle(*name)->GetIsolate();
1426 70546 : i::Handle<i::AccessorInfo> obj = isolate->factory()->NewAccessorInfo();
1427 141092 : SET_FIELD_WRAPPED(obj, set_getter, getter);
1428 : DCHECK_IMPLIES(replace_on_access,
1429 : is_special_data_property && setter == nullptr);
1430 70546 : if (is_special_data_property && setter == nullptr) {
1431 : setter = reinterpret_cast<Setter>(&i::Accessors::ReconfigureToDataProperty);
1432 : }
1433 141092 : SET_FIELD_WRAPPED(obj, set_setter, setter);
1434 70546 : i::Address redirected = obj->redirected_getter();
1435 211584 : if (redirected != nullptr) SET_FIELD_WRAPPED(obj, set_js_getter, redirected);
1436 70546 : if (data.IsEmpty()) {
1437 : data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1438 : }
1439 70546 : obj->set_data(*Utils::OpenHandle(*data));
1440 : obj->set_is_special_data_property(is_special_data_property);
1441 : obj->set_replace_on_access(replace_on_access);
1442 70546 : return SetAccessorInfoProperties(obj, name, settings, attributes, signature);
1443 : }
1444 :
1445 : } // namespace
1446 :
1447 71240 : Local<ObjectTemplate> FunctionTemplate::InstanceTemplate() {
1448 : i::Handle<i::FunctionTemplateInfo> handle = Utils::OpenHandle(this, true);
1449 142480 : if (!Utils::ApiCheck(!handle.is_null(),
1450 : "v8::FunctionTemplate::InstanceTemplate()",
1451 : "Reading from empty handle")) {
1452 0 : return Local<ObjectTemplate>();
1453 : }
1454 : i::Isolate* isolate = handle->GetIsolate();
1455 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1456 71240 : if (handle->instance_template()->IsUndefined(isolate)) {
1457 : Local<ObjectTemplate> templ =
1458 : ObjectTemplate::New(isolate, ToApiHandle<FunctionTemplate>(handle));
1459 63836 : handle->set_instance_template(*Utils::OpenHandle(*templ));
1460 : }
1461 : i::Handle<i::ObjectTemplateInfo> result(
1462 : i::ObjectTemplateInfo::cast(handle->instance_template()));
1463 : return Utils::ToLocal(result);
1464 : }
1465 :
1466 :
1467 14 : void FunctionTemplate::SetLength(int length) {
1468 : auto info = Utils::OpenHandle(this);
1469 14 : EnsureNotInstantiated(info, "v8::FunctionTemplate::SetLength");
1470 : auto isolate = info->GetIsolate();
1471 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1472 : info->set_length(length);
1473 14 : }
1474 :
1475 :
1476 62710 : void FunctionTemplate::SetClassName(Local<String> name) {
1477 : auto info = Utils::OpenHandle(this);
1478 62710 : EnsureNotInstantiated(info, "v8::FunctionTemplate::SetClassName");
1479 : auto isolate = info->GetIsolate();
1480 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1481 62710 : info->set_class_name(*Utils::OpenHandle(*name));
1482 62710 : }
1483 :
1484 :
1485 8 : void FunctionTemplate::SetAcceptAnyReceiver(bool value) {
1486 : auto info = Utils::OpenHandle(this);
1487 8 : EnsureNotInstantiated(info, "v8::FunctionTemplate::SetAcceptAnyReceiver");
1488 : auto isolate = info->GetIsolate();
1489 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1490 : info->set_accept_any_receiver(value);
1491 8 : }
1492 :
1493 :
1494 299 : void FunctionTemplate::SetHiddenPrototype(bool value) {
1495 : auto info = Utils::OpenHandle(this);
1496 299 : EnsureNotInstantiated(info, "v8::FunctionTemplate::SetHiddenPrototype");
1497 : auto isolate = info->GetIsolate();
1498 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1499 : info->set_hidden_prototype(value);
1500 299 : }
1501 :
1502 :
1503 1748625 : void FunctionTemplate::ReadOnlyPrototype() {
1504 : auto info = Utils::OpenHandle(this);
1505 1748625 : EnsureNotInstantiated(info, "v8::FunctionTemplate::ReadOnlyPrototype");
1506 : auto isolate = info->GetIsolate();
1507 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1508 : info->set_read_only_prototype(true);
1509 1748625 : }
1510 :
1511 :
1512 41188 : void FunctionTemplate::RemovePrototype() {
1513 : auto info = Utils::OpenHandle(this);
1514 41188 : EnsureNotInstantiated(info, "v8::FunctionTemplate::RemovePrototype");
1515 : auto isolate = info->GetIsolate();
1516 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1517 : info->set_remove_prototype(true);
1518 41188 : }
1519 :
1520 :
1521 : // --- O b j e c t T e m p l a t e ---
1522 :
1523 :
1524 258010 : Local<ObjectTemplate> ObjectTemplate::New(
1525 : Isolate* isolate, v8::Local<FunctionTemplate> constructor) {
1526 258010 : return New(reinterpret_cast<i::Isolate*>(isolate), constructor);
1527 : }
1528 :
1529 :
1530 0 : Local<ObjectTemplate> ObjectTemplate::New() {
1531 0 : return New(i::Isolate::Current(), Local<FunctionTemplate>());
1532 : }
1533 :
1534 449554 : static Local<ObjectTemplate> ObjectTemplateNew(
1535 449554 : i::Isolate* isolate, v8::Local<FunctionTemplate> constructor,
1536 : bool do_not_cache) {
1537 899108 : LOG_API(isolate, ObjectTemplate, New);
1538 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1539 : i::Handle<i::Struct> struct_obj =
1540 449554 : isolate->factory()->NewStruct(i::OBJECT_TEMPLATE_INFO_TYPE);
1541 : i::Handle<i::ObjectTemplateInfo> obj =
1542 : i::Handle<i::ObjectTemplateInfo>::cast(struct_obj);
1543 : InitializeTemplate(obj, Consts::OBJECT_TEMPLATE);
1544 : int next_serial_number = 0;
1545 449554 : if (!do_not_cache) {
1546 : next_serial_number = isolate->heap()->GetNextTemplateSerialNumber();
1547 : }
1548 449554 : obj->set_serial_number(i::Smi::FromInt(next_serial_number));
1549 449554 : if (!constructor.IsEmpty())
1550 66554 : obj->set_constructor(*Utils::OpenHandle(*constructor));
1551 449554 : obj->set_data(i::Smi::kZero);
1552 449554 : return Utils::ToLocal(obj);
1553 : }
1554 :
1555 0 : Local<ObjectTemplate> ObjectTemplate::New(
1556 : i::Isolate* isolate, v8::Local<FunctionTemplate> constructor) {
1557 386645 : return ObjectTemplateNew(isolate, constructor, false);
1558 : }
1559 :
1560 12 : MaybeLocal<ObjectTemplate> ObjectTemplate::FromSnapshot(Isolate* isolate,
1561 : size_t index) {
1562 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
1563 12 : i::FixedArray* templates = i_isolate->heap()->serialized_templates();
1564 12 : int int_index = static_cast<int>(index);
1565 12 : if (int_index < templates->length()) {
1566 : i::Object* info = templates->get(int_index);
1567 6 : if (info->IsObjectTemplateInfo()) {
1568 : return Utils::ToLocal(
1569 6 : i::Handle<i::ObjectTemplateInfo>(i::ObjectTemplateInfo::cast(info)));
1570 : }
1571 : }
1572 6 : return Local<ObjectTemplate>();
1573 : }
1574 :
1575 : // Ensure that the object template has a constructor. If no
1576 : // constructor is available we create one.
1577 194399 : static i::Handle<i::FunctionTemplateInfo> EnsureConstructor(
1578 : i::Isolate* isolate,
1579 : ObjectTemplate* object_template) {
1580 : i::Object* obj = Utils::OpenHandle(object_template)->constructor();
1581 194399 : if (!obj->IsUndefined(isolate)) {
1582 : i::FunctionTemplateInfo* info = i::FunctionTemplateInfo::cast(obj);
1583 64143 : return i::Handle<i::FunctionTemplateInfo>(info, isolate);
1584 : }
1585 : Local<FunctionTemplate> templ =
1586 130256 : FunctionTemplate::New(reinterpret_cast<Isolate*>(isolate));
1587 : i::Handle<i::FunctionTemplateInfo> constructor = Utils::OpenHandle(*templ);
1588 130256 : constructor->set_instance_template(*Utils::OpenHandle(object_template));
1589 130256 : Utils::OpenHandle(object_template)->set_constructor(*constructor);
1590 130256 : return constructor;
1591 : }
1592 :
1593 : template <typename Getter, typename Setter, typename Data, typename Template>
1594 63481 : static bool TemplateSetAccessor(Template* template_obj, v8::Local<Name> name,
1595 : Getter getter, Setter setter, Data data,
1596 : AccessControl settings,
1597 : PropertyAttribute attribute,
1598 : v8::Local<AccessorSignature> signature,
1599 : bool is_special_data_property,
1600 : bool replace_on_access) {
1601 : auto info = Utils::OpenHandle(template_obj);
1602 : auto isolate = info->GetIsolate();
1603 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1604 : i::HandleScope scope(isolate);
1605 : auto obj =
1606 : MakeAccessorInfo(name, getter, setter, data, settings, attribute,
1607 63481 : signature, is_special_data_property, replace_on_access);
1608 63481 : if (obj.is_null()) return false;
1609 63481 : i::ApiNatives::AddNativeDataProperty(isolate, info, obj);
1610 63481 : return true;
1611 : }
1612 :
1613 :
1614 215 : void Template::SetNativeDataProperty(v8::Local<String> name,
1615 : AccessorGetterCallback getter,
1616 : AccessorSetterCallback setter,
1617 : v8::Local<Value> data,
1618 : PropertyAttribute attribute,
1619 : v8::Local<AccessorSignature> signature,
1620 : AccessControl settings) {
1621 : TemplateSetAccessor(this, name, getter, setter, data, settings, attribute,
1622 215 : signature, true, false);
1623 215 : }
1624 :
1625 :
1626 0 : void Template::SetNativeDataProperty(v8::Local<Name> name,
1627 : AccessorNameGetterCallback getter,
1628 : AccessorNameSetterCallback setter,
1629 : v8::Local<Value> data,
1630 : PropertyAttribute attribute,
1631 : v8::Local<AccessorSignature> signature,
1632 : AccessControl settings) {
1633 : TemplateSetAccessor(this, name, getter, setter, data, settings, attribute,
1634 0 : signature, true, false);
1635 0 : }
1636 :
1637 0 : void Template::SetLazyDataProperty(v8::Local<Name> name,
1638 : AccessorNameGetterCallback getter,
1639 : v8::Local<Value> data,
1640 : PropertyAttribute attribute) {
1641 : TemplateSetAccessor(
1642 : this, name, getter, static_cast<AccessorNameSetterCallback>(nullptr),
1643 0 : data, DEFAULT, attribute, Local<AccessorSignature>(), true, true);
1644 0 : }
1645 :
1646 48 : void Template::SetIntrinsicDataProperty(Local<Name> name, Intrinsic intrinsic,
1647 : PropertyAttribute attribute) {
1648 : auto templ = Utils::OpenHandle(this);
1649 : i::Isolate* isolate = templ->GetIsolate();
1650 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1651 : i::HandleScope scope(isolate);
1652 : i::ApiNatives::AddDataProperty(isolate, templ, Utils::OpenHandle(*name),
1653 : intrinsic,
1654 48 : static_cast<i::PropertyAttributes>(attribute));
1655 48 : }
1656 :
1657 :
1658 63175 : void ObjectTemplate::SetAccessor(v8::Local<String> name,
1659 : AccessorGetterCallback getter,
1660 : AccessorSetterCallback setter,
1661 : v8::Local<Value> data, AccessControl settings,
1662 : PropertyAttribute attribute,
1663 : v8::Local<AccessorSignature> signature) {
1664 : TemplateSetAccessor(this, name, getter, setter, data, settings, attribute,
1665 126350 : signature, i::FLAG_disable_old_api_accessors, false);
1666 63175 : }
1667 :
1668 :
1669 91 : void ObjectTemplate::SetAccessor(v8::Local<Name> name,
1670 : AccessorNameGetterCallback getter,
1671 : AccessorNameSetterCallback setter,
1672 : v8::Local<Value> data, AccessControl settings,
1673 : PropertyAttribute attribute,
1674 : v8::Local<AccessorSignature> signature) {
1675 : TemplateSetAccessor(this, name, getter, setter, data, settings, attribute,
1676 91 : signature, i::FLAG_disable_old_api_accessors, false);
1677 91 : }
1678 :
1679 : template <typename Getter, typename Setter, typename Query, typename Descriptor,
1680 : typename Deleter, typename Enumerator, typename Definer>
1681 1389 : static i::Handle<i::InterceptorInfo> CreateInterceptorInfo(
1682 : i::Isolate* isolate, Getter getter, Setter setter, Query query,
1683 : Descriptor descriptor, Deleter remover, Enumerator enumerator,
1684 : Definer definer, Local<Value> data, PropertyHandlerFlags flags) {
1685 : DCHECK(query == nullptr ||
1686 : descriptor == nullptr); // Either intercept attributes or descriptor.
1687 : DCHECK(query == nullptr ||
1688 : definer ==
1689 : nullptr); // Only use descriptor callback with definer callback.
1690 : auto obj = i::Handle<i::InterceptorInfo>::cast(
1691 1389 : isolate->factory()->NewStruct(i::INTERCEPTOR_INFO_TYPE));
1692 : obj->set_flags(0);
1693 :
1694 3727 : if (getter != 0) SET_FIELD_WRAPPED(obj, set_getter, getter);
1695 2399 : if (setter != 0) SET_FIELD_WRAPPED(obj, set_setter, setter);
1696 1809 : if (query != 0) SET_FIELD_WRAPPED(obj, set_query, query);
1697 1441 : if (descriptor != 0) SET_FIELD_WRAPPED(obj, set_descriptor, descriptor);
1698 1567 : if (remover != 0) SET_FIELD_WRAPPED(obj, set_deleter, remover);
1699 1725 : if (enumerator != 0) SET_FIELD_WRAPPED(obj, set_enumerator, enumerator);
1700 1581 : if (definer != 0) SET_FIELD_WRAPPED(obj, set_definer, definer);
1701 : obj->set_can_intercept_symbols(
1702 : !(static_cast<int>(flags) &
1703 1389 : static_cast<int>(PropertyHandlerFlags::kOnlyInterceptStrings)));
1704 : obj->set_all_can_read(static_cast<int>(flags) &
1705 1389 : static_cast<int>(PropertyHandlerFlags::kAllCanRead));
1706 : obj->set_non_masking(static_cast<int>(flags) &
1707 1389 : static_cast<int>(PropertyHandlerFlags::kNonMasking));
1708 :
1709 1389 : if (data.IsEmpty()) {
1710 : data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1711 : }
1712 1389 : obj->set_data(*Utils::OpenHandle(*data));
1713 1389 : return obj;
1714 : }
1715 :
1716 : template <typename Getter, typename Setter, typename Query, typename Descriptor,
1717 : typename Deleter, typename Enumerator, typename Definer>
1718 1098 : static void ObjectTemplateSetNamedPropertyHandler(
1719 : ObjectTemplate* templ, Getter getter, Setter setter, Query query,
1720 : Descriptor descriptor, Deleter remover, Enumerator enumerator,
1721 : Definer definer, Local<Value> data, PropertyHandlerFlags flags) {
1722 : i::Isolate* isolate = Utils::OpenHandle(templ)->GetIsolate();
1723 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1724 : i::HandleScope scope(isolate);
1725 1098 : auto cons = EnsureConstructor(isolate, templ);
1726 1098 : EnsureNotInstantiated(cons, "ObjectTemplateSetNamedPropertyHandler");
1727 : auto obj = CreateInterceptorInfo(isolate, getter, setter, query, descriptor,
1728 1098 : remover, enumerator, definer, data, flags);
1729 1098 : cons->set_named_property_handler(*obj);
1730 1098 : }
1731 :
1732 14 : void ObjectTemplate::SetNamedPropertyHandler(
1733 : NamedPropertyGetterCallback getter, NamedPropertySetterCallback setter,
1734 : NamedPropertyQueryCallback query, NamedPropertyDeleterCallback remover,
1735 : NamedPropertyEnumeratorCallback enumerator, Local<Value> data) {
1736 : ObjectTemplateSetNamedPropertyHandler(
1737 : this, getter, setter, query, nullptr, remover, enumerator, nullptr, data,
1738 14 : PropertyHandlerFlags::kOnlyInterceptStrings);
1739 14 : }
1740 :
1741 1084 : void ObjectTemplate::SetHandler(
1742 : const NamedPropertyHandlerConfiguration& config) {
1743 : ObjectTemplateSetNamedPropertyHandler(
1744 : this, config.getter, config.setter, config.query, config.descriptor,
1745 : config.deleter, config.enumerator, config.definer, config.data,
1746 1084 : config.flags);
1747 1084 : }
1748 :
1749 :
1750 223 : void ObjectTemplate::MarkAsUndetectable() {
1751 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1752 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1753 : i::HandleScope scope(isolate);
1754 223 : auto cons = EnsureConstructor(isolate, this);
1755 223 : EnsureNotInstantiated(cons, "v8::ObjectTemplate::MarkAsUndetectable");
1756 : cons->set_undetectable(true);
1757 223 : }
1758 :
1759 :
1760 180 : void ObjectTemplate::SetAccessCheckCallback(AccessCheckCallback callback,
1761 : Local<Value> data) {
1762 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1763 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1764 : i::HandleScope scope(isolate);
1765 180 : auto cons = EnsureConstructor(isolate, this);
1766 180 : EnsureNotInstantiated(cons, "v8::ObjectTemplate::SetAccessCheckCallback");
1767 :
1768 : i::Handle<i::Struct> struct_info =
1769 180 : isolate->factory()->NewStruct(i::ACCESS_CHECK_INFO_TYPE);
1770 : i::Handle<i::AccessCheckInfo> info =
1771 : i::Handle<i::AccessCheckInfo>::cast(struct_info);
1772 :
1773 360 : SET_FIELD_WRAPPED(info, set_callback, callback);
1774 180 : info->set_named_interceptor(nullptr);
1775 180 : info->set_indexed_interceptor(nullptr);
1776 :
1777 180 : if (data.IsEmpty()) {
1778 : data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1779 : }
1780 180 : info->set_data(*Utils::OpenHandle(*data));
1781 :
1782 180 : cons->set_access_check_info(*info);
1783 : cons->set_needs_access_check(true);
1784 180 : }
1785 :
1786 41 : void ObjectTemplate::SetAccessCheckCallbackAndHandler(
1787 : AccessCheckCallback callback,
1788 : const NamedPropertyHandlerConfiguration& named_handler,
1789 : const IndexedPropertyHandlerConfiguration& indexed_handler,
1790 : Local<Value> data) {
1791 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1792 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1793 : i::HandleScope scope(isolate);
1794 41 : auto cons = EnsureConstructor(isolate, this);
1795 : EnsureNotInstantiated(
1796 41 : cons, "v8::ObjectTemplate::SetAccessCheckCallbackWithHandler");
1797 :
1798 : i::Handle<i::Struct> struct_info =
1799 41 : isolate->factory()->NewStruct(i::ACCESS_CHECK_INFO_TYPE);
1800 : i::Handle<i::AccessCheckInfo> info =
1801 : i::Handle<i::AccessCheckInfo>::cast(struct_info);
1802 :
1803 82 : SET_FIELD_WRAPPED(info, set_callback, callback);
1804 : auto named_interceptor = CreateInterceptorInfo(
1805 : isolate, named_handler.getter, named_handler.setter, named_handler.query,
1806 : named_handler.descriptor, named_handler.deleter, named_handler.enumerator,
1807 41 : named_handler.definer, named_handler.data, named_handler.flags);
1808 41 : info->set_named_interceptor(*named_interceptor);
1809 : auto indexed_interceptor = CreateInterceptorInfo(
1810 : isolate, indexed_handler.getter, indexed_handler.setter,
1811 : indexed_handler.query, indexed_handler.descriptor,
1812 : indexed_handler.deleter, indexed_handler.enumerator,
1813 41 : indexed_handler.definer, indexed_handler.data, indexed_handler.flags);
1814 41 : info->set_indexed_interceptor(*indexed_interceptor);
1815 :
1816 41 : if (data.IsEmpty()) {
1817 : data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1818 : }
1819 41 : info->set_data(*Utils::OpenHandle(*data));
1820 :
1821 41 : cons->set_access_check_info(*info);
1822 : cons->set_needs_access_check(true);
1823 41 : }
1824 :
1825 209 : void ObjectTemplate::SetHandler(
1826 : const IndexedPropertyHandlerConfiguration& config) {
1827 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1828 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1829 : i::HandleScope scope(isolate);
1830 209 : auto cons = EnsureConstructor(isolate, this);
1831 209 : EnsureNotInstantiated(cons, "v8::ObjectTemplate::SetHandler");
1832 : auto obj = CreateInterceptorInfo(isolate, config.getter, config.setter,
1833 : config.query, config.descriptor,
1834 : config.deleter, config.enumerator,
1835 209 : config.definer, config.data, config.flags);
1836 209 : cons->set_indexed_property_handler(*obj);
1837 209 : }
1838 :
1839 :
1840 287 : void ObjectTemplate::SetCallAsFunctionHandler(FunctionCallback callback,
1841 : Local<Value> data) {
1842 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1843 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1844 : i::HandleScope scope(isolate);
1845 287 : auto cons = EnsureConstructor(isolate, this);
1846 287 : EnsureNotInstantiated(cons, "v8::ObjectTemplate::SetCallAsFunctionHandler");
1847 : i::Handle<i::Struct> struct_obj =
1848 287 : isolate->factory()->NewStruct(i::TUPLE2_TYPE);
1849 : i::Handle<i::CallHandlerInfo> obj =
1850 : i::Handle<i::CallHandlerInfo>::cast(struct_obj);
1851 574 : SET_FIELD_WRAPPED(obj, set_callback, callback);
1852 287 : if (data.IsEmpty()) {
1853 : data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1854 : }
1855 287 : obj->set_data(*Utils::OpenHandle(*data));
1856 287 : cons->set_instance_call_handler(*obj);
1857 287 : }
1858 :
1859 64719 : int ObjectTemplate::InternalFieldCount() {
1860 64719 : return Utils::OpenHandle(this)->embedder_field_count();
1861 : }
1862 :
1863 127497 : void ObjectTemplate::SetInternalFieldCount(int value) {
1864 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1865 : if (!Utils::ApiCheck(i::Smi::IsValid(value),
1866 : "v8::ObjectTemplate::SetInternalFieldCount()",
1867 : "Invalid embedder field count")) {
1868 127497 : return;
1869 : }
1870 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1871 127497 : if (value > 0) {
1872 : // The embedder field count is set by the constructor function's
1873 : // construct code, so we ensure that there is a constructor
1874 : // function to do the setting.
1875 62729 : EnsureConstructor(isolate, this);
1876 : }
1877 127497 : Utils::OpenHandle(this)->set_embedder_field_count(value);
1878 : }
1879 :
1880 0 : bool ObjectTemplate::IsImmutableProto() {
1881 0 : return Utils::OpenHandle(this)->immutable_proto();
1882 : }
1883 :
1884 21 : void ObjectTemplate::SetImmutableProto() {
1885 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1886 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1887 21 : Utils::OpenHandle(this)->set_immutable_proto(true);
1888 21 : }
1889 :
1890 : // --- S c r i p t s ---
1891 :
1892 :
1893 : // Internally, UnboundScript is a SharedFunctionInfo, and Script is a
1894 : // JSFunction.
1895 :
1896 419 : ScriptCompiler::CachedData::CachedData(const uint8_t* data_, int length_,
1897 : BufferPolicy buffer_policy_)
1898 : : data(data_),
1899 : length(length_),
1900 : rejected(false),
1901 850 : buffer_policy(buffer_policy_) {}
1902 :
1903 :
1904 844 : ScriptCompiler::CachedData::~CachedData() {
1905 850 : if (buffer_policy == BufferOwned) {
1906 766 : delete[] data;
1907 : }
1908 844 : }
1909 :
1910 :
1911 0 : bool ScriptCompiler::ExternalSourceStream::SetBookmark() { return false; }
1912 :
1913 :
1914 0 : void ScriptCompiler::ExternalSourceStream::ResetToBookmark() { UNREACHABLE(); }
1915 :
1916 :
1917 132 : ScriptCompiler::StreamedSource::StreamedSource(ExternalSourceStream* stream,
1918 : Encoding encoding)
1919 264 : : impl_(new i::StreamedSource(stream, encoding)) {}
1920 :
1921 :
1922 132 : ScriptCompiler::StreamedSource::~StreamedSource() { delete impl_; }
1923 :
1924 :
1925 : const ScriptCompiler::CachedData*
1926 6 : ScriptCompiler::StreamedSource::GetCachedData() const {
1927 12 : return impl_->cached_data.get();
1928 : }
1929 :
1930 :
1931 322318 : Local<Script> UnboundScript::BindToCurrentContext() {
1932 : i::Handle<i::HeapObject> obj =
1933 : i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this));
1934 : i::Isolate* isolate = obj->GetIsolate();
1935 : i::Handle<i::SharedFunctionInfo> function_info(
1936 : i::SharedFunctionInfo::cast(*obj), isolate);
1937 : i::Handle<i::JSFunction> function =
1938 : isolate->factory()->NewFunctionFromSharedFunctionInfo(
1939 322318 : function_info, isolate->native_context());
1940 322317 : return ToApiHandle<Script>(function);
1941 : }
1942 :
1943 :
1944 214 : int UnboundScript::GetId() {
1945 : i::Handle<i::HeapObject> obj =
1946 : i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this));
1947 214 : i::Isolate* isolate = obj->GetIsolate();
1948 428 : LOG_API(isolate, UnboundScript, GetId);
1949 : i::HandleScope scope(isolate);
1950 : i::Handle<i::SharedFunctionInfo> function_info(
1951 : i::SharedFunctionInfo::cast(*obj));
1952 : i::Handle<i::Script> script(i::Script::cast(function_info->script()));
1953 214 : return script->id();
1954 : }
1955 :
1956 :
1957 6 : int UnboundScript::GetLineNumber(int code_pos) {
1958 : i::Handle<i::SharedFunctionInfo> obj =
1959 : i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
1960 6 : i::Isolate* isolate = obj->GetIsolate();
1961 12 : LOG_API(isolate, UnboundScript, GetLineNumber);
1962 6 : if (obj->script()->IsScript()) {
1963 : i::Handle<i::Script> script(i::Script::cast(obj->script()));
1964 6 : return i::Script::GetLineNumber(script, code_pos);
1965 : } else {
1966 : return -1;
1967 : }
1968 : }
1969 :
1970 :
1971 6 : Local<Value> UnboundScript::GetScriptName() {
1972 : i::Handle<i::SharedFunctionInfo> obj =
1973 : i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
1974 6 : i::Isolate* isolate = obj->GetIsolate();
1975 12 : LOG_API(isolate, UnboundScript, GetName);
1976 6 : if (obj->script()->IsScript()) {
1977 : i::Object* name = i::Script::cast(obj->script())->name();
1978 : return Utils::ToLocal(i::Handle<i::Object>(name, isolate));
1979 : } else {
1980 0 : return Local<String>();
1981 : }
1982 : }
1983 :
1984 :
1985 186 : Local<Value> UnboundScript::GetSourceURL() {
1986 : i::Handle<i::SharedFunctionInfo> obj =
1987 : i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
1988 186 : i::Isolate* isolate = obj->GetIsolate();
1989 372 : LOG_API(isolate, UnboundScript, GetSourceURL);
1990 186 : if (obj->script()->IsScript()) {
1991 : i::Object* url = i::Script::cast(obj->script())->source_url();
1992 : return Utils::ToLocal(i::Handle<i::Object>(url, isolate));
1993 : } else {
1994 0 : return Local<String>();
1995 : }
1996 : }
1997 :
1998 :
1999 186 : Local<Value> UnboundScript::GetSourceMappingURL() {
2000 : i::Handle<i::SharedFunctionInfo> obj =
2001 : i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
2002 186 : i::Isolate* isolate = obj->GetIsolate();
2003 372 : LOG_API(isolate, UnboundScript, GetSourceMappingURL);
2004 186 : if (obj->script()->IsScript()) {
2005 : i::Object* url = i::Script::cast(obj->script())->source_mapping_url();
2006 : return Utils::ToLocal(i::Handle<i::Object>(url, isolate));
2007 : } else {
2008 0 : return Local<String>();
2009 : }
2010 : }
2011 :
2012 :
2013 334956 : MaybeLocal<Value> Script::Run(Local<Context> context) {
2014 2679648 : PREPARE_FOR_EXECUTION_WITH_CONTEXT_IN_RUNTIME_CALL_STATS_SCOPE(
2015 : "v8", "V8.Execute", context, Script, Run, MaybeLocal<Value>(),
2016 : InternalEscapableScope, true);
2017 334956 : i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true);
2018 334956 : i::AggregatingHistogramTimerScope timer(isolate->counters()->compile_lazy());
2019 : i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
2020 : auto fun = i::Handle<i::JSFunction>::cast(Utils::OpenHandle(this));
2021 :
2022 334955 : i::Handle<i::Object> receiver = isolate->global_proxy();
2023 : Local<Value> result;
2024 : has_pending_exception = !ToLocal<Value>(
2025 669910 : i::Execution::Call(isolate, fun, receiver, 0, nullptr), &result);
2026 :
2027 350306 : RETURN_ON_FAILED_EXECUTION(Value);
2028 654561 : RETURN_ESCAPED(result);
2029 : }
2030 :
2031 :
2032 0 : Local<Value> Script::Run() {
2033 : auto self = Utils::OpenHandle(this, true);
2034 : // If execution is terminating, Compile(..)->Run() requires this
2035 : // check.
2036 0 : if (self.is_null()) return Local<Value>();
2037 0 : auto context = ContextFromHeapObject(self);
2038 0 : RETURN_TO_LOCAL_UNCHECKED(Run(context), Value);
2039 : }
2040 :
2041 :
2042 610 : Local<UnboundScript> Script::GetUnboundScript() {
2043 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
2044 : return ToApiHandle<UnboundScript>(
2045 610 : i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared()));
2046 : }
2047 :
2048 252 : bool DynamicImportResult::FinishDynamicImportSuccess(Local<Context> context,
2049 : Local<Module> module) {
2050 1008 : PREPARE_FOR_EXECUTION_BOOL(context, Module, FinishDynamicImportSuccess);
2051 : auto promise = Utils::OpenHandle(this);
2052 252 : i::Handle<i::Module> module_obj = Utils::OpenHandle(*module);
2053 : i::Handle<i::JSModuleNamespace> module_namespace =
2054 252 : i::Module::GetModuleNamespace(module_obj);
2055 : i::Handle<i::Object> argv[] = {promise, module_namespace};
2056 : has_pending_exception =
2057 : i::Execution::Call(isolate, isolate->promise_resolve(),
2058 : isolate->factory()->undefined_value(), arraysize(argv),
2059 : argv)
2060 756 : .is_null();
2061 252 : RETURN_ON_FAILED_EXECUTION_BOOL();
2062 : return true;
2063 : }
2064 :
2065 70 : bool DynamicImportResult::FinishDynamicImportFailure(Local<Context> context,
2066 : Local<Value> exception) {
2067 280 : PREPARE_FOR_EXECUTION_BOOL(context, Module, FinishDynamicImportFailure);
2068 : auto promise = Utils::OpenHandle(this);
2069 : // We pass true to trigger the debugger's on exception handler.
2070 : i::Handle<i::Object> argv[] = {promise, Utils::OpenHandle(*exception),
2071 70 : isolate->factory()->ToBoolean(true)};
2072 : has_pending_exception =
2073 : i::Execution::Call(isolate, isolate->promise_internal_reject(),
2074 : isolate->factory()->undefined_value(), arraysize(argv),
2075 : argv)
2076 210 : .is_null();
2077 70 : RETURN_ON_FAILED_EXECUTION_BOOL();
2078 : return true;
2079 : }
2080 :
2081 1574 : int Module::GetModuleRequestsLength() const {
2082 : i::Handle<i::Module> self = Utils::OpenHandle(this);
2083 3148 : return self->info()->module_requests()->length();
2084 : }
2085 :
2086 1086 : Local<String> Module::GetModuleRequest(int i) const {
2087 1086 : CHECK_GE(i, 0);
2088 : i::Handle<i::Module> self = Utils::OpenHandle(this);
2089 : i::Isolate* isolate = self->GetIsolate();
2090 : i::Handle<i::FixedArray> module_requests(self->info()->module_requests(),
2091 1086 : isolate);
2092 1086 : CHECK_LT(i, module_requests->length());
2093 1086 : return ToApiHandle<String>(i::handle(module_requests->get(i), isolate));
2094 : }
2095 :
2096 5278 : int Module::GetIdentityHash() const { return Utils::OpenHandle(this)->hash(); }
2097 :
2098 1216 : bool Module::Instantiate(Local<Context> context,
2099 : Module::ResolveCallback callback) {
2100 4864 : PREPARE_FOR_EXECUTION_BOOL(context, Module, Instantiate);
2101 : has_pending_exception =
2102 1216 : !i::Module::Instantiate(Utils::OpenHandle(this), context, callback);
2103 1244 : RETURN_ON_FAILED_EXECUTION_BOOL();
2104 : return true;
2105 : }
2106 :
2107 1188 : MaybeLocal<Value> Module::Evaluate(Local<Context> context) {
2108 9504 : PREPARE_FOR_EXECUTION_WITH_CONTEXT_IN_RUNTIME_CALL_STATS_SCOPE(
2109 : "v8", "V8.Execute", context, Module, Evaluate, MaybeLocal<Value>(),
2110 : InternalEscapableScope, true);
2111 1188 : i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true);
2112 1188 : i::AggregatingHistogramTimerScope timer(isolate->counters()->compile_lazy());
2113 : i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
2114 :
2115 : i::Handle<i::Module> self = Utils::OpenHandle(this);
2116 : // It's an API error to call Evaluate before Instantiate.
2117 1188 : CHECK(self->instantiated());
2118 :
2119 : Local<Value> result;
2120 2376 : has_pending_exception = !ToLocal(i::Module::Evaluate(self), &result);
2121 1216 : RETURN_ON_FAILED_EXECUTION(Value);
2122 2348 : RETURN_ESCAPED(result);
2123 : }
2124 :
2125 317452 : MaybeLocal<UnboundScript> ScriptCompiler::CompileUnboundInternal(
2126 : Isolate* v8_isolate, Source* source, CompileOptions options) {
2127 952335 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
2128 1269787 : PREPARE_FOR_EXECUTION_WITH_ISOLATE(isolate, ScriptCompiler, CompileUnbound,
2129 : UnboundScript);
2130 952335 : TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.ScriptCompiler");
2131 :
2132 : // Don't try to produce any kind of cache when the debugger is loaded.
2133 325735 : if (isolate->debug()->is_loaded() &&
2134 8290 : (options == kProduceParserCache || options == kProduceCodeCache)) {
2135 : options = kNoCompileOptions;
2136 : }
2137 :
2138 317445 : i::ScriptData* script_data = NULL;
2139 317445 : if (options == kConsumeParserCache || options == kConsumeCodeCache) {
2140 : DCHECK(source->cached_data);
2141 : // ScriptData takes care of pointer-aligning the data.
2142 : script_data = new i::ScriptData(source->cached_data->data,
2143 443 : source->cached_data->length);
2144 : }
2145 :
2146 317445 : i::Handle<i::String> str = Utils::OpenHandle(*(source->source_string));
2147 : i::Handle<i::SharedFunctionInfo> result;
2148 : {
2149 317445 : i::HistogramTimerScope total(isolate->counters()->compile_script(), true);
2150 950825 : TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.CompileScript");
2151 : i::Handle<i::Object> name_obj;
2152 : i::Handle<i::Object> source_map_url;
2153 : int line_offset = 0;
2154 : int column_offset = 0;
2155 317445 : if (!source->resource_name.IsEmpty()) {
2156 202745 : name_obj = Utils::OpenHandle(*(source->resource_name));
2157 : }
2158 317445 : if (!source->resource_line_offset.IsEmpty()) {
2159 46689 : line_offset = static_cast<int>(source->resource_line_offset->Value());
2160 : }
2161 317445 : if (!source->resource_column_offset.IsEmpty()) {
2162 : column_offset =
2163 46634 : static_cast<int>(source->resource_column_offset->Value());
2164 : }
2165 317445 : if (!source->source_map_url.IsEmpty()) {
2166 5268 : source_map_url = Utils::OpenHandle(*(source->source_map_url));
2167 : }
2168 : result = i::Compiler::GetSharedFunctionInfoForScript(
2169 : str, name_obj, line_offset, column_offset, source->resource_options,
2170 : source_map_url, isolate->native_context(), NULL, &script_data, options,
2171 317445 : i::NOT_NATIVES_CODE);
2172 : has_pending_exception = result.is_null();
2173 317875 : if (has_pending_exception && script_data != NULL) {
2174 : // This case won't happen during normal operation; we have compiled
2175 : // successfully and produced cached data, and but the second compilation
2176 : // of the same source code fails.
2177 0 : delete script_data;
2178 0 : script_data = NULL;
2179 : }
2180 318953 : RETURN_ON_FAILED_EXECUTION(UnboundScript);
2181 :
2182 316381 : if ((options == kProduceParserCache || options == kProduceCodeCache) &&
2183 446 : script_data != NULL) {
2184 : // script_data now contains the data that was generated. source will
2185 : // take the ownership.
2186 : source->cached_data = new CachedData(
2187 862 : script_data->data(), script_data->length(), CachedData::BufferOwned);
2188 431 : script_data->ReleaseDataOwnership();
2189 315504 : } else if (options == kConsumeParserCache || options == kConsumeCodeCache) {
2190 886 : source->cached_data->rejected = script_data->rejected();
2191 : }
2192 316809 : delete script_data;
2193 : }
2194 315936 : RETURN_ESCAPED(ToApiHandle<UnboundScript>(result));
2195 : }
2196 :
2197 :
2198 15027 : MaybeLocal<UnboundScript> ScriptCompiler::CompileUnboundScript(
2199 : Isolate* v8_isolate, Source* source, CompileOptions options) {
2200 : Utils::ApiCheck(
2201 : !source->GetResourceOptions().IsModule(),
2202 : "v8::ScriptCompiler::CompileUnboundScript",
2203 : "v8::ScriptCompiler::CompileModule must be used to compile modules");
2204 15027 : return CompileUnboundInternal(v8_isolate, source, options);
2205 : }
2206 :
2207 :
2208 0 : Local<UnboundScript> ScriptCompiler::CompileUnbound(Isolate* v8_isolate,
2209 : Source* source,
2210 : CompileOptions options) {
2211 : Utils::ApiCheck(
2212 : !source->GetResourceOptions().IsModule(),
2213 : "v8::ScriptCompiler::CompileUnbound",
2214 : "v8::ScriptCompiler::CompileModule must be used to compile modules");
2215 0 : RETURN_TO_LOCAL_UNCHECKED(CompileUnboundInternal(v8_isolate, source, options),
2216 : UnboundScript);
2217 : }
2218 :
2219 :
2220 300392 : MaybeLocal<Script> ScriptCompiler::Compile(Local<Context> context,
2221 : Source* source,
2222 : CompileOptions options) {
2223 : Utils::ApiCheck(
2224 : !source->GetResourceOptions().IsModule(), "v8::ScriptCompiler::Compile",
2225 : "v8::ScriptCompiler::CompileModule must be used to compile modules");
2226 : auto isolate = context->GetIsolate();
2227 300392 : auto maybe = CompileUnboundInternal(isolate, source, options);
2228 : Local<UnboundScript> result;
2229 300391 : if (!maybe.ToLocal(&result)) return MaybeLocal<Script>();
2230 : v8::Context::Scope scope(context);
2231 298923 : return result->BindToCurrentContext();
2232 : }
2233 :
2234 :
2235 0 : Local<Script> ScriptCompiler::Compile(
2236 : Isolate* v8_isolate,
2237 : Source* source,
2238 : CompileOptions options) {
2239 0 : auto context = v8_isolate->GetCurrentContext();
2240 0 : RETURN_TO_LOCAL_UNCHECKED(Compile(context, source, options), Script);
2241 : }
2242 :
2243 2033 : MaybeLocal<Module> ScriptCompiler::CompileModule(Isolate* isolate,
2244 : Source* source) {
2245 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2246 :
2247 : Utils::ApiCheck(source->GetResourceOptions().IsModule(),
2248 : "v8::ScriptCompiler::CompileModule",
2249 : "Invalid ScriptOrigin: is_module must be true");
2250 2033 : auto maybe = CompileUnboundInternal(isolate, source, kNoCompileOptions);
2251 : Local<UnboundScript> unbound;
2252 2033 : if (!maybe.ToLocal(&unbound)) return MaybeLocal<Module>();
2253 :
2254 2001 : i::Handle<i::SharedFunctionInfo> shared = Utils::OpenHandle(*unbound);
2255 2001 : return ToApiHandle<Module>(i_isolate->factory()->NewModule(shared));
2256 : }
2257 :
2258 :
2259 : class IsIdentifierHelper {
2260 : public:
2261 28 : IsIdentifierHelper() : is_identifier_(false), first_char_(true) {}
2262 :
2263 : bool Check(i::String* string) {
2264 28 : i::ConsString* cons_string = i::String::VisitFlat(this, string, 0);
2265 28 : if (cons_string == NULL) return is_identifier_;
2266 : // We don't support cons strings here.
2267 : return false;
2268 : }
2269 28 : void VisitOneByteString(const uint8_t* chars, int length) {
2270 98 : for (int i = 0; i < length; ++i) {
2271 70 : if (first_char_) {
2272 28 : first_char_ = false;
2273 56 : is_identifier_ = unicode_cache_.IsIdentifierStart(chars[0]);
2274 : } else {
2275 84 : is_identifier_ &= unicode_cache_.IsIdentifierPart(chars[i]);
2276 : }
2277 : }
2278 28 : }
2279 0 : void VisitTwoByteString(const uint16_t* chars, int length) {
2280 0 : for (int i = 0; i < length; ++i) {
2281 0 : if (first_char_) {
2282 0 : first_char_ = false;
2283 0 : is_identifier_ = unicode_cache_.IsIdentifierStart(chars[0]);
2284 : } else {
2285 0 : is_identifier_ &= unicode_cache_.IsIdentifierPart(chars[i]);
2286 : }
2287 : }
2288 0 : }
2289 :
2290 : private:
2291 : bool is_identifier_;
2292 : bool first_char_;
2293 : i::UnicodeCache unicode_cache_;
2294 : DISALLOW_COPY_AND_ASSIGN(IsIdentifierHelper);
2295 : };
2296 :
2297 :
2298 56 : MaybeLocal<Function> ScriptCompiler::CompileFunctionInContext(
2299 : Local<Context> v8_context, Source* source, size_t arguments_count,
2300 : Local<String> arguments[], size_t context_extension_count,
2301 : Local<Object> context_extensions[]) {
2302 224 : PREPARE_FOR_EXECUTION(v8_context, ScriptCompiler, CompileFunctionInContext,
2303 : Function);
2304 168 : TRACE_EVENT0("v8", "V8.ScriptCompiler");
2305 : i::Handle<i::String> source_string;
2306 : int parameters_end_pos = i::kNoSourcePosition;
2307 : auto factory = isolate->factory();
2308 56 : if (arguments_count) {
2309 28 : if (i::FLAG_harmony_function_tostring) {
2310 7 : source_string = factory->NewStringFromStaticChars("(function anonymous(");
2311 : } else {
2312 21 : source_string = factory->NewStringFromStaticChars("(function(");
2313 : }
2314 49 : for (size_t i = 0; i < arguments_count; ++i) {
2315 : IsIdentifierHelper helper;
2316 56 : if (!helper.Check(*Utils::OpenHandle(*arguments[i]))) {
2317 7 : return Local<Function>();
2318 : }
2319 : has_pending_exception =
2320 : !factory->NewConsString(source_string,
2321 : Utils::OpenHandle(*arguments[i]))
2322 42 : .ToHandle(&source_string);
2323 21 : RETURN_ON_FAILED_EXECUTION(Function);
2324 42 : if (i + 1 == arguments_count) continue;
2325 : has_pending_exception =
2326 : !factory->NewConsString(source_string,
2327 : factory->LookupSingleCharacterStringFromCode(
2328 0 : ',')).ToHandle(&source_string);
2329 0 : RETURN_ON_FAILED_EXECUTION(Function);
2330 : }
2331 : i::Handle<i::String> brackets;
2332 21 : if (i::FLAG_harmony_function_tostring) {
2333 : // Append linefeed and signal that text beyond the linefeed is not part of
2334 : // the formal parameters.
2335 7 : brackets = factory->NewStringFromStaticChars("\n) {\n");
2336 7 : parameters_end_pos = source_string->length() + 1;
2337 : } else {
2338 14 : brackets = factory->NewStringFromStaticChars("){");
2339 : }
2340 : has_pending_exception = !factory->NewConsString(source_string, brackets)
2341 42 : .ToHandle(&source_string);
2342 21 : RETURN_ON_FAILED_EXECUTION(Function);
2343 : } else {
2344 28 : if (i::FLAG_harmony_function_tostring) {
2345 : source_string =
2346 7 : factory->NewStringFromStaticChars("(function anonymous(\n) {\n");
2347 7 : parameters_end_pos = source_string->length() - 4;
2348 : } else {
2349 21 : source_string = factory->NewStringFromStaticChars("(function(){");
2350 : }
2351 : }
2352 :
2353 : int scope_position = source_string->length();
2354 : has_pending_exception =
2355 : !factory->NewConsString(source_string,
2356 : Utils::OpenHandle(*source->source_string))
2357 98 : .ToHandle(&source_string);
2358 49 : RETURN_ON_FAILED_EXECUTION(Function);
2359 : // Include \n in case the source contains a line end comment.
2360 49 : auto brackets = factory->NewStringFromStaticChars("\n})");
2361 : has_pending_exception =
2362 98 : !factory->NewConsString(source_string, brackets).ToHandle(&source_string);
2363 49 : RETURN_ON_FAILED_EXECUTION(Function);
2364 :
2365 : i::Handle<i::Context> context = Utils::OpenHandle(*v8_context);
2366 : i::Handle<i::SharedFunctionInfo> outer_info(context->closure()->shared(),
2367 : isolate);
2368 84 : for (size_t i = 0; i < context_extension_count; ++i) {
2369 : i::Handle<i::JSReceiver> extension =
2370 35 : Utils::OpenHandle(*context_extensions[i]);
2371 35 : if (!extension->IsJSObject()) return Local<Function>();
2372 : i::Handle<i::JSFunction> closure(context->closure(), isolate);
2373 : context = factory->NewWithContext(
2374 : closure, context,
2375 : i::ScopeInfo::CreateForWithScope(
2376 35 : isolate, context->IsNativeContext()
2377 : ? i::Handle<i::ScopeInfo>::null()
2378 : : i::Handle<i::ScopeInfo>(context->scope_info())),
2379 77 : extension);
2380 : }
2381 :
2382 : i::Handle<i::Object> name_obj;
2383 : int eval_scope_position = 0;
2384 : int eval_position = i::kNoSourcePosition;
2385 : int line_offset = 0;
2386 : int column_offset = 0;
2387 49 : if (!source->resource_name.IsEmpty()) {
2388 21 : name_obj = Utils::OpenHandle(*(source->resource_name));
2389 : }
2390 49 : if (!source->resource_line_offset.IsEmpty()) {
2391 21 : line_offset = static_cast<int>(source->resource_line_offset->Value());
2392 : }
2393 49 : if (!source->resource_column_offset.IsEmpty()) {
2394 21 : column_offset = static_cast<int>(source->resource_column_offset->Value());
2395 : }
2396 : i::Handle<i::JSFunction> fun;
2397 : has_pending_exception =
2398 : !i::Compiler::GetFunctionFromEval(
2399 : source_string, outer_info, context, i::SLOPPY,
2400 : i::ONLY_SINGLE_FUNCTION_LITERAL, parameters_end_pos,
2401 : eval_scope_position, eval_position, line_offset,
2402 : column_offset - scope_position, name_obj, source->resource_options)
2403 98 : .ToHandle(&fun);
2404 49 : if (has_pending_exception) {
2405 0 : isolate->ReportPendingMessages();
2406 : }
2407 49 : RETURN_ON_FAILED_EXECUTION(Function);
2408 :
2409 : i::Handle<i::Object> result;
2410 : has_pending_exception =
2411 : !i::Execution::Call(isolate, fun,
2412 98 : Utils::OpenHandle(*v8_context->Global()), 0,
2413 98 : nullptr).ToHandle(&result);
2414 49 : RETURN_ON_FAILED_EXECUTION(Function);
2415 49 : RETURN_ESCAPED(
2416 : Utils::CallableToLocal(i::Handle<i::JSFunction>::cast(result)));
2417 : }
2418 :
2419 :
2420 0 : Local<Function> ScriptCompiler::CompileFunctionInContext(
2421 : Isolate* v8_isolate, Source* source, Local<Context> v8_context,
2422 : size_t arguments_count, Local<String> arguments[],
2423 : size_t context_extension_count, Local<Object> context_extensions[]) {
2424 0 : RETURN_TO_LOCAL_UNCHECKED(
2425 : CompileFunctionInContext(v8_context, source, arguments_count, arguments,
2426 : context_extension_count, context_extensions),
2427 : Function);
2428 : }
2429 :
2430 :
2431 132 : ScriptCompiler::ScriptStreamingTask* ScriptCompiler::StartStreamingScript(
2432 : Isolate* v8_isolate, StreamedSource* source, CompileOptions options) {
2433 132 : if (!i::FLAG_script_streaming) {
2434 : return nullptr;
2435 : }
2436 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
2437 : return new i::BackgroundParsingTask(source->impl(), options,
2438 132 : i::FLAG_stack_size, isolate);
2439 : }
2440 :
2441 :
2442 126 : MaybeLocal<Script> ScriptCompiler::Compile(Local<Context> context,
2443 : StreamedSource* v8_source,
2444 : Local<String> full_source_string,
2445 : const ScriptOrigin& origin) {
2446 504 : PREPARE_FOR_EXECUTION(context, ScriptCompiler, Compile, Script);
2447 378 : TRACE_EVENT0("v8", "V8.ScriptCompiler");
2448 : i::StreamedSource* source = v8_source->impl();
2449 : i::Handle<i::String> str = Utils::OpenHandle(*(full_source_string));
2450 126 : i::Handle<i::Script> script = isolate->factory()->NewScript(str);
2451 126 : if (!origin.ResourceName().IsEmpty()) {
2452 126 : script->set_name(*Utils::OpenHandle(*(origin.ResourceName())));
2453 : }
2454 126 : if (!origin.ResourceLineOffset().IsEmpty()) {
2455 : script->set_line_offset(
2456 0 : static_cast<int>(origin.ResourceLineOffset()->Value()));
2457 : }
2458 126 : if (!origin.ResourceColumnOffset().IsEmpty()) {
2459 : script->set_column_offset(
2460 0 : static_cast<int>(origin.ResourceColumnOffset()->Value()));
2461 : }
2462 : script->set_origin_options(origin.Options());
2463 126 : if (!origin.SourceMapUrl().IsEmpty()) {
2464 : script->set_source_mapping_url(
2465 0 : *Utils::OpenHandle(*(origin.SourceMapUrl())));
2466 : }
2467 :
2468 : source->info->set_script(script);
2469 126 : if (source->info->literal() == nullptr) {
2470 12 : source->parser->ReportErrors(isolate, script);
2471 : }
2472 126 : source->parser->UpdateStatistics(isolate, script);
2473 :
2474 252 : i::DeferredHandleScope deferred_handle_scope(isolate);
2475 : {
2476 : // Internalize AST values on the main thread.
2477 126 : source->info->ReopenHandlesInNewHandleScope();
2478 126 : source->info->ast_value_factory()->Internalize(isolate);
2479 126 : source->parser->HandleSourceURLComments(isolate, script);
2480 : }
2481 252 : source->info->set_deferred_handles(deferred_handle_scope.Detach());
2482 :
2483 : i::Handle<i::SharedFunctionInfo> result;
2484 126 : if (source->info->literal() != nullptr) {
2485 : // Parsing has succeeded.
2486 : result = i::Compiler::GetSharedFunctionInfoForStreamedScript(
2487 114 : script, source->info.get(), str->length());
2488 : }
2489 : has_pending_exception = result.is_null();
2490 126 : if (has_pending_exception) isolate->ReportPendingMessages();
2491 :
2492 126 : source->Release();
2493 :
2494 138 : RETURN_ON_FAILED_EXECUTION(Script);
2495 :
2496 : Local<UnboundScript> generic = ToApiHandle<UnboundScript>(result);
2497 114 : if (generic.IsEmpty()) return Local<Script>();
2498 114 : Local<Script> bound = generic->BindToCurrentContext();
2499 114 : if (bound.IsEmpty()) return Local<Script>();
2500 114 : RETURN_ESCAPED(bound);
2501 : }
2502 :
2503 :
2504 0 : Local<Script> ScriptCompiler::Compile(Isolate* v8_isolate,
2505 : StreamedSource* v8_source,
2506 : Local<String> full_source_string,
2507 : const ScriptOrigin& origin) {
2508 0 : auto context = v8_isolate->GetCurrentContext();
2509 0 : RETURN_TO_LOCAL_UNCHECKED(
2510 : Compile(context, v8_source, full_source_string, origin), Script);
2511 : }
2512 :
2513 :
2514 0 : uint32_t ScriptCompiler::CachedDataVersionTag() {
2515 : return static_cast<uint32_t>(base::hash_combine(
2516 0 : internal::Version::Hash(), internal::FlagList::Hash(),
2517 0 : static_cast<uint32_t>(internal::CpuFeatures::SupportedFeatures())));
2518 : }
2519 :
2520 :
2521 107002 : MaybeLocal<Script> Script::Compile(Local<Context> context, Local<String> source,
2522 : ScriptOrigin* origin) {
2523 107002 : if (origin) {
2524 : ScriptCompiler::Source script_source(source, *origin);
2525 7202 : return ScriptCompiler::Compile(context, &script_source);
2526 : }
2527 : ScriptCompiler::Source script_source(source);
2528 99800 : return ScriptCompiler::Compile(context, &script_source);
2529 : }
2530 :
2531 :
2532 0 : Local<Script> Script::Compile(v8::Local<String> source,
2533 : v8::ScriptOrigin* origin) {
2534 : auto str = Utils::OpenHandle(*source);
2535 0 : auto context = ContextFromHeapObject(str);
2536 0 : RETURN_TO_LOCAL_UNCHECKED(Compile(context, source, origin), Script);
2537 : }
2538 :
2539 :
2540 0 : Local<Script> Script::Compile(v8::Local<String> source,
2541 : v8::Local<String> file_name) {
2542 : auto str = Utils::OpenHandle(*source);
2543 0 : auto context = ContextFromHeapObject(str);
2544 : ScriptOrigin origin(file_name);
2545 0 : return Compile(context, source, &origin).FromMaybe(Local<Script>());
2546 : }
2547 :
2548 :
2549 : // --- E x c e p t i o n s ---
2550 :
2551 :
2552 0 : v8::TryCatch::TryCatch()
2553 : : isolate_(i::Isolate::Current()),
2554 : next_(isolate_->try_catch_handler()),
2555 : is_verbose_(false),
2556 : can_continue_(true),
2557 : capture_message_(true),
2558 : rethrow_(false),
2559 0 : has_terminated_(false) {
2560 : ResetInternal();
2561 : // Special handling for simulators which have a separate JS stack.
2562 : js_stack_comparable_address_ =
2563 : reinterpret_cast<void*>(i::SimulatorStack::RegisterCTryCatch(
2564 0 : isolate_, i::GetCurrentStackPosition()));
2565 0 : isolate_->RegisterTryCatchHandler(this);
2566 0 : }
2567 :
2568 :
2569 23359260 : v8::TryCatch::TryCatch(v8::Isolate* isolate)
2570 : : isolate_(reinterpret_cast<i::Isolate*>(isolate)),
2571 : next_(isolate_->try_catch_handler()),
2572 : is_verbose_(false),
2573 : can_continue_(true),
2574 : capture_message_(true),
2575 : rethrow_(false),
2576 46718520 : has_terminated_(false) {
2577 : ResetInternal();
2578 : // Special handling for simulators which have a separate JS stack.
2579 : js_stack_comparable_address_ =
2580 : reinterpret_cast<void*>(i::SimulatorStack::RegisterCTryCatch(
2581 23359260 : isolate_, i::GetCurrentStackPosition()));
2582 23359260 : isolate_->RegisterTryCatchHandler(this);
2583 23359257 : }
2584 :
2585 :
2586 23359258 : v8::TryCatch::~TryCatch() {
2587 23359258 : if (rethrow_) {
2588 143 : v8::Isolate* isolate = reinterpret_cast<Isolate*>(isolate_);
2589 : v8::HandleScope scope(isolate);
2590 286 : v8::Local<v8::Value> exc = v8::Local<v8::Value>::New(isolate, Exception());
2591 143 : if (HasCaught() && capture_message_) {
2592 : // If an exception was caught and rethrow_ is indicated, the saved
2593 : // message, script, and location need to be restored to Isolate TLS
2594 : // for reuse. capture_message_ needs to be disabled so that Throw()
2595 : // does not create a new message.
2596 143 : isolate_->thread_local_top()->rethrowing_message_ = true;
2597 143 : isolate_->RestorePendingMessageFromTryCatch(this);
2598 : }
2599 143 : isolate_->UnregisterTryCatchHandler(this);
2600 : i::SimulatorStack::UnregisterCTryCatch(isolate_);
2601 143 : reinterpret_cast<Isolate*>(isolate_)->ThrowException(exc);
2602 : DCHECK(!isolate_->thread_local_top()->rethrowing_message_);
2603 : } else {
2604 23375339 : if (HasCaught() && isolate_->has_scheduled_exception()) {
2605 : // If an exception was caught but is still scheduled because no API call
2606 : // promoted it, then it is canceled to prevent it from being propagated.
2607 : // Note that this will not cancel termination exceptions.
2608 657 : isolate_->CancelScheduledExceptionFromTryCatch(this);
2609 : }
2610 23359117 : isolate_->UnregisterTryCatchHandler(this);
2611 : i::SimulatorStack::UnregisterCTryCatch(isolate_);
2612 : }
2613 23359262 : }
2614 :
2615 0 : V8_NORETURN void* v8::TryCatch::operator new(size_t) {
2616 0 : base::OS::Abort();
2617 : abort();
2618 : }
2619 :
2620 0 : void v8::TryCatch::operator delete(void*, size_t) { base::OS::Abort(); }
2621 :
2622 24016428 : bool v8::TryCatch::HasCaught() const {
2623 48032858 : return !reinterpret_cast<i::Object*>(exception_)->IsTheHole(isolate_);
2624 : }
2625 :
2626 :
2627 70 : bool v8::TryCatch::CanContinue() const {
2628 70 : return can_continue_;
2629 : }
2630 :
2631 :
2632 60 : bool v8::TryCatch::HasTerminated() const {
2633 60 : return has_terminated_;
2634 : }
2635 :
2636 :
2637 143 : v8::Local<v8::Value> v8::TryCatch::ReThrow() {
2638 143 : if (!HasCaught()) return v8::Local<v8::Value>();
2639 143 : rethrow_ = true;
2640 286 : return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate_));
2641 : }
2642 :
2643 :
2644 15549 : v8::Local<Value> v8::TryCatch::Exception() const {
2645 15549 : if (HasCaught()) {
2646 : // Check for out of memory exception.
2647 15549 : i::Object* exception = reinterpret_cast<i::Object*>(exception_);
2648 15549 : return v8::Utils::ToLocal(i::Handle<i::Object>(exception, isolate_));
2649 : } else {
2650 0 : return v8::Local<Value>();
2651 : }
2652 : }
2653 :
2654 :
2655 11517 : MaybeLocal<Value> v8::TryCatch::StackTrace(Local<Context> context) const {
2656 11517 : if (!HasCaught()) return v8::Local<Value>();
2657 11517 : i::Object* raw_obj = reinterpret_cast<i::Object*>(exception_);
2658 11517 : if (!raw_obj->IsJSObject()) return v8::Local<Value>();
2659 5624 : PREPARE_FOR_EXECUTION(context, TryCatch, StackTrace, Value);
2660 1406 : i::Handle<i::JSObject> obj(i::JSObject::cast(raw_obj), isolate_);
2661 : i::Handle<i::String> name = isolate->factory()->stack_string();
2662 1406 : Maybe<bool> maybe = i::JSReceiver::HasProperty(obj, name);
2663 1406 : has_pending_exception = !maybe.IsJust();
2664 1406 : RETURN_ON_FAILED_EXECUTION(Value);
2665 1406 : if (!maybe.FromJust()) return v8::Local<Value>();
2666 : Local<Value> result;
2667 : has_pending_exception =
2668 2812 : !ToLocal<Value>(i::JSReceiver::GetProperty(obj, name), &result);
2669 1406 : RETURN_ON_FAILED_EXECUTION(Value);
2670 1406 : RETURN_ESCAPED(result);
2671 : }
2672 :
2673 :
2674 0 : v8::Local<Value> v8::TryCatch::StackTrace() const {
2675 0 : auto context = reinterpret_cast<v8::Isolate*>(isolate_)->GetCurrentContext();
2676 0 : RETURN_TO_LOCAL_UNCHECKED(StackTrace(context), Value);
2677 : }
2678 :
2679 :
2680 13797 : v8::Local<v8::Message> v8::TryCatch::Message() const {
2681 13797 : i::Object* message = reinterpret_cast<i::Object*>(message_obj_);
2682 : DCHECK(message->IsJSMessageObject() || message->IsTheHole(isolate_));
2683 27594 : if (HasCaught() && !message->IsTheHole(isolate_)) {
2684 13727 : return v8::Utils::MessageToLocal(i::Handle<i::Object>(message, isolate_));
2685 : } else {
2686 70 : return v8::Local<v8::Message>();
2687 : }
2688 : }
2689 :
2690 :
2691 864 : void v8::TryCatch::Reset() {
2692 1441 : if (!rethrow_ && HasCaught() && isolate_->has_scheduled_exception()) {
2693 : // If an exception was caught but is still scheduled because no API call
2694 : // promoted it, then it is canceled to prevent it from being propagated.
2695 : // Note that this will not cancel termination exceptions.
2696 6 : isolate_->CancelScheduledExceptionFromTryCatch(this);
2697 : }
2698 : ResetInternal();
2699 864 : }
2700 :
2701 :
2702 0 : void v8::TryCatch::ResetInternal() {
2703 23360124 : i::Object* the_hole = isolate_->heap()->the_hole_value();
2704 23360124 : exception_ = the_hole;
2705 23360124 : message_obj_ = the_hole;
2706 0 : }
2707 :
2708 :
2709 1042343 : void v8::TryCatch::SetVerbose(bool value) {
2710 1042343 : is_verbose_ = value;
2711 1042343 : }
2712 :
2713 :
2714 465902 : void v8::TryCatch::SetCaptureMessage(bool value) {
2715 465902 : capture_message_ = value;
2716 465902 : }
2717 :
2718 :
2719 : // --- M e s s a g e ---
2720 :
2721 :
2722 2606 : Local<String> Message::Get() const {
2723 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2724 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2725 2606 : EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
2726 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
2727 2606 : i::Handle<i::String> raw_result = i::MessageHandler::GetMessage(isolate, obj);
2728 : Local<String> result = Utils::ToLocal(raw_result);
2729 2606 : return scope.Escape(result);
2730 : }
2731 :
2732 :
2733 25562 : ScriptOrigin Message::GetScriptOrigin() const {
2734 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2735 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2736 : auto message = i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
2737 : auto script_wraper = i::Handle<i::Object>(message->script(), isolate);
2738 : auto script_value = i::Handle<i::JSValue>::cast(script_wraper);
2739 : i::Handle<i::Script> script(i::Script::cast(script_value->value()));
2740 51124 : return GetScriptOriginForScript(isolate, script);
2741 : }
2742 :
2743 :
2744 0 : v8::Local<Value> Message::GetScriptResourceName() const {
2745 0 : return GetScriptOrigin().ResourceName();
2746 : }
2747 :
2748 :
2749 1827 : v8::Local<v8::StackTrace> Message::GetStackTrace() const {
2750 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2751 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2752 1827 : EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
2753 : auto message = i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
2754 : i::Handle<i::Object> stackFramesObj(message->stack_frames(), isolate);
2755 1827 : if (!stackFramesObj->IsFixedArray()) return v8::Local<v8::StackTrace>();
2756 : auto stackTrace = i::Handle<i::FixedArray>::cast(stackFramesObj);
2757 : return scope.Escape(Utils::StackTraceToLocal(stackTrace));
2758 : }
2759 :
2760 :
2761 13971 : Maybe<int> Message::GetLineNumber(Local<Context> context) const {
2762 : auto self = Utils::OpenHandle(this);
2763 : i::Isolate* isolate = self->GetIsolate();
2764 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2765 13971 : EscapableHandleScope handle_scope(reinterpret_cast<Isolate*>(isolate));
2766 : auto msg = i::Handle<i::JSMessageObject>::cast(self);
2767 27942 : return Just(msg->GetLineNumber());
2768 : }
2769 :
2770 :
2771 0 : int Message::GetLineNumber() const {
2772 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
2773 0 : return GetLineNumber(context).FromMaybe(0);
2774 : }
2775 :
2776 :
2777 42 : int Message::GetStartPosition() const {
2778 : auto self = Utils::OpenHandle(this);
2779 42 : return self->start_position();
2780 : }
2781 :
2782 :
2783 21 : int Message::GetEndPosition() const {
2784 : auto self = Utils::OpenHandle(this);
2785 21 : return self->end_position();
2786 : }
2787 :
2788 40909 : int Message::ErrorLevel() const {
2789 : auto self = Utils::OpenHandle(this);
2790 40909 : return self->error_level();
2791 : }
2792 :
2793 13418 : Maybe<int> Message::GetStartColumn(Local<Context> context) const {
2794 : auto self = Utils::OpenHandle(this);
2795 : i::Isolate* isolate = self->GetIsolate();
2796 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2797 13418 : EscapableHandleScope handle_scope(reinterpret_cast<Isolate*>(isolate));
2798 : auto msg = i::Handle<i::JSMessageObject>::cast(self);
2799 26836 : return Just(msg->GetColumnNumber());
2800 : }
2801 :
2802 :
2803 0 : int Message::GetStartColumn() const {
2804 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
2805 : const int default_value = kNoColumnInfo;
2806 0 : return GetStartColumn(context).FromMaybe(default_value);
2807 : }
2808 :
2809 :
2810 11525 : Maybe<int> Message::GetEndColumn(Local<Context> context) const {
2811 : auto self = Utils::OpenHandle(this);
2812 : i::Isolate* isolate = self->GetIsolate();
2813 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2814 11525 : EscapableHandleScope handle_scope(reinterpret_cast<Isolate*>(isolate));
2815 : auto msg = i::Handle<i::JSMessageObject>::cast(self);
2816 11525 : const int column_number = msg->GetColumnNumber();
2817 11525 : if (column_number == -1) return Just(-1);
2818 : const int start = self->start_position();
2819 : const int end = self->end_position();
2820 11525 : return Just(column_number + (end - start));
2821 : }
2822 :
2823 :
2824 0 : int Message::GetEndColumn() const {
2825 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
2826 : const int default_value = kNoColumnInfo;
2827 0 : return GetEndColumn(context).FromMaybe(default_value);
2828 : }
2829 :
2830 :
2831 43 : bool Message::IsSharedCrossOrigin() const {
2832 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2833 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2834 : auto self = Utils::OpenHandle(this);
2835 : auto script = i::Handle<i::JSValue>::cast(
2836 : i::Handle<i::Object>(self->script(), isolate));
2837 : return i::Script::cast(script->value())
2838 : ->origin_options()
2839 43 : .IsSharedCrossOrigin();
2840 : }
2841 :
2842 0 : bool Message::IsOpaque() const {
2843 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2844 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2845 : auto self = Utils::OpenHandle(this);
2846 : auto script = i::Handle<i::JSValue>::cast(
2847 : i::Handle<i::Object>(self->script(), isolate));
2848 0 : return i::Script::cast(script->value())->origin_options().IsOpaque();
2849 : }
2850 :
2851 :
2852 11538 : MaybeLocal<String> Message::GetSourceLine(Local<Context> context) const {
2853 : auto self = Utils::OpenHandle(this);
2854 : i::Isolate* isolate = self->GetIsolate();
2855 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2856 11538 : EscapableHandleScope handle_scope(reinterpret_cast<Isolate*>(isolate));
2857 : auto msg = i::Handle<i::JSMessageObject>::cast(self);
2858 34614 : RETURN_ESCAPED(Utils::ToLocal(msg->GetSourceLine()));
2859 : }
2860 :
2861 :
2862 0 : Local<String> Message::GetSourceLine() const {
2863 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
2864 0 : RETURN_TO_LOCAL_UNCHECKED(GetSourceLine(context), String)
2865 : }
2866 :
2867 :
2868 0 : void Message::PrintCurrentStackTrace(Isolate* isolate, FILE* out) {
2869 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2870 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
2871 0 : i_isolate->PrintCurrentStackTrace(out);
2872 0 : }
2873 :
2874 :
2875 : // --- S t a c k T r a c e ---
2876 :
2877 47103 : Local<StackFrame> StackTrace::GetFrame(uint32_t index) const {
2878 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2879 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2880 47103 : EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
2881 47103 : auto obj = handle(Utils::OpenHandle(this)->get(index), isolate);
2882 : auto info = i::Handle<i::StackFrameInfo>::cast(obj);
2883 47103 : return scope.Escape(Utils::StackFrameToLocal(info));
2884 : }
2885 :
2886 :
2887 96198 : int StackTrace::GetFrameCount() const {
2888 96198 : return Utils::OpenHandle(this)->length();
2889 : }
2890 :
2891 : namespace {
2892 0 : i::Handle<i::JSObject> NewFrameObject(i::Isolate* isolate,
2893 : i::Handle<i::StackFrameInfo> frame) {
2894 : i::Handle<i::JSObject> frame_obj =
2895 0 : isolate->factory()->NewJSObject(isolate->object_function());
2896 : i::JSObject::AddProperty(
2897 : frame_obj, handle(isolate->heap()->line_string()),
2898 0 : handle(i::Smi::FromInt(frame->line_number() + 1), isolate), i::NONE);
2899 : i::JSObject::AddProperty(
2900 : frame_obj, handle(isolate->heap()->column_string()),
2901 0 : handle(i::Smi::FromInt(frame->column_number() + 1), isolate), i::NONE);
2902 : i::JSObject::AddProperty(frame_obj,
2903 : isolate->factory()->InternalizeOneByteString(
2904 : STATIC_CHAR_VECTOR("scriptId")),
2905 : handle(i::Smi::FromInt(frame->script_id()), isolate),
2906 0 : i::NONE);
2907 : i::JSObject::AddProperty(frame_obj,
2908 : isolate->factory()->InternalizeOneByteString(
2909 : STATIC_CHAR_VECTOR("scriptName")),
2910 0 : handle(frame->script_name(), isolate), i::NONE);
2911 : i::JSObject::AddProperty(frame_obj,
2912 : isolate->factory()->InternalizeOneByteString(
2913 : STATIC_CHAR_VECTOR("scriptNameOrSourceURL")),
2914 : handle(frame->script_name_or_source_url(), isolate),
2915 0 : i::NONE);
2916 : i::JSObject::AddProperty(frame_obj,
2917 : isolate->factory()->InternalizeOneByteString(
2918 : STATIC_CHAR_VECTOR("functionName")),
2919 0 : handle(frame->function_name(), isolate), i::NONE);
2920 : i::JSObject::AddProperty(frame_obj,
2921 : isolate->factory()->InternalizeOneByteString(
2922 : STATIC_CHAR_VECTOR("isEval")),
2923 : isolate->factory()->ToBoolean(frame->is_eval()),
2924 0 : i::NONE);
2925 : i::JSObject::AddProperty(
2926 : frame_obj,
2927 : isolate->factory()->InternalizeOneByteString(
2928 : STATIC_CHAR_VECTOR("isConstructor")),
2929 0 : isolate->factory()->ToBoolean(frame->is_constructor()), i::NONE);
2930 0 : return frame_obj;
2931 : }
2932 : } // namespace
2933 :
2934 0 : Local<Array> StackTrace::AsArray() {
2935 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2936 : i::Handle<i::FixedArray> self = Utils::OpenHandle(this);
2937 : int frame_count = self->length();
2938 : i::Handle<i::FixedArray> frames =
2939 0 : isolate->factory()->NewFixedArray(frame_count);
2940 0 : for (int i = 0; i < frame_count; ++i) {
2941 : auto obj = handle(self->get(i), isolate);
2942 0 : auto frame = i::Handle<i::StackFrameInfo>::cast(obj);
2943 0 : i::Handle<i::JSObject> frame_obj = NewFrameObject(isolate, frame);
2944 0 : frames->set(i, *frame_obj);
2945 : }
2946 : return Utils::ToLocal(isolate->factory()->NewJSArrayWithElements(
2947 0 : frames, i::FAST_ELEMENTS, frame_count));
2948 : }
2949 :
2950 :
2951 61660 : Local<StackTrace> StackTrace::CurrentStackTrace(
2952 : Isolate* isolate,
2953 : int frame_limit,
2954 : StackTraceOptions options) {
2955 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2956 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
2957 : i::Handle<i::FixedArray> stackTrace =
2958 61660 : i_isolate->CaptureCurrentStackTrace(frame_limit, options);
2959 61660 : return Utils::StackTraceToLocal(stackTrace);
2960 : }
2961 :
2962 :
2963 : // --- S t a c k F r a m e ---
2964 :
2965 45483 : int StackFrame::GetLineNumber() const {
2966 : int v = Utils::OpenHandle(this)->line_number();
2967 45483 : return v ? v : Message::kNoLineNumberInfo;
2968 : }
2969 :
2970 :
2971 45441 : int StackFrame::GetColumn() const {
2972 : int v = Utils::OpenHandle(this)->column_number();
2973 45441 : return v ? v : Message::kNoLineNumberInfo;
2974 : }
2975 :
2976 :
2977 45173 : int StackFrame::GetScriptId() const {
2978 : int v = Utils::OpenHandle(this)->script_id();
2979 45173 : return v ? v : Message::kNoScriptIdInfo;
2980 : }
2981 :
2982 212 : Local<String> StackFrame::GetScriptName() const {
2983 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2984 212 : EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
2985 : i::Handle<i::StackFrameInfo> self = Utils::OpenHandle(this);
2986 : i::Handle<i::Object> obj(self->script_name(), isolate);
2987 : return obj->IsString()
2988 : ? scope.Escape(Local<String>::Cast(Utils::ToLocal(obj)))
2989 568 : : Local<String>();
2990 : }
2991 :
2992 :
2993 45239 : Local<String> StackFrame::GetScriptNameOrSourceURL() const {
2994 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2995 45239 : EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
2996 : i::Handle<i::StackFrameInfo> self = Utils::OpenHandle(this);
2997 : i::Handle<i::Object> obj(self->script_name_or_source_url(), isolate);
2998 : return obj->IsString()
2999 : ? scope.Escape(Local<String>::Cast(Utils::ToLocal(obj)))
3000 135195 : : Local<String>();
3001 : }
3002 :
3003 :
3004 45398 : Local<String> StackFrame::GetFunctionName() const {
3005 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
3006 45398 : EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
3007 : i::Handle<i::StackFrameInfo> self = Utils::OpenHandle(this);
3008 : i::Handle<i::Object> obj(self->function_name(), isolate);
3009 : return obj->IsString()
3010 : ? scope.Escape(Local<String>::Cast(Utils::ToLocal(obj)))
3011 136194 : : Local<String>();
3012 : }
3013 :
3014 240 : bool StackFrame::IsEval() const { return Utils::OpenHandle(this)->is_eval(); }
3015 :
3016 120 : bool StackFrame::IsConstructor() const {
3017 120 : return Utils::OpenHandle(this)->is_constructor();
3018 : }
3019 :
3020 90262 : bool StackFrame::IsWasm() const { return Utils::OpenHandle(this)->is_wasm(); }
3021 :
3022 : // --- N a t i v e W e a k M a p ---
3023 :
3024 6 : Local<NativeWeakMap> NativeWeakMap::New(Isolate* v8_isolate) {
3025 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
3026 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
3027 6 : i::Handle<i::JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
3028 6 : i::JSWeakCollection::Initialize(weakmap, isolate);
3029 6 : return Utils::NativeWeakMapToLocal(weakmap);
3030 : }
3031 :
3032 :
3033 24 : void NativeWeakMap::Set(Local<Value> v8_key, Local<Value> v8_value) {
3034 : i::Handle<i::JSWeakMap> weak_collection = Utils::OpenHandle(this);
3035 : i::Isolate* isolate = weak_collection->GetIsolate();
3036 : ENTER_V8(isolate);
3037 : i::HandleScope scope(isolate);
3038 : i::Handle<i::Object> key = Utils::OpenHandle(*v8_key);
3039 24 : i::Handle<i::Object> value = Utils::OpenHandle(*v8_value);
3040 30 : if (!key->IsJSReceiver() && !key->IsSymbol()) {
3041 : DCHECK(false);
3042 : return;
3043 : }
3044 : i::Handle<i::ObjectHashTable> table(
3045 : i::ObjectHashTable::cast(weak_collection->table()));
3046 24 : if (!table->IsKey(isolate, *key)) {
3047 : DCHECK(false);
3048 : return;
3049 : }
3050 24 : int32_t hash = i::Object::GetOrCreateHash(isolate, key)->value();
3051 24 : i::JSWeakCollection::Set(weak_collection, key, value, hash);
3052 : }
3053 :
3054 72 : Local<Value> NativeWeakMap::Get(Local<Value> v8_key) const {
3055 : i::Handle<i::JSWeakMap> weak_collection = Utils::OpenHandle(this);
3056 : i::Isolate* isolate = weak_collection->GetIsolate();
3057 : ENTER_V8(isolate);
3058 : i::Handle<i::Object> key = Utils::OpenHandle(*v8_key);
3059 84 : if (!key->IsJSReceiver() && !key->IsSymbol()) {
3060 : DCHECK(false);
3061 0 : return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
3062 : }
3063 : i::Handle<i::ObjectHashTable> table(
3064 : i::ObjectHashTable::cast(weak_collection->table()));
3065 72 : if (!table->IsKey(isolate, *key)) {
3066 : DCHECK(false);
3067 0 : return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
3068 : }
3069 72 : i::Handle<i::Object> lookup(table->Lookup(key), isolate);
3070 72 : if (lookup->IsTheHole(isolate))
3071 12 : return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
3072 : return Utils::ToLocal(lookup);
3073 : }
3074 :
3075 :
3076 42 : bool NativeWeakMap::Has(Local<Value> v8_key) {
3077 : i::Handle<i::JSWeakMap> weak_collection = Utils::OpenHandle(this);
3078 : i::Isolate* isolate = weak_collection->GetIsolate();
3079 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
3080 : i::HandleScope scope(isolate);
3081 : i::Handle<i::Object> key = Utils::OpenHandle(*v8_key);
3082 48 : if (!key->IsJSReceiver() && !key->IsSymbol()) {
3083 : DCHECK(false);
3084 : return false;
3085 : }
3086 : i::Handle<i::ObjectHashTable> table(
3087 : i::ObjectHashTable::cast(weak_collection->table()));
3088 42 : if (!table->IsKey(isolate, *key)) {
3089 : DCHECK(false);
3090 : return false;
3091 : }
3092 42 : i::Handle<i::Object> lookup(table->Lookup(key), isolate);
3093 42 : return !lookup->IsTheHole(isolate);
3094 : }
3095 :
3096 :
3097 6 : bool NativeWeakMap::Delete(Local<Value> v8_key) {
3098 : i::Handle<i::JSWeakMap> weak_collection = Utils::OpenHandle(this);
3099 : i::Isolate* isolate = weak_collection->GetIsolate();
3100 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
3101 : i::HandleScope scope(isolate);
3102 : i::Handle<i::Object> key = Utils::OpenHandle(*v8_key);
3103 6 : if (!key->IsJSReceiver() && !key->IsSymbol()) {
3104 : DCHECK(false);
3105 : return false;
3106 : }
3107 : i::Handle<i::ObjectHashTable> table(
3108 : i::ObjectHashTable::cast(weak_collection->table()));
3109 6 : if (!table->IsKey(isolate, *key)) {
3110 : DCHECK(false);
3111 : return false;
3112 : }
3113 6 : int32_t hash = i::Object::GetOrCreateHash(isolate, key)->value();
3114 6 : return i::JSWeakCollection::Delete(weak_collection, key, hash);
3115 : }
3116 :
3117 :
3118 : // --- J S O N ---
3119 :
3120 0 : MaybeLocal<Value> JSON::Parse(Isolate* v8_isolate, Local<String> json_string) {
3121 0 : auto isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
3122 0 : PREPARE_FOR_EXECUTION_WITH_ISOLATE(isolate, JSON, Parse, Value);
3123 0 : i::Handle<i::String> string = Utils::OpenHandle(*json_string);
3124 0 : i::Handle<i::String> source = i::String::Flatten(string);
3125 : i::Handle<i::Object> undefined = isolate->factory()->undefined_value();
3126 : auto maybe = source->IsSeqOneByteString()
3127 : ? i::JsonParser<true>::Parse(isolate, source, undefined)
3128 0 : : i::JsonParser<false>::Parse(isolate, source, undefined);
3129 : Local<Value> result;
3130 0 : has_pending_exception = !ToLocal<Value>(maybe, &result);
3131 0 : RETURN_ON_FAILED_EXECUTION(Value);
3132 0 : RETURN_ESCAPED(result);
3133 : }
3134 :
3135 32 : MaybeLocal<Value> JSON::Parse(Local<Context> context,
3136 : Local<String> json_string) {
3137 128 : PREPARE_FOR_EXECUTION(context, JSON, Parse, Value);
3138 32 : i::Handle<i::String> string = Utils::OpenHandle(*json_string);
3139 32 : i::Handle<i::String> source = i::String::Flatten(string);
3140 : i::Handle<i::Object> undefined = isolate->factory()->undefined_value();
3141 : auto maybe = source->IsSeqOneByteString()
3142 : ? i::JsonParser<true>::Parse(isolate, source, undefined)
3143 32 : : i::JsonParser<false>::Parse(isolate, source, undefined);
3144 : Local<Value> result;
3145 32 : has_pending_exception = !ToLocal<Value>(maybe, &result);
3146 33 : RETURN_ON_FAILED_EXECUTION(Value);
3147 31 : RETURN_ESCAPED(result);
3148 : }
3149 :
3150 0 : Local<Value> JSON::Parse(Local<String> json_string) {
3151 0 : RETURN_TO_LOCAL_UNCHECKED(Parse(Local<Context>(), json_string), Value);
3152 : }
3153 :
3154 16 : MaybeLocal<String> JSON::Stringify(Local<Context> context,
3155 : Local<Object> json_object,
3156 : Local<String> gap) {
3157 64 : PREPARE_FOR_EXECUTION(context, JSON, Stringify, String);
3158 : i::Handle<i::Object> object = Utils::OpenHandle(*json_object);
3159 : i::Handle<i::Object> replacer = isolate->factory()->undefined_value();
3160 : i::Handle<i::String> gap_string = gap.IsEmpty()
3161 : ? isolate->factory()->empty_string()
3162 16 : : Utils::OpenHandle(*gap);
3163 : i::Handle<i::Object> maybe;
3164 : has_pending_exception = !i::JsonStringifier(isolate)
3165 : .Stringify(object, replacer, gap_string)
3166 48 : .ToHandle(&maybe);
3167 16 : RETURN_ON_FAILED_EXECUTION(String);
3168 : Local<String> result;
3169 : has_pending_exception =
3170 32 : !ToLocal<String>(i::Object::ToString(isolate, maybe), &result);
3171 16 : RETURN_ON_FAILED_EXECUTION(String);
3172 16 : RETURN_ESCAPED(result);
3173 : }
3174 :
3175 : // --- V a l u e S e r i a l i z a t i o n ---
3176 :
3177 0 : Maybe<bool> ValueSerializer::Delegate::WriteHostObject(Isolate* v8_isolate,
3178 : Local<Object> object) {
3179 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
3180 : isolate->ScheduleThrow(*isolate->factory()->NewError(
3181 : isolate->error_function(), i::MessageTemplate::kDataCloneError,
3182 0 : Utils::OpenHandle(*object)));
3183 0 : return Nothing<bool>();
3184 : }
3185 :
3186 0 : Maybe<uint32_t> ValueSerializer::Delegate::GetSharedArrayBufferId(
3187 : Isolate* v8_isolate, Local<SharedArrayBuffer> shared_array_buffer) {
3188 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
3189 : isolate->ScheduleThrow(*isolate->factory()->NewError(
3190 : isolate->error_function(), i::MessageTemplate::kDataCloneError,
3191 0 : Utils::OpenHandle(*shared_array_buffer)));
3192 0 : return Nothing<uint32_t>();
3193 : }
3194 :
3195 0 : Maybe<uint32_t> ValueSerializer::Delegate::GetWasmModuleTransferId(
3196 : Isolate* v8_isolate, Local<WasmCompiledModule> module) {
3197 0 : return Nothing<uint32_t>();
3198 : }
3199 :
3200 19 : void* ValueSerializer::Delegate::ReallocateBufferMemory(void* old_buffer,
3201 : size_t size,
3202 : size_t* actual_size) {
3203 19 : *actual_size = size;
3204 19 : return realloc(old_buffer, size);
3205 : }
3206 :
3207 1 : void ValueSerializer::Delegate::FreeBufferMemory(void* buffer) {
3208 1 : return free(buffer);
3209 : }
3210 :
3211 1243 : struct ValueSerializer::PrivateData {
3212 : explicit PrivateData(i::Isolate* i, ValueSerializer::Delegate* delegate)
3213 1243 : : isolate(i), serializer(i, delegate) {}
3214 : i::Isolate* isolate;
3215 : i::ValueSerializer serializer;
3216 : };
3217 :
3218 : // static
3219 0 : uint32_t ValueSerializer::GetCurrentDataFormatVersion() {
3220 0 : return i::ValueSerializer::GetCurrentDataFormatVersion();
3221 : }
3222 :
3223 0 : ValueSerializer::ValueSerializer(Isolate* isolate)
3224 0 : : ValueSerializer(isolate, nullptr) {}
3225 :
3226 1243 : ValueSerializer::ValueSerializer(Isolate* isolate, Delegate* delegate)
3227 : : private_(
3228 2486 : new PrivateData(reinterpret_cast<i::Isolate*>(isolate), delegate)) {}
3229 :
3230 2486 : ValueSerializer::~ValueSerializer() { delete private_; }
3231 :
3232 1228 : void ValueSerializer::WriteHeader() { private_->serializer.WriteHeader(); }
3233 :
3234 1 : void ValueSerializer::SetTreatArrayBufferViewsAsHostObjects(bool mode) {
3235 1 : private_->serializer.SetTreatArrayBufferViewsAsHostObjects(mode);
3236 1 : }
3237 :
3238 1228 : Maybe<bool> ValueSerializer::WriteValue(Local<Context> context,
3239 : Local<Value> value) {
3240 4912 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, ValueSerializer, WriteValue, bool);
3241 1228 : i::Handle<i::Object> object = Utils::OpenHandle(*value);
3242 1228 : Maybe<bool> result = private_->serializer.WriteObject(object);
3243 1228 : has_pending_exception = result.IsNothing();
3244 1228 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3245 983 : return result;
3246 : }
3247 :
3248 0 : std::vector<uint8_t> ValueSerializer::ReleaseBuffer() {
3249 0 : return private_->serializer.ReleaseBuffer();
3250 : }
3251 :
3252 983 : std::pair<uint8_t*, size_t> ValueSerializer::Release() {
3253 983 : return private_->serializer.Release();
3254 : }
3255 :
3256 32 : void ValueSerializer::TransferArrayBuffer(uint32_t transfer_id,
3257 : Local<ArrayBuffer> array_buffer) {
3258 : private_->serializer.TransferArrayBuffer(transfer_id,
3259 32 : Utils::OpenHandle(*array_buffer));
3260 32 : }
3261 :
3262 0 : void ValueSerializer::TransferSharedArrayBuffer(
3263 : uint32_t transfer_id, Local<SharedArrayBuffer> shared_array_buffer) {
3264 : private_->serializer.TransferArrayBuffer(
3265 0 : transfer_id, Utils::OpenHandle(*shared_array_buffer));
3266 0 : }
3267 :
3268 2 : void ValueSerializer::WriteUint32(uint32_t value) {
3269 2 : private_->serializer.WriteUint32(value);
3270 2 : }
3271 :
3272 2 : void ValueSerializer::WriteUint64(uint64_t value) {
3273 2 : private_->serializer.WriteUint64(value);
3274 2 : }
3275 :
3276 4 : void ValueSerializer::WriteDouble(double value) {
3277 4 : private_->serializer.WriteDouble(value);
3278 4 : }
3279 :
3280 12 : void ValueSerializer::WriteRawBytes(const void* source, size_t length) {
3281 12 : private_->serializer.WriteRawBytes(source, length);
3282 12 : }
3283 :
3284 0 : MaybeLocal<Object> ValueDeserializer::Delegate::ReadHostObject(
3285 : Isolate* v8_isolate) {
3286 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
3287 : isolate->ScheduleThrow(*isolate->factory()->NewError(
3288 : isolate->error_function(),
3289 0 : i::MessageTemplate::kDataCloneDeserializationError));
3290 0 : return MaybeLocal<Object>();
3291 : }
3292 :
3293 1 : MaybeLocal<WasmCompiledModule> ValueDeserializer::Delegate::GetWasmModuleFromId(
3294 : Isolate* v8_isolate, uint32_t id) {
3295 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
3296 : isolate->ScheduleThrow(*isolate->factory()->NewError(
3297 : isolate->error_function(),
3298 2 : i::MessageTemplate::kDataCloneDeserializationError));
3299 1 : return MaybeLocal<WasmCompiledModule>();
3300 : }
3301 :
3302 1025 : struct ValueDeserializer::PrivateData {
3303 : PrivateData(i::Isolate* i, i::Vector<const uint8_t> data, Delegate* delegate)
3304 1025 : : isolate(i), deserializer(i, data, delegate) {}
3305 : i::Isolate* isolate;
3306 : i::ValueDeserializer deserializer;
3307 : bool has_aborted = false;
3308 : bool supports_legacy_wire_format = false;
3309 : };
3310 :
3311 0 : ValueDeserializer::ValueDeserializer(Isolate* isolate, const uint8_t* data,
3312 : size_t size)
3313 0 : : ValueDeserializer(isolate, data, size, nullptr) {}
3314 :
3315 1025 : ValueDeserializer::ValueDeserializer(Isolate* isolate, const uint8_t* data,
3316 : size_t size, Delegate* delegate) {
3317 1025 : if (base::IsValueInRangeForNumericType<int>(size)) {
3318 : private_ = new PrivateData(
3319 : reinterpret_cast<i::Isolate*>(isolate),
3320 2050 : i::Vector<const uint8_t>(data, static_cast<int>(size)), delegate);
3321 : } else {
3322 : private_ = new PrivateData(reinterpret_cast<i::Isolate*>(isolate),
3323 0 : i::Vector<const uint8_t>(nullptr, 0), nullptr);
3324 0 : private_->has_aborted = true;
3325 : }
3326 1025 : }
3327 :
3328 2050 : ValueDeserializer::~ValueDeserializer() { delete private_; }
3329 :
3330 1025 : Maybe<bool> ValueDeserializer::ReadHeader(Local<Context> context) {
3331 4100 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, ValueDeserializer, ReadHeader, bool);
3332 :
3333 : // We could have aborted during the constructor.
3334 : // If so, ReadHeader is where we report it.
3335 1025 : if (private_->has_aborted) {
3336 : isolate->Throw(*isolate->factory()->NewError(
3337 0 : i::MessageTemplate::kDataCloneDeserializationError));
3338 : has_pending_exception = true;
3339 0 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3340 : }
3341 :
3342 : bool read_header = false;
3343 2050 : has_pending_exception = !private_->deserializer.ReadHeader().To(&read_header);
3344 1025 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3345 : DCHECK(read_header);
3346 :
3347 : static const uint32_t kMinimumNonLegacyVersion = 13;
3348 1140 : if (GetWireFormatVersion() < kMinimumNonLegacyVersion &&
3349 117 : !private_->supports_legacy_wire_format) {
3350 : isolate->Throw(*isolate->factory()->NewError(
3351 0 : i::MessageTemplate::kDataCloneDeserializationVersionError));
3352 : has_pending_exception = true;
3353 0 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3354 : }
3355 :
3356 : return Just(true);
3357 : }
3358 :
3359 1025 : void ValueDeserializer::SetSupportsLegacyWireFormat(
3360 : bool supports_legacy_wire_format) {
3361 1025 : private_->supports_legacy_wire_format = supports_legacy_wire_format;
3362 1025 : }
3363 :
3364 240 : void ValueDeserializer::SetExpectInlineWasm(bool expect_inline_wasm) {
3365 240 : private_->deserializer.set_expect_inline_wasm(expect_inline_wasm);
3366 240 : }
3367 :
3368 2054 : uint32_t ValueDeserializer::GetWireFormatVersion() const {
3369 2054 : CHECK(!private_->has_aborted);
3370 2054 : return private_->deserializer.GetWireFormatVersion();
3371 : }
3372 :
3373 1023 : MaybeLocal<Value> ValueDeserializer::ReadValue(Local<Context> context) {
3374 1023 : CHECK(!private_->has_aborted);
3375 4092 : PREPARE_FOR_EXECUTION(context, ValueDeserializer, ReadValue, Value);
3376 : i::MaybeHandle<i::Object> result;
3377 1023 : if (GetWireFormatVersion() > 0) {
3378 1011 : result = private_->deserializer.ReadObject();
3379 : } else {
3380 : result =
3381 12 : private_->deserializer.ReadObjectUsingEntireBufferForLegacyFormat();
3382 : }
3383 : Local<Value> value;
3384 : has_pending_exception = !ToLocal(result, &value);
3385 1046 : RETURN_ON_FAILED_EXECUTION(Value);
3386 1000 : RETURN_ESCAPED(value);
3387 : }
3388 :
3389 32 : void ValueDeserializer::TransferArrayBuffer(uint32_t transfer_id,
3390 : Local<ArrayBuffer> array_buffer) {
3391 32 : CHECK(!private_->has_aborted);
3392 : private_->deserializer.TransferArrayBuffer(transfer_id,
3393 32 : Utils::OpenHandle(*array_buffer));
3394 32 : }
3395 :
3396 259 : void ValueDeserializer::TransferSharedArrayBuffer(
3397 : uint32_t transfer_id, Local<SharedArrayBuffer> shared_array_buffer) {
3398 259 : CHECK(!private_->has_aborted);
3399 : private_->deserializer.TransferArrayBuffer(
3400 259 : transfer_id, Utils::OpenHandle(*shared_array_buffer));
3401 259 : }
3402 :
3403 2 : bool ValueDeserializer::ReadUint32(uint32_t* value) {
3404 2 : return private_->deserializer.ReadUint32(value);
3405 : }
3406 :
3407 2 : bool ValueDeserializer::ReadUint64(uint64_t* value) {
3408 2 : return private_->deserializer.ReadUint64(value);
3409 : }
3410 :
3411 4 : bool ValueDeserializer::ReadDouble(double* value) {
3412 4 : return private_->deserializer.ReadDouble(value);
3413 : }
3414 :
3415 13 : bool ValueDeserializer::ReadRawBytes(size_t length, const void** data) {
3416 13 : return private_->deserializer.ReadRawBytes(length, data);
3417 : }
3418 :
3419 : // --- D a t a ---
3420 :
3421 0 : bool Value::FullIsUndefined() const {
3422 : i::Handle<i::Object> object = Utils::OpenHandle(this);
3423 : bool result = false;
3424 0 : if (!object->IsSmi()) {
3425 : result = object->IsUndefined(i::HeapObject::cast(*object)->GetIsolate());
3426 : }
3427 : DCHECK_EQ(result, QuickIsUndefined());
3428 0 : return result;
3429 : }
3430 :
3431 :
3432 0 : bool Value::FullIsNull() const {
3433 : i::Handle<i::Object> object = Utils::OpenHandle(this);
3434 : bool result = false;
3435 0 : if (!object->IsSmi()) {
3436 : result = object->IsNull(i::HeapObject::cast(*object)->GetIsolate());
3437 : }
3438 : DCHECK_EQ(result, QuickIsNull());
3439 0 : return result;
3440 : }
3441 :
3442 :
3443 156549 : bool Value::IsTrue() const {
3444 : i::Handle<i::Object> object = Utils::OpenHandle(this);
3445 156549 : if (object->IsSmi()) return false;
3446 156549 : return object->IsTrue(i::HeapObject::cast(*object)->GetIsolate());
3447 : }
3448 :
3449 :
3450 137 : bool Value::IsFalse() const {
3451 : i::Handle<i::Object> object = Utils::OpenHandle(this);
3452 137 : if (object->IsSmi()) return false;
3453 137 : return object->IsFalse(i::HeapObject::cast(*object)->GetIsolate());
3454 : }
3455 :
3456 :
3457 1190870 : bool Value::IsFunction() const { return Utils::OpenHandle(this)->IsCallable(); }
3458 :
3459 :
3460 15042 : bool Value::IsName() const {
3461 15042 : return Utils::OpenHandle(this)->IsName();
3462 : }
3463 :
3464 :
3465 0 : bool Value::FullIsString() const {
3466 : bool result = Utils::OpenHandle(this)->IsString();
3467 : DCHECK_EQ(result, QuickIsString());
3468 0 : return result;
3469 : }
3470 :
3471 :
3472 4317521 : bool Value::IsSymbol() const {
3473 4317521 : return Utils::OpenHandle(this)->IsSymbol();
3474 : }
3475 :
3476 :
3477 20086498 : bool Value::IsArray() const {
3478 20086498 : return Utils::OpenHandle(this)->IsJSArray();
3479 : }
3480 :
3481 :
3482 13771 : bool Value::IsArrayBuffer() const {
3483 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3484 26132 : return obj->IsJSArrayBuffer() && !i::JSArrayBuffer::cast(*obj)->is_shared();
3485 : }
3486 :
3487 :
3488 91 : bool Value::IsArrayBufferView() const {
3489 91 : return Utils::OpenHandle(this)->IsJSArrayBufferView();
3490 : }
3491 :
3492 :
3493 10171662 : bool Value::IsTypedArray() const {
3494 10171662 : return Utils::OpenHandle(this)->IsJSTypedArray();
3495 : }
3496 :
3497 :
3498 : #define VALUE_IS_TYPED_ARRAY(Type, typeName, TYPE, ctype, size) \
3499 : bool Value::Is##Type##Array() const { \
3500 : i::Handle<i::Object> obj = Utils::OpenHandle(this); \
3501 : return obj->IsJSTypedArray() && \
3502 : i::JSTypedArray::cast(*obj)->type() == i::kExternal##Type##Array; \
3503 : }
3504 :
3505 :
3506 243 : TYPED_ARRAYS(VALUE_IS_TYPED_ARRAY)
3507 :
3508 : #undef VALUE_IS_TYPED_ARRAY
3509 :
3510 :
3511 9 : bool Value::IsDataView() const {
3512 9 : return Utils::OpenHandle(this)->IsJSDataView();
3513 : }
3514 :
3515 :
3516 1 : bool Value::IsSharedArrayBuffer() const {
3517 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3518 2 : return obj->IsJSArrayBuffer() && i::JSArrayBuffer::cast(*obj)->is_shared();
3519 : }
3520 :
3521 :
3522 66439794 : bool Value::IsObject() const { return Utils::OpenHandle(this)->IsJSReceiver(); }
3523 :
3524 :
3525 32037519 : bool Value::IsNumber() const {
3526 32037519 : return Utils::OpenHandle(this)->IsNumber();
3527 : }
3528 :
3529 :
3530 20305204 : bool Value::IsProxy() const { return Utils::OpenHandle(this)->IsJSProxy(); }
3531 :
3532 112 : bool Value::IsWebAssemblyCompiledModule() const {
3533 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3534 112 : if (!obj->IsJSObject()) return false;
3535 : i::Handle<i::JSObject> js_obj = i::Handle<i::JSObject>::cast(obj);
3536 224 : return js_obj->GetIsolate()->native_context()->wasm_module_constructor() ==
3537 112 : js_obj->map()->GetConstructor();
3538 : }
3539 :
3540 : #define VALUE_IS_SPECIFIC_TYPE(Type, Class) \
3541 : bool Value::Is##Type() const { \
3542 : i::Handle<i::Object> obj = Utils::OpenHandle(this); \
3543 : if (!obj->IsHeapObject()) return false; \
3544 : i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate(); \
3545 : return obj->HasSpecificClassOf(isolate->heap()->Class##_string()); \
3546 : }
3547 :
3548 30619298 : VALUE_IS_SPECIFIC_TYPE(ArgumentsObject, Arguments)
3549 2931 : VALUE_IS_SPECIFIC_TYPE(BooleanObject, Boolean)
3550 2777 : VALUE_IS_SPECIFIC_TYPE(NumberObject, Number)
3551 2834 : VALUE_IS_SPECIFIC_TYPE(StringObject, String)
3552 2703 : VALUE_IS_SPECIFIC_TYPE(SymbolObject, Symbol)
3553 30510723 : VALUE_IS_SPECIFIC_TYPE(Date, Date)
3554 30830084 : VALUE_IS_SPECIFIC_TYPE(Map, Map)
3555 30828290 : VALUE_IS_SPECIFIC_TYPE(Set, Set)
3556 30509888 : VALUE_IS_SPECIFIC_TYPE(WeakMap, WeakMap)
3557 30508160 : VALUE_IS_SPECIFIC_TYPE(WeakSet, WeakSet)
3558 :
3559 : #undef VALUE_IS_SPECIFIC_TYPE
3560 :
3561 :
3562 45912071 : bool Value::IsBoolean() const {
3563 45912071 : return Utils::OpenHandle(this)->IsBoolean();
3564 : }
3565 :
3566 :
3567 6 : bool Value::IsExternal() const {
3568 6 : return Utils::OpenHandle(this)->IsExternal();
3569 : }
3570 :
3571 :
3572 795795 : bool Value::IsInt32() const {
3573 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3574 795795 : if (obj->IsSmi()) return true;
3575 42 : if (obj->IsNumber()) {
3576 42 : return i::IsInt32Double(obj->Number());
3577 : }
3578 : return false;
3579 : }
3580 :
3581 :
3582 258 : bool Value::IsUint32() const {
3583 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3584 383 : if (obj->IsSmi()) return i::Smi::cast(*obj)->value() >= 0;
3585 133 : if (obj->IsNumber()) {
3586 : double value = obj->Number();
3587 126 : return !i::IsMinusZero(value) &&
3588 112 : value >= 0 &&
3589 238 : value <= i::kMaxUInt32 &&
3590 133 : value == i::FastUI2D(i::FastD2UI(value));
3591 : }
3592 : return false;
3593 : }
3594 :
3595 :
3596 10167588 : bool Value::IsNativeError() const {
3597 10167588 : return Utils::OpenHandle(this)->IsJSError();
3598 : }
3599 :
3600 :
3601 10170185 : bool Value::IsRegExp() const {
3602 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3603 10170185 : return obj->IsJSRegExp();
3604 : }
3605 :
3606 14 : bool Value::IsAsyncFunction() const {
3607 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3608 14 : if (!obj->IsJSFunction()) return false;
3609 : i::Handle<i::JSFunction> func = i::Handle<i::JSFunction>::cast(obj);
3610 14 : return i::IsAsyncFunction(func->shared()->kind());
3611 : }
3612 :
3613 65 : bool Value::IsGeneratorFunction() const {
3614 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3615 65 : if (!obj->IsJSFunction()) return false;
3616 : i::Handle<i::JSFunction> func = i::Handle<i::JSFunction>::cast(obj);
3617 45 : return i::IsGeneratorFunction(func->shared()->kind());
3618 : }
3619 :
3620 :
3621 10274719 : bool Value::IsGeneratorObject() const {
3622 10274719 : return Utils::OpenHandle(this)->IsJSGeneratorObject();
3623 : }
3624 :
3625 :
3626 10169222 : bool Value::IsMapIterator() const {
3627 10169222 : return Utils::OpenHandle(this)->IsJSMapIterator();
3628 : }
3629 :
3630 :
3631 10168550 : bool Value::IsSetIterator() const {
3632 10168550 : return Utils::OpenHandle(this)->IsJSSetIterator();
3633 : }
3634 :
3635 20310768 : bool Value::IsPromise() const { return Utils::OpenHandle(this)->IsJSPromise(); }
3636 :
3637 60694787 : MaybeLocal<String> Value::ToString(Local<Context> context) const {
3638 : auto obj = Utils::OpenHandle(this);
3639 60694785 : if (obj->IsString()) return ToApiHandle<String>(obj);
3640 166676 : PREPARE_FOR_EXECUTION(context, Object, ToString, String);
3641 : Local<String> result;
3642 : has_pending_exception =
3643 83338 : !ToLocal<String>(i::Object::ToString(isolate, obj), &result);
3644 41798 : RETURN_ON_FAILED_EXECUTION(String);
3645 41540 : RETURN_ESCAPED(result);
3646 : }
3647 :
3648 :
3649 0 : Local<String> Value::ToString(Isolate* isolate) const {
3650 0 : RETURN_TO_LOCAL_UNCHECKED(ToString(isolate->GetCurrentContext()), String);
3651 : }
3652 :
3653 :
3654 0 : MaybeLocal<String> Value::ToDetailString(Local<Context> context) const {
3655 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3656 0 : if (obj->IsString()) return ToApiHandle<String>(obj);
3657 0 : PREPARE_FOR_EXECUTION(context, Object, ToDetailString, String);
3658 : Local<String> result =
3659 0 : Utils::ToLocal(i::Object::NoSideEffectsToString(isolate, obj));
3660 : RETURN_ON_FAILED_EXECUTION(String);
3661 0 : RETURN_ESCAPED(result);
3662 : }
3663 :
3664 :
3665 0 : Local<String> Value::ToDetailString(Isolate* isolate) const {
3666 0 : RETURN_TO_LOCAL_UNCHECKED(ToDetailString(isolate->GetCurrentContext()),
3667 : String);
3668 : }
3669 :
3670 :
3671 2544 : MaybeLocal<Object> Value::ToObject(Local<Context> context) const {
3672 : auto obj = Utils::OpenHandle(this);
3673 2544 : if (obj->IsJSReceiver()) return ToApiHandle<Object>(obj);
3674 28 : PREPARE_FOR_EXECUTION(context, Object, ToObject, Object);
3675 : Local<Object> result;
3676 : has_pending_exception =
3677 14 : !ToLocal<Object>(i::Object::ToObject(isolate, obj), &result);
3678 14 : RETURN_ON_FAILED_EXECUTION(Object);
3679 0 : RETURN_ESCAPED(result);
3680 : }
3681 :
3682 :
3683 0 : Local<v8::Object> Value::ToObject(Isolate* isolate) const {
3684 0 : RETURN_TO_LOCAL_UNCHECKED(ToObject(isolate->GetCurrentContext()), Object);
3685 : }
3686 :
3687 :
3688 180 : MaybeLocal<Boolean> Value::ToBoolean(Local<Context> context) const {
3689 : auto obj = Utils::OpenHandle(this);
3690 180 : if (obj->IsBoolean()) return ToApiHandle<Boolean>(obj);
3691 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
3692 0 : auto val = isolate->factory()->ToBoolean(obj->BooleanValue());
3693 0 : return ToApiHandle<Boolean>(val);
3694 : }
3695 :
3696 :
3697 0 : Local<Boolean> Value::ToBoolean(Isolate* v8_isolate) const {
3698 0 : return ToBoolean(v8_isolate->GetCurrentContext()).ToLocalChecked();
3699 : }
3700 :
3701 :
3702 141 : MaybeLocal<Number> Value::ToNumber(Local<Context> context) const {
3703 : auto obj = Utils::OpenHandle(this);
3704 141 : if (obj->IsNumber()) return ToApiHandle<Number>(obj);
3705 28 : PREPARE_FOR_EXECUTION(context, Object, ToNumber, Number);
3706 : Local<Number> result;
3707 14 : has_pending_exception = !ToLocal<Number>(i::Object::ToNumber(obj), &result);
3708 14 : RETURN_ON_FAILED_EXECUTION(Number);
3709 0 : RETURN_ESCAPED(result);
3710 : }
3711 :
3712 :
3713 0 : Local<Number> Value::ToNumber(Isolate* isolate) const {
3714 0 : RETURN_TO_LOCAL_UNCHECKED(ToNumber(isolate->GetCurrentContext()), Number);
3715 : }
3716 :
3717 :
3718 49 : MaybeLocal<Integer> Value::ToInteger(Local<Context> context) const {
3719 : auto obj = Utils::OpenHandle(this);
3720 49 : if (obj->IsSmi()) return ToApiHandle<Integer>(obj);
3721 28 : PREPARE_FOR_EXECUTION(context, Object, ToInteger, Integer);
3722 : Local<Integer> result;
3723 : has_pending_exception =
3724 7 : !ToLocal<Integer>(i::Object::ToInteger(isolate, obj), &result);
3725 14 : RETURN_ON_FAILED_EXECUTION(Integer);
3726 0 : RETURN_ESCAPED(result);
3727 : }
3728 :
3729 :
3730 0 : Local<Integer> Value::ToInteger(Isolate* isolate) const {
3731 0 : RETURN_TO_LOCAL_UNCHECKED(ToInteger(isolate->GetCurrentContext()), Integer);
3732 : }
3733 :
3734 :
3735 148 : MaybeLocal<Int32> Value::ToInt32(Local<Context> context) const {
3736 : auto obj = Utils::OpenHandle(this);
3737 148 : if (obj->IsSmi()) return ToApiHandle<Int32>(obj);
3738 : Local<Int32> result;
3739 192 : PREPARE_FOR_EXECUTION(context, Object, ToInt32, Int32);
3740 : has_pending_exception =
3741 48 : !ToLocal<Int32>(i::Object::ToInt32(isolate, obj), &result);
3742 55 : RETURN_ON_FAILED_EXECUTION(Int32);
3743 41 : RETURN_ESCAPED(result);
3744 : }
3745 :
3746 :
3747 0 : Local<Int32> Value::ToInt32(Isolate* isolate) const {
3748 0 : RETURN_TO_LOCAL_UNCHECKED(ToInt32(isolate->GetCurrentContext()), Int32);
3749 : }
3750 :
3751 :
3752 56 : MaybeLocal<Uint32> Value::ToUint32(Local<Context> context) const {
3753 : auto obj = Utils::OpenHandle(this);
3754 56 : if (obj->IsSmi()) return ToApiHandle<Uint32>(obj);
3755 : Local<Uint32> result;
3756 168 : PREPARE_FOR_EXECUTION(context, Object, ToUint32, Uint32);
3757 : has_pending_exception =
3758 84 : !ToLocal<Uint32>(i::Object::ToUint32(isolate, obj), &result);
3759 49 : RETURN_ON_FAILED_EXECUTION(Uint32);
3760 35 : RETURN_ESCAPED(result);
3761 : }
3762 :
3763 :
3764 0 : Local<Uint32> Value::ToUint32(Isolate* isolate) const {
3765 0 : RETURN_TO_LOCAL_UNCHECKED(ToUint32(isolate->GetCurrentContext()), Uint32);
3766 : }
3767 :
3768 :
3769 0 : void i::Internals::CheckInitializedImpl(v8::Isolate* external_isolate) {
3770 0 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
3771 0 : Utils::ApiCheck(isolate != NULL && !isolate->IsDead(),
3772 : "v8::internal::Internals::CheckInitialized",
3773 : "Isolate is not initialized or V8 has died");
3774 0 : }
3775 :
3776 :
3777 0 : void External::CheckCast(v8::Value* that) {
3778 : Utils::ApiCheck(Utils::OpenHandle(that)->IsExternal(), "v8::External::Cast",
3779 : "Could not convert to external");
3780 0 : }
3781 :
3782 :
3783 0 : void v8::Object::CheckCast(Value* that) {
3784 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3785 : Utils::ApiCheck(obj->IsJSReceiver(), "v8::Object::Cast",
3786 : "Could not convert to object");
3787 0 : }
3788 :
3789 :
3790 0 : void v8::Function::CheckCast(Value* that) {
3791 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3792 : Utils::ApiCheck(obj->IsCallable(), "v8::Function::Cast",
3793 : "Could not convert to function");
3794 0 : }
3795 :
3796 :
3797 0 : void v8::Boolean::CheckCast(v8::Value* that) {
3798 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3799 : Utils::ApiCheck(obj->IsBoolean(), "v8::Boolean::Cast",
3800 : "Could not convert to boolean");
3801 0 : }
3802 :
3803 :
3804 0 : void v8::Name::CheckCast(v8::Value* that) {
3805 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3806 : Utils::ApiCheck(obj->IsName(), "v8::Name::Cast", "Could not convert to name");
3807 0 : }
3808 :
3809 :
3810 0 : void v8::String::CheckCast(v8::Value* that) {
3811 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3812 : Utils::ApiCheck(obj->IsString(), "v8::String::Cast",
3813 : "Could not convert to string");
3814 0 : }
3815 :
3816 :
3817 0 : void v8::Symbol::CheckCast(v8::Value* that) {
3818 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3819 : Utils::ApiCheck(obj->IsSymbol(), "v8::Symbol::Cast",
3820 : "Could not convert to symbol");
3821 0 : }
3822 :
3823 :
3824 0 : void v8::Number::CheckCast(v8::Value* that) {
3825 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3826 : Utils::ApiCheck(obj->IsNumber(),
3827 : "v8::Number::Cast()",
3828 : "Could not convert to number");
3829 0 : }
3830 :
3831 :
3832 0 : void v8::Integer::CheckCast(v8::Value* that) {
3833 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3834 : Utils::ApiCheck(obj->IsNumber(), "v8::Integer::Cast",
3835 : "Could not convert to number");
3836 0 : }
3837 :
3838 :
3839 0 : void v8::Int32::CheckCast(v8::Value* that) {
3840 0 : Utils::ApiCheck(that->IsInt32(), "v8::Int32::Cast",
3841 : "Could not convert to 32-bit signed integer");
3842 0 : }
3843 :
3844 :
3845 0 : void v8::Uint32::CheckCast(v8::Value* that) {
3846 0 : Utils::ApiCheck(that->IsUint32(), "v8::Uint32::Cast",
3847 : "Could not convert to 32-bit unsigned integer");
3848 0 : }
3849 :
3850 :
3851 0 : void v8::Array::CheckCast(Value* that) {
3852 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3853 : Utils::ApiCheck(obj->IsJSArray(), "v8::Array::Cast",
3854 : "Could not convert to array");
3855 0 : }
3856 :
3857 :
3858 0 : void v8::Map::CheckCast(Value* that) {
3859 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3860 : Utils::ApiCheck(obj->IsJSMap(), "v8::Map::Cast", "Could not convert to Map");
3861 0 : }
3862 :
3863 :
3864 0 : void v8::Set::CheckCast(Value* that) {
3865 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3866 : Utils::ApiCheck(obj->IsJSSet(), "v8_Set_Cast", "Could not convert to Set");
3867 0 : }
3868 :
3869 :
3870 0 : void v8::Promise::CheckCast(Value* that) {
3871 0 : Utils::ApiCheck(that->IsPromise(), "v8::Promise::Cast",
3872 : "Could not convert to promise");
3873 0 : }
3874 :
3875 :
3876 0 : void v8::Promise::Resolver::CheckCast(Value* that) {
3877 0 : Utils::ApiCheck(that->IsPromise(), "v8::Promise::Resolver::Cast",
3878 : "Could not convert to promise resolver");
3879 0 : }
3880 :
3881 :
3882 0 : void v8::Proxy::CheckCast(Value* that) {
3883 0 : Utils::ApiCheck(that->IsProxy(), "v8::Proxy::Cast",
3884 : "Could not convert to proxy");
3885 0 : }
3886 :
3887 0 : void v8::WasmCompiledModule::CheckCast(Value* that) {
3888 0 : Utils::ApiCheck(that->IsWebAssemblyCompiledModule(),
3889 : "v8::WasmCompiledModule::Cast",
3890 : "Could not convert to wasm compiled module");
3891 0 : }
3892 :
3893 0 : void v8::ArrayBuffer::CheckCast(Value* that) {
3894 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3895 : Utils::ApiCheck(
3896 0 : obj->IsJSArrayBuffer() && !i::JSArrayBuffer::cast(*obj)->is_shared(),
3897 : "v8::ArrayBuffer::Cast()", "Could not convert to ArrayBuffer");
3898 0 : }
3899 :
3900 :
3901 0 : void v8::ArrayBufferView::CheckCast(Value* that) {
3902 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3903 : Utils::ApiCheck(obj->IsJSArrayBufferView(),
3904 : "v8::ArrayBufferView::Cast()",
3905 : "Could not convert to ArrayBufferView");
3906 0 : }
3907 :
3908 :
3909 0 : void v8::TypedArray::CheckCast(Value* that) {
3910 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3911 : Utils::ApiCheck(obj->IsJSTypedArray(),
3912 : "v8::TypedArray::Cast()",
3913 : "Could not convert to TypedArray");
3914 0 : }
3915 :
3916 :
3917 : #define CHECK_TYPED_ARRAY_CAST(Type, typeName, TYPE, ctype, size) \
3918 : void v8::Type##Array::CheckCast(Value* that) { \
3919 : i::Handle<i::Object> obj = Utils::OpenHandle(that); \
3920 : Utils::ApiCheck( \
3921 : obj->IsJSTypedArray() && \
3922 : i::JSTypedArray::cast(*obj)->type() == i::kExternal##Type##Array, \
3923 : "v8::" #Type "Array::Cast()", "Could not convert to " #Type "Array"); \
3924 : }
3925 :
3926 :
3927 0 : TYPED_ARRAYS(CHECK_TYPED_ARRAY_CAST)
3928 :
3929 : #undef CHECK_TYPED_ARRAY_CAST
3930 :
3931 :
3932 0 : void v8::DataView::CheckCast(Value* that) {
3933 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3934 : Utils::ApiCheck(obj->IsJSDataView(),
3935 : "v8::DataView::Cast()",
3936 : "Could not convert to DataView");
3937 0 : }
3938 :
3939 :
3940 0 : void v8::SharedArrayBuffer::CheckCast(Value* that) {
3941 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3942 : Utils::ApiCheck(
3943 0 : obj->IsJSArrayBuffer() && i::JSArrayBuffer::cast(*obj)->is_shared(),
3944 : "v8::SharedArrayBuffer::Cast()",
3945 : "Could not convert to SharedArrayBuffer");
3946 0 : }
3947 :
3948 :
3949 0 : void v8::Date::CheckCast(v8::Value* that) {
3950 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3951 : i::Isolate* isolate = NULL;
3952 0 : if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
3953 0 : Utils::ApiCheck(isolate != NULL &&
3954 0 : obj->HasSpecificClassOf(isolate->heap()->Date_string()),
3955 : "v8::Date::Cast()",
3956 : "Could not convert to date");
3957 0 : }
3958 :
3959 :
3960 0 : void v8::StringObject::CheckCast(v8::Value* that) {
3961 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3962 : i::Isolate* isolate = NULL;
3963 0 : if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
3964 0 : Utils::ApiCheck(isolate != NULL &&
3965 0 : obj->HasSpecificClassOf(isolate->heap()->String_string()),
3966 : "v8::StringObject::Cast()",
3967 : "Could not convert to StringObject");
3968 0 : }
3969 :
3970 :
3971 0 : void v8::SymbolObject::CheckCast(v8::Value* that) {
3972 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3973 : i::Isolate* isolate = NULL;
3974 0 : if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
3975 0 : Utils::ApiCheck(isolate != NULL &&
3976 0 : obj->HasSpecificClassOf(isolate->heap()->Symbol_string()),
3977 : "v8::SymbolObject::Cast()",
3978 : "Could not convert to SymbolObject");
3979 0 : }
3980 :
3981 :
3982 0 : void v8::NumberObject::CheckCast(v8::Value* that) {
3983 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3984 : i::Isolate* isolate = NULL;
3985 0 : if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
3986 0 : Utils::ApiCheck(isolate != NULL &&
3987 0 : obj->HasSpecificClassOf(isolate->heap()->Number_string()),
3988 : "v8::NumberObject::Cast()",
3989 : "Could not convert to NumberObject");
3990 0 : }
3991 :
3992 :
3993 0 : void v8::BooleanObject::CheckCast(v8::Value* that) {
3994 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3995 : i::Isolate* isolate = NULL;
3996 0 : if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
3997 0 : Utils::ApiCheck(isolate != NULL &&
3998 0 : obj->HasSpecificClassOf(isolate->heap()->Boolean_string()),
3999 : "v8::BooleanObject::Cast()",
4000 : "Could not convert to BooleanObject");
4001 0 : }
4002 :
4003 :
4004 0 : void v8::RegExp::CheckCast(v8::Value* that) {
4005 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
4006 : Utils::ApiCheck(obj->IsJSRegExp(),
4007 : "v8::RegExp::Cast()",
4008 : "Could not convert to regular expression");
4009 0 : }
4010 :
4011 :
4012 15543 : Maybe<bool> Value::BooleanValue(Local<Context> context) const {
4013 31086 : return Just(Utils::OpenHandle(this)->BooleanValue());
4014 : }
4015 :
4016 :
4017 0 : bool Value::BooleanValue() const {
4018 0 : return Utils::OpenHandle(this)->BooleanValue();
4019 : }
4020 :
4021 :
4022 10174 : Maybe<double> Value::NumberValue(Local<Context> context) const {
4023 : auto obj = Utils::OpenHandle(this);
4024 10174 : if (obj->IsNumber()) return Just(obj->Number());
4025 904 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, NumberValue, double);
4026 : i::Handle<i::Object> num;
4027 452 : has_pending_exception = !i::Object::ToNumber(obj).ToHandle(&num);
4028 226 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(double);
4029 : return Just(num->Number());
4030 : }
4031 :
4032 :
4033 0 : double Value::NumberValue() const {
4034 : auto obj = Utils::OpenHandle(this);
4035 0 : if (obj->IsNumber()) return obj->Number();
4036 : return NumberValue(ContextFromHeapObject(obj))
4037 0 : .FromMaybe(std::numeric_limits<double>::quiet_NaN());
4038 : }
4039 :
4040 :
4041 6496 : Maybe<int64_t> Value::IntegerValue(Local<Context> context) const {
4042 : auto obj = Utils::OpenHandle(this);
4043 6496 : if (obj->IsNumber()) {
4044 : return Just(NumberToInt64(*obj));
4045 : }
4046 1436 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, IntegerValue, int64_t);
4047 : i::Handle<i::Object> num;
4048 359 : has_pending_exception = !i::Object::ToInteger(isolate, obj).ToHandle(&num);
4049 359 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(int64_t);
4050 : return Just(NumberToInt64(*num));
4051 : }
4052 :
4053 :
4054 0 : int64_t Value::IntegerValue() const {
4055 : auto obj = Utils::OpenHandle(this);
4056 0 : if (obj->IsNumber()) {
4057 0 : if (obj->IsSmi()) {
4058 0 : return i::Smi::cast(*obj)->value();
4059 : } else {
4060 0 : return static_cast<int64_t>(obj->Number());
4061 : }
4062 : }
4063 0 : return IntegerValue(ContextFromHeapObject(obj)).FromMaybe(0);
4064 : }
4065 :
4066 :
4067 659679 : Maybe<int32_t> Value::Int32Value(Local<Context> context) const {
4068 : auto obj = Utils::OpenHandle(this);
4069 1318265 : if (obj->IsNumber()) return Just(NumberToInt32(*obj));
4070 4372 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, Int32Value, int32_t);
4071 : i::Handle<i::Object> num;
4072 1093 : has_pending_exception = !i::Object::ToInt32(isolate, obj).ToHandle(&num);
4073 1093 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(int32_t);
4074 : return Just(num->IsSmi() ? i::Smi::cast(*num)->value()
4075 1011 : : static_cast<int32_t>(num->Number()));
4076 : }
4077 :
4078 :
4079 0 : int32_t Value::Int32Value() const {
4080 : auto obj = Utils::OpenHandle(this);
4081 0 : if (obj->IsNumber()) return NumberToInt32(*obj);
4082 0 : return Int32Value(ContextFromHeapObject(obj)).FromMaybe(0);
4083 : }
4084 :
4085 :
4086 249 : Maybe<uint32_t> Value::Uint32Value(Local<Context> context) const {
4087 : auto obj = Utils::OpenHandle(this);
4088 491 : if (obj->IsNumber()) return Just(NumberToUint32(*obj));
4089 28 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, Uint32Value, uint32_t);
4090 : i::Handle<i::Object> num;
4091 14 : has_pending_exception = !i::Object::ToUint32(isolate, obj).ToHandle(&num);
4092 7 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(uint32_t);
4093 : return Just(num->IsSmi() ? static_cast<uint32_t>(i::Smi::cast(*num)->value())
4094 0 : : static_cast<uint32_t>(num->Number()));
4095 : }
4096 :
4097 :
4098 0 : uint32_t Value::Uint32Value() const {
4099 : auto obj = Utils::OpenHandle(this);
4100 0 : if (obj->IsNumber()) return NumberToUint32(*obj);
4101 0 : return Uint32Value(ContextFromHeapObject(obj)).FromMaybe(0);
4102 : }
4103 :
4104 :
4105 49 : MaybeLocal<Uint32> Value::ToArrayIndex(Local<Context> context) const {
4106 : auto self = Utils::OpenHandle(this);
4107 49 : if (self->IsSmi()) {
4108 14 : if (i::Smi::cast(*self)->value() >= 0) return Utils::Uint32ToLocal(self);
4109 7 : return Local<Uint32>();
4110 : }
4111 140 : PREPARE_FOR_EXECUTION(context, Object, ToArrayIndex, Uint32);
4112 : i::Handle<i::Object> string_obj;
4113 : has_pending_exception =
4114 70 : !i::Object::ToString(isolate, self).ToHandle(&string_obj);
4115 35 : RETURN_ON_FAILED_EXECUTION(Uint32);
4116 : i::Handle<i::String> str = i::Handle<i::String>::cast(string_obj);
4117 : uint32_t index;
4118 35 : if (str->AsArrayIndex(&index)) {
4119 : i::Handle<i::Object> value;
4120 14 : if (index <= static_cast<uint32_t>(i::Smi::kMaxValue)) {
4121 : value = i::Handle<i::Object>(i::Smi::FromInt(index), isolate);
4122 : } else {
4123 7 : value = isolate->factory()->NewNumber(index);
4124 : }
4125 14 : RETURN_ESCAPED(Utils::Uint32ToLocal(value));
4126 : }
4127 21 : return Local<Uint32>();
4128 : }
4129 :
4130 :
4131 0 : Local<Uint32> Value::ToArrayIndex() const {
4132 : auto self = Utils::OpenHandle(this);
4133 0 : if (self->IsSmi()) {
4134 0 : if (i::Smi::cast(*self)->value() >= 0) return Utils::Uint32ToLocal(self);
4135 0 : return Local<Uint32>();
4136 : }
4137 0 : auto context = ContextFromHeapObject(self);
4138 0 : RETURN_TO_LOCAL_UNCHECKED(ToArrayIndex(context), Uint32);
4139 : }
4140 :
4141 :
4142 192426 : Maybe<bool> Value::Equals(Local<Context> context, Local<Value> that) const {
4143 192426 : auto self = Utils::OpenHandle(this);
4144 192426 : auto other = Utils::OpenHandle(*that);
4145 192426 : return i::Object::Equals(self, other);
4146 : }
4147 :
4148 :
4149 0 : bool Value::Equals(Local<Value> that) const {
4150 0 : auto self = Utils::OpenHandle(this);
4151 0 : auto other = Utils::OpenHandle(*that);
4152 0 : if (self->IsSmi() && other->IsSmi()) {
4153 0 : return self->Number() == other->Number();
4154 : }
4155 0 : if (self->IsJSObject() && other->IsJSObject()) {
4156 0 : return *self == *other;
4157 : }
4158 0 : auto heap_object = self->IsSmi() ? other : self;
4159 : auto context = ContextFromHeapObject(heap_object);
4160 : return Equals(context, that).FromMaybe(false);
4161 : }
4162 :
4163 :
4164 659 : bool Value::StrictEquals(Local<Value> that) const {
4165 : auto self = Utils::OpenHandle(this);
4166 : auto other = Utils::OpenHandle(*that);
4167 659 : return self->StrictEquals(*other);
4168 : }
4169 :
4170 :
4171 334 : bool Value::SameValue(Local<Value> that) const {
4172 : auto self = Utils::OpenHandle(this);
4173 : auto other = Utils::OpenHandle(*that);
4174 334 : return self->SameValue(*other);
4175 : }
4176 :
4177 58 : Local<String> Value::TypeOf(v8::Isolate* external_isolate) {
4178 58 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
4179 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
4180 116 : LOG_API(isolate, Value, TypeOf);
4181 116 : return Utils::ToLocal(i::Object::TypeOf(isolate, Utils::OpenHandle(this)));
4182 : }
4183 :
4184 84 : Maybe<bool> Value::InstanceOf(v8::Local<v8::Context> context,
4185 : v8::Local<v8::Object> object) {
4186 336 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Value, InstanceOf, bool);
4187 84 : auto left = Utils::OpenHandle(this);
4188 : auto right = Utils::OpenHandle(*object);
4189 : i::Handle<i::Object> result;
4190 : has_pending_exception =
4191 168 : !i::Object::InstanceOf(isolate, left, right).ToHandle(&result);
4192 84 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4193 : return Just(result->IsTrue(isolate));
4194 : }
4195 :
4196 420763 : Maybe<bool> v8::Object::Set(v8::Local<v8::Context> context,
4197 : v8::Local<Value> key, v8::Local<Value> value) {
4198 1683052 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, Set, bool);
4199 : auto self = Utils::OpenHandle(this);
4200 420763 : auto key_obj = Utils::OpenHandle(*key);
4201 420763 : auto value_obj = Utils::OpenHandle(*value);
4202 : has_pending_exception =
4203 : i::Runtime::SetObjectProperty(isolate, self, key_obj, value_obj,
4204 841526 : i::SLOPPY).is_null();
4205 420763 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4206 : return Just(true);
4207 : }
4208 :
4209 :
4210 0 : bool v8::Object::Set(v8::Local<Value> key, v8::Local<Value> value) {
4211 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4212 0 : return Set(context, key, value).FromMaybe(false);
4213 : }
4214 :
4215 :
4216 204572 : Maybe<bool> v8::Object::Set(v8::Local<v8::Context> context, uint32_t index,
4217 : v8::Local<Value> value) {
4218 818288 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, Set, bool);
4219 : auto self = Utils::OpenHandle(this);
4220 204572 : auto value_obj = Utils::OpenHandle(*value);
4221 : has_pending_exception = i::Object::SetElement(isolate, self, index, value_obj,
4222 409144 : i::SLOPPY).is_null();
4223 204572 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4224 : return Just(true);
4225 : }
4226 :
4227 :
4228 0 : bool v8::Object::Set(uint32_t index, v8::Local<Value> value) {
4229 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4230 0 : return Set(context, index, value).FromMaybe(false);
4231 : }
4232 :
4233 :
4234 279524 : Maybe<bool> v8::Object::CreateDataProperty(v8::Local<v8::Context> context,
4235 : v8::Local<Name> key,
4236 : v8::Local<Value> value) {
4237 1118096 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, CreateDataProperty, bool);
4238 : i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4239 279524 : i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
4240 279524 : i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
4241 :
4242 : i::LookupIterator it = i::LookupIterator::PropertyOrElement(
4243 279524 : isolate, self, key_obj, self, i::LookupIterator::OWN);
4244 : Maybe<bool> result =
4245 279524 : i::JSReceiver::CreateDataProperty(&it, value_obj, i::Object::DONT_THROW);
4246 279524 : has_pending_exception = result.IsNothing();
4247 279524 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4248 279518 : return result;
4249 : }
4250 :
4251 :
4252 13702 : Maybe<bool> v8::Object::CreateDataProperty(v8::Local<v8::Context> context,
4253 : uint32_t index,
4254 : v8::Local<Value> value) {
4255 54808 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, CreateDataProperty, bool);
4256 : i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4257 13702 : i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
4258 :
4259 : i::LookupIterator it(isolate, self, index, self, i::LookupIterator::OWN);
4260 : Maybe<bool> result =
4261 13702 : i::JSReceiver::CreateDataProperty(&it, value_obj, i::Object::DONT_THROW);
4262 13702 : has_pending_exception = result.IsNothing();
4263 13702 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4264 13702 : return result;
4265 : }
4266 :
4267 : struct v8::PropertyDescriptor::PrivateData {
4268 : PrivateData() : desc() {}
4269 : i::PropertyDescriptor desc;
4270 : };
4271 :
4272 242 : v8::PropertyDescriptor::PropertyDescriptor() : private_(new PrivateData()) {}
4273 :
4274 : // DataDescriptor
4275 96 : v8::PropertyDescriptor::PropertyDescriptor(v8::Local<v8::Value> value)
4276 192 : : private_(new PrivateData()) {
4277 : private_->desc.set_value(Utils::OpenHandle(*value, true));
4278 96 : }
4279 :
4280 : // DataDescriptor with writable field
4281 31 : v8::PropertyDescriptor::PropertyDescriptor(v8::Local<v8::Value> value,
4282 : bool writable)
4283 62 : : private_(new PrivateData()) {
4284 : private_->desc.set_value(Utils::OpenHandle(*value, true));
4285 : private_->desc.set_writable(writable);
4286 31 : }
4287 :
4288 : // AccessorDescriptor
4289 77 : v8::PropertyDescriptor::PropertyDescriptor(v8::Local<v8::Value> get,
4290 : v8::Local<v8::Value> set)
4291 154 : : private_(new PrivateData()) {
4292 : DCHECK(get.IsEmpty() || get->IsUndefined() || get->IsFunction());
4293 : DCHECK(set.IsEmpty() || set->IsUndefined() || set->IsFunction());
4294 : private_->desc.set_get(Utils::OpenHandle(*get, true));
4295 : private_->desc.set_set(Utils::OpenHandle(*set, true));
4296 77 : }
4297 :
4298 325 : v8::PropertyDescriptor::~PropertyDescriptor() { delete private_; }
4299 :
4300 38 : v8::Local<Value> v8::PropertyDescriptor::value() const {
4301 : DCHECK(private_->desc.has_value());
4302 38 : return Utils::ToLocal(private_->desc.value());
4303 : }
4304 :
4305 26 : v8::Local<Value> v8::PropertyDescriptor::get() const {
4306 : DCHECK(private_->desc.has_get());
4307 26 : return Utils::ToLocal(private_->desc.get());
4308 : }
4309 :
4310 26 : v8::Local<Value> v8::PropertyDescriptor::set() const {
4311 : DCHECK(private_->desc.has_set());
4312 26 : return Utils::ToLocal(private_->desc.set());
4313 : }
4314 :
4315 82 : bool v8::PropertyDescriptor::has_value() const {
4316 164 : return private_->desc.has_value();
4317 : }
4318 82 : bool v8::PropertyDescriptor::has_get() const {
4319 164 : return private_->desc.has_get();
4320 : }
4321 82 : bool v8::PropertyDescriptor::has_set() const {
4322 164 : return private_->desc.has_set();
4323 : }
4324 :
4325 19 : bool v8::PropertyDescriptor::writable() const {
4326 : DCHECK(private_->desc.has_writable());
4327 38 : return private_->desc.writable();
4328 : }
4329 :
4330 82 : bool v8::PropertyDescriptor::has_writable() const {
4331 164 : return private_->desc.has_writable();
4332 : }
4333 :
4334 44 : void v8::PropertyDescriptor::set_enumerable(bool enumerable) {
4335 44 : private_->desc.set_enumerable(enumerable);
4336 44 : }
4337 :
4338 20 : bool v8::PropertyDescriptor::enumerable() const {
4339 : DCHECK(private_->desc.has_enumerable());
4340 40 : return private_->desc.enumerable();
4341 : }
4342 :
4343 75 : bool v8::PropertyDescriptor::has_enumerable() const {
4344 150 : return private_->desc.has_enumerable();
4345 : }
4346 :
4347 45 : void v8::PropertyDescriptor::set_configurable(bool configurable) {
4348 45 : private_->desc.set_configurable(configurable);
4349 45 : }
4350 :
4351 38 : bool v8::PropertyDescriptor::configurable() const {
4352 : DCHECK(private_->desc.has_configurable());
4353 76 : return private_->desc.configurable();
4354 : }
4355 :
4356 75 : bool v8::PropertyDescriptor::has_configurable() const {
4357 150 : return private_->desc.has_configurable();
4358 : }
4359 :
4360 4734 : Maybe<bool> v8::Object::DefineOwnProperty(v8::Local<v8::Context> context,
4361 : v8::Local<Name> key,
4362 : v8::Local<Value> value,
4363 : v8::PropertyAttribute attributes) {
4364 18936 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, DefineOwnProperty, bool);
4365 4734 : i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4366 : i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
4367 : i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
4368 :
4369 : i::PropertyDescriptor desc;
4370 4734 : desc.set_writable(!(attributes & v8::ReadOnly));
4371 4734 : desc.set_enumerable(!(attributes & v8::DontEnum));
4372 4734 : desc.set_configurable(!(attributes & v8::DontDelete));
4373 : desc.set_value(value_obj);
4374 : Maybe<bool> success = i::JSReceiver::DefineOwnProperty(
4375 4734 : isolate, self, key_obj, &desc, i::Object::DONT_THROW);
4376 : // Even though we said DONT_THROW, there might be accessors that do throw.
4377 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4378 4734 : return success;
4379 : }
4380 :
4381 151 : Maybe<bool> v8::Object::DefineProperty(v8::Local<v8::Context> context,
4382 : v8::Local<Name> key,
4383 : PropertyDescriptor& descriptor) {
4384 604 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, DefineProperty, bool);
4385 151 : i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4386 : i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
4387 :
4388 : Maybe<bool> success = i::JSReceiver::DefineOwnProperty(
4389 : isolate, self, key_obj, &descriptor.get_private()->desc,
4390 302 : i::Object::DONT_THROW);
4391 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4392 151 : return success;
4393 : }
4394 :
4395 : MUST_USE_RESULT
4396 0 : static i::MaybeHandle<i::Object> DefineObjectProperty(
4397 : i::Handle<i::JSObject> js_object, i::Handle<i::Object> key,
4398 : i::Handle<i::Object> value, i::PropertyAttributes attrs) {
4399 : i::Isolate* isolate = js_object->GetIsolate();
4400 0 : bool success = false;
4401 : i::LookupIterator it = i::LookupIterator::PropertyOrElement(
4402 0 : isolate, js_object, key, &success, i::LookupIterator::OWN);
4403 0 : if (!success) return i::MaybeHandle<i::Object>();
4404 :
4405 : return i::JSObject::DefineOwnPropertyIgnoreAttributes(
4406 0 : &it, value, attrs, i::JSObject::FORCE_FIELD);
4407 : }
4408 :
4409 :
4410 0 : Maybe<bool> v8::Object::ForceSet(v8::Local<v8::Context> context,
4411 : v8::Local<Value> key, v8::Local<Value> value,
4412 : v8::PropertyAttribute attribs) {
4413 0 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, ForceSet, bool);
4414 0 : auto self = i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
4415 0 : auto key_obj = Utils::OpenHandle(*key);
4416 0 : auto value_obj = Utils::OpenHandle(*value);
4417 : has_pending_exception =
4418 : DefineObjectProperty(self, key_obj, value_obj,
4419 : static_cast<i::PropertyAttributes>(attribs))
4420 0 : .is_null();
4421 0 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4422 : return Just(true);
4423 : }
4424 :
4425 :
4426 0 : bool v8::Object::ForceSet(v8::Local<Value> key, v8::Local<Value> value,
4427 : v8::PropertyAttribute attribs) {
4428 0 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4429 0 : PREPARE_FOR_EXECUTION_GENERIC(isolate, Local<Context>(), Object, ForceSet,
4430 : false, i::HandleScope, false);
4431 : i::Handle<i::JSObject> self =
4432 0 : i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
4433 0 : i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
4434 0 : i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
4435 : has_pending_exception =
4436 : DefineObjectProperty(self, key_obj, value_obj,
4437 : static_cast<i::PropertyAttributes>(attribs))
4438 0 : .is_null();
4439 0 : EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, false);
4440 : return true;
4441 : }
4442 :
4443 :
4444 297200 : Maybe<bool> v8::Object::SetPrivate(Local<Context> context, Local<Private> key,
4445 : Local<Value> value) {
4446 1188800 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, SetPrivate, bool);
4447 : auto self = Utils::OpenHandle(this);
4448 : auto key_obj = Utils::OpenHandle(reinterpret_cast<Name*>(*key));
4449 : auto value_obj = Utils::OpenHandle(*value);
4450 297200 : if (self->IsJSProxy()) {
4451 : i::PropertyDescriptor desc;
4452 : desc.set_writable(true);
4453 : desc.set_enumerable(false);
4454 : desc.set_configurable(true);
4455 : desc.set_value(value_obj);
4456 : return i::JSProxy::SetPrivateProperty(
4457 : isolate, i::Handle<i::JSProxy>::cast(self),
4458 21 : i::Handle<i::Symbol>::cast(key_obj), &desc, i::Object::DONT_THROW);
4459 : }
4460 : auto js_object = i::Handle<i::JSObject>::cast(self);
4461 297179 : i::LookupIterator it(js_object, key_obj, js_object);
4462 : has_pending_exception = i::JSObject::DefineOwnPropertyIgnoreAttributes(
4463 : &it, value_obj, i::DONT_ENUM)
4464 594358 : .is_null();
4465 297179 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4466 : return Just(true);
4467 : }
4468 :
4469 :
4470 49487154 : MaybeLocal<Value> v8::Object::Get(Local<v8::Context> context,
4471 : Local<Value> key) {
4472 197948616 : PREPARE_FOR_EXECUTION(context, Object, Get, Value);
4473 : auto self = Utils::OpenHandle(this);
4474 49487154 : auto key_obj = Utils::OpenHandle(*key);
4475 : i::Handle<i::Object> result;
4476 : has_pending_exception =
4477 98974308 : !i::Runtime::GetObjectProperty(isolate, self, key_obj).ToHandle(&result);
4478 49487202 : RETURN_ON_FAILED_EXECUTION(Value);
4479 49487106 : RETURN_ESCAPED(Utils::ToLocal(result));
4480 : }
4481 :
4482 :
4483 0 : Local<Value> v8::Object::Get(v8::Local<Value> key) {
4484 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4485 0 : RETURN_TO_LOCAL_UNCHECKED(Get(context, key), Value);
4486 : }
4487 :
4488 :
4489 71321200 : MaybeLocal<Value> v8::Object::Get(Local<Context> context, uint32_t index) {
4490 285284800 : PREPARE_FOR_EXECUTION(context, Object, Get, Value);
4491 71321200 : auto self = Utils::OpenHandle(this);
4492 : i::Handle<i::Object> result;
4493 : has_pending_exception =
4494 142642400 : !i::JSReceiver::GetElement(isolate, self, index).ToHandle(&result);
4495 71321215 : RETURN_ON_FAILED_EXECUTION(Value);
4496 71321185 : RETURN_ESCAPED(Utils::ToLocal(result));
4497 : }
4498 :
4499 :
4500 0 : Local<Value> v8::Object::Get(uint32_t index) {
4501 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4502 0 : RETURN_TO_LOCAL_UNCHECKED(Get(context, index), Value);
4503 : }
4504 :
4505 :
4506 3781944 : MaybeLocal<Value> v8::Object::GetPrivate(Local<Context> context,
4507 : Local<Private> key) {
4508 3781944 : return Get(context, Local<Value>(reinterpret_cast<Value*>(*key)));
4509 : }
4510 :
4511 :
4512 68 : Maybe<PropertyAttribute> v8::Object::GetPropertyAttributes(
4513 : Local<Context> context, Local<Value> key) {
4514 272 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, GetPropertyAttributes,
4515 : PropertyAttribute);
4516 68 : auto self = Utils::OpenHandle(this);
4517 : auto key_obj = Utils::OpenHandle(*key);
4518 68 : if (!key_obj->IsName()) {
4519 : has_pending_exception =
4520 28 : !i::Object::ToString(isolate, key_obj).ToHandle(&key_obj);
4521 14 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(PropertyAttribute);
4522 : }
4523 61 : auto key_name = i::Handle<i::Name>::cast(key_obj);
4524 61 : auto result = i::JSReceiver::GetPropertyAttributes(self, key_name);
4525 61 : has_pending_exception = result.IsNothing();
4526 61 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(PropertyAttribute);
4527 61 : if (result.FromJust() == i::ABSENT) {
4528 : return Just(static_cast<PropertyAttribute>(i::NONE));
4529 : }
4530 : return Just(static_cast<PropertyAttribute>(result.FromJust()));
4531 : }
4532 :
4533 :
4534 0 : PropertyAttribute v8::Object::GetPropertyAttributes(v8::Local<Value> key) {
4535 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4536 : return GetPropertyAttributes(context, key)
4537 0 : .FromMaybe(static_cast<PropertyAttribute>(i::NONE));
4538 : }
4539 :
4540 :
4541 6900 : MaybeLocal<Value> v8::Object::GetOwnPropertyDescriptor(Local<Context> context,
4542 : Local<String> key) {
4543 27600 : PREPARE_FOR_EXECUTION(context, Object, GetOwnPropertyDescriptor, Value);
4544 6900 : i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
4545 : i::Handle<i::String> key_name = Utils::OpenHandle(*key);
4546 :
4547 : i::PropertyDescriptor desc;
4548 : Maybe<bool> found =
4549 6900 : i::JSReceiver::GetOwnPropertyDescriptor(isolate, obj, key_name, &desc);
4550 6900 : has_pending_exception = found.IsNothing();
4551 6900 : RETURN_ON_FAILED_EXECUTION(Value);
4552 6900 : if (!found.FromJust()) {
4553 6 : return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
4554 : }
4555 13788 : RETURN_ESCAPED(Utils::ToLocal(desc.ToObject(isolate)));
4556 : }
4557 :
4558 :
4559 0 : Local<Value> v8::Object::GetOwnPropertyDescriptor(Local<String> key) {
4560 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4561 0 : RETURN_TO_LOCAL_UNCHECKED(GetOwnPropertyDescriptor(context, key), Value);
4562 : }
4563 :
4564 :
4565 7571 : Local<Value> v8::Object::GetPrototype() {
4566 : auto isolate = Utils::OpenHandle(this)->GetIsolate();
4567 7571 : auto self = Utils::OpenHandle(this);
4568 7571 : i::PrototypeIterator iter(isolate, self);
4569 7571 : return Utils::ToLocal(i::PrototypeIterator::GetCurrent(iter));
4570 : }
4571 :
4572 :
4573 3579514 : Maybe<bool> v8::Object::SetPrototype(Local<Context> context,
4574 : Local<Value> value) {
4575 14318056 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, SetPrototype, bool);
4576 3579514 : auto self = Utils::OpenHandle(this);
4577 3579514 : auto value_obj = Utils::OpenHandle(*value);
4578 : // We do not allow exceptions thrown while setting the prototype
4579 : // to propagate outside.
4580 7159028 : TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
4581 : auto result = i::JSReceiver::SetPrototype(self, value_obj, false,
4582 3579514 : i::Object::THROW_ON_ERROR);
4583 3579514 : has_pending_exception = result.IsNothing();
4584 3579514 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4585 : return Just(true);
4586 : }
4587 :
4588 :
4589 0 : bool v8::Object::SetPrototype(Local<Value> value) {
4590 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4591 0 : return SetPrototype(context, value).FromMaybe(false);
4592 : }
4593 :
4594 63 : Local<Object> v8::Object::FindInstanceInPrototypeChain(
4595 : v8::Local<FunctionTemplate> tmpl) {
4596 : auto self = Utils::OpenHandle(this);
4597 : auto isolate = self->GetIsolate();
4598 : i::PrototypeIterator iter(isolate, *self, i::kStartAtReceiver);
4599 : auto tmpl_info = *Utils::OpenHandle(*tmpl);
4600 189 : while (!tmpl_info->IsTemplateFor(iter.GetCurrent<i::JSObject>())) {
4601 84 : iter.Advance();
4602 84 : if (iter.IsAtEnd()) return Local<Object>();
4603 126 : if (!iter.GetCurrent()->IsJSObject()) return Local<Object>();
4604 : }
4605 : // IsTemplateFor() ensures that iter.GetCurrent() can't be a Proxy here.
4606 42 : return Utils::ToLocal(i::handle(iter.GetCurrent<i::JSObject>(), isolate));
4607 : }
4608 :
4609 9025367 : MaybeLocal<Array> v8::Object::GetPropertyNames(Local<Context> context) {
4610 : return GetPropertyNames(
4611 : context, v8::KeyCollectionMode::kIncludePrototypes,
4612 : static_cast<v8::PropertyFilter>(ONLY_ENUMERABLE | SKIP_SYMBOLS),
4613 9025367 : v8::IndexFilter::kIncludeIndices);
4614 : }
4615 :
4616 9026129 : MaybeLocal<Array> v8::Object::GetPropertyNames(Local<Context> context,
4617 : KeyCollectionMode mode,
4618 : PropertyFilter property_filter,
4619 : IndexFilter index_filter) {
4620 36104516 : PREPARE_FOR_EXECUTION(context, Object, GetPropertyNames, Array);
4621 9026129 : auto self = Utils::OpenHandle(this);
4622 : i::Handle<i::FixedArray> value;
4623 : i::KeyAccumulator accumulator(
4624 : isolate, static_cast<i::KeyCollectionMode>(mode),
4625 9026129 : static_cast<i::PropertyFilter>(property_filter));
4626 9026129 : accumulator.set_skip_indices(index_filter == IndexFilter::kSkipIndices);
4627 9026129 : has_pending_exception = accumulator.CollectKeys(self, self).IsNothing();
4628 9026129 : RETURN_ON_FAILED_EXECUTION(Array);
4629 9026129 : value = accumulator.GetKeys(i::GetKeysConversion::kKeepNumbers);
4630 : DCHECK(self->map()->EnumLength() == i::kInvalidEnumCacheSentinel ||
4631 : self->map()->EnumLength() == 0 ||
4632 : self->map()->instance_descriptors()->GetEnumCache() != *value);
4633 : auto result = isolate->factory()->NewJSArrayWithElements(value);
4634 9026129 : RETURN_ESCAPED(Utils::ToLocal(result));
4635 : }
4636 :
4637 :
4638 0 : Local<Array> v8::Object::GetPropertyNames() {
4639 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4640 0 : RETURN_TO_LOCAL_UNCHECKED(GetPropertyNames(context), Array);
4641 : }
4642 :
4643 685 : MaybeLocal<Array> v8::Object::GetOwnPropertyNames(Local<Context> context) {
4644 : return GetOwnPropertyNames(
4645 685 : context, static_cast<v8::PropertyFilter>(ONLY_ENUMERABLE | SKIP_SYMBOLS));
4646 : }
4647 :
4648 0 : Local<Array> v8::Object::GetOwnPropertyNames() {
4649 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4650 0 : RETURN_TO_LOCAL_UNCHECKED(GetOwnPropertyNames(context), Array);
4651 : }
4652 :
4653 21 : MaybeLocal<Array> v8::Object::GetOwnPropertyNames(Local<Context> context,
4654 : PropertyFilter filter) {
4655 : return GetPropertyNames(context, KeyCollectionMode::kOwnOnly, filter,
4656 706 : v8::IndexFilter::kIncludeIndices);
4657 : }
4658 :
4659 495 : MaybeLocal<String> v8::Object::ObjectProtoToString(Local<Context> context) {
4660 1980 : PREPARE_FOR_EXECUTION(context, Object, ObjectProtoToString, String);
4661 : auto self = Utils::OpenHandle(this);
4662 : Local<Value> result;
4663 : has_pending_exception =
4664 : !ToLocal<Value>(i::Execution::Call(isolate, isolate->object_to_string(),
4665 : self, 0, nullptr),
4666 1485 : &result);
4667 507 : RETURN_ON_FAILED_EXECUTION(String);
4668 483 : RETURN_ESCAPED(Local<String>::Cast(result));
4669 : }
4670 :
4671 :
4672 0 : Local<String> v8::Object::ObjectProtoToString() {
4673 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4674 0 : RETURN_TO_LOCAL_UNCHECKED(ObjectProtoToString(context), String);
4675 : }
4676 :
4677 :
4678 7504192 : Local<String> v8::Object::GetConstructorName() {
4679 7504192 : auto self = Utils::OpenHandle(this);
4680 7504192 : i::Handle<i::String> name = i::JSReceiver::GetConstructorName(self);
4681 7504192 : return Utils::ToLocal(name);
4682 : }
4683 :
4684 7 : Maybe<bool> v8::Object::SetIntegrityLevel(Local<Context> context,
4685 : IntegrityLevel level) {
4686 28 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, SetIntegrityLevel, bool);
4687 7 : auto self = Utils::OpenHandle(this);
4688 : i::JSReceiver::IntegrityLevel i_level =
4689 7 : level == IntegrityLevel::kFrozen ? i::FROZEN : i::SEALED;
4690 : Maybe<bool> result = i::JSReceiver::SetIntegrityLevel(
4691 7 : self, i_level, i::Object::THROW_ON_ERROR);
4692 7 : has_pending_exception = result.IsNothing();
4693 7 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4694 6 : return result;
4695 : }
4696 :
4697 7072 : Maybe<bool> v8::Object::Delete(Local<Context> context, Local<Value> key) {
4698 28288 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, Delete, bool);
4699 7072 : auto self = Utils::OpenHandle(this);
4700 7072 : auto key_obj = Utils::OpenHandle(*key);
4701 : Maybe<bool> result =
4702 7072 : i::Runtime::DeleteObjectProperty(isolate, self, key_obj, i::SLOPPY);
4703 7072 : has_pending_exception = result.IsNothing();
4704 7072 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4705 7072 : return result;
4706 : }
4707 :
4708 :
4709 0 : bool v8::Object::Delete(v8::Local<Value> key) {
4710 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4711 0 : return Delete(context, key).FromMaybe(false);
4712 : }
4713 :
4714 :
4715 49 : Maybe<bool> v8::Object::DeletePrivate(Local<Context> context,
4716 : Local<Private> key) {
4717 49 : return Delete(context, Local<Value>(reinterpret_cast<Value*>(*key)));
4718 : }
4719 :
4720 :
4721 201621 : Maybe<bool> v8::Object::Has(Local<Context> context, Local<Value> key) {
4722 806484 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, Get, bool);
4723 201621 : auto self = Utils::OpenHandle(this);
4724 : auto key_obj = Utils::OpenHandle(*key);
4725 : Maybe<bool> maybe = Nothing<bool>();
4726 : // Check if the given key is an array index.
4727 201621 : uint32_t index = 0;
4728 201621 : if (key_obj->ToArrayIndex(&index)) {
4729 0 : maybe = i::JSReceiver::HasElement(self, index);
4730 : } else {
4731 : // Convert the key to a name - possibly by calling back into JavaScript.
4732 : i::Handle<i::Name> name;
4733 403242 : if (i::Object::ToName(isolate, key_obj).ToHandle(&name)) {
4734 201621 : maybe = i::JSReceiver::HasProperty(self, name);
4735 : }
4736 : }
4737 201621 : has_pending_exception = maybe.IsNothing();
4738 201621 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4739 201621 : return maybe;
4740 : }
4741 :
4742 :
4743 0 : bool v8::Object::Has(v8::Local<Value> key) {
4744 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4745 0 : return Has(context, key).FromMaybe(false);
4746 : }
4747 :
4748 :
4749 10188134 : Maybe<bool> v8::Object::HasPrivate(Local<Context> context, Local<Private> key) {
4750 10188134 : return HasOwnProperty(context, Local<Name>(reinterpret_cast<Name*>(*key)));
4751 : }
4752 :
4753 :
4754 12 : Maybe<bool> v8::Object::Delete(Local<Context> context, uint32_t index) {
4755 48 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, DeleteProperty, bool);
4756 12 : auto self = Utils::OpenHandle(this);
4757 12 : Maybe<bool> result = i::JSReceiver::DeleteElement(self, index);
4758 12 : has_pending_exception = result.IsNothing();
4759 12 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4760 6 : return result;
4761 : }
4762 :
4763 :
4764 0 : bool v8::Object::Delete(uint32_t index) {
4765 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4766 0 : return Delete(context, index).FromMaybe(false);
4767 : }
4768 :
4769 :
4770 35 : Maybe<bool> v8::Object::Has(Local<Context> context, uint32_t index) {
4771 140 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, Get, bool);
4772 35 : auto self = Utils::OpenHandle(this);
4773 35 : auto maybe = i::JSReceiver::HasElement(self, index);
4774 35 : has_pending_exception = maybe.IsNothing();
4775 35 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4776 35 : return maybe;
4777 : }
4778 :
4779 :
4780 0 : bool v8::Object::Has(uint32_t index) {
4781 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4782 0 : return Has(context, index).FromMaybe(false);
4783 : }
4784 :
4785 : template <typename Getter, typename Setter, typename Data>
4786 7065 : static Maybe<bool> ObjectSetAccessor(Local<Context> context, Object* self,
4787 : Local<Name> name, Getter getter,
4788 : Setter setter, Data data,
4789 : AccessControl settings,
4790 : PropertyAttribute attributes,
4791 : bool is_special_data_property) {
4792 28260 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, SetAccessor, bool);
4793 7065 : if (!Utils::OpenHandle(self)->IsJSObject()) return Just(false);
4794 : i::Handle<i::JSObject> obj =
4795 : i::Handle<i::JSObject>::cast(Utils::OpenHandle(self));
4796 : v8::Local<AccessorSignature> signature;
4797 : auto info = MakeAccessorInfo(name, getter, setter, data, settings, attributes,
4798 7065 : signature, is_special_data_property, false);
4799 7065 : if (info.is_null()) return Nothing<bool>();
4800 7065 : bool fast = obj->HasFastProperties();
4801 : i::Handle<i::Object> result;
4802 : has_pending_exception =
4803 14130 : !i::JSObject::SetAccessor(obj, info).ToHandle(&result);
4804 7065 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4805 7059 : if (result->IsUndefined(obj->GetIsolate())) return Just(false);
4806 7030 : if (fast) {
4807 7016 : i::JSObject::MigrateSlowToFast(obj, 0, "APISetAccessor");
4808 : }
4809 : return Just(true);
4810 : }
4811 :
4812 :
4813 7058 : Maybe<bool> Object::SetAccessor(Local<Context> context, Local<Name> name,
4814 : AccessorNameGetterCallback getter,
4815 : AccessorNameSetterCallback setter,
4816 : MaybeLocal<Value> data, AccessControl settings,
4817 : PropertyAttribute attribute) {
4818 : return ObjectSetAccessor(context, this, name, getter, setter,
4819 : data.FromMaybe(Local<Value>()), settings, attribute,
4820 14116 : i::FLAG_disable_old_api_accessors);
4821 : }
4822 :
4823 :
4824 0 : bool Object::SetAccessor(Local<String> name, AccessorGetterCallback getter,
4825 : AccessorSetterCallback setter, v8::Local<Value> data,
4826 : AccessControl settings, PropertyAttribute attributes) {
4827 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4828 : return ObjectSetAccessor(context, this, name, getter, setter, data, settings,
4829 0 : attributes, i::FLAG_disable_old_api_accessors)
4830 0 : .FromMaybe(false);
4831 : }
4832 :
4833 :
4834 0 : bool Object::SetAccessor(Local<Name> name, AccessorNameGetterCallback getter,
4835 : AccessorNameSetterCallback setter,
4836 : v8::Local<Value> data, AccessControl settings,
4837 : PropertyAttribute attributes) {
4838 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4839 : return ObjectSetAccessor(context, this, name, getter, setter, data, settings,
4840 0 : attributes, i::FLAG_disable_old_api_accessors)
4841 0 : .FromMaybe(false);
4842 : }
4843 :
4844 :
4845 211590 : void Object::SetAccessorProperty(Local<Name> name, Local<Function> getter,
4846 : Local<Function> setter,
4847 : PropertyAttribute attribute,
4848 : AccessControl settings) {
4849 : // TODO(verwaest): Remove |settings|.
4850 : DCHECK_EQ(v8::DEFAULT, settings);
4851 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4852 : ENTER_V8(isolate);
4853 : i::HandleScope scope(isolate);
4854 : auto self = Utils::OpenHandle(this);
4855 423182 : if (!self->IsJSObject()) return;
4856 : i::Handle<i::Object> getter_i = v8::Utils::OpenHandle(*getter);
4857 : i::Handle<i::Object> setter_i = v8::Utils::OpenHandle(*setter, true);
4858 211591 : if (setter_i.is_null()) setter_i = isolate->factory()->null_value();
4859 : i::JSObject::DefineAccessor(i::Handle<i::JSObject>::cast(self),
4860 : v8::Utils::OpenHandle(*name), getter_i, setter_i,
4861 211591 : static_cast<i::PropertyAttributes>(attribute));
4862 : }
4863 :
4864 7 : Maybe<bool> Object::SetNativeDataProperty(v8::Local<v8::Context> context,
4865 : v8::Local<Name> name,
4866 : AccessorNameGetterCallback getter,
4867 : AccessorNameSetterCallback setter,
4868 : v8::Local<Value> data,
4869 : PropertyAttribute attributes) {
4870 : return ObjectSetAccessor(context, this, name, getter, setter, data, DEFAULT,
4871 7 : attributes, true);
4872 : }
4873 :
4874 10188272 : Maybe<bool> v8::Object::HasOwnProperty(Local<Context> context,
4875 : Local<Name> key) {
4876 40753088 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, HasOwnProperty, bool);
4877 10188272 : auto self = Utils::OpenHandle(this);
4878 10188272 : auto key_val = Utils::OpenHandle(*key);
4879 10188272 : auto result = i::JSReceiver::HasOwnProperty(self, key_val);
4880 10188272 : has_pending_exception = result.IsNothing();
4881 10188272 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4882 10188266 : return result;
4883 : }
4884 :
4885 30 : Maybe<bool> v8::Object::HasOwnProperty(Local<Context> context, uint32_t index) {
4886 120 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, HasOwnProperty, bool);
4887 30 : auto self = Utils::OpenHandle(this);
4888 30 : auto result = i::JSReceiver::HasOwnProperty(self, index);
4889 30 : has_pending_exception = result.IsNothing();
4890 30 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4891 30 : return result;
4892 : }
4893 :
4894 0 : bool v8::Object::HasOwnProperty(Local<String> key) {
4895 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4896 0 : return HasOwnProperty(context, key).FromMaybe(false);
4897 : }
4898 :
4899 :
4900 66900019 : Maybe<bool> v8::Object::HasRealNamedProperty(Local<Context> context,
4901 : Local<Name> key) {
4902 267600076 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, HasRealNamedProperty, bool);
4903 : auto self = Utils::OpenHandle(this);
4904 66900019 : if (!self->IsJSObject()) return Just(false);
4905 66900019 : auto key_val = Utils::OpenHandle(*key);
4906 : auto result = i::JSObject::HasRealNamedProperty(
4907 66900019 : i::Handle<i::JSObject>::cast(self), key_val);
4908 66900019 : has_pending_exception = result.IsNothing();
4909 66900019 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4910 66900013 : return result;
4911 : }
4912 :
4913 :
4914 0 : bool v8::Object::HasRealNamedProperty(Local<String> key) {
4915 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4916 0 : return HasRealNamedProperty(context, key).FromMaybe(false);
4917 : }
4918 :
4919 :
4920 20 : Maybe<bool> v8::Object::HasRealIndexedProperty(Local<Context> context,
4921 : uint32_t index) {
4922 80 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, HasRealIndexedProperty,
4923 : bool);
4924 : auto self = Utils::OpenHandle(this);
4925 20 : if (!self->IsJSObject()) return Just(false);
4926 : auto result = i::JSObject::HasRealElementProperty(
4927 20 : i::Handle<i::JSObject>::cast(self), index);
4928 20 : has_pending_exception = result.IsNothing();
4929 20 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4930 14 : return result;
4931 : }
4932 :
4933 :
4934 0 : bool v8::Object::HasRealIndexedProperty(uint32_t index) {
4935 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4936 0 : return HasRealIndexedProperty(context, index).FromMaybe(false);
4937 : }
4938 :
4939 :
4940 6 : Maybe<bool> v8::Object::HasRealNamedCallbackProperty(Local<Context> context,
4941 : Local<Name> key) {
4942 24 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, HasRealNamedCallbackProperty,
4943 : bool);
4944 : auto self = Utils::OpenHandle(this);
4945 6 : if (!self->IsJSObject()) return Just(false);
4946 6 : auto key_val = Utils::OpenHandle(*key);
4947 : auto result = i::JSObject::HasRealNamedCallbackProperty(
4948 6 : i::Handle<i::JSObject>::cast(self), key_val);
4949 6 : has_pending_exception = result.IsNothing();
4950 6 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4951 0 : return result;
4952 : }
4953 :
4954 :
4955 0 : bool v8::Object::HasRealNamedCallbackProperty(Local<String> key) {
4956 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4957 0 : return HasRealNamedCallbackProperty(context, key).FromMaybe(false);
4958 : }
4959 :
4960 :
4961 0 : bool v8::Object::HasNamedLookupInterceptor() {
4962 : auto self = Utils::OpenHandle(this);
4963 0 : return self->IsJSObject() &&
4964 0 : i::Handle<i::JSObject>::cast(self)->HasNamedInterceptor();
4965 : }
4966 :
4967 :
4968 0 : bool v8::Object::HasIndexedLookupInterceptor() {
4969 : auto self = Utils::OpenHandle(this);
4970 0 : return self->IsJSObject() &&
4971 0 : i::Handle<i::JSObject>::cast(self)->HasIndexedInterceptor();
4972 : }
4973 :
4974 :
4975 7 : MaybeLocal<Value> v8::Object::GetRealNamedPropertyInPrototypeChain(
4976 : Local<Context> context, Local<Name> key) {
4977 28 : PREPARE_FOR_EXECUTION(context, Object, GetRealNamedPropertyInPrototypeChain,
4978 : Value);
4979 : i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4980 7 : if (!self->IsJSObject()) return MaybeLocal<Value>();
4981 7 : i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
4982 7 : i::PrototypeIterator iter(isolate, self);
4983 7 : if (iter.IsAtEnd()) return MaybeLocal<Value>();
4984 : i::Handle<i::JSReceiver> proto =
4985 7 : i::PrototypeIterator::GetCurrent<i::JSReceiver>(iter);
4986 : i::LookupIterator it = i::LookupIterator::PropertyOrElement(
4987 : isolate, self, key_obj, proto,
4988 7 : i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
4989 : Local<Value> result;
4990 14 : has_pending_exception = !ToLocal<Value>(i::Object::GetProperty(&it), &result);
4991 14 : RETURN_ON_FAILED_EXECUTION(Value);
4992 0 : if (!it.IsFound()) return MaybeLocal<Value>();
4993 0 : RETURN_ESCAPED(result);
4994 : }
4995 :
4996 :
4997 0 : Local<Value> v8::Object::GetRealNamedPropertyInPrototypeChain(
4998 : Local<String> key) {
4999 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
5000 0 : RETURN_TO_LOCAL_UNCHECKED(GetRealNamedPropertyInPrototypeChain(context, key),
5001 : Value);
5002 : }
5003 :
5004 :
5005 : Maybe<PropertyAttribute>
5006 7 : v8::Object::GetRealNamedPropertyAttributesInPrototypeChain(
5007 : Local<Context> context, Local<Name> key) {
5008 28 : PREPARE_FOR_EXECUTION_PRIMITIVE(
5009 : context, Object, GetRealNamedPropertyAttributesInPrototypeChain,
5010 : PropertyAttribute);
5011 : i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
5012 7 : if (!self->IsJSObject()) return Nothing<PropertyAttribute>();
5013 7 : i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
5014 7 : i::PrototypeIterator iter(isolate, self);
5015 7 : if (iter.IsAtEnd()) return Nothing<PropertyAttribute>();
5016 : i::Handle<i::JSReceiver> proto =
5017 7 : i::PrototypeIterator::GetCurrent<i::JSReceiver>(iter);
5018 : i::LookupIterator it = i::LookupIterator::PropertyOrElement(
5019 : isolate, self, key_obj, proto,
5020 7 : i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
5021 : Maybe<i::PropertyAttributes> result =
5022 7 : i::JSReceiver::GetPropertyAttributes(&it);
5023 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(PropertyAttribute);
5024 7 : if (!it.IsFound()) return Nothing<PropertyAttribute>();
5025 7 : if (result.FromJust() == i::ABSENT) return Just(None);
5026 : return Just(static_cast<PropertyAttribute>(result.FromJust()));
5027 : }
5028 :
5029 :
5030 : Maybe<PropertyAttribute>
5031 0 : v8::Object::GetRealNamedPropertyAttributesInPrototypeChain(Local<String> key) {
5032 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
5033 0 : return GetRealNamedPropertyAttributesInPrototypeChain(context, key);
5034 : }
5035 :
5036 :
5037 80 : MaybeLocal<Value> v8::Object::GetRealNamedProperty(Local<Context> context,
5038 : Local<Name> key) {
5039 320 : PREPARE_FOR_EXECUTION(context, Object, GetRealNamedProperty, Value);
5040 : auto self = Utils::OpenHandle(this);
5041 80 : auto key_obj = Utils::OpenHandle(*key);
5042 : i::LookupIterator it = i::LookupIterator::PropertyOrElement(
5043 : isolate, self, key_obj, self,
5044 80 : i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
5045 : Local<Value> result;
5046 160 : has_pending_exception = !ToLocal<Value>(i::Object::GetProperty(&it), &result);
5047 101 : RETURN_ON_FAILED_EXECUTION(Value);
5048 59 : if (!it.IsFound()) return MaybeLocal<Value>();
5049 45 : RETURN_ESCAPED(result);
5050 : }
5051 :
5052 :
5053 0 : Local<Value> v8::Object::GetRealNamedProperty(Local<String> key) {
5054 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
5055 0 : RETURN_TO_LOCAL_UNCHECKED(GetRealNamedProperty(context, key), Value);
5056 : }
5057 :
5058 :
5059 21 : Maybe<PropertyAttribute> v8::Object::GetRealNamedPropertyAttributes(
5060 : Local<Context> context, Local<Name> key) {
5061 84 : PREPARE_FOR_EXECUTION_PRIMITIVE(
5062 : context, Object, GetRealNamedPropertyAttributes, PropertyAttribute);
5063 : auto self = Utils::OpenHandle(this);
5064 21 : auto key_obj = Utils::OpenHandle(*key);
5065 : i::LookupIterator it = i::LookupIterator::PropertyOrElement(
5066 : isolate, self, key_obj, self,
5067 21 : i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
5068 21 : auto result = i::JSReceiver::GetPropertyAttributes(&it);
5069 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(PropertyAttribute);
5070 21 : if (!it.IsFound()) return Nothing<PropertyAttribute>();
5071 21 : if (result.FromJust() == i::ABSENT) {
5072 : return Just(static_cast<PropertyAttribute>(i::NONE));
5073 : }
5074 : return Just<PropertyAttribute>(
5075 : static_cast<PropertyAttribute>(result.FromJust()));
5076 : }
5077 :
5078 :
5079 0 : Maybe<PropertyAttribute> v8::Object::GetRealNamedPropertyAttributes(
5080 : Local<String> key) {
5081 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
5082 0 : return GetRealNamedPropertyAttributes(context, key);
5083 : }
5084 :
5085 :
5086 19 : Local<v8::Object> v8::Object::Clone() {
5087 : auto self = i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
5088 : auto isolate = self->GetIsolate();
5089 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
5090 19 : auto result = isolate->factory()->CopyJSObject(self);
5091 19 : CHECK(!result.is_null());
5092 19 : return Utils::ToLocal(result);
5093 : }
5094 :
5095 :
5096 479 : Local<v8::Context> v8::Object::CreationContext() {
5097 : auto self = Utils::OpenHandle(this);
5098 479 : return Utils::ToLocal(self->GetCreationContext());
5099 : }
5100 :
5101 :
5102 124 : int v8::Object::GetIdentityHash() {
5103 : auto isolate = Utils::OpenHandle(this)->GetIsolate();
5104 : i::HandleScope scope(isolate);
5105 124 : auto self = Utils::OpenHandle(this);
5106 248 : return i::JSReceiver::GetOrCreateIdentityHash(isolate, self)->value();
5107 : }
5108 :
5109 :
5110 28 : bool v8::Object::IsCallable() {
5111 : auto self = Utils::OpenHandle(this);
5112 28 : return self->IsCallable();
5113 : }
5114 :
5115 7 : bool v8::Object::IsConstructor() {
5116 : auto self = Utils::OpenHandle(this);
5117 7 : return self->IsConstructor();
5118 : }
5119 :
5120 126 : MaybeLocal<Value> Object::CallAsFunction(Local<Context> context,
5121 : Local<Value> recv, int argc,
5122 : Local<Value> argv[]) {
5123 756 : PREPARE_FOR_EXECUTION_WITH_CONTEXT_IN_RUNTIME_CALL_STATS_SCOPE(
5124 : "v8", "V8.Execute", context, Object, CallAsFunction, MaybeLocal<Value>(),
5125 : InternalEscapableScope, true);
5126 : i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
5127 : auto self = Utils::OpenHandle(this);
5128 126 : auto recv_obj = Utils::OpenHandle(*recv);
5129 : STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Object**));
5130 : i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
5131 : Local<Value> result;
5132 : has_pending_exception = !ToLocal<Value>(
5133 252 : i::Execution::Call(isolate, self, recv_obj, argc, args), &result);
5134 140 : RETURN_ON_FAILED_EXECUTION(Value);
5135 238 : RETURN_ESCAPED(result);
5136 : }
5137 :
5138 :
5139 0 : Local<v8::Value> Object::CallAsFunction(v8::Local<v8::Value> recv, int argc,
5140 : v8::Local<v8::Value> argv[]) {
5141 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
5142 : Local<Value>* argv_cast = reinterpret_cast<Local<Value>*>(argv);
5143 0 : RETURN_TO_LOCAL_UNCHECKED(CallAsFunction(context, recv, argc, argv_cast),
5144 : Value);
5145 : }
5146 :
5147 :
5148 63 : MaybeLocal<Value> Object::CallAsConstructor(Local<Context> context, int argc,
5149 : Local<Value> argv[]) {
5150 378 : PREPARE_FOR_EXECUTION_WITH_CONTEXT_IN_RUNTIME_CALL_STATS_SCOPE(
5151 : "v8", "V8.Execute", context, Object, CallAsConstructor,
5152 : MaybeLocal<Value>(), InternalEscapableScope, true);
5153 : i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
5154 : auto self = Utils::OpenHandle(this);
5155 : STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Object**));
5156 : i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
5157 : Local<Value> result;
5158 : has_pending_exception = !ToLocal<Value>(
5159 126 : i::Execution::New(isolate, self, self, argc, args), &result);
5160 77 : RETURN_ON_FAILED_EXECUTION(Value);
5161 112 : RETURN_ESCAPED(result);
5162 : }
5163 :
5164 :
5165 0 : Local<v8::Value> Object::CallAsConstructor(int argc,
5166 : v8::Local<v8::Value> argv[]) {
5167 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
5168 : Local<Value>* argv_cast = reinterpret_cast<Local<Value>*>(argv);
5169 0 : RETURN_TO_LOCAL_UNCHECKED(CallAsConstructor(context, argc, argv_cast), Value);
5170 : }
5171 :
5172 42894 : MaybeLocal<Function> Function::New(Local<Context> context,
5173 : FunctionCallback callback, Local<Value> data,
5174 : int length, ConstructorBehavior behavior) {
5175 42894 : i::Isolate* isolate = Utils::OpenHandle(*context)->GetIsolate();
5176 85788 : LOG_API(isolate, Function, New);
5177 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
5178 : auto templ = FunctionTemplateNew(isolate, callback, data, Local<Signature>(),
5179 42894 : length, true);
5180 42894 : if (behavior == ConstructorBehavior::kThrow) templ->RemovePrototype();
5181 85788 : return templ->GetFunction(context);
5182 : }
5183 :
5184 :
5185 0 : Local<Function> Function::New(Isolate* v8_isolate, FunctionCallback callback,
5186 : Local<Value> data, int length) {
5187 : return Function::New(v8_isolate->GetCurrentContext(), callback, data, length,
5188 0 : ConstructorBehavior::kAllow)
5189 0 : .FromMaybe(Local<Function>());
5190 : }
5191 :
5192 :
5193 0 : Local<v8::Object> Function::NewInstance() const {
5194 0 : return NewInstance(Isolate::GetCurrent()->GetCurrentContext(), 0, NULL)
5195 0 : .FromMaybe(Local<Object>());
5196 : }
5197 :
5198 :
5199 920 : MaybeLocal<Object> Function::NewInstance(Local<Context> context, int argc,
5200 : v8::Local<v8::Value> argv[]) const {
5201 5520 : PREPARE_FOR_EXECUTION_WITH_CONTEXT_IN_RUNTIME_CALL_STATS_SCOPE(
5202 : "v8", "V8.Execute", context, Function, NewInstance, MaybeLocal<Object>(),
5203 : InternalEscapableScope, true);
5204 : i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
5205 : auto self = Utils::OpenHandle(this);
5206 : STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Object**));
5207 : i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
5208 : Local<Object> result;
5209 : has_pending_exception = !ToLocal<Object>(
5210 1840 : i::Execution::New(isolate, self, self, argc, args), &result);
5211 927 : RETURN_ON_FAILED_EXECUTION(Object);
5212 1833 : RETURN_ESCAPED(result);
5213 : }
5214 :
5215 :
5216 0 : Local<v8::Object> Function::NewInstance(int argc,
5217 : v8::Local<v8::Value> argv[]) const {
5218 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
5219 0 : RETURN_TO_LOCAL_UNCHECKED(NewInstance(context, argc, argv), Object);
5220 : }
5221 :
5222 :
5223 29701234 : MaybeLocal<v8::Value> Function::Call(Local<Context> context,
5224 : v8::Local<v8::Value> recv, int argc,
5225 : v8::Local<v8::Value> argv[]) {
5226 178207404 : PREPARE_FOR_EXECUTION_WITH_CONTEXT_IN_RUNTIME_CALL_STATS_SCOPE(
5227 : "v8", "V8.Execute", context, Function, Call, MaybeLocal<Value>(),
5228 : InternalEscapableScope, true);
5229 : i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
5230 : auto self = Utils::OpenHandle(this);
5231 29701234 : i::Handle<i::Object> recv_obj = Utils::OpenHandle(*recv);
5232 : STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Object**));
5233 : i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
5234 : Local<Value> result;
5235 : has_pending_exception = !ToLocal<Value>(
5236 59402468 : i::Execution::Call(isolate, self, recv_obj, argc, args), &result);
5237 29703127 : RETURN_ON_FAILED_EXECUTION(Value);
5238 59400575 : RETURN_ESCAPED(result);
5239 : }
5240 :
5241 :
5242 0 : Local<v8::Value> Function::Call(v8::Local<v8::Value> recv, int argc,
5243 : v8::Local<v8::Value> argv[]) {
5244 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
5245 0 : RETURN_TO_LOCAL_UNCHECKED(Call(context, recv, argc, argv), Value);
5246 : }
5247 :
5248 :
5249 38073 : void Function::SetName(v8::Local<v8::String> name) {
5250 : auto self = Utils::OpenHandle(this);
5251 76146 : if (!self->IsJSFunction()) return;
5252 : auto func = i::Handle<i::JSFunction>::cast(self);
5253 38073 : func->shared()->set_name(*Utils::OpenHandle(*name));
5254 : }
5255 :
5256 :
5257 44 : Local<Value> Function::GetName() const {
5258 : auto self = Utils::OpenHandle(this);
5259 : i::Isolate* isolate = self->GetIsolate();
5260 44 : if (self->IsJSBoundFunction()) {
5261 0 : auto func = i::Handle<i::JSBoundFunction>::cast(self);
5262 : i::Handle<i::Object> name;
5263 0 : ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, name,
5264 : i::JSBoundFunction::GetName(isolate, func),
5265 : Local<Value>());
5266 : return Utils::ToLocal(name);
5267 : }
5268 44 : if (self->IsJSFunction()) {
5269 : auto func = i::Handle<i::JSFunction>::cast(self);
5270 : return Utils::ToLocal(handle(func->shared()->name(), isolate));
5271 : }
5272 0 : return ToApiHandle<Primitive>(isolate->factory()->undefined_value());
5273 : }
5274 :
5275 :
5276 7 : Local<Value> Function::GetInferredName() const {
5277 : auto self = Utils::OpenHandle(this);
5278 7 : if (!self->IsJSFunction()) {
5279 : return ToApiHandle<Primitive>(
5280 0 : self->GetIsolate()->factory()->undefined_value());
5281 : }
5282 : auto func = i::Handle<i::JSFunction>::cast(self);
5283 7 : return Utils::ToLocal(i::Handle<i::Object>(func->shared()->inferred_name(),
5284 : func->GetIsolate()));
5285 : }
5286 :
5287 :
5288 84 : Local<Value> Function::GetDebugName() const {
5289 : auto self = Utils::OpenHandle(this);
5290 84 : if (!self->IsJSFunction()) {
5291 : return ToApiHandle<Primitive>(
5292 0 : self->GetIsolate()->factory()->undefined_value());
5293 : }
5294 84 : auto func = i::Handle<i::JSFunction>::cast(self);
5295 84 : i::Handle<i::String> name = i::JSFunction::GetDebugName(func);
5296 : return Utils::ToLocal(i::Handle<i::Object>(*name, name->GetIsolate()));
5297 : }
5298 :
5299 :
5300 49 : Local<Value> Function::GetDisplayName() const {
5301 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
5302 : ENTER_V8(isolate);
5303 : auto self = Utils::OpenHandle(this);
5304 49 : if (!self->IsJSFunction()) {
5305 0 : return ToApiHandle<Primitive>(isolate->factory()->undefined_value());
5306 : }
5307 : auto func = i::Handle<i::JSFunction>::cast(self);
5308 : i::Handle<i::String> property_name =
5309 49 : isolate->factory()->NewStringFromStaticChars("displayName");
5310 : i::Handle<i::Object> value =
5311 49 : i::JSReceiver::GetDataProperty(func, property_name);
5312 49 : if (value->IsString()) {
5313 : i::Handle<i::String> name = i::Handle<i::String>::cast(value);
5314 21 : if (name->length() > 0) return Utils::ToLocal(name);
5315 : }
5316 28 : return ToApiHandle<Primitive>(isolate->factory()->undefined_value());
5317 : }
5318 :
5319 :
5320 14 : ScriptOrigin Function::GetScriptOrigin() const {
5321 : auto self = Utils::OpenHandle(this);
5322 14 : if (!self->IsJSFunction()) {
5323 : return v8::ScriptOrigin(Local<Value>());
5324 : }
5325 : auto func = i::Handle<i::JSFunction>::cast(self);
5326 14 : if (func->shared()->script()->IsScript()) {
5327 : i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
5328 14 : return GetScriptOriginForScript(func->GetIsolate(), script);
5329 : }
5330 : return v8::ScriptOrigin(Local<Value>());
5331 : }
5332 :
5333 :
5334 : const int Function::kLineOffsetNotFound = -1;
5335 :
5336 :
5337 7196 : int Function::GetScriptLineNumber() const {
5338 : auto self = Utils::OpenHandle(this);
5339 7196 : if (!self->IsJSFunction()) {
5340 : return kLineOffsetNotFound;
5341 : }
5342 : auto func = i::Handle<i::JSFunction>::cast(self);
5343 7190 : if (func->shared()->script()->IsScript()) {
5344 : i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
5345 7190 : return i::Script::GetLineNumber(script, func->shared()->start_position());
5346 : }
5347 : return kLineOffsetNotFound;
5348 : }
5349 :
5350 :
5351 196 : int Function::GetScriptColumnNumber() const {
5352 : auto self = Utils::OpenHandle(this);
5353 196 : if (!self->IsJSFunction()) {
5354 : return kLineOffsetNotFound;
5355 : }
5356 : auto func = i::Handle<i::JSFunction>::cast(self);
5357 190 : if (func->shared()->script()->IsScript()) {
5358 : i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
5359 190 : return i::Script::GetColumnNumber(script, func->shared()->start_position());
5360 : }
5361 : return kLineOffsetNotFound;
5362 : }
5363 :
5364 :
5365 0 : bool Function::IsBuiltin() const {
5366 : auto self = Utils::OpenHandle(this);
5367 0 : if (!self->IsJSFunction()) {
5368 : return false;
5369 : }
5370 : auto func = i::Handle<i::JSFunction>::cast(self);
5371 0 : return !func->shared()->IsUserJavaScript();
5372 : }
5373 :
5374 :
5375 9080 : int Function::ScriptId() const {
5376 : auto self = Utils::OpenHandle(this);
5377 9080 : if (!self->IsJSFunction()) {
5378 : return v8::UnboundScript::kNoScriptId;
5379 : }
5380 : auto func = i::Handle<i::JSFunction>::cast(self);
5381 9074 : if (!func->shared()->script()->IsScript()) {
5382 : return v8::UnboundScript::kNoScriptId;
5383 : }
5384 : i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
5385 8990 : return script->id();
5386 : }
5387 :
5388 :
5389 272 : Local<v8::Value> Function::GetBoundFunction() const {
5390 : auto self = Utils::OpenHandle(this);
5391 272 : if (self->IsJSBoundFunction()) {
5392 : auto bound_function = i::Handle<i::JSBoundFunction>::cast(self);
5393 : auto bound_target_function = i::handle(
5394 : bound_function->bound_target_function(), bound_function->GetIsolate());
5395 122 : return Utils::CallableToLocal(bound_target_function);
5396 : }
5397 150 : return v8::Undefined(reinterpret_cast<v8::Isolate*>(self->GetIsolate()));
5398 : }
5399 :
5400 60 : int Name::GetIdentityHash() {
5401 : auto self = Utils::OpenHandle(this);
5402 60 : return static_cast<int>(self->Hash());
5403 : }
5404 :
5405 :
5406 200254204 : int String::Length() const {
5407 : i::Handle<i::String> str = Utils::OpenHandle(this);
5408 200254204 : return str->length();
5409 : }
5410 :
5411 :
5412 80 : bool String::IsOneByte() const {
5413 : i::Handle<i::String> str = Utils::OpenHandle(this);
5414 80 : return str->HasOnlyOneByteChars();
5415 : }
5416 :
5417 :
5418 : // Helpers for ContainsOnlyOneByteHelper
5419 : template<size_t size> struct OneByteMask;
5420 : template<> struct OneByteMask<4> {
5421 : static const uint32_t value = 0xFF00FF00;
5422 : };
5423 : template<> struct OneByteMask<8> {
5424 : static const uint64_t value = V8_2PART_UINT64_C(0xFF00FF00, FF00FF00);
5425 : };
5426 : static const uintptr_t kOneByteMask = OneByteMask<sizeof(uintptr_t)>::value;
5427 : static const uintptr_t kAlignmentMask = sizeof(uintptr_t) - 1;
5428 : static inline bool Unaligned(const uint16_t* chars) {
5429 55098 : return reinterpret_cast<const uintptr_t>(chars) & kAlignmentMask;
5430 : }
5431 :
5432 :
5433 : static inline const uint16_t* Align(const uint16_t* chars) {
5434 : return reinterpret_cast<uint16_t*>(
5435 21462 : reinterpret_cast<uintptr_t>(chars) & ~kAlignmentMask);
5436 : }
5437 :
5438 : class ContainsOnlyOneByteHelper {
5439 : public:
5440 21462 : ContainsOnlyOneByteHelper() : is_one_byte_(true) {}
5441 21462 : bool Check(i::String* string) {
5442 21462 : i::ConsString* cons_string = i::String::VisitFlat(this, string, 0);
5443 21462 : if (cons_string == NULL) return is_one_byte_;
5444 36 : return CheckCons(cons_string);
5445 : }
5446 : void VisitOneByteString(const uint8_t* chars, int length) {
5447 : // Nothing to do.
5448 : }
5449 21462 : void VisitTwoByteString(const uint16_t* chars, int length) {
5450 : // Accumulated bits.
5451 : uintptr_t acc = 0;
5452 : // Align to uintptr_t.
5453 21462 : const uint16_t* end = chars + length;
5454 76560 : while (Unaligned(chars) && chars != end) {
5455 33636 : acc |= *chars++;
5456 : }
5457 : // Read word aligned in blocks,
5458 : // checking the return value at the end of each block.
5459 : const uint16_t* aligned_end = Align(end);
5460 : const int increment = sizeof(uintptr_t)/sizeof(uint16_t);
5461 : const int inner_loops = 16;
5462 61770 : while (chars + inner_loops*increment < aligned_end) {
5463 475680 : for (int i = 0; i < inner_loops; i++) {
5464 475680 : acc |= *reinterpret_cast<const uintptr_t*>(chars);
5465 475680 : chars += increment;
5466 : }
5467 : // Check for early return.
5468 29730 : if ((acc & kOneByteMask) != 0) {
5469 10884 : is_one_byte_ = false;
5470 32346 : return;
5471 : }
5472 : }
5473 : // Read the rest.
5474 561744 : while (chars != end) {
5475 551166 : acc |= *chars++;
5476 : }
5477 : // Check result.
5478 10578 : if ((acc & kOneByteMask) != 0) is_one_byte_ = false;
5479 : }
5480 :
5481 : private:
5482 48 : bool CheckCons(i::ConsString* cons_string) {
5483 : while (true) {
5484 : // Check left side if flat.
5485 : i::String* left = cons_string->first();
5486 : i::ConsString* left_as_cons =
5487 47532 : i::String::VisitFlat(this, left, 0);
5488 47532 : if (!is_one_byte_) return false;
5489 : // Check right side if flat.
5490 : i::String* right = cons_string->second();
5491 : i::ConsString* right_as_cons =
5492 47532 : i::String::VisitFlat(this, right, 0);
5493 47532 : if (!is_one_byte_) return false;
5494 : // Standard recurse/iterate trick.
5495 47532 : if (left_as_cons != NULL && right_as_cons != NULL) {
5496 12 : if (left->length() < right->length()) {
5497 0 : CheckCons(left_as_cons);
5498 : cons_string = right_as_cons;
5499 : } else {
5500 12 : CheckCons(right_as_cons);
5501 : cons_string = left_as_cons;
5502 : }
5503 : // Check fast return.
5504 12 : if (!is_one_byte_) return false;
5505 : continue;
5506 : }
5507 : // Descend left in place.
5508 47520 : if (left_as_cons != NULL) {
5509 : cons_string = left_as_cons;
5510 : continue;
5511 : }
5512 : // Descend right in place.
5513 23778 : if (right_as_cons != NULL) {
5514 : cons_string = right_as_cons;
5515 : continue;
5516 : }
5517 : // Terminate.
5518 : break;
5519 : }
5520 : return is_one_byte_;
5521 : }
5522 : bool is_one_byte_;
5523 : DISALLOW_COPY_AND_ASSIGN(ContainsOnlyOneByteHelper);
5524 : };
5525 :
5526 :
5527 21486 : bool String::ContainsOnlyOneByte() const {
5528 : i::Handle<i::String> str = Utils::OpenHandle(this);
5529 21486 : if (str->HasOnlyOneByteChars()) return true;
5530 : ContainsOnlyOneByteHelper helper;
5531 21462 : return helper.Check(*str);
5532 : }
5533 :
5534 :
5535 : class Utf8LengthHelper : public i::AllStatic {
5536 : public:
5537 : enum State {
5538 : kEndsWithLeadingSurrogate = 1 << 0,
5539 : kStartsWithTrailingSurrogate = 1 << 1,
5540 : kLeftmostEdgeIsCalculated = 1 << 2,
5541 : kRightmostEdgeIsCalculated = 1 << 3,
5542 : kLeftmostEdgeIsSurrogate = 1 << 4,
5543 : kRightmostEdgeIsSurrogate = 1 << 5
5544 : };
5545 :
5546 : static const uint8_t kInitialState = 0;
5547 :
5548 : static inline bool EndsWithSurrogate(uint8_t state) {
5549 1803845 : return state & kEndsWithLeadingSurrogate;
5550 : }
5551 :
5552 : static inline bool StartsWithSurrogate(uint8_t state) {
5553 1222559 : return state & kStartsWithTrailingSurrogate;
5554 : }
5555 :
5556 : class Visitor {
5557 : public:
5558 13598229 : Visitor() : utf8_length_(0), state_(kInitialState) {}
5559 :
5560 : void VisitOneByteString(const uint8_t* chars, int length) {
5561 : int utf8_length = 0;
5562 : // Add in length 1 for each non-Latin1 character.
5563 112517937 : for (int i = 0; i < length; i++) {
5564 112517937 : utf8_length += *chars++ >> 7;
5565 : }
5566 : // Add in length 1 for each character.
5567 12162713 : utf8_length_ = utf8_length + length;
5568 12162713 : state_ = kInitialState;
5569 : }
5570 :
5571 441474 : void VisitTwoByteString(const uint16_t* chars, int length) {
5572 : int utf8_length = 0;
5573 : int last_character = unibrow::Utf16::kNoPreviousCharacter;
5574 4030694 : for (int i = 0; i < length; i++) {
5575 3589220 : uint16_t c = chars[i];
5576 3589220 : utf8_length += unibrow::Utf8::Length(c, last_character);
5577 3589220 : last_character = c;
5578 : }
5579 441474 : utf8_length_ = utf8_length;
5580 : uint8_t state = 0;
5581 882948 : if (unibrow::Utf16::IsTrailSurrogate(chars[0])) {
5582 : state |= kStartsWithTrailingSurrogate;
5583 : }
5584 882948 : if (unibrow::Utf16::IsLeadSurrogate(chars[length-1])) {
5585 476 : state |= kEndsWithLeadingSurrogate;
5586 : }
5587 441474 : state_ = state;
5588 441474 : }
5589 :
5590 : static i::ConsString* VisitFlat(i::String* string,
5591 : int* length,
5592 : uint8_t* state) {
5593 : Visitor visitor;
5594 13598229 : i::ConsString* cons_string = i::String::VisitFlat(&visitor, string);
5595 13598229 : *length = visitor.utf8_length_;
5596 1988084 : *state = visitor.state_;
5597 : return cons_string;
5598 : }
5599 :
5600 : private:
5601 : int utf8_length_;
5602 : uint8_t state_;
5603 : DISALLOW_COPY_AND_ASSIGN(Visitor);
5604 : };
5605 :
5606 581475 : static inline void MergeLeafLeft(int* length,
5607 : uint8_t* state,
5608 : uint8_t leaf_state) {
5609 : bool edge_surrogate = StartsWithSurrogate(leaf_state);
5610 581475 : if (!(*state & kLeftmostEdgeIsCalculated)) {
5611 : DCHECK(!(*state & kLeftmostEdgeIsSurrogate));
5612 : *state |= kLeftmostEdgeIsCalculated
5613 228328 : | (edge_surrogate ? kLeftmostEdgeIsSurrogate : 0);
5614 353147 : } else if (EndsWithSurrogate(*state) && edge_surrogate) {
5615 14 : *length -= unibrow::Utf8::kBytesSavedByCombiningSurrogates;
5616 : }
5617 581475 : if (EndsWithSurrogate(leaf_state)) {
5618 210 : *state |= kEndsWithLeadingSurrogate;
5619 : } else {
5620 581265 : *state &= ~kEndsWithLeadingSurrogate;
5621 : }
5622 581475 : }
5623 :
5624 640895 : static inline void MergeLeafRight(int* length,
5625 : uint8_t* state,
5626 : uint8_t leaf_state) {
5627 : bool edge_surrogate = EndsWithSurrogate(leaf_state);
5628 640895 : if (!(*state & kRightmostEdgeIsCalculated)) {
5629 : DCHECK(!(*state & kRightmostEdgeIsSurrogate));
5630 : *state |= (kRightmostEdgeIsCalculated
5631 228328 : | (edge_surrogate ? kRightmostEdgeIsSurrogate : 0));
5632 412623 : } else if (edge_surrogate && StartsWithSurrogate(*state)) {
5633 21 : *length -= unibrow::Utf8::kBytesSavedByCombiningSurrogates;
5634 : }
5635 640895 : if (StartsWithSurrogate(leaf_state)) {
5636 238 : *state |= kStartsWithTrailingSurrogate;
5637 : } else {
5638 640657 : *state &= ~kStartsWithTrailingSurrogate;
5639 : }
5640 640895 : }
5641 :
5642 228328 : static inline void MergeTerminal(int* length,
5643 : uint8_t state,
5644 : uint8_t* state_out) {
5645 : DCHECK((state & kLeftmostEdgeIsCalculated) &&
5646 : (state & kRightmostEdgeIsCalculated));
5647 228461 : if (EndsWithSurrogate(state) && StartsWithSurrogate(state)) {
5648 0 : *length -= unibrow::Utf8::kBytesSavedByCombiningSurrogates;
5649 : }
5650 : *state_out = kInitialState |
5651 228328 : (state & kLeftmostEdgeIsSurrogate ? kStartsWithTrailingSurrogate : 0) |
5652 228328 : (state & kRightmostEdgeIsSurrogate ? kEndsWithLeadingSurrogate : 0);
5653 228328 : }
5654 :
5655 228328 : static int Calculate(i::ConsString* current, uint8_t* state_out) {
5656 : using internal::ConsString;
5657 228328 : int total_length = 0;
5658 228328 : uint8_t state = kInitialState;
5659 : while (true) {
5660 : i::String* left = current->first();
5661 : i::String* right = current->second();
5662 : uint8_t right_leaf_state;
5663 : uint8_t left_leaf_state;
5664 : int leaf_length;
5665 : ConsString* left_as_cons =
5666 : Visitor::VisitFlat(left, &leaf_length, &left_leaf_state);
5667 994042 : if (left_as_cons == NULL) {
5668 578381 : total_length += leaf_length;
5669 578381 : MergeLeafLeft(&total_length, &state, left_leaf_state);
5670 : }
5671 : ConsString* right_as_cons =
5672 : Visitor::VisitFlat(right, &leaf_length, &right_leaf_state);
5673 994042 : if (right_as_cons == NULL) {
5674 639165 : total_length += leaf_length;
5675 639165 : MergeLeafRight(&total_length, &state, right_leaf_state);
5676 639165 : if (left_as_cons != NULL) {
5677 : // 1 Leaf node. Descend in place.
5678 : current = left_as_cons;
5679 760890 : continue;
5680 : } else {
5681 : // Terminal node.
5682 228328 : MergeTerminal(&total_length, state, state_out);
5683 228328 : return total_length;
5684 : }
5685 354877 : } else if (left_as_cons == NULL) {
5686 : // 1 Leaf node. Descend in place.
5687 : current = right_as_cons;
5688 : continue;
5689 : }
5690 : // Both strings are ConsStrings.
5691 : // Recurse on smallest.
5692 4824 : if (left->length() < right->length()) {
5693 3094 : total_length += Calculate(left_as_cons, &left_leaf_state);
5694 3094 : MergeLeafLeft(&total_length, &state, left_leaf_state);
5695 : current = right_as_cons;
5696 : } else {
5697 1730 : total_length += Calculate(right_as_cons, &right_leaf_state);
5698 1730 : MergeLeafRight(&total_length, &state, right_leaf_state);
5699 : current = left_as_cons;
5700 : }
5701 : }
5702 : UNREACHABLE();
5703 : return 0;
5704 : }
5705 :
5706 : static inline int Calculate(i::ConsString* current) {
5707 223504 : uint8_t state = kInitialState;
5708 223504 : return Calculate(current, &state);
5709 : }
5710 :
5711 : private:
5712 : DISALLOW_IMPLICIT_CONSTRUCTORS(Utf8LengthHelper);
5713 : };
5714 :
5715 :
5716 11617265 : static int Utf8Length(i::String* str, i::Isolate* isolate) {
5717 : int length = str->length();
5718 11617265 : if (length == 0) return 0;
5719 : uint8_t state;
5720 : i::ConsString* cons_string =
5721 : Utf8LengthHelper::Visitor::VisitFlat(str, &length, &state);
5722 11610145 : if (cons_string == NULL) return length;
5723 : return Utf8LengthHelper::Calculate(cons_string);
5724 : }
5725 :
5726 :
5727 670 : int String::Utf8Length() const {
5728 : i::Handle<i::String> str = Utils::OpenHandle(this);
5729 : i::Isolate* isolate = str->GetIsolate();
5730 670 : return v8::Utf8Length(*str, isolate);
5731 : }
5732 :
5733 :
5734 : class Utf8WriterVisitor {
5735 : public:
5736 : Utf8WriterVisitor(
5737 : char* buffer,
5738 : int capacity,
5739 : bool skip_capacity_check,
5740 : bool replace_invalid_utf8)
5741 : : early_termination_(false),
5742 : last_character_(unibrow::Utf16::kNoPreviousCharacter),
5743 : buffer_(buffer),
5744 : start_(buffer),
5745 : capacity_(capacity),
5746 123 : skip_capacity_check_(capacity == -1 || skip_capacity_check),
5747 : replace_invalid_utf8_(replace_invalid_utf8),
5748 11616907 : utf16_chars_read_(0) {
5749 : }
5750 :
5751 260 : static int WriteEndCharacter(uint16_t character,
5752 : int last_character,
5753 : int remaining,
5754 : char* const buffer,
5755 : bool replace_invalid_utf8) {
5756 : DCHECK_GT(remaining, 0);
5757 : // We can't use a local buffer here because Encode needs to modify
5758 : // previous characters in the stream. We know, however, that
5759 : // exactly one character will be advanced.
5760 520 : if (unibrow::Utf16::IsSurrogatePair(last_character, character)) {
5761 : int written = unibrow::Utf8::Encode(buffer, character, last_character,
5762 0 : replace_invalid_utf8);
5763 : DCHECK_EQ(written, 1);
5764 0 : return written;
5765 : }
5766 : // Use a scratch buffer to check the required characters.
5767 : char temp_buffer[unibrow::Utf8::kMaxEncodedSize];
5768 : // Can't encode using last_character as gcc has array bounds issues.
5769 : int written = unibrow::Utf8::Encode(temp_buffer, character,
5770 : unibrow::Utf16::kNoPreviousCharacter,
5771 260 : replace_invalid_utf8);
5772 : // Won't fit.
5773 260 : if (written > remaining) return 0;
5774 : // Copy over the character from temp_buffer.
5775 290 : for (int j = 0; j < written; j++) {
5776 290 : buffer[j] = temp_buffer[j];
5777 : }
5778 : return written;
5779 : }
5780 :
5781 : // Visit writes out a group of code units (chars) of a v8::String to the
5782 : // internal buffer_. This is done in two phases. The first phase calculates a
5783 : // pesimistic estimate (writable_length) on how many code units can be safely
5784 : // written without exceeding the buffer capacity and without writing the last
5785 : // code unit (it could be a lead surrogate). The estimated number of code
5786 : // units is then written out in one go, and the reported byte usage is used
5787 : // to correct the estimate. This is repeated until the estimate becomes <= 0
5788 : // or all code units have been written out. The second phase writes out code
5789 : // units until the buffer capacity is reached, would be exceeded by the next
5790 : // unit, or all units have been written out.
5791 : template<typename Char>
5792 12319647 : void Visit(const Char* chars, const int length) {
5793 : DCHECK(!early_termination_);
5794 12319647 : if (length == 0) return;
5795 : // Copy state to stack.
5796 12310935 : char* buffer = buffer_;
5797 : int last_character = sizeof(Char) == 1
5798 : ? unibrow::Utf16::kNoPreviousCharacter
5799 151656 : : last_character_;
5800 : int i = 0;
5801 : // Do a fast loop where there is no exit capacity check.
5802 : while (true) {
5803 : int fast_length;
5804 12310992 : if (skip_capacity_check_) {
5805 : fast_length = length;
5806 : } else {
5807 173 : int remaining_capacity = capacity_ - static_cast<int>(buffer - start_);
5808 : // Need enough space to write everything but one character.
5809 : STATIC_ASSERT(unibrow::Utf16::kMaxExtraUtf8BytesForOneUtf16CodeUnit ==
5810 : 3);
5811 : int max_size_per_char = sizeof(Char) == 1 ? 2 : 3;
5812 : int writable_length =
5813 173 : (remaining_capacity - max_size_per_char)/max_size_per_char;
5814 : // Need to drop into slow loop.
5815 173 : if (writable_length <= 0) break;
5816 57 : fast_length = i + writable_length;
5817 57 : if (fast_length > length) fast_length = length;
5818 : }
5819 : // Write the characters to the stream.
5820 : if (sizeof(Char) == 1) {
5821 112500791 : for (; i < fast_length; i++) {
5822 112500791 : buffer += unibrow::Utf8::EncodeOneByte(
5823 112500791 : buffer, static_cast<uint8_t>(*chars++));
5824 : DCHECK(capacity_ == -1 || (buffer - start_) <= capacity_);
5825 : }
5826 : } else {
5827 3395392 : for (; i < fast_length; i++) {
5828 3395392 : uint16_t character = *chars++;
5829 3395392 : buffer += unibrow::Utf8::Encode(buffer, character, last_character,
5830 3395392 : replace_invalid_utf8_);
5831 3395392 : last_character = character;
5832 : DCHECK(capacity_ == -1 || (buffer - start_) <= capacity_);
5833 : }
5834 : }
5835 : // Array is fully written. Exit.
5836 12310876 : if (fast_length == length) {
5837 : // Write state back out to object.
5838 12310819 : last_character_ = last_character;
5839 12310819 : buffer_ = buffer;
5840 12310819 : utf16_chars_read_ += length;
5841 12310819 : return;
5842 : }
5843 : }
5844 : DCHECK(!skip_capacity_check_);
5845 : // Slow loop. Must check capacity on each iteration.
5846 : int remaining_capacity = capacity_ - static_cast<int>(buffer - start_);
5847 : DCHECK_GE(remaining_capacity, 0);
5848 209 : for (; i < length && remaining_capacity > 0; i++) {
5849 267 : uint16_t character = *chars++;
5850 : // remaining_capacity is <= 3 bytes at this point, so we do not write out
5851 : // an umatched lead surrogate.
5852 274 : if (replace_invalid_utf8_ && unibrow::Utf16::IsLeadSurrogate(character)) {
5853 7 : early_termination_ = true;
5854 7 : break;
5855 : }
5856 : int written = WriteEndCharacter(character,
5857 : last_character,
5858 : remaining_capacity,
5859 : buffer,
5860 260 : replace_invalid_utf8_);
5861 260 : if (written == 0) {
5862 51 : early_termination_ = true;
5863 51 : break;
5864 : }
5865 209 : buffer += written;
5866 209 : remaining_capacity -= written;
5867 : last_character = character;
5868 : }
5869 : // Write state back out to object.
5870 116 : last_character_ = last_character;
5871 116 : buffer_ = buffer;
5872 116 : utf16_chars_read_ += i;
5873 : }
5874 :
5875 : inline bool IsDone() {
5876 : return early_termination_;
5877 : }
5878 :
5879 : inline void VisitOneByteString(const uint8_t* chars, int length) {
5880 12167991 : Visit(chars, length);
5881 : }
5882 :
5883 : inline void VisitTwoByteString(const uint16_t* chars, int length) {
5884 151656 : Visit(chars, length);
5885 : }
5886 :
5887 11616777 : int CompleteWrite(bool write_null, int* utf16_chars_read_out) {
5888 : // Write out number of utf16 characters written to the stream.
5889 11616777 : if (utf16_chars_read_out != NULL) {
5890 198 : *utf16_chars_read_out = utf16_chars_read_;
5891 : }
5892 : // Only null terminate if all of the string was written and there's space.
5893 23233506 : if (write_null &&
5894 23233400 : !early_termination_ &&
5895 11616757 : (capacity_ == -1 || (buffer_ - start_) < capacity_)) {
5896 11616620 : *buffer_++ = '\0';
5897 : }
5898 11616777 : return static_cast<int>(buffer_ - start_);
5899 : }
5900 :
5901 : private:
5902 : bool early_termination_;
5903 : int last_character_;
5904 : char* buffer_;
5905 : char* const start_;
5906 : int capacity_;
5907 : bool const skip_capacity_check_;
5908 : bool const replace_invalid_utf8_;
5909 : int utf16_chars_read_;
5910 : DISALLOW_IMPLICIT_CONSTRUCTORS(Utf8WriterVisitor);
5911 : };
5912 :
5913 :
5914 12320231 : static bool RecursivelySerializeToUtf8(i::String* current,
5915 13023101 : Utf8WriterVisitor* writer,
5916 : int recursion_budget) {
5917 25343332 : while (!writer->IsDone()) {
5918 13023101 : i::ConsString* cons_string = i::String::VisitFlat(writer, current);
5919 13023101 : if (cons_string == NULL) return true; // Leaf node.
5920 703577 : if (recursion_budget <= 0) return false;
5921 : // Must write the left branch first.
5922 : i::String* first = cons_string->first();
5923 : bool success = RecursivelySerializeToUtf8(first,
5924 : writer,
5925 703570 : recursion_budget - 1);
5926 703570 : if (!success) return false;
5927 : // Inline tail recurse for right branch.
5928 : current = cons_string->second();
5929 : }
5930 : return true;
5931 : }
5932 :
5933 :
5934 11616810 : int String::WriteUtf8(char* buffer,
5935 : int capacity,
5936 : int* nchars_ref,
5937 : int options) const {
5938 : i::Handle<i::String> str = Utils::OpenHandle(this);
5939 11616810 : i::Isolate* isolate = str->GetIsolate();
5940 23233620 : LOG_API(isolate, String, WriteUtf8);
5941 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
5942 11616810 : if (options & HINT_MANY_WRITES_EXPECTED) {
5943 0 : str = i::String::Flatten(str); // Flatten the string for efficiency.
5944 : }
5945 : const int string_length = str->length();
5946 11616810 : bool write_null = !(options & NO_NULL_TERMINATION);
5947 11616810 : bool replace_invalid_utf8 = (options & REPLACE_INVALID_UTF8);
5948 : int max16BitCodeUnitSize = unibrow::Utf8::kMax16BitCodeUnitSize;
5949 : // First check if we can just write the string without checking capacity.
5950 11616810 : if (capacity == -1 || capacity / max16BitCodeUnitSize >= string_length) {
5951 : Utf8WriterVisitor writer(buffer, capacity, true, replace_invalid_utf8);
5952 : const int kMaxRecursion = 100;
5953 11616661 : bool success = RecursivelySerializeToUtf8(*str, &writer, kMaxRecursion);
5954 11616661 : if (success) return writer.CompleteWrite(write_null, nchars_ref);
5955 149 : } else if (capacity >= string_length) {
5956 : // First check that the buffer is large enough.
5957 91 : int utf8_bytes = v8::Utf8Length(*str, isolate);
5958 91 : if (utf8_bytes <= capacity) {
5959 : // one-byte fast path.
5960 33 : if (utf8_bytes == string_length) {
5961 : WriteOneByte(reinterpret_cast<uint8_t*>(buffer), 0, capacity, options);
5962 0 : if (nchars_ref != NULL) *nchars_ref = string_length;
5963 0 : if (write_null && (utf8_bytes+1 <= capacity)) {
5964 0 : return string_length + 1;
5965 : }
5966 : return string_length;
5967 : }
5968 33 : if (write_null && (utf8_bytes+1 > capacity)) {
5969 27 : options |= NO_NULL_TERMINATION;
5970 : }
5971 : // Recurse once without a capacity limit.
5972 : // This will get into the first branch above.
5973 : // TODO(dcarney) Check max left rec. in Utf8Length and fall through.
5974 33 : return WriteUtf8(buffer, -1, nchars_ref, options);
5975 : }
5976 : }
5977 : // Recursive slow path can potentially be unreasonable slow. Flatten.
5978 123 : str = i::String::Flatten(str);
5979 : Utf8WriterVisitor writer(buffer, capacity, false, replace_invalid_utf8);
5980 123 : i::String::VisitFlat(&writer, *str);
5981 123 : return writer.CompleteWrite(write_null, nchars_ref);
5982 : }
5983 :
5984 :
5985 : template<typename CharType>
5986 66905850 : static inline int WriteHelper(const String* string,
5987 : CharType* buffer,
5988 : int start,
5989 : int length,
5990 : int options) {
5991 66905850 : i::Isolate* isolate = Utils::OpenHandle(string)->GetIsolate();
5992 133811700 : LOG_API(isolate, String, Write);
5993 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
5994 : DCHECK(start >= 0 && length >= -1);
5995 : i::Handle<i::String> str = Utils::OpenHandle(string);
5996 66905850 : if (options & String::HINT_MANY_WRITES_EXPECTED) {
5997 : // Flatten the string for efficiency. This applies whether we are
5998 : // using StringCharacterStream or Get(i) to access the characters.
5999 0 : str = i::String::Flatten(str);
6000 : }
6001 66905850 : int end = start + length;
6002 133811645 : if ((length == -1) || (length > str->length() - start) )
6003 : end = str->length();
6004 66905850 : if (end < 0) return 0;
6005 66905850 : i::String::WriteToFlat(*str, buffer, start, end);
6006 66905849 : if (!(options & String::NO_NULL_TERMINATION) &&
6007 : (length == -1 || end - start < length)) {
6008 83 : buffer[end - start] = '\0';
6009 : }
6010 66905849 : return end - start;
6011 : }
6012 :
6013 :
6014 120 : int String::WriteOneByte(uint8_t* buffer,
6015 : int start,
6016 : int length,
6017 : int options) const {
6018 133 : return WriteHelper(this, buffer, start, length, options);
6019 : }
6020 :
6021 :
6022 66905710 : int String::Write(uint16_t* buffer,
6023 : int start,
6024 : int length,
6025 : int options) const {
6026 66905717 : return WriteHelper(this, buffer, start, length, options);
6027 : }
6028 :
6029 :
6030 164 : bool v8::String::IsExternal() const {
6031 : i::Handle<i::String> str = Utils::OpenHandle(this);
6032 164 : return i::StringShape(*str).IsExternalTwoByte();
6033 : }
6034 :
6035 :
6036 116 : bool v8::String::IsExternalOneByte() const {
6037 : i::Handle<i::String> str = Utils::OpenHandle(this);
6038 116 : return i::StringShape(*str).IsExternalOneByte();
6039 : }
6040 :
6041 :
6042 0 : void v8::String::VerifyExternalStringResource(
6043 : v8::String::ExternalStringResource* value) const {
6044 : i::Handle<i::String> str = Utils::OpenHandle(this);
6045 : const v8::String::ExternalStringResource* expected;
6046 0 : if (i::StringShape(*str).IsExternalTwoByte()) {
6047 : const void* resource =
6048 : i::Handle<i::ExternalTwoByteString>::cast(str)->resource();
6049 : expected = reinterpret_cast<const ExternalStringResource*>(resource);
6050 : } else {
6051 : expected = NULL;
6052 : }
6053 0 : CHECK_EQ(expected, value);
6054 0 : }
6055 :
6056 0 : void v8::String::VerifyExternalStringResourceBase(
6057 : v8::String::ExternalStringResourceBase* value, Encoding encoding) const {
6058 : i::Handle<i::String> str = Utils::OpenHandle(this);
6059 : const v8::String::ExternalStringResourceBase* expected;
6060 : Encoding expectedEncoding;
6061 0 : if (i::StringShape(*str).IsExternalOneByte()) {
6062 : const void* resource =
6063 : i::Handle<i::ExternalOneByteString>::cast(str)->resource();
6064 : expected = reinterpret_cast<const ExternalStringResourceBase*>(resource);
6065 : expectedEncoding = ONE_BYTE_ENCODING;
6066 0 : } else if (i::StringShape(*str).IsExternalTwoByte()) {
6067 : const void* resource =
6068 : i::Handle<i::ExternalTwoByteString>::cast(str)->resource();
6069 : expected = reinterpret_cast<const ExternalStringResourceBase*>(resource);
6070 : expectedEncoding = TWO_BYTE_ENCODING;
6071 : } else {
6072 : expected = NULL;
6073 : expectedEncoding =
6074 0 : str->IsOneByteRepresentation() ? ONE_BYTE_ENCODING : TWO_BYTE_ENCODING;
6075 : }
6076 0 : CHECK_EQ(expected, value);
6077 0 : CHECK_EQ(expectedEncoding, encoding);
6078 0 : }
6079 :
6080 : const v8::String::ExternalOneByteStringResource*
6081 13 : v8::String::GetExternalOneByteStringResource() const {
6082 : i::Handle<i::String> str = Utils::OpenHandle(this);
6083 13 : if (i::StringShape(*str).IsExternalOneByte()) {
6084 : const void* resource =
6085 : i::Handle<i::ExternalOneByteString>::cast(str)->resource();
6086 13 : return reinterpret_cast<const ExternalOneByteStringResource*>(resource);
6087 : } else {
6088 : return NULL;
6089 : }
6090 : }
6091 :
6092 :
6093 215 : Local<Value> Symbol::Name() const {
6094 : i::Handle<i::Symbol> sym = Utils::OpenHandle(this);
6095 : i::Handle<i::Object> name(sym->name(), sym->GetIsolate());
6096 215 : return Utils::ToLocal(name);
6097 : }
6098 :
6099 :
6100 14 : Local<Value> Private::Name() const {
6101 14 : return reinterpret_cast<const Symbol*>(this)->Name();
6102 : }
6103 :
6104 :
6105 1478568 : double Number::Value() const {
6106 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
6107 1478568 : return obj->Number();
6108 : }
6109 :
6110 :
6111 13938425 : bool Boolean::Value() const {
6112 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
6113 13938425 : return obj->IsTrue(i::HeapObject::cast(*obj)->GetIsolate());
6114 : }
6115 :
6116 :
6117 1786 : int64_t Integer::Value() const {
6118 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
6119 95151 : if (obj->IsSmi()) {
6120 95123 : return i::Smi::cast(*obj)->value();
6121 : } else {
6122 28 : return static_cast<int64_t>(obj->Number());
6123 : }
6124 : }
6125 :
6126 :
6127 287985 : int32_t Int32::Value() const {
6128 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
6129 287985 : if (obj->IsSmi()) {
6130 287985 : return i::Smi::cast(*obj)->value();
6131 : } else {
6132 0 : return static_cast<int32_t>(obj->Number());
6133 : }
6134 : }
6135 :
6136 :
6137 49 : uint32_t Uint32::Value() const {
6138 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
6139 49 : if (obj->IsSmi()) {
6140 28 : return i::Smi::cast(*obj)->value();
6141 : } else {
6142 21 : return static_cast<uint32_t>(obj->Number());
6143 : }
6144 : }
6145 :
6146 2862 : int v8::Object::InternalFieldCount() {
6147 : i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
6148 2862 : if (!self->IsJSObject()) return 0;
6149 2862 : return i::Handle<i::JSObject>::cast(self)->GetEmbedderFieldCount();
6150 : }
6151 :
6152 2013 : static bool InternalFieldOK(i::Handle<i::JSReceiver> obj, int index,
6153 : const char* location) {
6154 : return Utils::ApiCheck(
6155 4026 : obj->IsJSObject() &&
6156 2013 : (index < i::Handle<i::JSObject>::cast(obj)->GetEmbedderFieldCount()),
6157 2013 : location, "Internal field out of bounds");
6158 : }
6159 :
6160 658 : Local<Value> v8::Object::SlowGetInternalField(int index) {
6161 : i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
6162 : const char* location = "v8::Object::GetInternalField()";
6163 658 : if (!InternalFieldOK(obj, index, location)) return Local<Value>();
6164 : i::Handle<i::Object> value(
6165 : i::Handle<i::JSObject>::cast(obj)->GetEmbedderField(index),
6166 : obj->GetIsolate());
6167 : return Utils::ToLocal(value);
6168 : }
6169 :
6170 102 : void v8::Object::SetInternalField(int index, v8::Local<Value> value) {
6171 : i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
6172 : const char* location = "v8::Object::SetInternalField()";
6173 204 : if (!InternalFieldOK(obj, index, location)) return;
6174 : i::Handle<i::Object> val = Utils::OpenHandle(*value);
6175 102 : i::Handle<i::JSObject>::cast(obj)->SetEmbedderField(index, *val);
6176 : }
6177 :
6178 24 : void* v8::Object::SlowGetAlignedPointerFromInternalField(int index) {
6179 : i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
6180 : const char* location = "v8::Object::GetAlignedPointerFromInternalField()";
6181 24 : if (!InternalFieldOK(obj, index, location)) return NULL;
6182 : return DecodeSmiToAligned(
6183 : i::Handle<i::JSObject>::cast(obj)->GetEmbedderField(index), location);
6184 : }
6185 :
6186 1229 : void v8::Object::SetAlignedPointerInInternalField(int index, void* value) {
6187 : i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
6188 : const char* location = "v8::Object::SetAlignedPointerInInternalField()";
6189 2458 : if (!InternalFieldOK(obj, index, location)) return;
6190 : i::Handle<i::JSObject>::cast(obj)->SetEmbedderField(
6191 : index, EncodeAlignedAsSmi(value, location));
6192 : DCHECK_EQ(value, GetAlignedPointerFromInternalField(index));
6193 : }
6194 :
6195 14 : void v8::Object::SetAlignedPointerInInternalFields(int argc, int indices[],
6196 : void* values[]) {
6197 : i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
6198 : const char* location = "v8::Object::SetAlignedPointerInInternalFields()";
6199 : i::DisallowHeapAllocation no_gc;
6200 : i::JSObject* object = i::JSObject::cast(*obj);
6201 14 : int nof_embedder_fields = object->GetEmbedderFieldCount();
6202 42 : for (int i = 0; i < argc; i++) {
6203 28 : int index = indices[i];
6204 28 : if (!Utils::ApiCheck(index < nof_embedder_fields, location,
6205 28 : "Internal field out of bounds")) {
6206 14 : return;
6207 : }
6208 28 : void* value = values[i];
6209 : object->SetEmbedderField(index, EncodeAlignedAsSmi(value, location));
6210 : DCHECK_EQ(value, GetAlignedPointerFromInternalField(index));
6211 : }
6212 : }
6213 :
6214 17107652 : static void* ExternalValue(i::Object* obj) {
6215 : // Obscure semantics for undefined, but somehow checked in our unit tests...
6216 34215304 : if (!obj->IsSmi() &&
6217 : obj->IsUndefined(i::HeapObject::cast(obj)->GetIsolate())) {
6218 : return NULL;
6219 : }
6220 : i::Object* foreign = i::JSObject::cast(obj)->GetEmbedderField(0);
6221 17107652 : return i::Foreign::cast(foreign)->foreign_address();
6222 : }
6223 :
6224 :
6225 : // --- E n v i r o n m e n t ---
6226 :
6227 :
6228 59461 : void v8::V8::InitializePlatform(Platform* platform) {
6229 59461 : i::V8::InitializePlatform(platform);
6230 59461 : }
6231 :
6232 :
6233 58744 : void v8::V8::ShutdownPlatform() {
6234 58744 : i::V8::ShutdownPlatform();
6235 58744 : }
6236 :
6237 :
6238 59923 : bool v8::V8::Initialize() {
6239 59923 : i::V8::Initialize();
6240 : #ifdef V8_USE_EXTERNAL_STARTUP_DATA
6241 59923 : i::ReadNatives();
6242 : #endif
6243 59923 : return true;
6244 : }
6245 :
6246 : #if V8_OS_LINUX && V8_TARGET_ARCH_X64 && !V8_OS_ANDROID
6247 0 : bool V8::TryHandleSignal(int signum, void* info, void* context) {
6248 : return v8::internal::trap_handler::TryHandleSignal(
6249 0 : signum, static_cast<siginfo_t*>(info), static_cast<ucontext_t*>(context));
6250 : }
6251 : #endif // V8_OS_LINUX
6252 :
6253 4618 : bool V8::RegisterDefaultSignalHandler() {
6254 4618 : return v8::internal::trap_handler::RegisterDefaultSignalHandler();
6255 : }
6256 :
6257 0 : void v8::V8::SetEntropySource(EntropySource entropy_source) {
6258 0 : base::RandomNumberGenerator::SetEntropySource(entropy_source);
6259 0 : }
6260 :
6261 :
6262 0 : void v8::V8::SetReturnAddressLocationResolver(
6263 : ReturnAddressLocationResolver return_address_resolver) {
6264 0 : i::StackFrame::SetReturnAddressLocationResolver(return_address_resolver);
6265 0 : }
6266 :
6267 :
6268 32496 : bool v8::V8::Dispose() {
6269 32496 : i::V8::TearDown();
6270 : #ifdef V8_USE_EXTERNAL_STARTUP_DATA
6271 32496 : i::DisposeNatives();
6272 : #endif
6273 32496 : return true;
6274 : }
6275 :
6276 7 : HeapStatistics::HeapStatistics()
6277 : : total_heap_size_(0),
6278 : total_heap_size_executable_(0),
6279 : total_physical_size_(0),
6280 : total_available_size_(0),
6281 : used_heap_size_(0),
6282 : heap_size_limit_(0),
6283 : malloced_memory_(0),
6284 : peak_malloced_memory_(0),
6285 7 : does_zap_garbage_(0) {}
6286 :
6287 0 : HeapSpaceStatistics::HeapSpaceStatistics(): space_name_(0),
6288 : space_size_(0),
6289 : space_used_size_(0),
6290 : space_available_size_(0),
6291 0 : physical_space_size_(0) { }
6292 :
6293 :
6294 0 : HeapObjectStatistics::HeapObjectStatistics()
6295 : : object_type_(nullptr),
6296 : object_sub_type_(nullptr),
6297 : object_count_(0),
6298 0 : object_size_(0) {}
6299 :
6300 0 : HeapCodeStatistics::HeapCodeStatistics()
6301 0 : : code_and_metadata_size_(0), bytecode_and_metadata_size_(0) {}
6302 :
6303 0 : bool v8::V8::InitializeICU(const char* icu_data_file) {
6304 0 : return i::InitializeICU(icu_data_file);
6305 : }
6306 :
6307 56904 : bool v8::V8::InitializeICUDefaultLocation(const char* exec_path,
6308 : const char* icu_data_file) {
6309 56904 : return i::InitializeICUDefaultLocation(exec_path, icu_data_file);
6310 : }
6311 :
6312 59466 : void v8::V8::InitializeExternalStartupData(const char* directory_path) {
6313 59466 : i::InitializeExternalStartupData(directory_path);
6314 59466 : }
6315 :
6316 :
6317 0 : void v8::V8::InitializeExternalStartupData(const char* natives_blob,
6318 : const char* snapshot_blob) {
6319 0 : i::InitializeExternalStartupData(natives_blob, snapshot_blob);
6320 0 : }
6321 :
6322 :
6323 52 : const char* v8::V8::GetVersion() {
6324 52 : return i::Version::GetVersion();
6325 : }
6326 :
6327 : template <typename ObjectType>
6328 : struct InvokeBootstrapper;
6329 :
6330 : template <>
6331 : struct InvokeBootstrapper<i::Context> {
6332 : i::Handle<i::Context> Invoke(
6333 100278 : i::Isolate* isolate, i::MaybeHandle<i::JSGlobalProxy> maybe_global_proxy,
6334 : v8::Local<v8::ObjectTemplate> global_object_template,
6335 : v8::ExtensionConfiguration* extensions, size_t context_snapshot_index,
6336 : v8::DeserializeInternalFieldsCallback embedder_fields_deserializer) {
6337 : return isolate->bootstrapper()->CreateEnvironment(
6338 : maybe_global_proxy, global_object_template, extensions,
6339 200556 : context_snapshot_index, embedder_fields_deserializer);
6340 : }
6341 : };
6342 :
6343 : template <>
6344 : struct InvokeBootstrapper<i::JSGlobalProxy> {
6345 : i::Handle<i::JSGlobalProxy> Invoke(
6346 24 : i::Isolate* isolate, i::MaybeHandle<i::JSGlobalProxy> maybe_global_proxy,
6347 : v8::Local<v8::ObjectTemplate> global_object_template,
6348 : v8::ExtensionConfiguration* extensions, size_t context_snapshot_index,
6349 : v8::DeserializeInternalFieldsCallback embedder_fields_deserializer) {
6350 : USE(extensions);
6351 : USE(context_snapshot_index);
6352 : return isolate->bootstrapper()->NewRemoteContext(maybe_global_proxy,
6353 48 : global_object_template);
6354 : }
6355 : };
6356 :
6357 : template <typename ObjectType>
6358 100302 : static i::Handle<ObjectType> CreateEnvironment(
6359 : i::Isolate* isolate, v8::ExtensionConfiguration* extensions,
6360 : v8::MaybeLocal<ObjectTemplate> maybe_global_template,
6361 : v8::MaybeLocal<Value> maybe_global_proxy, size_t context_snapshot_index,
6362 : v8::DeserializeInternalFieldsCallback embedder_fields_deserializer) {
6363 : i::Handle<ObjectType> result;
6364 :
6365 : {
6366 : ENTER_V8_FOR_NEW_CONTEXT(isolate);
6367 : v8::Local<ObjectTemplate> proxy_template;
6368 : i::Handle<i::FunctionTemplateInfo> proxy_constructor;
6369 : i::Handle<i::FunctionTemplateInfo> global_constructor;
6370 : i::Handle<i::Object> named_interceptor(
6371 : isolate->factory()->undefined_value());
6372 : i::Handle<i::Object> indexed_interceptor(
6373 : isolate->factory()->undefined_value());
6374 :
6375 100302 : if (!maybe_global_template.IsEmpty()) {
6376 : v8::Local<v8::ObjectTemplate> global_template =
6377 : maybe_global_template.ToLocalChecked();
6378 : // Make sure that the global_template has a constructor.
6379 64799 : global_constructor = EnsureConstructor(isolate, *global_template);
6380 :
6381 : // Create a fresh template for the global proxy object.
6382 : proxy_template = ObjectTemplate::New(
6383 : reinterpret_cast<v8::Isolate*>(isolate));
6384 64799 : proxy_constructor = EnsureConstructor(isolate, *proxy_template);
6385 :
6386 : // Set the global template to be the prototype template of
6387 : // global proxy template.
6388 64799 : proxy_constructor->set_prototype_template(
6389 : *Utils::OpenHandle(*global_template));
6390 :
6391 64799 : proxy_template->SetInternalFieldCount(
6392 : global_template->InternalFieldCount());
6393 :
6394 : // Migrate security handlers from global_template to
6395 : // proxy_template. Temporarily removing access check
6396 : // information from the global template.
6397 64799 : if (!global_constructor->access_check_info()->IsUndefined(isolate)) {
6398 181 : proxy_constructor->set_access_check_info(
6399 : global_constructor->access_check_info());
6400 : proxy_constructor->set_needs_access_check(
6401 : global_constructor->needs_access_check());
6402 : global_constructor->set_needs_access_check(false);
6403 181 : global_constructor->set_access_check_info(
6404 181 : isolate->heap()->undefined_value());
6405 : }
6406 :
6407 : // Same for other interceptors. If the global constructor has
6408 : // interceptors, we need to replace them temporarily with noop
6409 : // interceptors, so the map is correctly marked as having interceptors,
6410 : // but we don't invoke any.
6411 64799 : if (!global_constructor->named_property_handler()->IsUndefined(isolate)) {
6412 : named_interceptor =
6413 : handle(global_constructor->named_property_handler(), isolate);
6414 163 : global_constructor->set_named_property_handler(
6415 163 : isolate->heap()->noop_interceptor_info());
6416 : }
6417 64799 : if (!global_constructor->indexed_property_handler()->IsUndefined(
6418 : isolate)) {
6419 : indexed_interceptor =
6420 : handle(global_constructor->indexed_property_handler(), isolate);
6421 0 : global_constructor->set_indexed_property_handler(
6422 0 : isolate->heap()->noop_interceptor_info());
6423 : }
6424 : }
6425 :
6426 : i::MaybeHandle<i::JSGlobalProxy> maybe_proxy;
6427 100302 : if (!maybe_global_proxy.IsEmpty()) {
6428 : maybe_proxy = i::Handle<i::JSGlobalProxy>::cast(
6429 : Utils::OpenHandle(*maybe_global_proxy.ToLocalChecked()));
6430 : }
6431 : // Create the environment.
6432 : InvokeBootstrapper<ObjectType> invoke;
6433 : result =
6434 : invoke.Invoke(isolate, maybe_proxy, proxy_template, extensions,
6435 : context_snapshot_index, embedder_fields_deserializer);
6436 :
6437 : // Restore the access check info and interceptors on the global template.
6438 100301 : if (!maybe_global_template.IsEmpty()) {
6439 : DCHECK(!global_constructor.is_null());
6440 : DCHECK(!proxy_constructor.is_null());
6441 64799 : global_constructor->set_access_check_info(
6442 : proxy_constructor->access_check_info());
6443 : global_constructor->set_needs_access_check(
6444 : proxy_constructor->needs_access_check());
6445 64799 : global_constructor->set_named_property_handler(*named_interceptor);
6446 64799 : global_constructor->set_indexed_property_handler(*indexed_interceptor);
6447 : }
6448 : }
6449 : // Leave V8.
6450 :
6451 100301 : return result;
6452 : }
6453 :
6454 100278 : Local<Context> NewContext(
6455 : v8::Isolate* external_isolate, v8::ExtensionConfiguration* extensions,
6456 : v8::MaybeLocal<ObjectTemplate> global_template,
6457 : v8::MaybeLocal<Value> global_object, size_t context_snapshot_index,
6458 : v8::DeserializeInternalFieldsCallback embedder_fields_deserializer) {
6459 100278 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
6460 200556 : TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.NewContext");
6461 200556 : LOG_API(isolate, Context, New);
6462 : i::HandleScope scope(isolate);
6463 : ExtensionConfiguration no_extensions;
6464 100278 : if (extensions == NULL) extensions = &no_extensions;
6465 : i::Handle<i::Context> env = CreateEnvironment<i::Context>(
6466 : isolate, extensions, global_template, global_object,
6467 100278 : context_snapshot_index, embedder_fields_deserializer);
6468 100277 : if (env.is_null()) {
6469 78 : if (isolate->has_pending_exception()) isolate->clear_pending_exception();
6470 78 : return Local<Context>();
6471 : }
6472 200476 : return Utils::ToLocal(scope.CloseAndEscape(env));
6473 : }
6474 :
6475 100163 : Local<Context> v8::Context::New(v8::Isolate* external_isolate,
6476 : v8::ExtensionConfiguration* extensions,
6477 : v8::MaybeLocal<ObjectTemplate> global_template,
6478 : v8::MaybeLocal<Value> global_object) {
6479 : return NewContext(external_isolate, extensions, global_template,
6480 100248 : global_object, 0, DeserializeInternalFieldsCallback());
6481 : }
6482 :
6483 36 : MaybeLocal<Context> v8::Context::FromSnapshot(
6484 : v8::Isolate* external_isolate, size_t context_snapshot_index,
6485 : v8::DeserializeInternalFieldsCallback embedder_fields_deserializer,
6486 : v8::ExtensionConfiguration* extensions, MaybeLocal<Value> global_object) {
6487 36 : size_t index_including_default_context = context_snapshot_index + 1;
6488 36 : if (!i::Snapshot::HasContextSnapshot(
6489 : reinterpret_cast<i::Isolate*>(external_isolate),
6490 36 : index_including_default_context)) {
6491 6 : return MaybeLocal<Context>();
6492 : }
6493 : return NewContext(external_isolate, extensions, MaybeLocal<ObjectTemplate>(),
6494 : global_object, index_including_default_context,
6495 30 : embedder_fields_deserializer);
6496 : }
6497 :
6498 24 : MaybeLocal<Object> v8::Context::NewRemoteContext(
6499 : v8::Isolate* external_isolate, v8::Local<ObjectTemplate> global_template,
6500 : v8::MaybeLocal<v8::Value> global_object) {
6501 24 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
6502 48 : LOG_API(isolate, Context, NewRemoteContext);
6503 : i::HandleScope scope(isolate);
6504 : i::Handle<i::FunctionTemplateInfo> global_constructor =
6505 24 : EnsureConstructor(isolate, *global_template);
6506 : Utils::ApiCheck(global_constructor->needs_access_check(),
6507 : "v8::Context::NewRemoteContext",
6508 : "Global template needs to have access checks enabled.");
6509 : i::Handle<i::AccessCheckInfo> access_check_info = i::handle(
6510 : i::AccessCheckInfo::cast(global_constructor->access_check_info()),
6511 : isolate);
6512 : Utils::ApiCheck(access_check_info->named_interceptor() != nullptr,
6513 : "v8::Context::NewRemoteContext",
6514 : "Global template needs to have access check handlers.");
6515 : i::Handle<i::JSGlobalProxy> global_proxy =
6516 : CreateEnvironment<i::JSGlobalProxy>(isolate, nullptr, global_template,
6517 : global_object, 0,
6518 24 : DeserializeInternalFieldsCallback());
6519 24 : if (global_proxy.is_null()) {
6520 0 : if (isolate->has_pending_exception()) isolate->clear_pending_exception();
6521 0 : return MaybeLocal<Object>();
6522 : }
6523 : return Utils::ToLocal(
6524 24 : scope.CloseAndEscape(i::Handle<i::JSObject>::cast(global_proxy)));
6525 : }
6526 :
6527 305 : void v8::Context::SetSecurityToken(Local<Value> token) {
6528 : i::Handle<i::Context> env = Utils::OpenHandle(this);
6529 : i::Handle<i::Object> token_handle = Utils::OpenHandle(*token);
6530 : env->set_security_token(*token_handle);
6531 305 : }
6532 :
6533 :
6534 0 : void v8::Context::UseDefaultSecurityToken() {
6535 : i::Handle<i::Context> env = Utils::OpenHandle(this);
6536 0 : env->set_security_token(env->global_object());
6537 0 : }
6538 :
6539 :
6540 15 : Local<Value> v8::Context::GetSecurityToken() {
6541 : i::Handle<i::Context> env = Utils::OpenHandle(this);
6542 : i::Isolate* isolate = env->GetIsolate();
6543 : i::Object* security_token = env->security_token();
6544 : i::Handle<i::Object> token_handle(security_token, isolate);
6545 15 : return Utils::ToLocal(token_handle);
6546 : }
6547 :
6548 :
6549 14462072 : v8::Isolate* Context::GetIsolate() {
6550 : i::Handle<i::Context> env = Utils::OpenHandle(this);
6551 14462072 : return reinterpret_cast<Isolate*>(env->GetIsolate());
6552 : }
6553 :
6554 849381 : v8::Local<v8::Object> Context::Global() {
6555 : i::Handle<i::Context> context = Utils::OpenHandle(this);
6556 : i::Isolate* isolate = context->GetIsolate();
6557 849381 : i::Handle<i::Object> global(context->global_proxy(), isolate);
6558 : // TODO(dcarney): This should always return the global proxy
6559 : // but can't presently as calls to GetProtoype will return the wrong result.
6560 849381 : if (i::Handle<i::JSGlobalProxy>::cast(
6561 849381 : global)->IsDetachedFrom(context->global_object())) {
6562 78 : global = i::Handle<i::Object>(context->global_object(), isolate);
6563 : }
6564 849381 : return Utils::ToLocal(i::Handle<i::JSObject>::cast(global));
6565 : }
6566 :
6567 :
6568 65 : void Context::DetachGlobal() {
6569 : i::Handle<i::Context> context = Utils::OpenHandle(this);
6570 65 : i::Isolate* isolate = context->GetIsolate();
6571 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
6572 65 : isolate->bootstrapper()->DetachGlobal(context);
6573 65 : }
6574 :
6575 :
6576 24 : Local<v8::Object> Context::GetExtrasBindingObject() {
6577 : i::Handle<i::Context> context = Utils::OpenHandle(this);
6578 : i::Isolate* isolate = context->GetIsolate();
6579 : i::Handle<i::JSObject> binding(context->extras_binding_object(), isolate);
6580 24 : return Utils::ToLocal(binding);
6581 : }
6582 :
6583 :
6584 39 : void Context::AllowCodeGenerationFromStrings(bool allow) {
6585 : i::Handle<i::Context> context = Utils::OpenHandle(this);
6586 : i::Isolate* isolate = context->GetIsolate();
6587 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
6588 : context->set_allow_code_gen_from_strings(
6589 39 : allow ? isolate->heap()->true_value() : isolate->heap()->false_value());
6590 39 : }
6591 :
6592 :
6593 4767 : bool Context::IsCodeGenerationFromStringsAllowed() {
6594 : i::Handle<i::Context> context = Utils::OpenHandle(this);
6595 : return !context->allow_code_gen_from_strings()->IsFalse(
6596 4767 : context->GetIsolate());
6597 : }
6598 :
6599 :
6600 6 : void Context::SetErrorMessageForCodeGenerationFromStrings(Local<String> error) {
6601 : i::Handle<i::Context> context = Utils::OpenHandle(this);
6602 : i::Handle<i::String> error_handle = Utils::OpenHandle(*error);
6603 : context->set_error_message_for_code_gen_from_strings(*error_handle);
6604 6 : }
6605 :
6606 0 : size_t Context::EstimatedSize() { return 0; }
6607 :
6608 2795 : MaybeLocal<v8::Object> ObjectTemplate::NewInstance(Local<Context> context) {
6609 11180 : PREPARE_FOR_EXECUTION(context, ObjectTemplate, NewInstance, Object);
6610 2795 : auto self = Utils::OpenHandle(this);
6611 : Local<Object> result;
6612 : has_pending_exception =
6613 5590 : !ToLocal<Object>(i::ApiNatives::InstantiateObject(self), &result);
6614 2795 : RETURN_ON_FAILED_EXECUTION(Object);
6615 2795 : RETURN_ESCAPED(result);
6616 : }
6617 :
6618 :
6619 0 : Local<v8::Object> ObjectTemplate::NewInstance() {
6620 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
6621 0 : RETURN_TO_LOCAL_UNCHECKED(NewInstance(context), Object);
6622 : }
6623 :
6624 :
6625 52848 : MaybeLocal<v8::Function> FunctionTemplate::GetFunction(Local<Context> context) {
6626 211392 : PREPARE_FOR_EXECUTION(context, FunctionTemplate, GetFunction, Function);
6627 52848 : auto self = Utils::OpenHandle(this);
6628 : Local<Function> result;
6629 : has_pending_exception =
6630 105696 : !ToLocal<Function>(i::ApiNatives::InstantiateFunction(self), &result);
6631 52848 : RETURN_ON_FAILED_EXECUTION(Function);
6632 52848 : RETURN_ESCAPED(result);
6633 : }
6634 :
6635 :
6636 0 : Local<v8::Function> FunctionTemplate::GetFunction() {
6637 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
6638 0 : RETURN_TO_LOCAL_UNCHECKED(GetFunction(context), Function);
6639 : }
6640 :
6641 10 : MaybeLocal<v8::Object> FunctionTemplate::NewRemoteInstance() {
6642 : auto self = Utils::OpenHandle(this);
6643 10 : i::Isolate* isolate = self->GetIsolate();
6644 20 : LOG_API(isolate, FunctionTemplate, NewRemoteInstance);
6645 : i::HandleScope scope(isolate);
6646 : i::Handle<i::FunctionTemplateInfo> constructor =
6647 20 : EnsureConstructor(isolate, *InstanceTemplate());
6648 : Utils::ApiCheck(constructor->needs_access_check(),
6649 : "v8::FunctionTemplate::NewRemoteInstance",
6650 : "InstanceTemplate needs to have access checks enabled.");
6651 : i::Handle<i::AccessCheckInfo> access_check_info = i::handle(
6652 : i::AccessCheckInfo::cast(constructor->access_check_info()), isolate);
6653 : Utils::ApiCheck(access_check_info->named_interceptor() != nullptr,
6654 : "v8::FunctionTemplate::NewRemoteInstance",
6655 : "InstanceTemplate needs to have access check handlers.");
6656 : i::Handle<i::JSObject> object;
6657 10 : if (!i::ApiNatives::InstantiateRemoteObject(
6658 20 : Utils::OpenHandle(*InstanceTemplate()))
6659 20 : .ToHandle(&object)) {
6660 0 : if (isolate->has_pending_exception()) {
6661 0 : isolate->OptionalRescheduleException(true);
6662 : }
6663 0 : return MaybeLocal<Object>();
6664 : }
6665 10 : return Utils::ToLocal(scope.CloseAndEscape(object));
6666 : }
6667 :
6668 51 : bool FunctionTemplate::HasInstance(v8::Local<v8::Value> value) {
6669 : auto self = Utils::OpenHandle(this);
6670 : auto obj = Utils::OpenHandle(*value);
6671 102 : if (obj->IsJSObject() && self->IsTemplateFor(i::JSObject::cast(*obj))) {
6672 : return true;
6673 : }
6674 23 : if (obj->IsJSGlobalProxy()) {
6675 : // If it's a global proxy, then test with the global object. Note that the
6676 : // inner global object may not necessarily be a JSGlobalObject.
6677 2 : i::PrototypeIterator iter(i::JSObject::cast(*obj)->map());
6678 : // The global proxy should always have a prototype, as it is a bug to call
6679 : // this on a detached JSGlobalProxy.
6680 : DCHECK(!iter.IsAtEnd());
6681 4 : return self->IsTemplateFor(iter.GetCurrent<i::JSObject>());
6682 : }
6683 : return false;
6684 : }
6685 :
6686 :
6687 11483 : Local<External> v8::External::New(Isolate* isolate, void* value) {
6688 : STATIC_ASSERT(sizeof(value) == sizeof(i::Address));
6689 11483 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6690 22966 : LOG_API(i_isolate, External, New);
6691 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6692 11483 : i::Handle<i::JSObject> external = i_isolate->factory()->NewExternal(value);
6693 11483 : return Utils::ExternalToLocal(external);
6694 : }
6695 :
6696 :
6697 17107652 : void* External::Value() const {
6698 17107652 : return ExternalValue(*Utils::OpenHandle(this));
6699 : }
6700 :
6701 :
6702 : // anonymous namespace for string creation helper functions
6703 : namespace {
6704 :
6705 : inline int StringLength(const char* string) {
6706 : return i::StrLength(string);
6707 : }
6708 :
6709 :
6710 : inline int StringLength(const uint8_t* string) {
6711 : return i::StrLength(reinterpret_cast<const char*>(string));
6712 : }
6713 :
6714 :
6715 : inline int StringLength(const uint16_t* string) {
6716 : int length = 0;
6717 4827 : while (string[length] != '\0')
6718 4770 : length++;
6719 : return length;
6720 : }
6721 :
6722 :
6723 : MUST_USE_RESULT
6724 26636563 : inline i::MaybeHandle<i::String> NewString(i::Factory* factory,
6725 : v8::NewStringType type,
6726 : i::Vector<const char> string) {
6727 26636563 : if (type == v8::NewStringType::kInternalized) {
6728 15939379 : return factory->InternalizeUtf8String(string);
6729 : }
6730 10697184 : return factory->NewStringFromUtf8(string);
6731 : }
6732 :
6733 :
6734 : MUST_USE_RESULT
6735 489 : inline i::MaybeHandle<i::String> NewString(i::Factory* factory,
6736 : v8::NewStringType type,
6737 : i::Vector<const uint8_t> string) {
6738 489 : if (type == v8::NewStringType::kInternalized) {
6739 390 : return factory->InternalizeOneByteString(string);
6740 : }
6741 99 : return factory->NewStringFromOneByte(string);
6742 : }
6743 :
6744 :
6745 : MUST_USE_RESULT
6746 1707618 : inline i::MaybeHandle<i::String> NewString(i::Factory* factory,
6747 : v8::NewStringType type,
6748 : i::Vector<const uint16_t> string) {
6749 1707618 : if (type == v8::NewStringType::kInternalized) {
6750 0 : return factory->InternalizeTwoByteString(string);
6751 : }
6752 1707618 : return factory->NewStringFromTwoByte(string);
6753 : }
6754 :
6755 :
6756 : STATIC_ASSERT(v8::String::kMaxLength == i::String::kMaxLength);
6757 :
6758 : } // anonymous namespace
6759 :
6760 : // TODO(dcarney): throw a context free exception.
6761 : #define NEW_STRING(isolate, class_name, function_name, Char, data, type, \
6762 : length) \
6763 : MaybeLocal<String> result; \
6764 : if (length == 0) { \
6765 : result = String::Empty(isolate); \
6766 : } else if (length > i::String::kMaxLength) { \
6767 : result = MaybeLocal<String>(); \
6768 : } else { \
6769 : i::Isolate* i_isolate = reinterpret_cast<internal::Isolate*>(isolate); \
6770 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate); \
6771 : LOG_API(i_isolate, class_name, function_name); \
6772 : if (length < 0) length = StringLength(data); \
6773 : i::Handle<i::String> handle_result = \
6774 : NewString(i_isolate->factory(), type, \
6775 : i::Vector<const Char>(data, length)) \
6776 : .ToHandleChecked(); \
6777 : result = Utils::ToLocal(handle_result); \
6778 : }
6779 :
6780 0 : Local<String> String::NewFromUtf8(Isolate* isolate,
6781 : const char* data,
6782 : NewStringType type,
6783 : int length) {
6784 0 : NEW_STRING(isolate, String, NewFromUtf8, char, data,
6785 : static_cast<v8::NewStringType>(type), length);
6786 0 : RETURN_TO_LOCAL_UNCHECKED(result, String);
6787 : }
6788 :
6789 :
6790 26636575 : MaybeLocal<String> String::NewFromUtf8(Isolate* isolate, const char* data,
6791 : v8::NewStringType type, int length) {
6792 133182833 : NEW_STRING(isolate, String, NewFromUtf8, char, data, type, length);
6793 26636575 : return result;
6794 : }
6795 :
6796 :
6797 0 : Local<String> String::NewFromOneByte(Isolate* isolate,
6798 : const uint8_t* data,
6799 : NewStringType type,
6800 : int length) {
6801 0 : NEW_STRING(isolate, String, NewFromOneByte, uint8_t, data,
6802 : static_cast<v8::NewStringType>(type), length);
6803 0 : RETURN_TO_LOCAL_UNCHECKED(result, String);
6804 : }
6805 :
6806 :
6807 507 : MaybeLocal<String> String::NewFromOneByte(Isolate* isolate, const uint8_t* data,
6808 : v8::NewStringType type, int length) {
6809 2469 : NEW_STRING(isolate, String, NewFromOneByte, uint8_t, data, type, length);
6810 507 : return result;
6811 : }
6812 :
6813 :
6814 0 : Local<String> String::NewFromTwoByte(Isolate* isolate,
6815 : const uint16_t* data,
6816 : NewStringType type,
6817 : int length) {
6818 0 : NEW_STRING(isolate, String, NewFromTwoByte, uint16_t, data,
6819 : static_cast<v8::NewStringType>(type), length);
6820 0 : RETURN_TO_LOCAL_UNCHECKED(result, String);
6821 : }
6822 :
6823 :
6824 1748798 : MaybeLocal<String> String::NewFromTwoByte(Isolate* isolate,
6825 : const uint16_t* data,
6826 : v8::NewStringType type, int length) {
6827 8579280 : NEW_STRING(isolate, String, NewFromTwoByte, uint16_t, data, type, length);
6828 1748800 : return result;
6829 : }
6830 :
6831 :
6832 231313 : Local<String> v8::String::Concat(Local<String> left, Local<String> right) {
6833 : i::Handle<i::String> left_string = Utils::OpenHandle(*left);
6834 231313 : i::Isolate* isolate = left_string->GetIsolate();
6835 : ENTER_V8(isolate);
6836 462626 : LOG_API(isolate, String, Concat);
6837 : i::Handle<i::String> right_string = Utils::OpenHandle(*right);
6838 : // If we are steering towards a range error, do not wait for the error to be
6839 : // thrown, and return the null handle instead.
6840 231313 : if (left_string->length() + right_string->length() > i::String::kMaxLength) {
6841 6 : return Local<String>();
6842 : }
6843 : i::Handle<i::String> result = isolate->factory()->NewConsString(
6844 462614 : left_string, right_string).ToHandleChecked();
6845 : return Utils::ToLocal(result);
6846 : }
6847 :
6848 :
6849 22686 : MaybeLocal<String> v8::String::NewExternalTwoByte(
6850 : Isolate* isolate, v8::String::ExternalStringResource* resource) {
6851 22686 : CHECK(resource && resource->data());
6852 : // TODO(dcarney): throw a context free exception.
6853 22686 : if (resource->length() > static_cast<size_t>(i::String::kMaxLength)) {
6854 7 : return MaybeLocal<String>();
6855 : }
6856 22679 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6857 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6858 45358 : LOG_API(i_isolate, String, NewExternalTwoByte);
6859 22679 : if (resource->length() > 0) {
6860 : i::Handle<i::String> string = i_isolate->factory()
6861 : ->NewExternalStringFromTwoByte(resource)
6862 45334 : .ToHandleChecked();
6863 : i_isolate->heap()->RegisterExternalString(*string);
6864 22667 : return Utils::ToLocal(string);
6865 : } else {
6866 : // The resource isn't going to be used, free it immediately.
6867 12 : resource->Dispose();
6868 12 : return Utils::ToLocal(i_isolate->factory()->empty_string());
6869 : }
6870 : }
6871 :
6872 :
6873 0 : Local<String> v8::String::NewExternal(
6874 : Isolate* isolate, v8::String::ExternalStringResource* resource) {
6875 0 : RETURN_TO_LOCAL_UNCHECKED(NewExternalTwoByte(isolate, resource), String);
6876 : }
6877 :
6878 :
6879 1500 : MaybeLocal<String> v8::String::NewExternalOneByte(
6880 : Isolate* isolate, v8::String::ExternalOneByteStringResource* resource) {
6881 1500 : CHECK(resource && resource->data());
6882 : // TODO(dcarney): throw a context free exception.
6883 1500 : if (resource->length() > static_cast<size_t>(i::String::kMaxLength)) {
6884 7 : return MaybeLocal<String>();
6885 : }
6886 1493 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6887 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6888 2986 : LOG_API(i_isolate, String, NewExternalOneByte);
6889 1493 : if (resource->length() > 0) {
6890 : i::Handle<i::String> string = i_isolate->factory()
6891 : ->NewExternalStringFromOneByte(resource)
6892 2974 : .ToHandleChecked();
6893 : i_isolate->heap()->RegisterExternalString(*string);
6894 1487 : return Utils::ToLocal(string);
6895 : } else {
6896 : // The resource isn't going to be used, free it immediately.
6897 6 : resource->Dispose();
6898 6 : return Utils::ToLocal(i_isolate->factory()->empty_string());
6899 : }
6900 : }
6901 :
6902 :
6903 0 : Local<String> v8::String::NewExternal(
6904 : Isolate* isolate, v8::String::ExternalOneByteStringResource* resource) {
6905 0 : RETURN_TO_LOCAL_UNCHECKED(NewExternalOneByte(isolate, resource), String);
6906 : }
6907 :
6908 :
6909 31 : bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) {
6910 : i::Handle<i::String> obj = Utils::OpenHandle(this);
6911 : i::Isolate* isolate = obj->GetIsolate();
6912 31 : if (i::StringShape(*obj).IsExternal()) {
6913 : return false; // Already an external string.
6914 : }
6915 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
6916 31 : if (isolate->heap()->IsInGCPostProcessing()) {
6917 : return false;
6918 : }
6919 31 : CHECK(resource && resource->data());
6920 :
6921 31 : bool result = obj->MakeExternal(resource);
6922 : // Assert that if CanMakeExternal(), then externalizing actually succeeds.
6923 : DCHECK(!CanMakeExternal() || result);
6924 31 : if (result) {
6925 : DCHECK(obj->IsExternalString());
6926 : isolate->heap()->RegisterExternalString(*obj);
6927 : }
6928 31 : return result;
6929 : }
6930 :
6931 :
6932 37 : bool v8::String::MakeExternal(
6933 : v8::String::ExternalOneByteStringResource* resource) {
6934 : i::Handle<i::String> obj = Utils::OpenHandle(this);
6935 : i::Isolate* isolate = obj->GetIsolate();
6936 37 : if (i::StringShape(*obj).IsExternal()) {
6937 : return false; // Already an external string.
6938 : }
6939 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
6940 37 : if (isolate->heap()->IsInGCPostProcessing()) {
6941 : return false;
6942 : }
6943 37 : CHECK(resource && resource->data());
6944 :
6945 37 : bool result = obj->MakeExternal(resource);
6946 : // Assert that if CanMakeExternal(), then externalizing actually succeeds.
6947 : DCHECK(!CanMakeExternal() || result);
6948 37 : if (result) {
6949 : DCHECK(obj->IsExternalString());
6950 : isolate->heap()->RegisterExternalString(*obj);
6951 : }
6952 37 : return result;
6953 : }
6954 :
6955 :
6956 24 : bool v8::String::CanMakeExternal() {
6957 : i::Handle<i::String> obj = Utils::OpenHandle(this);
6958 24 : if (obj->IsExternalString()) return false;
6959 :
6960 : // Old space strings should be externalized.
6961 : i::Isolate* isolate = obj->GetIsolate();
6962 24 : return !isolate->heap()->new_space()->Contains(*obj);
6963 : }
6964 :
6965 :
6966 0 : Isolate* v8::Object::GetIsolate() {
6967 : i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate();
6968 0 : return reinterpret_cast<Isolate*>(i_isolate);
6969 : }
6970 :
6971 :
6972 88085 : Local<v8::Object> v8::Object::New(Isolate* isolate) {
6973 88085 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6974 176170 : LOG_API(i_isolate, Object, New);
6975 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6976 : i::Handle<i::JSObject> obj =
6977 88085 : i_isolate->factory()->NewJSObject(i_isolate->object_function());
6978 88085 : return Utils::ToLocal(obj);
6979 : }
6980 :
6981 :
6982 7 : Local<v8::Value> v8::NumberObject::New(Isolate* isolate, double value) {
6983 7 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6984 14 : LOG_API(i_isolate, NumberObject, New);
6985 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6986 7 : i::Handle<i::Object> number = i_isolate->factory()->NewNumber(value);
6987 : i::Handle<i::Object> obj =
6988 14 : i::Object::ToObject(i_isolate, number).ToHandleChecked();
6989 7 : return Utils::ToLocal(obj);
6990 : }
6991 :
6992 :
6993 34 : double v8::NumberObject::ValueOf() const {
6994 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
6995 : i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
6996 34 : i::Isolate* isolate = jsvalue->GetIsolate();
6997 68 : LOG_API(isolate, NumberObject, NumberValue);
6998 34 : return jsvalue->value()->Number();
6999 : }
7000 :
7001 :
7002 28 : Local<v8::Value> v8::BooleanObject::New(Isolate* isolate, bool value) {
7003 28 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7004 56 : LOG_API(i_isolate, BooleanObject, New);
7005 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
7006 14 : i::Handle<i::Object> boolean(value ? i_isolate->heap()->true_value()
7007 14 : : i_isolate->heap()->false_value(),
7008 28 : i_isolate);
7009 : i::Handle<i::Object> obj =
7010 56 : i::Object::ToObject(i_isolate, boolean).ToHandleChecked();
7011 28 : return Utils::ToLocal(obj);
7012 : }
7013 :
7014 :
7015 0 : Local<v8::Value> v8::BooleanObject::New(bool value) {
7016 0 : return New(Isolate::GetCurrent(), value);
7017 : }
7018 :
7019 :
7020 55 : bool v8::BooleanObject::ValueOf() const {
7021 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
7022 : i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
7023 55 : i::Isolate* isolate = jsvalue->GetIsolate();
7024 110 : LOG_API(isolate, BooleanObject, BooleanValue);
7025 55 : return jsvalue->value()->IsTrue(isolate);
7026 : }
7027 :
7028 :
7029 14 : Local<v8::Value> v8::StringObject::New(Local<String> value) {
7030 : i::Handle<i::String> string = Utils::OpenHandle(*value);
7031 14 : i::Isolate* isolate = string->GetIsolate();
7032 28 : LOG_API(isolate, StringObject, New);
7033 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
7034 : i::Handle<i::Object> obj =
7035 28 : i::Object::ToObject(isolate, string).ToHandleChecked();
7036 14 : return Utils::ToLocal(obj);
7037 : }
7038 :
7039 :
7040 34 : Local<v8::String> v8::StringObject::ValueOf() const {
7041 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
7042 : i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
7043 34 : i::Isolate* isolate = jsvalue->GetIsolate();
7044 68 : LOG_API(isolate, StringObject, StringValue);
7045 : return Utils::ToLocal(
7046 34 : i::Handle<i::String>(i::String::cast(jsvalue->value())));
7047 : }
7048 :
7049 :
7050 7 : Local<v8::Value> v8::SymbolObject::New(Isolate* isolate, Local<Symbol> value) {
7051 7 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7052 14 : LOG_API(i_isolate, SymbolObject, New);
7053 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
7054 : i::Handle<i::Object> obj = i::Object::ToObject(
7055 14 : i_isolate, Utils::OpenHandle(*value)).ToHandleChecked();
7056 7 : return Utils::ToLocal(obj);
7057 : }
7058 :
7059 :
7060 13 : Local<v8::Symbol> v8::SymbolObject::ValueOf() const {
7061 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
7062 : i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
7063 13 : i::Isolate* isolate = jsvalue->GetIsolate();
7064 26 : LOG_API(isolate, SymbolObject, SymbolValue);
7065 : return Utils::ToLocal(
7066 13 : i::Handle<i::Symbol>(i::Symbol::cast(jsvalue->value())));
7067 : }
7068 :
7069 :
7070 154 : MaybeLocal<v8::Value> v8::Date::New(Local<Context> context, double time) {
7071 154 : if (std::isnan(time)) {
7072 : // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
7073 : time = std::numeric_limits<double>::quiet_NaN();
7074 : }
7075 616 : PREPARE_FOR_EXECUTION(context, Date, New, Value);
7076 : Local<Value> result;
7077 : has_pending_exception = !ToLocal<Value>(
7078 : i::JSDate::New(isolate->date_function(), isolate->date_function(), time),
7079 462 : &result);
7080 154 : RETURN_ON_FAILED_EXECUTION(Value);
7081 154 : RETURN_ESCAPED(result);
7082 : }
7083 :
7084 :
7085 0 : Local<v8::Value> v8::Date::New(Isolate* isolate, double time) {
7086 0 : auto context = isolate->GetCurrentContext();
7087 0 : RETURN_TO_LOCAL_UNCHECKED(New(context, time), Value);
7088 : }
7089 :
7090 :
7091 11 : double v8::Date::ValueOf() const {
7092 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
7093 : i::Handle<i::JSDate> jsdate = i::Handle<i::JSDate>::cast(obj);
7094 11 : i::Isolate* isolate = jsdate->GetIsolate();
7095 22 : LOG_API(isolate, Date, NumberValue);
7096 11 : return jsdate->value()->Number();
7097 : }
7098 :
7099 :
7100 7 : void v8::Date::DateTimeConfigurationChangeNotification(Isolate* isolate) {
7101 21 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7102 14 : LOG_API(i_isolate, Date, DateTimeConfigurationChangeNotification);
7103 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
7104 7 : i_isolate->date_cache()->ResetDateCache();
7105 7 : if (!i_isolate->eternal_handles()->Exists(
7106 : i::EternalHandles::DATE_CACHE_VERSION)) {
7107 7 : return;
7108 : }
7109 : i::Handle<i::FixedArray> date_cache_version =
7110 : i::Handle<i::FixedArray>::cast(i_isolate->eternal_handles()->GetSingleton(
7111 : i::EternalHandles::DATE_CACHE_VERSION));
7112 : DCHECK_EQ(1, date_cache_version->length());
7113 7 : CHECK(date_cache_version->get(0)->IsSmi());
7114 : date_cache_version->set(
7115 : 0,
7116 7 : i::Smi::FromInt(i::Smi::cast(date_cache_version->get(0))->value() + 1));
7117 : }
7118 :
7119 :
7120 104 : MaybeLocal<v8::RegExp> v8::RegExp::New(Local<Context> context,
7121 : Local<String> pattern, Flags flags) {
7122 416 : PREPARE_FOR_EXECUTION(context, RegExp, New, RegExp);
7123 : Local<v8::RegExp> result;
7124 : has_pending_exception =
7125 : !ToLocal<RegExp>(i::JSRegExp::New(Utils::OpenHandle(*pattern),
7126 : static_cast<i::JSRegExp::Flags>(flags)),
7127 312 : &result);
7128 111 : RETURN_ON_FAILED_EXECUTION(RegExp);
7129 97 : RETURN_ESCAPED(result);
7130 : }
7131 :
7132 :
7133 0 : Local<v8::RegExp> v8::RegExp::New(Local<String> pattern, Flags flags) {
7134 : auto isolate =
7135 : reinterpret_cast<Isolate*>(Utils::OpenHandle(*pattern)->GetIsolate());
7136 0 : auto context = isolate->GetCurrentContext();
7137 0 : RETURN_TO_LOCAL_UNCHECKED(New(context, pattern, flags), RegExp);
7138 : }
7139 :
7140 :
7141 48 : Local<v8::String> v8::RegExp::GetSource() const {
7142 : i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
7143 48 : return Utils::ToLocal(i::Handle<i::String>(obj->Pattern()));
7144 : }
7145 :
7146 :
7147 : // Assert that the static flags cast in GetFlags is valid.
7148 : #define REGEXP_FLAG_ASSERT_EQ(flag) \
7149 : STATIC_ASSERT(static_cast<int>(v8::RegExp::flag) == \
7150 : static_cast<int>(i::JSRegExp::flag))
7151 : REGEXP_FLAG_ASSERT_EQ(kNone);
7152 : REGEXP_FLAG_ASSERT_EQ(kGlobal);
7153 : REGEXP_FLAG_ASSERT_EQ(kIgnoreCase);
7154 : REGEXP_FLAG_ASSERT_EQ(kMultiline);
7155 : REGEXP_FLAG_ASSERT_EQ(kSticky);
7156 : REGEXP_FLAG_ASSERT_EQ(kUnicode);
7157 : #undef REGEXP_FLAG_ASSERT_EQ
7158 :
7159 48 : v8::RegExp::Flags v8::RegExp::GetFlags() const {
7160 : i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
7161 48 : return RegExp::Flags(static_cast<int>(obj->GetFlags()));
7162 : }
7163 :
7164 :
7165 94414 : Local<v8::Array> v8::Array::New(Isolate* isolate, int length) {
7166 94414 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7167 188828 : LOG_API(i_isolate, Array, New);
7168 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
7169 94414 : int real_length = length > 0 ? length : 0;
7170 94414 : i::Handle<i::JSArray> obj = i_isolate->factory()->NewJSArray(real_length);
7171 : i::Handle<i::Object> length_obj =
7172 94414 : i_isolate->factory()->NewNumberFromInt(real_length);
7173 94414 : obj->set_length(*length_obj);
7174 94414 : return Utils::ToLocal(obj);
7175 : }
7176 :
7177 :
7178 9985814 : uint32_t v8::Array::Length() const {
7179 : i::Handle<i::JSArray> obj = Utils::OpenHandle(this);
7180 : i::Object* length = obj->length();
7181 9985814 : if (length->IsSmi()) {
7182 9985814 : return i::Smi::cast(length)->value();
7183 : } else {
7184 0 : return static_cast<uint32_t>(length->Number());
7185 : }
7186 : }
7187 :
7188 :
7189 0 : MaybeLocal<Object> Array::CloneElementAt(Local<Context> context,
7190 : uint32_t index) {
7191 0 : PREPARE_FOR_EXECUTION(context, Array, CloneElementAt, Object);
7192 : auto self = Utils::OpenHandle(this);
7193 0 : if (!self->HasFastObjectElements()) return Local<Object>();
7194 : i::FixedArray* elms = i::FixedArray::cast(self->elements());
7195 0 : i::Object* paragon = elms->get(index);
7196 0 : if (!paragon->IsJSObject()) return Local<Object>();
7197 : i::Handle<i::JSObject> paragon_handle(i::JSObject::cast(paragon));
7198 : Local<Object> result;
7199 : has_pending_exception =
7200 : !ToLocal<Object>(isolate->factory()->CopyJSObject(paragon_handle),
7201 0 : &result);
7202 0 : RETURN_ON_FAILED_EXECUTION(Object);
7203 0 : RETURN_ESCAPED(result);
7204 : }
7205 :
7206 :
7207 0 : Local<Object> Array::CloneElementAt(uint32_t index) { return Local<Object>(); }
7208 :
7209 :
7210 6 : Local<v8::Map> v8::Map::New(Isolate* isolate) {
7211 6 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7212 12 : LOG_API(i_isolate, Map, New);
7213 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
7214 6 : i::Handle<i::JSMap> obj = i_isolate->factory()->NewJSMap();
7215 6 : return Utils::ToLocal(obj);
7216 : }
7217 :
7218 :
7219 42 : size_t v8::Map::Size() const {
7220 : i::Handle<i::JSMap> obj = Utils::OpenHandle(this);
7221 42 : return i::OrderedHashMap::cast(obj->table())->NumberOfElements();
7222 : }
7223 :
7224 :
7225 6 : void Map::Clear() {
7226 : auto self = Utils::OpenHandle(this);
7227 6 : i::Isolate* isolate = self->GetIsolate();
7228 12 : LOG_API(isolate, Map, Clear);
7229 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
7230 6 : i::JSMap::Clear(self);
7231 6 : }
7232 :
7233 :
7234 18 : MaybeLocal<Value> Map::Get(Local<Context> context, Local<Value> key) {
7235 72 : PREPARE_FOR_EXECUTION(context, Map, Get, Value);
7236 : auto self = Utils::OpenHandle(this);
7237 : Local<Value> result;
7238 18 : i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
7239 : has_pending_exception =
7240 : !ToLocal<Value>(i::Execution::Call(isolate, isolate->map_get(), self,
7241 : arraysize(argv), argv),
7242 54 : &result);
7243 18 : RETURN_ON_FAILED_EXECUTION(Value);
7244 18 : RETURN_ESCAPED(result);
7245 : }
7246 :
7247 :
7248 6 : MaybeLocal<Map> Map::Set(Local<Context> context, Local<Value> key,
7249 : Local<Value> value) {
7250 24 : PREPARE_FOR_EXECUTION(context, Map, Set, Map);
7251 : auto self = Utils::OpenHandle(this);
7252 : i::Handle<i::Object> result;
7253 : i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key),
7254 12 : Utils::OpenHandle(*value)};
7255 : has_pending_exception = !i::Execution::Call(isolate, isolate->map_set(), self,
7256 : arraysize(argv), argv)
7257 18 : .ToHandle(&result);
7258 6 : RETURN_ON_FAILED_EXECUTION(Map);
7259 6 : RETURN_ESCAPED(Local<Map>::Cast(Utils::ToLocal(result)));
7260 : }
7261 :
7262 :
7263 36 : Maybe<bool> Map::Has(Local<Context> context, Local<Value> key) {
7264 144 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Map, Has, bool);
7265 : auto self = Utils::OpenHandle(this);
7266 : i::Handle<i::Object> result;
7267 36 : i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
7268 : has_pending_exception = !i::Execution::Call(isolate, isolate->map_has(), self,
7269 : arraysize(argv), argv)
7270 108 : .ToHandle(&result);
7271 36 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
7272 : return Just(result->IsTrue(isolate));
7273 : }
7274 :
7275 :
7276 18 : Maybe<bool> Map::Delete(Local<Context> context, Local<Value> key) {
7277 72 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Map, Delete, bool);
7278 : auto self = Utils::OpenHandle(this);
7279 : i::Handle<i::Object> result;
7280 18 : i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
7281 : has_pending_exception = !i::Execution::Call(isolate, isolate->map_delete(),
7282 : self, arraysize(argv), argv)
7283 54 : .ToHandle(&result);
7284 18 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
7285 : return Just(result->IsTrue(isolate));
7286 : }
7287 :
7288 : namespace {
7289 140 : i::Handle<i::JSArray> MapAsArray(i::Isolate* isolate, i::Object* table_obj,
7290 : int offset, int kind) {
7291 : i::Factory* factory = isolate->factory();
7292 : i::Handle<i::OrderedHashMap> table(i::OrderedHashMap::cast(table_obj));
7293 140 : if (offset >= table->NumberOfElements()) return factory->NewJSArray(0);
7294 120 : int length = (table->NumberOfElements() - offset) *
7295 120 : (kind == i::JSMapIterator::kKindEntries ? 2 : 1);
7296 120 : i::Handle<i::FixedArray> result = factory->NewFixedArray(length);
7297 : int result_index = 0;
7298 : {
7299 : i::DisallowHeapAllocation no_gc;
7300 120 : int capacity = table->UsedCapacity();
7301 120 : i::Oddball* the_hole = isolate->heap()->the_hole_value();
7302 6390 : for (int i = 0; i < capacity; ++i) {
7303 6270 : i::Object* key = table->KeyAt(i);
7304 6270 : if (key == the_hole) continue;
7305 6264 : if (offset-- > 0) continue;
7306 12492 : if (kind == i::JSMapIterator::kKindEntries ||
7307 6246 : kind == i::JSMapIterator::kKindKeys) {
7308 12424 : result->set(result_index++, key);
7309 : }
7310 6246 : if (kind == i::JSMapIterator::kKindEntries ||
7311 : kind == i::JSMapIterator::kKindValues) {
7312 12424 : result->set(result_index++, table->ValueAt(i));
7313 : }
7314 : }
7315 : }
7316 : DCHECK_EQ(result_index, result->length());
7317 : DCHECK_EQ(result_index, length);
7318 120 : return factory->NewJSArrayWithElements(result, i::FAST_ELEMENTS, length);
7319 : }
7320 : } // namespace
7321 :
7322 62 : Local<Array> Map::AsArray() const {
7323 : i::Handle<i::JSMap> obj = Utils::OpenHandle(this);
7324 62 : i::Isolate* isolate = obj->GetIsolate();
7325 124 : LOG_API(isolate, Map, AsArray);
7326 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
7327 : return Utils::ToLocal(
7328 124 : MapAsArray(isolate, obj->table(), 0, i::JSMapIterator::kKindEntries));
7329 : }
7330 :
7331 :
7332 414 : Local<v8::Set> v8::Set::New(Isolate* isolate) {
7333 414 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7334 828 : LOG_API(i_isolate, Set, New);
7335 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
7336 414 : i::Handle<i::JSSet> obj = i_isolate->factory()->NewJSSet();
7337 414 : return Utils::ToLocal(obj);
7338 : }
7339 :
7340 :
7341 42 : size_t v8::Set::Size() const {
7342 : i::Handle<i::JSSet> obj = Utils::OpenHandle(this);
7343 42 : return i::OrderedHashSet::cast(obj->table())->NumberOfElements();
7344 : }
7345 :
7346 :
7347 6 : void Set::Clear() {
7348 : auto self = Utils::OpenHandle(this);
7349 6 : i::Isolate* isolate = self->GetIsolate();
7350 12 : LOG_API(isolate, Set, Clear);
7351 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
7352 6 : i::JSSet::Clear(self);
7353 6 : }
7354 :
7355 :
7356 6954 : MaybeLocal<Set> Set::Add(Local<Context> context, Local<Value> key) {
7357 27816 : PREPARE_FOR_EXECUTION(context, Set, Add, Set);
7358 : auto self = Utils::OpenHandle(this);
7359 : i::Handle<i::Object> result;
7360 6954 : i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
7361 : has_pending_exception = !i::Execution::Call(isolate, isolate->set_add(), self,
7362 : arraysize(argv), argv)
7363 20862 : .ToHandle(&result);
7364 6954 : RETURN_ON_FAILED_EXECUTION(Set);
7365 6954 : RETURN_ESCAPED(Local<Set>::Cast(Utils::ToLocal(result)));
7366 : }
7367 :
7368 :
7369 36 : Maybe<bool> Set::Has(Local<Context> context, Local<Value> key) {
7370 144 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Set, Has, bool);
7371 : auto self = Utils::OpenHandle(this);
7372 : i::Handle<i::Object> result;
7373 36 : i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
7374 : has_pending_exception = !i::Execution::Call(isolate, isolate->set_has(), self,
7375 : arraysize(argv), argv)
7376 108 : .ToHandle(&result);
7377 36 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
7378 : return Just(result->IsTrue(isolate));
7379 : }
7380 :
7381 :
7382 84 : Maybe<bool> Set::Delete(Local<Context> context, Local<Value> key) {
7383 336 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Set, Delete, bool);
7384 : auto self = Utils::OpenHandle(this);
7385 : i::Handle<i::Object> result;
7386 84 : i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
7387 : has_pending_exception = !i::Execution::Call(isolate, isolate->set_delete(),
7388 : self, arraysize(argv), argv)
7389 252 : .ToHandle(&result);
7390 84 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
7391 : return Just(result->IsTrue(isolate));
7392 : }
7393 :
7394 : namespace {
7395 510 : i::Handle<i::JSArray> SetAsArray(i::Isolate* isolate, i::Object* table_obj,
7396 : int offset) {
7397 : i::Factory* factory = isolate->factory();
7398 : i::Handle<i::OrderedHashSet> table(i::OrderedHashSet::cast(table_obj));
7399 510 : int length = table->NumberOfElements() - offset;
7400 510 : if (length <= 0) return factory->NewJSArray(0);
7401 504 : i::Handle<i::FixedArray> result = factory->NewFixedArray(length);
7402 : int result_index = 0;
7403 : {
7404 : i::DisallowHeapAllocation no_gc;
7405 504 : int capacity = table->UsedCapacity();
7406 504 : i::Oddball* the_hole = isolate->heap()->the_hole_value();
7407 13776 : for (int i = 0; i < capacity; ++i) {
7408 13272 : i::Object* key = table->KeyAt(i);
7409 13272 : if (key == the_hole) continue;
7410 13200 : if (offset-- > 0) continue;
7411 26364 : result->set(result_index++, key);
7412 : }
7413 : }
7414 : DCHECK_EQ(result_index, result->length());
7415 : DCHECK_EQ(result_index, length);
7416 504 : return factory->NewJSArrayWithElements(result, i::FAST_ELEMENTS, length);
7417 : }
7418 : } // namespace
7419 :
7420 474 : Local<Array> Set::AsArray() const {
7421 : i::Handle<i::JSSet> obj = Utils::OpenHandle(this);
7422 474 : i::Isolate* isolate = obj->GetIsolate();
7423 948 : LOG_API(isolate, Set, AsArray);
7424 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
7425 948 : return Utils::ToLocal(SetAsArray(isolate, obj->table(), 0));
7426 : }
7427 :
7428 :
7429 12486 : MaybeLocal<Promise::Resolver> Promise::Resolver::New(Local<Context> context) {
7430 49944 : PREPARE_FOR_EXECUTION(context, Promise_Resolver, New, Resolver);
7431 : i::Handle<i::Object> result;
7432 : has_pending_exception =
7433 : !i::Execution::Call(isolate, isolate->promise_internal_constructor(),
7434 : isolate->factory()->undefined_value(), 0, NULL)
7435 37458 : .ToHandle(&result);
7436 12501 : RETURN_ON_FAILED_EXECUTION(Promise::Resolver);
7437 12471 : RETURN_ESCAPED(Local<Promise::Resolver>::Cast(Utils::ToLocal(result)));
7438 : }
7439 :
7440 :
7441 0 : Local<Promise::Resolver> Promise::Resolver::New(Isolate* isolate) {
7442 0 : RETURN_TO_LOCAL_UNCHECKED(New(isolate->GetCurrentContext()),
7443 : Promise::Resolver);
7444 : }
7445 :
7446 :
7447 13395 : Local<Promise> Promise::Resolver::GetPromise() {
7448 : i::Handle<i::JSReceiver> promise = Utils::OpenHandle(this);
7449 13395 : return Local<Promise>::Cast(Utils::ToLocal(promise));
7450 : }
7451 :
7452 :
7453 960 : Maybe<bool> Promise::Resolver::Resolve(Local<Context> context,
7454 : Local<Value> value) {
7455 3840 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Promise_Resolver, Resolve, bool);
7456 : auto self = Utils::OpenHandle(this);
7457 960 : i::Handle<i::Object> argv[] = {self, Utils::OpenHandle(*value)};
7458 : has_pending_exception =
7459 : i::Execution::Call(isolate, isolate->promise_resolve(),
7460 : isolate->factory()->undefined_value(), arraysize(argv),
7461 : argv)
7462 2880 : .is_null();
7463 960 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
7464 : return Just(true);
7465 : }
7466 :
7467 :
7468 0 : void Promise::Resolver::Resolve(Local<Value> value) {
7469 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
7470 0 : USE(Resolve(context, value));
7471 0 : }
7472 :
7473 :
7474 10836 : Maybe<bool> Promise::Resolver::Reject(Local<Context> context,
7475 : Local<Value> value) {
7476 43344 : PREPARE_FOR_EXECUTION_PRIMITIVE(context, Promise_Resolver, Resolve, bool);
7477 : auto self = Utils::OpenHandle(this);
7478 :
7479 : // We pass true to trigger the debugger's on exception handler.
7480 : i::Handle<i::Object> argv[] = {self, Utils::OpenHandle(*value),
7481 10836 : isolate->factory()->ToBoolean(true)};
7482 : has_pending_exception =
7483 : i::Execution::Call(isolate, isolate->promise_internal_reject(),
7484 : isolate->factory()->undefined_value(), arraysize(argv),
7485 : argv)
7486 32508 : .is_null();
7487 10836 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
7488 : return Just(true);
7489 : }
7490 :
7491 :
7492 0 : void Promise::Resolver::Reject(Local<Value> value) {
7493 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
7494 0 : USE(Reject(context, value));
7495 0 : }
7496 :
7497 :
7498 432 : MaybeLocal<Promise> Promise::Catch(Local<Context> context,
7499 : Local<Function> handler) {
7500 1728 : PREPARE_FOR_EXECUTION(context, Promise, Catch, Promise);
7501 : auto self = Utils::OpenHandle(this);
7502 : i::Handle<i::Object> argv[] = { Utils::OpenHandle(*handler) };
7503 : i::Handle<i::Object> result;
7504 : has_pending_exception = !i::Execution::Call(isolate, isolate->promise_catch(),
7505 : self, arraysize(argv), argv)
7506 1296 : .ToHandle(&result);
7507 432 : RETURN_ON_FAILED_EXECUTION(Promise);
7508 432 : RETURN_ESCAPED(Local<Promise>::Cast(Utils::ToLocal(result)));
7509 : }
7510 :
7511 :
7512 0 : Local<Promise> Promise::Catch(Local<Function> handler) {
7513 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
7514 0 : RETURN_TO_LOCAL_UNCHECKED(Catch(context, handler), Promise);
7515 : }
7516 :
7517 :
7518 1296 : MaybeLocal<Promise> Promise::Then(Local<Context> context,
7519 : Local<Function> handler) {
7520 5184 : PREPARE_FOR_EXECUTION(context, Promise, Then, Promise);
7521 : auto self = Utils::OpenHandle(this);
7522 : i::Handle<i::Object> argv[] = { Utils::OpenHandle(*handler) };
7523 : i::Handle<i::Object> result;
7524 : has_pending_exception = !i::Execution::Call(isolate, isolate->promise_then(),
7525 : self, arraysize(argv), argv)
7526 3888 : .ToHandle(&result);
7527 1296 : RETURN_ON_FAILED_EXECUTION(Promise);
7528 1296 : RETURN_ESCAPED(Local<Promise>::Cast(Utils::ToLocal(result)));
7529 : }
7530 :
7531 :
7532 0 : Local<Promise> Promise::Then(Local<Function> handler) {
7533 0 : auto context = ContextFromHeapObject(Utils::OpenHandle(this));
7534 0 : RETURN_TO_LOCAL_UNCHECKED(Then(context, handler), Promise);
7535 : }
7536 :
7537 :
7538 372 : bool Promise::HasHandler() {
7539 : i::Handle<i::JSReceiver> promise = Utils::OpenHandle(this);
7540 372 : i::Isolate* isolate = promise->GetIsolate();
7541 744 : LOG_API(isolate, Promise, HasRejectHandler);
7542 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
7543 372 : if (promise->IsJSPromise()) {
7544 : i::Handle<i::JSPromise> js_promise = i::Handle<i::JSPromise>::cast(promise);
7545 : return js_promise->has_handler();
7546 : }
7547 : return false;
7548 : }
7549 :
7550 12 : Local<Value> Promise::Result() {
7551 : i::Handle<i::JSReceiver> promise = Utils::OpenHandle(this);
7552 12 : i::Isolate* isolate = promise->GetIsolate();
7553 24 : LOG_API(isolate, Promise, Result);
7554 : i::Handle<i::JSPromise> js_promise = i::Handle<i::JSPromise>::cast(promise);
7555 : Utils::ApiCheck(js_promise->status() != kPending, "v8_Promise_Result",
7556 : "Promise is still pending");
7557 : i::Handle<i::Object> result(js_promise->result(), isolate);
7558 12 : return Utils::ToLocal(result);
7559 : }
7560 :
7561 24 : Promise::PromiseState Promise::State() {
7562 : i::Handle<i::JSReceiver> promise = Utils::OpenHandle(this);
7563 24 : i::Isolate* isolate = promise->GetIsolate();
7564 48 : LOG_API(isolate, Promise, Status);
7565 : i::Handle<i::JSPromise> js_promise = i::Handle<i::JSPromise>::cast(promise);
7566 48 : return static_cast<PromiseState>(js_promise->status());
7567 : }
7568 :
7569 12 : Local<Object> Proxy::GetTarget() {
7570 : i::Handle<i::JSProxy> self = Utils::OpenHandle(this);
7571 : i::Handle<i::JSReceiver> target(self->target());
7572 12 : return Utils::ToLocal(target);
7573 : }
7574 :
7575 :
7576 12 : Local<Value> Proxy::GetHandler() {
7577 : i::Handle<i::JSProxy> self = Utils::OpenHandle(this);
7578 : i::Handle<i::Object> handler(self->handler(), self->GetIsolate());
7579 12 : return Utils::ToLocal(handler);
7580 : }
7581 :
7582 :
7583 12 : bool Proxy::IsRevoked() {
7584 : i::Handle<i::JSProxy> self = Utils::OpenHandle(this);
7585 12 : return self->IsRevoked();
7586 : }
7587 :
7588 :
7589 6 : void Proxy::Revoke() {
7590 6 : i::Handle<i::JSProxy> self = Utils::OpenHandle(this);
7591 6 : i::JSProxy::Revoke(self);
7592 6 : }
7593 :
7594 :
7595 20 : MaybeLocal<Proxy> Proxy::New(Local<Context> context, Local<Object> local_target,
7596 : Local<Object> local_handler) {
7597 80 : PREPARE_FOR_EXECUTION(context, Proxy, New, Proxy);
7598 : i::Handle<i::JSReceiver> target = Utils::OpenHandle(*local_target);
7599 : i::Handle<i::JSReceiver> handler = Utils::OpenHandle(*local_handler);
7600 : Local<Proxy> result;
7601 : has_pending_exception =
7602 40 : !ToLocal<Proxy>(i::JSProxy::New(isolate, target, handler), &result);
7603 20 : RETURN_ON_FAILED_EXECUTION(Proxy);
7604 20 : RETURN_ESCAPED(result);
7605 : }
7606 :
7607 93 : Local<String> WasmCompiledModule::GetWasmWireBytes() {
7608 : i::Handle<i::JSObject> obj =
7609 : i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
7610 : i::Handle<i::WasmCompiledModule> compiled_part =
7611 : i::handle(i::WasmCompiledModule::cast(obj->GetEmbedderField(0)));
7612 93 : i::Handle<i::String> wire_bytes(compiled_part->module_bytes());
7613 93 : return Local<String>::Cast(Utils::ToLocal(wire_bytes));
7614 : }
7615 :
7616 : // Currently, wasm modules are bound, both to Isolate and to
7617 : // the Context they were created in. The currently-supported means to
7618 : // decontextualize and then re-contextualize a module is via
7619 : // serialization/deserialization.
7620 : WasmCompiledModule::TransferrableModule
7621 13 : WasmCompiledModule::GetTransferrableModule() {
7622 : i::DisallowHeapAllocation no_gc;
7623 13 : WasmCompiledModule::SerializedModule compiled_part = Serialize();
7624 :
7625 13 : Local<String> wire_bytes = GetWasmWireBytes();
7626 13 : size_t wire_size = static_cast<size_t>(wire_bytes->Length());
7627 13 : uint8_t* bytes = new uint8_t[wire_size];
7628 : wire_bytes->WriteOneByte(bytes, 0, wire_bytes->Length());
7629 :
7630 : return TransferrableModule(
7631 : std::move(compiled_part),
7632 : std::make_pair(
7633 : std::unique_ptr<const uint8_t[]>(const_cast<const uint8_t*>(bytes)),
7634 13 : wire_size));
7635 : }
7636 :
7637 11 : MaybeLocal<WasmCompiledModule> WasmCompiledModule::FromTransferrableModule(
7638 : Isolate* isolate,
7639 : const WasmCompiledModule::TransferrableModule& transferrable_module) {
7640 : MaybeLocal<WasmCompiledModule> ret =
7641 : Deserialize(isolate, AsCallerOwned(transferrable_module.compiled_code),
7642 22 : AsCallerOwned(transferrable_module.wire_bytes));
7643 11 : return ret;
7644 : }
7645 :
7646 55 : WasmCompiledModule::SerializedModule WasmCompiledModule::Serialize() {
7647 : i::Handle<i::JSObject> obj =
7648 : i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
7649 : i::Handle<i::WasmCompiledModule> compiled_part =
7650 : i::handle(i::WasmCompiledModule::cast(obj->GetEmbedderField(0)));
7651 :
7652 : std::unique_ptr<i::ScriptData> script_data =
7653 : i::WasmCompiledModuleSerializer::SerializeWasmModule(obj->GetIsolate(),
7654 55 : compiled_part);
7655 : script_data->ReleaseDataOwnership();
7656 :
7657 55 : size_t size = static_cast<size_t>(script_data->length());
7658 110 : return {std::unique_ptr<const uint8_t[]>(script_data->data()), size};
7659 : }
7660 :
7661 65 : MaybeLocal<WasmCompiledModule> WasmCompiledModule::Deserialize(
7662 : Isolate* isolate,
7663 : const WasmCompiledModule::CallerOwnedBuffer& serialized_module,
7664 : const WasmCompiledModule::CallerOwnedBuffer& wire_bytes) {
7665 65 : int size = static_cast<int>(serialized_module.second);
7666 65 : i::ScriptData sc(serialized_module.first, size);
7667 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7668 : i::MaybeHandle<i::FixedArray> maybe_compiled_part =
7669 : i::WasmCompiledModuleSerializer::DeserializeWasmModule(
7670 : i_isolate, &sc,
7671 130 : {wire_bytes.first, static_cast<int>(wire_bytes.second)});
7672 : i::Handle<i::FixedArray> compiled_part;
7673 65 : if (!maybe_compiled_part.ToHandle(&compiled_part)) {
7674 47 : return MaybeLocal<WasmCompiledModule>();
7675 : }
7676 : i::Handle<i::WasmCompiledModule> compiled_module =
7677 18 : handle(i::WasmCompiledModule::cast(*compiled_part));
7678 : return Local<WasmCompiledModule>::Cast(
7679 : Utils::ToLocal(i::Handle<i::JSObject>::cast(
7680 18 : i::WasmModuleObject::New(i_isolate, compiled_module))));
7681 : }
7682 :
7683 54 : MaybeLocal<WasmCompiledModule> WasmCompiledModule::DeserializeOrCompile(
7684 : Isolate* isolate,
7685 : const WasmCompiledModule::CallerOwnedBuffer& serialized_module,
7686 : const WasmCompiledModule::CallerOwnedBuffer& wire_bytes) {
7687 : MaybeLocal<WasmCompiledModule> ret =
7688 54 : Deserialize(isolate, serialized_module, wire_bytes);
7689 54 : if (!ret.IsEmpty()) {
7690 7 : return ret;
7691 : }
7692 47 : return Compile(isolate, wire_bytes.first, wire_bytes.second);
7693 : }
7694 :
7695 61 : MaybeLocal<WasmCompiledModule> WasmCompiledModule::Compile(Isolate* isolate,
7696 : const uint8_t* start,
7697 : size_t length) {
7698 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7699 : i::wasm::ErrorThrower thrower(i_isolate, "WasmCompiledModule::Deserialize()");
7700 : i::MaybeHandle<i::JSObject> maybe_compiled = i::wasm::SyncCompile(
7701 183 : i_isolate, &thrower, i::wasm::ModuleWireBytes(start, start + length));
7702 61 : if (maybe_compiled.is_null()) return MaybeLocal<WasmCompiledModule>();
7703 : return Local<WasmCompiledModule>::Cast(
7704 40 : Utils::ToLocal(maybe_compiled.ToHandleChecked()));
7705 : }
7706 :
7707 28 : void WasmModuleObjectBuilder::OnBytesReceived(const uint8_t* bytes,
7708 : size_t size) {
7709 28 : std::unique_ptr<uint8_t[]> cloned_bytes(new uint8_t[size]);
7710 : memcpy(cloned_bytes.get(), bytes, size);
7711 : received_buffers_.push_back(
7712 : Buffer(std::unique_ptr<const uint8_t[]>(
7713 : const_cast<const uint8_t*>(cloned_bytes.release())),
7714 56 : size));
7715 28 : total_size_ += size;
7716 28 : }
7717 :
7718 14 : MaybeLocal<WasmCompiledModule> WasmModuleObjectBuilder::Finish() {
7719 14 : std::unique_ptr<uint8_t[]> wire_bytes(new uint8_t[total_size_]);
7720 : uint8_t* insert_at = wire_bytes.get();
7721 :
7722 84 : for (size_t i = 0; i < received_buffers_.size(); ++i) {
7723 42 : const Buffer& buff = received_buffers_[i];
7724 28 : memcpy(insert_at, buff.first.get(), buff.second);
7725 28 : insert_at += buff.second;
7726 : }
7727 28 : return WasmCompiledModule::Compile(isolate_, wire_bytes.get(), total_size_);
7728 : }
7729 :
7730 : // static
7731 3829 : v8::ArrayBuffer::Allocator* v8::ArrayBuffer::Allocator::NewDefaultAllocator() {
7732 7658 : return new ArrayBufferAllocator();
7733 : }
7734 :
7735 90 : bool v8::ArrayBuffer::IsExternal() const {
7736 90 : return Utils::OpenHandle(this)->is_external();
7737 : }
7738 :
7739 :
7740 56 : bool v8::ArrayBuffer::IsNeuterable() const {
7741 56 : return Utils::OpenHandle(this)->is_neuterable();
7742 : }
7743 :
7744 :
7745 90 : v8::ArrayBuffer::Contents v8::ArrayBuffer::Externalize() {
7746 : i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
7747 : i::Isolate* isolate = self->GetIsolate();
7748 : Utils::ApiCheck(!self->is_external(), "v8_ArrayBuffer_Externalize",
7749 : "ArrayBuffer already externalized");
7750 : self->set_is_external(true);
7751 90 : isolate->heap()->UnregisterArrayBuffer(*self);
7752 :
7753 90 : return GetContents();
7754 : }
7755 :
7756 :
7757 13188 : v8::ArrayBuffer::Contents v8::ArrayBuffer::GetContents() {
7758 : i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
7759 13278 : size_t byte_length = static_cast<size_t>(self->byte_length()->Number());
7760 : Contents contents;
7761 : contents.data_ = self->backing_store();
7762 : contents.byte_length_ = byte_length;
7763 13188 : return contents;
7764 : }
7765 :
7766 :
7767 53 : void v8::ArrayBuffer::Neuter() {
7768 : i::Handle<i::JSArrayBuffer> obj = Utils::OpenHandle(this);
7769 53 : i::Isolate* isolate = obj->GetIsolate();
7770 : Utils::ApiCheck(obj->is_external(),
7771 : "v8::ArrayBuffer::Neuter",
7772 : "Only externalized ArrayBuffers can be neutered");
7773 : Utils::ApiCheck(obj->is_neuterable(), "v8::ArrayBuffer::Neuter",
7774 : "Only neuterable ArrayBuffers can be neutered");
7775 106 : LOG_API(isolate, ArrayBuffer, Neuter);
7776 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
7777 53 : obj->Neuter();
7778 53 : }
7779 :
7780 :
7781 152 : size_t v8::ArrayBuffer::ByteLength() const {
7782 : i::Handle<i::JSArrayBuffer> obj = Utils::OpenHandle(this);
7783 152 : return static_cast<size_t>(obj->byte_length()->Number());
7784 : }
7785 :
7786 :
7787 110 : Local<ArrayBuffer> v8::ArrayBuffer::New(Isolate* isolate, size_t byte_length) {
7788 110 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7789 220 : LOG_API(i_isolate, ArrayBuffer, New);
7790 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
7791 : i::Handle<i::JSArrayBuffer> obj =
7792 110 : i_isolate->factory()->NewJSArrayBuffer(i::SharedFlag::kNotShared);
7793 : // TODO(jbroman): It may be useful in the future to provide a MaybeLocal
7794 : // version that throws an exception or otherwise does not crash.
7795 110 : if (!i::JSArrayBuffer::SetupAllocatingData(obj, i_isolate, byte_length)) {
7796 0 : i::FatalProcessOutOfMemory("v8::ArrayBuffer::New");
7797 : }
7798 110 : return Utils::ToLocal(obj);
7799 : }
7800 :
7801 :
7802 173 : Local<ArrayBuffer> v8::ArrayBuffer::New(Isolate* isolate, void* data,
7803 : size_t byte_length,
7804 : ArrayBufferCreationMode mode) {
7805 : // Embedders must guarantee that the external backing store is valid.
7806 173 : CHECK(byte_length == 0 || data != NULL);
7807 173 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7808 346 : LOG_API(i_isolate, ArrayBuffer, New);
7809 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
7810 : i::Handle<i::JSArrayBuffer> obj =
7811 173 : i_isolate->factory()->NewJSArrayBuffer(i::SharedFlag::kNotShared);
7812 : i::JSArrayBuffer::Setup(obj, i_isolate,
7813 : mode == ArrayBufferCreationMode::kExternalized, data,
7814 173 : byte_length);
7815 173 : return Utils::ToLocal(obj);
7816 : }
7817 :
7818 :
7819 1192 : Local<ArrayBuffer> v8::ArrayBufferView::Buffer() {
7820 : i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this);
7821 : i::Handle<i::JSArrayBuffer> buffer;
7822 1192 : if (obj->IsJSDataView()) {
7823 : i::Handle<i::JSDataView> data_view(i::JSDataView::cast(*obj));
7824 : DCHECK(data_view->buffer()->IsJSArrayBuffer());
7825 : buffer = i::handle(i::JSArrayBuffer::cast(data_view->buffer()));
7826 : } else {
7827 : DCHECK(obj->IsJSTypedArray());
7828 1176 : buffer = i::JSTypedArray::cast(*obj)->GetBuffer();
7829 : }
7830 1192 : return Utils::ToLocal(buffer);
7831 : }
7832 :
7833 :
7834 21 : size_t v8::ArrayBufferView::CopyContents(void* dest, size_t byte_length) {
7835 : i::Handle<i::JSArrayBufferView> self = Utils::OpenHandle(this);
7836 21 : size_t byte_offset = i::NumberToSize(self->byte_offset());
7837 : size_t bytes_to_copy =
7838 21 : i::Min(byte_length, i::NumberToSize(self->byte_length()));
7839 21 : if (bytes_to_copy) {
7840 : i::DisallowHeapAllocation no_gc;
7841 : i::Handle<i::JSArrayBuffer> buffer(i::JSArrayBuffer::cast(self->buffer()));
7842 : const char* source = reinterpret_cast<char*>(buffer->backing_store());
7843 21 : if (source == nullptr) {
7844 : DCHECK(self->IsJSTypedArray());
7845 : i::Handle<i::JSTypedArray> typed_array(i::JSTypedArray::cast(*self));
7846 : i::Handle<i::FixedTypedArrayBase> fixed_array(
7847 : i::FixedTypedArrayBase::cast(typed_array->elements()));
7848 : source = reinterpret_cast<char*>(fixed_array->DataPtr());
7849 : }
7850 21 : memcpy(dest, source + byte_offset, bytes_to_copy);
7851 : }
7852 21 : return bytes_to_copy;
7853 : }
7854 :
7855 :
7856 42 : bool v8::ArrayBufferView::HasBuffer() const {
7857 : i::Handle<i::JSArrayBufferView> self = Utils::OpenHandle(this);
7858 : i::Handle<i::JSArrayBuffer> buffer(i::JSArrayBuffer::cast(self->buffer()));
7859 42 : return buffer->backing_store() != nullptr;
7860 : }
7861 :
7862 :
7863 1402 : size_t v8::ArrayBufferView::ByteOffset() {
7864 : i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this);
7865 1402 : return static_cast<size_t>(obj->byte_offset()->Number());
7866 : }
7867 :
7868 :
7869 1419 : size_t v8::ArrayBufferView::ByteLength() {
7870 : i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this);
7871 1419 : return static_cast<size_t>(obj->byte_length()->Number());
7872 : }
7873 :
7874 :
7875 332 : size_t v8::TypedArray::Length() {
7876 : i::Handle<i::JSTypedArray> obj = Utils::OpenHandle(this);
7877 332 : return static_cast<size_t>(obj->length_value());
7878 : }
7879 :
7880 : #define TYPED_ARRAY_NEW(Type, type, TYPE, ctype, size) \
7881 : Local<Type##Array> Type##Array::New(Local<ArrayBuffer> array_buffer, \
7882 : size_t byte_offset, size_t length) { \
7883 : i::Isolate* isolate = Utils::OpenHandle(*array_buffer)->GetIsolate(); \
7884 : LOG_API(isolate, Type##Array, New); \
7885 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate); \
7886 : if (!Utils::ApiCheck(length <= static_cast<size_t>(i::Smi::kMaxValue), \
7887 : "v8::" #Type \
7888 : "Array::New(Local<ArrayBuffer>, size_t, size_t)", \
7889 : "length exceeds max allowed value")) { \
7890 : return Local<Type##Array>(); \
7891 : } \
7892 : i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*array_buffer); \
7893 : i::Handle<i::JSTypedArray> obj = isolate->factory()->NewJSTypedArray( \
7894 : i::kExternal##Type##Array, buffer, byte_offset, length); \
7895 : return Utils::ToLocal##Type##Array(obj); \
7896 : } \
7897 : Local<Type##Array> Type##Array::New( \
7898 : Local<SharedArrayBuffer> shared_array_buffer, size_t byte_offset, \
7899 : size_t length) { \
7900 : CHECK(i::FLAG_harmony_sharedarraybuffer); \
7901 : i::Isolate* isolate = \
7902 : Utils::OpenHandle(*shared_array_buffer)->GetIsolate(); \
7903 : LOG_API(isolate, Type##Array, New); \
7904 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate); \
7905 : if (!Utils::ApiCheck( \
7906 : length <= static_cast<size_t>(i::Smi::kMaxValue), \
7907 : "v8::" #Type \
7908 : "Array::New(Local<SharedArrayBuffer>, size_t, size_t)", \
7909 : "length exceeds max allowed value")) { \
7910 : return Local<Type##Array>(); \
7911 : } \
7912 : i::Handle<i::JSArrayBuffer> buffer = \
7913 : Utils::OpenHandle(*shared_array_buffer); \
7914 : i::Handle<i::JSTypedArray> obj = isolate->factory()->NewJSTypedArray( \
7915 : i::kExternal##Type##Array, buffer, byte_offset, length); \
7916 : return Utils::ToLocal##Type##Array(obj); \
7917 : }
7918 :
7919 784 : TYPED_ARRAYS(TYPED_ARRAY_NEW)
7920 : #undef TYPED_ARRAY_NEW
7921 :
7922 20 : Local<DataView> DataView::New(Local<ArrayBuffer> array_buffer,
7923 : size_t byte_offset, size_t byte_length) {
7924 : i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*array_buffer);
7925 20 : i::Isolate* isolate = buffer->GetIsolate();
7926 40 : LOG_API(isolate, DataView, New);
7927 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
7928 : i::Handle<i::JSDataView> obj =
7929 20 : isolate->factory()->NewJSDataView(buffer, byte_offset, byte_length);
7930 20 : return Utils::ToLocal(obj);
7931 : }
7932 :
7933 :
7934 7 : Local<DataView> DataView::New(Local<SharedArrayBuffer> shared_array_buffer,
7935 : size_t byte_offset, size_t byte_length) {
7936 7 : CHECK(i::FLAG_harmony_sharedarraybuffer);
7937 : i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*shared_array_buffer);
7938 7 : i::Isolate* isolate = buffer->GetIsolate();
7939 14 : LOG_API(isolate, DataView, New);
7940 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
7941 : i::Handle<i::JSDataView> obj =
7942 7 : isolate->factory()->NewJSDataView(buffer, byte_offset, byte_length);
7943 7 : return Utils::ToLocal(obj);
7944 : }
7945 :
7946 :
7947 292 : bool v8::SharedArrayBuffer::IsExternal() const {
7948 292 : return Utils::OpenHandle(this)->is_external();
7949 : }
7950 :
7951 :
7952 100 : v8::SharedArrayBuffer::Contents v8::SharedArrayBuffer::Externalize() {
7953 : i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
7954 : i::Isolate* isolate = self->GetIsolate();
7955 : Utils::ApiCheck(!self->is_external(), "v8_SharedArrayBuffer_Externalize",
7956 : "SharedArrayBuffer already externalized");
7957 : self->set_is_external(true);
7958 100 : isolate->heap()->UnregisterArrayBuffer(*self);
7959 100 : return GetContents();
7960 : }
7961 :
7962 :
7963 171 : v8::SharedArrayBuffer::Contents v8::SharedArrayBuffer::GetContents() {
7964 : i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
7965 271 : size_t byte_length = static_cast<size_t>(self->byte_length()->Number());
7966 : Contents contents;
7967 : contents.data_ = self->backing_store();
7968 : contents.byte_length_ = byte_length;
7969 171 : return contents;
7970 : }
7971 :
7972 :
7973 21 : size_t v8::SharedArrayBuffer::ByteLength() const {
7974 : i::Handle<i::JSArrayBuffer> obj = Utils::OpenHandle(this);
7975 21 : return static_cast<size_t>(obj->byte_length()->Number());
7976 : }
7977 :
7978 :
7979 7 : Local<SharedArrayBuffer> v8::SharedArrayBuffer::New(Isolate* isolate,
7980 : size_t byte_length) {
7981 7 : CHECK(i::FLAG_harmony_sharedarraybuffer);
7982 7 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7983 14 : LOG_API(i_isolate, SharedArrayBuffer, New);
7984 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
7985 : i::Handle<i::JSArrayBuffer> obj =
7986 7 : i_isolate->factory()->NewJSArrayBuffer(i::SharedFlag::kShared);
7987 : // TODO(jbroman): It may be useful in the future to provide a MaybeLocal
7988 : // version that throws an exception or otherwise does not crash.
7989 7 : if (!i::JSArrayBuffer::SetupAllocatingData(obj, i_isolate, byte_length, true,
7990 7 : i::SharedFlag::kShared)) {
7991 0 : i::FatalProcessOutOfMemory("v8::SharedArrayBuffer::New");
7992 : }
7993 7 : return Utils::ToLocalShared(obj);
7994 : }
7995 :
7996 :
7997 336 : Local<SharedArrayBuffer> v8::SharedArrayBuffer::New(
7998 : Isolate* isolate, void* data, size_t byte_length,
7999 : ArrayBufferCreationMode mode) {
8000 336 : CHECK(i::FLAG_harmony_sharedarraybuffer);
8001 : // Embedders must guarantee that the external backing store is valid.
8002 336 : CHECK(byte_length == 0 || data != NULL);
8003 336 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8004 672 : LOG_API(i_isolate, SharedArrayBuffer, New);
8005 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
8006 : i::Handle<i::JSArrayBuffer> obj =
8007 336 : i_isolate->factory()->NewJSArrayBuffer(i::SharedFlag::kShared);
8008 : i::JSArrayBuffer::Setup(obj, i_isolate,
8009 : mode == ArrayBufferCreationMode::kExternalized, data,
8010 336 : byte_length, i::SharedFlag::kShared);
8011 336 : return Utils::ToLocalShared(obj);
8012 : }
8013 :
8014 :
8015 148 : Local<Symbol> v8::Symbol::New(Isolate* isolate, Local<String> name) {
8016 148 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8017 296 : LOG_API(i_isolate, Symbol, New);
8018 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
8019 148 : i::Handle<i::Symbol> result = i_isolate->factory()->NewSymbol();
8020 203 : if (!name.IsEmpty()) result->set_name(*Utils::OpenHandle(*name));
8021 148 : return Utils::ToLocal(result);
8022 : }
8023 :
8024 :
8025 28 : Local<Symbol> v8::Symbol::For(Isolate* isolate, Local<String> name) {
8026 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8027 28 : i::Handle<i::String> i_name = Utils::OpenHandle(*name);
8028 : return Utils::ToLocal(i_isolate->SymbolFor(
8029 28 : i::Heap::kPublicSymbolTableRootIndex, i_name, false));
8030 : }
8031 :
8032 :
8033 28 : Local<Symbol> v8::Symbol::ForApi(Isolate* isolate, Local<String> name) {
8034 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8035 28 : i::Handle<i::String> i_name = Utils::OpenHandle(*name);
8036 : return Utils::ToLocal(
8037 28 : i_isolate->SymbolFor(i::Heap::kApiSymbolTableRootIndex, i_name, false));
8038 : }
8039 :
8040 : #define WELL_KNOWN_SYMBOLS(V) \
8041 : V(HasInstance, has_instance) \
8042 : V(IsConcatSpreadable, is_concat_spreadable) \
8043 : V(Iterator, iterator) \
8044 : V(Match, match) \
8045 : V(Replace, replace) \
8046 : V(Search, search) \
8047 : V(Split, split) \
8048 : V(ToPrimitive, to_primitive) \
8049 : V(ToStringTag, to_string_tag) \
8050 : V(Unscopables, unscopables)
8051 :
8052 : #define SYMBOL_GETTER(Name, name) \
8053 : Local<Symbol> v8::Symbol::Get##Name(Isolate* isolate) { \
8054 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); \
8055 : return Utils::ToLocal(i_isolate->factory()->name##_symbol()); \
8056 : }
8057 :
8058 125214 : WELL_KNOWN_SYMBOLS(SYMBOL_GETTER)
8059 :
8060 : #undef SYMBOL_GETTER
8061 : #undef WELL_KNOWN_SYMBOLS
8062 :
8063 87 : Local<Private> v8::Private::New(Isolate* isolate, Local<String> name) {
8064 87 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8065 174 : LOG_API(i_isolate, Private, New);
8066 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
8067 87 : i::Handle<i::Symbol> symbol = i_isolate->factory()->NewPrivateSymbol();
8068 160 : if (!name.IsEmpty()) symbol->set_name(*Utils::OpenHandle(*name));
8069 : Local<Symbol> result = Utils::ToLocal(symbol);
8070 174 : return v8::Local<Private>(reinterpret_cast<Private*>(*result));
8071 : }
8072 :
8073 :
8074 14253508 : Local<Private> v8::Private::ForApi(Isolate* isolate, Local<String> name) {
8075 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8076 14253508 : i::Handle<i::String> i_name = Utils::OpenHandle(*name);
8077 : Local<Symbol> result = Utils::ToLocal(i_isolate->SymbolFor(
8078 14253508 : i::Heap::kApiPrivateSymbolTableRootIndex, i_name, true));
8079 14253508 : return v8::Local<Private>(reinterpret_cast<Private*>(*result));
8080 : }
8081 :
8082 :
8083 36399 : Local<Number> v8::Number::New(Isolate* isolate, double value) {
8084 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
8085 36399 : if (std::isnan(value)) {
8086 : // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
8087 : value = std::numeric_limits<double>::quiet_NaN();
8088 : }
8089 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(internal_isolate);
8090 36399 : i::Handle<i::Object> result = internal_isolate->factory()->NewNumber(value);
8091 36399 : return Utils::NumberToLocal(result);
8092 : }
8093 :
8094 :
8095 1099197 : Local<Integer> v8::Integer::New(Isolate* isolate, int32_t value) {
8096 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
8097 : if (i::Smi::IsValid(value)) {
8098 : return Utils::IntegerToLocal(i::Handle<i::Object>(i::Smi::FromInt(value),
8099 : internal_isolate));
8100 : }
8101 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(internal_isolate);
8102 : i::Handle<i::Object> result = internal_isolate->factory()->NewNumber(value);
8103 : return Utils::IntegerToLocal(result);
8104 : }
8105 :
8106 :
8107 160 : Local<Integer> v8::Integer::NewFromUnsigned(Isolate* isolate, uint32_t value) {
8108 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
8109 160 : bool fits_into_int32_t = (value & (1 << 31)) == 0;
8110 160 : if (fits_into_int32_t) {
8111 35 : return Integer::New(isolate, static_cast<int32_t>(value));
8112 : }
8113 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(internal_isolate);
8114 125 : i::Handle<i::Object> result = internal_isolate->factory()->NewNumber(value);
8115 : return Utils::IntegerToLocal(result);
8116 : }
8117 :
8118 :
8119 619 : void Isolate::ReportExternalAllocationLimitReached() {
8120 619 : i::Heap* heap = reinterpret_cast<i::Isolate*>(this)->heap();
8121 1238 : if (heap->gc_state() != i::Heap::NOT_IN_GC) return;
8122 619 : heap->ReportExternalMemoryPressure();
8123 : }
8124 :
8125 :
8126 9769 : HeapProfiler* Isolate::GetHeapProfiler() {
8127 : i::HeapProfiler* heap_profiler =
8128 : reinterpret_cast<i::Isolate*>(this)->heap_profiler();
8129 9769 : return reinterpret_cast<HeapProfiler*>(heap_profiler);
8130 : }
8131 :
8132 :
8133 0 : CpuProfiler* Isolate::GetCpuProfiler() {
8134 : i::CpuProfiler* cpu_profiler =
8135 : reinterpret_cast<i::Isolate*>(this)->cpu_profiler();
8136 0 : return reinterpret_cast<CpuProfiler*>(cpu_profiler);
8137 : }
8138 :
8139 :
8140 83125 : bool Isolate::InContext() {
8141 83125 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8142 83125 : return isolate->context() != NULL;
8143 : }
8144 :
8145 :
8146 68545269 : v8::Local<v8::Context> Isolate::GetCurrentContext() {
8147 68545269 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8148 : i::Context* context = isolate->context();
8149 68545269 : if (context == NULL) return Local<Context>();
8150 : i::Context* native_context = context->native_context();
8151 68545241 : if (native_context == NULL) return Local<Context>();
8152 : return Utils::ToLocal(i::Handle<i::Context>(native_context));
8153 : }
8154 :
8155 :
8156 0 : v8::Local<v8::Context> Isolate::GetCallingContext() {
8157 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8158 0 : i::Handle<i::Object> calling = isolate->GetCallingNativeContext();
8159 0 : if (calling.is_null()) return Local<Context>();
8160 : return Utils::ToLocal(i::Handle<i::Context>::cast(calling));
8161 : }
8162 :
8163 :
8164 62035 : v8::Local<v8::Context> Isolate::GetEnteredContext() {
8165 62035 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8166 : i::Handle<i::Object> last =
8167 62035 : isolate->handle_scope_implementer()->LastEnteredContext();
8168 62035 : if (last.is_null()) return Local<Context>();
8169 : return Utils::ToLocal(i::Handle<i::Context>::cast(last));
8170 : }
8171 :
8172 0 : v8::Local<v8::Context> Isolate::GetEnteredOrMicrotaskContext() {
8173 0 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8174 : i::Handle<i::Object> last;
8175 0 : if (isolate->handle_scope_implementer()
8176 : ->MicrotaskContextIsLastEnteredContext()) {
8177 0 : last = isolate->handle_scope_implementer()->MicrotaskContext();
8178 : } else {
8179 0 : last = isolate->handle_scope_implementer()->LastEnteredContext();
8180 : }
8181 0 : if (last.is_null()) return Local<Context>();
8182 : return Utils::ToLocal(i::Handle<i::Context>::cast(last));
8183 : }
8184 :
8185 11882 : v8::Local<Value> Isolate::ThrowException(v8::Local<v8::Value> value) {
8186 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8187 : ENTER_V8(isolate);
8188 : // If we're passed an empty handle, we throw an undefined exception
8189 : // to deal more gracefully with out of memory situations.
8190 11882 : if (value.IsEmpty()) {
8191 28 : isolate->ScheduleThrow(isolate->heap()->undefined_value());
8192 : } else {
8193 11854 : isolate->ScheduleThrow(*Utils::OpenHandle(*value));
8194 : }
8195 23764 : return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
8196 : }
8197 :
8198 36 : void Isolate::AddGCPrologueCallback(GCCallback callback, GCType gc_type) {
8199 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8200 36 : isolate->heap()->AddGCPrologueCallback(callback, gc_type);
8201 36 : }
8202 :
8203 :
8204 30 : void Isolate::RemoveGCPrologueCallback(GCCallback callback) {
8205 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8206 30 : isolate->heap()->RemoveGCPrologueCallback(callback);
8207 30 : }
8208 :
8209 :
8210 30 : void Isolate::AddGCEpilogueCallback(GCCallback callback, GCType gc_type) {
8211 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8212 30 : isolate->heap()->AddGCEpilogueCallback(callback, gc_type);
8213 30 : }
8214 :
8215 :
8216 30 : void Isolate::RemoveGCEpilogueCallback(GCCallback callback) {
8217 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8218 30 : isolate->heap()->RemoveGCEpilogueCallback(callback);
8219 30 : }
8220 :
8221 :
8222 0 : void V8::AddGCPrologueCallback(GCCallback callback, GCType gc_type) {
8223 : i::Isolate* isolate = i::Isolate::Current();
8224 : isolate->heap()->AddGCPrologueCallback(
8225 0 : reinterpret_cast<v8::Isolate::GCCallback>(callback), gc_type, false);
8226 0 : }
8227 :
8228 :
8229 0 : void V8::AddGCEpilogueCallback(GCCallback callback, GCType gc_type) {
8230 : i::Isolate* isolate = i::Isolate::Current();
8231 : isolate->heap()->AddGCEpilogueCallback(
8232 0 : reinterpret_cast<v8::Isolate::GCCallback>(callback), gc_type, false);
8233 0 : }
8234 :
8235 0 : void Isolate::SetEmbedderHeapTracer(EmbedderHeapTracer* tracer) {
8236 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8237 0 : isolate->heap()->SetEmbedderHeapTracer(tracer);
8238 0 : }
8239 :
8240 1331 : void Isolate::TerminateExecution() {
8241 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8242 1331 : isolate->stack_guard()->RequestTerminateExecution();
8243 1331 : }
8244 :
8245 :
8246 1685 : bool Isolate::IsExecutionTerminating() {
8247 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8248 1685 : return IsExecutionTerminatingCheck(isolate);
8249 : }
8250 :
8251 :
8252 14 : void Isolate::CancelTerminateExecution() {
8253 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8254 14 : isolate->stack_guard()->ClearTerminateExecution();
8255 14 : isolate->CancelTerminateExecution();
8256 14 : }
8257 :
8258 :
8259 128 : void Isolate::RequestInterrupt(InterruptCallback callback, void* data) {
8260 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8261 128 : isolate->RequestInterrupt(callback, data);
8262 128 : }
8263 :
8264 :
8265 10647 : void Isolate::RequestGarbageCollectionForTesting(GarbageCollectionType type) {
8266 10647 : CHECK(i::FLAG_expose_gc);
8267 10647 : if (type == kMinorGarbageCollection) {
8268 : reinterpret_cast<i::Isolate*>(this)->heap()->CollectGarbage(
8269 : i::NEW_SPACE, i::GarbageCollectionReason::kTesting,
8270 150 : kGCCallbackFlagForced);
8271 : } else {
8272 : DCHECK_EQ(kFullGarbageCollection, type);
8273 : reinterpret_cast<i::Isolate*>(this)->heap()->CollectAllGarbage(
8274 : i::Heap::kAbortIncrementalMarkingMask,
8275 10497 : i::GarbageCollectionReason::kTesting, kGCCallbackFlagForced);
8276 : }
8277 10647 : }
8278 :
8279 :
8280 765500 : Isolate* Isolate::GetCurrent() {
8281 : i::Isolate* isolate = i::Isolate::Current();
8282 765500 : return reinterpret_cast<Isolate*>(isolate);
8283 : }
8284 :
8285 :
8286 60523 : Isolate* Isolate::New(const Isolate::CreateParams& params) {
8287 60523 : i::Isolate* isolate = new i::Isolate(false);
8288 60523 : return IsolateNewImpl(isolate, params);
8289 : }
8290 :
8291 : // This is separate so that tests can provide a different |isolate|.
8292 60601 : Isolate* IsolateNewImpl(internal::Isolate* isolate,
8293 : const v8::Isolate::CreateParams& params) {
8294 : Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate);
8295 60601 : CHECK(params.array_buffer_allocator != NULL);
8296 : isolate->set_array_buffer_allocator(params.array_buffer_allocator);
8297 60601 : if (params.snapshot_blob != NULL) {
8298 : isolate->set_snapshot_blob(params.snapshot_blob);
8299 : } else {
8300 60517 : isolate->set_snapshot_blob(i::Snapshot::DefaultSnapshotBlob());
8301 : }
8302 60601 : if (params.entry_hook) {
8303 : #ifdef V8_USE_SNAPSHOT
8304 : // Setting a FunctionEntryHook is only supported in no-snapshot builds.
8305 : Utils::ApiCheck(
8306 : false, "v8::Isolate::New",
8307 : "Setting a FunctionEntryHook is only supported in no-snapshot builds.");
8308 : #endif
8309 : isolate->set_function_entry_hook(params.entry_hook);
8310 : }
8311 60601 : auto code_event_handler = params.code_event_handler;
8312 : #ifdef ENABLE_GDB_JIT_INTERFACE
8313 60601 : if (code_event_handler == nullptr && i::FLAG_gdbjit) {
8314 : code_event_handler = i::GDBJITInterface::EventHandler;
8315 : }
8316 : #endif // ENABLE_GDB_JIT_INTERFACE
8317 60601 : if (code_event_handler) {
8318 0 : isolate->InitializeLoggingAndCounters();
8319 : isolate->logger()->SetCodeEventHandler(kJitCodeEventDefault,
8320 0 : code_event_handler);
8321 : }
8322 60601 : if (params.counter_lookup_callback) {
8323 0 : v8_isolate->SetCounterFunction(params.counter_lookup_callback);
8324 : }
8325 :
8326 60601 : if (params.create_histogram_callback) {
8327 0 : v8_isolate->SetCreateHistogramFunction(params.create_histogram_callback);
8328 : }
8329 :
8330 60601 : if (params.add_histogram_sample_callback) {
8331 : v8_isolate->SetAddHistogramSampleFunction(
8332 : params.add_histogram_sample_callback);
8333 : }
8334 :
8335 60601 : isolate->set_api_external_references(params.external_references);
8336 60601 : isolate->set_allow_atomics_wait(params.allow_atomics_wait);
8337 :
8338 60601 : if (params.host_import_module_dynamically_callback_ != nullptr) {
8339 : isolate->SetHostImportModuleDynamicallyCallback(
8340 29457 : params.host_import_module_dynamically_callback_);
8341 : }
8342 :
8343 60601 : SetResourceConstraints(isolate, params.constraints);
8344 : // TODO(jochen): Once we got rid of Isolate::Current(), we can remove this.
8345 : Isolate::Scope isolate_scope(v8_isolate);
8346 60601 : if (params.entry_hook || !i::Snapshot::Initialize(isolate)) {
8347 0 : isolate->Init(NULL);
8348 : }
8349 60601 : return v8_isolate;
8350 : }
8351 :
8352 :
8353 59290 : void Isolate::Dispose() {
8354 59290 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8355 118581 : if (!Utils::ApiCheck(!isolate->IsInUse(),
8356 : "v8::Isolate::Dispose()",
8357 : "Disposing the isolate that is entered by a thread.")) {
8358 59290 : return;
8359 : }
8360 59285 : isolate->TearDown();
8361 : }
8362 :
8363 0 : void Isolate::DumpAndResetStats() {
8364 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8365 0 : isolate->DumpAndResetStats();
8366 0 : }
8367 :
8368 1800 : void Isolate::DiscardThreadSpecificMetadata() {
8369 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8370 1800 : isolate->DiscardPerThreadDataForThisThread();
8371 1800 : }
8372 :
8373 :
8374 132589 : void Isolate::Enter() {
8375 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8376 193287 : isolate->Enter();
8377 132584 : }
8378 :
8379 :
8380 132587 : void Isolate::Exit() {
8381 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8382 193285 : isolate->Exit();
8383 132588 : }
8384 :
8385 :
8386 6 : void Isolate::SetAbortOnUncaughtExceptionCallback(
8387 : AbortOnUncaughtExceptionCallback callback) {
8388 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8389 6 : isolate->SetAbortOnUncaughtExceptionCallback(callback);
8390 6 : }
8391 :
8392 :
8393 2562502 : Isolate::DisallowJavascriptExecutionScope::DisallowJavascriptExecutionScope(
8394 : Isolate* isolate,
8395 : Isolate::DisallowJavascriptExecutionScope::OnFailure on_failure)
8396 2562502 : : on_failure_(on_failure) {
8397 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8398 2562502 : if (on_failure_ == CRASH_ON_FAILURE) {
8399 : internal_ = reinterpret_cast<void*>(
8400 6 : new i::DisallowJavascriptExecution(i_isolate));
8401 : } else {
8402 : DCHECK_EQ(THROW_ON_FAILURE, on_failure);
8403 : internal_ = reinterpret_cast<void*>(
8404 2562496 : new i::ThrowOnJavascriptExecution(i_isolate));
8405 : }
8406 2562502 : }
8407 :
8408 :
8409 2562502 : Isolate::DisallowJavascriptExecutionScope::~DisallowJavascriptExecutionScope() {
8410 2562502 : if (on_failure_ == CRASH_ON_FAILURE) {
8411 6 : delete reinterpret_cast<i::DisallowJavascriptExecution*>(internal_);
8412 : } else {
8413 2562496 : delete reinterpret_cast<i::ThrowOnJavascriptExecution*>(internal_);
8414 : }
8415 2562502 : }
8416 :
8417 :
8418 6 : Isolate::AllowJavascriptExecutionScope::AllowJavascriptExecutionScope(
8419 : Isolate* isolate) {
8420 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8421 : internal_assert_ = reinterpret_cast<void*>(
8422 6 : new i::AllowJavascriptExecution(i_isolate));
8423 : internal_throws_ = reinterpret_cast<void*>(
8424 6 : new i::NoThrowOnJavascriptExecution(i_isolate));
8425 6 : }
8426 :
8427 :
8428 6 : Isolate::AllowJavascriptExecutionScope::~AllowJavascriptExecutionScope() {
8429 6 : delete reinterpret_cast<i::AllowJavascriptExecution*>(internal_assert_);
8430 6 : delete reinterpret_cast<i::NoThrowOnJavascriptExecution*>(internal_throws_);
8431 6 : }
8432 :
8433 :
8434 69057 : Isolate::SuppressMicrotaskExecutionScope::SuppressMicrotaskExecutionScope(
8435 : Isolate* isolate)
8436 69057 : : isolate_(reinterpret_cast<i::Isolate*>(isolate)) {
8437 138114 : isolate_->handle_scope_implementer()->IncrementCallDepth();
8438 : isolate_->handle_scope_implementer()->IncrementMicrotasksSuppressions();
8439 69057 : }
8440 :
8441 :
8442 69057 : Isolate::SuppressMicrotaskExecutionScope::~SuppressMicrotaskExecutionScope() {
8443 138114 : isolate_->handle_scope_implementer()->DecrementMicrotasksSuppressions();
8444 : isolate_->handle_scope_implementer()->DecrementCallDepth();
8445 69057 : }
8446 :
8447 :
8448 7 : void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) {
8449 14 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8450 14 : i::Heap* heap = isolate->heap();
8451 7 : heap_statistics->total_heap_size_ = heap->CommittedMemory();
8452 : heap_statistics->total_heap_size_executable_ =
8453 7 : heap->CommittedMemoryExecutable();
8454 7 : heap_statistics->total_physical_size_ = heap->CommittedPhysicalMemory();
8455 7 : heap_statistics->total_available_size_ = heap->Available();
8456 7 : heap_statistics->used_heap_size_ = heap->SizeOfObjects();
8457 7 : heap_statistics->heap_size_limit_ = heap->MaxReserved();
8458 : heap_statistics->malloced_memory_ =
8459 7 : isolate->allocator()->GetCurrentMemoryUsage();
8460 : heap_statistics->peak_malloced_memory_ =
8461 7 : isolate->allocator()->GetMaxMemoryUsage();
8462 7 : heap_statistics->does_zap_garbage_ = heap->ShouldZapGarbage();
8463 7 : }
8464 :
8465 :
8466 0 : size_t Isolate::NumberOfHeapSpaces() {
8467 0 : return i::LAST_SPACE - i::FIRST_SPACE + 1;
8468 : }
8469 :
8470 :
8471 0 : bool Isolate::GetHeapSpaceStatistics(HeapSpaceStatistics* space_statistics,
8472 : size_t index) {
8473 0 : if (!space_statistics) return false;
8474 0 : if (!i::Heap::IsValidAllocationSpace(static_cast<i::AllocationSpace>(index)))
8475 : return false;
8476 :
8477 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8478 0 : i::Heap* heap = isolate->heap();
8479 0 : i::Space* space = heap->space(static_cast<int>(index));
8480 :
8481 0 : space_statistics->space_name_ = heap->GetSpaceName(static_cast<int>(index));
8482 0 : space_statistics->space_size_ = space->CommittedMemory();
8483 0 : space_statistics->space_used_size_ = space->SizeOfObjects();
8484 0 : space_statistics->space_available_size_ = space->Available();
8485 0 : space_statistics->physical_space_size_ = space->CommittedPhysicalMemory();
8486 0 : return true;
8487 : }
8488 :
8489 :
8490 0 : size_t Isolate::NumberOfTrackedHeapObjectTypes() {
8491 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8492 0 : i::Heap* heap = isolate->heap();
8493 0 : return heap->NumberOfTrackedHeapObjectTypes();
8494 : }
8495 :
8496 :
8497 0 : bool Isolate::GetHeapObjectStatisticsAtLastGC(
8498 : HeapObjectStatistics* object_statistics, size_t type_index) {
8499 0 : if (!object_statistics) return false;
8500 0 : if (V8_LIKELY(!i::FLAG_gc_stats)) return false;
8501 :
8502 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8503 0 : i::Heap* heap = isolate->heap();
8504 0 : if (type_index >= heap->NumberOfTrackedHeapObjectTypes()) return false;
8505 :
8506 : const char* object_type;
8507 : const char* object_sub_type;
8508 0 : size_t object_count = heap->ObjectCountAtLastGC(type_index);
8509 0 : size_t object_size = heap->ObjectSizeAtLastGC(type_index);
8510 0 : if (!heap->GetObjectTypeName(type_index, &object_type, &object_sub_type)) {
8511 : // There should be no objects counted when the type is unknown.
8512 : DCHECK_EQ(object_count, 0U);
8513 : DCHECK_EQ(object_size, 0U);
8514 : return false;
8515 : }
8516 :
8517 0 : object_statistics->object_type_ = object_type;
8518 0 : object_statistics->object_sub_type_ = object_sub_type;
8519 0 : object_statistics->object_count_ = object_count;
8520 0 : object_statistics->object_size_ = object_size;
8521 0 : return true;
8522 : }
8523 :
8524 0 : bool Isolate::GetHeapCodeAndMetadataStatistics(
8525 : HeapCodeStatistics* code_statistics) {
8526 0 : if (!code_statistics) return false;
8527 :
8528 0 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8529 0 : isolate->heap()->CollectCodeStatistics();
8530 :
8531 0 : code_statistics->code_and_metadata_size_ = isolate->code_and_metadata_size();
8532 : code_statistics->bytecode_and_metadata_size_ =
8533 0 : isolate->bytecode_and_metadata_size();
8534 0 : return true;
8535 : }
8536 :
8537 6937 : void Isolate::GetStackSample(const RegisterState& state, void** frames,
8538 : size_t frames_limit, SampleInfo* sample_info) {
8539 6937 : RegisterState regs = state;
8540 6937 : if (TickSample::GetStackSample(this, ®s, TickSample::kSkipCEntryFrame,
8541 : frames, frames_limit, sample_info)) {
8542 6817 : return;
8543 : }
8544 120 : sample_info->frames_count = 0;
8545 120 : sample_info->vm_state = OTHER;
8546 120 : sample_info->external_callback_entry = nullptr;
8547 : }
8548 :
8549 18 : size_t Isolate::NumberOfPhantomHandleResetsSinceLastCall() {
8550 18 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8551 18 : size_t result = isolate->global_handles()->NumberOfPhantomHandleResets();
8552 : isolate->global_handles()->ResetNumberOfPhantomHandleResets();
8553 18 : return result;
8554 : }
8555 :
8556 6 : void Isolate::SetEventLogger(LogEventCallback that) {
8557 : // Do not overwrite the event logger if we want to log explicitly.
8558 12 : if (i::FLAG_log_internal_timer_events) return;
8559 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8560 : isolate->set_event_logger(that);
8561 : }
8562 :
8563 :
8564 18 : void Isolate::AddBeforeCallEnteredCallback(BeforeCallEnteredCallback callback) {
8565 36 : if (callback == NULL) return;
8566 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8567 18 : isolate->AddBeforeCallEnteredCallback(callback);
8568 : }
8569 :
8570 :
8571 6 : void Isolate::RemoveBeforeCallEnteredCallback(
8572 : BeforeCallEnteredCallback callback) {
8573 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8574 6 : isolate->RemoveBeforeCallEnteredCallback(callback);
8575 6 : }
8576 :
8577 :
8578 30 : void Isolate::AddCallCompletedCallback(CallCompletedCallback callback) {
8579 60 : if (callback == NULL) return;
8580 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8581 30 : isolate->AddCallCompletedCallback(callback);
8582 : }
8583 :
8584 :
8585 6 : void Isolate::RemoveCallCompletedCallback(CallCompletedCallback callback) {
8586 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8587 6 : isolate->RemoveCallCompletedCallback(callback);
8588 6 : }
8589 :
8590 :
8591 0 : void Isolate::AddCallCompletedCallback(
8592 : DeprecatedCallCompletedCallback callback) {
8593 : AddCallCompletedCallback(reinterpret_cast<CallCompletedCallback>(callback));
8594 0 : }
8595 :
8596 :
8597 0 : void Isolate::RemoveCallCompletedCallback(
8598 : DeprecatedCallCompletedCallback callback) {
8599 : RemoveCallCompletedCallback(
8600 : reinterpret_cast<CallCompletedCallback>(callback));
8601 0 : }
8602 :
8603 12 : void Isolate::SetPromiseHook(PromiseHook hook) {
8604 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8605 12 : isolate->SetPromiseHook(hook);
8606 12 : }
8607 :
8608 6 : void Isolate::SetPromiseRejectCallback(PromiseRejectCallback callback) {
8609 12 : if (callback == NULL) return;
8610 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8611 6 : isolate->SetPromiseRejectCallback(callback);
8612 : }
8613 :
8614 :
8615 8953 : void Isolate::RunMicrotasks() {
8616 : DCHECK(MicrotasksPolicy::kScoped != GetMicrotasksPolicy());
8617 8953 : reinterpret_cast<i::Isolate*>(this)->RunMicrotasks();
8618 8953 : }
8619 :
8620 :
8621 147 : void Isolate::EnqueueMicrotask(Local<Function> microtask) {
8622 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8623 147 : isolate->EnqueueMicrotask(Utils::OpenHandle(*microtask));
8624 147 : }
8625 :
8626 :
8627 334 : void Isolate::EnqueueMicrotask(MicrotaskCallback microtask, void* data) {
8628 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8629 : i::HandleScope scope(isolate);
8630 : i::Handle<i::CallHandlerInfo> callback_info =
8631 : i::Handle<i::CallHandlerInfo>::cast(
8632 334 : isolate->factory()->NewStruct(i::TUPLE2_TYPE));
8633 668 : SET_FIELD_WRAPPED(callback_info, set_callback, microtask);
8634 668 : SET_FIELD_WRAPPED(callback_info, set_data, data);
8635 334 : isolate->EnqueueMicrotask(callback_info);
8636 334 : }
8637 :
8638 :
8639 0 : void Isolate::SetAutorunMicrotasks(bool autorun) {
8640 : SetMicrotasksPolicy(
8641 0 : autorun ? MicrotasksPolicy::kAuto : MicrotasksPolicy::kExplicit);
8642 0 : }
8643 :
8644 :
8645 0 : bool Isolate::WillAutorunMicrotasks() const {
8646 0 : return GetMicrotasksPolicy() == MicrotasksPolicy::kAuto;
8647 : }
8648 :
8649 :
8650 1477 : void Isolate::SetMicrotasksPolicy(MicrotasksPolicy policy) {
8651 1477 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8652 : isolate->handle_scope_implementer()->set_microtasks_policy(policy);
8653 1477 : }
8654 :
8655 :
8656 0 : MicrotasksPolicy Isolate::GetMicrotasksPolicy() const {
8657 0 : i::Isolate* isolate =
8658 : reinterpret_cast<i::Isolate*>(const_cast<Isolate*>(this));
8659 0 : return isolate->handle_scope_implementer()->microtasks_policy();
8660 : }
8661 :
8662 :
8663 6 : void Isolate::AddMicrotasksCompletedCallback(
8664 : MicrotasksCompletedCallback callback) {
8665 : DCHECK(callback);
8666 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8667 6 : isolate->AddMicrotasksCompletedCallback(callback);
8668 6 : }
8669 :
8670 :
8671 6 : void Isolate::RemoveMicrotasksCompletedCallback(
8672 : MicrotasksCompletedCallback callback) {
8673 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8674 6 : isolate->RemoveMicrotasksCompletedCallback(callback);
8675 6 : }
8676 :
8677 :
8678 58 : void Isolate::SetUseCounterCallback(UseCounterCallback callback) {
8679 58 : reinterpret_cast<i::Isolate*>(this)->SetUseCounterCallback(callback);
8680 58 : }
8681 :
8682 :
8683 6 : void Isolate::SetCounterFunction(CounterLookupCallback callback) {
8684 6 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8685 6 : isolate->stats_table()->SetCounterFunction(callback);
8686 6 : isolate->InitializeLoggingAndCounters();
8687 6 : isolate->counters()->ResetCounters();
8688 6 : }
8689 :
8690 :
8691 6 : void Isolate::SetCreateHistogramFunction(CreateHistogramCallback callback) {
8692 12 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8693 6 : isolate->stats_table()->SetCreateHistogramFunction(callback);
8694 6 : isolate->InitializeLoggingAndCounters();
8695 6 : isolate->counters()->ResetHistograms();
8696 6 : isolate->counters()->InitializeHistograms();
8697 6 : }
8698 :
8699 :
8700 6 : void Isolate::SetAddHistogramSampleFunction(
8701 : AddHistogramSampleCallback callback) {
8702 : reinterpret_cast<i::Isolate*>(this)
8703 : ->stats_table()
8704 6 : ->SetAddHistogramSampleFunction(callback);
8705 6 : }
8706 :
8707 :
8708 0 : bool Isolate::IdleNotification(int idle_time_in_ms) {
8709 : // Returning true tells the caller that it need not
8710 : // continue to call IdleNotification.
8711 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8712 0 : if (!i::FLAG_use_idle_notification) return true;
8713 0 : return isolate->heap()->IdleNotification(idle_time_in_ms);
8714 : }
8715 :
8716 :
8717 5450 : bool Isolate::IdleNotificationDeadline(double deadline_in_seconds) {
8718 : // Returning true tells the caller that it need not
8719 : // continue to call IdleNotification.
8720 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8721 5450 : if (!i::FLAG_use_idle_notification) return true;
8722 5450 : return isolate->heap()->IdleNotification(deadline_in_seconds);
8723 : }
8724 :
8725 6536 : void Isolate::LowMemoryNotification() {
8726 6536 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8727 : {
8728 : i::HistogramTimerScope idle_notification_scope(
8729 6536 : isolate->counters()->gc_low_memory_notification());
8730 19608 : TRACE_EVENT0("v8", "V8.GCLowMemoryNotification");
8731 : isolate->heap()->CollectAllAvailableGarbage(
8732 6536 : i::GarbageCollectionReason::kLowMemoryNotification);
8733 : }
8734 : {
8735 6536 : i::HeapIterator iterator(isolate->heap());
8736 : i::HeapObject* obj;
8737 44140226 : while ((obj = iterator.next()) != nullptr) {
8738 44127154 : if (obj->IsAbstractCode()) {
8739 8951201 : i::AbstractCode::cast(obj)->DropStackFrameCache();
8740 : }
8741 6536 : }
8742 : }
8743 6536 : }
8744 :
8745 :
8746 5614 : int Isolate::ContextDisposedNotification(bool dependant_context) {
8747 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8748 5626 : return isolate->heap()->NotifyContextDisposed(dependant_context);
8749 : }
8750 :
8751 :
8752 0 : void Isolate::IsolateInForegroundNotification() {
8753 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8754 0 : return isolate->IsolateInForegroundNotification();
8755 : }
8756 :
8757 :
8758 0 : void Isolate::IsolateInBackgroundNotification() {
8759 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8760 0 : return isolate->IsolateInBackgroundNotification();
8761 : }
8762 :
8763 26 : void Isolate::MemoryPressureNotification(MemoryPressureLevel level) {
8764 76 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8765 : bool on_isolate_thread =
8766 26 : v8::Locker::IsActive()
8767 : ? isolate->thread_manager()->IsLockedByCurrentThread()
8768 76 : : i::ThreadId::Current().Equals(isolate->thread_id());
8769 26 : isolate->heap()->MemoryPressureNotification(level, on_isolate_thread);
8770 26 : isolate->allocator()->MemoryPressureNotification(level);
8771 : isolate->compiler_dispatcher()->MemoryPressureNotification(level,
8772 26 : on_isolate_thread);
8773 26 : }
8774 :
8775 0 : void Isolate::SetRAILMode(RAILMode rail_mode) {
8776 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8777 0 : return isolate->SetRAILMode(rail_mode);
8778 : }
8779 :
8780 18 : void Isolate::IncreaseHeapLimitForDebugging() {
8781 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8782 : isolate->heap()->IncreaseHeapLimitForDebugging();
8783 18 : }
8784 :
8785 4321 : void Isolate::RestoreOriginalHeapLimit() {
8786 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8787 4321 : isolate->heap()->RestoreOriginalHeapLimit();
8788 4321 : }
8789 :
8790 18 : bool Isolate::IsHeapLimitIncreasedForDebugging() {
8791 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8792 18 : return isolate->heap()->IsHeapLimitIncreasedForDebugging();
8793 : }
8794 :
8795 72 : void Isolate::SetJitCodeEventHandler(JitCodeEventOptions options,
8796 : JitCodeEventHandler event_handler) {
8797 72 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8798 : // Ensure that logging is initialized for our isolate.
8799 72 : isolate->InitializeLoggingAndCounters();
8800 72 : isolate->logger()->SetCodeEventHandler(options, event_handler);
8801 72 : }
8802 :
8803 :
8804 12 : void Isolate::SetStackLimit(uintptr_t stack_limit) {
8805 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8806 12 : CHECK(stack_limit);
8807 12 : isolate->stack_guard()->SetStackLimit(stack_limit);
8808 12 : }
8809 :
8810 0 : void Isolate::GetCodeRange(void** start, size_t* length_in_bytes) {
8811 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8812 0 : if (isolate->heap()->memory_allocator()->code_range()->valid()) {
8813 0 : *start = isolate->heap()->memory_allocator()->code_range()->start();
8814 : *length_in_bytes =
8815 0 : isolate->heap()->memory_allocator()->code_range()->size();
8816 : } else {
8817 0 : *start = NULL;
8818 0 : *length_in_bytes = 0;
8819 : }
8820 0 : }
8821 :
8822 :
8823 30 : void Isolate::SetFatalErrorHandler(FatalErrorCallback that) {
8824 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8825 : isolate->set_exception_behavior(that);
8826 30 : }
8827 :
8828 0 : void Isolate::SetOOMErrorHandler(OOMErrorCallback that) {
8829 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8830 : isolate->set_oom_behavior(that);
8831 0 : }
8832 :
8833 56 : void Isolate::SetAllowCodeGenerationFromStringsCallback(
8834 : AllowCodeGenerationFromStringsCallback callback) {
8835 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8836 : isolate->set_allow_code_gen_callback(callback);
8837 56 : }
8838 :
8839 : #define CALLBACK_SETTER(ExternalName, Type, InternalName) \
8840 : void Isolate::Set##ExternalName(Type callback) { \
8841 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); \
8842 : isolate->set_##InternalName(callback); \
8843 : }
8844 :
8845 240 : CALLBACK_SETTER(WasmModuleCallback, ExtensionCallback, wasm_module_callback)
8846 240 : CALLBACK_SETTER(WasmCompileCallback, ExtensionCallback, wasm_compile_callback)
8847 30 : CALLBACK_SETTER(WasmInstanceCallback, ExtensionCallback, wasm_instance_callback)
8848 30 : CALLBACK_SETTER(WasmInstantiateCallback, ExtensionCallback,
8849 : wasm_instantiate_callback)
8850 :
8851 0 : bool Isolate::IsDead() {
8852 0 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8853 0 : return isolate->IsDead();
8854 : }
8855 :
8856 927 : bool Isolate::AddMessageListener(MessageCallback that, Local<Value> data) {
8857 927 : return AddMessageListenerWithErrorLevel(that, kMessageError, data);
8858 : }
8859 :
8860 29556 : bool Isolate::AddMessageListenerWithErrorLevel(MessageCallback that,
8861 : int message_levels,
8862 : Local<Value> data) {
8863 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8864 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
8865 : i::HandleScope scope(isolate);
8866 : i::Handle<i::TemplateList> list = isolate->factory()->message_listeners();
8867 29556 : i::Handle<i::FixedArray> listener = isolate->factory()->NewFixedArray(3);
8868 : i::Handle<i::Foreign> foreign =
8869 29556 : isolate->factory()->NewForeign(FUNCTION_ADDR(that));
8870 29556 : listener->set(0, *foreign);
8871 29549 : listener->set(1, data.IsEmpty() ? isolate->heap()->undefined_value()
8872 59112 : : *Utils::OpenHandle(*data));
8873 : listener->set(2, i::Smi::FromInt(message_levels));
8874 29556 : list = i::TemplateList::Add(isolate, list, listener);
8875 : isolate->heap()->SetMessageListeners(*list);
8876 29556 : return true;
8877 : }
8878 :
8879 :
8880 185 : void Isolate::RemoveMessageListeners(MessageCallback that) {
8881 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8882 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
8883 : i::HandleScope scope(isolate);
8884 : i::DisallowHeapAllocation no_gc;
8885 185 : i::TemplateList* listeners = isolate->heap()->message_listeners();
8886 603 : for (int i = 0; i < listeners->length(); i++) {
8887 233 : if (listeners->get(i)->IsUndefined(isolate)) continue; // skip deleted ones
8888 : i::FixedArray* listener = i::FixedArray::cast(listeners->get(i));
8889 : i::Foreign* callback_obj = i::Foreign::cast(listener->get(0));
8890 185 : if (callback_obj->foreign_address() == FUNCTION_ADDR(that)) {
8891 185 : listeners->set(i, isolate->heap()->undefined_value());
8892 : }
8893 : }
8894 185 : }
8895 :
8896 :
8897 32 : void Isolate::SetFailedAccessCheckCallbackFunction(
8898 : FailedAccessCheckCallback callback) {
8899 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8900 32 : isolate->SetFailedAccessCheckCallback(callback);
8901 32 : }
8902 :
8903 :
8904 885 : void Isolate::SetCaptureStackTraceForUncaughtExceptions(
8905 : bool capture, int frame_limit, StackTrace::StackTraceOptions options) {
8906 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8907 : isolate->SetCaptureStackTraceForUncaughtExceptions(capture, frame_limit,
8908 885 : options);
8909 885 : }
8910 :
8911 :
8912 6 : void Isolate::VisitExternalResources(ExternalResourceVisitor* visitor) {
8913 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8914 6 : isolate->heap()->VisitExternalResources(visitor);
8915 6 : }
8916 :
8917 :
8918 238959 : bool Isolate::IsInUse() {
8919 238959 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8920 238959 : return isolate->IsInUse();
8921 : }
8922 :
8923 :
8924 6 : void Isolate::VisitHandlesWithClassIds(PersistentHandleVisitor* visitor) {
8925 6 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8926 : i::DisallowHeapAllocation no_allocation;
8927 6 : isolate->global_handles()->IterateAllRootsWithClassIds(visitor);
8928 6 : }
8929 :
8930 :
8931 6 : void Isolate::VisitHandlesForPartialDependence(
8932 : PersistentHandleVisitor* visitor) {
8933 6 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8934 : i::DisallowHeapAllocation no_allocation;
8935 6 : isolate->global_handles()->IterateAllRootsInNewSpaceWithClassIds(visitor);
8936 6 : }
8937 :
8938 :
8939 0 : void Isolate::VisitWeakHandles(PersistentHandleVisitor* visitor) {
8940 0 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8941 : i::DisallowHeapAllocation no_allocation;
8942 0 : isolate->global_handles()->IterateWeakRootsInNewSpaceWithClassIds(visitor);
8943 0 : }
8944 :
8945 6 : void Isolate::SetAllowAtomicsWait(bool allow) {
8946 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8947 : isolate->set_allow_atomics_wait(allow);
8948 6 : }
8949 :
8950 1194192 : MicrotasksScope::MicrotasksScope(Isolate* isolate, MicrotasksScope::Type type)
8951 : : isolate_(reinterpret_cast<i::Isolate*>(isolate)),
8952 1194192 : run_(type == MicrotasksScope::kRunMicrotasks) {
8953 1194192 : auto handle_scope_implementer = isolate_->handle_scope_implementer();
8954 1194192 : if (run_) handle_scope_implementer->IncrementMicrotasksScopeDepth();
8955 : #ifdef DEBUG
8956 : if (!run_) handle_scope_implementer->IncrementDebugMicrotasksScopeDepth();
8957 : #endif
8958 1194192 : }
8959 :
8960 :
8961 1194191 : MicrotasksScope::~MicrotasksScope() {
8962 1269282 : auto handle_scope_implementer = isolate_->handle_scope_implementer();
8963 1194191 : if (run_) {
8964 : handle_scope_implementer->DecrementMicrotasksScopeDepth();
8965 75091 : if (MicrotasksPolicy::kScoped ==
8966 : handle_scope_implementer->microtasks_policy()) {
8967 48431 : PerformCheckpoint(reinterpret_cast<Isolate*>(isolate_));
8968 : }
8969 : }
8970 : #ifdef DEBUG
8971 : if (!run_) handle_scope_implementer->DecrementDebugMicrotasksScopeDepth();
8972 : #endif
8973 1194191 : }
8974 :
8975 :
8976 48461 : void MicrotasksScope::PerformCheckpoint(Isolate* v8Isolate) {
8977 48461 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8Isolate);
8978 96922 : if (IsExecutionTerminatingCheck(isolate)) return;
8979 96194 : auto handle_scope_implementer = isolate->handle_scope_implementer();
8980 96194 : if (!handle_scope_implementer->GetMicrotasksScopeDepth() &&
8981 : !handle_scope_implementer->HasMicrotasksSuppressions()) {
8982 47721 : isolate->RunMicrotasks();
8983 : }
8984 : }
8985 :
8986 :
8987 0 : int MicrotasksScope::GetCurrentDepth(Isolate* v8Isolate) {
8988 0 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8Isolate);
8989 0 : return isolate->handle_scope_implementer()->GetMicrotasksScopeDepth();
8990 : }
8991 :
8992 120 : bool MicrotasksScope::IsRunningMicrotasks(Isolate* v8Isolate) {
8993 120 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8Isolate);
8994 120 : return isolate->IsRunningMicrotasks();
8995 : }
8996 :
8997 11616539 : String::Utf8Value::Utf8Value(v8::Local<v8::Value> obj)
8998 11616539 : : str_(NULL), length_(0) {
8999 11616574 : if (obj.IsEmpty()) return;
9000 : i::Isolate* isolate = i::Isolate::Current();
9001 : Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate);
9002 : ENTER_V8(isolate);
9003 : i::HandleScope scope(isolate);
9004 11616527 : Local<Context> context = v8_isolate->GetCurrentContext();
9005 23233031 : TryCatch try_catch(v8_isolate);
9006 : Local<String> str;
9007 23233077 : if (!obj->ToString(context).ToLocal(&str)) return;
9008 : i::Handle<i::String> i_str = Utils::OpenHandle(*str);
9009 11616504 : length_ = v8::Utf8Length(*i_str, isolate);
9010 11616504 : str_ = i::NewArray<char>(length_ + 1);
9011 11616504 : str->WriteUtf8(str_);
9012 : }
9013 :
9014 :
9015 11616539 : String::Utf8Value::~Utf8Value() {
9016 11616539 : i::DeleteArray(str_);
9017 11616539 : }
9018 :
9019 :
9020 7 : String::Value::Value(v8::Local<v8::Value> obj) : str_(NULL), length_(0) {
9021 7 : if (obj.IsEmpty()) return;
9022 : i::Isolate* isolate = i::Isolate::Current();
9023 : Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate);
9024 : ENTER_V8(isolate);
9025 : i::HandleScope scope(isolate);
9026 7 : Local<Context> context = v8_isolate->GetCurrentContext();
9027 14 : TryCatch try_catch(v8_isolate);
9028 : Local<String> str;
9029 14 : if (!obj->ToString(context).ToLocal(&str)) return;
9030 7 : length_ = str->Length();
9031 7 : str_ = i::NewArray<uint16_t>(length_ + 1);
9032 : str->Write(str_);
9033 : }
9034 :
9035 :
9036 7 : String::Value::~Value() {
9037 7 : i::DeleteArray(str_);
9038 7 : }
9039 :
9040 : #define DEFINE_ERROR(NAME, name) \
9041 : Local<Value> Exception::NAME(v8::Local<v8::String> raw_message) { \
9042 : i::Isolate* isolate = i::Isolate::Current(); \
9043 : LOG_API(isolate, NAME, New); \
9044 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate); \
9045 : i::Object* error; \
9046 : { \
9047 : i::HandleScope scope(isolate); \
9048 : i::Handle<i::String> message = Utils::OpenHandle(*raw_message); \
9049 : i::Handle<i::JSFunction> constructor = isolate->name##_function(); \
9050 : error = *isolate->factory()->NewError(constructor, message); \
9051 : } \
9052 : i::Handle<i::Object> result(error, isolate); \
9053 : return Utils::ToLocal(result); \
9054 : }
9055 :
9056 485 : DEFINE_ERROR(RangeError, range_error)
9057 35 : DEFINE_ERROR(ReferenceError, reference_error)
9058 35 : DEFINE_ERROR(SyntaxError, syntax_error)
9059 35 : DEFINE_ERROR(TypeError, type_error)
9060 1240 : DEFINE_ERROR(Error, error)
9061 :
9062 : #undef DEFINE_ERROR
9063 :
9064 :
9065 110 : Local<Message> Exception::CreateMessage(Isolate* isolate,
9066 : Local<Value> exception) {
9067 110 : i::Handle<i::Object> obj = Utils::OpenHandle(*exception);
9068 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
9069 : ENTER_V8(i_isolate);
9070 : i::HandleScope scope(i_isolate);
9071 : return Utils::MessageToLocal(
9072 220 : scope.CloseAndEscape(i_isolate->CreateMessage(obj, NULL)));
9073 : }
9074 :
9075 :
9076 0 : Local<Message> Exception::CreateMessage(Local<Value> exception) {
9077 : i::Handle<i::Object> obj = Utils::OpenHandle(*exception);
9078 0 : if (!obj->IsHeapObject()) return Local<Message>();
9079 : i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate();
9080 0 : return CreateMessage(reinterpret_cast<Isolate*>(isolate), exception);
9081 : }
9082 :
9083 :
9084 49 : Local<StackTrace> Exception::GetStackTrace(Local<Value> exception) {
9085 : i::Handle<i::Object> obj = Utils::OpenHandle(*exception);
9086 49 : if (!obj->IsJSObject()) return Local<StackTrace>();
9087 : i::Handle<i::JSObject> js_obj = i::Handle<i::JSObject>::cast(obj);
9088 : i::Isolate* isolate = js_obj->GetIsolate();
9089 : ENTER_V8(isolate);
9090 49 : return Utils::StackTraceToLocal(isolate->GetDetailedStackTrace(js_obj));
9091 : }
9092 :
9093 :
9094 : // --- D e b u g S u p p o r t ---
9095 :
9096 0 : bool Debug::SetDebugEventListener(Isolate* isolate, EventCallback that,
9097 : Local<Value> data) {
9098 0 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
9099 : ENTER_V8(i_isolate);
9100 : i::HandleScope scope(i_isolate);
9101 0 : if (that == nullptr) {
9102 0 : i_isolate->debug()->SetDebugDelegate(nullptr, false);
9103 : } else {
9104 : i::Handle<i::Object> i_data = i_isolate->factory()->undefined_value();
9105 0 : if (!data.IsEmpty()) i_data = Utils::OpenHandle(*data);
9106 : i::NativeDebugDelegate* delegate =
9107 0 : new i::NativeDebugDelegate(i_isolate, that, i_data);
9108 0 : i_isolate->debug()->SetDebugDelegate(delegate, true);
9109 : }
9110 0 : return true;
9111 : }
9112 :
9113 0 : void Debug::DebugBreak(Isolate* isolate) { debug::DebugBreak(isolate); }
9114 :
9115 0 : void Debug::CancelDebugBreak(Isolate* isolate) {
9116 : debug::CancelDebugBreak(isolate);
9117 0 : }
9118 :
9119 0 : bool Debug::CheckDebugBreak(Isolate* isolate) {
9120 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
9121 0 : return internal_isolate->stack_guard()->CheckDebugBreak();
9122 : }
9123 :
9124 0 : void Debug::SetMessageHandler(Isolate* isolate,
9125 0 : v8::Debug::MessageHandler handler) {}
9126 :
9127 0 : void Debug::SendCommand(Isolate* isolate, const uint16_t* command, int length,
9128 0 : ClientData* client_data) {}
9129 :
9130 0 : MaybeLocal<Value> Debug::Call(Local<Context> context,
9131 : v8::Local<v8::Function> fun,
9132 : v8::Local<v8::Value> data) {
9133 0 : return debug::Call(context, fun, data);
9134 : }
9135 :
9136 0 : void Debug::ProcessDebugMessages(Isolate* isolate) {}
9137 :
9138 0 : Local<Context> Debug::GetDebugContext(Isolate* isolate) {
9139 0 : return debug::GetDebugContext(isolate);
9140 : }
9141 :
9142 0 : MaybeLocal<Context> Debug::GetDebuggedContext(Isolate* isolate) {
9143 0 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
9144 : ENTER_V8(i_isolate);
9145 0 : if (!i_isolate->debug()->in_debug_scope()) return MaybeLocal<Context>();
9146 0 : i::Handle<i::Object> calling = i_isolate->GetCallingNativeContext();
9147 0 : if (calling.is_null()) return MaybeLocal<Context>();
9148 0 : return Utils::ToLocal(i::Handle<i::Context>::cast(calling));
9149 : }
9150 :
9151 0 : void Debug::SetLiveEditEnabled(Isolate* isolate, bool enable) {
9152 : debug::SetLiveEditEnabled(isolate, enable);
9153 0 : }
9154 :
9155 0 : bool Debug::IsTailCallEliminationEnabled(Isolate* isolate) {
9156 0 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
9157 0 : return internal_isolate->is_tail_call_elimination_enabled();
9158 : }
9159 :
9160 0 : void Debug::SetTailCallEliminationEnabled(Isolate* isolate, bool enabled) {
9161 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
9162 0 : internal_isolate->SetTailCallEliminationEnabled(enabled);
9163 0 : }
9164 :
9165 0 : MaybeLocal<Array> Debug::GetInternalProperties(Isolate* v8_isolate,
9166 : Local<Value> value) {
9167 0 : return debug::GetInternalProperties(v8_isolate, value);
9168 : }
9169 :
9170 4579 : void debug::SetContextId(Local<Context> context, int id) {
9171 : Utils::OpenHandle(*context)->set_debug_context_id(i::Smi::FromInt(id));
9172 4579 : }
9173 :
9174 878040 : int debug::GetContextId(Local<Context> context) {
9175 : i::Object* value = Utils::OpenHandle(*context)->debug_context_id();
9176 1756080 : return (value->IsSmi()) ? i::Smi::cast(value)->value() : 0;
9177 : }
9178 :
9179 158583 : Local<Context> debug::GetDebugContext(Isolate* isolate) {
9180 158583 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
9181 : ENTER_V8(i_isolate);
9182 317166 : return Utils::ToLocal(i_isolate->debug()->GetDebugContext());
9183 : }
9184 :
9185 7751 : MaybeLocal<Value> debug::Call(Local<Context> context,
9186 : v8::Local<v8::Function> fun,
9187 : v8::Local<v8::Value> data) {
9188 38755 : PREPARE_FOR_EXECUTION(context, Debug, Call, Value);
9189 : i::Handle<i::Object> data_obj;
9190 7751 : if (data.IsEmpty()) {
9191 4465 : data_obj = isolate->factory()->undefined_value();
9192 : } else {
9193 3286 : data_obj = Utils::OpenHandle(*data);
9194 : }
9195 : Local<Value> result;
9196 : has_pending_exception = !ToLocal<Value>(
9197 15502 : isolate->debug()->Call(Utils::OpenHandle(*fun), data_obj), &result);
9198 7769 : RETURN_ON_FAILED_EXECUTION(Value);
9199 7733 : RETURN_ESCAPED(result);
9200 : }
9201 :
9202 4146 : void debug::SetLiveEditEnabled(Isolate* isolate, bool enable) {
9203 4146 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
9204 : internal_isolate->debug()->set_live_edit_enabled(enable);
9205 4146 : }
9206 :
9207 19611 : void debug::DebugBreak(Isolate* isolate) {
9208 19611 : reinterpret_cast<i::Isolate*>(isolate)->stack_guard()->RequestDebugBreak();
9209 19611 : }
9210 :
9211 93 : void debug::CancelDebugBreak(Isolate* isolate) {
9212 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
9213 93 : internal_isolate->stack_guard()->ClearDebugBreak();
9214 93 : }
9215 :
9216 106344 : MaybeLocal<Array> debug::GetInternalProperties(Isolate* v8_isolate,
9217 : Local<Value> value) {
9218 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9219 : ENTER_V8(isolate);
9220 106344 : i::Handle<i::Object> val = Utils::OpenHandle(*value);
9221 : i::Handle<i::JSArray> result;
9222 212688 : if (!i::Runtime::GetInternalProperties(isolate, val).ToHandle(&result))
9223 0 : return MaybeLocal<Array>();
9224 106344 : return Utils::ToLocal(result);
9225 : }
9226 :
9227 10287 : void debug::ChangeBreakOnException(Isolate* isolate, ExceptionBreakState type) {
9228 20574 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
9229 : internal_isolate->debug()->ChangeBreakOnException(
9230 20574 : i::BreakException, type == BreakOnAnyException);
9231 : internal_isolate->debug()->ChangeBreakOnException(i::BreakUncaughtException,
9232 20574 : type != NoBreakOnException);
9233 10287 : }
9234 :
9235 4519 : void debug::SetBreakPointsActive(Isolate* v8_isolate, bool is_active) {
9236 4519 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9237 : ENTER_V8(isolate);
9238 : isolate->debug()->set_break_points_active(is_active);
9239 4519 : }
9240 :
9241 8624 : void debug::SetOutOfMemoryCallback(Isolate* isolate,
9242 : OutOfMemoryCallback callback, void* data) {
9243 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
9244 : ENTER_V8(i_isolate);
9245 8624 : i_isolate->heap()->SetOutOfMemoryCallback(callback, data);
9246 8624 : }
9247 :
9248 62890 : void debug::PrepareStep(Isolate* v8_isolate, StepAction action) {
9249 125780 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9250 : ENTER_V8(isolate);
9251 62890 : CHECK(isolate->debug()->CheckExecutionState());
9252 : // Clear all current stepping setup.
9253 62890 : isolate->debug()->ClearStepping();
9254 : // Prepare step.
9255 125780 : isolate->debug()->PrepareStep(static_cast<i::StepAction>(action));
9256 62890 : }
9257 :
9258 72 : void debug::ClearStepping(Isolate* v8_isolate) {
9259 72 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9260 : ENTER_V8(isolate);
9261 : // Clear all current stepping setup.
9262 72 : isolate->debug()->ClearStepping();
9263 72 : }
9264 :
9265 66 : void debug::BreakRightNow(Isolate* v8_isolate) {
9266 66 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9267 : ENTER_V8(isolate);
9268 66 : isolate->debug()->HandleDebugBreak(i::kIgnoreIfAllFramesBlackboxed);
9269 66 : }
9270 :
9271 144 : bool debug::AllFramesOnStackAreBlackboxed(Isolate* v8_isolate) {
9272 144 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9273 : ENTER_V8(isolate);
9274 288 : return isolate->debug()->AllFramesOnStackAreBlackboxed();
9275 : }
9276 :
9277 0 : v8::Isolate* debug::Script::GetIsolate() const {
9278 0 : return reinterpret_cast<v8::Isolate*>(Utils::OpenHandle(this)->GetIsolate());
9279 : }
9280 :
9281 0 : ScriptOriginOptions debug::Script::OriginOptions() const {
9282 0 : return Utils::OpenHandle(this)->origin_options();
9283 : }
9284 :
9285 12100 : bool debug::Script::WasCompiled() const {
9286 : return Utils::OpenHandle(this)->compilation_state() ==
9287 12100 : i::Script::COMPILATION_STATE_COMPILED;
9288 : }
9289 :
9290 12076 : bool debug::Script::IsEmbedded() const {
9291 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9292 12076 : return script->context_data() == script->GetHeap()->uninitialized_symbol();
9293 : }
9294 :
9295 107772 : int debug::Script::Id() const { return Utils::OpenHandle(this)->id(); }
9296 :
9297 36576 : int debug::Script::LineOffset() const {
9298 36576 : return Utils::OpenHandle(this)->line_offset();
9299 : }
9300 :
9301 36576 : int debug::Script::ColumnOffset() const {
9302 36576 : return Utils::OpenHandle(this)->column_offset();
9303 : }
9304 :
9305 36576 : std::vector<int> debug::Script::LineEnds() const {
9306 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9307 36576 : if (script->type() == i::Script::TYPE_WASM) return std::vector<int>();
9308 : i::Isolate* isolate = script->GetIsolate();
9309 : i::HandleScope scope(isolate);
9310 36576 : i::Script::InitLineEnds(script);
9311 36576 : CHECK(script->line_ends()->IsFixedArray());
9312 : i::Handle<i::FixedArray> line_ends(i::FixedArray::cast(script->line_ends()));
9313 36576 : std::vector<int> result(line_ends->length());
9314 12505440 : for (int i = 0; i < line_ends->length(); ++i) {
9315 : i::Smi* line_end = i::Smi::cast(line_ends->get(i));
9316 12432288 : result[i] = line_end->value();
9317 : }
9318 : return result;
9319 : }
9320 :
9321 36796 : MaybeLocal<String> debug::Script::Name() const {
9322 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
9323 : i::HandleScope handle_scope(isolate);
9324 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9325 : i::Handle<i::Object> value(script->name(), isolate);
9326 36796 : if (!value->IsString()) return MaybeLocal<String>();
9327 : return Utils::ToLocal(
9328 15983 : handle_scope.CloseAndEscape(i::Handle<i::String>::cast(value)));
9329 : }
9330 :
9331 57389 : MaybeLocal<String> debug::Script::SourceURL() const {
9332 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
9333 : i::HandleScope handle_scope(isolate);
9334 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9335 : i::Handle<i::Object> value(script->source_url(), isolate);
9336 57389 : if (!value->IsString()) return MaybeLocal<String>();
9337 : return Utils::ToLocal(
9338 1301 : handle_scope.CloseAndEscape(i::Handle<i::String>::cast(value)));
9339 : }
9340 :
9341 36576 : MaybeLocal<String> debug::Script::SourceMappingURL() const {
9342 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
9343 : i::HandleScope handle_scope(isolate);
9344 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9345 : i::Handle<i::Object> value(script->source_mapping_url(), isolate);
9346 36576 : if (!value->IsString()) return MaybeLocal<String>();
9347 : return Utils::ToLocal(
9348 3367 : handle_scope.CloseAndEscape(i::Handle<i::String>::cast(value)));
9349 : }
9350 :
9351 90224 : Maybe<int> debug::Script::ContextId() const {
9352 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
9353 : i::HandleScope handle_scope(isolate);
9354 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9355 : i::Object* value = script->context_data();
9356 90224 : if (value->IsSmi()) return Just(i::Smi::cast(value)->value());
9357 : return Nothing<int>();
9358 : }
9359 :
9360 36582 : MaybeLocal<String> debug::Script::Source() const {
9361 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
9362 : i::HandleScope handle_scope(isolate);
9363 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9364 : i::Handle<i::Object> value(script->source(), isolate);
9365 36582 : if (!value->IsString()) return MaybeLocal<String>();
9366 : return Utils::ToLocal(
9367 36582 : handle_scope.CloseAndEscape(i::Handle<i::String>::cast(value)));
9368 : }
9369 :
9370 24646 : bool debug::Script::IsWasm() const {
9371 24646 : return Utils::OpenHandle(this)->type() == i::Script::TYPE_WASM;
9372 : }
9373 :
9374 36576 : bool debug::Script::IsModule() const {
9375 36576 : return Utils::OpenHandle(this)->origin_options().IsModule();
9376 : }
9377 :
9378 : namespace {
9379 : int GetSmiValue(i::Handle<i::FixedArray> array, int index) {
9380 : return i::Smi::cast(array->get(index))->value();
9381 : }
9382 :
9383 21752 : bool CompareBreakLocation(const i::BreakLocation& loc1,
9384 21752 : const i::BreakLocation& loc2) {
9385 21752 : return loc1.position() < loc2.position();
9386 : }
9387 : } // namespace
9388 :
9389 258 : bool debug::Script::GetPossibleBreakpoints(
9390 : const debug::Location& start, const debug::Location& end,
9391 : bool restrict_to_function,
9392 : std::vector<debug::BreakLocation>* locations) const {
9393 258 : CHECK(!start.IsEmpty());
9394 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9395 258 : if (script->type() == i::Script::TYPE_WASM) {
9396 : i::Handle<i::WasmCompiledModule> compiled_module(
9397 : i::WasmCompiledModule::cast(script->wasm_compiled_module()));
9398 36 : return compiled_module->GetPossibleBreakpoints(start, end, locations);
9399 : }
9400 :
9401 222 : i::Script::InitLineEnds(script);
9402 222 : CHECK(script->line_ends()->IsFixedArray());
9403 192 : i::Isolate* isolate = script->GetIsolate();
9404 : i::Handle<i::FixedArray> line_ends =
9405 : i::Handle<i::FixedArray>::cast(i::handle(script->line_ends(), isolate));
9406 222 : CHECK(line_ends->length());
9407 :
9408 222 : int start_offset = GetSourceOffset(start);
9409 : int end_offset = end.IsEmpty()
9410 120 : ? GetSmiValue(line_ends, line_ends->length() - 1) + 1
9411 342 : : GetSourceOffset(end);
9412 222 : if (start_offset >= end_offset) return true;
9413 :
9414 : std::vector<i::BreakLocation> v8_locations;
9415 192 : if (!isolate->debug()->GetPossibleBreakpoints(
9416 : script, start_offset, end_offset, restrict_to_function,
9417 384 : &v8_locations)) {
9418 : return false;
9419 : }
9420 :
9421 192 : std::sort(v8_locations.begin(), v8_locations.end(), CompareBreakLocation);
9422 : int current_line_end_index = 0;
9423 3822 : for (const auto& v8_location : v8_locations) {
9424 : int offset = v8_location.position();
9425 8868 : while (offset > GetSmiValue(line_ends, current_line_end_index)) {
9426 1992 : ++current_line_end_index;
9427 1992 : CHECK(current_line_end_index < line_ends->length());
9428 : }
9429 : int line_offset = 0;
9430 :
9431 3438 : if (current_line_end_index > 0) {
9432 5820 : line_offset = GetSmiValue(line_ends, current_line_end_index - 1) + 1;
9433 : }
9434 : locations->emplace_back(
9435 3438 : current_line_end_index + script->line_offset(),
9436 6876 : offset - line_offset +
9437 : (current_line_end_index == 0 ? script->column_offset() : 0),
9438 10314 : v8_location.type());
9439 : }
9440 : return true;
9441 : }
9442 :
9443 486 : int debug::Script::GetSourceOffset(const debug::Location& location) const {
9444 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9445 486 : if (script->type() == i::Script::TYPE_WASM) {
9446 : // TODO(clemensh): Return the proper thing for wasm.
9447 : return 0;
9448 : }
9449 :
9450 1458 : int line = std::max(location.GetLineNumber() - script->line_offset(), 0);
9451 486 : int column = location.GetColumnNumber();
9452 486 : if (line == 0) {
9453 420 : column = std::max(0, column - script->column_offset());
9454 : }
9455 :
9456 486 : i::Script::InitLineEnds(script);
9457 486 : CHECK(script->line_ends()->IsFixedArray());
9458 : i::Handle<i::FixedArray> line_ends = i::Handle<i::FixedArray>::cast(
9459 : i::handle(script->line_ends(), script->GetIsolate()));
9460 486 : CHECK(line_ends->length());
9461 486 : if (line >= line_ends->length())
9462 180 : return GetSmiValue(line_ends, line_ends->length() - 1);
9463 396 : int line_offset = GetSmiValue(line_ends, line);
9464 606 : if (line == 0) return std::min(column, line_offset);
9465 186 : int prev_line_offset = GetSmiValue(line_ends, line - 1);
9466 372 : return std::min(prev_line_offset + column + 1, line_offset);
9467 : }
9468 :
9469 54 : v8::debug::Location debug::Script::GetSourceLocation(int offset) const {
9470 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9471 54 : if (script->type() == i::Script::TYPE_WASM) {
9472 : // TODO(clemensh): Return the proper thing for wasm.
9473 0 : return v8::debug::Location();
9474 : }
9475 : i::Script::PositionInfo info;
9476 54 : i::Script::GetPositionInfo(script, offset, &info, i::Script::WITH_OFFSET);
9477 54 : return debug::Location(info.line, info.column);
9478 : }
9479 :
9480 56 : debug::WasmScript* debug::WasmScript::Cast(debug::Script* script) {
9481 56 : CHECK(script->IsWasm());
9482 56 : return static_cast<WasmScript*>(script);
9483 : }
9484 :
9485 156 : int debug::WasmScript::NumFunctions() const {
9486 : i::DisallowHeapAllocation no_gc;
9487 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9488 : DCHECK_EQ(i::Script::TYPE_WASM, script->type());
9489 : i::WasmCompiledModule* compiled_module =
9490 : i::WasmCompiledModule::cast(script->wasm_compiled_module());
9491 : DCHECK_GE(i::kMaxInt, compiled_module->module()->functions.size());
9492 312 : return static_cast<int>(compiled_module->module()->functions.size());
9493 : }
9494 :
9495 156 : int debug::WasmScript::NumImportedFunctions() const {
9496 : i::DisallowHeapAllocation no_gc;
9497 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9498 : DCHECK_EQ(i::Script::TYPE_WASM, script->type());
9499 : i::WasmCompiledModule* compiled_module =
9500 : i::WasmCompiledModule::cast(script->wasm_compiled_module());
9501 : DCHECK_GE(i::kMaxInt, compiled_module->module()->num_imported_functions);
9502 156 : return static_cast<int>(compiled_module->module()->num_imported_functions);
9503 : }
9504 :
9505 6 : std::pair<int, int> debug::WasmScript::GetFunctionRange(
9506 : int function_index) const {
9507 : i::DisallowHeapAllocation no_gc;
9508 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9509 : DCHECK_EQ(i::Script::TYPE_WASM, script->type());
9510 : i::WasmCompiledModule* compiled_module =
9511 : i::WasmCompiledModule::cast(script->wasm_compiled_module());
9512 : DCHECK_LE(0, function_index);
9513 : DCHECK_GT(compiled_module->module()->functions.size(), function_index);
9514 : i::wasm::WasmFunction& func =
9515 6 : compiled_module->module()->functions[function_index];
9516 : DCHECK_GE(i::kMaxInt, func.code_start_offset);
9517 : DCHECK_GE(i::kMaxInt, func.code_end_offset);
9518 : return std::make_pair(static_cast<int>(func.code_start_offset),
9519 6 : static_cast<int>(func.code_end_offset));
9520 : }
9521 :
9522 100 : debug::WasmDisassembly debug::WasmScript::DisassembleFunction(
9523 : int function_index) const {
9524 : i::DisallowHeapAllocation no_gc;
9525 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9526 : DCHECK_EQ(i::Script::TYPE_WASM, script->type());
9527 : i::WasmCompiledModule* compiled_module =
9528 : i::WasmCompiledModule::cast(script->wasm_compiled_module());
9529 100 : return compiled_module->DisassembleFunction(function_index);
9530 : }
9531 :
9532 44500 : debug::Location::Location(int line_number, int column_number)
9533 44500 : : line_number_(line_number), column_number_(column_number) {
9534 44500 : CHECK(line_number >= 0);
9535 44500 : CHECK(column_number >= 0);
9536 44500 : }
9537 :
9538 270 : debug::Location::Location()
9539 : : line_number_(v8::Function::kLineOffsetNotFound),
9540 270 : column_number_(v8::Function::kLineOffsetNotFound) {}
9541 :
9542 10731 : int debug::Location::GetLineNumber() const {
9543 10731 : CHECK(line_number_ >= 0);
9544 10731 : return line_number_;
9545 : }
9546 :
9547 8586 : int debug::Location::GetColumnNumber() const {
9548 8586 : CHECK(column_number_ >= 0);
9549 8586 : return column_number_;
9550 : }
9551 :
9552 289 : bool debug::Location::IsEmpty() const {
9553 769 : return line_number_ == -1 && column_number_ == -1;
9554 : }
9555 :
9556 4345 : void debug::GetLoadedScripts(v8::Isolate* v8_isolate,
9557 : PersistentValueVector<debug::Script>& scripts) {
9558 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9559 : ENTER_V8(isolate);
9560 : // TODO(kozyatinskiy): remove this GC once tests are dealt with.
9561 : isolate->heap()->CollectAllGarbage(i::Heap::kFinalizeIncrementalMarkingMask,
9562 4345 : i::GarbageCollectionReason::kDebugger);
9563 : {
9564 : i::DisallowHeapAllocation no_gc;
9565 4345 : i::Script::Iterator iterator(isolate);
9566 : i::Script* script;
9567 116496 : while ((script = iterator.Next()) != nullptr) {
9568 107806 : if (!script->IsUserJavaScript()) continue;
9569 12106 : if (script->HasValidSource()) {
9570 : i::HandleScope handle_scope(isolate);
9571 : i::Handle<i::Script> script_handle(script, isolate);
9572 12106 : scripts.Append(ToApiHandle<Script>(script_handle));
9573 : }
9574 : }
9575 : }
9576 4345 : }
9577 :
9578 8594 : MaybeLocal<UnboundScript> debug::CompileInspectorScript(Isolate* v8_isolate,
9579 : Local<String> source) {
9580 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9581 25782 : PREPARE_FOR_DEBUG_INTERFACE_EXECUTION_WITH_ISOLATE(isolate, UnboundScript);
9582 8594 : i::ScriptData* script_data = NULL;
9583 8594 : i::Handle<i::String> str = Utils::OpenHandle(*source);
9584 : i::Handle<i::SharedFunctionInfo> result;
9585 : {
9586 : ScriptOriginOptions origin_options;
9587 : result = i::Compiler::GetSharedFunctionInfoForScript(
9588 : str, i::Handle<i::Object>(), 0, 0, origin_options,
9589 : i::Handle<i::Object>(), isolate->native_context(), NULL, &script_data,
9590 : ScriptCompiler::kNoCompileOptions,
9591 : i::FLAG_expose_inspector_scripts ? i::NOT_NATIVES_CODE
9592 17188 : : i::INSPECTOR_CODE);
9593 : has_pending_exception = result.is_null();
9594 8594 : RETURN_ON_FAILED_EXECUTION(UnboundScript);
9595 : }
9596 8594 : RETURN_ESCAPED(ToApiHandle<UnboundScript>(result));
9597 : }
9598 :
9599 8742 : void debug::SetDebugDelegate(Isolate* v8_isolate,
9600 : debug::DebugDelegate* delegate) {
9601 8742 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9602 : ENTER_V8(isolate);
9603 8742 : isolate->debug()->SetDebugDelegate(delegate, false);
9604 8742 : }
9605 :
9606 73324 : void debug::ResetBlackboxedStateCache(Isolate* v8_isolate,
9607 : v8::Local<debug::Script> script) {
9608 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9609 : ENTER_V8(isolate);
9610 : i::DisallowHeapAllocation no_gc;
9611 73324 : i::SharedFunctionInfo::ScriptIterator iter(Utils::OpenHandle(*script));
9612 1128257 : while (i::SharedFunctionInfo* info = iter.Next()) {
9613 1054933 : info->set_computed_debug_is_blackboxed(false);
9614 : }
9615 73324 : }
9616 :
9617 7344 : int debug::EstimatedValueSize(Isolate* v8_isolate, v8::Local<v8::Value> value) {
9618 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9619 : ENTER_V8(isolate);
9620 : i::Handle<i::Object> object = Utils::OpenHandle(*value);
9621 7344 : if (object->IsSmi()) return i::kPointerSize;
9622 6792 : CHECK(object->IsHeapObject());
9623 6792 : return i::Handle<i::HeapObject>::cast(object)->Size();
9624 : }
9625 :
9626 106344 : v8::MaybeLocal<v8::Array> debug::EntriesPreview(Isolate* v8_isolate,
9627 : v8::Local<v8::Value> value,
9628 : bool* is_key_value) {
9629 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9630 : ENTER_V8(isolate);
9631 106344 : if (value->IsMap()) {
9632 44 : *is_key_value = true;
9633 44 : return value.As<Map>()->AsArray();
9634 : }
9635 106300 : if (value->IsSet()) {
9636 48 : *is_key_value = false;
9637 48 : return value.As<Set>()->AsArray();
9638 : }
9639 :
9640 : i::Handle<i::Object> object = Utils::OpenHandle(*value);
9641 106252 : if (object->IsJSWeakCollection()) {
9642 36 : *is_key_value = object->IsJSWeakMap();
9643 : return Utils::ToLocal(i::JSWeakCollection::GetEntries(
9644 36 : i::Handle<i::JSWeakCollection>::cast(object), 0));
9645 : }
9646 106216 : if (object->IsJSMapIterator()) {
9647 : i::Handle<i::JSMapIterator> iterator =
9648 : i::Handle<i::JSMapIterator>::cast(object);
9649 : int iterator_kind = i::Smi::cast(iterator->kind())->value();
9650 84 : *is_key_value = iterator_kind == i::JSMapIterator::kKindEntries;
9651 84 : if (!iterator->HasMore()) return v8::Array::New(v8_isolate);
9652 : return Utils::ToLocal(MapAsArray(isolate, iterator->table(),
9653 : i::Smi::cast(iterator->index())->value(),
9654 78 : iterator_kind));
9655 : }
9656 106132 : if (object->IsJSSetIterator()) {
9657 : i::Handle<i::JSSetIterator> it = i::Handle<i::JSSetIterator>::cast(object);
9658 42 : *is_key_value = false;
9659 42 : if (!it->HasMore()) return v8::Array::New(v8_isolate);
9660 : return Utils::ToLocal(
9661 36 : SetAsArray(isolate, it->table(), i::Smi::cast(it->index())->value()));
9662 : }
9663 106090 : return v8::MaybeLocal<v8::Array>();
9664 : }
9665 :
9666 20545 : Local<Function> debug::GetBuiltin(Isolate* v8_isolate, Builtin builtin) {
9667 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9668 : ENTER_V8(isolate);
9669 : i::HandleScope handle_scope(isolate);
9670 : i::Builtins::Name name;
9671 20545 : switch (builtin) {
9672 : case kObjectKeys:
9673 : name = i::Builtins::kObjectKeys;
9674 4109 : break;
9675 : case kObjectGetPrototypeOf:
9676 : name = i::Builtins::kObjectGetPrototypeOf;
9677 4109 : break;
9678 : case kObjectGetOwnPropertyDescriptor:
9679 : name = i::Builtins::kObjectGetOwnPropertyDescriptor;
9680 4109 : break;
9681 : case kObjectGetOwnPropertyNames:
9682 : name = i::Builtins::kObjectGetOwnPropertyNames;
9683 4109 : break;
9684 : case kObjectGetOwnPropertySymbols:
9685 : name = i::Builtins::kObjectGetOwnPropertySymbols;
9686 4109 : break;
9687 : }
9688 : i::Handle<i::Code> call_code(isolate->builtins()->builtin(name));
9689 : i::Handle<i::JSFunction> fun =
9690 : isolate->factory()->NewFunctionWithoutPrototype(
9691 20545 : isolate->factory()->empty_string(), call_code, false);
9692 : fun->shared()->DontAdaptArguments();
9693 41090 : return Utils::ToLocal(handle_scope.CloseAndEscape(fun));
9694 : }
9695 :
9696 38231 : void debug::SetConsoleDelegate(Isolate* v8_isolate, ConsoleDelegate* delegate) {
9697 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9698 : ENTER_V8(isolate);
9699 : isolate->set_console_delegate(delegate);
9700 38231 : }
9701 :
9702 318 : debug::ConsoleCallArguments::ConsoleCallArguments(
9703 : const v8::FunctionCallbackInfo<v8::Value>& info)
9704 318 : : v8::FunctionCallbackInfo<v8::Value>(nullptr, info.values_, info.length_) {
9705 318 : }
9706 :
9707 8105 : debug::ConsoleCallArguments::ConsoleCallArguments(
9708 : internal::BuiltinArguments& args)
9709 : : v8::FunctionCallbackInfo<v8::Value>(nullptr, &args[0] - 1,
9710 24315 : args.length() - 1) {}
9711 :
9712 6378 : int debug::GetStackFrameId(v8::Local<v8::StackFrame> frame) {
9713 6378 : return Utils::OpenHandle(*frame)->id();
9714 : }
9715 :
9716 42 : MaybeLocal<debug::Script> debug::GeneratorObject::Script() {
9717 : i::Handle<i::JSGeneratorObject> obj = Utils::OpenHandle(this);
9718 : i::Object* maybe_script = obj->function()->shared()->script();
9719 42 : if (!maybe_script->IsScript()) return MaybeLocal<debug::Script>();
9720 : i::Handle<i::Script> script(i::Script::cast(maybe_script), obj->GetIsolate());
9721 42 : return ToApiHandle<debug::Script>(script);
9722 : }
9723 :
9724 12 : Local<Function> debug::GeneratorObject::Function() {
9725 : i::Handle<i::JSGeneratorObject> obj = Utils::OpenHandle(this);
9726 12 : return Utils::ToLocal(handle(obj->function()));
9727 : }
9728 :
9729 42 : debug::Location debug::GeneratorObject::SuspendedLocation() {
9730 : i::Handle<i::JSGeneratorObject> obj = Utils::OpenHandle(this);
9731 42 : CHECK(obj->is_suspended());
9732 : i::Object* maybe_script = obj->function()->shared()->script();
9733 42 : if (!maybe_script->IsScript()) return debug::Location();
9734 : i::Handle<i::Script> script(i::Script::cast(maybe_script), obj->GetIsolate());
9735 : i::Script::PositionInfo info;
9736 : i::Script::GetPositionInfo(script, obj->source_position(), &info,
9737 42 : i::Script::WITH_OFFSET);
9738 42 : return debug::Location(info.line, info.column);
9739 : }
9740 :
9741 54 : bool debug::GeneratorObject::IsSuspended() {
9742 54 : return Utils::OpenHandle(this)->is_suspended();
9743 : }
9744 :
9745 54 : v8::Local<debug::GeneratorObject> debug::GeneratorObject::Cast(
9746 : v8::Local<v8::Value> value) {
9747 54 : CHECK(value->IsGeneratorObject());
9748 54 : return ToApiHandle<debug::GeneratorObject>(Utils::OpenHandle(*value));
9749 : }
9750 :
9751 535 : Local<String> CpuProfileNode::GetFunctionName() const {
9752 1070 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9753 : i::Isolate* isolate = node->isolate();
9754 1070 : const i::CodeEntry* entry = node->entry();
9755 : i::Handle<i::String> name =
9756 535 : isolate->factory()->InternalizeUtf8String(entry->name());
9757 535 : if (!entry->has_name_prefix()) {
9758 : return ToApiHandle<String>(name);
9759 : } else {
9760 : // We do not expect this to fail. Change this if it does.
9761 : i::Handle<i::String> cons = isolate->factory()->NewConsString(
9762 : isolate->factory()->InternalizeUtf8String(entry->name_prefix()),
9763 72 : name).ToHandleChecked();
9764 : return ToApiHandle<String>(cons);
9765 : }
9766 : }
9767 :
9768 300 : int debug::Coverage::FunctionData::StartOffset() const {
9769 300 : return function_->start;
9770 : }
9771 300 : int debug::Coverage::FunctionData::EndOffset() const { return function_->end; }
9772 300 : uint32_t debug::Coverage::FunctionData::Count() const {
9773 300 : return function_->count;
9774 : }
9775 :
9776 288 : MaybeLocal<String> debug::Coverage::FunctionData::Name() const {
9777 288 : return ToApiHandle<String>(function_->name);
9778 : }
9779 :
9780 126 : Local<debug::Script> debug::Coverage::ScriptData::GetScript() const {
9781 126 : return ToApiHandle<debug::Script>(script_->script);
9782 : }
9783 :
9784 414 : size_t debug::Coverage::ScriptData::FunctionCount() const {
9785 828 : return script_->functions.size();
9786 : }
9787 :
9788 300 : debug::Coverage::FunctionData debug::Coverage::ScriptData::GetFunctionData(
9789 : size_t i) const {
9790 600 : return FunctionData(&script_->functions.at(i));
9791 : }
9792 :
9793 204 : debug::Coverage::~Coverage() { delete coverage_; }
9794 :
9795 444 : size_t debug::Coverage::ScriptCount() const { return coverage_->size(); }
9796 :
9797 126 : debug::Coverage::ScriptData debug::Coverage::GetScriptData(size_t i) const {
9798 252 : return ScriptData(&coverage_->at(i));
9799 : }
9800 :
9801 66 : debug::Coverage debug::Coverage::CollectPrecise(Isolate* isolate) {
9802 : return Coverage(
9803 132 : i::Coverage::CollectPrecise(reinterpret_cast<i::Isolate*>(isolate)));
9804 : }
9805 :
9806 36 : debug::Coverage debug::Coverage::CollectBestEffort(Isolate* isolate) {
9807 : return Coverage(
9808 72 : i::Coverage::CollectBestEffort(reinterpret_cast<i::Isolate*>(isolate)));
9809 : }
9810 :
9811 182 : void debug::Coverage::SelectMode(Isolate* isolate, debug::Coverage::Mode mode) {
9812 182 : i::Coverage::SelectMode(reinterpret_cast<i::Isolate*>(isolate), mode);
9813 182 : }
9814 :
9815 24 : const char* CpuProfileNode::GetFunctionNameStr() const {
9816 24 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9817 24 : return node->entry()->name();
9818 : }
9819 :
9820 149 : int CpuProfileNode::GetScriptId() const {
9821 149 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9822 149 : const i::CodeEntry* entry = node->entry();
9823 149 : return entry->script_id();
9824 : }
9825 :
9826 135 : Local<String> CpuProfileNode::GetScriptResourceName() const {
9827 270 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9828 : i::Isolate* isolate = node->isolate();
9829 : return ToApiHandle<String>(isolate->factory()->InternalizeUtf8String(
9830 135 : node->entry()->resource_name()));
9831 : }
9832 :
9833 24 : const char* CpuProfileNode::GetScriptResourceNameStr() const {
9834 24 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9835 24 : return node->entry()->resource_name();
9836 : }
9837 :
9838 135 : int CpuProfileNode::GetLineNumber() const {
9839 135 : return reinterpret_cast<const i::ProfileNode*>(this)->entry()->line_number();
9840 : }
9841 :
9842 :
9843 135 : int CpuProfileNode::GetColumnNumber() const {
9844 : return reinterpret_cast<const i::ProfileNode*>(this)->
9845 135 : entry()->column_number();
9846 : }
9847 :
9848 :
9849 111 : unsigned int CpuProfileNode::GetHitLineCount() const {
9850 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9851 111 : return node->GetHitLineCount();
9852 : }
9853 :
9854 :
9855 24 : bool CpuProfileNode::GetLineTicks(LineTick* entries,
9856 : unsigned int length) const {
9857 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9858 24 : return node->GetLineTicks(entries, length);
9859 : }
9860 :
9861 :
9862 118 : const char* CpuProfileNode::GetBailoutReason() const {
9863 118 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9864 118 : return node->entry()->bailout_reason();
9865 : }
9866 :
9867 :
9868 123 : unsigned CpuProfileNode::GetHitCount() const {
9869 123 : return reinterpret_cast<const i::ProfileNode*>(this)->self_ticks();
9870 : }
9871 :
9872 :
9873 0 : unsigned CpuProfileNode::GetCallUid() const {
9874 0 : return reinterpret_cast<const i::ProfileNode*>(this)->function_id();
9875 : }
9876 :
9877 :
9878 290 : unsigned CpuProfileNode::GetNodeId() const {
9879 290 : return reinterpret_cast<const i::ProfileNode*>(this)->id();
9880 : }
9881 :
9882 :
9883 520 : int CpuProfileNode::GetChildrenCount() const {
9884 520 : return reinterpret_cast<const i::ProfileNode*>(this)->children()->length();
9885 : }
9886 :
9887 :
9888 554 : const CpuProfileNode* CpuProfileNode::GetChild(int index) const {
9889 : const i::ProfileNode* child =
9890 554 : reinterpret_cast<const i::ProfileNode*>(this)->children()->at(index);
9891 554 : return reinterpret_cast<const CpuProfileNode*>(child);
9892 : }
9893 :
9894 :
9895 0 : const std::vector<CpuProfileDeoptInfo>& CpuProfileNode::GetDeoptInfos() const {
9896 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9897 0 : return node->deopt_infos();
9898 : }
9899 :
9900 :
9901 142 : void CpuProfile::Delete() {
9902 142 : i::CpuProfile* profile = reinterpret_cast<i::CpuProfile*>(this);
9903 : i::CpuProfiler* profiler = profile->cpu_profiler();
9904 : DCHECK(profiler != nullptr);
9905 142 : profiler->DeleteProfile(profile);
9906 142 : }
9907 :
9908 :
9909 0 : Local<String> CpuProfile::GetTitle() const {
9910 0 : const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
9911 0 : i::Isolate* isolate = profile->top_down()->isolate();
9912 : return ToApiHandle<String>(isolate->factory()->InternalizeUtf8String(
9913 0 : profile->title()));
9914 : }
9915 :
9916 :
9917 138 : const CpuProfileNode* CpuProfile::GetTopDownRoot() const {
9918 : const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
9919 138 : return reinterpret_cast<const CpuProfileNode*>(profile->top_down()->root());
9920 : }
9921 :
9922 :
9923 9162 : const CpuProfileNode* CpuProfile::GetSample(int index) const {
9924 : const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
9925 9162 : return reinterpret_cast<const CpuProfileNode*>(profile->sample(index));
9926 : }
9927 :
9928 :
9929 9162 : int64_t CpuProfile::GetSampleTimestamp(int index) const {
9930 : const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
9931 : return (profile->sample_timestamp(index) - base::TimeTicks())
9932 9162 : .InMicroseconds();
9933 : }
9934 :
9935 :
9936 80 : int64_t CpuProfile::GetStartTime() const {
9937 : const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
9938 80 : return (profile->start_time() - base::TimeTicks()).InMicroseconds();
9939 : }
9940 :
9941 :
9942 46 : int64_t CpuProfile::GetEndTime() const {
9943 : const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
9944 46 : return (profile->end_time() - base::TimeTicks()).InMicroseconds();
9945 : }
9946 :
9947 :
9948 9140 : int CpuProfile::GetSamplesCount() const {
9949 9140 : return reinterpret_cast<const i::CpuProfile*>(this)->samples_count();
9950 : }
9951 :
9952 224 : CpuProfiler* CpuProfiler::New(Isolate* isolate) {
9953 : return reinterpret_cast<CpuProfiler*>(
9954 224 : new i::CpuProfiler(reinterpret_cast<i::Isolate*>(isolate)));
9955 : }
9956 :
9957 224 : void CpuProfiler::Dispose() { delete reinterpret_cast<i::CpuProfiler*>(this); }
9958 :
9959 54 : void CpuProfiler::SetSamplingInterval(int us) {
9960 : DCHECK_GE(us, 0);
9961 : return reinterpret_cast<i::CpuProfiler*>(this)->set_sampling_interval(
9962 54 : base::TimeDelta::FromMicroseconds(us));
9963 : }
9964 :
9965 6 : void CpuProfiler::CollectSample() {
9966 6 : reinterpret_cast<i::CpuProfiler*>(this)->CollectSample();
9967 6 : }
9968 :
9969 464 : void CpuProfiler::StartProfiling(Local<String> title, bool record_samples) {
9970 : reinterpret_cast<i::CpuProfiler*>(this)->StartProfiling(
9971 928 : *Utils::OpenHandle(*title), record_samples);
9972 464 : }
9973 :
9974 :
9975 187 : CpuProfile* CpuProfiler::StopProfiling(Local<String> title) {
9976 : return reinterpret_cast<CpuProfile*>(
9977 : reinterpret_cast<i::CpuProfiler*>(this)->StopProfiling(
9978 187 : *Utils::OpenHandle(*title)));
9979 : }
9980 :
9981 :
9982 44 : void CpuProfiler::SetIdle(bool is_idle) {
9983 44 : i::CpuProfiler* profiler = reinterpret_cast<i::CpuProfiler*>(this);
9984 56 : i::Isolate* isolate = profiler->isolate();
9985 44 : if (!isolate->is_profiling()) return;
9986 : v8::StateTag state = isolate->current_vm_state();
9987 : DCHECK(state == v8::EXTERNAL || state == v8::IDLE);
9988 12 : if (isolate->js_entry_sp() != NULL) return;
9989 12 : if (is_idle) {
9990 : isolate->set_current_vm_state(v8::IDLE);
9991 6 : } else if (state == v8::IDLE) {
9992 : isolate->set_current_vm_state(v8::EXTERNAL);
9993 : }
9994 : }
9995 :
9996 :
9997 : static i::HeapGraphEdge* ToInternal(const HeapGraphEdge* edge) {
9998 : return const_cast<i::HeapGraphEdge*>(
9999 : reinterpret_cast<const i::HeapGraphEdge*>(edge));
10000 : }
10001 :
10002 :
10003 7260866 : HeapGraphEdge::Type HeapGraphEdge::GetType() const {
10004 14521732 : return static_cast<HeapGraphEdge::Type>(ToInternal(this)->type());
10005 : }
10006 :
10007 :
10008 7202939 : Local<Value> HeapGraphEdge::GetName() const {
10009 14405878 : i::HeapGraphEdge* edge = ToInternal(this);
10010 : i::Isolate* isolate = edge->isolate();
10011 7202939 : switch (edge->type()) {
10012 : case i::HeapGraphEdge::kContextVariable:
10013 : case i::HeapGraphEdge::kInternal:
10014 : case i::HeapGraphEdge::kProperty:
10015 : case i::HeapGraphEdge::kShortcut:
10016 : case i::HeapGraphEdge::kWeak:
10017 : return ToApiHandle<String>(
10018 7201799 : isolate->factory()->InternalizeUtf8String(edge->name()));
10019 : case i::HeapGraphEdge::kElement:
10020 : case i::HeapGraphEdge::kHidden:
10021 : return ToApiHandle<Number>(
10022 1140 : isolate->factory()->NewNumberFromInt(edge->index()));
10023 0 : default: UNREACHABLE();
10024 : }
10025 : return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
10026 : }
10027 :
10028 :
10029 372 : const HeapGraphNode* HeapGraphEdge::GetFromNode() const {
10030 : const i::HeapEntry* from = ToInternal(this)->from();
10031 372 : return reinterpret_cast<const HeapGraphNode*>(from);
10032 : }
10033 :
10034 :
10035 963347 : const HeapGraphNode* HeapGraphEdge::GetToNode() const {
10036 963347 : const i::HeapEntry* to = ToInternal(this)->to();
10037 963347 : return reinterpret_cast<const HeapGraphNode*>(to);
10038 : }
10039 :
10040 :
10041 : static i::HeapEntry* ToInternal(const HeapGraphNode* entry) {
10042 : return const_cast<i::HeapEntry*>(
10043 : reinterpret_cast<const i::HeapEntry*>(entry));
10044 : }
10045 :
10046 :
10047 468 : HeapGraphNode::Type HeapGraphNode::GetType() const {
10048 468 : return static_cast<HeapGraphNode::Type>(ToInternal(this)->type());
10049 : }
10050 :
10051 :
10052 2316 : Local<String> HeapGraphNode::GetName() const {
10053 2316 : i::Isolate* isolate = ToInternal(this)->isolate();
10054 : return ToApiHandle<String>(
10055 2316 : isolate->factory()->InternalizeUtf8String(ToInternal(this)->name()));
10056 : }
10057 :
10058 :
10059 749562 : SnapshotObjectId HeapGraphNode::GetId() const {
10060 749562 : return ToInternal(this)->id();
10061 : }
10062 :
10063 :
10064 24 : size_t HeapGraphNode::GetShallowSize() const {
10065 24 : return ToInternal(this)->self_size();
10066 : }
10067 :
10068 :
10069 88485 : int HeapGraphNode::GetChildrenCount() const {
10070 88485 : return ToInternal(this)->children_count();
10071 : }
10072 :
10073 :
10074 7720380 : const HeapGraphEdge* HeapGraphNode::GetChild(int index) const {
10075 7720380 : return reinterpret_cast<const HeapGraphEdge*>(ToInternal(this)->child(index));
10076 : }
10077 :
10078 :
10079 : static i::HeapSnapshot* ToInternal(const HeapSnapshot* snapshot) {
10080 : return const_cast<i::HeapSnapshot*>(
10081 : reinterpret_cast<const i::HeapSnapshot*>(snapshot));
10082 : }
10083 :
10084 :
10085 30 : void HeapSnapshot::Delete() {
10086 84 : i::Isolate* isolate = ToInternal(this)->profiler()->isolate();
10087 30 : if (isolate->heap_profiler()->GetSnapshotsCount() > 1) {
10088 6 : ToInternal(this)->Delete();
10089 : } else {
10090 : // If this is the last snapshot, clean up all accessory data as well.
10091 24 : isolate->heap_profiler()->DeleteAllSnapshots();
10092 : }
10093 30 : }
10094 :
10095 :
10096 526 : const HeapGraphNode* HeapSnapshot::GetRoot() const {
10097 526 : return reinterpret_cast<const HeapGraphNode*>(ToInternal(this)->root());
10098 : }
10099 :
10100 :
10101 248130 : const HeapGraphNode* HeapSnapshot::GetNodeById(SnapshotObjectId id) const {
10102 : return reinterpret_cast<const HeapGraphNode*>(
10103 248130 : ToInternal(this)->GetEntryById(id));
10104 : }
10105 :
10106 :
10107 12 : int HeapSnapshot::GetNodesCount() const {
10108 12 : return ToInternal(this)->entries().length();
10109 : }
10110 :
10111 :
10112 109120 : const HeapGraphNode* HeapSnapshot::GetNode(int index) const {
10113 : return reinterpret_cast<const HeapGraphNode*>(
10114 109120 : &ToInternal(this)->entries().at(index));
10115 : }
10116 :
10117 :
10118 24 : SnapshotObjectId HeapSnapshot::GetMaxSnapshotJSObjectId() const {
10119 24 : return ToInternal(this)->max_snapshot_js_object_id();
10120 : }
10121 :
10122 :
10123 24 : void HeapSnapshot::Serialize(OutputStream* stream,
10124 : HeapSnapshot::SerializationFormat format) const {
10125 : Utils::ApiCheck(format == kJSON,
10126 : "v8::HeapSnapshot::Serialize",
10127 : "Unknown serialization format");
10128 24 : Utils::ApiCheck(stream->GetChunkSize() > 0,
10129 : "v8::HeapSnapshot::Serialize",
10130 : "Invalid stream chunk size");
10131 : i::HeapSnapshotJSONSerializer serializer(ToInternal(this));
10132 24 : serializer.Serialize(stream);
10133 24 : }
10134 :
10135 :
10136 : // static
10137 : STATIC_CONST_MEMBER_DEFINITION const SnapshotObjectId
10138 : HeapProfiler::kUnknownObjectId;
10139 :
10140 :
10141 138 : int HeapProfiler::GetSnapshotCount() {
10142 138 : return reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshotsCount();
10143 : }
10144 :
10145 :
10146 36 : const HeapSnapshot* HeapProfiler::GetHeapSnapshot(int index) {
10147 : return reinterpret_cast<const HeapSnapshot*>(
10148 36 : reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshot(index));
10149 : }
10150 :
10151 :
10152 186 : SnapshotObjectId HeapProfiler::GetObjectId(Local<Value> value) {
10153 186 : i::Handle<i::Object> obj = Utils::OpenHandle(*value);
10154 186 : return reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshotObjectId(obj);
10155 : }
10156 :
10157 :
10158 168 : Local<Value> HeapProfiler::FindObjectById(SnapshotObjectId id) {
10159 : i::Handle<i::Object> obj =
10160 168 : reinterpret_cast<i::HeapProfiler*>(this)->FindHeapObjectById(id);
10161 168 : if (obj.is_null()) return Local<Value>();
10162 : return Utils::ToLocal(obj);
10163 : }
10164 :
10165 :
10166 4705 : void HeapProfiler::ClearObjectIds() {
10167 4705 : reinterpret_cast<i::HeapProfiler*>(this)->ClearHeapObjectMap();
10168 4705 : }
10169 :
10170 :
10171 365 : const HeapSnapshot* HeapProfiler::TakeHeapSnapshot(
10172 : ActivityControl* control, ObjectNameResolver* resolver) {
10173 : return reinterpret_cast<const HeapSnapshot*>(
10174 : reinterpret_cast<i::HeapProfiler*>(this)
10175 365 : ->TakeSnapshot(control, resolver));
10176 : }
10177 :
10178 :
10179 48 : void HeapProfiler::StartTrackingHeapObjects(bool track_allocations) {
10180 : reinterpret_cast<i::HeapProfiler*>(this)->StartHeapObjectsTracking(
10181 48 : track_allocations);
10182 48 : }
10183 :
10184 :
10185 4747 : void HeapProfiler::StopTrackingHeapObjects() {
10186 4747 : reinterpret_cast<i::HeapProfiler*>(this)->StopHeapObjectsTracking();
10187 4747 : }
10188 :
10189 :
10190 66 : SnapshotObjectId HeapProfiler::GetHeapStats(OutputStream* stream,
10191 : int64_t* timestamp_us) {
10192 : i::HeapProfiler* heap_profiler = reinterpret_cast<i::HeapProfiler*>(this);
10193 66 : return heap_profiler->PushHeapObjectsStats(stream, timestamp_us);
10194 : }
10195 :
10196 36 : bool HeapProfiler::StartSamplingHeapProfiler(uint64_t sample_interval,
10197 : int stack_depth,
10198 : SamplingFlags flags) {
10199 : return reinterpret_cast<i::HeapProfiler*>(this)->StartSamplingHeapProfiler(
10200 36 : sample_interval, stack_depth, flags);
10201 : }
10202 :
10203 :
10204 36 : void HeapProfiler::StopSamplingHeapProfiler() {
10205 36 : reinterpret_cast<i::HeapProfiler*>(this)->StopSamplingHeapProfiler();
10206 36 : }
10207 :
10208 :
10209 42 : AllocationProfile* HeapProfiler::GetAllocationProfile() {
10210 42 : return reinterpret_cast<i::HeapProfiler*>(this)->GetAllocationProfile();
10211 : }
10212 :
10213 :
10214 30 : void HeapProfiler::DeleteAllHeapSnapshots() {
10215 30 : reinterpret_cast<i::HeapProfiler*>(this)->DeleteAllSnapshots();
10216 30 : }
10217 :
10218 :
10219 12 : void HeapProfiler::SetWrapperClassInfoProvider(uint16_t class_id,
10220 : WrapperInfoCallback callback) {
10221 : reinterpret_cast<i::HeapProfiler*>(this)->DefineWrapperClass(class_id,
10222 12 : callback);
10223 12 : }
10224 :
10225 0 : void HeapProfiler::SetGetRetainerInfosCallback(
10226 : GetRetainerInfosCallback callback) {
10227 : reinterpret_cast<i::HeapProfiler*>(this)->SetGetRetainerInfosCallback(
10228 0 : callback);
10229 0 : }
10230 :
10231 0 : size_t HeapProfiler::GetProfilerMemorySize() {
10232 : return reinterpret_cast<i::HeapProfiler*>(this)->
10233 0 : GetMemorySizeUsedByProfiler();
10234 : }
10235 :
10236 : v8::Testing::StressType internal::Testing::stress_type_ =
10237 : v8::Testing::kStressTypeOpt;
10238 :
10239 :
10240 8061 : void Testing::SetStressRunType(Testing::StressType type) {
10241 : internal::Testing::set_stress_type(type);
10242 8061 : }
10243 :
10244 :
10245 8061 : int Testing::GetStressRuns() {
10246 80594 : if (internal::FLAG_stress_runs != 0) return internal::FLAG_stress_runs;
10247 : #ifdef DEBUG
10248 : // In debug mode the code runs much slower so stressing will only make two
10249 : // runs.
10250 : return 2;
10251 : #else
10252 : return 5;
10253 : #endif
10254 : }
10255 :
10256 :
10257 32238 : static void SetFlagsFromString(const char* flags) {
10258 : V8::SetFlagsFromString(flags, i::StrLength(flags));
10259 32238 : }
10260 :
10261 :
10262 40297 : void Testing::PrepareStressRun(int run) {
10263 : static const char* kLazyOptimizations =
10264 : "--prepare-always-opt "
10265 : "--max-inlined-source-size=999999 "
10266 : "--max-inlined-nodes=999999 "
10267 : "--max-inlined-nodes-cumulative=999999 "
10268 : "--noalways-opt";
10269 : static const char* kForcedOptimizations = "--always-opt";
10270 :
10271 : // If deoptimization stressed turn on frequent deoptimization. If no value
10272 : // is spefified through --deopt-every-n-times use a default default value.
10273 : static const char* kDeoptEvery13Times = "--deopt-every-n-times=13";
10274 40297 : if (internal::Testing::stress_type() == Testing::kStressTypeDeopt &&
10275 0 : internal::FLAG_deopt_every_n_times == 0) {
10276 0 : SetFlagsFromString(kDeoptEvery13Times);
10277 : }
10278 :
10279 : #ifdef DEBUG
10280 : // As stressing in debug mode only make two runs skip the deopt stressing
10281 : // here.
10282 : if (run == GetStressRuns() - 1) {
10283 : SetFlagsFromString(kForcedOptimizations);
10284 : } else {
10285 : SetFlagsFromString(kLazyOptimizations);
10286 : }
10287 : #else
10288 40297 : if (run == GetStressRuns() - 1) {
10289 8061 : SetFlagsFromString(kForcedOptimizations);
10290 32236 : } else if (run != GetStressRuns() - 2) {
10291 24177 : SetFlagsFromString(kLazyOptimizations);
10292 : }
10293 : #endif
10294 40297 : }
10295 :
10296 :
10297 8061 : void Testing::DeoptimizeAll(Isolate* isolate) {
10298 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
10299 : i::HandleScope scope(i_isolate);
10300 8061 : internal::Deoptimizer::DeoptimizeAll(i_isolate);
10301 8061 : }
10302 :
10303 :
10304 : namespace internal {
10305 :
10306 :
10307 6863 : void HandleScopeImplementer::FreeThreadResources() {
10308 6863 : Free();
10309 6863 : }
10310 :
10311 :
10312 22574 : char* HandleScopeImplementer::ArchiveThread(char* storage) {
10313 22574 : HandleScopeData* current = isolate_->handle_scope_data();
10314 22574 : handle_scope_data_ = *current;
10315 : MemCopy(storage, this, sizeof(*this));
10316 :
10317 : ResetAfterArchive();
10318 : current->Initialize();
10319 :
10320 22574 : return storage + ArchiveSpacePerThread();
10321 : }
10322 :
10323 :
10324 2314 : int HandleScopeImplementer::ArchiveSpacePerThread() {
10325 2314 : return sizeof(HandleScopeImplementer);
10326 : }
10327 :
10328 :
10329 22574 : char* HandleScopeImplementer::RestoreThread(char* storage) {
10330 : MemCopy(this, storage, sizeof(*this));
10331 22574 : *isolate_->handle_scope_data() = handle_scope_data_;
10332 22574 : return storage + ArchiveSpacePerThread();
10333 : }
10334 :
10335 540571 : void HandleScopeImplementer::IterateThis(RootVisitor* v) {
10336 : #ifdef DEBUG
10337 : bool found_block_before_deferred = false;
10338 : #endif
10339 : // Iterate over all handles in the blocks except for the last.
10340 667163 : for (int i = blocks()->length() - 2; i >= 0; --i) {
10341 126592 : Object** block = blocks()->at(i);
10342 126632 : if (last_handle_before_deferred_block_ != NULL &&
10343 80 : (last_handle_before_deferred_block_ <= &block[kHandleBlockSize]) &&
10344 : (last_handle_before_deferred_block_ >= block)) {
10345 : v->VisitRootPointers(Root::kHandleScope, block,
10346 40 : last_handle_before_deferred_block_);
10347 40 : DCHECK(!found_block_before_deferred);
10348 : #ifdef DEBUG
10349 : found_block_before_deferred = true;
10350 : #endif
10351 : } else {
10352 126552 : v->VisitRootPointers(Root::kHandleScope, block, &block[kHandleBlockSize]);
10353 : }
10354 : }
10355 :
10356 : DCHECK(last_handle_before_deferred_block_ == NULL ||
10357 : found_block_before_deferred);
10358 :
10359 : // Iterate over live handles in the last block (if any).
10360 540571 : if (!blocks()->is_empty()) {
10361 : v->VisitRootPointers(Root::kHandleScope, blocks()->last(),
10362 679694 : handle_scope_data_.next);
10363 : }
10364 :
10365 540571 : List<Context*>* context_lists[2] = { &saved_contexts_, &entered_contexts_};
10366 1621713 : for (unsigned i = 0; i < arraysize(context_lists); i++) {
10367 1081142 : if (context_lists[i]->is_empty()) continue;
10368 : Object** start = reinterpret_cast<Object**>(&context_lists[i]->first());
10369 : v->VisitRootPointers(Root::kHandleScope, start,
10370 668278 : start + context_lists[i]->length());
10371 : }
10372 540571 : if (microtask_context_) {
10373 : v->VisitRootPointer(Root::kHandleScope,
10374 1450 : reinterpret_cast<Object**>(µtask_context_));
10375 : }
10376 540571 : }
10377 :
10378 536522 : void HandleScopeImplementer::Iterate(RootVisitor* v) {
10379 536522 : HandleScopeData* current = isolate_->handle_scope_data();
10380 536522 : handle_scope_data_ = *current;
10381 536522 : IterateThis(v);
10382 536522 : }
10383 :
10384 4049 : char* HandleScopeImplementer::Iterate(RootVisitor* v, char* storage) {
10385 : HandleScopeImplementer* scope_implementer =
10386 : reinterpret_cast<HandleScopeImplementer*>(storage);
10387 4049 : scope_implementer->IterateThis(v);
10388 4049 : return storage + ArchiveSpacePerThread();
10389 : }
10390 :
10391 :
10392 150621 : DeferredHandles* HandleScopeImplementer::Detach(Object** prev_limit) {
10393 : DeferredHandles* deferred =
10394 150621 : new DeferredHandles(isolate()->handle_scope_data()->next, isolate());
10395 :
10396 452407 : while (!blocks_.is_empty()) {
10397 603572 : Object** block_start = blocks_.last();
10398 301786 : Object** block_limit = &block_start[kHandleBlockSize];
10399 : // We should not need to check for SealHandleScope here. Assert this.
10400 : DCHECK(prev_limit == block_limit ||
10401 : !(block_start <= prev_limit && prev_limit <= block_limit));
10402 301786 : if (prev_limit == block_limit) break;
10403 151165 : deferred->blocks_.Add(blocks_.last());
10404 151165 : blocks_.RemoveLast();
10405 : }
10406 :
10407 : // deferred->blocks_ now contains the blocks installed on the
10408 : // HandleScope stack since BeginDeferredScope was called, but in
10409 : // reverse order.
10410 :
10411 : DCHECK(prev_limit == NULL || !blocks_.is_empty());
10412 :
10413 : DCHECK(!blocks_.is_empty() && prev_limit != NULL);
10414 : DCHECK(last_handle_before_deferred_block_ != NULL);
10415 150621 : last_handle_before_deferred_block_ = NULL;
10416 150621 : return deferred;
10417 : }
10418 :
10419 :
10420 150621 : void HandleScopeImplementer::BeginDeferredScope() {
10421 : DCHECK(last_handle_before_deferred_block_ == NULL);
10422 150621 : last_handle_before_deferred_block_ = isolate()->handle_scope_data()->next;
10423 150621 : }
10424 :
10425 :
10426 150607 : DeferredHandles::~DeferredHandles() {
10427 301758 : isolate_->UnlinkDeferredHandles(this);
10428 :
10429 603516 : for (int i = 0; i < blocks_.length(); i++) {
10430 : #ifdef ENABLE_HANDLE_ZAPPING
10431 604060 : HandleScope::ZapRange(blocks_[i], &blocks_[i][kHandleBlockSize]);
10432 : #endif
10433 151151 : isolate_->handle_scope_implementer()->ReturnBlock(blocks_[i]);
10434 : }
10435 150607 : }
10436 :
10437 24065 : void DeferredHandles::Iterate(RootVisitor* v) {
10438 : DCHECK(!blocks_.is_empty());
10439 :
10440 : DCHECK((first_block_limit_ >= blocks_.first()) &&
10441 : (first_block_limit_ <= &(blocks_.first())[kHandleBlockSize]));
10442 :
10443 73221 : v->VisitRootPointers(Root::kHandleScope, blocks_.first(), first_block_limit_);
10444 :
10445 49156 : for (int i = 1; i < blocks_.length(); i++) {
10446 : v->VisitRootPointers(Root::kHandleScope, blocks_[i],
10447 1026 : &blocks_[i][kHandleBlockSize]);
10448 : }
10449 24065 : }
10450 :
10451 :
10452 936 : void InvokeAccessorGetterCallback(
10453 : v8::Local<v8::Name> property,
10454 : const v8::PropertyCallbackInfo<v8::Value>& info,
10455 : v8::AccessorNameGetterCallback getter) {
10456 : // Leaving JavaScript.
10457 : Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
10458 : RuntimeCallTimerScope timer(isolate,
10459 936 : &RuntimeCallStats::AccessorGetterCallback);
10460 : Address getter_address = reinterpret_cast<Address>(reinterpret_cast<intptr_t>(
10461 : getter));
10462 1872 : VMState<EXTERNAL> state(isolate);
10463 1872 : ExternalCallbackScope call_scope(isolate, getter_address);
10464 936 : getter(property, info);
10465 936 : }
10466 :
10467 :
10468 229 : void InvokeFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info,
10469 : v8::FunctionCallback callback) {
10470 : Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
10471 : RuntimeCallTimerScope timer(isolate,
10472 229 : &RuntimeCallStats::InvokeFunctionCallback);
10473 : Address callback_address =
10474 : reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
10475 458 : VMState<EXTERNAL> state(isolate);
10476 458 : ExternalCallbackScope call_scope(isolate, callback_address);
10477 229 : callback(info);
10478 229 : }
10479 :
10480 :
10481 : } // namespace internal
10482 : } // namespace v8
|